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 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
| 9 | #include <malloc.h> |
| 10 | #include <extension_board.h> |
| 11 | #include <mapmem.h> |
| 12 | #include <linux/libfdt.h> |
| 13 | #include <fdt_support.h> |
| 14 | |
| 15 | static LIST_HEAD(extension_list); |
| 16 | |
| 17 | static int extension_apply(struct extension *extension) |
| 18 | { |
| 19 | char *overlay_cmd; |
| 20 | ulong extrasize, overlay_addr; |
| 21 | struct fdt_header *blob; |
| 22 | |
| 23 | if (!working_fdt) { |
| 24 | printf("No FDT memory address configured. Please configure\n" |
| 25 | "the FDT address via \"fdt addr <address>\" command.\n"); |
| 26 | return CMD_RET_FAILURE; |
| 27 | } |
| 28 | |
| 29 | overlay_cmd = env_get("extension_overlay_cmd"); |
| 30 | if (!overlay_cmd) { |
| 31 | printf("Environment extension_overlay_cmd is missing\n"); |
| 32 | return CMD_RET_FAILURE; |
| 33 | } |
| 34 | |
| 35 | overlay_addr = env_get_hex("extension_overlay_addr", 0); |
| 36 | if (!overlay_addr) { |
| 37 | printf("Environment extension_overlay_addr is missing\n"); |
| 38 | return CMD_RET_FAILURE; |
| 39 | } |
| 40 | |
| 41 | env_set("extension_overlay_name", extension->overlay); |
| 42 | if (run_command(overlay_cmd, 0) != 0) |
| 43 | return CMD_RET_FAILURE; |
| 44 | |
| 45 | extrasize = env_get_hex("filesize", 0); |
| 46 | if (!extrasize) |
| 47 | return CMD_RET_FAILURE; |
| 48 | |
| 49 | fdt_shrink_to_minimum(working_fdt, extrasize); |
| 50 | |
| 51 | blob = map_sysmem(overlay_addr, 0); |
| 52 | if (!fdt_valid(&blob)) |
| 53 | return CMD_RET_FAILURE; |
| 54 | |
| 55 | /* apply method prints messages on error */ |
| 56 | if (fdt_overlay_apply_verbose(working_fdt, blob)) |
| 57 | return CMD_RET_FAILURE; |
| 58 | |
| 59 | return CMD_RET_SUCCESS; |
| 60 | } |
| 61 | |
| 62 | static int do_extension_list(struct cmd_tbl *cmdtp, int flag, |
| 63 | int argc, char *const argv[]) |
| 64 | { |
| 65 | int i = 0; |
| 66 | struct extension *extension; |
| 67 | |
| 68 | if (list_empty(&extension_list)) { |
| 69 | printf("No extension registered - Please run \"extension scan\"\n"); |
| 70 | return CMD_RET_SUCCESS; |
| 71 | } |
| 72 | |
| 73 | list_for_each_entry(extension, &extension_list, list) { |
| 74 | printf("Extension %d: %s\n", i++, extension->name); |
| 75 | printf("\tManufacturer: \t\t%s\n", extension->owner); |
| 76 | printf("\tVersion: \t\t%s\n", extension->version); |
| 77 | printf("\tDevicetree overlay: \t%s\n", extension->overlay); |
| 78 | printf("\tOther information: \t%s\n", extension->other); |
| 79 | } |
| 80 | return CMD_RET_SUCCESS; |
| 81 | } |
| 82 | |
| 83 | static int do_extension_scan(struct cmd_tbl *cmdtp, int flag, |
| 84 | int argc, char *const argv[]) |
| 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); |
| 94 | |
| 95 | if (extension_num < 0) |
| 96 | return CMD_RET_FAILURE; |
| 97 | |
| 98 | printf("Found %d extension board(s).\n", extension_num); |
| 99 | |
| 100 | return CMD_RET_SUCCESS; |
| 101 | } |
| 102 | |
| 103 | static int do_extension_apply(struct cmd_tbl *cmdtp, int flag, |
| 104 | int argc, char *const argv[]) |
| 105 | { |
| 106 | struct extension *extension = NULL; |
| 107 | struct list_head *entry; |
| 108 | int i = 0, extension_id, ret; |
| 109 | |
| 110 | if (argc < 2) |
| 111 | return CMD_RET_USAGE; |
| 112 | |
| 113 | if (strcmp(argv[1], "all") == 0) { |
Heinrich Schuchardt | dffb177 | 2022-07-11 20:01:12 +0200 | [diff] [blame] | 114 | ret = CMD_RET_FAILURE; |
Kory Maincent | 7444a7d | 2021-05-04 19:31:22 +0200 | [diff] [blame] | 115 | list_for_each_entry(extension, &extension_list, list) { |
| 116 | ret = extension_apply(extension); |
| 117 | if (ret != CMD_RET_SUCCESS) |
| 118 | break; |
| 119 | } |
| 120 | } else { |
| 121 | extension_id = simple_strtol(argv[1], NULL, 10); |
| 122 | list_for_each(entry, &extension_list) { |
| 123 | if (i == extension_id) { |
| 124 | extension = list_entry(entry, struct extension, list); |
| 125 | break; |
| 126 | } |
| 127 | i++; |
| 128 | } |
| 129 | |
| 130 | if (!extension) { |
| 131 | printf("Wrong extension number\n"); |
| 132 | return CMD_RET_FAILURE; |
| 133 | } |
| 134 | |
| 135 | ret = extension_apply(extension); |
| 136 | } |
| 137 | |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | static struct cmd_tbl cmd_extension[] = { |
| 142 | U_BOOT_CMD_MKENT(scan, 1, 1, do_extension_scan, "", ""), |
| 143 | U_BOOT_CMD_MKENT(list, 1, 0, do_extension_list, "", ""), |
| 144 | U_BOOT_CMD_MKENT(apply, 2, 0, do_extension_apply, "", ""), |
| 145 | }; |
| 146 | |
| 147 | static int do_extensionops(struct cmd_tbl *cmdtp, int flag, int argc, |
| 148 | char *const argv[]) |
| 149 | { |
| 150 | struct cmd_tbl *cp; |
| 151 | |
| 152 | /* Drop the extension command */ |
| 153 | argc--; |
| 154 | argv++; |
| 155 | |
| 156 | cp = find_cmd_tbl(argv[0], cmd_extension, ARRAY_SIZE(cmd_extension)); |
| 157 | if (cp) |
| 158 | return cp->cmd(cmdtp, flag, argc, argv); |
| 159 | |
| 160 | return CMD_RET_USAGE; |
| 161 | } |
| 162 | |
| 163 | U_BOOT_CMD(extension, 3, 1, do_extensionops, |
| 164 | "Extension board management sub system", |
| 165 | "scan - scan plugged extension(s) board(s)\n" |
| 166 | "extension list - lists available extension(s) board(s)\n" |
| 167 | "extension apply <extension number|all> - applies DT overlays corresponding to extension boards\n" |
| 168 | ); |