2 * An inteface for configuring a hardware via u-boot environment.
4 * Copyright (c) 2009 MontaVista Software, Inc.
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
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.
19 #include <linux/types.h>
20 #include <linux/string.h>
26 #define min(a, b) (((a) < (b)) ? (a) : (b))
27 #endif /* HWCONFIG_TEST */
29 DECLARE_GLOBAL_DATA_PTR
;
31 static const char *hwconfig_parse(const char *opts
, size_t maxlen
,
32 const char *opt
, char *stopchs
, char eqch
,
35 size_t optlen
= strlen(opt
);
37 const char *start
= opts
;
41 str
= strstr(opts
, opt
);
43 if (end
- start
> maxlen
)
46 if (str
&& (str
== opts
|| strpbrk(str
- 1, stopchs
) == str
- 1) &&
47 (strpbrk(end
, stopchs
) == end
|| *end
== eqch
||
57 arg_end
= strpbrk(str
, stopchs
);
59 *arglen
= min(maxlen
, strlen(str
)) - optlen
- 1;
61 *arglen
= arg_end
- end
- 1;
71 const char *cpu_hwconfig
__attribute__((weak
));
72 const char *board_hwconfig
__attribute__((weak
));
74 #define HWCONFIG_PRE_RELOC_BUF_SIZE 128
76 static const char *__hwconfig(const char *opt
, size_t *arglen
)
78 const char *env_hwconfig
= NULL
;
79 char buf
[HWCONFIG_PRE_RELOC_BUF_SIZE
];
81 if (gd
->flags
& GD_FLG_ENV_READY
) {
82 env_hwconfig
= getenv("hwconfig");
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.
91 if ((getenv_f("hwconfig", buf
, sizeof(buf
))) > 0)
96 return hwconfig_parse(env_hwconfig
, strlen(env_hwconfig
),
97 opt
, ";", ':', arglen
);
100 return hwconfig_parse(board_hwconfig
, strlen(board_hwconfig
),
101 opt
, ";", ':', arglen
);
104 return hwconfig_parse(cpu_hwconfig
, strlen(cpu_hwconfig
),
105 opt
, ";", ':', arglen
);
111 * hwconfig - query if a particular hwconfig option is specified
112 * @opt: a string representing an option
114 * This call can be used to find out whether U-Boot should configure
115 * a particular hardware option.
117 * Returns non-zero value if the hardware option can be used and thus
118 * should be configured, 0 otherwise.
120 * This function also returns non-zero value if CONFIG_HWCONFIG is
123 * Returning non-zero value without CONFIG_HWCONFIG has its crucial
124 * purpose: the hwconfig() call should be a "transparent" interface,
125 * e.g. if a board doesn't need hwconfig facility, then we assume
126 * that the board file only calls things that are actually used, so
127 * hwconfig() will always return true result.
129 int hwconfig(const char *opt
)
131 return !!__hwconfig(opt
, NULL
);
135 * hwconfig_arg - get hwconfig option's argument
136 * @opt: a string representing an option
137 * @arglen: a pointer to an allocated size_t variable
139 * Unlike hwconfig() function, this function returns a pointer to the
140 * start of the hwconfig arguments, if option is not found or it has
141 * no specified arguments, the function returns NULL pointer.
143 * If CONFIG_HWCONFIG is undefined, the function returns "", and
144 * arglen is set to 0.
146 const char *hwconfig_arg(const char *opt
, size_t *arglen
)
148 return __hwconfig(opt
, arglen
);
152 * hwconfig_arg_cmp - compare hwconfig option's argument
153 * @opt: a string representing an option
154 * @arg: a string for comparing an option's argument
156 * This call is similar to hwconfig_arg, but instead of returning
157 * hwconfig argument and its length, it is comparing it to @arg.
159 * Returns non-zero value if @arg matches, 0 otherwise.
161 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
162 * value, i.e. the argument matches.
164 int hwconfig_arg_cmp(const char *opt
, const char *arg
)
169 argstr
= hwconfig_arg(opt
, &arglen
);
170 if (!argstr
|| arglen
!= strlen(arg
))
173 return !strncmp(argstr
, arg
, arglen
);
177 * hwconfig_sub - query if a particular hwconfig sub-option is specified
178 * @opt: a string representing an option
179 * @subopt: a string representing a sub-option
181 * This call is similar to hwconfig(), except that it takes additional
182 * 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
187 int hwconfig_sub(const char *opt
, const char *subopt
)
192 arg
= __hwconfig(opt
, &arglen
);
195 return !!hwconfig_parse(arg
, arglen
, subopt
, ",;", '=', NULL
);
199 * hwconfig_subarg - get hwconfig sub-option's argument
200 * @opt: a string representing an option
201 * @subopt: a string representing a sub-option
202 * @subarglen: a pointer to an allocated size_t variable
204 * This call is similar to hwconfig_arg(), except that it takes an additional
205 * argument @subopt, and so works with sub-options.
207 const char *hwconfig_subarg(const char *opt
, const char *subopt
,
213 arg
= __hwconfig(opt
, &arglen
);
216 return hwconfig_parse(arg
, arglen
, subopt
, ",;", '=', subarglen
);
220 * hwconfig_arg_cmp - compare hwconfig sub-option's argument
221 * @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
225 * This call is similar to hwconfig_arg_cmp, except that it takes an additional
226 * argument @subopt, and so works with sub-options.
228 int hwconfig_subarg_cmp(const char *opt
, const char *subopt
, const char *subarg
)
233 argstr
= hwconfig_subarg(opt
, subopt
, &arglen
);
234 if (!argstr
|| arglen
!= strlen(subarg
))
237 return !strncmp(argstr
, subarg
, arglen
);
246 setenv("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
247 "key3;:,:=;key4", 1);
249 ret
= hwconfig_arg("key1", &len
);
250 printf("%zd %.*s\n", len
, (int)len
, ret
);
252 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
253 assert(!strncmp(ret
, "subkey1=value1,subkey2=value2", len
));
255 ret
= hwconfig_subarg("key1", "subkey1", &len
);
256 printf("%zd %.*s\n", len
, (int)len
, ret
);
258 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
259 assert(!strncmp(ret
, "value1", len
));
261 ret
= hwconfig_subarg("key1", "subkey2", &len
);
262 printf("%zd %.*s\n", len
, (int)len
, ret
);
264 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
265 assert(!strncmp(ret
, "value2", len
));
267 ret
= hwconfig_arg("key2", &len
);
268 printf("%zd %.*s\n", len
, (int)len
, ret
);
270 assert(hwconfig_arg_cmp("key2", "value3"));
271 assert(!strncmp(ret
, "value3", len
));
273 assert(hwconfig("key3"));
274 assert(hwconfig_arg("key4", &len
) == NULL
);
275 assert(hwconfig_arg("bogus", &len
) == NULL
);
277 unsetenv("hwconfig");
279 assert(hwconfig(NULL
) == 0);
280 assert(hwconfig("") == 0);
281 assert(hwconfig("key3") == 0);
285 #endif /* HWCONFIG_TEST */