blob: 193863a9701043cc1cc20e95d7bfe37cfb5a4a1f [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.
5 *
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
7 *
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.
12 */
13
Anton Vorontsov3bf74a42010-06-18 15:08:27 +040014#ifndef HWCONFIG_TEST
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040015#include <config.h>
16#include <common.h>
17#include <exports.h>
18#include <hwconfig.h>
19#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 Galac4b115f2010-10-22 03:18:13 -050074#define HWCONFIG_PRE_RELOC_BUF_SIZE 128
75
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040076static const char *__hwconfig(const char *opt, size_t *arglen)
77{
Kumar Galabb141072010-11-30 15:58:27 -060078 const char *env_hwconfig = NULL, *ret;
Kumar Galac4b115f2010-10-22 03:18:13 -050079 char buf[HWCONFIG_PRE_RELOC_BUF_SIZE];
80
81 if (gd->flags & GD_FLG_ENV_READY) {
82 env_hwconfig = getenv("hwconfig");
83 } else {
84 /*
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.
90 */
91 if ((getenv_f("hwconfig", buf, sizeof(buf))) > 0)
92 env_hwconfig = buf;
93 }
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +040094
Kumar Galabb141072010-11-30 15:58:27 -060095 if (env_hwconfig) {
96 ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +040097 opt, ";", ':', arglen);
Kumar Galabb141072010-11-30 15:58:27 -060098 if (ret)
99 return ret;
100 }
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400101
Kumar Galabb141072010-11-30 15:58:27 -0600102 ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
Kumar Galab1945772010-11-30 15:01:28 -0600103 opt, ";", ':', arglen);
Kumar Galabb141072010-11-30 15:58:27 -0600104 if (ret)
105 return ret;
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400106
Kumar Galab1945772010-11-30 15:01:28 -0600107 return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
108 opt, ";", ':', arglen);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400109}
110
111/*
112 * hwconfig - query if a particular hwconfig option is specified
113 * @opt: a string representing an option
114 *
115 * This call can be used to find out whether U-Boot should configure
116 * a particular hardware option.
117 *
118 * Returns non-zero value if the hardware option can be used and thus
119 * should be configured, 0 otherwise.
120 *
121 * This function also returns non-zero value if CONFIG_HWCONFIG is
122 * undefined.
123 *
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.
129 */
130int hwconfig(const char *opt)
131{
132 return !!__hwconfig(opt, NULL);
133}
134
135/*
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
139 *
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.
143 *
144 * If CONFIG_HWCONFIG is undefined, the function returns "", and
145 * arglen is set to 0.
146 */
147const char *hwconfig_arg(const char *opt, size_t *arglen)
148{
149 return __hwconfig(opt, arglen);
150}
151
152/*
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
156 *
157 * This call is similar to hwconfig_arg, but instead of returning
158 * hwconfig argument and its length, it is comparing it to @arg.
159 *
160 * Returns non-zero value if @arg matches, 0 otherwise.
161 *
162 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
163 * value, i.e. the argument matches.
164 */
165int hwconfig_arg_cmp(const char *opt, const char *arg)
166{
167 const char *argstr;
168 size_t arglen;
169
170 argstr = hwconfig_arg(opt, &arglen);
171 if (!argstr || arglen != strlen(arg))
172 return 0;
173
174 return !strncmp(argstr, arg, arglen);
175}
176
177/*
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
181 *
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
186 * argument.
187 */
188int hwconfig_sub(const char *opt, const char *subopt)
189{
190 size_t arglen;
191 const char *arg;
192
193 arg = __hwconfig(opt, &arglen);
194 if (!arg)
195 return 0;
Anton Vorontsov81f8d3b2010-06-18 15:08:12 +0400196 return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
Anton Vorontsov93f9dcf2009-06-10 00:25:27 +0400197}
198
199/*
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
204 *
205 * This call is similar to hwconfig_arg(), except that it takes an additional
206 * argument @subopt, and so works with sub-options.
207 */
208const char *hwconfig_subarg(const char *opt, const char *subopt,
209 size_t *subarglen)
210{
211 size_t arglen;
212 const char *arg;
213
214 arg = __hwconfig(opt, &arglen);
215 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/*
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
225 *
226 * This call is similar to hwconfig_arg_cmp, except that it takes an additional
227 * argument @subopt, and so works with sub-options.
228 */
229int hwconfig_subarg_cmp(const char *opt, const char *subopt, const char *subarg)
230{
231 const char *argstr;
232 size_t arglen;
233
234 argstr = hwconfig_subarg(opt, subopt, &arglen);
235 if (!argstr || arglen != strlen(subarg))
236 return 0;
237
238 return !strncmp(argstr, subarg, arglen);
239}
Anton Vorontsov3bf74a42010-06-18 15:08:27 +0400240
241#ifdef HWCONFIG_TEST
242int main()
243{
244 const char *ret;
245 size_t len;
246
247 setenv("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
248 "key3;:,:=;key4", 1);
249
250 ret = hwconfig_arg("key1", &len);
251 printf("%zd %.*s\n", len, (int)len, ret);
252 assert(len == 29);
253 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
254 assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len));
255
256 ret = hwconfig_subarg("key1", "subkey1", &len);
257 printf("%zd %.*s\n", len, (int)len, ret);
258 assert(len == 6);
259 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
260 assert(!strncmp(ret, "value1", len));
261
262 ret = hwconfig_subarg("key1", "subkey2", &len);
263 printf("%zd %.*s\n", len, (int)len, ret);
264 assert(len == 6);
265 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
266 assert(!strncmp(ret, "value2", len));
267
268 ret = hwconfig_arg("key2", &len);
269 printf("%zd %.*s\n", len, (int)len, ret);
270 assert(len == 6);
271 assert(hwconfig_arg_cmp("key2", "value3"));
272 assert(!strncmp(ret, "value3", len));
273
274 assert(hwconfig("key3"));
275 assert(hwconfig_arg("key4", &len) == NULL);
276 assert(hwconfig_arg("bogus", &len) == NULL);
277
278 unsetenv("hwconfig");
279
280 assert(hwconfig(NULL) == 0);
281 assert(hwconfig("") == 0);
282 assert(hwconfig("key3") == 0);
283
284 return 0;
285}
286#endif /* HWCONFIG_TEST */