hwconfig.c 7.6 KB

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