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 #define MAX_CMD_LEN 25
13 static void env_clear_common(u32 index, const struct env_map_common *map)
15 map[index].val->val = 0;
16 map[index].val->set = false;
19 static int env_read_common(u32 index, const struct env_map_common *map)
23 if (!env_get_yesno(map[index].env_name)) {
24 if (map[index].type == ENV_HEX) {
25 val = (u32)env_get_hex(map[index].env_name, 0);
26 debug("ENV: %s: = %#x\n", map[index].env_name, val);
28 val = (u32)env_get_ulong(map[index].env_name, 10, 0);
29 debug("ENV: %s: = %d\n", map[index].env_name, val);
32 map[index].val->val = val;
33 map[index].val->set = true;
39 static void env_clear_core(u32 index, const struct env_map_percpu *map)
41 for (u32 i = 0; i < NR_CPUS; i++) {
42 (*map[index].val)[i].val = 0;
43 (*map[index].val)[i].set = false;
47 static int env_read_core(u32 index, const struct env_map_percpu *map)
50 char command[MAX_CMD_LEN];
52 for (u32 i = 0; i < NR_CPUS; i++) {
53 sprintf(command, "%s_%u", map[index].env_name, i);
54 if (!env_get_yesno(command)) {
55 if (map[index].type == ENV_HEX) {
56 val = (u32)env_get_hex(command, 0);
57 debug("ENV: %s: = %#x\n", command, val);
59 val = (u32)env_get_ulong(command, 10, 0);
60 debug("ENV: %s: = %d\n", command, val);
63 (*map[index].val)[i].val = val;
64 (*map[index].val)[i].set = true;
71 static int env_validate_common(u32 index, const struct env_map_common *map)
73 u32 value = map[index].val->val;
74 bool set = map[index].val->set;
75 u32 min = map[index].min;
76 u32 max = map[index].max;
78 /* Check if environment is mandatory */
79 if (map[index].mandatory && !set) {
80 pr_err("Variable \'%s\' is mandatory, but it is not defined\n",
86 /* Check environment boundary */
87 if (set && (value < min || value > max)) {
88 if (map[index].type == ENV_HEX)
89 pr_err("Variable \'%s\' must be between %#x and %#x\n",
90 map[index].env_name, min, max);
92 pr_err("Variable \'%s\' must be between %u and %u\n",
93 map[index].env_name, min, max);
101 static int env_validate_core(u32 index, const struct env_map_percpu *map,
102 bool (*cpu_used)(u32))
106 bool mandatory = map[index].mandatory;
109 for (u32 i = 0; i < NR_CPUS; i++) {
110 set = (*map[index].val)[i].set;
111 value = (*map[index].val)[i].val;
113 /* Check if environment is mandatory */
114 if (cpu_used(i) && mandatory && !set) {
115 pr_err("CPU %u is used, but \'%s_%u\' is not defined\n",
116 i, map[index].env_name, i);
121 min = map[index].min[i];
122 max = map[index].max[i];
124 /* Check environment boundary */
125 if (set && (value < min || value > max)) {
126 if (map[index].type == ENV_HEX)
127 pr_err("Variable \'%s_%u\' must be between %#x and %#x\n",
128 map[index].env_name, i, min, max);
130 pr_err("Variable \'%s_%u\' must be between %d and %d\n",
131 map[index].env_name, i, min, max);
140 void envs_cleanup_core(const struct env_map_percpu *map)
142 /* Cleanup env struct first */
143 for (u32 i = 0; map[i].env_name; i++)
144 env_clear_core(i, map);
147 void envs_cleanup_common(const struct env_map_common *map)
149 /* Cleanup env struct first */
150 for (u32 i = 0; map[i].env_name; i++)
151 env_clear_common(i, map);
154 int envs_read_common(const struct env_map_common *map)
158 for (u32 i = 0; map[i].env_name; i++) {
159 ret = env_read_common(i, map);
167 int envs_validate_common(const struct env_map_common *map)
171 for (u32 i = 0; map[i].env_name; i++) {
172 ret = env_validate_common(i, map);
180 int envs_read_validate_common(const struct env_map_common *map)
184 envs_cleanup_common(map);
186 ret = envs_read_common(map);
190 ret = envs_validate_common(map);
197 int envs_read_validate_core(const struct env_map_percpu *map,
198 bool (*cpu_used)(u32))
202 envs_cleanup_core(map);
204 for (u32 i = 0; map[i].env_name; i++) {
205 ret = env_read_core(i, map);
210 for (u32 i = 0; map[i].env_name; i++) {
211 ret = env_validate_core(i, map, cpu_used);
219 int envs_process_and_validate(const struct env_map_common *common,
220 const struct env_map_percpu *core,
221 bool (*cpu_used)(u32))
225 ret = envs_read_validate_common(common);
229 ret = envs_read_validate_core(core, cpu_used);
236 static int args_envs_read_search(const struct env_map_common *map,
237 int argc, char *const argv[])
239 for (int i = 0; map[i].env_name; i++) {
240 if (!strcmp(argv[0], map[i].env_name))
244 pr_err("Unexpected argument '%s', can't parse\n", argv[0]);
249 static int arg_read_set(const struct env_map_common *map, u32 i, int argc,
252 char *endp = argv[1];
254 if (map[i].type == ENV_HEX)
255 map[i].val->val = simple_strtoul(argv[1], &endp, 16);
257 map[i].val->val = simple_strtoul(argv[1], &endp, 10);
259 map[i].val->set = true;
264 pr_err("Unexpected argument '%s', can't parse\n", argv[1]);
266 map[i].val->set = false;
271 int args_envs_enumerate(const struct env_map_common *map, int enum_by,
272 int argc, char *const argv[])
276 if (argc % enum_by) {
277 pr_err("unexpected argument number: %d\n", argc);
282 i = args_envs_read_search(map, argc, argv);
286 debug("ARG: found '%s' with index %d\n", map[i].env_name, i);
289 pr_err("unknown arg: %s\n", argv[0]);
293 if (arg_read_set(map, i, argc, argv))
296 debug("ARG: value.s '%s' == %#x\n", argv[1], map[i].val->val);