blob: 0326f053a390baa77d5eb6401af85b8d51dcbf9e [file] [log] [blame]
Colin Cross5e9a0862013-06-24 18:36:39 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
30 * This file is only used to provide backwards compatibility to property areas
31 * created by old versions of init, which occurs when an ota runs. The updater
32 * binary is compiled statically against the newest bionic, but the recovery
33 * ramdisk may be using an old version of init. This can all be removed once
34 * OTAs from pre-K versions are no longer supported.
35 */
36
37#include <string.h>
38#include <sys/atomics.h>
39
40#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
41#include <sys/_system_properties.h>
42
43#define TOC_NAME_LEN(toc) ((toc) >> 24)
44#define TOC_TO_INFO(area, toc) ((prop_info_compat*) (((char*) area) + ((toc) & 0xFFFFFF)))
45
46struct prop_area_compat {
47 unsigned volatile count;
48 unsigned volatile serial;
49 unsigned magic;
50 unsigned version;
Joshua J. Drake063a5722013-12-13 13:44:53 -060051 unsigned reserved[4];
Colin Cross5e9a0862013-06-24 18:36:39 -070052 unsigned toc[1];
53};
54
55typedef struct prop_area_compat prop_area_compat;
56
57struct prop_area;
58typedef struct prop_area prop_area;
59
60struct prop_info_compat {
61 char name[PROP_NAME_MAX];
62 unsigned volatile serial;
63 char value[PROP_VALUE_MAX];
64};
65
66typedef struct prop_info_compat prop_info_compat;
67
68extern prop_area *__system_property_area__;
69
70const prop_info *__system_property_find_compat(const char *name)
71{
72 prop_area_compat *pa = (prop_area_compat *)__system_property_area__;
73 unsigned count = pa->count;
74 unsigned *toc = pa->toc;
75 unsigned len = strlen(name);
76 prop_info_compat *pi;
77
78 if (len >= PROP_NAME_MAX)
79 return 0;
80 if (len < 1)
81 return 0;
82
83 while(count--) {
84 unsigned entry = *toc++;
85 if(TOC_NAME_LEN(entry) != len) continue;
86
87 pi = TOC_TO_INFO(pa, entry);
88 if(memcmp(name, pi->name, len)) continue;
89
90 return (const prop_info *)pi;
91 }
92
93 return 0;
94}
95
96int __system_property_read_compat(const prop_info *_pi, char *name, char *value)
97{
98 unsigned serial, len;
99 const prop_info_compat *pi = (const prop_info_compat *)_pi;
100
101 for(;;) {
102 serial = pi->serial;
103 while(SERIAL_DIRTY(serial)) {
104 __futex_wait((volatile void *)&pi->serial, serial, 0);
105 serial = pi->serial;
106 }
107 len = SERIAL_VALUE_LEN(serial);
108 memcpy(value, pi->value, len + 1);
109 if(serial == pi->serial) {
110 if(name != 0) {
111 strcpy(name, pi->name);
112 }
113 return len;
114 }
115 }
116}
117
118int __system_property_foreach_compat(
119 void (*propfn)(const prop_info *pi, void *cookie),
120 void *cookie)
121{
122 prop_area_compat *pa = (prop_area_compat *)__system_property_area__;
123 unsigned i;
124
125 for (i = 0; i < pa->count; i++) {
126 unsigned entry = pa->toc[i];
127 prop_info_compat *pi = TOC_TO_INFO(pa, entry);
128 propfn((const prop_info *)pi, cookie);
129 }
130
131 return 0;
132}