193f9dcf9SAnton Vorontsov /*
293f9dcf9SAnton Vorontsov * An inteface for configuring a hardware via u-boot environment.
393f9dcf9SAnton Vorontsov *
493f9dcf9SAnton Vorontsov * Copyright (c) 2009 MontaVista Software, Inc.
5dd50af25SKumar Gala * Copyright 2011 Freescale Semiconductor, Inc.
693f9dcf9SAnton Vorontsov *
793f9dcf9SAnton Vorontsov * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
893f9dcf9SAnton Vorontsov *
91a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+
1093f9dcf9SAnton Vorontsov */
1193f9dcf9SAnton Vorontsov
123bf74a41SAnton Vorontsov #ifndef HWCONFIG_TEST
1393f9dcf9SAnton Vorontsov #include <config.h>
1493f9dcf9SAnton Vorontsov #include <common.h>
1593f9dcf9SAnton Vorontsov #include <exports.h>
1693f9dcf9SAnton Vorontsov #include <hwconfig.h>
1793f9dcf9SAnton Vorontsov #include <linux/types.h>
1893f9dcf9SAnton Vorontsov #include <linux/string.h>
193bf74a41SAnton Vorontsov #else
203bf74a41SAnton Vorontsov #include <stdio.h>
213bf74a41SAnton Vorontsov #include <stdlib.h>
223bf74a41SAnton Vorontsov #include <string.h>
233bf74a41SAnton Vorontsov #include <assert.h>
243bf74a41SAnton Vorontsov #define min(a, b) (((a) < (b)) ? (a) : (b))
253bf74a41SAnton Vorontsov #endif /* HWCONFIG_TEST */
2693f9dcf9SAnton Vorontsov
27c4b115f5SKumar Gala DECLARE_GLOBAL_DATA_PTR;
28c4b115f5SKumar Gala
hwconfig_parse(const char * opts,size_t maxlen,const char * opt,char * stopchs,char eqch,size_t * arglen)2993f9dcf9SAnton Vorontsov static const char *hwconfig_parse(const char *opts, size_t maxlen,
3081f8d3b0SAnton Vorontsov const char *opt, char *stopchs, char eqch,
3193f9dcf9SAnton Vorontsov size_t *arglen)
3293f9dcf9SAnton Vorontsov {
3393f9dcf9SAnton Vorontsov size_t optlen = strlen(opt);
3493f9dcf9SAnton Vorontsov char *str;
3593f9dcf9SAnton Vorontsov const char *start = opts;
3693f9dcf9SAnton Vorontsov const char *end;
3793f9dcf9SAnton Vorontsov
3893f9dcf9SAnton Vorontsov next:
3993f9dcf9SAnton Vorontsov str = strstr(opts, opt);
4093f9dcf9SAnton Vorontsov end = str + optlen;
4193f9dcf9SAnton Vorontsov if (end - start > maxlen)
4293f9dcf9SAnton Vorontsov return NULL;
4393f9dcf9SAnton Vorontsov
4481f8d3b0SAnton Vorontsov if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) &&
4581f8d3b0SAnton Vorontsov (strpbrk(end, stopchs) == end || *end == eqch ||
4681f8d3b0SAnton Vorontsov *end == '\0')) {
4793f9dcf9SAnton Vorontsov const char *arg_end;
4893f9dcf9SAnton Vorontsov
4993f9dcf9SAnton Vorontsov if (!arglen)
5093f9dcf9SAnton Vorontsov return str;
5193f9dcf9SAnton Vorontsov
5293f9dcf9SAnton Vorontsov if (*end != eqch)
5393f9dcf9SAnton Vorontsov return NULL;
5493f9dcf9SAnton Vorontsov
5581f8d3b0SAnton Vorontsov arg_end = strpbrk(str, stopchs);
5693f9dcf9SAnton Vorontsov if (!arg_end)
5793f9dcf9SAnton Vorontsov *arglen = min(maxlen, strlen(str)) - optlen - 1;
5893f9dcf9SAnton Vorontsov else
5993f9dcf9SAnton Vorontsov *arglen = arg_end - end - 1;
6093f9dcf9SAnton Vorontsov
6193f9dcf9SAnton Vorontsov return end + 1;
6293f9dcf9SAnton Vorontsov } else if (str) {
6393f9dcf9SAnton Vorontsov opts = end;
6493f9dcf9SAnton Vorontsov goto next;
6593f9dcf9SAnton Vorontsov }
6693f9dcf9SAnton Vorontsov return NULL;
6793f9dcf9SAnton Vorontsov }
6893f9dcf9SAnton Vorontsov
69b194577bSKumar Gala const char cpu_hwconfig[] __attribute__((weak)) = "";
70b194577bSKumar Gala const char board_hwconfig[] __attribute__((weak)) = "";
7193f9dcf9SAnton Vorontsov
__hwconfig(const char * opt,size_t * arglen,const char * env_hwconfig)72dd50af25SKumar Gala static const char *__hwconfig(const char *opt, size_t *arglen,
73dd50af25SKumar Gala const char *env_hwconfig)
7493f9dcf9SAnton Vorontsov {
75dd50af25SKumar Gala const char *ret;
76c4b115f5SKumar Gala
77dd50af25SKumar Gala /* if we are passed a buffer use it, otherwise try the environment */
78dd50af25SKumar Gala if (!env_hwconfig) {
79dd50af25SKumar Gala if (!(gd->flags & GD_FLG_ENV_READY)) {
80dd50af25SKumar Gala printf("WARNING: Calling __hwconfig without a buffer "
81dd50af25SKumar Gala "and before environment is ready\n");
82dd50af25SKumar Gala return NULL;
83dd50af25SKumar Gala }
84*00caae6dSSimon Glass env_hwconfig = env_get("hwconfig");
85c4b115f5SKumar Gala }
8693f9dcf9SAnton Vorontsov
87bb141079SKumar Gala if (env_hwconfig) {
88bb141079SKumar Gala ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
8981f8d3b0SAnton Vorontsov opt, ";", ':', arglen);
90bb141079SKumar Gala if (ret)
91bb141079SKumar Gala return ret;
92bb141079SKumar Gala }
9393f9dcf9SAnton Vorontsov
94bb141079SKumar Gala ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
9581f8d3b0SAnton Vorontsov opt, ";", ':', arglen);
96bb141079SKumar Gala if (ret)
97bb141079SKumar Gala return ret;
9893f9dcf9SAnton Vorontsov
9993f9dcf9SAnton Vorontsov return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
10081f8d3b0SAnton Vorontsov opt, ";", ':', arglen);
10193f9dcf9SAnton Vorontsov }
10293f9dcf9SAnton Vorontsov
10393f9dcf9SAnton Vorontsov /*
104dd50af25SKumar Gala * hwconfig_f - query if a particular hwconfig option is specified
10593f9dcf9SAnton Vorontsov * @opt: a string representing an option
106dd50af25SKumar Gala * @buf: if non-NULL use this buffer to parse, otherwise try env
10793f9dcf9SAnton Vorontsov *
10893f9dcf9SAnton Vorontsov * This call can be used to find out whether U-Boot should configure
10993f9dcf9SAnton Vorontsov * a particular hardware option.
11093f9dcf9SAnton Vorontsov *
11193f9dcf9SAnton Vorontsov * Returns non-zero value if the hardware option can be used and thus
11293f9dcf9SAnton Vorontsov * should be configured, 0 otherwise.
11393f9dcf9SAnton Vorontsov *
11493f9dcf9SAnton Vorontsov * This function also returns non-zero value if CONFIG_HWCONFIG is
11593f9dcf9SAnton Vorontsov * undefined.
11693f9dcf9SAnton Vorontsov *
11793f9dcf9SAnton Vorontsov * Returning non-zero value without CONFIG_HWCONFIG has its crucial
11893f9dcf9SAnton Vorontsov * purpose: the hwconfig() call should be a "transparent" interface,
11993f9dcf9SAnton Vorontsov * e.g. if a board doesn't need hwconfig facility, then we assume
12093f9dcf9SAnton Vorontsov * that the board file only calls things that are actually used, so
12193f9dcf9SAnton Vorontsov * hwconfig() will always return true result.
12293f9dcf9SAnton Vorontsov */
hwconfig_f(const char * opt,char * buf)123dd50af25SKumar Gala int hwconfig_f(const char *opt, char *buf)
12493f9dcf9SAnton Vorontsov {
125dd50af25SKumar Gala return !!__hwconfig(opt, NULL, buf);
12693f9dcf9SAnton Vorontsov }
12793f9dcf9SAnton Vorontsov
12893f9dcf9SAnton Vorontsov /*
129dd50af25SKumar Gala * hwconfig_arg_f - get hwconfig option's argument
13093f9dcf9SAnton Vorontsov * @opt: a string representing an option
13193f9dcf9SAnton Vorontsov * @arglen: a pointer to an allocated size_t variable
132dd50af25SKumar Gala * @buf: if non-NULL use this buffer to parse, otherwise try env
13393f9dcf9SAnton Vorontsov *
134dd50af25SKumar Gala * Unlike hwconfig_f() function, this function returns a pointer to the
13593f9dcf9SAnton Vorontsov * start of the hwconfig arguments, if option is not found or it has
13693f9dcf9SAnton Vorontsov * no specified arguments, the function returns NULL pointer.
13793f9dcf9SAnton Vorontsov *
13893f9dcf9SAnton Vorontsov * If CONFIG_HWCONFIG is undefined, the function returns "", and
13993f9dcf9SAnton Vorontsov * arglen is set to 0.
14093f9dcf9SAnton Vorontsov */
hwconfig_arg_f(const char * opt,size_t * arglen,char * buf)141dd50af25SKumar Gala const char *hwconfig_arg_f(const char *opt, size_t *arglen, char *buf)
14293f9dcf9SAnton Vorontsov {
143dd50af25SKumar Gala return __hwconfig(opt, arglen, buf);
14493f9dcf9SAnton Vorontsov }
14593f9dcf9SAnton Vorontsov
14693f9dcf9SAnton Vorontsov /*
147dd50af25SKumar Gala * hwconfig_arg_cmp_f - compare hwconfig option's argument
14893f9dcf9SAnton Vorontsov * @opt: a string representing an option
14993f9dcf9SAnton Vorontsov * @arg: a string for comparing an option's argument
150dd50af25SKumar Gala * @buf: if non-NULL use this buffer to parse, otherwise try env
15193f9dcf9SAnton Vorontsov *
152dd50af25SKumar Gala * This call is similar to hwconfig_arg_f, but instead of returning
15393f9dcf9SAnton Vorontsov * hwconfig argument and its length, it is comparing it to @arg.
15493f9dcf9SAnton Vorontsov *
15593f9dcf9SAnton Vorontsov * Returns non-zero value if @arg matches, 0 otherwise.
15693f9dcf9SAnton Vorontsov *
15793f9dcf9SAnton Vorontsov * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
15893f9dcf9SAnton Vorontsov * value, i.e. the argument matches.
15993f9dcf9SAnton Vorontsov */
hwconfig_arg_cmp_f(const char * opt,const char * arg,char * buf)160dd50af25SKumar Gala int hwconfig_arg_cmp_f(const char *opt, const char *arg, char *buf)
16193f9dcf9SAnton Vorontsov {
16293f9dcf9SAnton Vorontsov const char *argstr;
16393f9dcf9SAnton Vorontsov size_t arglen;
16493f9dcf9SAnton Vorontsov
165dd50af25SKumar Gala argstr = hwconfig_arg_f(opt, &arglen, buf);
16693f9dcf9SAnton Vorontsov if (!argstr || arglen != strlen(arg))
16793f9dcf9SAnton Vorontsov return 0;
16893f9dcf9SAnton Vorontsov
16993f9dcf9SAnton Vorontsov return !strncmp(argstr, arg, arglen);
17093f9dcf9SAnton Vorontsov }
17193f9dcf9SAnton Vorontsov
17293f9dcf9SAnton Vorontsov /*
173dd50af25SKumar Gala * hwconfig_sub_f - query if a particular hwconfig sub-option is specified
17493f9dcf9SAnton Vorontsov * @opt: a string representing an option
17593f9dcf9SAnton Vorontsov * @subopt: a string representing a sub-option
176dd50af25SKumar Gala * @buf: if non-NULL use this buffer to parse, otherwise try env
17793f9dcf9SAnton Vorontsov *
178dd50af25SKumar Gala * This call is similar to hwconfig_f(), except that it takes additional
17993f9dcf9SAnton Vorontsov * argument @subopt. In this example:
18093f9dcf9SAnton Vorontsov * "dr_usb:mode=peripheral"
18193f9dcf9SAnton Vorontsov * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its
18293f9dcf9SAnton Vorontsov * argument.
18393f9dcf9SAnton Vorontsov */
hwconfig_sub_f(const char * opt,const char * subopt,char * buf)184dd50af25SKumar Gala int hwconfig_sub_f(const char *opt, const char *subopt, char *buf)
18593f9dcf9SAnton Vorontsov {
18693f9dcf9SAnton Vorontsov size_t arglen;
18793f9dcf9SAnton Vorontsov const char *arg;
18893f9dcf9SAnton Vorontsov
189dd50af25SKumar Gala arg = __hwconfig(opt, &arglen, buf);
19093f9dcf9SAnton Vorontsov if (!arg)
19193f9dcf9SAnton Vorontsov return 0;
19281f8d3b0SAnton Vorontsov return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
19393f9dcf9SAnton Vorontsov }
19493f9dcf9SAnton Vorontsov
19593f9dcf9SAnton Vorontsov /*
196dd50af25SKumar Gala * hwconfig_subarg_f - get hwconfig sub-option's argument
19793f9dcf9SAnton Vorontsov * @opt: a string representing an option
19893f9dcf9SAnton Vorontsov * @subopt: a string representing a sub-option
19993f9dcf9SAnton Vorontsov * @subarglen: a pointer to an allocated size_t variable
200dd50af25SKumar Gala * @buf: if non-NULL use this buffer to parse, otherwise try env
20193f9dcf9SAnton Vorontsov *
202dd50af25SKumar Gala * This call is similar to hwconfig_arg_f(), except that it takes an
203dd50af25SKumar Gala * additional argument @subopt, and so works with sub-options.
20493f9dcf9SAnton Vorontsov */
hwconfig_subarg_f(const char * opt,const char * subopt,size_t * subarglen,char * buf)205dd50af25SKumar Gala const char *hwconfig_subarg_f(const char *opt, const char *subopt,
206dd50af25SKumar Gala size_t *subarglen, char *buf)
20793f9dcf9SAnton Vorontsov {
20893f9dcf9SAnton Vorontsov size_t arglen;
20993f9dcf9SAnton Vorontsov const char *arg;
21093f9dcf9SAnton Vorontsov
211dd50af25SKumar Gala arg = __hwconfig(opt, &arglen, buf);
21293f9dcf9SAnton Vorontsov if (!arg)
21393f9dcf9SAnton Vorontsov return NULL;
21481f8d3b0SAnton Vorontsov return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
21593f9dcf9SAnton Vorontsov }
21693f9dcf9SAnton Vorontsov
21793f9dcf9SAnton Vorontsov /*
218dd50af25SKumar Gala * hwconfig_arg_cmp_f - compare hwconfig sub-option's argument
21993f9dcf9SAnton Vorontsov * @opt: a string representing an option
22093f9dcf9SAnton Vorontsov * @subopt: a string representing a sub-option
22193f9dcf9SAnton Vorontsov * @subarg: a string for comparing an sub-option's argument
222dd50af25SKumar Gala * @buf: if non-NULL use this buffer to parse, otherwise try env
22393f9dcf9SAnton Vorontsov *
224dd50af25SKumar Gala * This call is similar to hwconfig_arg_cmp_f, except that it takes an
225dd50af25SKumar Gala * additional argument @subopt, and so works with sub-options.
22693f9dcf9SAnton Vorontsov */
hwconfig_subarg_cmp_f(const char * opt,const char * subopt,const char * subarg,char * buf)227dd50af25SKumar Gala int hwconfig_subarg_cmp_f(const char *opt, const char *subopt,
228dd50af25SKumar Gala const char *subarg, char *buf)
22993f9dcf9SAnton Vorontsov {
23093f9dcf9SAnton Vorontsov const char *argstr;
23193f9dcf9SAnton Vorontsov size_t arglen;
23293f9dcf9SAnton Vorontsov
233dd50af25SKumar Gala argstr = hwconfig_subarg_f(opt, subopt, &arglen, buf);
23493f9dcf9SAnton Vorontsov if (!argstr || arglen != strlen(subarg))
23593f9dcf9SAnton Vorontsov return 0;
23693f9dcf9SAnton Vorontsov
23793f9dcf9SAnton Vorontsov return !strncmp(argstr, subarg, arglen);
23893f9dcf9SAnton Vorontsov }
2393bf74a41SAnton Vorontsov
2403bf74a41SAnton Vorontsov #ifdef HWCONFIG_TEST
main()2413bf74a41SAnton Vorontsov int main()
2423bf74a41SAnton Vorontsov {
2433bf74a41SAnton Vorontsov const char *ret;
2443bf74a41SAnton Vorontsov size_t len;
2453bf74a41SAnton Vorontsov
246382bee57SSimon Glass env_set("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
2473bf74a41SAnton Vorontsov "key3;:,:=;key4", 1);
2483bf74a41SAnton Vorontsov
2493bf74a41SAnton Vorontsov ret = hwconfig_arg("key1", &len);
2503bf74a41SAnton Vorontsov printf("%zd %.*s\n", len, (int)len, ret);
2513bf74a41SAnton Vorontsov assert(len == 29);
2523bf74a41SAnton Vorontsov assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
2533bf74a41SAnton Vorontsov assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len));
2543bf74a41SAnton Vorontsov
2553bf74a41SAnton Vorontsov ret = hwconfig_subarg("key1", "subkey1", &len);
2563bf74a41SAnton Vorontsov printf("%zd %.*s\n", len, (int)len, ret);
2573bf74a41SAnton Vorontsov assert(len == 6);
2583bf74a41SAnton Vorontsov assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
2593bf74a41SAnton Vorontsov assert(!strncmp(ret, "value1", len));
2603bf74a41SAnton Vorontsov
2613bf74a41SAnton Vorontsov ret = hwconfig_subarg("key1", "subkey2", &len);
2623bf74a41SAnton Vorontsov printf("%zd %.*s\n", len, (int)len, ret);
2633bf74a41SAnton Vorontsov assert(len == 6);
2643bf74a41SAnton Vorontsov assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
2653bf74a41SAnton Vorontsov assert(!strncmp(ret, "value2", len));
2663bf74a41SAnton Vorontsov
2673bf74a41SAnton Vorontsov ret = hwconfig_arg("key2", &len);
2683bf74a41SAnton Vorontsov printf("%zd %.*s\n", len, (int)len, ret);
2693bf74a41SAnton Vorontsov assert(len == 6);
2703bf74a41SAnton Vorontsov assert(hwconfig_arg_cmp("key2", "value3"));
2713bf74a41SAnton Vorontsov assert(!strncmp(ret, "value3", len));
2723bf74a41SAnton Vorontsov
2733bf74a41SAnton Vorontsov assert(hwconfig("key3"));
2743bf74a41SAnton Vorontsov assert(hwconfig_arg("key4", &len) == NULL);
2753bf74a41SAnton Vorontsov assert(hwconfig_arg("bogus", &len) == NULL);
2763bf74a41SAnton Vorontsov
277382bee57SSimon Glass unenv_set("hwconfig");
2783bf74a41SAnton Vorontsov
2793bf74a41SAnton Vorontsov assert(hwconfig(NULL) == 0);
2803bf74a41SAnton Vorontsov assert(hwconfig("") == 0);
2813bf74a41SAnton Vorontsov assert(hwconfig("key3") == 0);
2823bf74a41SAnton Vorontsov
2833bf74a41SAnton Vorontsov return 0;
2843bf74a41SAnton Vorontsov }
2853bf74a41SAnton Vorontsov #endif /* HWCONFIG_TEST */
286