env-lib.c 6.1 KB

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