command: Remove the cmd_tbl_t typedef
[oweals/u-boot.git] / cmd / spl.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2011
4  * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <cmd_spl.h>
10 #include <env.h>
11 #include <image.h>
12 #include <linux/libfdt.h>
13
14 DECLARE_GLOBAL_DATA_PTR;
15
16 static const char **subcmd_list[] = {
17
18         [SPL_EXPORT_FDT] = (const char * []) {
19 #ifdef CONFIG_OF_LIBFDT
20                 "start",
21                 "loados",
22         #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
23                 "ramdisk",
24         #endif
25                 "fdt",
26                 "cmdline",
27                 "bdt",
28                 "prep",
29 #endif
30                 NULL,
31         },
32         [SPL_EXPORT_ATAGS] = (const char * []) {
33 #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
34         defined(CONFIG_CMDLINE_TAG) || \
35         defined(CONFIG_INITRD_TAG) || \
36         defined(CONFIG_SERIAL_TAG) || \
37         defined(CONFIG_REVISION_TAG)
38                 "start",
39                 "loados",
40 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
41                 "ramdisk",
42 #endif
43                 "cmdline",
44                 "bdt",
45                 "prep",
46 #endif
47                 NULL,
48         },
49         NULL
50 };
51
52 /* Calls bootm with the parameters given */
53 static int call_bootm(int argc, char *const argv[], const char *subcommand[])
54 {
55         char *bootm_argv[5];
56
57         int i = 0;
58         int ret = 0;
59         int j;
60
61         /* create paramter array */
62         bootm_argv[0] = "do_bootm";
63         switch (argc) {
64         case 3:
65                 bootm_argv[4] = argv[2]; /* fdt addr */
66         case 2:
67                 bootm_argv[3] = argv[1]; /* initrd addr */
68         case 1:
69                 bootm_argv[2] = argv[0]; /* kernel addr */
70         }
71
72
73         /*
74          * - do the work -
75          * exec subcommands of do_bootm to init the images
76          * data structure
77          */
78         while (subcommand[i] != NULL) {
79                 bootm_argv[1] = (char *)subcommand[i];
80                 debug("args %d: %s %s ", argc, bootm_argv[0], bootm_argv[1]);
81                 for (j = 0; j < argc; j++)
82                         debug("%s ", bootm_argv[j + 2]);
83                 debug("\n");
84
85                 ret = do_bootm(find_cmd("do_bootm"), 0, argc+2,
86                         bootm_argv);
87                 debug("Subcommand retcode: %d\n", ret);
88                 i++;
89         }
90
91         if (ret) {
92                 printf("ERROR prep subcommand failed!\n");
93                 return -1;
94         }
95
96         return 0;
97 }
98
99 static struct cmd_tbl cmd_spl_export_sub[] = {
100         U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)SPL_EXPORT_FDT, "", ""),
101         U_BOOT_CMD_MKENT(atags, 0, 1, (void *)SPL_EXPORT_ATAGS, "", ""),
102 };
103
104 static int spl_export(struct cmd_tbl *cmdtp, int flag, int argc,
105                       char *const argv[])
106 {
107         const struct cmd_tbl *c;
108
109         if (argc < 2) /* no subcommand */
110                 return cmd_usage(cmdtp);
111
112         c = find_cmd_tbl(argv[1], &cmd_spl_export_sub[0],
113                 ARRAY_SIZE(cmd_spl_export_sub));
114         if ((c) && ((long)c->cmd <= SPL_EXPORT_LAST)) {
115                 argc -= 2;
116                 argv += 2;
117                 if (call_bootm(argc, argv, subcmd_list[(long)c->cmd]))
118                         return -1;
119                 switch ((long)c->cmd) {
120 #ifdef CONFIG_OF_LIBFDT
121                 case SPL_EXPORT_FDT:
122                         printf("Argument image is now in RAM: 0x%p\n",
123                                 (void *)images.ft_addr);
124                         env_set_addr("fdtargsaddr", images.ft_addr);
125                         env_set_hex("fdtargslen", fdt_totalsize(images.ft_addr));
126 #ifdef CONFIG_CMD_SPL_WRITE_SIZE
127                         if (fdt_totalsize(images.ft_addr) >
128                             CONFIG_CMD_SPL_WRITE_SIZE)
129                                 puts("WARN: FDT size > CMD_SPL_WRITE_SIZE\n");
130 #endif
131                         break;
132 #endif
133                 case SPL_EXPORT_ATAGS:
134                         printf("Argument image is now in RAM at: 0x%p\n",
135                                 (void *)gd->bd->bi_boot_params);
136                         break;
137                 }
138         } else {
139                 /* Unrecognized command */
140                 return cmd_usage(cmdtp);
141         }
142
143         return 0;
144 }
145
146 static struct cmd_tbl cmd_spl_sub[] = {
147         U_BOOT_CMD_MKENT(export, 0, 1, (void *)SPL_EXPORT, "", ""),
148 };
149
150 static int do_spl(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
151 {
152         const struct cmd_tbl *c;
153         int cmd;
154
155         if (argc < 2) /* no subcommand */
156                 return cmd_usage(cmdtp);
157
158         c = find_cmd_tbl(argv[1], &cmd_spl_sub[0], ARRAY_SIZE(cmd_spl_sub));
159         if (c) {
160                 cmd = (long)c->cmd;
161                 switch (cmd) {
162                 case SPL_EXPORT:
163                         argc--;
164                         argv++;
165                         if (spl_export(cmdtp, flag, argc, argv))
166                                 printf("Subcommand failed\n");
167                         break;
168                 default:
169                         /* unrecognized command */
170                         return cmd_usage(cmdtp);
171                 }
172         } else {
173                 /* Unrecognized command */
174                 return cmd_usage(cmdtp);
175         }
176         return 0;
177 }
178
179 U_BOOT_CMD(
180         spl, 6 , 1, do_spl, "SPL configuration",
181         "export <img=atags|fdt> [kernel_addr] [initrd_addr] [fdt_addr]\n"
182         "\timg\t\t\"atags\" or \"fdt\"\n"
183         "\tkernel_addr\taddress where a kernel image is stored.\n"
184         "\t\t\tkernel is loaded as part of the boot process, but it is not started.\n"
185         "\tinitrd_addr\taddress of initial ramdisk\n"
186         "\t\t\tcan be set to \"-\" if fdt_addr without initrd_addr is used.\n"
187         "\tfdt_addr\tin case of fdt, the address of the device tree.\n"
188         );