Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 2 | /* |
| 3 | * An inteface for configuring a hardware via u-boot environment. |
| 4 | * |
| 5 | * Copyright (c) 2009 MontaVista Software, Inc. |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 6 | * Copyright 2011 Freescale Semiconductor, Inc. |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 7 | * |
| 8 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 9 | */ |
| 10 | |
Anton Vorontsov | 3bf74a4 | 2010-06-18 15:08:27 +0400 | [diff] [blame] | 11 | #ifndef HWCONFIG_TEST |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 12 | #include <config.h> |
| 13 | #include <common.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 14 | #include <env.h> |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 15 | #include <exports.h> |
| 16 | #include <hwconfig.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 17 | #include <log.h> |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 18 | #include <linux/types.h> |
| 19 | #include <linux/string.h> |
Anton Vorontsov | 3bf74a4 | 2010-06-18 15:08:27 +0400 | [diff] [blame] | 20 | #else |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <assert.h> |
| 25 | #define min(a, b) (((a) < (b)) ? (a) : (b)) |
| 26 | #endif /* HWCONFIG_TEST */ |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 27 | |
Kumar Gala | c4b115f | 2010-10-22 03:18:13 -0500 | [diff] [blame] | 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 30 | static const char *hwconfig_parse(const char *opts, size_t maxlen, |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 31 | const char *opt, char *stopchs, char eqch, |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 32 | size_t *arglen) |
| 33 | { |
| 34 | size_t optlen = strlen(opt); |
| 35 | char *str; |
| 36 | const char *start = opts; |
| 37 | const char *end; |
| 38 | |
| 39 | next: |
| 40 | str = strstr(opts, opt); |
| 41 | end = str + optlen; |
| 42 | if (end - start > maxlen) |
| 43 | return NULL; |
| 44 | |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 45 | if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) && |
| 46 | (strpbrk(end, stopchs) == end || *end == eqch || |
| 47 | *end == '\0')) { |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 48 | const char *arg_end; |
| 49 | |
| 50 | if (!arglen) |
| 51 | return str; |
| 52 | |
| 53 | if (*end != eqch) |
| 54 | return NULL; |
| 55 | |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 56 | arg_end = strpbrk(str, stopchs); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 57 | if (!arg_end) |
| 58 | *arglen = min(maxlen, strlen(str)) - optlen - 1; |
| 59 | else |
| 60 | *arglen = arg_end - end - 1; |
| 61 | |
| 62 | return end + 1; |
| 63 | } else if (str) { |
| 64 | opts = end; |
| 65 | goto next; |
| 66 | } |
| 67 | return NULL; |
| 68 | } |
| 69 | |
Kumar Gala | b194577 | 2010-11-30 15:01:28 -0600 | [diff] [blame] | 70 | const char cpu_hwconfig[] __attribute__((weak)) = ""; |
| 71 | const char board_hwconfig[] __attribute__((weak)) = ""; |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 72 | |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 73 | static const char *__hwconfig(const char *opt, size_t *arglen, |
| 74 | const char *env_hwconfig) |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 75 | { |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 76 | const char *ret; |
Kumar Gala | c4b115f | 2010-10-22 03:18:13 -0500 | [diff] [blame] | 77 | |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 78 | /* if we are passed a buffer use it, otherwise try the environment */ |
| 79 | if (!env_hwconfig) { |
| 80 | if (!(gd->flags & GD_FLG_ENV_READY)) { |
| 81 | printf("WARNING: Calling __hwconfig without a buffer " |
Wolfgang Denk | d1a24f0 | 2011-02-02 22:36:10 +0100 | [diff] [blame] | 82 | "and before environment is ready\n"); |
| 83 | return NULL; |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 84 | } |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 85 | env_hwconfig = env_get("hwconfig"); |
Kumar Gala | c4b115f | 2010-10-22 03:18:13 -0500 | [diff] [blame] | 86 | } |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 87 | |
Kumar Gala | bb14107 | 2010-11-30 15:58:27 -0600 | [diff] [blame] | 88 | if (env_hwconfig) { |
| 89 | ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig), |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 90 | opt, ";", ':', arglen); |
Kumar Gala | bb14107 | 2010-11-30 15:58:27 -0600 | [diff] [blame] | 91 | if (ret) |
| 92 | return ret; |
| 93 | } |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 94 | |
Kumar Gala | bb14107 | 2010-11-30 15:58:27 -0600 | [diff] [blame] | 95 | ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig), |
Kumar Gala | b194577 | 2010-11-30 15:01:28 -0600 | [diff] [blame] | 96 | opt, ";", ':', arglen); |
Kumar Gala | bb14107 | 2010-11-30 15:58:27 -0600 | [diff] [blame] | 97 | if (ret) |
| 98 | return ret; |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 99 | |
Kumar Gala | b194577 | 2010-11-30 15:01:28 -0600 | [diff] [blame] | 100 | return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig), |
| 101 | opt, ";", ':', arglen); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 105 | * hwconfig_f - query if a particular hwconfig option is specified |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 106 | * @opt: a string representing an option |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 107 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 108 | * |
| 109 | * This call can be used to find out whether U-Boot should configure |
| 110 | * a particular hardware option. |
| 111 | * |
| 112 | * Returns non-zero value if the hardware option can be used and thus |
| 113 | * should be configured, 0 otherwise. |
| 114 | * |
| 115 | * This function also returns non-zero value if CONFIG_HWCONFIG is |
| 116 | * undefined. |
| 117 | * |
| 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. |
| 123 | */ |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 124 | int hwconfig_f(const char *opt, char *buf) |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 125 | { |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 126 | return !!__hwconfig(opt, NULL, buf); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 130 | * hwconfig_arg_f - get hwconfig option's argument |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 131 | * @opt: a string representing an option |
| 132 | * @arglen: a pointer to an allocated size_t variable |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 133 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 134 | * |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 135 | * Unlike hwconfig_f() function, this function returns a pointer to the |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 136 | * start of the hwconfig arguments, if option is not found or it has |
| 137 | * no specified arguments, the function returns NULL pointer. |
| 138 | * |
| 139 | * If CONFIG_HWCONFIG is undefined, the function returns "", and |
| 140 | * arglen is set to 0. |
| 141 | */ |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 142 | const char *hwconfig_arg_f(const char *opt, size_t *arglen, char *buf) |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 143 | { |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 144 | return __hwconfig(opt, arglen, buf); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 148 | * hwconfig_arg_cmp_f - compare hwconfig option's argument |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 149 | * @opt: a string representing an option |
| 150 | * @arg: a string for comparing an option's argument |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 151 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 152 | * |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 153 | * This call is similar to hwconfig_arg_f, but instead of returning |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 154 | * hwconfig argument and its length, it is comparing it to @arg. |
| 155 | * |
| 156 | * Returns non-zero value if @arg matches, 0 otherwise. |
| 157 | * |
| 158 | * If CONFIG_HWCONFIG is undefined, the function returns a non-zero |
| 159 | * value, i.e. the argument matches. |
| 160 | */ |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 161 | int hwconfig_arg_cmp_f(const char *opt, const char *arg, char *buf) |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 162 | { |
| 163 | const char *argstr; |
| 164 | size_t arglen; |
| 165 | |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 166 | argstr = hwconfig_arg_f(opt, &arglen, buf); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 167 | if (!argstr || arglen != strlen(arg)) |
| 168 | return 0; |
| 169 | |
| 170 | return !strncmp(argstr, arg, arglen); |
| 171 | } |
| 172 | |
| 173 | /* |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 174 | * hwconfig_sub_f - query if a particular hwconfig sub-option is specified |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 175 | * @opt: a string representing an option |
| 176 | * @subopt: a string representing a sub-option |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 177 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 178 | * |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 179 | * This call is similar to hwconfig_f(), except that it takes additional |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 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 |
| 183 | * argument. |
| 184 | */ |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 185 | int hwconfig_sub_f(const char *opt, const char *subopt, char *buf) |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 186 | { |
| 187 | size_t arglen; |
| 188 | const char *arg; |
| 189 | |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 190 | arg = __hwconfig(opt, &arglen, buf); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 191 | if (!arg) |
| 192 | return 0; |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 193 | return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 197 | * hwconfig_subarg_f - get hwconfig sub-option's argument |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 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 |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 201 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 202 | * |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 203 | * This call is similar to hwconfig_arg_f(), except that it takes an |
| 204 | * additional argument @subopt, and so works with sub-options. |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 205 | */ |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 206 | const char *hwconfig_subarg_f(const char *opt, const char *subopt, |
| 207 | size_t *subarglen, char *buf) |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 208 | { |
| 209 | size_t arglen; |
| 210 | const char *arg; |
| 211 | |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 212 | arg = __hwconfig(opt, &arglen, buf); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 213 | if (!arg) |
| 214 | return NULL; |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 215 | return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 219 | * hwconfig_arg_cmp_f - compare hwconfig sub-option's argument |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 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 |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 223 | * @buf: if non-NULL use this buffer to parse, otherwise try env |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 224 | * |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 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. |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 227 | */ |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 228 | int hwconfig_subarg_cmp_f(const char *opt, const char *subopt, |
| 229 | const char *subarg, char *buf) |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 230 | { |
| 231 | const char *argstr; |
| 232 | size_t arglen; |
| 233 | |
Kumar Gala | dd50af2 | 2011-01-09 11:37:00 -0600 | [diff] [blame] | 234 | argstr = hwconfig_subarg_f(opt, subopt, &arglen, buf); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 235 | if (!argstr || arglen != strlen(subarg)) |
| 236 | return 0; |
| 237 | |
| 238 | return !strncmp(argstr, subarg, arglen); |
| 239 | } |
Anton Vorontsov | 3bf74a4 | 2010-06-18 15:08:27 +0400 | [diff] [blame] | 240 | |
| 241 | #ifdef HWCONFIG_TEST |
| 242 | int main() |
| 243 | { |
| 244 | const char *ret; |
| 245 | size_t len; |
| 246 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 247 | env_set("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;" |
Anton Vorontsov | 3bf74a4 | 2010-06-18 15:08:27 +0400 | [diff] [blame] | 248 | "key3;:,:=;key4", 1); |
| 249 | |
| 250 | ret = hwconfig_arg("key1", &len); |
| 251 | printf("%zd %.*s\n", len, (int)len, ret); |
| 252 | assert(len == 29); |
| 253 | assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2")); |
| 254 | assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len)); |
| 255 | |
| 256 | ret = hwconfig_subarg("key1", "subkey1", &len); |
| 257 | printf("%zd %.*s\n", len, (int)len, ret); |
| 258 | assert(len == 6); |
| 259 | assert(hwconfig_subarg_cmp("key1", "subkey1", "value1")); |
| 260 | assert(!strncmp(ret, "value1", len)); |
| 261 | |
| 262 | ret = hwconfig_subarg("key1", "subkey2", &len); |
| 263 | printf("%zd %.*s\n", len, (int)len, ret); |
| 264 | assert(len == 6); |
| 265 | assert(hwconfig_subarg_cmp("key1", "subkey2", "value2")); |
| 266 | assert(!strncmp(ret, "value2", len)); |
| 267 | |
| 268 | ret = hwconfig_arg("key2", &len); |
| 269 | printf("%zd %.*s\n", len, (int)len, ret); |
| 270 | assert(len == 6); |
| 271 | assert(hwconfig_arg_cmp("key2", "value3")); |
| 272 | assert(!strncmp(ret, "value3", len)); |
| 273 | |
| 274 | assert(hwconfig("key3")); |
| 275 | assert(hwconfig_arg("key4", &len) == NULL); |
| 276 | assert(hwconfig_arg("bogus", &len) == NULL); |
| 277 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 278 | unenv_set("hwconfig"); |
Anton Vorontsov | 3bf74a4 | 2010-06-18 15:08:27 +0400 | [diff] [blame] | 279 | |
| 280 | assert(hwconfig(NULL) == 0); |
| 281 | assert(hwconfig("") == 0); |
| 282 | assert(hwconfig("key3") == 0); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | #endif /* HWCONFIG_TEST */ |