blob: e9e956a3223a098386ff6f47d9049da89f44fdf6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Anton Vorontsova73904e2009-06-10 00:25:27 +04002/*
3 * An inteface for configuring a hardware via u-boot environment.
4 *
5 * Copyright (c) 2009 MontaVista Software, Inc.
Kumar Gala72301602011-01-09 11:37:00 -06006 * Copyright 2011 Freescale Semiconductor, Inc.
Anton Vorontsova73904e2009-06-10 00:25:27 +04007 *
8 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
Anton Vorontsova73904e2009-06-10 00:25:27 +04009 */
10
Anton Vorontsov7b963732010-06-18 15:08:27 +040011#ifndef HWCONFIG_TEST
Anton Vorontsova73904e2009-06-10 00:25:27 +040012#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>
Anton Vorontsov7b963732010-06-18 15:08:27 +040018#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 */
Anton Vorontsova73904e2009-06-10 00:25:27 +040025
Kumar Galae9168262010-10-22 03:18:13 -050026DECLARE_GLOBAL_DATA_PTR;
27
Anton Vorontsova73904e2009-06-10 00:25:27 +040028static const char *hwconfig_parse(const char *opts, size_t maxlen,
Anton Vorontsovfe98ce92010-06-18 15:08:12 +040029 const char *opt, char *stopchs, char eqch,
Anton Vorontsova73904e2009-06-10 00:25:27 +040030 size_t *arglen)
31{
32 size_t optlen = strlen(opt);
33 char *str;
34 const char *start = opts;
35 const char *end;
36
37next:
38 str = strstr(opts, opt);
39 end = str + optlen;
40 if (end - start > maxlen)
41 return NULL;
42
Anton Vorontsovfe98ce92010-06-18 15:08:12 +040043 if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) &&
44 (strpbrk(end, stopchs) == end || *end == eqch ||
45 *end == '\0')) {
Anton Vorontsova73904e2009-06-10 00:25:27 +040046 const char *arg_end;
47
48 if (!arglen)
49 return str;
50
51 if (*end != eqch)
52 return NULL;
53
Anton Vorontsovfe98ce92010-06-18 15:08:12 +040054 arg_end = strpbrk(str, stopchs);
Anton Vorontsova73904e2009-06-10 00:25:27 +040055 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
Kumar Gala6c971692010-11-30 15:01:28 -060068const char cpu_hwconfig[] __attribute__((weak)) = "";
69const char board_hwconfig[] __attribute__((weak)) = "";
Anton Vorontsova73904e2009-06-10 00:25:27 +040070
Kumar Gala72301602011-01-09 11:37:00 -060071static const char *__hwconfig(const char *opt, size_t *arglen,
72 const char *env_hwconfig)
Anton Vorontsova73904e2009-06-10 00:25:27 +040073{
Kumar Gala72301602011-01-09 11:37:00 -060074 const char *ret;
Kumar Galae9168262010-10-22 03:18:13 -050075
Kumar Gala72301602011-01-09 11:37:00 -060076 /* 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 "
Wolfgang Denka4de8352011-02-02 22:36:10 +010080 "and before environment is ready\n");
81 return NULL;
Kumar Gala72301602011-01-09 11:37:00 -060082 }
Simon Glass64b723f2017-08-03 12:22:12 -060083 env_hwconfig = env_get("hwconfig");
Kumar Galae9168262010-10-22 03:18:13 -050084 }
Anton Vorontsova73904e2009-06-10 00:25:27 +040085
Kumar Galaadf5c242010-11-30 15:58:27 -060086 if (env_hwconfig) {
87 ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
Anton Vorontsovfe98ce92010-06-18 15:08:12 +040088 opt, ";", ':', arglen);
Kumar Galaadf5c242010-11-30 15:58:27 -060089 if (ret)
90 return ret;
91 }
Anton Vorontsova73904e2009-06-10 00:25:27 +040092
Kumar Galaadf5c242010-11-30 15:58:27 -060093 ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
Kumar Gala6c971692010-11-30 15:01:28 -060094 opt, ";", ':', arglen);
Kumar Galaadf5c242010-11-30 15:58:27 -060095 if (ret)
96 return ret;
Anton Vorontsova73904e2009-06-10 00:25:27 +040097
Kumar Gala6c971692010-11-30 15:01:28 -060098 return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
99 opt, ";", ':', arglen);
Anton Vorontsova73904e2009-06-10 00:25:27 +0400100}
101
102/*
Kumar Gala72301602011-01-09 11:37:00 -0600103 * hwconfig_f - query if a particular hwconfig option is specified
Anton Vorontsova73904e2009-06-10 00:25:27 +0400104 * @opt: a string representing an option
Kumar Gala72301602011-01-09 11:37:00 -0600105 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsova73904e2009-06-10 00:25:27 +0400106 *
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 */
Kumar Gala72301602011-01-09 11:37:00 -0600122int hwconfig_f(const char *opt, char *buf)
Anton Vorontsova73904e2009-06-10 00:25:27 +0400123{
Kumar Gala72301602011-01-09 11:37:00 -0600124 return !!__hwconfig(opt, NULL, buf);
Anton Vorontsova73904e2009-06-10 00:25:27 +0400125}
126
127/*
Kumar Gala72301602011-01-09 11:37:00 -0600128 * hwconfig_arg_f - get hwconfig option's argument
Anton Vorontsova73904e2009-06-10 00:25:27 +0400129 * @opt: a string representing an option
130 * @arglen: a pointer to an allocated size_t variable
Kumar Gala72301602011-01-09 11:37:00 -0600131 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsova73904e2009-06-10 00:25:27 +0400132 *
Kumar Gala72301602011-01-09 11:37:00 -0600133 * Unlike hwconfig_f() function, this function returns a pointer to the
Anton Vorontsova73904e2009-06-10 00:25:27 +0400134 * 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 */
Kumar Gala72301602011-01-09 11:37:00 -0600140const char *hwconfig_arg_f(const char *opt, size_t *arglen, char *buf)
Anton Vorontsova73904e2009-06-10 00:25:27 +0400141{
Kumar Gala72301602011-01-09 11:37:00 -0600142 return __hwconfig(opt, arglen, buf);
Anton Vorontsova73904e2009-06-10 00:25:27 +0400143}
144
145/*
Kumar Gala72301602011-01-09 11:37:00 -0600146 * hwconfig_arg_cmp_f - compare hwconfig option's argument
Anton Vorontsova73904e2009-06-10 00:25:27 +0400147 * @opt: a string representing an option
148 * @arg: a string for comparing an option's argument
Kumar Gala72301602011-01-09 11:37:00 -0600149 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsova73904e2009-06-10 00:25:27 +0400150 *
Kumar Gala72301602011-01-09 11:37:00 -0600151 * This call is similar to hwconfig_arg_f, but instead of returning
Anton Vorontsova73904e2009-06-10 00:25:27 +0400152 * 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 */
Kumar Gala72301602011-01-09 11:37:00 -0600159int hwconfig_arg_cmp_f(const char *opt, const char *arg, char *buf)
Anton Vorontsova73904e2009-06-10 00:25:27 +0400160{
161 const char *argstr;
162 size_t arglen;
163
Kumar Gala72301602011-01-09 11:37:00 -0600164 argstr = hwconfig_arg_f(opt, &arglen, buf);
Anton Vorontsova73904e2009-06-10 00:25:27 +0400165 if (!argstr || arglen != strlen(arg))
166 return 0;
167
168 return !strncmp(argstr, arg, arglen);
169}
170
171/*
Kumar Gala72301602011-01-09 11:37:00 -0600172 * hwconfig_sub_f - query if a particular hwconfig sub-option is specified
Anton Vorontsova73904e2009-06-10 00:25:27 +0400173 * @opt: a string representing an option
174 * @subopt: a string representing a sub-option
Kumar Gala72301602011-01-09 11:37:00 -0600175 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsova73904e2009-06-10 00:25:27 +0400176 *
Kumar Gala72301602011-01-09 11:37:00 -0600177 * This call is similar to hwconfig_f(), except that it takes additional
Anton Vorontsova73904e2009-06-10 00:25:27 +0400178 * 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 */
Kumar Gala72301602011-01-09 11:37:00 -0600183int hwconfig_sub_f(const char *opt, const char *subopt, char *buf)
Anton Vorontsova73904e2009-06-10 00:25:27 +0400184{
185 size_t arglen;
186 const char *arg;
187
Kumar Gala72301602011-01-09 11:37:00 -0600188 arg = __hwconfig(opt, &arglen, buf);
Anton Vorontsova73904e2009-06-10 00:25:27 +0400189 if (!arg)
190 return 0;
Anton Vorontsovfe98ce92010-06-18 15:08:12 +0400191 return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
Anton Vorontsova73904e2009-06-10 00:25:27 +0400192}
193
194/*
Kumar Gala72301602011-01-09 11:37:00 -0600195 * hwconfig_subarg_f - get hwconfig sub-option's argument
Anton Vorontsova73904e2009-06-10 00:25:27 +0400196 * @opt: a string representing an option
197 * @subopt: a string representing a sub-option
198 * @subarglen: a pointer to an allocated size_t variable
Kumar Gala72301602011-01-09 11:37:00 -0600199 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsova73904e2009-06-10 00:25:27 +0400200 *
Kumar Gala72301602011-01-09 11:37:00 -0600201 * This call is similar to hwconfig_arg_f(), except that it takes an
202 * additional argument @subopt, and so works with sub-options.
Anton Vorontsova73904e2009-06-10 00:25:27 +0400203 */
Kumar Gala72301602011-01-09 11:37:00 -0600204const char *hwconfig_subarg_f(const char *opt, const char *subopt,
205 size_t *subarglen, char *buf)
Anton Vorontsova73904e2009-06-10 00:25:27 +0400206{
207 size_t arglen;
208 const char *arg;
209
Kumar Gala72301602011-01-09 11:37:00 -0600210 arg = __hwconfig(opt, &arglen, buf);
Anton Vorontsova73904e2009-06-10 00:25:27 +0400211 if (!arg)
212 return NULL;
Anton Vorontsovfe98ce92010-06-18 15:08:12 +0400213 return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
Anton Vorontsova73904e2009-06-10 00:25:27 +0400214}
215
216/*
Kumar Gala72301602011-01-09 11:37:00 -0600217 * hwconfig_arg_cmp_f - compare hwconfig sub-option's argument
Anton Vorontsova73904e2009-06-10 00:25:27 +0400218 * @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
Kumar Gala72301602011-01-09 11:37:00 -0600221 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsova73904e2009-06-10 00:25:27 +0400222 *
Kumar Gala72301602011-01-09 11:37:00 -0600223 * This call is similar to hwconfig_arg_cmp_f, except that it takes an
224 * additional argument @subopt, and so works with sub-options.
Anton Vorontsova73904e2009-06-10 00:25:27 +0400225 */
Kumar Gala72301602011-01-09 11:37:00 -0600226int hwconfig_subarg_cmp_f(const char *opt, const char *subopt,
227 const char *subarg, char *buf)
Anton Vorontsova73904e2009-06-10 00:25:27 +0400228{
229 const char *argstr;
230 size_t arglen;
231
Kumar Gala72301602011-01-09 11:37:00 -0600232 argstr = hwconfig_subarg_f(opt, subopt, &arglen, buf);
Anton Vorontsova73904e2009-06-10 00:25:27 +0400233 if (!argstr || arglen != strlen(subarg))
234 return 0;
235
236 return !strncmp(argstr, subarg, arglen);
237}
Anton Vorontsov7b963732010-06-18 15:08:27 +0400238
239#ifdef HWCONFIG_TEST
240int main()
241{
242 const char *ret;
243 size_t len;
244
Simon Glass6a38e412017-08-03 12:22:09 -0600245 env_set("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
Anton Vorontsov7b963732010-06-18 15:08:27 +0400246 "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
Simon Glass6a38e412017-08-03 12:22:09 -0600276 unenv_set("hwconfig");
Anton Vorontsov7b963732010-06-18 15:08:27 +0400277
278 assert(hwconfig(NULL) == 0);
279 assert(hwconfig("") == 0);
280 assert(hwconfig("key3") == 0);
281
282 return 0;
283}
284#endif /* HWCONFIG_TEST */