1/*
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
39#include "private/bionic_futex.h"
40
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)))
46
47struct prop_area_compat {
48    unsigned volatile count;
49    unsigned volatile serial;
50    unsigned magic;
51    unsigned version;
52    unsigned reserved[4];
53    unsigned toc[1];
54};
55
56typedef struct prop_area_compat prop_area_compat;
57
58struct prop_area;
59typedef struct prop_area prop_area;
60
61struct prop_info_compat {
62    char name[PROP_NAME_MAX];
63    unsigned volatile serial;
64    char value[PROP_VALUE_MAX];
65};
66
67typedef struct prop_info_compat prop_info_compat;
68
69extern prop_area *__system_property_area__;
70
71__LIBC_HIDDEN__ const prop_info *__system_property_find_compat(const char *name)
72{
73    prop_area_compat *pa = (prop_area_compat *)__system_property_area__;
74    unsigned count = pa->count;
75    unsigned *toc = pa->toc;
76    unsigned len = strlen(name);
77    prop_info_compat *pi;
78
79    if (len >= PROP_NAME_MAX)
80        return 0;
81    if (len < 1)
82        return 0;
83
84    while(count--) {
85        unsigned entry = *toc++;
86        if(TOC_NAME_LEN(entry) != len) continue;
87
88        pi = TOC_TO_INFO(pa, entry);
89        if(memcmp(name, pi->name, len)) continue;
90
91        return (const prop_info *)pi;
92    }
93
94    return 0;
95}
96
97__LIBC_HIDDEN__ int __system_property_read_compat(const prop_info *_pi, char *name, char *value)
98{
99    unsigned serial, len;
100    const prop_info_compat *pi = (const prop_info_compat *)_pi;
101
102    for(;;) {
103        serial = pi->serial;
104        while(SERIAL_DIRTY(serial)) {
105            __futex_wait((volatile void *)&pi->serial, serial, NULL);
106            serial = pi->serial;
107        }
108        len = SERIAL_VALUE_LEN(serial);
109        memcpy(value, pi->value, len + 1);
110        if(serial == pi->serial) {
111            if(name != 0) {
112                strcpy(name, pi->name);
113            }
114            return len;
115        }
116    }
117}
118
119__LIBC_HIDDEN__ int __system_property_foreach_compat(
120        void (*propfn)(const prop_info *pi, void *cookie),
121        void *cookie)
122{
123    prop_area_compat *pa = (prop_area_compat *)__system_property_area__;
124    unsigned i;
125
126    for (i = 0; i < pa->count; i++) {
127        unsigned entry = pa->toc[i];
128        prop_info_compat *pi = TOC_TO_INFO(pa, entry);
129        propfn((const prop_info *)pi, cookie);
130    }
131
132    return 0;
133}
134