hwconfig.c 7.7 KB

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