env-lib.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Synopsys, Inc. All rights reserved.
  4. * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
  5. */
  6. #include "env-lib.h"
  7. #define MAX_CMD_LEN 25
  8. static void env_clear_common(u32 index, const struct env_map_common *map)
  9. {
  10. map[index].val->val = 0;
  11. map[index].val->set = false;
  12. }
  13. static int env_read_common(u32 index, const struct env_map_common *map)
  14. {
  15. u32 val;
  16. if (!env_get_yesno(map[index].env_name)) {
  17. if (map[index].type == ENV_HEX) {
  18. val = (u32)env_get_hex(map[index].env_name, 0);
  19. debug("ENV: %s: = %#x\n", map[index].env_name, val);
  20. } else {
  21. val = (u32)env_get_ulong(map[index].env_name, 10, 0);
  22. debug("ENV: %s: = %d\n", map[index].env_name, val);
  23. }
  24. map[index].val->val = val;
  25. map[index].val->set = true;
  26. }
  27. return 0;
  28. }
  29. static void env_clear_core(u32 index, const struct env_map_percpu *map)
  30. {
  31. for (u32 i = 0; i < NR_CPUS; i++) {
  32. (*map[index].val)[i].val = 0;
  33. (*map[index].val)[i].set = false;
  34. }
  35. }
  36. static int env_read_core(u32 index, const struct env_map_percpu *map)
  37. {
  38. u32 val;
  39. char command[MAX_CMD_LEN];
  40. for (u32 i = 0; i < NR_CPUS; i++) {
  41. sprintf(command, "%s_%u", map[index].env_name, i);
  42. if (!env_get_yesno(command)) {
  43. if (map[index].type == ENV_HEX) {
  44. val = (u32)env_get_hex(command, 0);
  45. debug("ENV: %s: = %#x\n", command, val);
  46. } else {
  47. val = (u32)env_get_ulong(command, 10, 0);
  48. debug("ENV: %s: = %d\n", command, val);
  49. }
  50. (*map[index].val)[i].val = val;
  51. (*map[index].val)[i].set = true;
  52. }
  53. }
  54. return 0;
  55. }
  56. static int env_validate_common(u32 index, const struct env_map_common *map)
  57. {
  58. u32 value = map[index].val->val;
  59. bool set = map[index].val->set;
  60. u32 min = map[index].min;
  61. u32 max = map[index].max;
  62. /* Check if environment is mandatory */
  63. if (map[index].mandatory && !set) {
  64. pr_err("Variable \'%s\' is mandatory, but it is not defined\n",
  65. map[index].env_name);
  66. return -EINVAL;
  67. }
  68. /* Check environment boundary */
  69. if (set && (value < min || value > max)) {
  70. if (map[index].type == ENV_HEX)
  71. pr_err("Variable \'%s\' must be between %#x and %#x\n",
  72. map[index].env_name, min, max);
  73. else
  74. pr_err("Variable \'%s\' must be between %u and %u\n",
  75. map[index].env_name, min, max);
  76. return -EINVAL;
  77. }
  78. return 0;
  79. }
  80. static int env_validate_core(u32 index, const struct env_map_percpu *map,
  81. bool (*cpu_used)(u32))
  82. {
  83. u32 value;
  84. bool set;
  85. bool mandatory = map[index].mandatory;
  86. u32 min, max;
  87. for (u32 i = 0; i < NR_CPUS; i++) {
  88. set = (*map[index].val)[i].set;
  89. value = (*map[index].val)[i].val;
  90. /* Check if environment is mandatory */
  91. if (cpu_used(i) && mandatory && !set) {
  92. pr_err("CPU %u is used, but \'%s_%u\' is not defined\n",
  93. i, map[index].env_name, i);
  94. return -EINVAL;
  95. }
  96. min = map[index].min[i];
  97. max = map[index].max[i];
  98. /* Check environment boundary */
  99. if (set && (value < min || value > max)) {
  100. if (map[index].type == ENV_HEX)
  101. pr_err("Variable \'%s_%u\' must be between %#x and %#x\n",
  102. map[index].env_name, i, min, max);
  103. else
  104. pr_err("Variable \'%s_%u\' must be between %d and %d\n",
  105. map[index].env_name, i, min, max);
  106. return -EINVAL;
  107. }
  108. }
  109. return 0;
  110. }
  111. void envs_cleanup_core(const struct env_map_percpu *map)
  112. {
  113. /* Cleanup env struct first */
  114. for (u32 i = 0; map[i].env_name; i++)
  115. env_clear_core(i, map);
  116. }
  117. void envs_cleanup_common(const struct env_map_common *map)
  118. {
  119. /* Cleanup env struct first */
  120. for (u32 i = 0; map[i].env_name; i++)
  121. env_clear_common(i, map);
  122. }
  123. int envs_read_common(const struct env_map_common *map)
  124. {
  125. int ret;
  126. for (u32 i = 0; map[i].env_name; i++) {
  127. ret = env_read_common(i, map);
  128. if (ret)
  129. return ret;
  130. }
  131. return 0;
  132. }
  133. int envs_validate_common(const struct env_map_common *map)
  134. {
  135. int ret;
  136. for (u32 i = 0; map[i].env_name; i++) {
  137. ret = env_validate_common(i, map);
  138. if (ret)
  139. return ret;
  140. }
  141. return 0;
  142. }
  143. int envs_read_validate_common(const struct env_map_common *map)
  144. {
  145. int ret;
  146. envs_cleanup_common(map);
  147. ret = envs_read_common(map);
  148. if (ret)
  149. return ret;
  150. ret = envs_validate_common(map);
  151. if (ret)
  152. return ret;
  153. return 0;
  154. }
  155. int envs_read_validate_core(const struct env_map_percpu *map,
  156. bool (*cpu_used)(u32))
  157. {
  158. int ret;
  159. envs_cleanup_core(map);
  160. for (u32 i = 0; map[i].env_name; i++) {
  161. ret = env_read_core(i, map);
  162. if (ret)
  163. return ret;
  164. }
  165. for (u32 i = 0; map[i].env_name; i++) {
  166. ret = env_validate_core(i, map, cpu_used);
  167. if (ret)
  168. return ret;
  169. }
  170. return 0;
  171. }
  172. int envs_process_and_validate(const struct env_map_common *common,
  173. const struct env_map_percpu *core,
  174. bool (*cpu_used)(u32))
  175. {
  176. int ret;
  177. ret = envs_read_validate_common(common);
  178. if (ret)
  179. return ret;
  180. ret = envs_read_validate_core(core, cpu_used);
  181. if (ret)
  182. return ret;
  183. return 0;
  184. }
  185. static int args_envs_read_search(const struct env_map_common *map,
  186. int argc, char *const argv[])
  187. {
  188. for (int i = 0; map[i].env_name; i++) {
  189. if (!strcmp(argv[0], map[i].env_name))
  190. return i;
  191. }
  192. pr_err("Unexpected argument '%s', can't parse\n", argv[0]);
  193. return -ENOENT;
  194. }
  195. static int arg_read_set(const struct env_map_common *map, u32 i, int argc,
  196. char *const argv[])
  197. {
  198. char *endp = argv[1];
  199. if (map[i].type == ENV_HEX)
  200. map[i].val->val = simple_strtoul(argv[1], &endp, 16);
  201. else
  202. map[i].val->val = simple_strtoul(argv[1], &endp, 10);
  203. map[i].val->set = true;
  204. if (*endp == '\0')
  205. return 0;
  206. pr_err("Unexpected argument '%s', can't parse\n", argv[1]);
  207. map[i].val->set = false;
  208. return -EINVAL;
  209. }
  210. int args_envs_enumerate(const struct env_map_common *map, int enum_by,
  211. int argc, char *const argv[])
  212. {
  213. u32 i;
  214. if (argc % enum_by) {
  215. pr_err("unexpected argument number: %d\n", argc);
  216. return -EINVAL;
  217. }
  218. while (argc > 0) {
  219. i = args_envs_read_search(map, argc, argv);
  220. if (i < 0)
  221. return i;
  222. debug("ARG: found '%s' with index %d\n", map[i].env_name, i);
  223. if (i < 0) {
  224. pr_err("unknown arg: %s\n", argv[0]);
  225. return -EINVAL;
  226. }
  227. if (arg_read_set(map, i, argc, argv))
  228. return -EINVAL;
  229. debug("ARG: value.s '%s' == %#x\n", argv[1], map[i].val->val);
  230. argc -= enum_by;
  231. argv += enum_by;
  232. }
  233. return 0;
  234. }