1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Synopsys, Inc. All rights reserved.
4 * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
10 #define MAX_CMD_LEN 25
12 static void env_clear_common(u32 index, const struct env_map_common *map)
14 map[index].val->val = 0;
15 map[index].val->set = false;
18 static int env_read_common(u32 index, const struct env_map_common *map)
22 if (!env_get_yesno(map[index].env_name)) {
23 if (map[index].type == ENV_HEX) {
24 val = (u32)env_get_hex(map[index].env_name, 0);
25 debug("ENV: %s: = %#x\n", map[index].env_name, val);
27 val = (u32)env_get_ulong(map[index].env_name, 10, 0);
28 debug("ENV: %s: = %d\n", map[index].env_name, val);
31 map[index].val->val = val;
32 map[index].val->set = true;
38 static void env_clear_core(u32 index, const struct env_map_percpu *map)
40 for (u32 i = 0; i < NR_CPUS; i++) {
41 (*map[index].val)[i].val = 0;
42 (*map[index].val)[i].set = false;
46 static int env_read_core(u32 index, const struct env_map_percpu *map)
49 char command[MAX_CMD_LEN];
51 for (u32 i = 0; i < NR_CPUS; i++) {
52 sprintf(command, "%s_%u", map[index].env_name, i);
53 if (!env_get_yesno(command)) {
54 if (map[index].type == ENV_HEX) {
55 val = (u32)env_get_hex(command, 0);
56 debug("ENV: %s: = %#x\n", command, val);
58 val = (u32)env_get_ulong(command, 10, 0);
59 debug("ENV: %s: = %d\n", command, val);
62 (*map[index].val)[i].val = val;
63 (*map[index].val)[i].set = true;
70 static int env_validate_common(u32 index, const struct env_map_common *map)
72 u32 value = map[index].val->val;
73 bool set = map[index].val->set;
74 u32 min = map[index].min;
75 u32 max = map[index].max;
77 /* Check if environment is mandatory */
78 if (map[index].mandatory && !set) {
79 pr_err("Variable \'%s\' is mandatory, but it is not defined\n",
85 /* Check environment boundary */
86 if (set && (value < min || value > max)) {
87 if (map[index].type == ENV_HEX)
88 pr_err("Variable \'%s\' must be between %#x and %#x\n",
89 map[index].env_name, min, max);
91 pr_err("Variable \'%s\' must be between %u and %u\n",
92 map[index].env_name, min, max);
100 static int env_validate_core(u32 index, const struct env_map_percpu *map,
101 bool (*cpu_used)(u32))
105 bool mandatory = map[index].mandatory;
108 for (u32 i = 0; i < NR_CPUS; i++) {
109 set = (*map[index].val)[i].set;
110 value = (*map[index].val)[i].val;
112 /* Check if environment is mandatory */
113 if (cpu_used(i) && mandatory && !set) {
114 pr_err("CPU %u is used, but \'%s_%u\' is not defined\n",
115 i, map[index].env_name, i);
120 min = map[index].min[i];
121 max = map[index].max[i];
123 /* Check environment boundary */
124 if (set && (value < min || value > max)) {
125 if (map[index].type == ENV_HEX)
126 pr_err("Variable \'%s_%u\' must be between %#x and %#x\n",
127 map[index].env_name, i, min, max);
129 pr_err("Variable \'%s_%u\' must be between %d and %d\n",
130 map[index].env_name, i, min, max);
139 void envs_cleanup_core(const struct env_map_percpu *map)
141 /* Cleanup env struct first */
142 for (u32 i = 0; map[i].env_name; i++)
143 env_clear_core(i, map);
146 void envs_cleanup_common(const struct env_map_common *map)
148 /* Cleanup env struct first */
149 for (u32 i = 0; map[i].env_name; i++)
150 env_clear_common(i, map);
153 int envs_read_common(const struct env_map_common *map)
157 for (u32 i = 0; map[i].env_name; i++) {
158 ret = env_read_common(i, map);
166 int envs_validate_common(const struct env_map_common *map)
170 for (u32 i = 0; map[i].env_name; i++) {
171 ret = env_validate_common(i, map);
179 int envs_read_validate_common(const struct env_map_common *map)
183 envs_cleanup_common(map);
185 ret = envs_read_common(map);
189 ret = envs_validate_common(map);
196 int envs_read_validate_core(const struct env_map_percpu *map,
197 bool (*cpu_used)(u32))
201 envs_cleanup_core(map);
203 for (u32 i = 0; map[i].env_name; i++) {
204 ret = env_read_core(i, map);
209 for (u32 i = 0; map[i].env_name; i++) {
210 ret = env_validate_core(i, map, cpu_used);
218 int envs_process_and_validate(const struct env_map_common *common,
219 const struct env_map_percpu *core,
220 bool (*cpu_used)(u32))
224 ret = envs_read_validate_common(common);
228 ret = envs_read_validate_core(core, cpu_used);
235 static int args_envs_read_search(const struct env_map_common *map,
236 int argc, char *const argv[])
238 for (int i = 0; map[i].env_name; i++) {
239 if (!strcmp(argv[0], map[i].env_name))
243 pr_err("Unexpected argument '%s', can't parse\n", argv[0]);
248 static int arg_read_set(const struct env_map_common *map, u32 i, int argc,
251 char *endp = argv[1];
253 if (map[i].type == ENV_HEX)
254 map[i].val->val = simple_strtoul(argv[1], &endp, 16);
256 map[i].val->val = simple_strtoul(argv[1], &endp, 10);
258 map[i].val->set = true;
263 pr_err("Unexpected argument '%s', can't parse\n", argv[1]);
265 map[i].val->set = false;
270 int args_envs_enumerate(const struct env_map_common *map, int enum_by,
271 int argc, char *const argv[])
275 if (argc % enum_by) {
276 pr_err("unexpected argument number: %d\n", argc);
281 i = args_envs_read_search(map, argc, argv);
285 debug("ARG: found '%s' with index %d\n", map[i].env_name, i);
288 pr_err("unknown arg: %s\n", argv[0]);
292 if (arg_read_set(map, i, argc, argv))
295 debug("ARG: value.s '%s' == %#x\n", argv[1], map[i].val->val);