1 // SPDX-License-Identifier: GPL-2.0+
11 #include <extension_board.h>
13 #include <linux/libfdt.h>
14 #include <fdt_support.h>
16 static LIST_HEAD(extension_list);
18 static int extension_apply(struct extension *extension)
21 ulong extrasize, overlay_addr;
22 struct fdt_header *blob;
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;
30 overlay_cmd = env_get("extension_overlay_cmd");
32 printf("Environment extension_overlay_cmd is missing\n");
33 return CMD_RET_FAILURE;
36 overlay_addr = env_get_hex("extension_overlay_addr", 0);
38 printf("Environment extension_overlay_addr is missing\n");
39 return CMD_RET_FAILURE;
42 env_set("extension_overlay_name", extension->overlay);
43 if (run_command(overlay_cmd, 0) != 0)
44 return CMD_RET_FAILURE;
46 extrasize = env_get_hex("filesize", 0);
48 return CMD_RET_FAILURE;
50 fdt_shrink_to_minimum(working_fdt, extrasize);
52 blob = map_sysmem(overlay_addr, 0);
53 if (!fdt_valid(&blob))
54 return CMD_RET_FAILURE;
56 /* apply method prints messages on error */
57 if (fdt_overlay_apply_verbose(working_fdt, blob))
58 return CMD_RET_FAILURE;
60 return CMD_RET_SUCCESS;
63 static int do_extension_list(struct cmd_tbl *cmdtp, int flag,
64 int argc, char *const argv[])
67 struct extension *extension;
69 if (list_empty(&extension_list)) {
70 printf("No extension registered - Please run \"extension scan\"\n");
71 return CMD_RET_SUCCESS;
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);
81 return CMD_RET_SUCCESS;
84 static int extension_scan(bool show)
86 struct extension *extension, *next;
89 list_for_each_entry_safe(extension, next, &extension_list, list) {
90 list_del(&extension->list);
93 extension_num = extension_board_scan(&extension_list);
94 if (show && extension_num >= 0)
95 printf("Found %d extension board(s).\n", extension_num);
97 /* either the number of extensions, or -ve for error */
101 static int do_extension_scan(struct cmd_tbl *cmdtp, int flag,
102 int argc, char *const argv[])
106 extension_num = extension_scan(true);
107 if (extension_num < 0)
108 return CMD_RET_FAILURE;
110 return CMD_RET_SUCCESS;
113 static int do_extension_apply(struct cmd_tbl *cmdtp, int flag,
114 int argc, char *const argv[])
116 struct extension *extension = NULL;
117 struct list_head *entry;
118 int i = 0, extension_id, ret;
121 return CMD_RET_USAGE;
123 if (strcmp(argv[1], "all") == 0) {
124 ret = CMD_RET_FAILURE;
125 list_for_each_entry(extension, &extension_list, list) {
126 ret = extension_apply(extension);
127 if (ret != CMD_RET_SUCCESS)
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);
141 printf("Wrong extension number\n");
142 return CMD_RET_FAILURE;
145 ret = extension_apply(extension);
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, "", ""),
157 static int do_extensionops(struct cmd_tbl *cmdtp, int flag, int argc,
162 /* Drop the extension command */
166 cp = find_cmd_tbl(argv[0], cmd_extension, ARRAY_SIZE(cmd_extension));
168 return cp->cmd(cmdtp, flag, argc, argv);
170 return CMD_RET_USAGE;
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"
180 static int extension_bootdev_hunt(struct bootdev_hunter *info, bool show)
184 ret = env_set_hex("extension_overlay_addr",
185 env_get_hex("fdtoverlay_addr_r", 0));
187 return log_msg_ret("env", ret);
189 ret = extension_scan(show);
191 return log_msg_ret("ext", ret);
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,