Merge tag 'tpm-master-28012025' of https://source.denx.de/u-boot/custodians/u-boot-tpm
[u-boot.git] / cmd / cedit.c
blobf696356419e49b43675f289f0d084df4647184ab
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * 'cedit' command
5 * Copyright 2023 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
9 #include <abuf.h>
10 #include <cedit.h>
11 #include <command.h>
12 #include <dm.h>
13 #include <expo.h>
14 #include <fs.h>
15 #include <malloc.h>
16 #include <mapmem.h>
17 #include <dm/ofnode.h>
18 #include <linux/sizes.h>
20 struct expo *cur_exp;
22 static int check_cur_expo(void)
24 if (!cur_exp) {
25 printf("No expo loaded\n");
26 return -ENOENT;
29 return 0;
32 static int do_cedit_load(struct cmd_tbl *cmdtp, int flag, int argc,
33 char *const argv[])
35 const char *fname;
36 struct expo *exp;
37 oftree tree;
38 ulong size;
39 void *buf;
40 int ret;
42 if (argc < 4)
43 return CMD_RET_USAGE;
44 fname = argv[3];
46 ret = fs_load_alloc(argv[1], argv[2], argv[3], SZ_1M, 0, &buf, &size);
47 if (ret) {
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);
59 oftree_dispose(tree);
60 if (ret) {
61 printf("Failed to build expo: %dE\n", ret);
62 return CMD_RET_FAILURE;
65 cur_exp = exp;
67 return 0;
70 #ifdef CONFIG_COREBOOT_SYSINFO
71 static int do_cedit_cb_load(struct cmd_tbl *cmdtp, int flag, int argc,
72 char *const argv[])
74 struct expo *exp;
75 int ret;
77 if (argc > 1)
78 return CMD_RET_USAGE;
80 ret = cb_expo_build(&exp);
81 if (ret) {
82 printf("Failed to build expo: %dE\n", ret);
83 return CMD_RET_FAILURE;
86 cur_exp = exp;
88 return 0;
90 #endif /* CONFIG_COREBOOT_SYSINFO */
92 static int do_cedit_write_fdt(struct cmd_tbl *cmdtp, int flag, int argc,
93 char *const argv[])
95 const char *fname;
96 struct abuf buf;
97 loff_t bytes;
98 int ret;
100 if (argc < 4)
101 return CMD_RET_USAGE;
102 fname = argv[3];
104 if (check_cur_expo())
105 return CMD_RET_FAILURE;
107 ret = cedit_write_settings(cur_exp, &buf);
108 if (ret) {
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);
118 if (ret)
119 return CMD_RET_FAILURE;
121 return 0;
124 static int do_cedit_read_fdt(struct cmd_tbl *cmdtp, int flag, int argc,
125 char *const argv[])
127 const char *fname;
128 void *buf;
129 oftree tree;
130 ulong size;
131 int ret;
133 if (argc < 4)
134 return CMD_RET_USAGE;
135 fname = argv[3];
137 ret = fs_load_alloc(argv[1], argv[2], argv[3], SZ_1M, 0, &buf, &size);
138 if (ret) {
139 printf("File not found\n");
140 return CMD_RET_FAILURE;
143 tree = oftree_from_fdt(buf);
144 if (!oftree_valid(tree)) {
145 free(buf);
146 printf("Cannot create oftree\n");
147 return CMD_RET_FAILURE;
150 ret = cedit_read_settings(cur_exp, tree);
151 oftree_dispose(tree);
152 free(buf);
153 if (ret) {
154 printf("Failed to read settings: %dE\n", ret);
155 return CMD_RET_FAILURE;
158 return 0;
161 static int do_cedit_write_env(struct cmd_tbl *cmdtp, int flag, int argc,
162 char *const argv[])
164 bool verbose;
165 int ret;
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);
173 if (ret) {
174 printf("Failed to write settings to environment: %dE\n", ret);
175 return CMD_RET_FAILURE;
178 return 0;
181 static int do_cedit_read_env(struct cmd_tbl *cmdtp, int flag, int argc,
182 char *const argv[])
184 bool verbose;
185 int ret;
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);
193 if (ret) {
194 printf("Failed to read settings from environment: %dE\n", ret);
195 return CMD_RET_FAILURE;
198 return 0;
201 static int do_cedit_write_cmos(struct cmd_tbl *cmdtp, int flag, int argc,
202 char *const argv[])
204 struct udevice *dev;
205 bool verbose = false;
206 int ret;
208 if (check_cur_expo())
209 return CMD_RET_FAILURE;
211 if (argc > 1 && !strcmp(argv[1], "-v")) {
212 verbose = true;
213 argc--;
214 argv++;
217 if (argc > 1)
218 ret = uclass_get_device_by_name(UCLASS_RTC, argv[1], &dev);
219 else
220 ret = uclass_first_device_err(UCLASS_RTC, &dev);
221 if (ret) {
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;
231 return 0;
234 static int do_cedit_read_cmos(struct cmd_tbl *cmdtp, int flag, int argc,
235 char *const argv[])
237 struct udevice *dev;
238 bool verbose = false;
239 int ret;
241 if (check_cur_expo())
242 return CMD_RET_FAILURE;
244 if (argc > 1 && !strcmp(argv[1], "-v")) {
245 verbose = true;
246 argc--;
247 argv++;
250 if (argc > 1)
251 ret = uclass_get_device_by_name(UCLASS_RTC, argv[1], &dev);
252 else
253 ret = uclass_first_device_err(UCLASS_RTC, &dev);
254 if (ret) {
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);
260 if (ret) {
261 printf("Failed to read settings from CMOS: %dE\n", ret);
262 return CMD_RET_FAILURE;
265 return 0;
268 static int do_cedit_run(struct cmd_tbl *cmdtp, int flag, int argc,
269 char *const argv[])
271 ofnode node;
272 int ret;
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);
280 if (ret)
281 return CMD_RET_FAILURE;
282 } else {
283 log_warning("No theme found\n");
285 ret = cedit_run(cur_exp);
286 if (ret) {
287 log_err("Failed (err=%dE)\n", ret);
288 return CMD_RET_FAILURE;
291 return 0;
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"
298 #endif
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),
311 #endif
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),