env-lib.c 6.1 KB

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