1
2
3
4
5
6
7
8
9
10
11#ifndef HWCONFIG_TEST
12#include <config.h>
13#include <common.h>
14#include <env.h>
15#include <exports.h>
16#include <hwconfig.h>
17#include <log.h>
18#include <asm/global_data.h>
19#include <linux/types.h>
20#include <linux/string.h>
21#else
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <assert.h>
26#define min(a, b) (((a) < (b)) ? (a) : (b))
27#endif
28
29DECLARE_GLOBAL_DATA_PTR;
30
31static const char *hwconfig_parse(const char *opts, size_t maxlen,
32 const char *opt, char *stopchs, char eqch,
33 size_t *arglen)
34{
35 size_t optlen = strlen(opt);
36 char *str;
37 const char *start = opts;
38 const char *end;
39
40next:
41 str = strstr(opts, opt);
42 end = str + optlen;
43 if (end - start > maxlen)
44 return NULL;
45
46 if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) &&
47 (strpbrk(end, stopchs) == end || *end == eqch ||
48 *end == '\0')) {
49 const char *arg_end;
50
51 if (!arglen)
52 return str;
53
54 if (*end != eqch)
55 return NULL;
56
57 arg_end = strpbrk(str, stopchs);
58 if (!arg_end)
59 *arglen = min(maxlen, strlen(str)) - optlen - 1;
60 else
61 *arglen = arg_end - end - 1;
62
63 return end + 1;
64 } else if (str) {
65 opts = end;
66 goto next;
67 }
68 return NULL;
69}
70
71const char cpu_hwconfig[] __attribute__((weak)) = "";
72const char board_hwconfig[] __attribute__((weak)) = "";
73
74static const char *__hwconfig(const char *opt, size_t *arglen,
75 const char *env_hwconfig)
76{
77 const char *ret;
78
79
80 if (!env_hwconfig) {
81 if (!(gd->flags & GD_FLG_ENV_READY)) {
82 printf("WARNING: Calling __hwconfig without a buffer "
83 "and before environment is ready\n");
84 return NULL;
85 }
86 env_hwconfig = env_get("hwconfig");
87 }
88
89 if (env_hwconfig) {
90 ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
91 opt, ";", ':', arglen);
92 if (ret)
93 return ret;
94 }
95
96 ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
97 opt, ";", ':', arglen);
98 if (ret)
99 return ret;
100
101 return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
102 opt, ";", ':', arglen);
103}
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125int hwconfig_f(const char *opt, char *buf)
126{
127 return !!__hwconfig(opt, NULL, buf);
128}
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143const char *hwconfig_arg_f(const char *opt, size_t *arglen, char *buf)
144{
145 return __hwconfig(opt, arglen, buf);
146}
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162int hwconfig_arg_cmp_f(const char *opt, const char *arg, char *buf)
163{
164 const char *argstr;
165 size_t arglen;
166
167 argstr = hwconfig_arg_f(opt, &arglen, buf);
168 if (!argstr || arglen != strlen(arg))
169 return 0;
170
171 return !strncmp(argstr, arg, arglen);
172}
173
174
175
176
177
178
179
180
181
182
183
184
185
186int hwconfig_sub_f(const char *opt, const char *subopt, char *buf)
187{
188 size_t arglen;
189 const char *arg;
190
191 arg = __hwconfig(opt, &arglen, buf);
192 if (!arg)
193 return 0;
194 return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
195}
196
197
198
199
200
201
202
203
204
205
206
207const char *hwconfig_subarg_f(const char *opt, const char *subopt,
208 size_t *subarglen, char *buf)
209{
210 size_t arglen;
211 const char *arg;
212
213 arg = __hwconfig(opt, &arglen, buf);
214 if (!arg)
215 return NULL;
216 return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
217}
218
219
220
221
222
223
224
225
226
227
228
229int hwconfig_subarg_cmp_f(const char *opt, const char *subopt,
230 const char *subarg, char *buf)
231{
232 const char *argstr;
233 size_t arglen;
234
235 argstr = hwconfig_subarg_f(opt, subopt, &arglen, buf);
236 if (!argstr || arglen != strlen(subarg))
237 return 0;
238
239 return !strncmp(argstr, subarg, arglen);
240}
241
242#ifdef HWCONFIG_TEST
243int main()
244{
245 const char *ret;
246 size_t len;
247
248 env_set("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
249 "key3;:,:=;key4", 1);
250
251 ret = hwconfig_arg("key1", &len);
252 printf("%zd %.*s\n", len, (int)len, ret);
253 assert(len == 29);
254 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
255 assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len));
256
257 ret = hwconfig_subarg("key1", "subkey1", &len);
258 printf("%zd %.*s\n", len, (int)len, ret);
259 assert(len == 6);
260 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
261 assert(!strncmp(ret, "value1", len));
262
263 ret = hwconfig_subarg("key1", "subkey2", &len);
264 printf("%zd %.*s\n", len, (int)len, ret);
265 assert(len == 6);
266 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
267 assert(!strncmp(ret, "value2", len));
268
269 ret = hwconfig_arg("key2", &len);
270 printf("%zd %.*s\n", len, (int)len, ret);
271 assert(len == 6);
272 assert(hwconfig_arg_cmp("key2", "value3"));
273 assert(!strncmp(ret, "value3", len));
274
275 assert(hwconfig("key3"));
276 assert(hwconfig_arg("key4", &len) == NULL);
277 assert(hwconfig_arg("bogus", &len) == NULL);
278
279 unenv_set("hwconfig");
280
281 assert(hwconfig(NULL) == 0);
282 assert(hwconfig("") == 0);
283 assert(hwconfig("key3") == 0);
284
285 return 0;
286}
287#endif
288