hwconfig.c 7.1 KB

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