]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - common/hwconfig.c
1 // SPDX-License-Identifier: GPL-2.0+
3 * An inteface for configuring a hardware via u-boot environment.
5 * Copyright (c) 2009 MontaVista Software, Inc.
6 * Copyright 2011 Freescale Semiconductor, Inc.
8 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
18 #include <linux/types.h>
19 #include <linux/string.h>
25 #define min(a, b) (((a) < (b)) ? (a) : (b))
26 #endif /* HWCONFIG_TEST */
28 DECLARE_GLOBAL_DATA_PTR
;
30 static const char *hwconfig_parse(const char *opts
, size_t maxlen
,
31 const char *opt
, char *stopchs
, char eqch
,
34 size_t optlen
= strlen(opt
);
36 const char *start
= opts
;
40 str
= strstr(opts
, opt
);
42 if (end
- start
> maxlen
)
45 if (str
&& (str
== opts
|| strpbrk(str
- 1, stopchs
) == str
- 1) &&
46 (strpbrk(end
, stopchs
) == end
|| *end
== eqch
||
56 arg_end
= strpbrk(str
, stopchs
);
58 *arglen
= min(maxlen
, strlen(str
)) - optlen
- 1;
60 *arglen
= arg_end
- end
- 1;
70 const char cpu_hwconfig
[] __attribute__((weak
)) = "";
71 const char board_hwconfig
[] __attribute__((weak
)) = "";
73 static const char *__hwconfig(const char *opt
, size_t *arglen
,
74 const char *env_hwconfig
)
78 /* if we are passed a buffer use it, otherwise try the environment */
80 if (!(gd
->flags
& GD_FLG_ENV_READY
)) {
81 printf("WARNING: Calling __hwconfig without a buffer "
82 "and before environment is ready\n");
85 env_hwconfig
= env_get("hwconfig");
89 ret
= hwconfig_parse(env_hwconfig
, strlen(env_hwconfig
),
90 opt
, ";", ':', arglen
);
95 ret
= hwconfig_parse(board_hwconfig
, strlen(board_hwconfig
),
96 opt
, ";", ':', arglen
);
100 return hwconfig_parse(cpu_hwconfig
, strlen(cpu_hwconfig
),
101 opt
, ";", ':', arglen
);
105 * hwconfig_f - query if a particular hwconfig option is specified
106 * @opt: a string representing an option
107 * @buf: if non-NULL use this buffer to parse, otherwise try env
109 * This call can be used to find out whether U-Boot should configure
110 * a particular hardware option.
112 * Returns non-zero value if the hardware option can be used and thus
113 * should be configured, 0 otherwise.
115 * This function also returns non-zero value if CONFIG_HWCONFIG is
118 * Returning non-zero value without CONFIG_HWCONFIG has its crucial
119 * purpose: the hwconfig() call should be a "transparent" interface,
120 * e.g. if a board doesn't need hwconfig facility, then we assume
121 * that the board file only calls things that are actually used, so
122 * hwconfig() will always return true result.
124 int hwconfig_f(const char *opt
, char *buf
)
126 return !!__hwconfig(opt
, NULL
, buf
);
130 * hwconfig_arg_f - get hwconfig option's argument
131 * @opt: a string representing an option
132 * @arglen: a pointer to an allocated size_t variable
133 * @buf: if non-NULL use this buffer to parse, otherwise try env
135 * Unlike hwconfig_f() function, this function returns a pointer to the
136 * start of the hwconfig arguments, if option is not found or it has
137 * no specified arguments, the function returns NULL pointer.
139 * If CONFIG_HWCONFIG is undefined, the function returns "", and
140 * arglen is set to 0.
142 const char *hwconfig_arg_f(const char *opt
, size_t *arglen
, char *buf
)
144 return __hwconfig(opt
, arglen
, buf
);
148 * hwconfig_arg_cmp_f - compare hwconfig option's argument
149 * @opt: a string representing an option
150 * @arg: a string for comparing an option's argument
151 * @buf: if non-NULL use this buffer to parse, otherwise try env
153 * This call is similar to hwconfig_arg_f, but instead of returning
154 * hwconfig argument and its length, it is comparing it to @arg.
156 * Returns non-zero value if @arg matches, 0 otherwise.
158 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
159 * value, i.e. the argument matches.
161 int hwconfig_arg_cmp_f(const char *opt
, const char *arg
, char *buf
)
166 argstr
= hwconfig_arg_f(opt
, &arglen
, buf
);
167 if (!argstr
|| arglen
!= strlen(arg
))
170 return !strncmp(argstr
, arg
, arglen
);
174 * hwconfig_sub_f - query if a particular hwconfig sub-option is specified
175 * @opt: a string representing an option
176 * @subopt: a string representing a sub-option
177 * @buf: if non-NULL use this buffer to parse, otherwise try env
179 * This call is similar to hwconfig_f(), 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_f(const char *opt
, const char *subopt
, char *buf
)
190 arg
= __hwconfig(opt
, &arglen
, buf
);
193 return !!hwconfig_parse(arg
, arglen
, subopt
, ",;", '=', NULL
);
197 * hwconfig_subarg_f - 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
201 * @buf: if non-NULL use this buffer to parse, otherwise try env
203 * This call is similar to hwconfig_arg_f(), except that it takes an
204 * additional argument @subopt, and so works with sub-options.
206 const char *hwconfig_subarg_f(const char *opt
, const char *subopt
,
207 size_t *subarglen
, char *buf
)
212 arg
= __hwconfig(opt
, &arglen
, buf
);
215 return hwconfig_parse(arg
, arglen
, subopt
, ",;", '=', subarglen
);
219 * hwconfig_arg_cmp_f - compare hwconfig sub-option's argument
220 * @opt: a string representing an option
221 * @subopt: a string representing a sub-option
222 * @subarg: a string for comparing an sub-option's argument
223 * @buf: if non-NULL use this buffer to parse, otherwise try env
225 * This call is similar to hwconfig_arg_cmp_f, except that it takes an
226 * additional argument @subopt, and so works with sub-options.
228 int hwconfig_subarg_cmp_f(const char *opt
, const char *subopt
,
229 const char *subarg
, char *buf
)
234 argstr
= hwconfig_subarg_f(opt
, subopt
, &arglen
, buf
);
235 if (!argstr
|| arglen
!= strlen(subarg
))
238 return !strncmp(argstr
, subarg
, arglen
);
247 env_set("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
248 "key3;:,:=;key4", 1);
250 ret
= hwconfig_arg("key1", &len
);
251 printf("%zd %.*s\n", len
, (int)len
, ret
);
253 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
254 assert(!strncmp(ret
, "subkey1=value1,subkey2=value2", len
));
256 ret
= hwconfig_subarg("key1", "subkey1", &len
);
257 printf("%zd %.*s\n", len
, (int)len
, ret
);
259 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
260 assert(!strncmp(ret
, "value1", len
));
262 ret
= hwconfig_subarg("key1", "subkey2", &len
);
263 printf("%zd %.*s\n", len
, (int)len
, ret
);
265 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
266 assert(!strncmp(ret
, "value2", len
));
268 ret
= hwconfig_arg("key2", &len
);
269 printf("%zd %.*s\n", len
, (int)len
, ret
);
271 assert(hwconfig_arg_cmp("key2", "value3"));
272 assert(!strncmp(ret
, "value3", len
));
274 assert(hwconfig("key3"));
275 assert(hwconfig_arg("key4", &len
) == NULL
);
276 assert(hwconfig_arg("bogus", &len
) == NULL
);
278 unenv_set("hwconfig");
280 assert(hwconfig(NULL
) == 0);
281 assert(hwconfig("") == 0);
282 assert(hwconfig("key3") == 0);
286 #endif /* HWCONFIG_TEST */