hwconfig.c 7.8 KB

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