Anton Vorontsov | 93f9dcf | 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. |
| 5 | * |
| 6 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
Anton Vorontsov | 3bf74a4 | 2010-06-18 15:08:27 +0400 | [diff] [blame] | 14 | #ifndef HWCONFIG_TEST |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 15 | #include <config.h> |
| 16 | #include <common.h> |
| 17 | #include <exports.h> |
| 18 | #include <hwconfig.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/string.h> |
Anton Vorontsov | 3bf74a4 | 2010-06-18 15:08:27 +0400 | [diff] [blame] | 21 | #else |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <assert.h> |
| 26 | #define min(a, b) (((a) < (b)) ? (a) : (b)) |
| 27 | #endif /* HWCONFIG_TEST */ |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 28 | |
Kumar Gala | c4b115f | 2010-10-22 03:18:13 -0500 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 31 | static const char *hwconfig_parse(const char *opts, size_t maxlen, |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 32 | const char *opt, char *stopchs, char eqch, |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 33 | size_t *arglen) |
| 34 | { |
| 35 | size_t optlen = strlen(opt); |
| 36 | char *str; |
| 37 | const char *start = opts; |
| 38 | const char *end; |
| 39 | |
| 40 | next: |
| 41 | str = strstr(opts, opt); |
| 42 | end = str + optlen; |
| 43 | if (end - start > maxlen) |
| 44 | return NULL; |
| 45 | |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 46 | if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) && |
| 47 | (strpbrk(end, stopchs) == end || *end == eqch || |
| 48 | *end == '\0')) { |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 49 | const char *arg_end; |
| 50 | |
| 51 | if (!arglen) |
| 52 | return str; |
| 53 | |
| 54 | if (*end != eqch) |
| 55 | return NULL; |
| 56 | |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 57 | arg_end = strpbrk(str, stopchs); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 58 | if (!arg_end) |
| 59 | *arglen = min(maxlen, strlen(str)) - optlen - 1; |
| 60 | else |
| 61 | *arglen = arg_end - end - 1; |
| 62 | |
| 63 | return end + 1; |
| 64 | } else if (str) { |
| 65 | opts = end; |
| 66 | goto next; |
| 67 | } |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | const char *cpu_hwconfig __attribute__((weak)); |
| 72 | const char *board_hwconfig __attribute__((weak)); |
| 73 | |
Kumar Gala | c4b115f | 2010-10-22 03:18:13 -0500 | [diff] [blame] | 74 | #define HWCONFIG_PRE_RELOC_BUF_SIZE 128 |
| 75 | |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 76 | static const char *__hwconfig(const char *opt, size_t *arglen) |
| 77 | { |
Kumar Gala | c4b115f | 2010-10-22 03:18:13 -0500 | [diff] [blame] | 78 | const char *env_hwconfig = NULL; |
| 79 | char buf[HWCONFIG_PRE_RELOC_BUF_SIZE]; |
| 80 | |
| 81 | if (gd->flags & GD_FLG_ENV_READY) { |
| 82 | env_hwconfig = getenv("hwconfig"); |
| 83 | } else { |
| 84 | /* |
| 85 | * Use our own on stack based buffer before relocation to allow |
| 86 | * accessing longer hwconfig strings that might be in the |
| 87 | * environment before we've relocated. This is pretty fragile |
| 88 | * on both the use of stack and if the buffer is big enough. |
| 89 | * However we will get a warning from getenv_f for the later. |
| 90 | */ |
| 91 | if ((getenv_f("hwconfig", buf, sizeof(buf))) > 0) |
| 92 | env_hwconfig = buf; |
| 93 | } |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 94 | |
| 95 | if (env_hwconfig) |
| 96 | return hwconfig_parse(env_hwconfig, strlen(env_hwconfig), |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 97 | opt, ";", ':', arglen); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 98 | |
| 99 | if (board_hwconfig) |
| 100 | return hwconfig_parse(board_hwconfig, strlen(board_hwconfig), |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 101 | opt, ";", ':', arglen); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 102 | |
| 103 | if (cpu_hwconfig) |
| 104 | return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig), |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 105 | opt, ";", ':', arglen); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 106 | |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * hwconfig - query if a particular hwconfig option is specified |
| 112 | * @opt: a string representing an option |
| 113 | * |
| 114 | * This call can be used to find out whether U-Boot should configure |
| 115 | * a particular hardware option. |
| 116 | * |
| 117 | * Returns non-zero value if the hardware option can be used and thus |
| 118 | * should be configured, 0 otherwise. |
| 119 | * |
| 120 | * This function also returns non-zero value if CONFIG_HWCONFIG is |
| 121 | * undefined. |
| 122 | * |
| 123 | * Returning non-zero value without CONFIG_HWCONFIG has its crucial |
| 124 | * purpose: the hwconfig() call should be a "transparent" interface, |
| 125 | * e.g. if a board doesn't need hwconfig facility, then we assume |
| 126 | * that the board file only calls things that are actually used, so |
| 127 | * hwconfig() will always return true result. |
| 128 | */ |
| 129 | int hwconfig(const char *opt) |
| 130 | { |
| 131 | return !!__hwconfig(opt, NULL); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * hwconfig_arg - get hwconfig option's argument |
| 136 | * @opt: a string representing an option |
| 137 | * @arglen: a pointer to an allocated size_t variable |
| 138 | * |
| 139 | * Unlike hwconfig() function, this function returns a pointer to the |
| 140 | * start of the hwconfig arguments, if option is not found or it has |
| 141 | * no specified arguments, the function returns NULL pointer. |
| 142 | * |
| 143 | * If CONFIG_HWCONFIG is undefined, the function returns "", and |
| 144 | * arglen is set to 0. |
| 145 | */ |
| 146 | const char *hwconfig_arg(const char *opt, size_t *arglen) |
| 147 | { |
| 148 | return __hwconfig(opt, arglen); |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * hwconfig_arg_cmp - compare hwconfig option's argument |
| 153 | * @opt: a string representing an option |
| 154 | * @arg: a string for comparing an option's argument |
| 155 | * |
| 156 | * This call is similar to hwconfig_arg, but instead of returning |
| 157 | * hwconfig argument and its length, it is comparing it to @arg. |
| 158 | * |
| 159 | * Returns non-zero value if @arg matches, 0 otherwise. |
| 160 | * |
| 161 | * If CONFIG_HWCONFIG is undefined, the function returns a non-zero |
| 162 | * value, i.e. the argument matches. |
| 163 | */ |
| 164 | int hwconfig_arg_cmp(const char *opt, const char *arg) |
| 165 | { |
| 166 | const char *argstr; |
| 167 | size_t arglen; |
| 168 | |
| 169 | argstr = hwconfig_arg(opt, &arglen); |
| 170 | if (!argstr || arglen != strlen(arg)) |
| 171 | return 0; |
| 172 | |
| 173 | return !strncmp(argstr, arg, arglen); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * hwconfig_sub - query if a particular hwconfig sub-option is specified |
| 178 | * @opt: a string representing an option |
| 179 | * @subopt: a string representing a sub-option |
| 180 | * |
| 181 | * This call is similar to hwconfig(), except that it takes additional |
| 182 | * argument @subopt. In this example: |
| 183 | * "dr_usb:mode=peripheral" |
| 184 | * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its |
| 185 | * argument. |
| 186 | */ |
| 187 | int hwconfig_sub(const char *opt, const char *subopt) |
| 188 | { |
| 189 | size_t arglen; |
| 190 | const char *arg; |
| 191 | |
| 192 | arg = __hwconfig(opt, &arglen); |
| 193 | if (!arg) |
| 194 | return 0; |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 195 | return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* |
| 199 | * hwconfig_subarg - get hwconfig sub-option's argument |
| 200 | * @opt: a string representing an option |
| 201 | * @subopt: a string representing a sub-option |
| 202 | * @subarglen: a pointer to an allocated size_t variable |
| 203 | * |
| 204 | * This call is similar to hwconfig_arg(), except that it takes an additional |
| 205 | * argument @subopt, and so works with sub-options. |
| 206 | */ |
| 207 | const char *hwconfig_subarg(const char *opt, const char *subopt, |
| 208 | size_t *subarglen) |
| 209 | { |
| 210 | size_t arglen; |
| 211 | const char *arg; |
| 212 | |
| 213 | arg = __hwconfig(opt, &arglen); |
| 214 | if (!arg) |
| 215 | return NULL; |
Anton Vorontsov | 81f8d3b | 2010-06-18 15:08:12 +0400 | [diff] [blame] | 216 | return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen); |
Anton Vorontsov | 93f9dcf | 2009-06-10 00:25:27 +0400 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* |
| 220 | * hwconfig_arg_cmp - compare hwconfig sub-option's argument |
| 221 | * @opt: a string representing an option |
| 222 | * @subopt: a string representing a sub-option |
| 223 | * @subarg: a string for comparing an sub-option's argument |
| 224 | * |
| 225 | * This call is similar to hwconfig_arg_cmp, except that it takes an additional |
| 226 | * argument @subopt, and so works with sub-options. |
| 227 | */ |
| 228 | int hwconfig_subarg_cmp(const char *opt, const char *subopt, const char *subarg) |
| 229 | { |
| 230 | const char *argstr; |
| 231 | size_t arglen; |
| 232 | |
| 233 | argstr = hwconfig_subarg(opt, subopt, &arglen); |
| 234 | if (!argstr || arglen != strlen(subarg)) |
| 235 | return 0; |
| 236 | |
| 237 | return !strncmp(argstr, subarg, arglen); |
| 238 | } |
Anton Vorontsov | 3bf74a4 | 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 */ |