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
);
99 return hwconfig_parse(board_hwconfig
, strlen(board_hwconfig
),
100 opt
, ";", ':', arglen
);
102 return hwconfig_parse(cpu_hwconfig
, strlen(cpu_hwconfig
),
103 opt
, ";", ':', arglen
);
109 * hwconfig - query if a particular hwconfig option is specified
110 * @opt: a string representing an option
112 * This call can be used to find out whether U-Boot should configure
113 * a particular hardware option.
115 * Returns non-zero value if the hardware option can be used and thus
116 * should be configured, 0 otherwise.
118 * This function also returns non-zero value if CONFIG_HWCONFIG is
121 * Returning non-zero value without CONFIG_HWCONFIG has its crucial
122 * purpose: the hwconfig() call should be a "transparent" interface,
123 * e.g. if a board doesn't need hwconfig facility, then we assume
124 * that the board file only calls things that are actually used, so
125 * hwconfig() will always return true result.
127 int hwconfig(const char *opt
)
129 return !!__hwconfig(opt
, NULL
);
133 * hwconfig_arg - get hwconfig option's argument
134 * @opt: a string representing an option
135 * @arglen: a pointer to an allocated size_t variable
137 * Unlike hwconfig() function, this function returns a pointer to the
138 * start of the hwconfig arguments, if option is not found or it has
139 * no specified arguments, the function returns NULL pointer.
141 * If CONFIG_HWCONFIG is undefined, the function returns "", and
142 * arglen is set to 0.
144 const char *hwconfig_arg(const char *opt
, size_t *arglen
)
146 return __hwconfig(opt
, arglen
);
150 * hwconfig_arg_cmp - compare hwconfig option's argument
151 * @opt: a string representing an option
152 * @arg: a string for comparing an option's argument
154 * This call is similar to hwconfig_arg, but instead of returning
155 * hwconfig argument and its length, it is comparing it to @arg.
157 * Returns non-zero value if @arg matches, 0 otherwise.
159 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
160 * value, i.e. the argument matches.
162 int hwconfig_arg_cmp(const char *opt
, const char *arg
)
167 argstr
= hwconfig_arg(opt
, &arglen
);
168 if (!argstr
|| arglen
!= strlen(arg
))
171 return !strncmp(argstr
, arg
, arglen
);
175 * hwconfig_sub - query if a particular hwconfig sub-option is specified
176 * @opt: a string representing an option
177 * @subopt: a string representing a sub-option
179 * This call is similar to hwconfig(), except that it takes additional
180 * argument @subopt. In this example:
181 * "dr_usb:mode=peripheral"
182 * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its
185 int hwconfig_sub(const char *opt
, const char *subopt
)
190 arg
= __hwconfig(opt
, &arglen
);
193 return !!hwconfig_parse(arg
, arglen
, subopt
, ",;", '=', NULL
);
197 * hwconfig_subarg - get hwconfig sub-option's argument
198 * @opt: a string representing an option
199 * @subopt: a string representing a sub-option
200 * @subarglen: a pointer to an allocated size_t variable
202 * This call is similar to hwconfig_arg(), except that it takes an additional
203 * argument @subopt, and so works with sub-options.
205 const char *hwconfig_subarg(const char *opt
, const char *subopt
,
211 arg
= __hwconfig(opt
, &arglen
);
214 return hwconfig_parse(arg
, arglen
, subopt
, ",;", '=', subarglen
);
218 * hwconfig_arg_cmp - compare hwconfig sub-option's argument
219 * @opt: a string representing an option
220 * @subopt: a string representing a sub-option
221 * @subarg: a string for comparing an sub-option's argument
223 * This call is similar to hwconfig_arg_cmp, except that it takes an additional
224 * argument @subopt, and so works with sub-options.
226 int hwconfig_subarg_cmp(const char *opt
, const char *subopt
, const char *subarg
)
231 argstr
= hwconfig_subarg(opt
, subopt
, &arglen
);
232 if (!argstr
|| arglen
!= strlen(subarg
))
235 return !strncmp(argstr
, subarg
, arglen
);
244 setenv("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
245 "key3;:,:=;key4", 1);
247 ret
= hwconfig_arg("key1", &len
);
248 printf("%zd %.*s\n", len
, (int)len
, ret
);
250 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
251 assert(!strncmp(ret
, "subkey1=value1,subkey2=value2", len
));
253 ret
= hwconfig_subarg("key1", "subkey1", &len
);
254 printf("%zd %.*s\n", len
, (int)len
, ret
);
256 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
257 assert(!strncmp(ret
, "value1", len
));
259 ret
= hwconfig_subarg("key1", "subkey2", &len
);
260 printf("%zd %.*s\n", len
, (int)len
, ret
);
262 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
263 assert(!strncmp(ret
, "value2", len
));
265 ret
= hwconfig_arg("key2", &len
);
266 printf("%zd %.*s\n", len
, (int)len
, ret
);
268 assert(hwconfig_arg_cmp("key2", "value3"));
269 assert(!strncmp(ret
, "value3", len
));
271 assert(hwconfig("key3"));
272 assert(hwconfig_arg("key4", &len
) == NULL
);
273 assert(hwconfig_arg("bogus", &len
) == NULL
);
275 unsetenv("hwconfig");
277 assert(hwconfig(NULL
) == 0);
278 assert(hwconfig("") == 0);
279 assert(hwconfig("key3") == 0);
283 #endif /* HWCONFIG_TEST */