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