x86: cmd: hob: Fix display of resource type for system memory
[platform/kernel/u-boot.git] / cmd / extension_board.c
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) {
114                 list_for_each_entry(extension, &extension_list, list) {
115                         ret = extension_apply(extension);
116                         if (ret != CMD_RET_SUCCESS)
117                                 break;
118                 }
119         } else {
120                 extension_id = simple_strtol(argv[1], NULL, 10);
121                 list_for_each(entry, &extension_list) {
122                         if (i == extension_id) {
123                                 extension = list_entry(entry, struct extension,  list);
124                                 break;
125                         }
126                         i++;
127                 }
128
129                 if (!extension) {
130                         printf("Wrong extension number\n");
131                         return CMD_RET_FAILURE;
132                 }
133
134                 ret = extension_apply(extension);
135         }
136
137         return ret;
138 }
139
140 static struct cmd_tbl cmd_extension[] = {
141         U_BOOT_CMD_MKENT(scan, 1, 1, do_extension_scan, "", ""),
142         U_BOOT_CMD_MKENT(list, 1, 0, do_extension_list, "", ""),
143         U_BOOT_CMD_MKENT(apply, 2, 0, do_extension_apply, "", ""),
144 };
145
146 static int do_extensionops(struct cmd_tbl *cmdtp, int flag, int argc,
147                            char *const argv[])
148 {
149         struct cmd_tbl *cp;
150
151         /* Drop the extension command */
152         argc--;
153         argv++;
154
155         cp = find_cmd_tbl(argv[0], cmd_extension, ARRAY_SIZE(cmd_extension));
156         if (cp)
157                 return cp->cmd(cmdtp, flag, argc, argv);
158
159         return CMD_RET_USAGE;
160 }
161
162 U_BOOT_CMD(extension, 3, 1, do_extensionops,
163         "Extension board management sub system",
164         "scan - scan plugged extension(s) board(s)\n"
165         "extension list - lists available extension(s) board(s)\n"
166         "extension apply <extension number|all> - applies DT overlays corresponding to extension boards\n"
167 );