hwconfig.c 5.6 KB

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