hwconfig.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * An inteface for configuring a hardware via u-boot environment.
  4. *
  5. * Copyright (c) 2009 MontaVista Software, Inc.
  6. * Copyright 2011 Freescale Semiconductor, Inc.
  7. *
  8. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  9. */
  10. #ifndef HWCONFIG_TEST
  11. #include <config.h>
  12. #include <common.h>
  13. #include <env.h>
  14. #include <exports.h>
  15. #include <hwconfig.h>
  16. #include <linux/types.h>
  17. #include <linux/string.h>
  18. #else
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <assert.h>
  23. #define min(a, b) (((a) < (b)) ? (a) : (b))
  24. #endif /* HWCONFIG_TEST */
  25. DECLARE_GLOBAL_DATA_PTR;
  26. static const char *hwconfig_parse(const char *opts, size_t maxlen,
  27. const char *opt, char *stopchs, char eqch,
  28. size_t *arglen)
  29. {
  30. size_t optlen = strlen(opt);
  31. char *str;
  32. const char *start = opts;
  33. const char *end;
  34. next:
  35. str = strstr(opts, opt);
  36. end = str + optlen;
  37. if (end - start > maxlen)
  38. return NULL;
  39. if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) &&
  40. (strpbrk(end, stopchs) == end || *end == eqch ||
  41. *end == '\0')) {
  42. const char *arg_end;
  43. if (!arglen)
  44. return str;
  45. if (*end != eqch)
  46. return NULL;
  47. arg_end = strpbrk(str, stopchs);
  48. if (!arg_end)
  49. *arglen = min(maxlen, strlen(str)) - optlen - 1;
  50. else
  51. *arglen = arg_end - end - 1;
  52. return end + 1;
  53. } else if (str) {
  54. opts = end;
  55. goto next;
  56. }
  57. return NULL;
  58. }
  59. const char cpu_hwconfig[] __attribute__((weak)) = "";
  60. const char board_hwconfig[] __attribute__((weak)) = "";
  61. static const char *__hwconfig(const char *opt, size_t *arglen,
  62. const char *env_hwconfig)
  63. {
  64. const char *ret;
  65. /* if we are passed a buffer use it, otherwise try the environment */
  66. if (!env_hwconfig) {
  67. if (!(gd->flags & GD_FLG_ENV_READY)) {
  68. printf("WARNING: Calling __hwconfig without a buffer "
  69. "and before environment is ready\n");
  70. return NULL;
  71. }
  72. env_hwconfig = env_get("hwconfig");
  73. }
  74. if (env_hwconfig) {
  75. ret = hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
  76. opt, ";", ':', arglen);
  77. if (ret)
  78. return ret;
  79. }
  80. ret = hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
  81. opt, ";", ':', arglen);
  82. if (ret)
  83. return ret;
  84. return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
  85. opt, ";", ':', arglen);
  86. }
  87. /*
  88. * hwconfig_f - query if a particular hwconfig option is specified
  89. * @opt: a string representing an option
  90. * @buf: if non-NULL use this buffer to parse, otherwise try env
  91. *
  92. * This call can be used to find out whether U-Boot should configure
  93. * a particular hardware option.
  94. *
  95. * Returns non-zero value if the hardware option can be used and thus
  96. * should be configured, 0 otherwise.
  97. *
  98. * This function also returns non-zero value if CONFIG_HWCONFIG is
  99. * undefined.
  100. *
  101. * Returning non-zero value without CONFIG_HWCONFIG has its crucial
  102. * purpose: the hwconfig() call should be a "transparent" interface,
  103. * e.g. if a board doesn't need hwconfig facility, then we assume
  104. * that the board file only calls things that are actually used, so
  105. * hwconfig() will always return true result.
  106. */
  107. int hwconfig_f(const char *opt, char *buf)
  108. {
  109. return !!__hwconfig(opt, NULL, buf);
  110. }
  111. /*
  112. * hwconfig_arg_f - get hwconfig option's argument
  113. * @opt: a string representing an option
  114. * @arglen: a pointer to an allocated size_t variable
  115. * @buf: if non-NULL use this buffer to parse, otherwise try env
  116. *
  117. * Unlike hwconfig_f() function, this function returns a pointer to the
  118. * start of the hwconfig arguments, if option is not found or it has
  119. * no specified arguments, the function returns NULL pointer.
  120. *
  121. * If CONFIG_HWCONFIG is undefined, the function returns "", and
  122. * arglen is set to 0.
  123. */
  124. const char *hwconfig_arg_f(const char *opt, size_t *arglen, char *buf)
  125. {
  126. return __hwconfig(opt, arglen, buf);
  127. }
  128. /*
  129. * hwconfig_arg_cmp_f - compare hwconfig option's argument
  130. * @opt: a string representing an option
  131. * @arg: a string for comparing an option's argument
  132. * @buf: if non-NULL use this buffer to parse, otherwise try env
  133. *
  134. * This call is similar to hwconfig_arg_f, but instead of returning
  135. * hwconfig argument and its length, it is comparing it to @arg.
  136. *
  137. * Returns non-zero value if @arg matches, 0 otherwise.
  138. *
  139. * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
  140. * value, i.e. the argument matches.
  141. */
  142. int hwconfig_arg_cmp_f(const char *opt, const char *arg, char *buf)
  143. {
  144. const char *argstr;
  145. size_t arglen;
  146. argstr = hwconfig_arg_f(opt, &arglen, buf);
  147. if (!argstr || arglen != strlen(arg))
  148. return 0;
  149. return !strncmp(argstr, arg, arglen);
  150. }
  151. /*
  152. * hwconfig_sub_f - query if a particular hwconfig sub-option is specified
  153. * @opt: a string representing an option
  154. * @subopt: a string representing a sub-option
  155. * @buf: if non-NULL use this buffer to parse, otherwise try env
  156. *
  157. * This call is similar to hwconfig_f(), except that it takes additional
  158. * argument @subopt. In this example:
  159. * "dr_usb:mode=peripheral"
  160. * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its
  161. * argument.
  162. */
  163. int hwconfig_sub_f(const char *opt, const char *subopt, char *buf)
  164. {
  165. size_t arglen;
  166. const char *arg;
  167. arg = __hwconfig(opt, &arglen, buf);
  168. if (!arg)
  169. return 0;
  170. return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
  171. }
  172. /*
  173. * hwconfig_subarg_f - get hwconfig sub-option's argument
  174. * @opt: a string representing an option
  175. * @subopt: a string representing a sub-option
  176. * @subarglen: a pointer to an allocated size_t variable
  177. * @buf: if non-NULL use this buffer to parse, otherwise try env
  178. *
  179. * This call is similar to hwconfig_arg_f(), except that it takes an
  180. * additional argument @subopt, and so works with sub-options.
  181. */
  182. const char *hwconfig_subarg_f(const char *opt, const char *subopt,
  183. size_t *subarglen, char *buf)
  184. {
  185. size_t arglen;
  186. const char *arg;
  187. arg = __hwconfig(opt, &arglen, buf);
  188. if (!arg)
  189. return NULL;
  190. return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
  191. }
  192. /*
  193. * hwconfig_arg_cmp_f - 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. * @buf: if non-NULL use this buffer to parse, otherwise try env
  198. *
  199. * This call is similar to hwconfig_arg_cmp_f, except that it takes an
  200. * additional argument @subopt, and so works with sub-options.
  201. */
  202. int hwconfig_subarg_cmp_f(const char *opt, const char *subopt,
  203. const char *subarg, char *buf)
  204. {
  205. const char *argstr;
  206. size_t arglen;
  207. argstr = hwconfig_subarg_f(opt, subopt, &arglen, buf);
  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. env_set("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. unenv_set("hwconfig");
  243. assert(hwconfig(NULL) == 0);
  244. assert(hwconfig("") == 0);
  245. assert(hwconfig("key3") == 0);
  246. return 0;
  247. }
  248. #endif /* HWCONFIG_TEST */