hwconfig.c 7.8 KB

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