1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
// SPDX-License-Identifier: GPL-2.0+
/*
* 'cedit' command
*
* Copyright 2023 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#include <common.h>
#include <abuf.h>
#include <cedit.h>
#include <command.h>
#include <expo.h>
#include <fs.h>
#include <malloc.h>
#include <mapmem.h>
#include <dm/ofnode.h>
#include <linux/sizes.h>
struct expo *cur_exp;
static int check_cur_expo(void)
{
if (!cur_exp) {
printf("No expo loaded\n");
return -ENOENT;
}
return 0;
}
static int do_cedit_load(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
const char *fname;
struct expo *exp;
oftree tree;
ulong size;
void *buf;
int ret;
if (argc < 4)
return CMD_RET_USAGE;
fname = argv[3];
ret = fs_load_alloc(argv[1], argv[2], argv[3], SZ_1M, 0, &buf, &size);
if (ret) {
printf("File not found\n");
return CMD_RET_FAILURE;
}
tree = oftree_from_fdt(buf);
if (!oftree_valid(tree)) {
printf("Cannot create oftree\n");
return CMD_RET_FAILURE;
}
ret = expo_build(oftree_root(tree), &exp);
oftree_dispose(tree);
if (ret) {
printf("Failed to build expo: %dE\n", ret);
return CMD_RET_FAILURE;
}
cur_exp = exp;
return 0;
}
static int do_cedit_write_fdt(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
const char *fname;
struct abuf buf;
loff_t bytes;
int ret;
if (argc < 4)
return CMD_RET_USAGE;
fname = argv[3];
if (check_cur_expo())
return CMD_RET_FAILURE;
ret = cedit_write_settings(cur_exp, &buf);
if (ret) {
printf("Failed to write settings: %dE\n", ret);
return CMD_RET_FAILURE;
}
if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
return CMD_RET_FAILURE;
ret = fs_write(fname, map_to_sysmem(abuf_data(&buf)), 0,
abuf_size(&buf), &bytes);
if (ret)
return CMD_RET_FAILURE;
return 0;
}
static int do_cedit_read_fdt(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
const char *fname;
void *buf;
oftree tree;
ulong size;
int ret;
if (argc < 4)
return CMD_RET_USAGE;
fname = argv[3];
ret = fs_load_alloc(argv[1], argv[2], argv[3], SZ_1M, 0, &buf, &size);
if (ret) {
printf("File not found\n");
return CMD_RET_FAILURE;
}
tree = oftree_from_fdt(buf);
if (!oftree_valid(tree)) {
free(buf);
printf("Cannot create oftree\n");
return CMD_RET_FAILURE;
}
ret = cedit_read_settings(cur_exp, tree);
oftree_dispose(tree);
free(buf);
if (ret) {
printf("Failed to read settings: %dE\n", ret);
return CMD_RET_FAILURE;
}
return 0;
}
static int do_cedit_write_env(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
bool verbose;
int ret;
if (check_cur_expo())
return CMD_RET_FAILURE;
verbose = argc > 1 && !strcmp(argv[1], "-v");
ret = cedit_write_settings_env(cur_exp, verbose);
if (ret) {
printf("Failed to write settings to environment: %dE\n", ret);
return CMD_RET_FAILURE;
}
return 0;
}
static int do_cedit_run(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
ofnode node;
int ret;
if (check_cur_expo())
return CMD_RET_FAILURE;
node = ofnode_path("/bootstd/cedit-theme");
if (ofnode_valid(node)) {
ret = expo_apply_theme(cur_exp, node);
if (ret)
return CMD_RET_FAILURE;
} else {
log_warning("No theme found\n");
}
ret = cedit_run(cur_exp);
if (ret) {
log_err("Failed (err=%dE)\n", ret);
return CMD_RET_FAILURE;
}
return 0;
}
#ifdef CONFIG_SYS_LONGHELP
static char cedit_help_text[] =
"load <interface> <dev[:part]> <filename> - load config editor\n"
"cedit read_fdt <i/f> <dev[:part]> <filename> - read settings\n"
"cedit write_fdt <i/f> <dev[:part]> <filename> - write settings\n"
"cedit write_env [-v] - write settings to env vars\n"
"cedit run - run config editor";
#endif /* CONFIG_SYS_LONGHELP */
U_BOOT_CMD_WITH_SUBCMDS(cedit, "Configuration editor", cedit_help_text,
U_BOOT_SUBCMD_MKENT(load, 5, 1, do_cedit_load),
U_BOOT_SUBCMD_MKENT(read_fdt, 5, 1, do_cedit_read_fdt),
U_BOOT_SUBCMD_MKENT(write_fdt, 5, 1, do_cedit_write_fdt),
U_BOOT_SUBCMD_MKENT(write_env, 2, 1, do_cedit_write_env),
U_BOOT_SUBCMD_MKENT(run, 1, 1, do_cedit_run),
);
|