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