summaryrefslogtreecommitdiff
path: root/cmd/cedit.c
blob: fec67a8e334980b3aa7b8e6253cbab5f8a994faa (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// SPDX-License-Identifier: GPL-2.0+
/*
 * 'cedit' command
 *
 * Copyright 2023 Google LLC
 * Written by Simon Glass <sjg@chromium.org>
 */

#include <abuf.h>
#include <cedit.h>
#include <command.h>
#include <dm.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_read_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_read_settings_env(cur_exp, verbose);
	if (ret) {
		printf("Failed to read settings from environment: %dE\n", ret);
		return CMD_RET_FAILURE;
	}

	return 0;
}

static int do_cedit_write_cmos(struct cmd_tbl *cmdtp, int flag, int argc,
			       char *const argv[])
{
	struct udevice *dev;
	bool verbose = false;
	int ret;

	if (check_cur_expo())
		return CMD_RET_FAILURE;

	if (argc > 1 && !strcmp(argv[1], "-v")) {
		verbose = true;
		argc--;
		argv++;
	}

	if (argc > 1)
		ret = uclass_get_device_by_name(UCLASS_RTC, argv[1], &dev);
	else
		ret = uclass_first_device_err(UCLASS_RTC, &dev);
	if (ret) {
		printf("Failed to get RTC device: %dE\n", ret);
		return CMD_RET_FAILURE;
	}

	if (cedit_write_settings_cmos(cur_exp, dev, verbose)) {
		printf("Failed to write settings to CMOS\n");
		return CMD_RET_FAILURE;
	}

	return 0;
}

static int do_cedit_read_cmos(struct cmd_tbl *cmdtp, int flag, int argc,
			      char *const argv[])
{
	struct udevice *dev;
	bool verbose = false;
	int ret;

	if (check_cur_expo())
		return CMD_RET_FAILURE;

	if (argc > 1 && !strcmp(argv[1], "-v")) {
		verbose = true;
		argc--;
		argv++;
	}

	if (argc > 1)
		ret = uclass_get_device_by_name(UCLASS_RTC, argv[1], &dev);
	else
		ret = uclass_first_device_err(UCLASS_RTC, &dev);
	if (ret) {
		printf("Failed to get RTC device: %dE\n", ret);
		return CMD_RET_FAILURE;
	}

	ret = cedit_read_settings_cmos(cur_exp, dev, verbose);
	if (ret) {
		printf("Failed to read settings from CMOS: %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;
}

U_BOOT_LONGHELP(cedit,
	"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 read_env [-v]                              - read settings from env vars\n"
	"cedit write_env [-v]                             - write settings to env vars\n"
	"cedit read_cmos [-v] [dev]                       - read settings from CMOS RAM\n"
	"cedit write_cmos [-v] [dev]                      - write settings to CMOS RAM\n"
	"cedit run                                        - run config editor");

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(read_env, 2, 1, do_cedit_read_env),
	U_BOOT_SUBCMD_MKENT(write_env, 2, 1, do_cedit_write_env),
	U_BOOT_SUBCMD_MKENT(read_cmos, 2, 1, do_cedit_read_cmos),
	U_BOOT_SUBCMD_MKENT(write_cmos, 2, 1, do_cedit_write_cmos),
	U_BOOT_SUBCMD_MKENT(run, 1, 1, do_cedit_run),
);