]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
93f9dcf9 AV |
2 | /* |
3 | * An inteface for configuring a hardware via u-boot environment. | |
4 | * | |
5 | * Copyright (c) 2009 MontaVista Software, Inc. | |
dd50af25 | 6 | * Copyright 2011 Freescale Semiconductor, Inc. |
93f9dcf9 AV |
7 | * |
8 | * Author: Anton Vorontsov <[email protected]> | |
93f9dcf9 AV |
9 | */ |
10 | ||
3bf74a41 | 11 | #ifndef HWCONFIG_TEST |
93f9dcf9 | 12 | #include <config.h> |
9fb625ce | 13 | #include <env.h> |
93f9dcf9 AV |
14 | #include <exports.h> |
15 | #include <hwconfig.h> | |
f7ae49fc | 16 | #include <log.h> |
401d1c4f | 17 | #include <asm/global_data.h> |
93f9dcf9 AV |
18 | #include <linux/types.h> |
19 | #include <linux/string.h> | |
3bf74a41 AV |
20 | #else |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <assert.h> | |
25 | #define min(a, b) (((a) < (b)) ? (a) : (b)) | |
26 | #endif /* HWCONFIG_TEST */ | |
93f9dcf9 | 27 | |
c4b115f5 KG |
28 | DECLARE_GLOBAL_DATA_PTR; |
29 | ||
93f9dcf9 | 30 | static const char *hwconfig_parse(const char *opts, size_t maxlen, |
81f8d3b0 | 31 | const char *opt, char *stopchs, char eqch, |
93f9dcf9 AV |
32 | size_t *arglen) |
33 | { | |
34 | size_t optlen = strlen(opt); | |
35 | char *str; | |
36 | const char *start = opts; | |
37 | const char *end; | |
38 | ||
39 | next: | |
40 | str = strstr(opts, opt); | |
41 | end = str + optlen; | |
42 | if (end - start > maxlen) | |
43 | return NULL; | |
44 | ||
81f8d3b0 AV |
45 | if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) && |
46 | (strpbrk(end, stopchs) == end || *end == eqch || | |
47 | *end == '\0')) { | |
93f9dcf9 AV |
48 | const char *arg_end; |
49 | ||
50 | if (!arglen) | |
51 | return str; | |
52 | ||
53 | if (*end != eqch) | |
54 | return NULL; | |
55 | ||
81f8d3b0 | 56 | arg_end = strpbrk(str, stopchs); |
93f9dcf9 AV |
57 | if (!arg_end) |
58 | *arglen = min(maxlen, strlen(str)) - optlen - 1; | |
59 | else | |
60 | *arglen = arg_end - end - 1; | |
61 | ||
62 | return end + 1; | |
63 | } else if (str) { | |
64 | opts = end; | |
65 | goto next; | |
66 | } | |
67 | return NULL; | |
68 | } | |
69 | ||
b194577b KG |
70 | const char cpu_hwconfig[] __attribute__((weak)) = ""; |
71 | const char board_hwconfig[] __attribute__((weak)) = ""; | |
93f9dcf9 | 72 | |
dd50af25 KG |
73 | static const char *__hwconfig(const char *opt, size_t *arglen, |
74 | const char *env_hwconfig) | |
93f9dcf9 | 75 | { |
dd50af25 | 76 | const char *ret; |
c4b115f5 | 77 | |
dd50af25 KG |
78 | /* if we are passed a buffer use it, otherwise try the environment */ |
79 | if (!env_hwconfig) { | |
66ca5b78 SG |
80 | #if CONFIG_IS_ENABLED(ENV_SUPPORT) |
81 | if (!(gd->flags & GD_FLG_ENV_READY) && | |
82 | gd->env_valid != ENV_VALID) | |
83 | #else | |
84 | if (true) | |
85 | #endif | |
86 | { | |
dd50af25 | 87 | printf("WARNING: Calling __hwconfig without a buffer " |
d1a24f06 WD |
88 | "and before environment is ready\n"); |
89 | return NULL; | |
dd50af25 | 90 | } |
d64a8fd0 | 91 | #if CONFIG_IS_ENABLED(ENV_SUPPORT) |
00caae6d | 92 | env_hwconfig = env_get("hwconfig"); |
d64a8fd0 | 93 | #endif |
c4b115f5 | 94 | } |
93f9dcf9 | 95 | |
bb141079 KG |
96 | if (env_hwconfig) { |
97 | ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig), | |
81f8d3b0 | 98 | opt, ";", ':', arglen); |
bb141079 KG |
99 | if (ret) |
100 | return ret; | |
101 | } | |
93f9dcf9 | 102 | |
bb141079 | 103 | ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig), |
b194577b | 104 | opt, ";", ':', arglen); |
bb141079 KG |
105 | if (ret) |
106 | return ret; | |
93f9dcf9 | 107 | |
b194577b KG |
108 | return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig), |
109 | opt, ";", ':', arglen); | |
93f9dcf9 AV |
110 | } |
111 | ||
112 | /* | |
dd50af25 | 113 | * hwconfig_f - query if a particular hwconfig option is specified |
93f9dcf9 | 114 | * @opt: a string representing an option |
dd50af25 | 115 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
93f9dcf9 AV |
116 | * |
117 | * This call can be used to find out whether U-Boot should configure | |
118 | * a particular hardware option. | |
119 | * | |
120 | * Returns non-zero value if the hardware option can be used and thus | |
121 | * should be configured, 0 otherwise. | |
122 | * | |
123 | * This function also returns non-zero value if CONFIG_HWCONFIG is | |
124 | * undefined. | |
125 | * | |
126 | * Returning non-zero value without CONFIG_HWCONFIG has its crucial | |
127 | * purpose: the hwconfig() call should be a "transparent" interface, | |
128 | * e.g. if a board doesn't need hwconfig facility, then we assume | |
129 | * that the board file only calls things that are actually used, so | |
130 | * hwconfig() will always return true result. | |
131 | */ | |
dd50af25 | 132 | int hwconfig_f(const char *opt, char *buf) |
93f9dcf9 | 133 | { |
dd50af25 | 134 | return !!__hwconfig(opt, NULL, buf); |
93f9dcf9 AV |
135 | } |
136 | ||
137 | /* | |
dd50af25 | 138 | * hwconfig_arg_f - get hwconfig option's argument |
93f9dcf9 AV |
139 | * @opt: a string representing an option |
140 | * @arglen: a pointer to an allocated size_t variable | |
dd50af25 | 141 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
93f9dcf9 | 142 | * |
dd50af25 | 143 | * Unlike hwconfig_f() function, this function returns a pointer to the |
93f9dcf9 AV |
144 | * start of the hwconfig arguments, if option is not found or it has |
145 | * no specified arguments, the function returns NULL pointer. | |
146 | * | |
147 | * If CONFIG_HWCONFIG is undefined, the function returns "", and | |
148 | * arglen is set to 0. | |
149 | */ | |
dd50af25 | 150 | const char *hwconfig_arg_f(const char *opt, size_t *arglen, char *buf) |
93f9dcf9 | 151 | { |
dd50af25 | 152 | return __hwconfig(opt, arglen, buf); |
93f9dcf9 AV |
153 | } |
154 | ||
155 | /* | |
dd50af25 | 156 | * hwconfig_arg_cmp_f - compare hwconfig option's argument |
93f9dcf9 AV |
157 | * @opt: a string representing an option |
158 | * @arg: a string for comparing an option's argument | |
dd50af25 | 159 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
93f9dcf9 | 160 | * |
dd50af25 | 161 | * This call is similar to hwconfig_arg_f, but instead of returning |
93f9dcf9 AV |
162 | * hwconfig argument and its length, it is comparing it to @arg. |
163 | * | |
164 | * Returns non-zero value if @arg matches, 0 otherwise. | |
165 | * | |
166 | * If CONFIG_HWCONFIG is undefined, the function returns a non-zero | |
167 | * value, i.e. the argument matches. | |
168 | */ | |
dd50af25 | 169 | int hwconfig_arg_cmp_f(const char *opt, const char *arg, char *buf) |
93f9dcf9 AV |
170 | { |
171 | const char *argstr; | |
172 | size_t arglen; | |
173 | ||
dd50af25 | 174 | argstr = hwconfig_arg_f(opt, &arglen, buf); |
93f9dcf9 AV |
175 | if (!argstr || arglen != strlen(arg)) |
176 | return 0; | |
177 | ||
178 | return !strncmp(argstr, arg, arglen); | |
179 | } | |
180 | ||
181 | /* | |
dd50af25 | 182 | * hwconfig_sub_f - query if a particular hwconfig sub-option is specified |
93f9dcf9 AV |
183 | * @opt: a string representing an option |
184 | * @subopt: a string representing a sub-option | |
dd50af25 | 185 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
93f9dcf9 | 186 | * |
dd50af25 | 187 | * This call is similar to hwconfig_f(), except that it takes additional |
93f9dcf9 | 188 | * argument @subopt. In this example: |
0cf207ec | 189 | * "dr_usb:mode=peripheral" |
93f9dcf9 AV |
190 | * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its |
191 | * argument. | |
192 | */ | |
dd50af25 | 193 | int hwconfig_sub_f(const char *opt, const char *subopt, char *buf) |
93f9dcf9 AV |
194 | { |
195 | size_t arglen; | |
196 | const char *arg; | |
197 | ||
dd50af25 | 198 | arg = __hwconfig(opt, &arglen, buf); |
93f9dcf9 AV |
199 | if (!arg) |
200 | return 0; | |
81f8d3b0 | 201 | return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL); |
93f9dcf9 AV |
202 | } |
203 | ||
204 | /* | |
dd50af25 | 205 | * hwconfig_subarg_f - get hwconfig sub-option's argument |
93f9dcf9 AV |
206 | * @opt: a string representing an option |
207 | * @subopt: a string representing a sub-option | |
208 | * @subarglen: a pointer to an allocated size_t variable | |
dd50af25 | 209 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
93f9dcf9 | 210 | * |
dd50af25 KG |
211 | * This call is similar to hwconfig_arg_f(), except that it takes an |
212 | * additional argument @subopt, and so works with sub-options. | |
93f9dcf9 | 213 | */ |
dd50af25 KG |
214 | const char *hwconfig_subarg_f(const char *opt, const char *subopt, |
215 | size_t *subarglen, char *buf) | |
93f9dcf9 AV |
216 | { |
217 | size_t arglen; | |
218 | const char *arg; | |
219 | ||
dd50af25 | 220 | arg = __hwconfig(opt, &arglen, buf); |
93f9dcf9 AV |
221 | if (!arg) |
222 | return NULL; | |
81f8d3b0 | 223 | return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen); |
93f9dcf9 AV |
224 | } |
225 | ||
226 | /* | |
dd50af25 | 227 | * hwconfig_arg_cmp_f - compare hwconfig sub-option's argument |
93f9dcf9 AV |
228 | * @opt: a string representing an option |
229 | * @subopt: a string representing a sub-option | |
230 | * @subarg: a string for comparing an sub-option's argument | |
dd50af25 | 231 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
93f9dcf9 | 232 | * |
dd50af25 KG |
233 | * This call is similar to hwconfig_arg_cmp_f, except that it takes an |
234 | * additional argument @subopt, and so works with sub-options. | |
93f9dcf9 | 235 | */ |
dd50af25 KG |
236 | int hwconfig_subarg_cmp_f(const char *opt, const char *subopt, |
237 | const char *subarg, char *buf) | |
93f9dcf9 AV |
238 | { |
239 | const char *argstr; | |
240 | size_t arglen; | |
241 | ||
dd50af25 | 242 | argstr = hwconfig_subarg_f(opt, subopt, &arglen, buf); |
93f9dcf9 AV |
243 | if (!argstr || arglen != strlen(subarg)) |
244 | return 0; | |
245 | ||
246 | return !strncmp(argstr, subarg, arglen); | |
247 | } | |
3bf74a41 AV |
248 | |
249 | #ifdef HWCONFIG_TEST | |
250 | int main() | |
251 | { | |
252 | const char *ret; | |
253 | size_t len; | |
254 | ||
382bee57 | 255 | env_set("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;" |
3bf74a41 AV |
256 | "key3;:,:=;key4", 1); |
257 | ||
258 | ret = hwconfig_arg("key1", &len); | |
259 | printf("%zd %.*s\n", len, (int)len, ret); | |
260 | assert(len == 29); | |
261 | assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2")); | |
262 | assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len)); | |
263 | ||
264 | ret = hwconfig_subarg("key1", "subkey1", &len); | |
265 | printf("%zd %.*s\n", len, (int)len, ret); | |
266 | assert(len == 6); | |
267 | assert(hwconfig_subarg_cmp("key1", "subkey1", "value1")); | |
268 | assert(!strncmp(ret, "value1", len)); | |
269 | ||
270 | ret = hwconfig_subarg("key1", "subkey2", &len); | |
271 | printf("%zd %.*s\n", len, (int)len, ret); | |
272 | assert(len == 6); | |
273 | assert(hwconfig_subarg_cmp("key1", "subkey2", "value2")); | |
274 | assert(!strncmp(ret, "value2", len)); | |
275 | ||
276 | ret = hwconfig_arg("key2", &len); | |
277 | printf("%zd %.*s\n", len, (int)len, ret); | |
278 | assert(len == 6); | |
279 | assert(hwconfig_arg_cmp("key2", "value3")); | |
280 | assert(!strncmp(ret, "value3", len)); | |
281 | ||
282 | assert(hwconfig("key3")); | |
283 | assert(hwconfig_arg("key4", &len) == NULL); | |
284 | assert(hwconfig_arg("bogus", &len) == NULL); | |
285 | ||
382bee57 | 286 | unenv_set("hwconfig"); |
3bf74a41 AV |
287 | |
288 | assert(hwconfig(NULL) == 0); | |
289 | assert(hwconfig("") == 0); | |
290 | assert(hwconfig("key3") == 0); | |
291 | ||
292 | return 0; | |
293 | } | |
294 | #endif /* HWCONFIG_TEST */ |