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