blob: 26a561c365b7b91eed7dd1791e391c8daad3209a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Anton Vorontsov93f9dcf2009-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 Galadd50af22011-01-09 11:37:00 -06006 * Copyright 2011 Freescale Semiconductor, Inc.
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +04007 *
8 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +04009 */
10
Anton Vorontsov3bf74a42010-06-18 15:08:27 +040011#ifndef HWCONFIG_TEST
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040012#include <config.h>
13#include <common.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060014#include <env.h>
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040015#include <exports.h>
16#include <hwconfig.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060018#include <asm/global_data.h>
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040019#include <linux/types.h>
20#include <linux/string.h>
Anton Vorontsov3bf74a42010-06-18 15:08:27 +040021#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 */
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040028
Kumar Galac4b115f2010-10-22 03:18:13 -050029DECLARE_GLOBAL_DATA_PTR;
30
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040031static const char *hwconfig_parse(const char *opts, size_t maxlen,
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +040032 const char *opt, char *stopchs, char eqch,
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040033 size_t *arglen)
34{
35 size_t optlen = strlen(opt);
36 char *str;
37 const char *start = opts;
38 const char *end;
39
40next:
41 str = strstr(opts, opt);
42 end = str + optlen;
43 if (end - start > maxlen)
44 return NULL;
45
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +040046 if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) &&
47 (strpbrk(end, stopchs) == end || *end == eqch ||
48 *end == '\0')) {
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040049 const char *arg_end;
50
51 if (!arglen)
52 return str;
53
54 if (*end != eqch)
55 return NULL;
56
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +040057 arg_end = strpbrk(str, stopchs);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040058 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
Kumar Galab1945772010-11-30 15:01:28 -060071const char cpu_hwconfig[] __attribute__((weak)) = "";
72const char board_hwconfig[] __attribute__((weak)) = "";
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040073
Kumar Galadd50af22011-01-09 11:37:00 -060074static const char *__hwconfig(const char *opt, size_t *arglen,
75 const char *env_hwconfig)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040076{
Kumar Galadd50af22011-01-09 11:37:00 -060077 const char *ret;
Kumar Galac4b115f2010-10-22 03:18:13 -050078
Kumar Galadd50af22011-01-09 11:37:00 -060079 /* 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 "
Wolfgang Denkd1a24f02011-02-02 22:36:10 +010083 "and before environment is ready\n");
84 return NULL;
Kumar Galadd50af22011-01-09 11:37:00 -060085 }
Simon Glass00caae62017-08-03 12:22:12 -060086 env_hwconfig = env_get("hwconfig");
Kumar Galac4b115f2010-10-22 03:18:13 -050087 }
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040088
Kumar Galabb141072010-11-30 15:58:27 -060089 if (env_hwconfig) {
90 ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +040091 opt, ";", ':', arglen);
Kumar Galabb141072010-11-30 15:58:27 -060092 if (ret)
93 return ret;
94 }
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040095
Kumar Galabb141072010-11-30 15:58:27 -060096 ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
Kumar Galab1945772010-11-30 15:01:28 -060097 opt, ";", ':', arglen);
Kumar Galabb141072010-11-30 15:58:27 -060098 if (ret)
99 return ret;
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400100
Kumar Galab1945772010-11-30 15:01:28 -0600101 return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
102 opt, ";", ':', arglen);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400103}
104
105/*
Kumar Galadd50af22011-01-09 11:37:00 -0600106 * hwconfig_f - query if a particular hwconfig option is specified
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400107 * @opt: a string representing an option
Kumar Galadd50af22011-01-09 11:37:00 -0600108 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400109 *
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 */
Kumar Galadd50af22011-01-09 11:37:00 -0600125int hwconfig_f(const char *opt, char *buf)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400126{
Kumar Galadd50af22011-01-09 11:37:00 -0600127 return !!__hwconfig(opt, NULL, buf);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400128}
129
130/*
Kumar Galadd50af22011-01-09 11:37:00 -0600131 * hwconfig_arg_f - get hwconfig option's argument
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400132 * @opt: a string representing an option
133 * @arglen: a pointer to an allocated size_t variable
Kumar Galadd50af22011-01-09 11:37:00 -0600134 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400135 *
Kumar Galadd50af22011-01-09 11:37:00 -0600136 * Unlike hwconfig_f() function, this function returns a pointer to the
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400137 * 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 */
Kumar Galadd50af22011-01-09 11:37:00 -0600143const char *hwconfig_arg_f(const char *opt, size_t *arglen, char *buf)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400144{
Kumar Galadd50af22011-01-09 11:37:00 -0600145 return __hwconfig(opt, arglen, buf);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400146}
147
148/*
Kumar Galadd50af22011-01-09 11:37:00 -0600149 * hwconfig_arg_cmp_f - compare hwconfig option's argument
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400150 * @opt: a string representing an option
151 * @arg: a string for comparing an option's argument
Kumar Galadd50af22011-01-09 11:37:00 -0600152 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400153 *
Kumar Galadd50af22011-01-09 11:37:00 -0600154 * This call is similar to hwconfig_arg_f, but instead of returning
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400155 * 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 */
Kumar Galadd50af22011-01-09 11:37:00 -0600162int hwconfig_arg_cmp_f(const char *opt, const char *arg, char *buf)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400163{
164 const char *argstr;
165 size_t arglen;
166
Kumar Galadd50af22011-01-09 11:37:00 -0600167 argstr = hwconfig_arg_f(opt, &arglen, buf);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400168 if (!argstr || arglen != strlen(arg))
169 return 0;
170
171 return !strncmp(argstr, arg, arglen);
172}
173
174/*
Kumar Galadd50af22011-01-09 11:37:00 -0600175 * hwconfig_sub_f - query if a particular hwconfig sub-option is specified
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400176 * @opt: a string representing an option
177 * @subopt: a string representing a sub-option
Kumar Galadd50af22011-01-09 11:37:00 -0600178 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400179 *
Kumar Galadd50af22011-01-09 11:37:00 -0600180 * This call is similar to hwconfig_f(), except that it takes additional
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400181 * argument @subopt. In this example:
182 * "dr_usb:mode=peripheral"
183 * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its
184 * argument.
185 */
Kumar Galadd50af22011-01-09 11:37:00 -0600186int hwconfig_sub_f(const char *opt, const char *subopt, char *buf)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400187{
188 size_t arglen;
189 const char *arg;
190
Kumar Galadd50af22011-01-09 11:37:00 -0600191 arg = __hwconfig(opt, &arglen, buf);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400192 if (!arg)
193 return 0;
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +0400194 return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400195}
196
197/*
Kumar Galadd50af22011-01-09 11:37:00 -0600198 * hwconfig_subarg_f - get hwconfig sub-option's argument
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400199 * @opt: a string representing an option
200 * @subopt: a string representing a sub-option
201 * @subarglen: a pointer to an allocated size_t variable
Kumar Galadd50af22011-01-09 11:37:00 -0600202 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400203 *
Kumar Galadd50af22011-01-09 11:37:00 -0600204 * This call is similar to hwconfig_arg_f(), except that it takes an
205 * additional argument @subopt, and so works with sub-options.
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400206 */
Kumar Galadd50af22011-01-09 11:37:00 -0600207const char *hwconfig_subarg_f(const char *opt, const char *subopt,
208 size_t *subarglen, char *buf)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400209{
210 size_t arglen;
211 const char *arg;
212
Kumar Galadd50af22011-01-09 11:37:00 -0600213 arg = __hwconfig(opt, &arglen, buf);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400214 if (!arg)
215 return NULL;
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +0400216 return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400217}
218
219/*
Kumar Galadd50af22011-01-09 11:37:00 -0600220 * hwconfig_arg_cmp_f - compare hwconfig sub-option's argument
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400221 * @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
Kumar Galadd50af22011-01-09 11:37:00 -0600224 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400225 *
Kumar Galadd50af22011-01-09 11:37:00 -0600226 * This call is similar to hwconfig_arg_cmp_f, except that it takes an
227 * additional argument @subopt, and so works with sub-options.
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400228 */
Kumar Galadd50af22011-01-09 11:37:00 -0600229int hwconfig_subarg_cmp_f(const char *opt, const char *subopt,
230 const char *subarg, char *buf)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400231{
232 const char *argstr;
233 size_t arglen;
234
Kumar Galadd50af22011-01-09 11:37:00 -0600235 argstr = hwconfig_subarg_f(opt, subopt, &arglen, buf);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400236 if (!argstr || arglen != strlen(subarg))
237 return 0;
238
239 return !strncmp(argstr, subarg, arglen);
240}
Anton Vorontsov3bf74a42010-06-18 15:08:27 +0400241
242#ifdef HWCONFIG_TEST
243int main()
244{
245 const char *ret;
246 size_t len;
247
Simon Glass382bee52017-08-03 12:22:09 -0600248 env_set("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
Anton Vorontsov3bf74a42010-06-18 15:08:27 +0400249 "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
Simon Glass382bee52017-08-03 12:22:09 -0600279 unenv_set("hwconfig");
Anton Vorontsov3bf74a42010-06-18 15:08:27 +0400280
281 assert(hwconfig(NULL) == 0);
282 assert(hwconfig("") == 0);
283 assert(hwconfig("key3") == 0);
284
285 return 0;
286}
287#endif /* HWCONFIG_TEST */