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 | 3434420 | 2024-10-14 16:32:11 -0600 | [diff] [blame] | 70 | #ifdef CONFIG_COREBOOT_SYSINFO |
| 71 | static int do_cedit_cb_load(struct cmd_tbl *cmdtp, int flag, int argc, |
| 72 | char *const argv[]) |
| 73 | { |
| 74 | struct expo *exp; |
| 75 | int ret; |
| 76 | |
| 77 | if (argc > 1) |
| 78 | return CMD_RET_USAGE; |
| 79 | |
| 80 | ret = cb_expo_build(&exp); |
| 81 | if (ret) { |
| 82 | printf("Failed to build expo: %dE\n", ret); |
| 83 | return CMD_RET_FAILURE; |
| 84 | } |
| 85 | |
| 86 | cur_exp = exp; |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | #endif /* CONFIG_COREBOOT_SYSINFO */ |
| 91 | |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 92 | static int do_cedit_write_fdt(struct cmd_tbl *cmdtp, int flag, int argc, |
| 93 | char *const argv[]) |
| 94 | { |
| 95 | const char *fname; |
| 96 | struct abuf buf; |
| 97 | loff_t bytes; |
| 98 | int ret; |
| 99 | |
| 100 | if (argc < 4) |
| 101 | return CMD_RET_USAGE; |
| 102 | fname = argv[3]; |
| 103 | |
| 104 | if (check_cur_expo()) |
| 105 | return CMD_RET_FAILURE; |
| 106 | |
| 107 | ret = cedit_write_settings(cur_exp, &buf); |
| 108 | if (ret) { |
| 109 | printf("Failed to write settings: %dE\n", ret); |
| 110 | return CMD_RET_FAILURE; |
| 111 | } |
| 112 | |
| 113 | if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY)) |
| 114 | return CMD_RET_FAILURE; |
| 115 | |
| 116 | ret = fs_write(fname, map_to_sysmem(abuf_data(&buf)), 0, |
| 117 | abuf_size(&buf), &bytes); |
| 118 | if (ret) |
| 119 | return CMD_RET_FAILURE; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Simon Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 124 | static int do_cedit_read_fdt(struct cmd_tbl *cmdtp, int flag, int argc, |
| 125 | char *const argv[]) |
| 126 | { |
| 127 | const char *fname; |
| 128 | void *buf; |
| 129 | oftree tree; |
| 130 | ulong size; |
| 131 | int ret; |
| 132 | |
| 133 | if (argc < 4) |
| 134 | return CMD_RET_USAGE; |
| 135 | fname = argv[3]; |
| 136 | |
| 137 | ret = fs_load_alloc(argv[1], argv[2], argv[3], SZ_1M, 0, &buf, &size); |
| 138 | if (ret) { |
| 139 | printf("File not found\n"); |
| 140 | return CMD_RET_FAILURE; |
| 141 | } |
| 142 | |
| 143 | tree = oftree_from_fdt(buf); |
| 144 | if (!oftree_valid(tree)) { |
| 145 | free(buf); |
| 146 | printf("Cannot create oftree\n"); |
| 147 | return CMD_RET_FAILURE; |
| 148 | } |
| 149 | |
| 150 | ret = cedit_read_settings(cur_exp, tree); |
| 151 | oftree_dispose(tree); |
| 152 | free(buf); |
| 153 | if (ret) { |
| 154 | printf("Failed to read settings: %dE\n", ret); |
| 155 | return CMD_RET_FAILURE; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 161 | static int do_cedit_write_env(struct cmd_tbl *cmdtp, int flag, int argc, |
| 162 | char *const argv[]) |
| 163 | { |
| 164 | bool verbose; |
| 165 | int ret; |
| 166 | |
| 167 | if (check_cur_expo()) |
| 168 | return CMD_RET_FAILURE; |
| 169 | |
| 170 | verbose = argc > 1 && !strcmp(argv[1], "-v"); |
| 171 | |
| 172 | ret = cedit_write_settings_env(cur_exp, verbose); |
| 173 | if (ret) { |
| 174 | printf("Failed to write settings to environment: %dE\n", ret); |
| 175 | return CMD_RET_FAILURE; |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Simon Glass | 0f2e5a6 | 2023-08-14 16:40:36 -0600 | [diff] [blame] | 181 | static int do_cedit_read_env(struct cmd_tbl *cmdtp, int flag, int argc, |
| 182 | char *const argv[]) |
| 183 | { |
| 184 | bool verbose; |
| 185 | int ret; |
| 186 | |
| 187 | if (check_cur_expo()) |
| 188 | return CMD_RET_FAILURE; |
| 189 | |
| 190 | verbose = argc > 1 && !strcmp(argv[1], "-v"); |
| 191 | |
| 192 | ret = cedit_read_settings_env(cur_exp, verbose); |
| 193 | if (ret) { |
| 194 | printf("Failed to read settings from environment: %dE\n", ret); |
| 195 | return CMD_RET_FAILURE; |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Simon Glass | 2b91ca6 | 2023-08-14 16:40:37 -0600 | [diff] [blame] | 201 | static int do_cedit_write_cmos(struct cmd_tbl *cmdtp, int flag, int argc, |
| 202 | char *const argv[]) |
| 203 | { |
| 204 | struct udevice *dev; |
| 205 | bool verbose = false; |
| 206 | int ret; |
| 207 | |
| 208 | if (check_cur_expo()) |
| 209 | return CMD_RET_FAILURE; |
| 210 | |
| 211 | if (argc > 1 && !strcmp(argv[1], "-v")) { |
| 212 | verbose = true; |
| 213 | argc--; |
| 214 | argv++; |
| 215 | } |
| 216 | |
| 217 | if (argc > 1) |
| 218 | ret = uclass_get_device_by_name(UCLASS_RTC, argv[1], &dev); |
| 219 | else |
| 220 | ret = uclass_first_device_err(UCLASS_RTC, &dev); |
| 221 | if (ret) { |
| 222 | printf("Failed to get RTC device: %dE\n", ret); |
| 223 | return CMD_RET_FAILURE; |
| 224 | } |
| 225 | |
| 226 | if (cedit_write_settings_cmos(cur_exp, dev, verbose)) { |
| 227 | printf("Failed to write settings to CMOS\n"); |
| 228 | return CMD_RET_FAILURE; |
| 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Simon Glass | 4462fa3 | 2023-08-14 16:40:38 -0600 | [diff] [blame] | 234 | static int do_cedit_read_cmos(struct cmd_tbl *cmdtp, int flag, int argc, |
| 235 | char *const argv[]) |
| 236 | { |
| 237 | struct udevice *dev; |
| 238 | bool verbose = false; |
| 239 | int ret; |
| 240 | |
| 241 | if (check_cur_expo()) |
| 242 | return CMD_RET_FAILURE; |
| 243 | |
| 244 | if (argc > 1 && !strcmp(argv[1], "-v")) { |
| 245 | verbose = true; |
| 246 | argc--; |
| 247 | argv++; |
| 248 | } |
| 249 | |
| 250 | if (argc > 1) |
| 251 | ret = uclass_get_device_by_name(UCLASS_RTC, argv[1], &dev); |
| 252 | else |
| 253 | ret = uclass_first_device_err(UCLASS_RTC, &dev); |
| 254 | if (ret) { |
| 255 | printf("Failed to get RTC device: %dE\n", ret); |
| 256 | return CMD_RET_FAILURE; |
| 257 | } |
| 258 | |
| 259 | ret = cedit_read_settings_cmos(cur_exp, dev, verbose); |
| 260 | if (ret) { |
| 261 | printf("Failed to read settings from CMOS: %dE\n", ret); |
| 262 | return CMD_RET_FAILURE; |
| 263 | } |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 268 | static int do_cedit_run(struct cmd_tbl *cmdtp, int flag, int argc, |
| 269 | char *const argv[]) |
| 270 | { |
| 271 | ofnode node; |
| 272 | int ret; |
| 273 | |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 274 | if (check_cur_expo()) |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 275 | return CMD_RET_FAILURE; |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 276 | |
Simon Glass | 82adc29 | 2023-08-14 16:40:30 -0600 | [diff] [blame] | 277 | node = ofnode_path("/bootstd/cedit-theme"); |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 278 | if (ofnode_valid(node)) { |
| 279 | ret = expo_apply_theme(cur_exp, node); |
| 280 | if (ret) |
| 281 | return CMD_RET_FAILURE; |
| 282 | } else { |
| 283 | log_warning("No theme found\n"); |
| 284 | } |
| 285 | ret = cedit_run(cur_exp); |
| 286 | if (ret) { |
| 287 | log_err("Failed (err=%dE)\n", ret); |
| 288 | return CMD_RET_FAILURE; |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 294 | U_BOOT_LONGHELP(cedit, |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 295 | "load <interface> <dev[:part]> <filename> - load config editor\n" |
Simon Glass | 3434420 | 2024-10-14 16:32:11 -0600 | [diff] [blame] | 296 | #ifdef CONFIG_COREBOOT_SYSINFO |
| 297 | "cb_load - load coreboot CMOS editor\n" |
| 298 | #endif |
Simon Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 299 | "cedit read_fdt <i/f> <dev[:part]> <filename> - read settings\n" |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 300 | "cedit write_fdt <i/f> <dev[:part]> <filename> - write settings\n" |
Simon Glass | 0f2e5a6 | 2023-08-14 16:40:36 -0600 | [diff] [blame] | 301 | "cedit read_env [-v] - read settings from env vars\n" |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 302 | "cedit write_env [-v] - write settings to env vars\n" |
Simon Glass | 4462fa3 | 2023-08-14 16:40:38 -0600 | [diff] [blame] | 303 | "cedit read_cmos [-v] [dev] - read settings from CMOS RAM\n" |
Simon Glass | 2b91ca6 | 2023-08-14 16:40:37 -0600 | [diff] [blame] | 304 | "cedit write_cmos [-v] [dev] - write settings to CMOS RAM\n" |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 305 | "cedit run - run config editor"); |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 306 | |
| 307 | U_BOOT_CMD_WITH_SUBCMDS(cedit, "Configuration editor", cedit_help_text, |
| 308 | U_BOOT_SUBCMD_MKENT(load, 5, 1, do_cedit_load), |
Simon Glass | 3434420 | 2024-10-14 16:32:11 -0600 | [diff] [blame] | 309 | #ifdef CONFIG_COREBOOT_SYSINFO |
| 310 | U_BOOT_SUBCMD_MKENT(cb_load, 5, 1, do_cedit_cb_load), |
| 311 | #endif |
Simon Glass | b1cd32b | 2023-08-14 16:40:34 -0600 | [diff] [blame] | 312 | U_BOOT_SUBCMD_MKENT(read_fdt, 5, 1, do_cedit_read_fdt), |
Simon Glass | 28bf435 | 2023-08-14 16:40:33 -0600 | [diff] [blame] | 313 | U_BOOT_SUBCMD_MKENT(write_fdt, 5, 1, do_cedit_write_fdt), |
Simon Glass | 0f2e5a6 | 2023-08-14 16:40:36 -0600 | [diff] [blame] | 314 | U_BOOT_SUBCMD_MKENT(read_env, 2, 1, do_cedit_read_env), |
Simon Glass | 237f375 | 2023-08-14 16:40:35 -0600 | [diff] [blame] | 315 | U_BOOT_SUBCMD_MKENT(write_env, 2, 1, do_cedit_write_env), |
Simon Glass | 4462fa3 | 2023-08-14 16:40:38 -0600 | [diff] [blame] | 316 | U_BOOT_SUBCMD_MKENT(read_cmos, 2, 1, do_cedit_read_cmos), |
Simon Glass | 2b91ca6 | 2023-08-14 16:40:37 -0600 | [diff] [blame] | 317 | U_BOOT_SUBCMD_MKENT(write_cmos, 2, 1, do_cedit_write_cmos), |
Simon Glass | c892511 | 2023-06-01 10:23:02 -0600 | [diff] [blame] | 318 | U_BOOT_SUBCMD_MKENT(run, 1, 1, do_cedit_run), |
| 319 | ); |