2023-06-01 16:23:02 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* 'cedit' command
|
|
|
|
*
|
|
|
|
* Copyright 2023 Google LLC
|
|
|
|
* Written by Simon Glass <sjg@chromium.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2023-08-14 22:40:33 +00:00
|
|
|
#include <abuf.h>
|
2023-08-14 22:40:25 +00:00
|
|
|
#include <cedit.h>
|
2023-06-01 16:23:02 +00:00
|
|
|
#include <command.h>
|
2023-08-14 22:40:37 +00:00
|
|
|
#include <dm.h>
|
2023-06-01 16:23:02 +00:00
|
|
|
#include <expo.h>
|
|
|
|
#include <fs.h>
|
2023-08-14 22:40:33 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <mapmem.h>
|
2023-06-01 16:23:02 +00:00
|
|
|
#include <dm/ofnode.h>
|
|
|
|
#include <linux/sizes.h>
|
|
|
|
|
|
|
|
struct expo *cur_exp;
|
|
|
|
|
2023-08-14 22:40:33 +00:00
|
|
|
static int check_cur_expo(void)
|
|
|
|
{
|
|
|
|
if (!cur_exp) {
|
|
|
|
printf("No expo loaded\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:23:02 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-14 22:40:33 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-14 22:40:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-14 22:40:35 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-14 22:40:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-14 22:40:37 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-14 22:40:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:23:02 +00:00
|
|
|
static int do_cedit_run(struct cmd_tbl *cmdtp, int flag, int argc,
|
|
|
|
char *const argv[])
|
|
|
|
{
|
|
|
|
ofnode node;
|
|
|
|
int ret;
|
|
|
|
|
2023-08-14 22:40:33 +00:00
|
|
|
if (check_cur_expo())
|
2023-06-01 16:23:02 +00:00
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
|
2023-08-14 22:40:30 +00:00
|
|
|
node = ofnode_path("/bootstd/cedit-theme");
|
2023-06-01 16:23:02 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-10-07 19:13:08 +00:00
|
|
|
U_BOOT_LONGHELP(cedit,
|
2023-06-01 16:23:02 +00:00
|
|
|
"load <interface> <dev[:part]> <filename> - load config editor\n"
|
2023-08-14 22:40:34 +00:00
|
|
|
"cedit read_fdt <i/f> <dev[:part]> <filename> - read settings\n"
|
2023-08-14 22:40:33 +00:00
|
|
|
"cedit write_fdt <i/f> <dev[:part]> <filename> - write settings\n"
|
2023-08-14 22:40:36 +00:00
|
|
|
"cedit read_env [-v] - read settings from env vars\n"
|
2023-08-14 22:40:35 +00:00
|
|
|
"cedit write_env [-v] - write settings to env vars\n"
|
2023-08-14 22:40:38 +00:00
|
|
|
"cedit read_cmos [-v] [dev] - read settings from CMOS RAM\n"
|
2023-08-14 22:40:37 +00:00
|
|
|
"cedit write_cmos [-v] [dev] - write settings to CMOS RAM\n"
|
2023-10-07 19:13:08 +00:00
|
|
|
"cedit run - run config editor");
|
2023-06-01 16:23:02 +00:00
|
|
|
|
|
|
|
U_BOOT_CMD_WITH_SUBCMDS(cedit, "Configuration editor", cedit_help_text,
|
|
|
|
U_BOOT_SUBCMD_MKENT(load, 5, 1, do_cedit_load),
|
2023-08-14 22:40:34 +00:00
|
|
|
U_BOOT_SUBCMD_MKENT(read_fdt, 5, 1, do_cedit_read_fdt),
|
2023-08-14 22:40:33 +00:00
|
|
|
U_BOOT_SUBCMD_MKENT(write_fdt, 5, 1, do_cedit_write_fdt),
|
2023-08-14 22:40:36 +00:00
|
|
|
U_BOOT_SUBCMD_MKENT(read_env, 2, 1, do_cedit_read_env),
|
2023-08-14 22:40:35 +00:00
|
|
|
U_BOOT_SUBCMD_MKENT(write_env, 2, 1, do_cedit_write_env),
|
2023-08-14 22:40:38 +00:00
|
|
|
U_BOOT_SUBCMD_MKENT(read_cmos, 2, 1, do_cedit_read_cmos),
|
2023-08-14 22:40:37 +00:00
|
|
|
U_BOOT_SUBCMD_MKENT(write_cmos, 2, 1, do_cedit_write_cmos),
|
2023-06-01 16:23:02 +00:00
|
|
|
U_BOOT_SUBCMD_MKENT(run, 1, 1, do_cedit_run),
|
|
|
|
);
|