Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 1 | // 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 | |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 9 | #include <abuf.h> |
Simon Glass | 8df4a4e | 2023-08-14 16:40:25 -0600 | [diff] [blame] | 10 | #include <cedit.h> |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | 2b91ca6 | 2023-08-14 16:40:37 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 13 | #include <expo.h> |
| 14 | #include <fs.h> |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 15 | #include <malloc.h> |
| 16 | #include <mapmem.h> |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 17 | #include <dm/ofnode.h> |
| 18 | #include <linux/sizes.h> |
| 19 | |
| 20 | struct expo *cur_exp; |
| 21 | |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 22 | static 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 Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 32 | static 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 Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 70 | static 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 Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 102 | static int do_cedit_read_fdt(struct cmd_tbl *cmdtp, int flag, int argc, |
| 103 | char *const argv[]) |
| 104 | { |
| 105 | const char *fname; |
| 106 | void *buf; |
| 107 | oftree tree; |
| 108 | ulong size; |
| 109 | int ret; |
| 110 | |
| 111 | if (argc < 4) |
| 112 | return CMD_RET_USAGE; |
| 113 | fname = argv[3]; |
| 114 | |
| 115 | ret = fs_load_alloc(argv[1], argv[2], argv[3], SZ_1M, 0, &buf, &size); |
| 116 | if (ret) { |
| 117 | printf("File not found\n"); |
| 118 | return CMD_RET_FAILURE; |
| 119 | } |
| 120 | |
| 121 | tree = oftree_from_fdt(buf); |
| 122 | if (!oftree_valid(tree)) { |
| 123 | free(buf); |
| 124 | printf("Cannot create oftree\n"); |
| 125 | return CMD_RET_FAILURE; |
| 126 | } |
| 127 | |
| 128 | ret = cedit_read_settings(cur_exp, tree); |
| 129 | oftree_dispose(tree); |
| 130 | free(buf); |
| 131 | if (ret) { |
| 132 | printf("Failed to read settings: %dE\n", ret); |
| 133 | return CMD_RET_FAILURE; |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 139 | static int do_cedit_write_env(struct cmd_tbl *cmdtp, int flag, int argc, |
| 140 | char *const argv[]) |
| 141 | { |
| 142 | bool verbose; |
| 143 | int ret; |
| 144 | |
| 145 | if (check_cur_expo()) |
| 146 | return CMD_RET_FAILURE; |
| 147 | |
| 148 | verbose = argc > 1 && !strcmp(argv[1], "-v"); |
| 149 | |
| 150 | ret = cedit_write_settings_env(cur_exp, verbose); |
| 151 | if (ret) { |
| 152 | printf("Failed to write settings to environment: %dE\n", ret); |
| 153 | return CMD_RET_FAILURE; |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Simon Glass | 0f2e5a6 | 2023-08-14 16:40:36 -0600 | [diff] [blame] | 159 | static int do_cedit_read_env(struct cmd_tbl *cmdtp, int flag, int argc, |
| 160 | char *const argv[]) |
| 161 | { |
| 162 | bool verbose; |
| 163 | int ret; |
| 164 | |
| 165 | if (check_cur_expo()) |
| 166 | return CMD_RET_FAILURE; |
| 167 | |
| 168 | verbose = argc > 1 && !strcmp(argv[1], "-v"); |
| 169 | |
| 170 | ret = cedit_read_settings_env(cur_exp, verbose); |
| 171 | if (ret) { |
| 172 | printf("Failed to read settings from environment: %dE\n", ret); |
| 173 | return CMD_RET_FAILURE; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Simon Glass | 2b91ca6 | 2023-08-14 16:40:37 -0600 | [diff] [blame] | 179 | static int do_cedit_write_cmos(struct cmd_tbl *cmdtp, int flag, int argc, |
| 180 | char *const argv[]) |
| 181 | { |
| 182 | struct udevice *dev; |
| 183 | bool verbose = false; |
| 184 | int ret; |
| 185 | |
| 186 | if (check_cur_expo()) |
| 187 | return CMD_RET_FAILURE; |
| 188 | |
| 189 | if (argc > 1 && !strcmp(argv[1], "-v")) { |
| 190 | verbose = true; |
| 191 | argc--; |
| 192 | argv++; |
| 193 | } |
| 194 | |
| 195 | if (argc > 1) |
| 196 | ret = uclass_get_device_by_name(UCLASS_RTC, argv[1], &dev); |
| 197 | else |
| 198 | ret = uclass_first_device_err(UCLASS_RTC, &dev); |
| 199 | if (ret) { |
| 200 | printf("Failed to get RTC device: %dE\n", ret); |
| 201 | return CMD_RET_FAILURE; |
| 202 | } |
| 203 | |
| 204 | if (cedit_write_settings_cmos(cur_exp, dev, verbose)) { |
| 205 | printf("Failed to write settings to CMOS\n"); |
| 206 | return CMD_RET_FAILURE; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Simon Glass | 4462fa3 | 2023-08-14 16:40:38 -0600 | [diff] [blame] | 212 | static int do_cedit_read_cmos(struct cmd_tbl *cmdtp, int flag, int argc, |
| 213 | char *const argv[]) |
| 214 | { |
| 215 | struct udevice *dev; |
| 216 | bool verbose = false; |
| 217 | int ret; |
| 218 | |
| 219 | if (check_cur_expo()) |
| 220 | return CMD_RET_FAILURE; |
| 221 | |
| 222 | if (argc > 1 && !strcmp(argv[1], "-v")) { |
| 223 | verbose = true; |
| 224 | argc--; |
| 225 | argv++; |
| 226 | } |
| 227 | |
| 228 | if (argc > 1) |
| 229 | ret = uclass_get_device_by_name(UCLASS_RTC, argv[1], &dev); |
| 230 | else |
| 231 | ret = uclass_first_device_err(UCLASS_RTC, &dev); |
| 232 | if (ret) { |
| 233 | printf("Failed to get RTC device: %dE\n", ret); |
| 234 | return CMD_RET_FAILURE; |
| 235 | } |
| 236 | |
| 237 | ret = cedit_read_settings_cmos(cur_exp, dev, verbose); |
| 238 | if (ret) { |
| 239 | printf("Failed to read settings from CMOS: %dE\n", ret); |
| 240 | return CMD_RET_FAILURE; |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 246 | static int do_cedit_run(struct cmd_tbl *cmdtp, int flag, int argc, |
| 247 | char *const argv[]) |
| 248 | { |
| 249 | ofnode node; |
| 250 | int ret; |
| 251 | |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 252 | if (check_cur_expo()) |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 253 | return CMD_RET_FAILURE; |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 254 | |
Simon Glass | 82adc29 | 2023-08-14 16:40:30 -0600 | [diff] [blame] | 255 | node = ofnode_path("/bootstd/cedit-theme"); |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 256 | if (ofnode_valid(node)) { |
| 257 | ret = expo_apply_theme(cur_exp, node); |
| 258 | if (ret) |
| 259 | return CMD_RET_FAILURE; |
| 260 | } else { |
| 261 | log_warning("No theme found\n"); |
| 262 | } |
| 263 | ret = cedit_run(cur_exp); |
| 264 | if (ret) { |
| 265 | log_err("Failed (err=%dE)\n", ret); |
| 266 | return CMD_RET_FAILURE; |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 272 | U_BOOT_LONGHELP(cedit, |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 273 | "load <interface> <dev[:part]> <filename> - load config editor\n" |
Simon Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 274 | "cedit read_fdt <i/f> <dev[:part]> <filename> - read settings\n" |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 275 | "cedit write_fdt <i/f> <dev[:part]> <filename> - write settings\n" |
Simon Glass | 0f2e5a6 | 2023-08-14 16:40:36 -0600 | [diff] [blame] | 276 | "cedit read_env [-v] - read settings from env vars\n" |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 277 | "cedit write_env [-v] - write settings to env vars\n" |
Simon Glass | 4462fa3 | 2023-08-14 16:40:38 -0600 | [diff] [blame] | 278 | "cedit read_cmos [-v] [dev] - read settings from CMOS RAM\n" |
Simon Glass | 2b91ca6 | 2023-08-14 16:40:37 -0600 | [diff] [blame] | 279 | "cedit write_cmos [-v] [dev] - write settings to CMOS RAM\n" |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 280 | "cedit run - run config editor"); |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 281 | |
| 282 | U_BOOT_CMD_WITH_SUBCMDS(cedit, "Configuration editor", cedit_help_text, |
| 283 | U_BOOT_SUBCMD_MKENT(load, 5, 1, do_cedit_load), |
Simon Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 284 | U_BOOT_SUBCMD_MKENT(read_fdt, 5, 1, do_cedit_read_fdt), |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 285 | U_BOOT_SUBCMD_MKENT(write_fdt, 5, 1, do_cedit_write_fdt), |
Simon Glass | 0f2e5a6 | 2023-08-14 16:40:36 -0600 | [diff] [blame] | 286 | U_BOOT_SUBCMD_MKENT(read_env, 2, 1, do_cedit_read_env), |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 287 | U_BOOT_SUBCMD_MKENT(write_env, 2, 1, do_cedit_write_env), |
Simon Glass | 4462fa3 | 2023-08-14 16:40:38 -0600 | [diff] [blame] | 288 | U_BOOT_SUBCMD_MKENT(read_cmos, 2, 1, do_cedit_read_cmos), |
Simon Glass | 2b91ca6 | 2023-08-14 16:40:37 -0600 | [diff] [blame] | 289 | U_BOOT_SUBCMD_MKENT(write_cmos, 2, 1, do_cedit_write_cmos), |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 290 | U_BOOT_SUBCMD_MKENT(run, 1, 1, do_cedit_run), |
| 291 | ); |