2 * An inteface for configuring a hardware via u-boot environment.
4 * Copyright (c) 2009 MontaVista Software, Inc.
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
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 #define HWCONFIG_PRE_RELOC_BUF_SIZE 128
76 static const char *__hwconfig(const char *opt, size_t *arglen)
78 const char *env_hwconfig = NULL, *ret;
79 char buf[HWCONFIG_PRE_RELOC_BUF_SIZE];
81 if (gd->flags & GD_FLG_ENV_READY) {
82 env_hwconfig = getenv("hwconfig");
85 * Use our own on stack based buffer before relocation to allow
86 * accessing longer hwconfig strings that might be in the
87 * environment before we've relocated. This is pretty fragile
88 * on both the use of stack and if the buffer is big enough.
89 * However we will get a warning from getenv_f for the later.
91 if ((getenv_f("hwconfig", buf, sizeof(buf))) > 0)
96 ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
97 opt, ";", ':', arglen);
102 ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
103 opt, ";", ':', arglen);
107 return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
108 opt, ";", ':', arglen);
112 * hwconfig - query if a particular hwconfig option is specified
113 * @opt: a string representing an option
115 * This call can be used to find out whether U-Boot should configure
116 * a particular hardware option.
118 * Returns non-zero value if the hardware option can be used and thus
119 * should be configured, 0 otherwise.
121 * This function also returns non-zero value if CONFIG_HWCONFIG is
124 * Returning non-zero value without CONFIG_HWCONFIG has its crucial
125 * purpose: the hwconfig() call should be a "transparent" interface,
126 * e.g. if a board doesn't need hwconfig facility, then we assume
127 * that the board file only calls things that are actually used, so
128 * hwconfig() will always return true result.
130 int hwconfig(const char *opt)
132 return !!__hwconfig(opt, NULL);
136 * hwconfig_arg - get hwconfig option's argument
137 * @opt: a string representing an option
138 * @arglen: a pointer to an allocated size_t variable
140 * Unlike hwconfig() function, this function returns a pointer to the
141 * start of the hwconfig arguments, if option is not found or it has
142 * no specified arguments, the function returns NULL pointer.
144 * If CONFIG_HWCONFIG is undefined, the function returns "", and
145 * arglen is set to 0.
147 const char *hwconfig_arg(const char *opt, size_t *arglen)
149 return __hwconfig(opt, arglen);
153 * hwconfig_arg_cmp - compare hwconfig option's argument
154 * @opt: a string representing an option
155 * @arg: a string for comparing an option's argument
157 * This call is similar to hwconfig_arg, but instead of returning
158 * hwconfig argument and its length, it is comparing it to @arg.
160 * Returns non-zero value if @arg matches, 0 otherwise.
162 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
163 * value, i.e. the argument matches.
165 int hwconfig_arg_cmp(const char *opt, const char *arg)
170 argstr = hwconfig_arg(opt, &arglen);
171 if (!argstr || arglen != strlen(arg))
174 return !strncmp(argstr, arg, arglen);
178 * hwconfig_sub - query if a particular hwconfig sub-option is specified
179 * @opt: a string representing an option
180 * @subopt: a string representing a sub-option
182 * This call is similar to hwconfig(), 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(const char *opt, const char *subopt)
193 arg = __hwconfig(opt, &arglen);
196 return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
200 * hwconfig_subarg - 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
205 * This call is similar to hwconfig_arg(), except that it takes an additional
206 * argument @subopt, and so works with sub-options.
208 const char *hwconfig_subarg(const char *opt, const char *subopt,
214 arg = __hwconfig(opt, &arglen);
217 return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
221 * hwconfig_arg_cmp - compare hwconfig sub-option's argument
222 * @opt: a string representing an option
223 * @subopt: a string representing a sub-option
224 * @subarg: a string for comparing an sub-option's argument
226 * This call is similar to hwconfig_arg_cmp, except that it takes an additional
227 * argument @subopt, and so works with sub-options.
229 int hwconfig_subarg_cmp(const char *opt, const char *subopt, const char *subarg)
234 argstr = hwconfig_subarg(opt, subopt, &arglen);
235 if (!argstr || arglen != strlen(subarg))
238 return !strncmp(argstr, subarg, arglen);
247 setenv("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
248 "key3;:,:=;key4", 1);
250 ret = hwconfig_arg("key1", &len);
251 printf("%zd %.*s\n", len, (int)len, ret);
253 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
254 assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len));
256 ret = hwconfig_subarg("key1", "subkey1", &len);
257 printf("%zd %.*s\n", len, (int)len, ret);
259 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
260 assert(!strncmp(ret, "value1", len));
262 ret = hwconfig_subarg("key1", "subkey2", &len);
263 printf("%zd %.*s\n", len, (int)len, ret);
265 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
266 assert(!strncmp(ret, "value2", len));
268 ret = hwconfig_arg("key2", &len);
269 printf("%zd %.*s\n", len, (int)len, ret);
271 assert(hwconfig_arg_cmp("key2", "value3"));
272 assert(!strncmp(ret, "value3", len));
274 assert(hwconfig("key3"));
275 assert(hwconfig_arg("key4", &len) == NULL);
276 assert(hwconfig_arg("bogus", &len) == NULL);
278 unsetenv("hwconfig");
280 assert(hwconfig(NULL) == 0);
281 assert(hwconfig("") == 0);
282 assert(hwconfig("key3") == 0);
286 #endif /* HWCONFIG_TEST */