blob: 1f9f4a09b4b5a2293c995a9ef4a714b7a60099d0 [file] [log] [blame]
Anton Vorontsova73904e2009-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 Vorontsov7b963732010-06-18 15:08:27 +040014#ifndef HWCONFIG_TEST
Anton Vorontsova73904e2009-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 Vorontsov7b963732010-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 Vorontsova73904e2009-06-10 00:25:27 +040028
29static const char *hwconfig_parse(const char *opts, size_t maxlen,
Anton Vorontsovfe98ce92010-06-18 15:08:12 +040030 const char *opt, char *stopchs, char eqch,
Anton Vorontsova73904e2009-06-10 00:25:27 +040031 size_t *arglen)
32{
33 size_t optlen = strlen(opt);
34 char *str;
35 const char *start = opts;
36 const char *end;
37
38next:
39 str = strstr(opts, opt);
40 end = str + optlen;
41 if (end - start > maxlen)
42 return NULL;
43
Anton Vorontsovfe98ce92010-06-18 15:08:12 +040044 if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) &&
45 (strpbrk(end, stopchs) == end || *end == eqch ||
46 *end == '\0')) {
Anton Vorontsova73904e2009-06-10 00:25:27 +040047 const char *arg_end;
48
49 if (!arglen)
50 return str;
51
52 if (*end != eqch)
53 return NULL;
54
Anton Vorontsovfe98ce92010-06-18 15:08:12 +040055 arg_end = strpbrk(str, stopchs);
Anton Vorontsova73904e2009-06-10 00:25:27 +040056 if (!arg_end)
57 *arglen = min(maxlen, strlen(str)) - optlen - 1;
58 else
59 *arglen = arg_end - end - 1;
60
61 return end + 1;
62 } else if (str) {
63 opts = end;
64 goto next;
65 }
66 return NULL;
67}
68
69const char *cpu_hwconfig __attribute__((weak));
70const char *board_hwconfig __attribute__((weak));
71
72static const char *__hwconfig(const char *opt, size_t *arglen)
73{
74 const char *env_hwconfig = getenv("hwconfig");
75
76 if (env_hwconfig)
77 return hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
Anton Vorontsovfe98ce92010-06-18 15:08:12 +040078 opt, ";", ':', arglen);
Anton Vorontsova73904e2009-06-10 00:25:27 +040079
80 if (board_hwconfig)
81 return hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
Anton Vorontsovfe98ce92010-06-18 15:08:12 +040082 opt, ";", ':', arglen);
Anton Vorontsova73904e2009-06-10 00:25:27 +040083
84 if (cpu_hwconfig)
85 return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
Anton Vorontsovfe98ce92010-06-18 15:08:12 +040086 opt, ";", ':', arglen);
Anton Vorontsova73904e2009-06-10 00:25:27 +040087
88 return NULL;
89}
90
91/*
92 * hwconfig - query if a particular hwconfig option is specified
93 * @opt: a string representing an option
94 *
95 * This call can be used to find out whether U-Boot should configure
96 * a particular hardware option.
97 *
98 * Returns non-zero value if the hardware option can be used and thus
99 * should be configured, 0 otherwise.
100 *
101 * This function also returns non-zero value if CONFIG_HWCONFIG is
102 * undefined.
103 *
104 * Returning non-zero value without CONFIG_HWCONFIG has its crucial
105 * purpose: the hwconfig() call should be a "transparent" interface,
106 * e.g. if a board doesn't need hwconfig facility, then we assume
107 * that the board file only calls things that are actually used, so
108 * hwconfig() will always return true result.
109 */
110int hwconfig(const char *opt)
111{
112 return !!__hwconfig(opt, NULL);
113}
114
115/*
116 * hwconfig_arg - get hwconfig option's argument
117 * @opt: a string representing an option
118 * @arglen: a pointer to an allocated size_t variable
119 *
120 * Unlike hwconfig() function, this function returns a pointer to the
121 * start of the hwconfig arguments, if option is not found or it has
122 * no specified arguments, the function returns NULL pointer.
123 *
124 * If CONFIG_HWCONFIG is undefined, the function returns "", and
125 * arglen is set to 0.
126 */
127const char *hwconfig_arg(const char *opt, size_t *arglen)
128{
129 return __hwconfig(opt, arglen);
130}
131
132/*
133 * hwconfig_arg_cmp - compare hwconfig option's argument
134 * @opt: a string representing an option
135 * @arg: a string for comparing an option's argument
136 *
137 * This call is similar to hwconfig_arg, but instead of returning
138 * hwconfig argument and its length, it is comparing it to @arg.
139 *
140 * Returns non-zero value if @arg matches, 0 otherwise.
141 *
142 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
143 * value, i.e. the argument matches.
144 */
145int hwconfig_arg_cmp(const char *opt, const char *arg)
146{
147 const char *argstr;
148 size_t arglen;
149
150 argstr = hwconfig_arg(opt, &arglen);
151 if (!argstr || arglen != strlen(arg))
152 return 0;
153
154 return !strncmp(argstr, arg, arglen);
155}
156
157/*
158 * hwconfig_sub - query if a particular hwconfig sub-option is specified
159 * @opt: a string representing an option
160 * @subopt: a string representing a sub-option
161 *
162 * This call is similar to hwconfig(), except that it takes additional
163 * argument @subopt. In this example:
164 * "dr_usb:mode=peripheral"
165 * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its
166 * argument.
167 */
168int hwconfig_sub(const char *opt, const char *subopt)
169{
170 size_t arglen;
171 const char *arg;
172
173 arg = __hwconfig(opt, &arglen);
174 if (!arg)
175 return 0;
Anton Vorontsovfe98ce92010-06-18 15:08:12 +0400176 return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
Anton Vorontsova73904e2009-06-10 00:25:27 +0400177}
178
179/*
180 * hwconfig_subarg - get hwconfig sub-option's argument
181 * @opt: a string representing an option
182 * @subopt: a string representing a sub-option
183 * @subarglen: a pointer to an allocated size_t variable
184 *
185 * This call is similar to hwconfig_arg(), except that it takes an additional
186 * argument @subopt, and so works with sub-options.
187 */
188const char *hwconfig_subarg(const char *opt, const char *subopt,
189 size_t *subarglen)
190{
191 size_t arglen;
192 const char *arg;
193
194 arg = __hwconfig(opt, &arglen);
195 if (!arg)
196 return NULL;
Anton Vorontsovfe98ce92010-06-18 15:08:12 +0400197 return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
Anton Vorontsova73904e2009-06-10 00:25:27 +0400198}
199
200/*
201 * hwconfig_arg_cmp - compare hwconfig sub-option's argument
202 * @opt: a string representing an option
203 * @subopt: a string representing a sub-option
204 * @subarg: a string for comparing an sub-option's argument
205 *
206 * This call is similar to hwconfig_arg_cmp, except that it takes an additional
207 * argument @subopt, and so works with sub-options.
208 */
209int hwconfig_subarg_cmp(const char *opt, const char *subopt, const char *subarg)
210{
211 const char *argstr;
212 size_t arglen;
213
214 argstr = hwconfig_subarg(opt, subopt, &arglen);
215 if (!argstr || arglen != strlen(subarg))
216 return 0;
217
218 return !strncmp(argstr, subarg, arglen);
219}
Anton Vorontsov7b963732010-06-18 15:08:27 +0400220
221#ifdef HWCONFIG_TEST
222int main()
223{
224 const char *ret;
225 size_t len;
226
227 setenv("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
228 "key3;:,:=;key4", 1);
229
230 ret = hwconfig_arg("key1", &len);
231 printf("%zd %.*s\n", len, (int)len, ret);
232 assert(len == 29);
233 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
234 assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len));
235
236 ret = hwconfig_subarg("key1", "subkey1", &len);
237 printf("%zd %.*s\n", len, (int)len, ret);
238 assert(len == 6);
239 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
240 assert(!strncmp(ret, "value1", len));
241
242 ret = hwconfig_subarg("key1", "subkey2", &len);
243 printf("%zd %.*s\n", len, (int)len, ret);
244 assert(len == 6);
245 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
246 assert(!strncmp(ret, "value2", len));
247
248 ret = hwconfig_arg("key2", &len);
249 printf("%zd %.*s\n", len, (int)len, ret);
250 assert(len == 6);
251 assert(hwconfig_arg_cmp("key2", "value3"));
252 assert(!strncmp(ret, "value3", len));
253
254 assert(hwconfig("key3"));
255 assert(hwconfig_arg("key4", &len) == NULL);
256 assert(hwconfig_arg("bogus", &len) == NULL);
257
258 unsetenv("hwconfig");
259
260 assert(hwconfig(NULL) == 0);
261 assert(hwconfig("") == 0);
262 assert(hwconfig("key3") == 0);
263
264 return 0;
265}
266#endif /* HWCONFIG_TEST */