1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
293f9dcf9SAnton Vorontsov /*
393f9dcf9SAnton Vorontsov * An inteface for configuring a hardware via u-boot environment.
493f9dcf9SAnton Vorontsov *
593f9dcf9SAnton Vorontsov * Copyright (c) 2009 MontaVista Software, Inc.
6dd50af25SKumar Gala * Copyright 2011 Freescale Semiconductor, Inc.
793f9dcf9SAnton Vorontsov *
893f9dcf9SAnton Vorontsov * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
993f9dcf9SAnton Vorontsov */
1093f9dcf9SAnton Vorontsov
113bf74a41SAnton Vorontsov #ifndef HWCONFIG_TEST
1293f9dcf9SAnton Vorontsov #include <config.h>
1393f9dcf9SAnton Vorontsov #include <common.h>
1493f9dcf9SAnton Vorontsov #include <exports.h>
1593f9dcf9SAnton Vorontsov #include <hwconfig.h>
1693f9dcf9SAnton Vorontsov #include <linux/types.h>
1793f9dcf9SAnton Vorontsov #include <linux/string.h>
183bf74a41SAnton Vorontsov #else
193bf74a41SAnton Vorontsov #include <stdio.h>
203bf74a41SAnton Vorontsov #include <stdlib.h>
213bf74a41SAnton Vorontsov #include <string.h>
223bf74a41SAnton Vorontsov #include <assert.h>
233bf74a41SAnton Vorontsov #define min(a, b) (((a) < (b)) ? (a) : (b))
243bf74a41SAnton Vorontsov #endif /* HWCONFIG_TEST */
2593f9dcf9SAnton Vorontsov
26c4b115f5SKumar Gala DECLARE_GLOBAL_DATA_PTR;
27c4b115f5SKumar Gala
hwconfig_parse(const char * opts,size_t maxlen,const char * opt,char * stopchs,char eqch,size_t * arglen)2893f9dcf9SAnton Vorontsov static const char *hwconfig_parse(const char *opts, size_t maxlen,
2981f8d3b0SAnton Vorontsov const char *opt, char *stopchs, char eqch,
3093f9dcf9SAnton Vorontsov size_t *arglen)
3193f9dcf9SAnton Vorontsov {
3293f9dcf9SAnton Vorontsov size_t optlen = strlen(opt);
3393f9dcf9SAnton Vorontsov char *str;
3493f9dcf9SAnton Vorontsov const char *start = opts;
3593f9dcf9SAnton Vorontsov const char *end;
3693f9dcf9SAnton Vorontsov
3793f9dcf9SAnton Vorontsov next:
3893f9dcf9SAnton Vorontsov str = strstr(opts, opt);
3993f9dcf9SAnton Vorontsov end = str + optlen;
4093f9dcf9SAnton Vorontsov if (end - start > maxlen)
4193f9dcf9SAnton Vorontsov return NULL;
4293f9dcf9SAnton Vorontsov
4381f8d3b0SAnton Vorontsov if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) &&
4481f8d3b0SAnton Vorontsov (strpbrk(end, stopchs) == end || *end == eqch ||
4581f8d3b0SAnton Vorontsov *end == '\0')) {
4693f9dcf9SAnton Vorontsov const char *arg_end;
4793f9dcf9SAnton Vorontsov
4893f9dcf9SAnton Vorontsov if (!arglen)
4993f9dcf9SAnton Vorontsov return str;
5093f9dcf9SAnton Vorontsov
5193f9dcf9SAnton Vorontsov if (*end != eqch)
5293f9dcf9SAnton Vorontsov return NULL;
5393f9dcf9SAnton Vorontsov
5481f8d3b0SAnton Vorontsov arg_end = strpbrk(str, stopchs);
5593f9dcf9SAnton Vorontsov if (!arg_end)
5693f9dcf9SAnton Vorontsov *arglen = min(maxlen, strlen(str)) - optlen - 1;
5793f9dcf9SAnton Vorontsov else
5893f9dcf9SAnton Vorontsov *arglen = arg_end - end - 1;
5993f9dcf9SAnton Vorontsov
6093f9dcf9SAnton Vorontsov return end + 1;
6193f9dcf9SAnton Vorontsov } else if (str) {
6293f9dcf9SAnton Vorontsov opts = end;
6393f9dcf9SAnton Vorontsov goto next;
6493f9dcf9SAnton Vorontsov }
6593f9dcf9SAnton Vorontsov return NULL;
6693f9dcf9SAnton Vorontsov }
6793f9dcf9SAnton Vorontsov
68b194577bSKumar Gala const char cpu_hwconfig[] __attribute__((weak)) = "";
69b194577bSKumar Gala const char board_hwconfig[] __attribute__((weak)) = "";
7093f9dcf9SAnton Vorontsov
__hwconfig(const char * opt,size_t * arglen,const char * env_hwconfig)71dd50af25SKumar Gala static const char *__hwconfig(const char *opt, size_t *arglen,
72dd50af25SKumar Gala const char *env_hwconfig)
7393f9dcf9SAnton Vorontsov {
74dd50af25SKumar Gala const char *ret;
75c4b115f5SKumar Gala
76dd50af25SKumar Gala /* if we are passed a buffer use it, otherwise try the environment */
77dd50af25SKumar Gala if (!env_hwconfig) {
78dd50af25SKumar Gala if (!(gd->flags & GD_FLG_ENV_READY)) {
79dd50af25SKumar Gala printf("WARNING: Calling __hwconfig without a buffer "
80dd50af25SKumar Gala "and before environment is ready\n");
81dd50af25SKumar Gala return NULL;
82dd50af25SKumar Gala }
8300caae6dSSimon Glass env_hwconfig = env_get("hwconfig");
84c4b115f5SKumar Gala }
8593f9dcf9SAnton Vorontsov
86bb141079SKumar Gala if (env_hwconfig) {
87bb141079SKumar Gala ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
8881f8d3b0SAnton Vorontsov opt, ";", ':', arglen);
89bb141079SKumar Gala if (ret)
90bb141079SKumar Gala return ret;
91bb141079SKumar Gala }
9293f9dcf9SAnton Vorontsov
93bb141079SKumar Gala ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
9481f8d3b0SAnton Vorontsov opt, ";", ':', arglen);
95bb141079SKumar Gala if (ret)
96bb141079SKumar Gala return ret;
9793f9dcf9SAnton Vorontsov
9893f9dcf9SAnton Vorontsov return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
9981f8d3b0SAnton Vorontsov opt, ";", ':', arglen);
10093f9dcf9SAnton Vorontsov }
10193f9dcf9SAnton Vorontsov
10293f9dcf9SAnton Vorontsov /*
103dd50af25SKumar Gala * hwconfig_f - query if a particular hwconfig option is specified
10493f9dcf9SAnton Vorontsov * @opt: a string representing an option
105dd50af25SKumar Gala * @buf: if non-NULL use this buffer to parse, otherwise try env
10693f9dcf9SAnton Vorontsov *
10793f9dcf9SAnton Vorontsov * This call can be used to find out whether U-Boot should configure
10893f9dcf9SAnton Vorontsov * a particular hardware option.
10993f9dcf9SAnton Vorontsov *
11093f9dcf9SAnton Vorontsov * Returns non-zero value if the hardware option can be used and thus
11193f9dcf9SAnton Vorontsov * should be configured, 0 otherwise.
11293f9dcf9SAnton Vorontsov *
11393f9dcf9SAnton Vorontsov * This function also returns non-zero value if CONFIG_HWCONFIG is
11493f9dcf9SAnton Vorontsov * undefined.
11593f9dcf9SAnton Vorontsov *
11693f9dcf9SAnton Vorontsov * Returning non-zero value without CONFIG_HWCONFIG has its crucial
11793f9dcf9SAnton Vorontsov * purpose: the hwconfig() call should be a "transparent" interface,
11893f9dcf9SAnton Vorontsov * e.g. if a board doesn't need hwconfig facility, then we assume
11993f9dcf9SAnton Vorontsov * that the board file only calls things that are actually used, so
12093f9dcf9SAnton Vorontsov * hwconfig() will always return true result.
12193f9dcf9SAnton Vorontsov */
hwconfig_f(const char * opt,char * buf)122dd50af25SKumar Gala int hwconfig_f(const char *opt, char *buf)
12393f9dcf9SAnton Vorontsov {
124dd50af25SKumar Gala return !!__hwconfig(opt, NULL, buf);
12593f9dcf9SAnton Vorontsov }
12693f9dcf9SAnton Vorontsov
12793f9dcf9SAnton Vorontsov /*
128dd50af25SKumar Gala * hwconfig_arg_f - get hwconfig option's argument
12993f9dcf9SAnton Vorontsov * @opt: a string representing an option
13093f9dcf9SAnton Vorontsov * @arglen: a pointer to an allocated size_t variable
131dd50af25SKumar Gala * @buf: if non-NULL use this buffer to parse, otherwise try env
13293f9dcf9SAnton Vorontsov *
133dd50af25SKumar Gala * Unlike hwconfig_f() function, this function returns a pointer to the
13493f9dcf9SAnton Vorontsov * start of the hwconfig arguments, if option is not found or it has
13593f9dcf9SAnton Vorontsov * no specified arguments, the function returns NULL pointer.
13693f9dcf9SAnton Vorontsov *
13793f9dcf9SAnton Vorontsov * If CONFIG_HWCONFIG is undefined, the function returns "", and
13893f9dcf9SAnton Vorontsov * arglen is set to 0.
13993f9dcf9SAnton Vorontsov */
hwconfig_arg_f(const char * opt,size_t * arglen,char * buf)140dd50af25SKumar Gala const char *hwconfig_arg_f(const char *opt, size_t *arglen, char *buf)
14193f9dcf9SAnton Vorontsov {
142dd50af25SKumar Gala return __hwconfig(opt, arglen, buf);
14393f9dcf9SAnton Vorontsov }
14493f9dcf9SAnton Vorontsov
14593f9dcf9SAnton Vorontsov /*
146dd50af25SKumar Gala * hwconfig_arg_cmp_f - compare hwconfig option's argument
14793f9dcf9SAnton Vorontsov * @opt: a string representing an option
14893f9dcf9SAnton Vorontsov * @arg: a string for comparing an option's argument
149dd50af25SKumar Gala * @buf: if non-NULL use this buffer to parse, otherwise try env
15093f9dcf9SAnton Vorontsov *
151dd50af25SKumar Gala * This call is similar to hwconfig_arg_f, but instead of returning
15293f9dcf9SAnton Vorontsov * hwconfig argument and its length, it is comparing it to @arg.
15393f9dcf9SAnton Vorontsov *
15493f9dcf9SAnton Vorontsov * Returns non-zero value if @arg matches, 0 otherwise.
15593f9dcf9SAnton Vorontsov *
15693f9dcf9SAnton Vorontsov * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
15793f9dcf9SAnton Vorontsov * value, i.e. the argument matches.
15893f9dcf9SAnton Vorontsov */
hwconfig_arg_cmp_f(const char * opt,const char * arg,char * buf)159dd50af25SKumar Gala int hwconfig_arg_cmp_f(const char *opt, const char *arg, char *buf)
16093f9dcf9SAnton Vorontsov {
16193f9dcf9SAnton Vorontsov const char *argstr;
16293f9dcf9SAnton Vorontsov size_t arglen;
16393f9dcf9SAnton Vorontsov
164dd50af25SKumar Gala argstr = hwconfig_arg_f(opt, &arglen, buf);
16593f9dcf9SAnton Vorontsov if (!argstr || arglen != strlen(arg))
16693f9dcf9SAnton Vorontsov return 0;
16793f9dcf9SAnton Vorontsov
16893f9dcf9SAnton Vorontsov return !strncmp(argstr, arg, arglen);
16993f9dcf9SAnton Vorontsov }
17093f9dcf9SAnton Vorontsov
17193f9dcf9SAnton Vorontsov /*
172dd50af25SKumar Gala * hwconfig_sub_f - query if a particular hwconfig sub-option is specified
17393f9dcf9SAnton Vorontsov * @opt: a string representing an option
17493f9dcf9SAnton Vorontsov * @subopt: a string representing a sub-option
175dd50af25SKumar Gala * @buf: if non-NULL use this buffer to parse, otherwise try env
17693f9dcf9SAnton Vorontsov *
177dd50af25SKumar Gala * This call is similar to hwconfig_f(), except that it takes additional
17893f9dcf9SAnton Vorontsov * argument @subopt. In this example:
17993f9dcf9SAnton Vorontsov * "dr_usb:mode=peripheral"
18093f9dcf9SAnton Vorontsov * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its
18193f9dcf9SAnton Vorontsov * argument.
18293f9dcf9SAnton Vorontsov */
hwconfig_sub_f(const char * opt,const char * subopt,char * buf)183dd50af25SKumar Gala int hwconfig_sub_f(const char *opt, const char *subopt, char *buf)
18493f9dcf9SAnton Vorontsov {
18593f9dcf9SAnton Vorontsov size_t arglen;
18693f9dcf9SAnton Vorontsov const char *arg;
18793f9dcf9SAnton Vorontsov
188dd50af25SKumar Gala arg = __hwconfig(opt, &arglen, buf);
18993f9dcf9SAnton Vorontsov if (!arg)
19093f9dcf9SAnton Vorontsov return 0;
19181f8d3b0SAnton Vorontsov return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
19293f9dcf9SAnton Vorontsov }
19393f9dcf9SAnton Vorontsov
19493f9dcf9SAnton Vorontsov /*
195dd50af25SKumar Gala * hwconfig_subarg_f - get hwconfig sub-option's argument
19693f9dcf9SAnton Vorontsov * @opt: a string representing an option
19793f9dcf9SAnton Vorontsov * @subopt: a string representing a sub-option
19893f9dcf9SAnton Vorontsov * @subarglen: a pointer to an allocated size_t variable
199dd50af25SKumar Gala * @buf: if non-NULL use this buffer to parse, otherwise try env
20093f9dcf9SAnton Vorontsov *
201dd50af25SKumar Gala * This call is similar to hwconfig_arg_f(), except that it takes an
202dd50af25SKumar Gala * additional argument @subopt, and so works with sub-options.
20393f9dcf9SAnton Vorontsov */
hwconfig_subarg_f(const char * opt,const char * subopt,size_t * subarglen,char * buf)204dd50af25SKumar Gala const char *hwconfig_subarg_f(const char *opt, const char *subopt,
205dd50af25SKumar Gala size_t *subarglen, char *buf)
20693f9dcf9SAnton Vorontsov {
20793f9dcf9SAnton Vorontsov size_t arglen;
20893f9dcf9SAnton Vorontsov const char *arg;
20993f9dcf9SAnton Vorontsov
210dd50af25SKumar Gala arg = __hwconfig(opt, &arglen, buf);
21193f9dcf9SAnton Vorontsov if (!arg)
21293f9dcf9SAnton Vorontsov return NULL;
21381f8d3b0SAnton Vorontsov return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
21493f9dcf9SAnton Vorontsov }
21593f9dcf9SAnton Vorontsov
21693f9dcf9SAnton Vorontsov /*
217dd50af25SKumar Gala * hwconfig_arg_cmp_f - compare hwconfig sub-option's argument
21893f9dcf9SAnton Vorontsov * @opt: a string representing an option
21993f9dcf9SAnton Vorontsov * @subopt: a string representing a sub-option
22093f9dcf9SAnton Vorontsov * @subarg: a string for comparing an sub-option's argument
221dd50af25SKumar Gala * @buf: if non-NULL use this buffer to parse, otherwise try env
22293f9dcf9SAnton Vorontsov *
223dd50af25SKumar Gala * This call is similar to hwconfig_arg_cmp_f, except that it takes an
224dd50af25SKumar Gala * additional argument @subopt, and so works with sub-options.
22593f9dcf9SAnton Vorontsov */
hwconfig_subarg_cmp_f(const char * opt,const char * subopt,const char * subarg,char * buf)226dd50af25SKumar Gala int hwconfig_subarg_cmp_f(const char *opt, const char *subopt,
227dd50af25SKumar Gala const char *subarg, char *buf)
22893f9dcf9SAnton Vorontsov {
22993f9dcf9SAnton Vorontsov const char *argstr;
23093f9dcf9SAnton Vorontsov size_t arglen;
23193f9dcf9SAnton Vorontsov
232dd50af25SKumar Gala argstr = hwconfig_subarg_f(opt, subopt, &arglen, buf);
23393f9dcf9SAnton Vorontsov if (!argstr || arglen != strlen(subarg))
23493f9dcf9SAnton Vorontsov return 0;
23593f9dcf9SAnton Vorontsov
23693f9dcf9SAnton Vorontsov return !strncmp(argstr, subarg, arglen);
23793f9dcf9SAnton Vorontsov }
2383bf74a41SAnton Vorontsov
2393bf74a41SAnton Vorontsov #ifdef HWCONFIG_TEST
main()2403bf74a41SAnton Vorontsov int main()
2413bf74a41SAnton Vorontsov {
2423bf74a41SAnton Vorontsov const char *ret;
2433bf74a41SAnton Vorontsov size_t len;
2443bf74a41SAnton Vorontsov
245382bee57SSimon Glass env_set("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
2463bf74a41SAnton Vorontsov "key3;:,:=;key4", 1);
2473bf74a41SAnton Vorontsov
2483bf74a41SAnton Vorontsov ret = hwconfig_arg("key1", &len);
2493bf74a41SAnton Vorontsov printf("%zd %.*s\n", len, (int)len, ret);
2503bf74a41SAnton Vorontsov assert(len == 29);
2513bf74a41SAnton Vorontsov assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
2523bf74a41SAnton Vorontsov assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len));
2533bf74a41SAnton Vorontsov
2543bf74a41SAnton Vorontsov ret = hwconfig_subarg("key1", "subkey1", &len);
2553bf74a41SAnton Vorontsov printf("%zd %.*s\n", len, (int)len, ret);
2563bf74a41SAnton Vorontsov assert(len == 6);
2573bf74a41SAnton Vorontsov assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
2583bf74a41SAnton Vorontsov assert(!strncmp(ret, "value1", len));
2593bf74a41SAnton Vorontsov
2603bf74a41SAnton Vorontsov ret = hwconfig_subarg("key1", "subkey2", &len);
2613bf74a41SAnton Vorontsov printf("%zd %.*s\n", len, (int)len, ret);
2623bf74a41SAnton Vorontsov assert(len == 6);
2633bf74a41SAnton Vorontsov assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
2643bf74a41SAnton Vorontsov assert(!strncmp(ret, "value2", len));
2653bf74a41SAnton Vorontsov
2663bf74a41SAnton Vorontsov ret = hwconfig_arg("key2", &len);
2673bf74a41SAnton Vorontsov printf("%zd %.*s\n", len, (int)len, ret);
2683bf74a41SAnton Vorontsov assert(len == 6);
2693bf74a41SAnton Vorontsov assert(hwconfig_arg_cmp("key2", "value3"));
2703bf74a41SAnton Vorontsov assert(!strncmp(ret, "value3", len));
2713bf74a41SAnton Vorontsov
2723bf74a41SAnton Vorontsov assert(hwconfig("key3"));
2733bf74a41SAnton Vorontsov assert(hwconfig_arg("key4", &len) == NULL);
2743bf74a41SAnton Vorontsov assert(hwconfig_arg("bogus", &len) == NULL);
2753bf74a41SAnton Vorontsov
276382bee57SSimon Glass unenv_set("hwconfig");
2773bf74a41SAnton Vorontsov
2783bf74a41SAnton Vorontsov assert(hwconfig(NULL) == 0);
2793bf74a41SAnton Vorontsov assert(hwconfig("") == 0);
2803bf74a41SAnton Vorontsov assert(hwconfig("key3") == 0);
2813bf74a41SAnton Vorontsov
2823bf74a41SAnton Vorontsov return 0;
2833bf74a41SAnton Vorontsov }
2843bf74a41SAnton Vorontsov #endif /* HWCONFIG_TEST */
285