blob: ba73a2c28bf4601209eaecbe476d371ffff32391 [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>
Elliott Hughesd5ed63a2014-05-21 18:27:40 -070038
39#include "private/bionic_futex.h"
Colin Cross5e9a0862013-06-24 18:36:39 -070040
41#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
42#include <sys/_system_properties.h>
43
44#define TOC_NAME_LEN(toc) ((toc) >> 24)
45#define TOC_TO_INFO(area, toc) ((prop_info_compat*) (((char*) area) + ((toc) & 0xFFFFFF)))
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000046#define SERIAL_VALUE_LEN(serial) ((serial) >> 24)
Colin Cross5e9a0862013-06-24 18:36:39 -070047
48struct prop_area_compat {
49 unsigned volatile count;
50 unsigned volatile serial;
51 unsigned magic;
52 unsigned version;
Joshua J. Drake063a5722013-12-13 13:44:53 -060053 unsigned reserved[4];
Colin Cross5e9a0862013-06-24 18:36:39 -070054 unsigned toc[1];
55};
56
57typedef struct prop_area_compat prop_area_compat;
58
59struct prop_area;
60typedef struct prop_area prop_area;
61
62struct prop_info_compat {
63 char name[PROP_NAME_MAX];
64 unsigned volatile serial;
65 char value[PROP_VALUE_MAX];
66};
67
68typedef struct prop_info_compat prop_info_compat;
69
70extern prop_area *__system_property_area__;
71
Elliott Hughesd5ed63a2014-05-21 18:27:40 -070072__LIBC_HIDDEN__ const prop_info *__system_property_find_compat(const char *name)
Colin Cross5e9a0862013-06-24 18:36:39 -070073{
74 prop_area_compat *pa = (prop_area_compat *)__system_property_area__;
75 unsigned count = pa->count;
76 unsigned *toc = pa->toc;
77 unsigned len = strlen(name);
78 prop_info_compat *pi;
79
80 if (len >= PROP_NAME_MAX)
81 return 0;
82 if (len < 1)
83 return 0;
84
85 while(count--) {
86 unsigned entry = *toc++;
87 if(TOC_NAME_LEN(entry) != len) continue;
88
89 pi = TOC_TO_INFO(pa, entry);
90 if(memcmp(name, pi->name, len)) continue;
91
92 return (const prop_info *)pi;
93 }
94
95 return 0;
96}
97
Elliott Hughesd5ed63a2014-05-21 18:27:40 -070098__LIBC_HIDDEN__ int __system_property_read_compat(const prop_info *_pi, char *name, char *value)
Colin Cross5e9a0862013-06-24 18:36:39 -070099{
100 unsigned serial, len;
101 const prop_info_compat *pi = (const prop_info_compat *)_pi;
102
103 for(;;) {
104 serial = pi->serial;
105 while(SERIAL_DIRTY(serial)) {
Elliott Hughes5eccb962013-12-20 16:58:06 -0800106 __futex_wait((volatile void *)&pi->serial, serial, NULL);
Colin Cross5e9a0862013-06-24 18:36:39 -0700107 serial = pi->serial;
108 }
109 len = SERIAL_VALUE_LEN(serial);
110 memcpy(value, pi->value, len + 1);
111 if(serial == pi->serial) {
112 if(name != 0) {
113 strcpy(name, pi->name);
114 }
115 return len;
116 }
117 }
118}
119
Elliott Hughesd5ed63a2014-05-21 18:27:40 -0700120__LIBC_HIDDEN__ int __system_property_foreach_compat(
Colin Cross5e9a0862013-06-24 18:36:39 -0700121 void (*propfn)(const prop_info *pi, void *cookie),
122 void *cookie)
123{
124 prop_area_compat *pa = (prop_area_compat *)__system_property_area__;
125 unsigned i;
126
127 for (i = 0; i < pa->count; i++) {
128 unsigned entry = pa->toc[i];
129 prop_info_compat *pi = TOC_TO_INFO(pa, entry);
130 propfn((const prop_info *)pi, cookie);
131 }
132
133 return 0;
134}