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 | |
| 101 | |
| 102 | static int do_extension_scan(struct cmd_tbl *cmdtp, int flag, |
| 103 | int argc, char *const argv[]) |
| 104 | { |
| 105 | int extension_num; |
Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 106 | |
Simon Glass | 5e50858 | 2023-01-17 10:48:12 -0700 | [diff] [blame] | 107 | extension_num = extension_scan(true); |
Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 108 | if (extension_num < 0) |
| 109 | return CMD_RET_FAILURE; |
| 110 | |
Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 111 | return CMD_RET_SUCCESS; |
| 112 | } |
| 113 | |
| 114 | static int do_extension_apply(struct cmd_tbl *cmdtp, int flag, |
| 115 | int argc, char *const argv[]) |
| 116 | { |
| 117 | struct extension *extension = NULL; |
| 118 | struct list_head *entry; |
| 119 | int i = 0, extension_id, ret; |
| 120 | |
| 121 | if (argc < 2) |
| 122 | return CMD_RET_USAGE; |
| 123 | |
| 124 | if (strcmp(argv[1], "all") == 0) { |
Heinrich Schuchardt | dffb177 | 2022-07-11 20:01:12 +0200 | [diff] [blame] | 125 | ret = CMD_RET_FAILURE; |
Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 126 | list_for_each_entry(extension, &extension_list, list) { |
| 127 | ret = extension_apply(extension); |
| 128 | if (ret != CMD_RET_SUCCESS) |
| 129 | break; |
| 130 | } |
| 131 | } else { |
| 132 | extension_id = simple_strtol(argv[1], NULL, 10); |
| 133 | list_for_each(entry, &extension_list) { |
| 134 | if (i == extension_id) { |
| 135 | extension = list_entry(entry, struct extension, list); |
| 136 | break; |
| 137 | } |
| 138 | i++; |
| 139 | } |
| 140 | |
| 141 | if (!extension) { |
| 142 | printf("Wrong extension number\n"); |
| 143 | return CMD_RET_FAILURE; |
| 144 | } |
| 145 | |
| 146 | ret = extension_apply(extension); |
| 147 | } |
| 148 | |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | static struct cmd_tbl cmd_extension[] = { |
| 153 | U_BOOT_CMD_MKENT(scan, 1, 1, do_extension_scan, "", ""), |
| 154 | U_BOOT_CMD_MKENT(list, 1, 0, do_extension_list, "", ""), |
| 155 | U_BOOT_CMD_MKENT(apply, 2, 0, do_extension_apply, "", ""), |
| 156 | }; |
| 157 | |
| 158 | static int do_extensionops(struct cmd_tbl *cmdtp, int flag, int argc, |
| 159 | char *const argv[]) |
| 160 | { |
| 161 | struct cmd_tbl *cp; |
| 162 | |
| 163 | /* Drop the extension command */ |
| 164 | argc--; |
| 165 | argv++; |
| 166 | |
| 167 | cp = find_cmd_tbl(argv[0], cmd_extension, ARRAY_SIZE(cmd_extension)); |
| 168 | if (cp) |
| 169 | return cp->cmd(cmdtp, flag, argc, argv); |
| 170 | |
| 171 | return CMD_RET_USAGE; |
| 172 | } |
| 173 | |
| 174 | U_BOOT_CMD(extension, 3, 1, do_extensionops, |
| 175 | "Extension board management sub system", |
| 176 | "scan - scan plugged extension(s) board(s)\n" |
| 177 | "extension list - lists available extension(s) board(s)\n" |
| 178 | "extension apply <extension number|all> - applies DT overlays corresponding to extension boards\n" |
| 179 | ); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 180 | |
| 181 | static int extension_bootdev_hunt(struct bootdev_hunter *info, bool show) |
| 182 | { |
| 183 | int ret; |
| 184 | |
| 185 | ret = env_set_hex("extension_overlay_addr", |
| 186 | env_get_hex("fdtoverlay_addr_r", 0)); |
| 187 | if (ret) |
| 188 | return log_msg_ret("env", ret); |
| 189 | |
| 190 | ret = extension_scan(show); |
| 191 | if (ret < 0) |
| 192 | return log_msg_ret("ext", ret); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /* extensions should have a uclass - for now we use UCLASS_SIMPLE_BUS uclass */ |
| 198 | BOOTDEV_HUNTER(extension_bootdev_hunter) = { |
| 199 | .prio = BOOTDEVP_1_PRE_SCAN, |
| 200 | .uclass = UCLASS_SIMPLE_BUS, |
| 201 | .hunt = extension_bootdev_hunt, |
| 202 | }; |