1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright 2023 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
17 #include <dm/ofnode.h>
18 #include <linux/sizes.h>
22 static int check_cur_expo(void)
25 printf("No expo loaded\n");
32 static int do_cedit_load(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
46 ret
= fs_load_alloc(argv
[1], argv
[2], argv
[3], SZ_1M
, 0, &buf
, &size
);
48 printf("File not found\n");
49 return CMD_RET_FAILURE
;
52 tree
= oftree_from_fdt(buf
);
53 if (!oftree_valid(tree
)) {
54 printf("Cannot create oftree\n");
55 return CMD_RET_FAILURE
;
58 ret
= expo_build(oftree_root(tree
), &exp
);
61 printf("Failed to build expo: %dE\n", ret
);
62 return CMD_RET_FAILURE
;
70 #ifdef CONFIG_COREBOOT_SYSINFO
71 static int do_cedit_cb_load(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
80 ret
= cb_expo_build(&exp
);
82 printf("Failed to build expo: %dE\n", ret
);
83 return CMD_RET_FAILURE
;
90 #endif /* CONFIG_COREBOOT_SYSINFO */
92 static int do_cedit_write_fdt(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
101 return CMD_RET_USAGE
;
104 if (check_cur_expo())
105 return CMD_RET_FAILURE
;
107 ret
= cedit_write_settings(cur_exp
, &buf
);
109 printf("Failed to write settings: %dE\n", ret
);
110 return CMD_RET_FAILURE
;
113 if (fs_set_blk_dev(argv
[1], argv
[2], FS_TYPE_ANY
))
114 return CMD_RET_FAILURE
;
116 ret
= fs_write(fname
, map_to_sysmem(abuf_data(&buf
)), 0,
117 abuf_size(&buf
), &bytes
);
119 return CMD_RET_FAILURE
;
124 static int do_cedit_read_fdt(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
134 return CMD_RET_USAGE
;
137 ret
= fs_load_alloc(argv
[1], argv
[2], argv
[3], SZ_1M
, 0, &buf
, &size
);
139 printf("File not found\n");
140 return CMD_RET_FAILURE
;
143 tree
= oftree_from_fdt(buf
);
144 if (!oftree_valid(tree
)) {
146 printf("Cannot create oftree\n");
147 return CMD_RET_FAILURE
;
150 ret
= cedit_read_settings(cur_exp
, tree
);
151 oftree_dispose(tree
);
154 printf("Failed to read settings: %dE\n", ret
);
155 return CMD_RET_FAILURE
;
161 static int do_cedit_write_env(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
167 if (check_cur_expo())
168 return CMD_RET_FAILURE
;
170 verbose
= argc
> 1 && !strcmp(argv
[1], "-v");
172 ret
= cedit_write_settings_env(cur_exp
, verbose
);
174 printf("Failed to write settings to environment: %dE\n", ret
);
175 return CMD_RET_FAILURE
;
181 static int do_cedit_read_env(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
187 if (check_cur_expo())
188 return CMD_RET_FAILURE
;
190 verbose
= argc
> 1 && !strcmp(argv
[1], "-v");
192 ret
= cedit_read_settings_env(cur_exp
, verbose
);
194 printf("Failed to read settings from environment: %dE\n", ret
);
195 return CMD_RET_FAILURE
;
201 static int do_cedit_write_cmos(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
205 bool verbose
= false;
208 if (check_cur_expo())
209 return CMD_RET_FAILURE
;
211 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
218 ret
= uclass_get_device_by_name(UCLASS_RTC
, argv
[1], &dev
);
220 ret
= uclass_first_device_err(UCLASS_RTC
, &dev
);
222 printf("Failed to get RTC device: %dE\n", ret
);
223 return CMD_RET_FAILURE
;
226 if (cedit_write_settings_cmos(cur_exp
, dev
, verbose
)) {
227 printf("Failed to write settings to CMOS\n");
228 return CMD_RET_FAILURE
;
234 static int do_cedit_read_cmos(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
238 bool verbose
= false;
241 if (check_cur_expo())
242 return CMD_RET_FAILURE
;
244 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
251 ret
= uclass_get_device_by_name(UCLASS_RTC
, argv
[1], &dev
);
253 ret
= uclass_first_device_err(UCLASS_RTC
, &dev
);
255 printf("Failed to get RTC device: %dE\n", ret
);
256 return CMD_RET_FAILURE
;
259 ret
= cedit_read_settings_cmos(cur_exp
, dev
, verbose
);
261 printf("Failed to read settings from CMOS: %dE\n", ret
);
262 return CMD_RET_FAILURE
;
268 static int do_cedit_run(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
274 if (check_cur_expo())
275 return CMD_RET_FAILURE
;
277 node
= ofnode_path("/bootstd/cedit-theme");
278 if (ofnode_valid(node
)) {
279 ret
= expo_apply_theme(cur_exp
, node
);
281 return CMD_RET_FAILURE
;
283 log_warning("No theme found\n");
285 ret
= cedit_run(cur_exp
);
287 log_err("Failed (err=%dE)\n", ret
);
288 return CMD_RET_FAILURE
;
294 U_BOOT_LONGHELP(cedit
,
295 "load <interface> <dev[:part]> <filename> - load config editor\n"
296 #ifdef CONFIG_COREBOOT_SYSINFO
297 "cb_load - load coreboot CMOS editor\n"
299 "cedit read_fdt <i/f> <dev[:part]> <filename> - read settings\n"
300 "cedit write_fdt <i/f> <dev[:part]> <filename> - write settings\n"
301 "cedit read_env [-v] - read settings from env vars\n"
302 "cedit write_env [-v] - write settings to env vars\n"
303 "cedit read_cmos [-v] [dev] - read settings from CMOS RAM\n"
304 "cedit write_cmos [-v] [dev] - write settings to CMOS RAM\n"
305 "cedit run - run config editor");
307 U_BOOT_CMD_WITH_SUBCMDS(cedit
, "Configuration editor", cedit_help_text
,
308 U_BOOT_SUBCMD_MKENT(load
, 5, 1, do_cedit_load
),
309 #ifdef CONFIG_COREBOOT_SYSINFO
310 U_BOOT_SUBCMD_MKENT(cb_load
, 5, 1, do_cedit_cb_load
),
312 U_BOOT_SUBCMD_MKENT(read_fdt
, 5, 1, do_cedit_read_fdt
),
313 U_BOOT_SUBCMD_MKENT(write_fdt
, 5, 1, do_cedit_write_fdt
),
314 U_BOOT_SUBCMD_MKENT(read_env
, 2, 1, do_cedit_read_env
),
315 U_BOOT_SUBCMD_MKENT(write_env
, 2, 1, do_cedit_write_env
),
316 U_BOOT_SUBCMD_MKENT(read_cmos
, 2, 1, do_cedit_read_cmos
),
317 U_BOOT_SUBCMD_MKENT(write_cmos
, 2, 1, do_cedit_write_cmos
),
318 U_BOOT_SUBCMD_MKENT(run
, 1, 1, do_cedit_run
),