Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2021 |
| 4 | * Köry Maincent, Bootlin, <kory.maincent@bootlin.com> |
| 5 | */ |
| 6 | |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 7 | #include <bootdev.h> |
Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 8 | #include <command.h> |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 9 | #include <dm.h> |
Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <extension_board.h> |
| 12 | #include <mapmem.h> |
| 13 | #include <linux/libfdt.h> |
| 14 | #include <fdt_support.h> |
| 15 | |
| 16 | static LIST_HEAD(extension_list); |
| 17 | |
| 18 | static int extension_apply(struct extension *extension) |
| 19 | { |
| 20 | char *overlay_cmd; |
| 21 | ulong extrasize, overlay_addr; |
| 22 | struct fdt_header *blob; |
| 23 | |
| 24 | if (!working_fdt) { |
| 25 | printf("No FDT memory address configured. Please configure\n" |
| 26 | "the FDT address via \"fdt addr <address>\" command.\n"); |
| 27 | return CMD_RET_FAILURE; |
| 28 | } |
| 29 | |
| 30 | overlay_cmd = env_get("extension_overlay_cmd"); |
| 31 | if (!overlay_cmd) { |
| 32 | printf("Environment extension_overlay_cmd is missing\n"); |
| 33 | return CMD_RET_FAILURE; |
| 34 | } |
| 35 | |
| 36 | overlay_addr = env_get_hex("extension_overlay_addr", 0); |
| 37 | if (!overlay_addr) { |
| 38 | printf("Environment extension_overlay_addr is missing\n"); |
| 39 | return CMD_RET_FAILURE; |
| 40 | } |
| 41 | |
| 42 | env_set("extension_overlay_name", extension->overlay); |
| 43 | if (run_command(overlay_cmd, 0) != 0) |
| 44 | return CMD_RET_FAILURE; |
| 45 | |
| 46 | extrasize = env_get_hex("filesize", 0); |
| 47 | if (!extrasize) |
| 48 | return CMD_RET_FAILURE; |
| 49 | |
| 50 | fdt_shrink_to_minimum(working_fdt, extrasize); |
| 51 | |
| 52 | blob = map_sysmem(overlay_addr, 0); |
| 53 | if (!fdt_valid(&blob)) |
| 54 | return CMD_RET_FAILURE; |
| 55 | |
| 56 | /* apply method prints messages on error */ |
| 57 | if (fdt_overlay_apply_verbose(working_fdt, blob)) |
| 58 | return CMD_RET_FAILURE; |
| 59 | |
| 60 | return CMD_RET_SUCCESS; |
| 61 | } |
| 62 | |
| 63 | static int do_extension_list(struct cmd_tbl *cmdtp, int flag, |
| 64 | int argc, char *const argv[]) |
| 65 | { |
| 66 | int i = 0; |
| 67 | struct extension *extension; |
| 68 | |
| 69 | if (list_empty(&extension_list)) { |
| 70 | printf("No extension registered - Please run \"extension scan\"\n"); |
| 71 | return CMD_RET_SUCCESS; |
| 72 | } |
| 73 | |
| 74 | list_for_each_entry(extension, &extension_list, list) { |
| 75 | printf("Extension %d: %s\n", i++, extension->name); |
| 76 | printf("\tManufacturer: \t\t%s\n", extension->owner); |
| 77 | printf("\tVersion: \t\t%s\n", extension->version); |
| 78 | printf("\tDevicetree overlay: \t%s\n", extension->overlay); |
| 79 | printf("\tOther information: \t%s\n", extension->other); |
| 80 | } |
| 81 | return CMD_RET_SUCCESS; |
| 82 | } |
| 83 | |
Simon Glass | 5e50858 | 2023-01-17 10:48:12 -0700 | [diff] [blame] | 84 | static int extension_scan(bool show) |
Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 85 | { |
| 86 | struct extension *extension, *next; |
| 87 | int extension_num; |
| 88 | |
| 89 | list_for_each_entry_safe(extension, next, &extension_list, list) { |
| 90 | list_del(&extension->list); |
| 91 | free(extension); |
| 92 | } |
| 93 | extension_num = extension_board_scan(&extension_list); |
Simon Glass | 5e50858 | 2023-01-17 10:48:12 -0700 | [diff] [blame] | 94 | if (show && extension_num >= 0) |
| 95 | printf("Found %d extension board(s).\n", extension_num); |
| 96 | |
| 97 | /* either the number of extensions, or -ve for error */ |
| 98 | return extension_num; |
| 99 | } |
| 100 | |
Simon Glass | 5e50858 | 2023-01-17 10:48:12 -0700 | [diff] [blame] | 101 | static int do_extension_scan(struct cmd_tbl *cmdtp, int flag, |
| 102 | int argc, char *const argv[]) |
| 103 | { |
| 104 | int extension_num; |
Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 105 | |
Simon Glass | 5e50858 | 2023-01-17 10:48:12 -0700 | [diff] [blame] | 106 | extension_num = extension_scan(true); |
Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 107 | if (extension_num < 0) |
| 108 | return CMD_RET_FAILURE; |
| 109 | |
Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 110 | return CMD_RET_SUCCESS; |
| 111 | } |
| 112 | |
| 113 | static int do_extension_apply(struct cmd_tbl *cmdtp, int flag, |
| 114 | int argc, char *const argv[]) |
| 115 | { |
| 116 | struct extension *extension = NULL; |
| 117 | struct list_head *entry; |
| 118 | int i = 0, extension_id, ret; |
| 119 | |
| 120 | if (argc < 2) |
| 121 | return CMD_RET_USAGE; |
| 122 | |
| 123 | if (strcmp(argv[1], "all") == 0) { |
Heinrich Schuchardt | dffb177 | 2022-07-11 20:01:12 +0200 | [diff] [blame] | 124 | ret = CMD_RET_FAILURE; |
Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 125 | list_for_each_entry(extension, &extension_list, list) { |
| 126 | ret = extension_apply(extension); |
| 127 | if (ret != CMD_RET_SUCCESS) |
| 128 | break; |
| 129 | } |
| 130 | } else { |
| 131 | extension_id = simple_strtol(argv[1], NULL, 10); |
| 132 | list_for_each(entry, &extension_list) { |
| 133 | if (i == extension_id) { |
| 134 | extension = list_entry(entry, struct extension, list); |
| 135 | break; |
| 136 | } |
| 137 | i++; |
| 138 | } |
| 139 | |
| 140 | if (!extension) { |
| 141 | printf("Wrong extension number\n"); |
| 142 | return CMD_RET_FAILURE; |
| 143 | } |
| 144 | |
| 145 | ret = extension_apply(extension); |
| 146 | } |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static struct cmd_tbl cmd_extension[] = { |
| 152 | U_BOOT_CMD_MKENT(scan, 1, 1, do_extension_scan, "", ""), |
| 153 | U_BOOT_CMD_MKENT(list, 1, 0, do_extension_list, "", ""), |
| 154 | U_BOOT_CMD_MKENT(apply, 2, 0, do_extension_apply, "", ""), |
| 155 | }; |
| 156 | |
| 157 | static int do_extensionops(struct cmd_tbl *cmdtp, int flag, int argc, |
| 158 | char *const argv[]) |
| 159 | { |
| 160 | struct cmd_tbl *cp; |
| 161 | |
| 162 | /* Drop the extension command */ |
| 163 | argc--; |
| 164 | argv++; |
| 165 | |
| 166 | cp = find_cmd_tbl(argv[0], cmd_extension, ARRAY_SIZE(cmd_extension)); |
| 167 | if (cp) |
| 168 | return cp->cmd(cmdtp, flag, argc, argv); |
| 169 | |
| 170 | return CMD_RET_USAGE; |
| 171 | } |
| 172 | |
| 173 | U_BOOT_CMD(extension, 3, 1, do_extensionops, |
| 174 | "Extension board management sub system", |
| 175 | "scan - scan plugged extension(s) board(s)\n" |
| 176 | "extension list - lists available extension(s) board(s)\n" |
| 177 | "extension apply <extension number|all> - applies DT overlays corresponding to extension boards\n" |
| 178 | ); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 179 | |
| 180 | static int extension_bootdev_hunt(struct bootdev_hunter *info, bool show) |
| 181 | { |
| 182 | int ret; |
| 183 | |
| 184 | ret = env_set_hex("extension_overlay_addr", |
| 185 | env_get_hex("fdtoverlay_addr_r", 0)); |
| 186 | if (ret) |
| 187 | return log_msg_ret("env", ret); |
| 188 | |
| 189 | ret = extension_scan(show); |
| 190 | if (ret < 0) |
| 191 | return log_msg_ret("ext", ret); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /* extensions should have a uclass - for now we use UCLASS_SIMPLE_BUS uclass */ |
| 197 | BOOTDEV_HUNTER(extension_bootdev_hunter) = { |
| 198 | .prio = BOOTDEVP_1_PRE_SCAN, |
| 199 | .uclass = UCLASS_SIMPLE_BUS, |
| 200 | .hunt = extension_bootdev_hunt, |
| 201 | }; |