blob: 18cc8ba191bf99815b6b4afd4d842a7d1c03358b [file] [log] [blame]
Simon Glassa0874dc2023-06-01 10:23:02 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * 'cedit' command
4 *
5 * Copyright 2023 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#include <common.h>
Simon Glass2dee81f2023-08-14 16:40:33 -060010#include <abuf.h>
Simon Glass040b0462023-08-14 16:40:25 -060011#include <cedit.h>
Simon Glassa0874dc2023-06-01 10:23:02 -060012#include <command.h>
13#include <expo.h>
14#include <fs.h>
Simon Glass2dee81f2023-08-14 16:40:33 -060015#include <malloc.h>
16#include <mapmem.h>
Simon Glassa0874dc2023-06-01 10:23:02 -060017#include <dm/ofnode.h>
18#include <linux/sizes.h>
19
20struct expo *cur_exp;
21
Simon Glass2dee81f2023-08-14 16:40:33 -060022static int check_cur_expo(void)
23{
24 if (!cur_exp) {
25 printf("No expo loaded\n");
26 return -ENOENT;
27 }
28
29 return 0;
30}
31
Simon Glassa0874dc2023-06-01 10:23:02 -060032static int do_cedit_load(struct cmd_tbl *cmdtp, int flag, int argc,
33 char *const argv[])
34{
35 const char *fname;
36 struct expo *exp;
37 oftree tree;
38 ulong size;
39 void *buf;
40 int ret;
41
42 if (argc < 4)
43 return CMD_RET_USAGE;
44 fname = argv[3];
45
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;
50 }
51
52 tree = oftree_from_fdt(buf);
53 if (!oftree_valid(tree)) {
54 printf("Cannot create oftree\n");
55 return CMD_RET_FAILURE;
56 }
57
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;
63 }
64
65 cur_exp = exp;
66
67 return 0;
68}
69
Simon Glass2dee81f2023-08-14 16:40:33 -060070static int do_cedit_write_fdt(struct cmd_tbl *cmdtp, int flag, int argc,
71 char *const argv[])
72{
73 const char *fname;
74 struct abuf buf;
75 loff_t bytes;
76 int ret;
77
78 if (argc < 4)
79 return CMD_RET_USAGE;
80 fname = argv[3];
81
82 if (check_cur_expo())
83 return CMD_RET_FAILURE;
84
85 ret = cedit_write_settings(cur_exp, &buf);
86 if (ret) {
87 printf("Failed to write settings: %dE\n", ret);
88 return CMD_RET_FAILURE;
89 }
90
91 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
92 return CMD_RET_FAILURE;
93
94 ret = fs_write(fname, map_to_sysmem(abuf_data(&buf)), 0,
95 abuf_size(&buf), &bytes);
96 if (ret)
97 return CMD_RET_FAILURE;
98
99 return 0;
100}
101
Simon Glassa0874dc2023-06-01 10:23:02 -0600102static int do_cedit_run(struct cmd_tbl *cmdtp, int flag, int argc,
103 char *const argv[])
104{
105 ofnode node;
106 int ret;
107
Simon Glass2dee81f2023-08-14 16:40:33 -0600108 if (check_cur_expo())
Simon Glassa0874dc2023-06-01 10:23:02 -0600109 return CMD_RET_FAILURE;
Simon Glassa0874dc2023-06-01 10:23:02 -0600110
Simon Glass2045ca52023-08-14 16:40:30 -0600111 node = ofnode_path("/bootstd/cedit-theme");
Simon Glassa0874dc2023-06-01 10:23:02 -0600112 if (ofnode_valid(node)) {
113 ret = expo_apply_theme(cur_exp, node);
114 if (ret)
115 return CMD_RET_FAILURE;
116 } else {
117 log_warning("No theme found\n");
118 }
119 ret = cedit_run(cur_exp);
120 if (ret) {
121 log_err("Failed (err=%dE)\n", ret);
122 return CMD_RET_FAILURE;
123 }
124
125 return 0;
126}
127
128#ifdef CONFIG_SYS_LONGHELP
129static char cedit_help_text[] =
130 "load <interface> <dev[:part]> <filename> - load config editor\n"
Simon Glass2dee81f2023-08-14 16:40:33 -0600131 "cedit write_fdt <i/f> <dev[:part]> <filename> - write settings\n"
Simon Glassa0874dc2023-06-01 10:23:02 -0600132 "cedit run - run config editor";
133#endif /* CONFIG_SYS_LONGHELP */
134
135U_BOOT_CMD_WITH_SUBCMDS(cedit, "Configuration editor", cedit_help_text,
136 U_BOOT_SUBCMD_MKENT(load, 5, 1, do_cedit_load),
Simon Glass2dee81f2023-08-14 16:40:33 -0600137 U_BOOT_SUBCMD_MKENT(write_fdt, 5, 1, do_cedit_write_fdt),
Simon Glassa0874dc2023-06-01 10:23:02 -0600138 U_BOOT_SUBCMD_MKENT(run, 1, 1, do_cedit_run),
139);