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