hwconfig.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include <config.h>
  14. #include <common.h>
  15. #include <exports.h>
  16. #include <hwconfig.h>
  17. #include <linux/types.h>
  18. #include <linux/string.h>
  19. static const char *hwconfig_parse(const char *opts, size_t maxlen,
  20. const char *opt, char *stopchs, char eqch,
  21. size_t *arglen)
  22. {
  23. size_t optlen = strlen(opt);
  24. char *str;
  25. const char *start = opts;
  26. const char *end;
  27. next:
  28. str = strstr(opts, opt);
  29. end = str + optlen;
  30. if (end - start > maxlen)
  31. return NULL;
  32. if (str && (str == opts || strpbrk(str - 1, stopchs) == str - 1) &&
  33. (strpbrk(end, stopchs) == end || *end == eqch ||
  34. *end == '\0')) {
  35. const char *arg_end;
  36. if (!arglen)
  37. return str;
  38. if (*end != eqch)
  39. return NULL;
  40. arg_end = strpbrk(str, stopchs);
  41. if (!arg_end)
  42. *arglen = min(maxlen, strlen(str)) - optlen - 1;
  43. else
  44. *arglen = arg_end - end - 1;
  45. return end + 1;
  46. } else if (str) {
  47. opts = end;
  48. goto next;
  49. }
  50. return NULL;
  51. }
  52. const char *cpu_hwconfig __attribute__((weak));
  53. const char *board_hwconfig __attribute__((weak));
  54. static const char *__hwconfig(const char *opt, size_t *arglen)
  55. {
  56. const char *env_hwconfig = getenv("hwconfig");
  57. if (env_hwconfig)
  58. return hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
  59. opt, ";", ':', arglen);
  60. if (board_hwconfig)
  61. return hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
  62. opt, ";", ':', arglen);
  63. if (cpu_hwconfig)
  64. return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
  65. opt, ";", ':', arglen);
  66. return NULL;
  67. }
  68. /*
  69. * hwconfig - query if a particular hwconfig option is specified
  70. * @opt: a string representing an option
  71. *
  72. * This call can be used to find out whether U-Boot should configure
  73. * a particular hardware option.
  74. *
  75. * Returns non-zero value if the hardware option can be used and thus
  76. * should be configured, 0 otherwise.
  77. *
  78. * This function also returns non-zero value if CONFIG_HWCONFIG is
  79. * undefined.
  80. *
  81. * Returning non-zero value without CONFIG_HWCONFIG has its crucial
  82. * purpose: the hwconfig() call should be a "transparent" interface,
  83. * e.g. if a board doesn't need hwconfig facility, then we assume
  84. * that the board file only calls things that are actually used, so
  85. * hwconfig() will always return true result.
  86. */
  87. int hwconfig(const char *opt)
  88. {
  89. return !!__hwconfig(opt, NULL);
  90. }
  91. /*
  92. * hwconfig_arg - get hwconfig option's argument
  93. * @opt: a string representing an option
  94. * @arglen: a pointer to an allocated size_t variable
  95. *
  96. * Unlike hwconfig() function, this function returns a pointer to the
  97. * start of the hwconfig arguments, if option is not found or it has
  98. * no specified arguments, the function returns NULL pointer.
  99. *
  100. * If CONFIG_HWCONFIG is undefined, the function returns "", and
  101. * arglen is set to 0.
  102. */
  103. const char *hwconfig_arg(const char *opt, size_t *arglen)
  104. {
  105. return __hwconfig(opt, arglen);
  106. }
  107. /*
  108. * hwconfig_arg_cmp - compare hwconfig option's argument
  109. * @opt: a string representing an option
  110. * @arg: a string for comparing an option's argument
  111. *
  112. * This call is similar to hwconfig_arg, but instead of returning
  113. * hwconfig argument and its length, it is comparing it to @arg.
  114. *
  115. * Returns non-zero value if @arg matches, 0 otherwise.
  116. *
  117. * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
  118. * value, i.e. the argument matches.
  119. */
  120. int hwconfig_arg_cmp(const char *opt, const char *arg)
  121. {
  122. const char *argstr;
  123. size_t arglen;
  124. argstr = hwconfig_arg(opt, &arglen);
  125. if (!argstr || arglen != strlen(arg))
  126. return 0;
  127. return !strncmp(argstr, arg, arglen);
  128. }
  129. /*
  130. * hwconfig_sub - query if a particular hwconfig sub-option is specified
  131. * @opt: a string representing an option
  132. * @subopt: a string representing a sub-option
  133. *
  134. * This call is similar to hwconfig(), except that it takes additional
  135. * argument @subopt. In this example:
  136. * "dr_usb:mode=peripheral"
  137. * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its
  138. * argument.
  139. */
  140. int hwconfig_sub(const char *opt, const char *subopt)
  141. {
  142. size_t arglen;
  143. const char *arg;
  144. arg = __hwconfig(opt, &arglen);
  145. if (!arg)
  146. return 0;
  147. return !!hwconfig_parse(arg, arglen, subopt, ",;", '=', NULL);
  148. }
  149. /*
  150. * hwconfig_subarg - get hwconfig sub-option's argument
  151. * @opt: a string representing an option
  152. * @subopt: a string representing a sub-option
  153. * @subarglen: a pointer to an allocated size_t variable
  154. *
  155. * This call is similar to hwconfig_arg(), except that it takes an additional
  156. * argument @subopt, and so works with sub-options.
  157. */
  158. const char *hwconfig_subarg(const char *opt, const char *subopt,
  159. size_t *subarglen)
  160. {
  161. size_t arglen;
  162. const char *arg;
  163. arg = __hwconfig(opt, &arglen);
  164. if (!arg)
  165. return NULL;
  166. return hwconfig_parse(arg, arglen, subopt, ",;", '=', subarglen);
  167. }
  168. /*
  169. * hwconfig_arg_cmp - compare hwconfig sub-option's argument
  170. * @opt: a string representing an option
  171. * @subopt: a string representing a sub-option
  172. * @subarg: a string for comparing an sub-option's argument
  173. *
  174. * This call is similar to hwconfig_arg_cmp, except that it takes an additional
  175. * argument @subopt, and so works with sub-options.
  176. */
  177. int hwconfig_subarg_cmp(const char *opt, const char *subopt, const char *subarg)
  178. {
  179. const char *argstr;
  180. size_t arglen;
  181. argstr = hwconfig_subarg(opt, subopt, &arglen);
  182. if (!argstr || arglen != strlen(subarg))
  183. return 0;
  184. return !strncmp(argstr, subarg, arglen);
  185. }