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 14 #ifndef HWCONFIG_TEST 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> 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 */ 28 29 DECLARE_GLOBAL_DATA_PTR; 30 31 static const char *hwconfig_parse(const char *opts, size_t maxlen, 32 const char *opt, char *stopchs, char eqch, 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 46 if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) && 47 (strpbrk(end, stopchs) == end || *end == eqch || 48 *end == '\0')) { 49 const char *arg_end; 50 51 if (!arglen) 52 return str; 53 54 if (*end != eqch) 55 return NULL; 56 57 arg_end = strpbrk(str, stopchs); 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 74 #define HWCONFIG_PRE_RELOC_BUF_SIZE 128 75 76 static const char *__hwconfig(const char *opt, size_t *arglen) 77 { 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 } 94 95 if (env_hwconfig) 96 return hwconfig_parse(env_hwconfig, strlen(env_hwconfig), 97 opt, ";", ':', arglen); 98 99 return hwconfig_parse(board_hwconfig, strlen(board_hwconfig), 100 opt, ";", ':', arglen); 101 102 return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig), 103 opt, ";", ':', arglen); 104 105 return NULL; 106 } 107 108 /* 109 * hwconfig - query if a particular hwconfig option is specified 110 * @opt: a string representing an option 111 * 112 * This call can be used to find out whether U-Boot should configure 113 * a particular hardware option. 114 * 115 * Returns non-zero value if the hardware option can be used and thus 116 * should be configured, 0 otherwise. 117 * 118 * This function also returns non-zero value if CONFIG_HWCONFIG is 119 * undefined. 120 * 121 * Returning non-zero value without CONFIG_HWCONFIG has its crucial 122 * purpose: the hwconfig() call should be a "transparent" interface, 123 * e.g. if a board doesn't need hwconfig facility, then we assume 124 * that the board file only calls things that are actually used, so 125 * hwconfig() will always return true result. 126 */ 127 int hwconfig(const char *opt) 128 { 129 return !!__hwconfig(opt, NULL); 130 } 131 132 /* 133 * hwconfig_arg - get hwconfig option's argument 134 * @opt: a string representing an option 135 * @arglen: a pointer to an allocated size_t variable 136 * 137 * Unlike hwconfig() function, this function returns a pointer to the 138 * start of the hwconfig arguments, if option is not found or it has 139 * no specified arguments, the function returns NULL pointer. 140 * 141 * If CONFIG_HWCONFIG is undefined, the function returns "", and 142 * arglen is set to 0. 143 */ 144 const char *hwconfig_arg(const char *opt, size_t *arglen) 145 { 146 return __hwconfig(opt, arglen); 147 } 148 149 /* 150 * hwconfig_arg_cmp - compare hwconfig option's argument 151 * @opt: a string representing an option 152 * @arg: a string for comparing an option's argument 153 * 154 * This call is similar to hwconfig_arg, but instead of returning 155 * hwconfig argument and its length, it is comparing it to @arg. 156 * 157 * Returns non-zero value if @arg matches, 0 otherwise. 158 * 159 * If CONFIG_HWCONFIG is undefined, the function returns a non-zero 160 * value, i.e. the argument matches. 161 */ 162 int hwconfig_arg_cmp(const char *opt, const char *arg) 163 { 164 const char *argstr; 165 size_t arglen; 166 167 argstr = hwconfig_arg(opt, &arglen); 168 if (!argstr || arglen != strlen(arg)) 169 return 0; 170 171 return !strncmp(argstr, arg, arglen); 172 } 173 174 /* 175 * hwconfig_sub - query if a particular hwconfig sub-option is specified 176 * @opt: a string representing an option 177 * @subopt: a string representing a sub-option 178 * 179 * This call is similar to hwconfig(), except that it takes additional 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 */ 185 int hwconfig_sub(const char *opt, const char *subopt) 186 { 187 size_t arglen; 188 const char *arg; 189 190 arg = __hwconfig(opt, &arglen); 191 if (!arg) 192 return 0; 193 return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL); 194 } 195 196 /* 197 * hwconfig_subarg - get hwconfig sub-option's argument 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 201 * 202 * This call is similar to hwconfig_arg(), except that it takes an additional 203 * argument @subopt, and so works with sub-options. 204 */ 205 const char *hwconfig_subarg(const char *opt, const char *subopt, 206 size_t *subarglen) 207 { 208 size_t arglen; 209 const char *arg; 210 211 arg = __hwconfig(opt, &arglen); 212 if (!arg) 213 return NULL; 214 return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen); 215 } 216 217 /* 218 * hwconfig_arg_cmp - compare hwconfig sub-option's argument 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 222 * 223 * This call is similar to hwconfig_arg_cmp, except that it takes an additional 224 * argument @subopt, and so works with sub-options. 225 */ 226 int hwconfig_subarg_cmp(const char *opt, const char *subopt, const char *subarg) 227 { 228 const char *argstr; 229 size_t arglen; 230 231 argstr = hwconfig_subarg(opt, subopt, &arglen); 232 if (!argstr || arglen != strlen(subarg)) 233 return 0; 234 235 return !strncmp(argstr, subarg, arglen); 236 } 237 238 #ifdef HWCONFIG_TEST 239 int main() 240 { 241 const char *ret; 242 size_t len; 243 244 setenv("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;" 245 "key3;:,:=;key4", 1); 246 247 ret = hwconfig_arg("key1", &len); 248 printf("%zd %.*s\n", len, (int)len, ret); 249 assert(len == 29); 250 assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2")); 251 assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len)); 252 253 ret = hwconfig_subarg("key1", "subkey1", &len); 254 printf("%zd %.*s\n", len, (int)len, ret); 255 assert(len == 6); 256 assert(hwconfig_subarg_cmp("key1", "subkey1", "value1")); 257 assert(!strncmp(ret, "value1", len)); 258 259 ret = hwconfig_subarg("key1", "subkey2", &len); 260 printf("%zd %.*s\n", len, (int)len, ret); 261 assert(len == 6); 262 assert(hwconfig_subarg_cmp("key1", "subkey2", "value2")); 263 assert(!strncmp(ret, "value2", len)); 264 265 ret = hwconfig_arg("key2", &len); 266 printf("%zd %.*s\n", len, (int)len, ret); 267 assert(len == 6); 268 assert(hwconfig_arg_cmp("key2", "value3")); 269 assert(!strncmp(ret, "value3", len)); 270 271 assert(hwconfig("key3")); 272 assert(hwconfig_arg("key4", &len) == NULL); 273 assert(hwconfig_arg("bogus", &len) == NULL); 274 275 unsetenv("hwconfig"); 276 277 assert(hwconfig(NULL) == 0); 278 assert(hwconfig("") == 0); 279 assert(hwconfig("key3") == 0); 280 281 return 0; 282 } 283 #endif /* HWCONFIG_TEST */ 284