blob: e47d4d742066eb2b221833293c8057208c3dfb9b [file] [log] [blame]
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +04001/*
2 * An inteface for configuring a hardware via u-boot environment.
3 *
4 * Copyright (c) 2009 MontaVista Software, Inc.
Kumar Galadd50af22011-01-09 11:37:00 -06005 * Copyright 2011 Freescale Semiconductor, Inc.
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +04006 *
7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 */
14
Anton Vorontsov3bf74a42010-06-18 15:08:27 +040015#ifndef HWCONFIG_TEST
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040016#include <config.h>
17#include <common.h>
18#include <exports.h>
19#include <hwconfig.h>
20#include <linux/types.h>
21#include <linux/string.h>
Anton Vorontsov3bf74a42010-06-18 15:08:27 +040022#else
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <assert.h>
27#define min(a, b) (((a) < (b)) ? (a) : (b))
28#endif /* HWCONFIG_TEST */
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040029
Kumar Galac4b115f2010-10-22 03:18:13 -050030DECLARE_GLOBAL_DATA_PTR;
31
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040032static const char *hwconfig_parse(const char *opts, size_t maxlen,
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +040033 const char *opt, char *stopchs, char eqch,
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040034 size_t *arglen)
35{
36 size_t optlen = strlen(opt);
37 char *str;
38 const char *start = opts;
39 const char *end;
40
41next:
42 str = strstr(opts, opt);
43 end = str + optlen;
44 if (end - start > maxlen)
45 return NULL;
46
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +040047 if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) &&
48 (strpbrk(end, stopchs) == end || *end == eqch ||
49 *end == '\0')) {
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040050 const char *arg_end;
51
52 if (!arglen)
53 return str;
54
55 if (*end != eqch)
56 return NULL;
57
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +040058 arg_end = strpbrk(str, stopchs);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040059 if (!arg_end)
60 *arglen = min(maxlen, strlen(str)) - optlen - 1;
61 else
62 *arglen = arg_end - end - 1;
63
64 return end + 1;
65 } else if (str) {
66 opts = end;
67 goto next;
68 }
69 return NULL;
70}
71
Kumar Galab1945772010-11-30 15:01:28 -060072const char cpu_hwconfig[] __attribute__((weak)) = "";
73const char board_hwconfig[] __attribute__((weak)) = "";
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040074
Kumar Galadd50af22011-01-09 11:37:00 -060075static const char *__hwconfig(const char *opt, size_t *arglen,
76 const char *env_hwconfig)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040077{
Kumar Galadd50af22011-01-09 11:37:00 -060078 const char *ret;
Kumar Galac4b115f2010-10-22 03:18:13 -050079
Kumar Galadd50af22011-01-09 11:37:00 -060080 /* if we are passed a buffer use it, otherwise try the environment */
81 if (!env_hwconfig) {
82 if (!(gd->flags & GD_FLG_ENV_READY)) {
83 printf("WARNING: Calling __hwconfig without a buffer "
Wolfgang Denkd1a24f02011-02-02 22:36:10 +010084 "and before environment is ready\n");
85 return NULL;
Kumar Galadd50af22011-01-09 11:37:00 -060086 }
Kumar Galac4b115f2010-10-22 03:18:13 -050087 env_hwconfig = getenv("hwconfig");
Kumar Galac4b115f2010-10-22 03:18:13 -050088 }
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040089
Kumar Galabb141072010-11-30 15:58:27 -060090 if (env_hwconfig) {
91 ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +040092 opt, ";", ':', arglen);
Kumar Galabb141072010-11-30 15:58:27 -060093 if (ret)
94 return ret;
95 }
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040096
Kumar Galabb141072010-11-30 15:58:27 -060097 ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
Kumar Galab1945772010-11-30 15:01:28 -060098 opt, ";", ':', arglen);
Kumar Galabb141072010-11-30 15:58:27 -060099 if (ret)
100 return ret;
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400101
Kumar Galab1945772010-11-30 15:01:28 -0600102 return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
103 opt, ";", ':', arglen);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400104}
105
106/*
Kumar Galadd50af22011-01-09 11:37:00 -0600107 * hwconfig_f - query if a particular hwconfig option is specified
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400108 * @opt: a string representing an option
Kumar Galadd50af22011-01-09 11:37:00 -0600109 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400110 *
111 * This call can be used to find out whether U-Boot should configure
112 * a particular hardware option.
113 *
114 * Returns non-zero value if the hardware option can be used and thus
115 * should be configured, 0 otherwise.
116 *
117 * This function also returns non-zero value if CONFIG_HWCONFIG is
118 * undefined.
119 *
120 * Returning non-zero value without CONFIG_HWCONFIG has its crucial
121 * purpose: the hwconfig() call should be a "transparent" interface,
122 * e.g. if a board doesn't need hwconfig facility, then we assume
123 * that the board file only calls things that are actually used, so
124 * hwconfig() will always return true result.
125 */
Kumar Galadd50af22011-01-09 11:37:00 -0600126int hwconfig_f(const char *opt, char *buf)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400127{
Kumar Galadd50af22011-01-09 11:37:00 -0600128 return !!__hwconfig(opt, NULL, buf);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400129}
130
131/*
Kumar Galadd50af22011-01-09 11:37:00 -0600132 * hwconfig_arg_f - get hwconfig option's argument
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400133 * @opt: a string representing an option
134 * @arglen: a pointer to an allocated size_t variable
Kumar Galadd50af22011-01-09 11:37:00 -0600135 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400136 *
Kumar Galadd50af22011-01-09 11:37:00 -0600137 * Unlike hwconfig_f() function, this function returns a pointer to the
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400138 * start of the hwconfig arguments, if option is not found or it has
139 * no specified arguments, the function returns NULL pointer.
140 *
141 * If CONFIG_HWCONFIG is undefined, the function returns "", and
142 * arglen is set to 0.
143 */
Kumar Galadd50af22011-01-09 11:37:00 -0600144const char *hwconfig_arg_f(const char *opt, size_t *arglen, char *buf)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400145{
Kumar Galadd50af22011-01-09 11:37:00 -0600146 return __hwconfig(opt, arglen, buf);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400147}
148
149/*
Kumar Galadd50af22011-01-09 11:37:00 -0600150 * hwconfig_arg_cmp_f - compare hwconfig option's argument
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400151 * @opt: a string representing an option
152 * @arg: a string for comparing an option's argument
Kumar Galadd50af22011-01-09 11:37:00 -0600153 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400154 *
Kumar Galadd50af22011-01-09 11:37:00 -0600155 * This call is similar to hwconfig_arg_f, but instead of returning
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400156 * hwconfig argument and its length, it is comparing it to @arg.
157 *
158 * Returns non-zero value if @arg matches, 0 otherwise.
159 *
160 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
161 * value, i.e. the argument matches.
162 */
Kumar Galadd50af22011-01-09 11:37:00 -0600163int hwconfig_arg_cmp_f(const char *opt, const char *arg, char *buf)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400164{
165 const char *argstr;
166 size_t arglen;
167
Kumar Galadd50af22011-01-09 11:37:00 -0600168 argstr = hwconfig_arg_f(opt, &arglen, buf);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400169 if (!argstr || arglen != strlen(arg))
170 return 0;
171
172 return !strncmp(argstr, arg, arglen);
173}
174
175/*
Kumar Galadd50af22011-01-09 11:37:00 -0600176 * hwconfig_sub_f - query if a particular hwconfig sub-option is specified
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400177 * @opt: a string representing an option
178 * @subopt: a string representing a sub-option
Kumar Galadd50af22011-01-09 11:37:00 -0600179 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400180 *
Kumar Galadd50af22011-01-09 11:37:00 -0600181 * This call is similar to hwconfig_f(), except that it takes additional
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400182 * argument @subopt. In this example:
183 * "dr_usb:mode=peripheral"
184 * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its
185 * argument.
186 */
Kumar Galadd50af22011-01-09 11:37:00 -0600187int hwconfig_sub_f(const char *opt, const char *subopt, char *buf)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400188{
189 size_t arglen;
190 const char *arg;
191
Kumar Galadd50af22011-01-09 11:37:00 -0600192 arg = __hwconfig(opt, &arglen, buf);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400193 if (!arg)
194 return 0;
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +0400195 return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400196}
197
198/*
Kumar Galadd50af22011-01-09 11:37:00 -0600199 * hwconfig_subarg_f - get hwconfig sub-option's argument
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400200 * @opt: a string representing an option
201 * @subopt: a string representing a sub-option
202 * @subarglen: a pointer to an allocated size_t variable
Kumar Galadd50af22011-01-09 11:37:00 -0600203 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400204 *
Kumar Galadd50af22011-01-09 11:37:00 -0600205 * This call is similar to hwconfig_arg_f(), except that it takes an
206 * additional argument @subopt, and so works with sub-options.
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400207 */
Kumar Galadd50af22011-01-09 11:37:00 -0600208const char *hwconfig_subarg_f(const char *opt, const char *subopt,
209 size_t *subarglen, char *buf)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400210{
211 size_t arglen;
212 const char *arg;
213
Kumar Galadd50af22011-01-09 11:37:00 -0600214 arg = __hwconfig(opt, &arglen, buf);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400215 if (!arg)
216 return NULL;
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +0400217 return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400218}
219
220/*
Kumar Galadd50af22011-01-09 11:37:00 -0600221 * hwconfig_arg_cmp_f - compare hwconfig sub-option's argument
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400222 * @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
Kumar Galadd50af22011-01-09 11:37:00 -0600225 * @buf: if non-NULL use this buffer to parse, otherwise try env
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400226 *
Kumar Galadd50af22011-01-09 11:37:00 -0600227 * This call is similar to hwconfig_arg_cmp_f, except that it takes an
228 * additional argument @subopt, and so works with sub-options.
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400229 */
Kumar Galadd50af22011-01-09 11:37:00 -0600230int hwconfig_subarg_cmp_f(const char *opt, const char *subopt,
231 const char *subarg, char *buf)
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400232{
233 const char *argstr;
234 size_t arglen;
235
Kumar Galadd50af22011-01-09 11:37:00 -0600236 argstr = hwconfig_subarg_f(opt, subopt, &arglen, buf);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400237 if (!argstr || arglen != strlen(subarg))
238 return 0;
239
240 return !strncmp(argstr, subarg, arglen);
241}
Anton Vorontsov3bf74a42010-06-18 15:08:27 +0400242
243#ifdef HWCONFIG_TEST
244int main()
245{
246 const char *ret;
247 size_t len;
248
249 setenv("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
250 "key3;:,:=;key4", 1);
251
252 ret = hwconfig_arg("key1", &len);
253 printf("%zd %.*s\n", len, (int)len, ret);
254 assert(len == 29);
255 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
256 assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len));
257
258 ret = hwconfig_subarg("key1", "subkey1", &len);
259 printf("%zd %.*s\n", len, (int)len, ret);
260 assert(len == 6);
261 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
262 assert(!strncmp(ret, "value1", len));
263
264 ret = hwconfig_subarg("key1", "subkey2", &len);
265 printf("%zd %.*s\n", len, (int)len, ret);
266 assert(len == 6);
267 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
268 assert(!strncmp(ret, "value2", len));
269
270 ret = hwconfig_arg("key2", &len);
271 printf("%zd %.*s\n", len, (int)len, ret);
272 assert(len == 6);
273 assert(hwconfig_arg_cmp("key2", "value3"));
274 assert(!strncmp(ret, "value3", len));
275
276 assert(hwconfig("key3"));
277 assert(hwconfig_arg("key4", &len) == NULL);
278 assert(hwconfig_arg("bogus", &len) == NULL);
279
280 unsetenv("hwconfig");
281
282 assert(hwconfig(NULL) == 0);
283 assert(hwconfig("") == 0);
284 assert(hwconfig("key3") == 0);
285
286 return 0;
287}
288#endif /* HWCONFIG_TEST */