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>
18 #include <asm/global_data.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
26 #define min(a, b) (((a) < (b)) ? (a) : (b))
27 #endif /* HWCONFIG_TEST */
29 DECLARE_GLOBAL_DATA_PTR;
31 static const char *hwconfig_parse(const char *opts, size_t maxlen,
32 const char *opt, char *stopchs, char eqch,
35 size_t optlen = strlen(opt);
37 const char *start = opts;
41 str = strstr(opts, opt);
43 if (end - start > maxlen)
46 if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) &&
47 (strpbrk(end, stopchs) == end || *end == eqch ||
57 arg_end = strpbrk(str, stopchs);
59 *arglen = min(maxlen, strlen(str)) - optlen - 1;
61 *arglen = arg_end - end - 1;
71 const char cpu_hwconfig[] __attribute__((weak)) = "";
72 const char board_hwconfig[] __attribute__((weak)) = "";
74 static const char *__hwconfig(const char *opt, size_t *arglen,
75 const char *env_hwconfig)
79 /* if we are passed a buffer use it, otherwise try the environment */
81 if (!(gd->flags & GD_FLG_ENV_READY)) {
82 printf("WARNING: Calling __hwconfig without a buffer "
83 "and before environment is ready\n");
86 #if CONFIG_IS_ENABLED(ENV_SUPPORT)
87 env_hwconfig = env_get("hwconfig");
92 ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
93 opt, ";", ':', arglen);
98 ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
99 opt, ";", ':', arglen);
103 return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
104 opt, ";", ':', arglen);
108 * hwconfig_f - query if a particular hwconfig option is specified
109 * @opt: a string representing an option
110 * @buf: if non-NULL use this buffer to parse, otherwise try env
112 * This call can be used to find out whether U-Boot should configure
113 * a particular hardware option.
115 * Returns non-zero value if the hardware option can be used and thus
116 * should be configured, 0 otherwise.
118 * This function also returns non-zero value if CONFIG_HWCONFIG is
121 * Returning non-zero value without CONFIG_HWCONFIG has its crucial
122 * purpose: the hwconfig() call should be a "transparent" interface,
123 * e.g. if a board doesn't need hwconfig facility, then we assume
124 * that the board file only calls things that are actually used, so
125 * hwconfig() will always return true result.
127 int hwconfig_f(const char *opt, char *buf)
129 return !!__hwconfig(opt, NULL, buf);
133 * hwconfig_arg_f - get hwconfig option's argument
134 * @opt: a string representing an option
135 * @arglen: a pointer to an allocated size_t variable
136 * @buf: if non-NULL use this buffer to parse, otherwise try env
138 * Unlike hwconfig_f() function, this function returns a pointer to the
139 * start of the hwconfig arguments, if option is not found or it has
140 * no specified arguments, the function returns NULL pointer.
142 * If CONFIG_HWCONFIG is undefined, the function returns "", and
143 * arglen is set to 0.
145 const char *hwconfig_arg_f(const char *opt, size_t *arglen, char *buf)
147 return __hwconfig(opt, arglen, buf);
151 * hwconfig_arg_cmp_f - compare hwconfig option's argument
152 * @opt: a string representing an option
153 * @arg: a string for comparing an option's argument
154 * @buf: if non-NULL use this buffer to parse, otherwise try env
156 * This call is similar to hwconfig_arg_f, but instead of returning
157 * hwconfig argument and its length, it is comparing it to @arg.
159 * Returns non-zero value if @arg matches, 0 otherwise.
161 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
162 * value, i.e. the argument matches.
164 int hwconfig_arg_cmp_f(const char *opt, const char *arg, char *buf)
169 argstr = hwconfig_arg_f(opt, &arglen, buf);
170 if (!argstr || arglen != strlen(arg))
173 return !strncmp(argstr, arg, arglen);
177 * hwconfig_sub_f - query if a particular hwconfig sub-option is specified
178 * @opt: a string representing an option
179 * @subopt: a string representing a sub-option
180 * @buf: if non-NULL use this buffer to parse, otherwise try env
182 * This call is similar to hwconfig_f(), except that it takes additional
183 * argument @subopt. In this example:
184 * "dr_usb:mode=peripheral"
185 * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its
188 int hwconfig_sub_f(const char *opt, const char *subopt, char *buf)
193 arg = __hwconfig(opt, &arglen, buf);
196 return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
200 * hwconfig_subarg_f - get hwconfig sub-option's argument
201 * @opt: a string representing an option
202 * @subopt: a string representing a sub-option
203 * @subarglen: a pointer to an allocated size_t variable
204 * @buf: if non-NULL use this buffer to parse, otherwise try env
206 * This call is similar to hwconfig_arg_f(), except that it takes an
207 * additional argument @subopt, and so works with sub-options.
209 const char *hwconfig_subarg_f(const char *opt, const char *subopt,
210 size_t *subarglen, char *buf)
215 arg = __hwconfig(opt, &arglen, buf);
218 return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
222 * hwconfig_arg_cmp_f - compare hwconfig sub-option's argument
223 * @opt: a string representing an option
224 * @subopt: a string representing a sub-option
225 * @subarg: a string for comparing an sub-option's argument
226 * @buf: if non-NULL use this buffer to parse, otherwise try env
228 * This call is similar to hwconfig_arg_cmp_f, except that it takes an
229 * additional argument @subopt, and so works with sub-options.
231 int hwconfig_subarg_cmp_f(const char *opt, const char *subopt,
232 const char *subarg, char *buf)
237 argstr = hwconfig_subarg_f(opt, subopt, &arglen, buf);
238 if (!argstr || arglen != strlen(subarg))
241 return !strncmp(argstr, subarg, arglen);
250 env_set("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
251 "key3;:,:=;key4", 1);
253 ret = hwconfig_arg("key1", &len);
254 printf("%zd %.*s\n", len, (int)len, ret);
256 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
257 assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len));
259 ret = hwconfig_subarg("key1", "subkey1", &len);
260 printf("%zd %.*s\n", len, (int)len, ret);
262 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
263 assert(!strncmp(ret, "value1", len));
265 ret = hwconfig_subarg("key1", "subkey2", &len);
266 printf("%zd %.*s\n", len, (int)len, ret);
268 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
269 assert(!strncmp(ret, "value2", len));
271 ret = hwconfig_arg("key2", &len);
272 printf("%zd %.*s\n", len, (int)len, ret);
274 assert(hwconfig_arg_cmp("key2", "value3"));
275 assert(!strncmp(ret, "value3", len));
277 assert(hwconfig("key3"));
278 assert(hwconfig_arg("key4", &len) == NULL);
279 assert(hwconfig_arg("bogus", &len) == NULL);
281 unenv_set("hwconfig");
283 assert(hwconfig(NULL) == 0);
284 assert(hwconfig("") == 0);
285 assert(hwconfig("key3") == 0);
289 #endif /* HWCONFIG_TEST */