]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/hwconfig.c
2 * An inteface for configuring a hardware via u-boot environment.
4 * Copyright (c) 2009 MontaVista Software, Inc.
5 * Copyright 2011 Freescale Semiconductor, Inc.
7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
20 #include <linux/types.h>
21 #include <linux/string.h>
27 #define min(a, b) (((a) < (b)) ? (a) : (b))
28 #endif /* HWCONFIG_TEST */
30 DECLARE_GLOBAL_DATA_PTR
;
32 static const char *hwconfig_parse(const char *opts
, size_t maxlen
,
33 const char *opt
, char *stopchs
, char eqch
,
36 size_t optlen
= strlen(opt
);
38 const char *start
= opts
;
42 str
= strstr(opts
, opt
);
44 if (end
- start
> maxlen
)
47 if (str
&& (str
== opts
|| strpbrk(str
- 1, stopchs
) == str
- 1) &&
48 (strpbrk(end
, stopchs
) == end
|| *end
== eqch
||
58 arg_end
= strpbrk(str
, stopchs
);
60 *arglen
= min(maxlen
, strlen(str
)) - optlen
- 1;
62 *arglen
= arg_end
- end
- 1;
72 const char cpu_hwconfig
[] __attribute__((weak
)) = "";
73 const char board_hwconfig
[] __attribute__((weak
)) = "";
75 static const char *__hwconfig(const char *opt
, size_t *arglen
,
76 const char *env_hwconfig
)
80 /* if we are passed a buffer use it, otherwise try the environment */
82 if (!(gd
->flags
& GD_FLG_ENV_READY
)) {
83 printf("WARNING: Calling __hwconfig without a buffer "
84 "and before environment is ready\n");
87 env_hwconfig
= getenv("hwconfig");
91 ret
= hwconfig_parse(env_hwconfig
, strlen(env_hwconfig
),
92 opt
, ";", ':', arglen
);
97 ret
= hwconfig_parse(board_hwconfig
, strlen(board_hwconfig
),
98 opt
, ";", ':', arglen
);
102 return hwconfig_parse(cpu_hwconfig
, strlen(cpu_hwconfig
),
103 opt
, ";", ':', arglen
);
107 * hwconfig_f - query if a particular hwconfig option is specified
108 * @opt: a string representing an option
109 * @buf: if non-NULL use this buffer to parse, otherwise try env
111 * This call can be used to find out whether U-Boot should configure
112 * a particular hardware option.
114 * Returns non-zero value if the hardware option can be used and thus
115 * should be configured, 0 otherwise.
117 * This function also returns non-zero value if CONFIG_HWCONFIG is
120 * Returning non-zero value without CONFIG_HWCONFIG has its crucial
121 * purpose: the hwconfig() call should be a "transparent" interface,
122 * e.g. if a board doesn't need hwconfig facility, then we assume
123 * that the board file only calls things that are actually used, so
124 * hwconfig() will always return true result.
126 int hwconfig_f(const char *opt
, char *buf
)
128 return !!__hwconfig(opt
, NULL
, buf
);
132 * hwconfig_arg_f - get hwconfig option's argument
133 * @opt: a string representing an option
134 * @arglen: a pointer to an allocated size_t variable
135 * @buf: if non-NULL use this buffer to parse, otherwise try env
137 * Unlike hwconfig_f() 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_f(const char *opt
, size_t *arglen
, char *buf
)
146 return __hwconfig(opt
, arglen
, buf
);
150 * hwconfig_arg_cmp_f - compare hwconfig option's argument
151 * @opt: a string representing an option
152 * @arg: a string for comparing an option's argument
153 * @buf: if non-NULL use this buffer to parse, otherwise try env
155 * This call is similar to hwconfig_arg_f, but instead of returning
156 * hwconfig argument and its length, it is comparing it to @arg.
158 * Returns non-zero value if @arg matches, 0 otherwise.
160 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
161 * value, i.e. the argument matches.
163 int hwconfig_arg_cmp_f(const char *opt
, const char *arg
, char *buf
)
168 argstr
= hwconfig_arg_f(opt
, &arglen
, buf
);
169 if (!argstr
|| arglen
!= strlen(arg
))
172 return !strncmp(argstr
, arg
, arglen
);
176 * hwconfig_sub_f - query if a particular hwconfig sub-option is specified
177 * @opt: a string representing an option
178 * @subopt: a string representing a sub-option
179 * @buf: if non-NULL use this buffer to parse, otherwise try env
181 * This call is similar to hwconfig_f(), 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_f(const char *opt
, const char *subopt
, char *buf
)
192 arg
= __hwconfig(opt
, &arglen
, buf
);
195 return !!hwconfig_parse(arg
, arglen
, subopt
, ",;", '=', NULL
);
199 * hwconfig_subarg_f - 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
203 * @buf: if non-NULL use this buffer to parse, otherwise try env
205 * This call is similar to hwconfig_arg_f(), except that it takes an
206 * additional argument @subopt, and so works with sub-options.
208 const char *hwconfig_subarg_f(const char *opt
, const char *subopt
,
209 size_t *subarglen
, char *buf
)
214 arg
= __hwconfig(opt
, &arglen
, buf
);
217 return hwconfig_parse(arg
, arglen
, subopt
, ",;", '=', subarglen
);
221 * hwconfig_arg_cmp_f - 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 * @buf: if non-NULL use this buffer to parse, otherwise try env
227 * This call is similar to hwconfig_arg_cmp_f, except that it takes an
228 * additional argument @subopt, and so works with sub-options.
230 int hwconfig_subarg_cmp_f(const char *opt
, const char *subopt
,
231 const char *subarg
, char *buf
)
236 argstr
= hwconfig_subarg_f(opt
, subopt
, &arglen
, buf
);
237 if (!argstr
|| arglen
!= strlen(subarg
))
240 return !strncmp(argstr
, subarg
, arglen
);
249 setenv("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
250 "key3;:,:=;key4", 1);
252 ret
= hwconfig_arg("key1", &len
);
253 printf("%zd %.*s\n", len
, (int)len
, ret
);
255 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
256 assert(!strncmp(ret
, "subkey1=value1,subkey2=value2", len
));
258 ret
= hwconfig_subarg("key1", "subkey1", &len
);
259 printf("%zd %.*s\n", len
, (int)len
, ret
);
261 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
262 assert(!strncmp(ret
, "value1", len
));
264 ret
= hwconfig_subarg("key1", "subkey2", &len
);
265 printf("%zd %.*s\n", len
, (int)len
, ret
);
267 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
268 assert(!strncmp(ret
, "value2", len
));
270 ret
= hwconfig_arg("key2", &len
);
271 printf("%zd %.*s\n", len
, (int)len
, ret
);
273 assert(hwconfig_arg_cmp("key2", "value3"));
274 assert(!strncmp(ret
, "value3", len
));
276 assert(hwconfig("key3"));
277 assert(hwconfig_arg("key4", &len
) == NULL
);
278 assert(hwconfig_arg("bogus", &len
) == NULL
);
280 unsetenv("hwconfig");
282 assert(hwconfig(NULL
) == 0);
283 assert(hwconfig("") == 0);
284 assert(hwconfig("key3") == 0);
288 #endif /* HWCONFIG_TEST */