1 // SPDX-License-Identifier: GPL-2.0+
4 * Köry Maincent, Bootlin, <kory.maincent@bootlin.com>
10 #include <extension_board.h>
12 #include <linux/libfdt.h>
13 #include <fdt_support.h>
15 static LIST_HEAD(extension_list);
17 static int extension_apply(struct extension *extension)
20 ulong extrasize, overlay_addr;
21 struct fdt_header *blob;
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;
29 overlay_cmd = env_get("extension_overlay_cmd");
31 printf("Environment extension_overlay_cmd is missing\n");
32 return CMD_RET_FAILURE;
35 overlay_addr = env_get_hex("extension_overlay_addr", 0);
37 printf("Environment extension_overlay_addr is missing\n");
38 return CMD_RET_FAILURE;
41 env_set("extension_overlay_name", extension->overlay);
42 if (run_command(overlay_cmd, 0) != 0)
43 return CMD_RET_FAILURE;
45 extrasize = env_get_hex("filesize", 0);
47 return CMD_RET_FAILURE;
49 fdt_shrink_to_minimum(working_fdt, extrasize);
51 blob = map_sysmem(overlay_addr, 0);
52 if (!fdt_valid(&blob))
53 return CMD_RET_FAILURE;
55 /* apply method prints messages on error */
56 if (fdt_overlay_apply_verbose(working_fdt, blob))
57 return CMD_RET_FAILURE;
59 return CMD_RET_SUCCESS;
62 static int do_extension_list(struct cmd_tbl *cmdtp, int flag,
63 int argc, char *const argv[])
66 struct extension *extension;
68 if (list_empty(&extension_list)) {
69 printf("No extension registered - Please run \"extension scan\"\n");
70 return CMD_RET_SUCCESS;
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);
80 return CMD_RET_SUCCESS;
83 static int do_extension_scan(struct cmd_tbl *cmdtp, int flag,
84 int argc, char *const argv[])
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);
95 if (extension_num < 0)
96 return CMD_RET_FAILURE;
98 printf("Found %d extension board(s).\n", extension_num);
100 return CMD_RET_SUCCESS;
103 static int do_extension_apply(struct cmd_tbl *cmdtp, int flag,
104 int argc, char *const argv[])
106 struct extension *extension = NULL;
107 struct list_head *entry;
108 int i = 0, extension_id, ret;
111 return CMD_RET_USAGE;
113 if (strcmp(argv[1], "all") == 0) {
114 ret = CMD_RET_FAILURE;
115 list_for_each_entry(extension, &extension_list, list) {
116 ret = extension_apply(extension);
117 if (ret != CMD_RET_SUCCESS)
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);
131 printf("Wrong extension number\n");
132 return CMD_RET_FAILURE;
135 ret = extension_apply(extension);
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, "", ""),
147 static int do_extensionops(struct cmd_tbl *cmdtp, int flag, int argc,
152 /* Drop the extension command */
156 cp = find_cmd_tbl(argv[0], cmd_extension, ARRAY_SIZE(cmd_extension));
158 return cp->cmd(cmdtp, flag, argc, argv);
160 return CMD_RET_USAGE;
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"