blob: 0cae304c4adced59d3ae40e3004c710b0b1ebf71 [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>
10#include <command.h>
11#include <expo.h>
12#include <fs.h>
13#include <dm/ofnode.h>
14#include <linux/sizes.h>
15
16struct expo *cur_exp;
17
18static int do_cedit_load(struct cmd_tbl *cmdtp, int flag, int argc,
19 char *const argv[])
20{
21 const char *fname;
22 struct expo *exp;
23 oftree tree;
24 ulong size;
25 void *buf;
26 int ret;
27
28 if (argc < 4)
29 return CMD_RET_USAGE;
30 fname = argv[3];
31
32 ret = fs_load_alloc(argv[1], argv[2], argv[3], SZ_1M, 0, &buf, &size);
33 if (ret) {
34 printf("File not found\n");
35 return CMD_RET_FAILURE;
36 }
37
38 tree = oftree_from_fdt(buf);
39 if (!oftree_valid(tree)) {
40 printf("Cannot create oftree\n");
41 return CMD_RET_FAILURE;
42 }
43
44 ret = expo_build(oftree_root(tree), &exp);
45 oftree_dispose(tree);
46 if (ret) {
47 printf("Failed to build expo: %dE\n", ret);
48 return CMD_RET_FAILURE;
49 }
50
51 cur_exp = exp;
52
53 return 0;
54}
55
56static int do_cedit_run(struct cmd_tbl *cmdtp, int flag, int argc,
57 char *const argv[])
58{
59 ofnode node;
60 int ret;
61
62 if (!cur_exp) {
63 printf("No expo loaded\n");
64 return CMD_RET_FAILURE;
65 }
66
67 node = ofnode_path("/cedit-theme");
68 if (ofnode_valid(node)) {
69 ret = expo_apply_theme(cur_exp, node);
70 if (ret)
71 return CMD_RET_FAILURE;
72 } else {
73 log_warning("No theme found\n");
74 }
75 ret = cedit_run(cur_exp);
76 if (ret) {
77 log_err("Failed (err=%dE)\n", ret);
78 return CMD_RET_FAILURE;
79 }
80
81 return 0;
82}
83
84#ifdef CONFIG_SYS_LONGHELP
85static char cedit_help_text[] =
86 "load <interface> <dev[:part]> <filename> - load config editor\n"
87 "cedit run - run config editor";
88#endif /* CONFIG_SYS_LONGHELP */
89
90U_BOOT_CMD_WITH_SUBCMDS(cedit, "Configuration editor", cedit_help_text,
91 U_BOOT_SUBCMD_MKENT(load, 5, 1, do_cedit_load),
92 U_BOOT_SUBCMD_MKENT(run, 1, 1, do_cedit_run),
93);