hwconfig.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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, *ret;
  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. ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
  83. opt, ";", ':', arglen);
  84. if (ret)
  85. return ret;
  86. }
  87. ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
  88. opt, ";", ':', arglen);
  89. if (ret)
  90. return ret;
  91. return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
  92. opt, ";", ':', arglen);
  93. }
  94. /*
  95. * hwconfig - query if a particular hwconfig option is specified
  96. * @opt: a string representing an option
  97. *
  98. * This call can be used to find out whether U-Boot should configure
  99. * a particular hardware option.
  100. *
  101. * Returns non-zero value if the hardware option can be used and thus
  102. * should be configured, 0 otherwise.
  103. *
  104. * This function also returns non-zero value if CONFIG_HWCONFIG is
  105. * undefined.
  106. *
  107. * Returning non-zero value without CONFIG_HWCONFIG has its crucial
  108. * purpose: the hwconfig() call should be a "transparent" interface,
  109. * e.g. if a board doesn't need hwconfig facility, then we assume
  110. * that the board file only calls things that are actually used, so
  111. * hwconfig() will always return true result.
  112. */
  113. int hwconfig(const char *opt)
  114. {
  115. return !!__hwconfig(opt, NULL);
  116. }
  117. /*
  118. * hwconfig_arg - get hwconfig option's argument
  119. * @opt: a string representing an option
  120. * @arglen: a pointer to an allocated size_t variable
  121. *
  122. * Unlike hwconfig() function, this function returns a pointer to the
  123. * start of the hwconfig arguments, if option is not found or it has
  124. * no specified arguments, the function returns NULL pointer.
  125. *
  126. * If CONFIG_HWCONFIG is undefined, the function returns "", and
  127. * arglen is set to 0.
  128. */
  129. const char *hwconfig_arg(const char *opt, size_t *arglen)
  130. {
  131. return __hwconfig(opt, arglen);
  132. }
  133. /*
  134. * hwconfig_arg_cmp - compare hwconfig option's argument
  135. * @opt: a string representing an option
  136. * @arg: a string for comparing an option's argument
  137. *
  138. * This call is similar to hwconfig_arg, but instead of returning
  139. * hwconfig argument and its length, it is comparing it to @arg.
  140. *
  141. * Returns non-zero value if @arg matches, 0 otherwise.
  142. *
  143. * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
  144. * value, i.e. the argument matches.
  145. */
  146. int hwconfig_arg_cmp(const char *opt, const char *arg)
  147. {
  148. const char *argstr;
  149. size_t arglen;
  150. argstr = hwconfig_arg(opt, &arglen);
  151. if (!argstr || arglen != strlen(arg))
  152. return 0;
  153. return !strncmp(argstr, arg, arglen);
  154. }
  155. /*
  156. * hwconfig_sub - query if a particular hwconfig sub-option is specified
  157. * @opt: a string representing an option
  158. * @subopt: a string representing a sub-option
  159. *
  160. * This call is similar to hwconfig(), except that it takes additional
  161. * argument @subopt. In this example:
  162. * "dr_usb:mode=peripheral"
  163. * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its
  164. * argument.
  165. */
  166. int hwconfig_sub(const char *opt, const char *subopt)
  167. {
  168. size_t arglen;
  169. const char *arg;
  170. arg = __hwconfig(opt, &arglen);
  171. if (!arg)
  172. return 0;
  173. return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
  174. }
  175. /*
  176. * hwconfig_subarg - get hwconfig sub-option's argument
  177. * @opt: a string representing an option
  178. * @subopt: a string representing a sub-option
  179. * @subarglen: a pointer to an allocated size_t variable
  180. *
  181. * This call is similar to hwconfig_arg(), except that it takes an additional
  182. * argument @subopt, and so works with sub-options.
  183. */
  184. const char *hwconfig_subarg(const char *opt, const char *subopt,
  185. size_t *subarglen)
  186. {
  187. size_t arglen;
  188. const char *arg;
  189. arg = __hwconfig(opt, &arglen);
  190. if (!arg)
  191. return NULL;
  192. return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
  193. }
  194. /*
  195. * hwconfig_arg_cmp - compare hwconfig sub-option's argument
  196. * @opt: a string representing an option
  197. * @subopt: a string representing a sub-option
  198. * @subarg: a string for comparing an sub-option's argument
  199. *
  200. * This call is similar to hwconfig_arg_cmp, except that it takes an additional
  201. * argument @subopt, and so works with sub-options.
  202. */
  203. int hwconfig_subarg_cmp(const char *opt, const char *subopt, const char *subarg)
  204. {
  205. const char *argstr;
  206. size_t arglen;
  207. argstr = hwconfig_subarg(opt, subopt, &arglen);
  208. if (!argstr || arglen != strlen(subarg))
  209. return 0;
  210. return !strncmp(argstr, subarg, arglen);
  211. }
  212. #ifdef HWCONFIG_TEST
  213. int main()
  214. {
  215. const char *ret;
  216. size_t len;
  217. setenv("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
  218. "key3;:,:=;key4", 1);
  219. ret = hwconfig_arg("key1", &len);
  220. printf("%zd %.*s\n", len, (int)len, ret);
  221. assert(len == 29);
  222. assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
  223. assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len));
  224. ret = hwconfig_subarg("key1", "subkey1", &len);
  225. printf("%zd %.*s\n", len, (int)len, ret);
  226. assert(len == 6);
  227. assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
  228. assert(!strncmp(ret, "value1", len));
  229. ret = hwconfig_subarg("key1", "subkey2", &len);
  230. printf("%zd %.*s\n", len, (int)len, ret);
  231. assert(len == 6);
  232. assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
  233. assert(!strncmp(ret, "value2", len));
  234. ret = hwconfig_arg("key2", &len);
  235. printf("%zd %.*s\n", len, (int)len, ret);
  236. assert(len == 6);
  237. assert(hwconfig_arg_cmp("key2", "value3"));
  238. assert(!strncmp(ret, "value3", len));
  239. assert(hwconfig("key3"));
  240. assert(hwconfig_arg("key4", &len) == NULL);
  241. assert(hwconfig_arg("bogus", &len) == NULL);
  242. unsetenv("hwconfig");
  243. assert(hwconfig(NULL) == 0);
  244. assert(hwconfig("") == 0);
  245. assert(hwconfig("key3") == 0);
  246. return 0;
  247. }
  248. #endif /* HWCONFIG_TEST */