1 // SPDX-License-Identifier: GPL-2.0+
3 * An inteface for configuring a hardware via u-boot environment.
5 * Copyright (c) 2009 MontaVista Software, Inc.
6 * Copyright 2011 Freescale Semiconductor, Inc.
8 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
16 #include <linux/types.h>
17 #include <linux/string.h>
23 #define min(a, b) (((a) < (b)) ? (a) : (b))
24 #endif /* HWCONFIG_TEST */
26 DECLARE_GLOBAL_DATA_PTR;
28 static const char *hwconfig_parse(const char *opts, size_t maxlen,
29 const char *opt, char *stopchs, char eqch,
32 size_t optlen = strlen(opt);
34 const char *start = opts;
38 str = strstr(opts, opt);
40 if (end - start > maxlen)
43 if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) &&
44 (strpbrk(end, stopchs) == end || *end == eqch ||
54 arg_end = strpbrk(str, stopchs);
56 *arglen = min(maxlen, strlen(str)) - optlen - 1;
58 *arglen = arg_end - end - 1;
68 const char cpu_hwconfig[] __attribute__((weak)) = "";
69 const char board_hwconfig[] __attribute__((weak)) = "";
71 static const char *__hwconfig(const char *opt, size_t *arglen,
72 const char *env_hwconfig)
76 /* if we are passed a buffer use it, otherwise try the environment */
78 if (!(gd->flags & GD_FLG_ENV_READY)) {
79 printf("WARNING: Calling __hwconfig without a buffer "
80 "and before environment is ready\n");
83 env_hwconfig = env_get("hwconfig");
87 ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
88 opt, ";", ':', arglen);
93 ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
94 opt, ";", ':', arglen);
98 return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
99 opt, ";", ':', arglen);
103 * hwconfig_f - query if a particular hwconfig option is specified
104 * @opt: a string representing an option
105 * @buf: if non-NULL use this buffer to parse, otherwise try env
107 * This call can be used to find out whether U-Boot should configure
108 * a particular hardware option.
110 * Returns non-zero value if the hardware option can be used and thus
111 * should be configured, 0 otherwise.
113 * This function also returns non-zero value if CONFIG_HWCONFIG is
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.
122 int hwconfig_f(const char *opt, char *buf)
124 return !!__hwconfig(opt, NULL, buf);
128 * hwconfig_arg_f - get hwconfig option's argument
129 * @opt: a string representing an option
130 * @arglen: a pointer to an allocated size_t variable
131 * @buf: if non-NULL use this buffer to parse, otherwise try env
133 * Unlike hwconfig_f() function, this function returns a pointer to the
134 * start of the hwconfig arguments, if option is not found or it has
135 * no specified arguments, the function returns NULL pointer.
137 * If CONFIG_HWCONFIG is undefined, the function returns "", and
138 * arglen is set to 0.
140 const char *hwconfig_arg_f(const char *opt, size_t *arglen, char *buf)
142 return __hwconfig(opt, arglen, buf);
146 * hwconfig_arg_cmp_f - compare hwconfig option's argument
147 * @opt: a string representing an option
148 * @arg: a string for comparing an option's argument
149 * @buf: if non-NULL use this buffer to parse, otherwise try env
151 * This call is similar to hwconfig_arg_f, but instead of returning
152 * hwconfig argument and its length, it is comparing it to @arg.
154 * Returns non-zero value if @arg matches, 0 otherwise.
156 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
157 * value, i.e. the argument matches.
159 int hwconfig_arg_cmp_f(const char *opt, const char *arg, char *buf)
164 argstr = hwconfig_arg_f(opt, &arglen, buf);
165 if (!argstr || arglen != strlen(arg))
168 return !strncmp(argstr, arg, arglen);
172 * hwconfig_sub_f - query if a particular hwconfig sub-option is specified
173 * @opt: a string representing an option
174 * @subopt: a string representing a sub-option
175 * @buf: if non-NULL use this buffer to parse, otherwise try env
177 * This call is similar to hwconfig_f(), except that it takes additional
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
183 int hwconfig_sub_f(const char *opt, const char *subopt, char *buf)
188 arg = __hwconfig(opt, &arglen, buf);
191 return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
195 * hwconfig_subarg_f - get hwconfig sub-option's argument
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
199 * @buf: if non-NULL use this buffer to parse, otherwise try env
201 * This call is similar to hwconfig_arg_f(), except that it takes an
202 * additional argument @subopt, and so works with sub-options.
204 const char *hwconfig_subarg_f(const char *opt, const char *subopt,
205 size_t *subarglen, char *buf)
210 arg = __hwconfig(opt, &arglen, buf);
213 return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
217 * hwconfig_arg_cmp_f - compare hwconfig sub-option's argument
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
221 * @buf: if non-NULL use this buffer to parse, otherwise try env
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.
226 int hwconfig_subarg_cmp_f(const char *opt, const char *subopt,
227 const char *subarg, char *buf)
232 argstr = hwconfig_subarg_f(opt, subopt, &arglen, buf);
233 if (!argstr || arglen != strlen(subarg))
236 return !strncmp(argstr, subarg, arglen);
245 env_set("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
246 "key3;:,:=;key4", 1);
248 ret = hwconfig_arg("key1", &len);
249 printf("%zd %.*s\n", len, (int)len, ret);
251 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
252 assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len));
254 ret = hwconfig_subarg("key1", "subkey1", &len);
255 printf("%zd %.*s\n", len, (int)len, ret);
257 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
258 assert(!strncmp(ret, "value1", len));
260 ret = hwconfig_subarg("key1", "subkey2", &len);
261 printf("%zd %.*s\n", len, (int)len, ret);
263 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
264 assert(!strncmp(ret, "value2", len));
266 ret = hwconfig_arg("key2", &len);
267 printf("%zd %.*s\n", len, (int)len, ret);
269 assert(hwconfig_arg_cmp("key2", "value3"));
270 assert(!strncmp(ret, "value3", len));
272 assert(hwconfig("key3"));
273 assert(hwconfig_arg("key4", &len) == NULL);
274 assert(hwconfig_arg("bogus", &len) == NULL);
276 unenv_set("hwconfig");
278 assert(hwconfig(NULL) == 0);
279 assert(hwconfig("") == 0);
280 assert(hwconfig("key3") == 0);
284 #endif /* HWCONFIG_TEST */