1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright 2023 Google LLC
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 static int do_cedit_write_fdt(struct cmd_tbl *cmdtp, int flag, int argc,
83 return CMD_RET_FAILURE;
85 ret = cedit_write_settings(cur_exp, &buf);
87 printf("Failed to write settings: %dE\n", ret);
88 return CMD_RET_FAILURE;
91 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
92 return CMD_RET_FAILURE;
94 ret = fs_write(fname, map_to_sysmem(abuf_data(&buf)), 0,
95 abuf_size(&buf), &bytes);
97 return CMD_RET_FAILURE;
102 static int do_cedit_read_fdt(struct cmd_tbl *cmdtp, int flag, int argc,
112 return CMD_RET_USAGE;
115 ret = fs_load_alloc(argv[1], argv[2], argv[3], SZ_1M, 0, &buf, &size);
117 printf("File not found\n");
118 return CMD_RET_FAILURE;
121 tree = oftree_from_fdt(buf);
122 if (!oftree_valid(tree)) {
124 printf("Cannot create oftree\n");
125 return CMD_RET_FAILURE;
128 ret = cedit_read_settings(cur_exp, tree);
129 oftree_dispose(tree);
132 printf("Failed to read settings: %dE\n", ret);
133 return CMD_RET_FAILURE;
139 static int do_cedit_write_env(struct cmd_tbl *cmdtp, int flag, int argc,
145 if (check_cur_expo())
146 return CMD_RET_FAILURE;
148 verbose = argc > 1 && !strcmp(argv[1], "-v");
150 ret = cedit_write_settings_env(cur_exp, verbose);
152 printf("Failed to write settings to environment: %dE\n", ret);
153 return CMD_RET_FAILURE;
159 static int do_cedit_read_env(struct cmd_tbl *cmdtp, int flag, int argc,
165 if (check_cur_expo())
166 return CMD_RET_FAILURE;
168 verbose = argc > 1 && !strcmp(argv[1], "-v");
170 ret = cedit_read_settings_env(cur_exp, verbose);
172 printf("Failed to read settings from environment: %dE\n", ret);
173 return CMD_RET_FAILURE;
179 static int do_cedit_write_cmos(struct cmd_tbl *cmdtp, int flag, int argc,
183 bool verbose = false;
186 if (check_cur_expo())
187 return CMD_RET_FAILURE;
189 if (argc > 1 && !strcmp(argv[1], "-v")) {
196 ret = uclass_get_device_by_name(UCLASS_RTC, argv[1], &dev);
198 ret = uclass_first_device_err(UCLASS_RTC, &dev);
200 printf("Failed to get RTC device: %dE\n", ret);
201 return CMD_RET_FAILURE;
204 if (cedit_write_settings_cmos(cur_exp, dev, verbose)) {
205 printf("Failed to write settings to CMOS\n");
206 return CMD_RET_FAILURE;
212 static int do_cedit_read_cmos(struct cmd_tbl *cmdtp, int flag, int argc,
216 bool verbose = false;
219 if (check_cur_expo())
220 return CMD_RET_FAILURE;
222 if (argc > 1 && !strcmp(argv[1], "-v")) {
229 ret = uclass_get_device_by_name(UCLASS_RTC, argv[1], &dev);
231 ret = uclass_first_device_err(UCLASS_RTC, &dev);
233 printf("Failed to get RTC device: %dE\n", ret);
234 return CMD_RET_FAILURE;
237 ret = cedit_read_settings_cmos(cur_exp, dev, verbose);
239 printf("Failed to read settings from CMOS: %dE\n", ret);
240 return CMD_RET_FAILURE;
246 static int do_cedit_run(struct cmd_tbl *cmdtp, int flag, int argc,
252 if (check_cur_expo())
253 return CMD_RET_FAILURE;
255 node = ofnode_path("/bootstd/cedit-theme");
256 if (ofnode_valid(node)) {
257 ret = expo_apply_theme(cur_exp, node);
259 return CMD_RET_FAILURE;
261 log_warning("No theme found\n");
263 ret = cedit_run(cur_exp);
265 log_err("Failed (err=%dE)\n", ret);
266 return CMD_RET_FAILURE;
272 U_BOOT_LONGHELP(cedit,
273 "load <interface> <dev[:part]> <filename> - load config editor\n"
274 "cedit read_fdt <i/f> <dev[:part]> <filename> - read settings\n"
275 "cedit write_fdt <i/f> <dev[:part]> <filename> - write settings\n"
276 "cedit read_env [-v] - read settings from env vars\n"
277 "cedit write_env [-v] - write settings to env vars\n"
278 "cedit read_cmos [-v] [dev] - read settings from CMOS RAM\n"
279 "cedit write_cmos [-v] [dev] - write settings to CMOS RAM\n"
280 "cedit run - run config editor");
282 U_BOOT_CMD_WITH_SUBCMDS(cedit, "Configuration editor", cedit_help_text,
283 U_BOOT_SUBCMD_MKENT(load, 5, 1, do_cedit_load),
284 U_BOOT_SUBCMD_MKENT(read_fdt, 5, 1, do_cedit_read_fdt),
285 U_BOOT_SUBCMD_MKENT(write_fdt, 5, 1, do_cedit_write_fdt),
286 U_BOOT_SUBCMD_MKENT(read_env, 2, 1, do_cedit_read_env),
287 U_BOOT_SUBCMD_MKENT(write_env, 2, 1, do_cedit_write_env),
288 U_BOOT_SUBCMD_MKENT(read_cmos, 2, 1, do_cedit_read_cmos),
289 U_BOOT_SUBCMD_MKENT(write_cmos, 2, 1, do_cedit_write_cmos),
290 U_BOOT_SUBCMD_MKENT(run, 1, 1, do_cedit_run),