]>
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.
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 static const char *hwconfig_parse(const char *opts
, size_t maxlen
,
30 const char *opt
, char *stopchs
, char eqch
,
33 size_t optlen
= strlen(opt
);
35 const char *start
= opts
;
39 str
= strstr(opts
, opt
);
41 if (end
- start
> maxlen
)
44 if (str
&& (str
== opts
|| strpbrk(str
- 1, stopchs
) == str
- 1) &&
45 (strpbrk(end
, stopchs
) == end
|| *end
== eqch
||
55 arg_end
= strpbrk(str
, stopchs
);
57 *arglen
= min(maxlen
, strlen(str
)) - optlen
- 1;
59 *arglen
= arg_end
- end
- 1;
69 const char *cpu_hwconfig
__attribute__((weak
));
70 const char *board_hwconfig
__attribute__((weak
));
72 static const char *__hwconfig(const char *opt
, size_t *arglen
)
74 const char *env_hwconfig
= getenv("hwconfig");
77 return hwconfig_parse(env_hwconfig
, strlen(env_hwconfig
),
78 opt
, ";", ':', arglen
);
81 return hwconfig_parse(board_hwconfig
, strlen(board_hwconfig
),
82 opt
, ";", ':', arglen
);
85 return hwconfig_parse(cpu_hwconfig
, strlen(cpu_hwconfig
),
86 opt
, ";", ':', arglen
);
92 * hwconfig - query if a particular hwconfig option is specified
93 * @opt: a string representing an option
95 * This call can be used to find out whether U-Boot should configure
96 * a particular hardware option.
98 * Returns non-zero value if the hardware option can be used and thus
99 * should be configured, 0 otherwise.
101 * This function also returns non-zero value if CONFIG_HWCONFIG is
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.
110 int hwconfig(const char *opt
)
112 return !!__hwconfig(opt
, NULL
);
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
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.
124 * If CONFIG_HWCONFIG is undefined, the function returns "", and
125 * arglen is set to 0.
127 const char *hwconfig_arg(const char *opt
, size_t *arglen
)
129 return __hwconfig(opt
, arglen
);
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
137 * This call is similar to hwconfig_arg, but instead of returning
138 * hwconfig argument and its length, it is comparing it to @arg.
140 * Returns non-zero value if @arg matches, 0 otherwise.
142 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
143 * value, i.e. the argument matches.
145 int hwconfig_arg_cmp(const char *opt
, const char *arg
)
150 argstr
= hwconfig_arg(opt
, &arglen
);
151 if (!argstr
|| arglen
!= strlen(arg
))
154 return !strncmp(argstr
, arg
, arglen
);
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
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
168 int hwconfig_sub(const char *opt
, const char *subopt
)
173 arg
= __hwconfig(opt
, &arglen
);
176 return !!hwconfig_parse(arg
, arglen
, subopt
, ",;", '=', NULL
);
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
185 * This call is similar to hwconfig_arg(), except that it takes an additional
186 * argument @subopt, and so works with sub-options.
188 const char *hwconfig_subarg(const char *opt
, const char *subopt
,
194 arg
= __hwconfig(opt
, &arglen
);
197 return hwconfig_parse(arg
, arglen
, subopt
, ",;", '=', subarglen
);
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
206 * This call is similar to hwconfig_arg_cmp, except that it takes an additional
207 * argument @subopt, and so works with sub-options.
209 int hwconfig_subarg_cmp(const char *opt
, const char *subopt
, const char *subarg
)
214 argstr
= hwconfig_subarg(opt
, subopt
, &arglen
);
215 if (!argstr
|| arglen
!= strlen(subarg
))
218 return !strncmp(argstr
, subarg
, arglen
);
227 setenv("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
228 "key3;:,:=;key4", 1);
230 ret
= hwconfig_arg("key1", &len
);
231 printf("%zd %.*s\n", len
, (int)len
, ret
);
233 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
234 assert(!strncmp(ret
, "subkey1=value1,subkey2=value2", len
));
236 ret
= hwconfig_subarg("key1", "subkey1", &len
);
237 printf("%zd %.*s\n", len
, (int)len
, ret
);
239 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
240 assert(!strncmp(ret
, "value1", len
));
242 ret
= hwconfig_subarg("key1", "subkey2", &len
);
243 printf("%zd %.*s\n", len
, (int)len
, ret
);
245 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
246 assert(!strncmp(ret
, "value2", len
));
248 ret
= hwconfig_arg("key2", &len
);
249 printf("%zd %.*s\n", len
, (int)len
, ret
);
251 assert(hwconfig_arg_cmp("key2", "value3"));
252 assert(!strncmp(ret
, "value3", len
));
254 assert(hwconfig("key3"));
255 assert(hwconfig_arg("key4", &len
) == NULL
);
256 assert(hwconfig_arg("bogus", &len
) == NULL
);
258 unsetenv("hwconfig");
260 assert(hwconfig(NULL
) == 0);
261 assert(hwconfig("") == 0);
262 assert(hwconfig("key3") == 0);
266 #endif /* HWCONFIG_TEST */