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.
18 #include <linux/types.h>
19 #include <linux/string.h>
21 static const char *hwconfig_parse(const char *opts, size_t maxlen,
22 const char *opt, char *stopchs, char eqch,
25 size_t optlen = strlen(opt);
27 const char *start = opts;
31 str = strstr(opts, opt);
33 if (end - start > maxlen)
36 if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) &&
37 (strpbrk(end, stopchs) == end || *end == eqch ||
47 arg_end = strpbrk(str, stopchs);
49 *arglen = min(maxlen, strlen(str)) - optlen - 1;
51 *arglen = arg_end - end - 1;
61 const char *cpu_hwconfig __attribute__((weak));
62 const char *board_hwconfig __attribute__((weak));
64 static const char *__hwconfig(const char *opt, size_t *arglen)
66 const char *env_hwconfig = getenv("hwconfig");
69 return hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
70 opt, ";", ':', arglen);
73 return hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
74 opt, ";", ':', arglen);
77 return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
78 opt, ";", ':', arglen);
84 * hwconfig - query if a particular hwconfig option is specified
85 * @opt: a string representing an option
87 * This call can be used to find out whether U-Boot should configure
88 * a particular hardware option.
90 * Returns non-zero value if the hardware option can be used and thus
91 * should be configured, 0 otherwise.
93 * This function also returns non-zero value if CONFIG_HWCONFIG is
96 * Returning non-zero value without CONFIG_HWCONFIG has its crucial
97 * purpose: the hwconfig() call should be a "transparent" interface,
98 * e.g. if a board doesn't need hwconfig facility, then we assume
99 * that the board file only calls things that are actually used, so
100 * hwconfig() will always return true result.
102 int hwconfig(const char *opt)
104 return !!__hwconfig(opt, NULL);
108 * hwconfig_arg - get hwconfig option's argument
109 * @opt: a string representing an option
110 * @arglen: a pointer to an allocated size_t variable
112 * Unlike hwconfig() function, this function returns a pointer to the
113 * start of the hwconfig arguments, if option is not found or it has
114 * no specified arguments, the function returns NULL pointer.
116 * If CONFIG_HWCONFIG is undefined, the function returns "", and
117 * arglen is set to 0.
119 const char *hwconfig_arg(const char *opt, size_t *arglen)
121 return __hwconfig(opt, arglen);
125 * hwconfig_arg_cmp - compare hwconfig option's argument
126 * @opt: a string representing an option
127 * @arg: a string for comparing an option's argument
129 * This call is similar to hwconfig_arg, but instead of returning
130 * hwconfig argument and its length, it is comparing it to @arg.
132 * Returns non-zero value if @arg matches, 0 otherwise.
134 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
135 * value, i.e. the argument matches.
137 int hwconfig_arg_cmp(const char *opt, const char *arg)
142 argstr = hwconfig_arg(opt, &arglen);
143 if (!argstr || arglen != strlen(arg))
146 return !strncmp(argstr, arg, arglen);
150 * hwconfig_sub - query if a particular hwconfig sub-option is specified
151 * @opt: a string representing an option
152 * @subopt: a string representing a sub-option
154 * This call is similar to hwconfig(), except that it takes additional
155 * argument @subopt. In this example:
156 * "dr_usb:mode=peripheral"
157 * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its
160 int hwconfig_sub(const char *opt, const char *subopt)
165 arg = __hwconfig(opt, &arglen);
168 return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
172 * hwconfig_subarg - get hwconfig sub-option's argument
173 * @opt: a string representing an option
174 * @subopt: a string representing a sub-option
175 * @subarglen: a pointer to an allocated size_t variable
177 * This call is similar to hwconfig_arg(), except that it takes an additional
178 * argument @subopt, and so works with sub-options.
180 const char *hwconfig_subarg(const char *opt, const char *subopt,
186 arg = __hwconfig(opt, &arglen);
189 return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
193 * hwconfig_arg_cmp - compare hwconfig sub-option's argument
194 * @opt: a string representing an option
195 * @subopt: a string representing a sub-option
196 * @subarg: a string for comparing an sub-option's argument
198 * This call is similar to hwconfig_arg_cmp, except that it takes an additional
199 * argument @subopt, and so works with sub-options.
201 int hwconfig_subarg_cmp(const char *opt, const char *subopt, const char *subarg)
206 argstr = hwconfig_subarg(opt, subopt, &arglen);
207 if (!argstr || arglen != strlen(subarg))
210 return !strncmp(argstr, subarg, arglen);