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