]>
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 AV |
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> | |
3bf74a41 AV |
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 /* HWCONFIG_TEST */ | |
93f9dcf9 | 25 | |
c4b115f5 KG |
26 | DECLARE_GLOBAL_DATA_PTR; |
27 | ||
93f9dcf9 | 28 | static const char *hwconfig_parse(const char *opts, size_t maxlen, |
81f8d3b0 | 29 | const char *opt, char *stopchs, char eqch, |
93f9dcf9 AV |
30 | size_t *arglen) |
31 | { | |
32 | size_t optlen = strlen(opt); | |
33 | char *str; | |
34 | const char *start = opts; | |
35 | const char *end; | |
36 | ||
37 | next: | |
38 | str = strstr(opts, opt); | |
39 | end = str + optlen; | |
40 | if (end - start > maxlen) | |
41 | return NULL; | |
42 | ||
81f8d3b0 AV |
43 | if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) && |
44 | (strpbrk(end, stopchs) == end || *end == eqch || | |
45 | *end == '\0')) { | |
93f9dcf9 AV |
46 | const char *arg_end; |
47 | ||
48 | if (!arglen) | |
49 | return str; | |
50 | ||
51 | if (*end != eqch) | |
52 | return NULL; | |
53 | ||
81f8d3b0 | 54 | arg_end = strpbrk(str, stopchs); |
93f9dcf9 AV |
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 | ||
b194577b KG |
68 | const char cpu_hwconfig[] __attribute__((weak)) = ""; |
69 | const char board_hwconfig[] __attribute__((weak)) = ""; | |
93f9dcf9 | 70 | |
dd50af25 KG |
71 | static const char *__hwconfig(const char *opt, size_t *arglen, |
72 | const char *env_hwconfig) | |
93f9dcf9 | 73 | { |
dd50af25 | 74 | const char *ret; |
c4b115f5 | 75 | |
dd50af25 KG |
76 | /* if we are passed a buffer use it, otherwise try the environment */ |
77 | if (!env_hwconfig) { | |
78 | if (!(gd->flags & GD_FLG_ENV_READY)) { | |
79 | printf("WARNING: Calling __hwconfig without a buffer " | |
d1a24f06 WD |
80 | "and before environment is ready\n"); |
81 | return NULL; | |
dd50af25 | 82 | } |
00caae6d | 83 | env_hwconfig = env_get("hwconfig"); |
c4b115f5 | 84 | } |
93f9dcf9 | 85 | |
bb141079 KG |
86 | if (env_hwconfig) { |
87 | ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig), | |
81f8d3b0 | 88 | opt, ";", ':', arglen); |
bb141079 KG |
89 | if (ret) |
90 | return ret; | |
91 | } | |
93f9dcf9 | 92 | |
bb141079 | 93 | ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig), |
b194577b | 94 | opt, ";", ':', arglen); |
bb141079 KG |
95 | if (ret) |
96 | return ret; | |
93f9dcf9 | 97 | |
b194577b KG |
98 | return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig), |
99 | opt, ";", ':', arglen); | |
93f9dcf9 AV |
100 | } |
101 | ||
102 | /* | |
dd50af25 | 103 | * hwconfig_f - query if a particular hwconfig option is specified |
93f9dcf9 | 104 | * @opt: a string representing an option |
dd50af25 | 105 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
93f9dcf9 AV |
106 | * |
107 | * This call can be used to find out whether U-Boot should configure | |
108 | * a particular hardware option. | |
109 | * | |
110 | * Returns non-zero value if the hardware option can be used and thus | |
111 | * should be configured, 0 otherwise. | |
112 | * | |
113 | * This function also returns non-zero value if CONFIG_HWCONFIG is | |
114 | * undefined. | |
115 | * | |
116 | * Returning non-zero value without CONFIG_HWCONFIG has its crucial | |
117 | * purpose: the hwconfig() call should be a "transparent" interface, | |
118 | * e.g. if a board doesn't need hwconfig facility, then we assume | |
119 | * that the board file only calls things that are actually used, so | |
120 | * hwconfig() will always return true result. | |
121 | */ | |
dd50af25 | 122 | int hwconfig_f(const char *opt, char *buf) |
93f9dcf9 | 123 | { |
dd50af25 | 124 | return !!__hwconfig(opt, NULL, buf); |
93f9dcf9 AV |
125 | } |
126 | ||
127 | /* | |
dd50af25 | 128 | * hwconfig_arg_f - get hwconfig option's argument |
93f9dcf9 AV |
129 | * @opt: a string representing an option |
130 | * @arglen: a pointer to an allocated size_t variable | |
dd50af25 | 131 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
93f9dcf9 | 132 | * |
dd50af25 | 133 | * Unlike hwconfig_f() function, this function returns a pointer to the |
93f9dcf9 AV |
134 | * start of the hwconfig arguments, if option is not found or it has |
135 | * no specified arguments, the function returns NULL pointer. | |
136 | * | |
137 | * If CONFIG_HWCONFIG is undefined, the function returns "", and | |
138 | * arglen is set to 0. | |
139 | */ | |
dd50af25 | 140 | const char *hwconfig_arg_f(const char *opt, size_t *arglen, char *buf) |
93f9dcf9 | 141 | { |
dd50af25 | 142 | return __hwconfig(opt, arglen, buf); |
93f9dcf9 AV |
143 | } |
144 | ||
145 | /* | |
dd50af25 | 146 | * hwconfig_arg_cmp_f - compare hwconfig option's argument |
93f9dcf9 AV |
147 | * @opt: a string representing an option |
148 | * @arg: a string for comparing an option's argument | |
dd50af25 | 149 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
93f9dcf9 | 150 | * |
dd50af25 | 151 | * This call is similar to hwconfig_arg_f, but instead of returning |
93f9dcf9 AV |
152 | * hwconfig argument and its length, it is comparing it to @arg. |
153 | * | |
154 | * Returns non-zero value if @arg matches, 0 otherwise. | |
155 | * | |
156 | * If CONFIG_HWCONFIG is undefined, the function returns a non-zero | |
157 | * value, i.e. the argument matches. | |
158 | */ | |
dd50af25 | 159 | int hwconfig_arg_cmp_f(const char *opt, const char *arg, char *buf) |
93f9dcf9 AV |
160 | { |
161 | const char *argstr; | |
162 | size_t arglen; | |
163 | ||
dd50af25 | 164 | argstr = hwconfig_arg_f(opt, &arglen, buf); |
93f9dcf9 AV |
165 | if (!argstr || arglen != strlen(arg)) |
166 | return 0; | |
167 | ||
168 | return !strncmp(argstr, arg, arglen); | |
169 | } | |
170 | ||
171 | /* | |
dd50af25 | 172 | * hwconfig_sub_f - query if a particular hwconfig sub-option is specified |
93f9dcf9 AV |
173 | * @opt: a string representing an option |
174 | * @subopt: a string representing a sub-option | |
dd50af25 | 175 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
93f9dcf9 | 176 | * |
dd50af25 | 177 | * This call is similar to hwconfig_f(), except that it takes additional |
93f9dcf9 AV |
178 | * argument @subopt. In this example: |
179 | * "dr_usb:mode=peripheral" | |
180 | * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its | |
181 | * argument. | |
182 | */ | |
dd50af25 | 183 | int hwconfig_sub_f(const char *opt, const char *subopt, char *buf) |
93f9dcf9 AV |
184 | { |
185 | size_t arglen; | |
186 | const char *arg; | |
187 | ||
dd50af25 | 188 | arg = __hwconfig(opt, &arglen, buf); |
93f9dcf9 AV |
189 | if (!arg) |
190 | return 0; | |
81f8d3b0 | 191 | return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL); |
93f9dcf9 AV |
192 | } |
193 | ||
194 | /* | |
dd50af25 | 195 | * hwconfig_subarg_f - get hwconfig sub-option's argument |
93f9dcf9 AV |
196 | * @opt: a string representing an option |
197 | * @subopt: a string representing a sub-option | |
198 | * @subarglen: a pointer to an allocated size_t variable | |
dd50af25 | 199 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
93f9dcf9 | 200 | * |
dd50af25 KG |
201 | * This call is similar to hwconfig_arg_f(), except that it takes an |
202 | * additional argument @subopt, and so works with sub-options. | |
93f9dcf9 | 203 | */ |
dd50af25 KG |
204 | const char *hwconfig_subarg_f(const char *opt, const char *subopt, |
205 | size_t *subarglen, char *buf) | |
93f9dcf9 AV |
206 | { |
207 | size_t arglen; | |
208 | const char *arg; | |
209 | ||
dd50af25 | 210 | arg = __hwconfig(opt, &arglen, buf); |
93f9dcf9 AV |
211 | if (!arg) |
212 | return NULL; | |
81f8d3b0 | 213 | return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen); |
93f9dcf9 AV |
214 | } |
215 | ||
216 | /* | |
dd50af25 | 217 | * hwconfig_arg_cmp_f - compare hwconfig sub-option's argument |
93f9dcf9 AV |
218 | * @opt: a string representing an option |
219 | * @subopt: a string representing a sub-option | |
220 | * @subarg: a string for comparing an sub-option's argument | |
dd50af25 | 221 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
93f9dcf9 | 222 | * |
dd50af25 KG |
223 | * This call is similar to hwconfig_arg_cmp_f, except that it takes an |
224 | * additional argument @subopt, and so works with sub-options. | |
93f9dcf9 | 225 | */ |
dd50af25 KG |
226 | int hwconfig_subarg_cmp_f(const char *opt, const char *subopt, |
227 | const char *subarg, char *buf) | |
93f9dcf9 AV |
228 | { |
229 | const char *argstr; | |
230 | size_t arglen; | |
231 | ||
dd50af25 | 232 | argstr = hwconfig_subarg_f(opt, subopt, &arglen, buf); |
93f9dcf9 AV |
233 | if (!argstr || arglen != strlen(subarg)) |
234 | return 0; | |
235 | ||
236 | return !strncmp(argstr, subarg, arglen); | |
237 | } | |
3bf74a41 AV |
238 | |
239 | #ifdef HWCONFIG_TEST | |
240 | int main() | |
241 | { | |
242 | const char *ret; | |
243 | size_t len; | |
244 | ||
382bee57 | 245 | env_set("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;" |
3bf74a41 AV |
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 | ||
382bee57 | 276 | unenv_set("hwconfig"); |
3bf74a41 AV |
277 | |
278 | assert(hwconfig(NULL) == 0); | |
279 | assert(hwconfig("") == 0); | |
280 | assert(hwconfig("key3") == 0); | |
281 | ||
282 | return 0; | |
283 | } | |
284 | #endif /* HWCONFIG_TEST */ |