1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Synopsys, Inc. All rights reserved.
4 * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
11 static void env_clear_common(u32 index, const struct env_map_common *map)
13 map[index].val->val = 0;
14 map[index].val->set = false;
17 static int env_read_common(u32 index, const struct env_map_common *map)
21 if (!env_get_yesno(map[index].env_name)) {
22 if (map[index].type == ENV_HEX) {
23 val = (u32)env_get_hex(map[index].env_name, 0);
24 debug("ENV: %s: = %#x\n", map[index].env_name, val);
26 val = (u32)env_get_ulong(map[index].env_name, 10, 0);
27 debug("ENV: %s: = %d\n", map[index].env_name, val);
30 map[index].val->val = val;
31 map[index].val->set = true;
37 static void env_clear_core(u32 index, const struct env_map_percpu *map)
39 for (u32 i = 0; i < NR_CPUS; i++) {
40 (*map[index].val)[i].val = 0;
41 (*map[index].val)[i].set = false;
45 static int env_read_core(u32 index, const struct env_map_percpu *map)
48 char command[MAX_CMD_LEN];
50 for (u32 i = 0; i < NR_CPUS; i++) {
51 sprintf(command, "%s_%u", map[index].env_name, i);
52 if (!env_get_yesno(command)) {
53 if (map[index].type == ENV_HEX) {
54 val = (u32)env_get_hex(command, 0);
55 debug("ENV: %s: = %#x\n", command, val);
57 val = (u32)env_get_ulong(command, 10, 0);
58 debug("ENV: %s: = %d\n", command, val);
61 (*map[index].val)[i].val = val;
62 (*map[index].val)[i].set = true;
69 static int env_validate_common(u32 index, const struct env_map_common *map)
71 u32 value = map[index].val->val;
72 bool set = map[index].val->set;
73 u32 min = map[index].min;
74 u32 max = map[index].max;
76 /* Check if environment is mandatory */
77 if (map[index].mandatory && !set) {
78 pr_err("Variable \'%s\' is mandatory, but it is not defined\n",
84 /* Check environment boundary */
85 if (set && (value < min || value > max)) {
86 if (map[index].type == ENV_HEX)
87 pr_err("Variable \'%s\' must be between %#x and %#x\n",
88 map[index].env_name, min, max);
90 pr_err("Variable \'%s\' must be between %u and %u\n",
91 map[index].env_name, min, max);
99 static int env_validate_core(u32 index, const struct env_map_percpu *map,
100 bool (*cpu_used)(u32))
104 bool mandatory = map[index].mandatory;
107 for (u32 i = 0; i < NR_CPUS; i++) {
108 set = (*map[index].val)[i].set;
109 value = (*map[index].val)[i].val;
111 /* Check if environment is mandatory */
112 if (cpu_used(i) && mandatory && !set) {
113 pr_err("CPU %u is used, but \'%s_%u\' is not defined\n",
114 i, map[index].env_name, i);
119 min = map[index].min[i];
120 max = map[index].max[i];
122 /* Check environment boundary */
123 if (set && (value < min || value > max)) {
124 if (map[index].type == ENV_HEX)
125 pr_err("Variable \'%s_%u\' must be between %#x and %#x\n",
126 map[index].env_name, i, min, max);
128 pr_err("Variable \'%s_%u\' must be between %d and %d\n",
129 map[index].env_name, i, min, max);
138 void envs_cleanup_core(const struct env_map_percpu *map)
140 /* Cleanup env struct first */
141 for (u32 i = 0; map[i].env_name; i++)
142 env_clear_core(i, map);
145 void envs_cleanup_common(const struct env_map_common *map)
147 /* Cleanup env struct first */
148 for (u32 i = 0; map[i].env_name; i++)
149 env_clear_common(i, map);
152 int envs_read_common(const struct env_map_common *map)
156 for (u32 i = 0; map[i].env_name; i++) {
157 ret = env_read_common(i, map);
165 int envs_validate_common(const struct env_map_common *map)
169 for (u32 i = 0; map[i].env_name; i++) {
170 ret = env_validate_common(i, map);
178 int envs_read_validate_common(const struct env_map_common *map)
182 envs_cleanup_common(map);
184 ret = envs_read_common(map);
188 ret = envs_validate_common(map);
195 int envs_read_validate_core(const struct env_map_percpu *map,
196 bool (*cpu_used)(u32))
200 envs_cleanup_core(map);
202 for (u32 i = 0; map[i].env_name; i++) {
203 ret = env_read_core(i, map);
208 for (u32 i = 0; map[i].env_name; i++) {
209 ret = env_validate_core(i, map, cpu_used);
217 int envs_process_and_validate(const struct env_map_common *common,
218 const struct env_map_percpu *core,
219 bool (*cpu_used)(u32))
223 ret = envs_read_validate_common(common);
227 ret = envs_read_validate_core(core, cpu_used);
234 static int args_envs_read_search(const struct env_map_common *map,
235 int argc, char *const argv[])
237 for (int i = 0; map[i].env_name; i++) {
238 if (!strcmp(argv[0], map[i].env_name))
242 pr_err("Unexpected argument '%s', can't parse\n", argv[0]);
247 static int arg_read_set(const struct env_map_common *map, u32 i, int argc,
250 char *endp = argv[1];
252 if (map[i].type == ENV_HEX)
253 map[i].val->val = simple_strtoul(argv[1], &endp, 16);
255 map[i].val->val = simple_strtoul(argv[1], &endp, 10);
257 map[i].val->set = true;
262 pr_err("Unexpected argument '%s', can't parse\n", argv[1]);
264 map[i].val->set = false;
269 int args_envs_enumerate(const struct env_map_common *map, int enum_by,
270 int argc, char *const argv[])
274 if (argc % enum_by) {
275 pr_err("unexpected argument number: %d\n", argc);
280 i = args_envs_read_search(map, argc, argv);
284 debug("ARG: found '%s' with index %d\n", map[i].env_name, i);
287 pr_err("unknown arg: %s\n", argv[0]);
291 if (arg_read_set(map, i, argc, argv))
294 debug("ARG: value.s '%s' == %#x\n", argv[1], map[i].val->val);