1 | /*␊ |
2 | * CoreHash usage example␊ |
3 | *␊ |
4 | */␊ |
5 | ␊ |
6 | #include "libsaio.h"␊ |
7 | #include "bootstruct.h"␊ |
8 | #include "pci.h"␊ |
9 | #include "platform.h"␊ |
10 | #include "cpu.h"␊ |
11 | #include "CoreHash.h"␊ |
12 | ␊ |
13 | ␊ |
14 | #ifndef DEBUG_PLATFORM␊ |
15 | #define DEBUG_PLATFORM 0␊ |
16 | #endif␊ |
17 | ␊ |
18 | #if DEBUG_PLATFORM␊ |
19 | #define DBG(x...)␉printf(x)␊ |
20 | #else␊ |
21 | #define DBG(x...)␊ |
22 | #endif␊ |
23 | ␊ |
24 | char *gboardproduct = NULL;␊ |
25 | char *gPlatformName = NULL;␊ |
26 | char *gRootDevice = NULL;␊ |
27 | ␊ |
28 | typedef enum envtype {␊ |
29 | kEnvPtr = 0,␊ |
30 | kEnvValue = 1␊ |
31 | } envtype;␊ |
32 | ␊ |
33 | struct env_struct {␊ |
34 | CoreHashHeader ␊ |
35 | ␉unsigned long value;␊ |
36 | ␉void * ptr;␊ |
37 | enum envtype Type;␊ |
38 | };␊ |
39 | ␊ |
40 | // CoreHash Declarations␊ |
41 | CHInitStr(env_struct)␊ |
42 | CHUnInit(env_struct)␊ |
43 | ␊ |
44 | static void CopyVarPtr (struct env_struct *var, void* ptr, size_t size);␊ |
45 | static struct env_struct *find_env(const char *name);␊ |
46 | static void _re_set_env_copy(struct env_struct *var , void* ptr,size_t size);␊ |
47 | struct env_struct *platform_env = NULL;␊ |
48 | ␊ |
49 | static void CopyVarPtr (struct env_struct *var, void* ptr, size_t size)␊ |
50 | {␊ |
51 | var->ptr = malloc(size);␊ |
52 | ␉if (var->ptr)␊ |
53 | ␉{␊ |
54 | ␉␉memcpy(var->ptr, ptr, size);␊ |
55 | ␊ |
56 | ␉}␊ |
57 | }␊ |
58 | ␊ |
59 | static struct env_struct *find_env(const char *name) {␊ |
60 | ␊ |
61 | return env_struct_FindStrVar(name,platform_env);␊ |
62 | }␊ |
63 | ␊ |
64 | static void _re_set_env_copy(struct env_struct *var , void* ptr,size_t size) {␊ |
65 | ␊ |
66 | ␉if (var->Type == kEnvPtr) {␊ |
67 | ␉␉return ;␊ |
68 | ␉} ␊ |
69 | ␊ |
70 | if (var->ptr) {␊ |
71 | free(var->ptr);␊ |
72 | var->ptr = NULL;␊ |
73 | }␊ |
74 | ␊ |
75 | CopyVarPtr(var, ptr, size);␊ |
76 | ␉␊ |
77 | ␉return; ␊ |
78 | }␊ |
79 | ␊ |
80 | void re_set_env_copy(const char *name , void* ptr,size_t size) {␊ |
81 | ␉struct env_struct *var;␊ |
82 | ␊ |
83 | ␉var = find_env(name);␊ |
84 | ␉if (!var|| (var->Type == kEnvPtr)) {␊ |
85 | ␉␉printf("Unable to find environement variable %s\n",name);␊ |
86 | ␉␉return ;␊ |
87 | ␉} ␊ |
88 | ␊ |
89 | _re_set_env_copy(var , ptr, size);␊ |
90 | ␉␊ |
91 | ␉return; ␊ |
92 | }␊ |
93 | ␊ |
94 | static void _set_env(const char *name, unsigned long value, enum envtype Type, void* ptr, size_t size ) {␊ |
95 | struct env_struct *var;␊ |
96 | ␊ |
97 | var = env_struct_NewStrVar(name, &platform_env);␊ |
98 | ␉if (!var) {␊ |
99 | ␉␉return;␊ |
100 | ␉}␊ |
101 | if (Type == kEnvPtr) {␊ |
102 | CopyVarPtr( var, ptr, size);␊ |
103 | } ␊ |
104 | else if (Type == kEnvValue) ␊ |
105 | var->value = value;␊ |
106 | else␊ |
107 | return;␊ |
108 | ␊ |
109 | var->Type = Type; ␊ |
110 | ␊ |
111 | }␊ |
112 | ␊ |
113 | /* Warning: set_env will create a new variable each time it will be called, ␊ |
114 | * if you want to set again an existing variable, please use safe_set_env or re_set_env .␊ |
115 | * NOTE: If you set several times the "same variable" by using this function,␊ |
116 | * the HASH_COUNT will grow up, ␊ |
117 | * but hopefully find_env will return the last variable that you have set with the same name␊ |
118 | * ex: set_env("test",10);␊ |
119 | * set_env("test",20);␊ |
120 | *␊ |
121 | * HASH_COUNT will be equal to 2, get_env("test") will return 20␊ |
122 | *␊ |
123 | * safe_set_env("test",10);␊ |
124 | * safe_set_env("test",20);␊ |
125 | * ␊ |
126 | * HASH_COUNT will be equal to 1, get_env("test") will return 20␊ |
127 | *␊ |
128 | * set_env("test",10);␊ |
129 | * re_set_env("test",20);␊ |
130 | *␊ |
131 | * HASH_COUNT will be equal to 1, get_env("test") will return 20␊ |
132 | *␊ |
133 | */␊ |
134 | void set_env(const char *name, unsigned long value ) {␊ |
135 | _set_env(name, value, kEnvValue,0,0);␊ |
136 | }␊ |
137 | ␊ |
138 | void set_env_copy(const char *name, void * ptr, size_t size ) {␊ |
139 | _set_env(name, 0, kEnvPtr,ptr,size);␊ |
140 | }␊ |
141 | ␊ |
142 | unsigned long get_env_var(const char *name) {␊ |
143 | ␉struct env_struct *var;␊ |
144 | ␊ |
145 | ␉var = find_env(name);␊ |
146 | ␉if (!var) {␊ |
147 | ␉␉printf("Unable to find environement variable %s\n",name);␊ |
148 | ␉␉return 0;␊ |
149 | ␉}␊ |
150 | ␊ |
151 | if (var->Type != kEnvValue) {␊ |
152 | printf("Variable %s is not a value\n",name);␊ |
153 | return 0;␊ |
154 | }␊ |
155 | ␉␊ |
156 | ␉return var->value;␊ |
157 | ␊ |
158 | }␊ |
159 | ␊ |
160 | unsigned long get_env(const char *name) {␉␊ |
161 | ␉␊ |
162 | ␉return get_env_var(name);␊ |
163 | ␊ |
164 | }␊ |
165 | ␊ |
166 | void * get_env_ptr(const char *name) {␊ |
167 | ␉struct env_struct *var;␊ |
168 | ␊ |
169 | ␉var = find_env(name);␊ |
170 | ␉if (!var) {␊ |
171 | ␉␉printf("Unable to get environement ptr variable %s\n",name);␊ |
172 | ␉␉return 0;␊ |
173 | ␉}␊ |
174 | ␊ |
175 | if (var->Type != kEnvPtr) {␊ |
176 | printf("Variable %s is not a ptr\n",name);␊ |
177 | return 0;␊ |
178 | }␊ |
179 | ␉␊ |
180 | ␉return var->ptr;␊ |
181 | ␊ |
182 | }␊ |
183 | ␊ |
184 | /* If no specified variable exist, safe_set_env will create one, else it only modify the value */␊ |
185 | static void _safe_set_env(const char *name, unsigned long long value, enum envtype Type, void* ptr, size_t size )␊ |
186 | {␊ |
187 | ␉struct env_struct *var;␊ |
188 | ␊ |
189 | ␉var = find_env(name);␊ |
190 | ␊ |
191 | if (!var) {␊ |
192 | if (Type == kEnvPtr) {␊ |
193 | _set_env(name, 0, kEnvPtr,ptr,size);␊ |
194 | } ␊ |
195 | else if (Type == kEnvValue) {␊ |
196 | _set_env(name, value, kEnvValue,0,0);␊ |
197 | }␊ |
198 | } ␊ |
199 | else if (var->Type != Type) {␊ |
200 | return; ␊ |
201 | ␉} ␊ |
202 | else {␊ |
203 | if (Type == kEnvValue) ␊ |
204 | var->value = value;␊ |
205 | else if (Type == kEnvPtr)␊ |
206 | _re_set_env_copy(var,ptr,size); ␊ |
207 | }␊ |
208 | ␉␊ |
209 | ␉return; ␊ |
210 | }␊ |
211 | ␊ |
212 | void safe_set_env_copy(const char *name , void * ptr, size_t size ) {␊ |
213 | ␊ |
214 | _safe_set_env(name, 0, kEnvPtr,ptr,size);␊ |
215 | ␉␊ |
216 | ␉return; ␊ |
217 | }␊ |
218 | ␊ |
219 | void safe_set_env(const char *name , unsigned long value) {␊ |
220 | ␊ |
221 | _safe_set_env(name, value, kEnvValue,0,0);␊ |
222 | ␉␊ |
223 | ␉return; ␊ |
224 | }␊ |
225 | ␊ |
226 | void re_set_env(const char *name , unsigned long value) {␊ |
227 | ␉struct env_struct *var;␊ |
228 | ␊ |
229 | ␉var = find_env(name);␊ |
230 | ␉if (!var || (var->Type != kEnvValue)/* kEnvPtr currently unsupported */) {␊ |
231 | ␉␉printf("Unable to reset environement value variable %s\n",name);␊ |
232 | ␉␉return ;␊ |
233 | ␉} ␊ |
234 | ␊ |
235 | var->value = value;␊ |
236 | ␉␊ |
237 | ␉return; ␊ |
238 | }␊ |
239 | ␊ |
240 | void unset_env(const char *name) {␊ |
241 | ␉env_struct_DeleteStrVar(name, platform_env); ␊ |
242 | }␊ |
243 | ␊ |
244 | void free_platform_env(void) {␊ |
245 | env_struct_DeleteAll(platform_env ); ␊ |
246 | } |