blob: 114f2caf7fab6607f297fc22c92981df4e607f83 [file] [log] [blame]
Tom Rini8abb4cb2020-01-29 11:03:34 -05001// SPDX-License-Identifier: BSD-2-Clause
wdenk458ded32002-09-20 10:59:08 +00002/*
3 * Copyright (c) 2001 William L. Pitts
4 * All rights reserved.
wdenk458ded32002-09-20 10:59:08 +00005 */
6
wdenk458ded32002-09-20 10:59:08 +00007#include <command.h>
Simon Glass370382c2019-11-14 12:57:35 -07008#include <cpu_func.h>
wdenk458ded32002-09-20 10:59:08 +00009#include <elf.h>
Simon Glass83c2e492019-08-01 09:46:50 -060010#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070011#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Daniel Palmer6218cf52024-09-29 18:27:39 +090013#ifdef CONFIG_CMD_ELF_BOOTVX
Bin Meng53f3afb2015-10-07 20:19:13 -070014#include <net.h>
Niklaus Gigerf662b232008-11-03 22:15:34 +010015#include <vxworks.h>
Daniel Palmer6218cf52024-09-29 18:27:39 +090016#endif
Bin Meng321e8602015-10-07 20:19:18 -070017#ifdef CONFIG_X86
Simon Glassec86bc62022-07-30 15:52:04 -060018#include <vesa.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <asm/cache.h>
Bin Meng321e8602015-10-07 20:19:18 -070020#include <asm/e820.h>
Bin Mengf6900e22015-10-07 20:19:19 -070021#include <linux/linkage.h>
Bin Meng321e8602015-10-07 20:19:18 -070022#endif
wdenk458ded32002-09-20 10:59:08 +000023
Bin Meng53f3afb2015-10-07 20:19:13 -070024/* Interpreter command to boot an arbitrary ELF image from memory */
Simon Glassed38aef2020-05-10 11:40:03 -060025int do_bootelf(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk458ded32002-09-20 10:59:08 +000026{
Maxim Moskalets938f0112024-03-08 00:29:14 +030027#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
28 struct bootm_headers img = {0};
29 unsigned long fdt_addr = 0; /* Address of the FDT */
30#endif
Bin Meng53f3afb2015-10-07 20:19:13 -070031 unsigned long addr; /* Address of the ELF image */
32 unsigned long rc; /* Return value from user code */
Maxim Moskaletse4170b82024-06-05 21:43:34 +030033 int rcode = CMD_RET_SUCCESS;
34 Bootelf_flags flags = {0};
wdenk458ded32002-09-20 10:59:08 +000035
Tom Rini3c95c792017-05-18 17:03:07 -040036 /* Consume 'bootelf' */
37 argc--; argv++;
Mike Frysingere6dc29f2010-10-02 15:44:53 -040038
Maxim Moskalets938f0112024-03-08 00:29:14 +030039 /* Check for [-p|-s] flag. */
Tom Rini3c95c792017-05-18 17:03:07 -040040 if (argc >= 1 && (argv[0][0] == '-' && \
41 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
Maxim Moskaletse4170b82024-06-05 21:43:34 +030042 if (argv[0][1] == 'p')
43 flags.phdr = 1;
44 log_debug("Using ELF header format %s\n",
45 flags.phdr ? "phdr" : "shdr");
Tom Rini3c95c792017-05-18 17:03:07 -040046 /* Consume flag. */
47 argc--; argv++;
48 }
Maxim Moskalets938f0112024-03-08 00:29:14 +030049
50#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
51 /* Check for [-d fdt_addr_r] option. */
52 if ((argc >= 2) && (argv[0][0] == '-') && (argv[0][1] == 'd')) {
53 if (strict_strtoul(argv[1], 16, &fdt_addr) != 0)
54 return CMD_RET_USAGE;
55 /* Consume option. */
56 argc -= 2;
57 argv += 2;
58 }
59#endif
60
Tom Rini3c95c792017-05-18 17:03:07 -040061 /* Check for address. */
62 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
63 /* Consume address */
64 argc--; argv++;
65 } else
Simon Glass892265d2019-12-28 10:45:02 -070066 addr = image_load_addr;
wdenk458ded32002-09-20 10:59:08 +000067
Maxim Moskalets938f0112024-03-08 00:29:14 +030068#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
69 if (fdt_addr) {
Maxim Moskaletse4170b82024-06-05 21:43:34 +030070 log_debug("Setting up FDT at 0x%08lx ...\n", fdt_addr);
Maxim Moskalets938f0112024-03-08 00:29:14 +030071 flush();
72
Maxim Moskalets987faed2024-06-28 11:34:11 +030073 fdt_set_totalsize((void *)fdt_addr,
74 fdt_totalsize(fdt_addr) + CONFIG_SYS_FDT_PAD);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053075 if (image_setup_libfdt(&img, (void *)fdt_addr, false))
Maxim Moskalets938f0112024-03-08 00:29:14 +030076 return 1;
77 }
78#endif
79
Maxim Moskaletse4170b82024-06-05 21:43:34 +030080 if (env_get_autostart()) {
81 flags.autostart = 1;
82 log_debug("Starting application at 0x%08lx ...\n", addr);
83 flush();
84 }
wdenk458ded32002-09-20 10:59:08 +000085
wdenk458ded32002-09-20 10:59:08 +000086 /*
87 * pass address parameter as argv[0] (aka command name),
Maxim Moskaletse4170b82024-06-05 21:43:34 +030088 * and all remaining arguments
wdenk458ded32002-09-20 10:59:08 +000089 */
Maxim Moskaletse4170b82024-06-05 21:43:34 +030090 rc = bootelf(addr, flags, argc, argv);
wdenk458ded32002-09-20 10:59:08 +000091 if (rc != 0)
Maxim Moskaletse4170b82024-06-05 21:43:34 +030092 rcode = CMD_RET_FAILURE;
wdenk458ded32002-09-20 10:59:08 +000093
Maxim Moskaletse4170b82024-06-05 21:43:34 +030094 if (flags.autostart)
95 {
96 if (ENOEXEC == errno)
97 log_err("Invalid ELF image\n");
98 else
99 log_debug("## Application terminated, rc = 0x%lx\n", rc);
100 }
Bin Meng53f3afb2015-10-07 20:19:13 -0700101
wdenk458ded32002-09-20 10:59:08 +0000102 return rcode;
103}
104
Daniel Palmer6218cf52024-09-29 18:27:39 +0900105#ifdef CONFIG_CMD_ELF_BOOTVX
Bin Meng53f3afb2015-10-07 20:19:13 -0700106/*
wdenk458ded32002-09-20 10:59:08 +0000107 * Interpreter command to boot VxWorks from a memory image. The image can
108 * be either an ELF image or a raw binary. Will attempt to setup the
109 * bootline and other parameters correctly.
Bin Meng53f3afb2015-10-07 20:19:13 -0700110 */
Simon Glassed38aef2020-05-10 11:40:03 -0600111int do_bootvx(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk458ded32002-09-20 10:59:08 +0000112{
Bin Meng53f3afb2015-10-07 20:19:13 -0700113 unsigned long addr; /* Address of image */
Bin Meng75f4b722018-04-11 22:02:22 -0700114 unsigned long bootaddr = 0; /* Address to put the bootline */
Bin Meng53f3afb2015-10-07 20:19:13 -0700115 char *bootline; /* Text of the bootline */
116 char *tmp; /* Temporary char pointer */
117 char build_buf[128]; /* Buffer for building the bootline */
Bin Meng0401c862015-10-07 20:19:15 -0700118 int ptr = 0;
Bin Meng321e8602015-10-07 20:19:18 -0700119#ifdef CONFIG_X86
Bin Menge3be8902018-04-11 22:02:07 -0700120 ulong base;
Bin Mengdde4ebb2018-04-11 22:02:09 -0700121 struct e820_info *info;
Bin Meng4b8fc742018-04-11 22:02:11 -0700122 struct e820_entry *data;
Bin Meng71606c42018-04-11 22:02:19 -0700123 struct efi_gop_info *gop;
124 struct vesa_mode_info *vesa = &mode_info.vesa;
Bin Meng321e8602015-10-07 20:19:18 -0700125#endif
wdenk458ded32002-09-20 10:59:08 +0000126
Bin Meng53f3afb2015-10-07 20:19:13 -0700127 /*
wdenk458ded32002-09-20 10:59:08 +0000128 * Check the loadaddr variable.
129 * If we don't know where the image is then we're done.
130 */
Stany MARCELe5deaf582013-11-27 14:48:43 +0100131 if (argc < 2)
Simon Glass892265d2019-12-28 10:45:02 -0700132 addr = image_load_addr;
Stefan Roese00487782006-11-29 12:53:15 +0100133 else
Simon Glass3ff49ec2021-07-24 09:03:29 -0600134 addr = hextoul(argv[1], NULL);
wdenk458ded32002-09-20 10:59:08 +0000135
Jon Loeliger54324d02007-07-08 17:51:39 -0500136#if defined(CONFIG_CMD_NET)
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000137 /*
Bin Meng53f3afb2015-10-07 20:19:13 -0700138 * Check to see if we need to tftp the image ourselves
139 * before starting
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000140 */
Stany MARCELe5deaf582013-11-27 14:48:43 +0100141 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500142 if (net_loop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000143 return 1;
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000144 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
145 addr);
wdenk458ded32002-09-20 10:59:08 +0000146 }
147#endif
148
Bin Meng53f3afb2015-10-07 20:19:13 -0700149 /*
150 * This should equate to
151 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
wdenk458ded32002-09-20 10:59:08 +0000152 * from the VxWorks BSP header files.
153 * This will vary from board to board
154 */
Tuomas Tynkkynenf66276d2018-01-21 18:16:42 +0200155#if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Bin Meng53f3afb2015-10-07 20:19:13 -0700156 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
Simon Glass399a9ce2017-08-03 12:22:14 -0600157 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger8bcee042009-02-11 18:51:43 -0500158 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000159#else
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000160 puts("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000161#endif
162
Bin Meng75f4b722018-04-11 22:02:22 -0700163#ifdef CONFIG_X86
164 /*
165 * Get VxWorks's physical memory base address from environment,
166 * if we don't specify it in the environment, use a default one.
167 */
168 base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
169 data = (struct e820_entry *)(base + E820_DATA_OFFSET);
170 info = (struct e820_info *)(base + E820_INFO_OFFSET);
171
172 memset(info, 0, sizeof(struct e820_info));
173 info->sign = E820_SIGNATURE;
174 info->entries = install_e820_map(E820MAX, data);
175 info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
176 E820_DATA_OFFSET;
177
178 /*
179 * Explicitly clear the bootloader image size otherwise if memory
180 * at this offset happens to contain some garbage data, the final
181 * available memory size for the kernel is insane.
182 */
183 *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
184
185 /*
186 * Prepare compatible framebuffer information block.
187 * The VESA mode has to be 32-bit RGBA.
188 */
189 if (vesa->x_resolution && vesa->y_resolution) {
190 gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET);
191 gop->magic = EFI_GOP_INFO_MAGIC;
192 gop->info.version = 0;
193 gop->info.width = vesa->x_resolution;
194 gop->info.height = vesa->y_resolution;
195 gop->info.pixel_format = EFI_GOT_RGBA8;
196 gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4;
197 gop->fb_base = vesa->phys_base_ptr;
198 gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution;
199 }
200#endif
201
wdenk57b2d802003-06-27 21:31:46 +0000202 /*
203 * Use bootaddr to find the location in memory that VxWorks
Bin Mengfb694b92015-10-07 20:19:17 -0700204 * will look for the bootline string. The default value is
205 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
206 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
wdenk458ded32002-09-20 10:59:08 +0000207 */
Simon Glass64b723f2017-08-03 12:22:12 -0600208 tmp = env_get("bootaddr");
Bin Mengfb694b92015-10-07 20:19:17 -0700209 if (!tmp) {
Bin Meng75f4b722018-04-11 22:02:22 -0700210#ifdef CONFIG_X86
211 bootaddr = base + X86_BOOT_LINE_OFFSET;
212#else
Bin Mengfb694b92015-10-07 20:19:17 -0700213 printf("## VxWorks bootline address not specified\n");
Bin Meng051f95e2018-04-11 22:02:21 -0700214 return 1;
Bin Meng75f4b722018-04-11 22:02:22 -0700215#endif
Bin Meng051f95e2018-04-11 22:02:21 -0700216 }
217
Bin Meng75f4b722018-04-11 22:02:22 -0700218 if (!bootaddr)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600219 bootaddr = hextoul(tmp, NULL);
Bin Meng051f95e2018-04-11 22:02:21 -0700220
221 /*
222 * Check to see if the bootline is defined in the 'bootargs' parameter.
223 * If it is not defined, we may be able to construct the info.
224 */
225 bootline = env_get("bootargs");
226 if (!bootline) {
227 tmp = env_get("bootdev");
228 if (tmp) {
229 strcpy(build_buf, tmp);
230 ptr = strlen(tmp);
231 } else {
232 printf("## VxWorks boot device not specified\n");
233 }
234
235 tmp = env_get("bootfile");
236 if (tmp)
237 ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
238 else
239 ptr += sprintf(build_buf + ptr, "host:vxWorks ");
wdenk458ded32002-09-20 10:59:08 +0000240
Bin Mengfb694b92015-10-07 20:19:17 -0700241 /*
Bin Meng051f95e2018-04-11 22:02:21 -0700242 * The following parameters are only needed if 'bootdev'
243 * is an ethernet device, otherwise they are optional.
Bin Mengfb694b92015-10-07 20:19:17 -0700244 */
Bin Meng051f95e2018-04-11 22:02:21 -0700245 tmp = env_get("ipaddr");
246 if (tmp) {
247 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
248 tmp = env_get("netmask");
Ben Whitten34fd6c92015-12-30 13:05:58 +0000249 if (tmp) {
Bin Meng051f95e2018-04-11 22:02:21 -0700250 u32 mask = env_get_ip("netmask").s_addr;
Bin Mengfb694b92015-10-07 20:19:17 -0700251 ptr += sprintf(build_buf + ptr,
Bin Meng051f95e2018-04-11 22:02:21 -0700252 ":%08x ", ntohl(mask));
253 } else {
254 ptr += sprintf(build_buf + ptr, " ");
Bin Mengbf0ba252015-10-07 20:19:16 -0700255 }
Bin Meng051f95e2018-04-11 22:02:21 -0700256 }
wdenk458ded32002-09-20 10:59:08 +0000257
Bin Meng051f95e2018-04-11 22:02:21 -0700258 tmp = env_get("serverip");
259 if (tmp)
260 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000261
Bin Meng051f95e2018-04-11 22:02:21 -0700262 tmp = env_get("gatewayip");
263 if (tmp)
264 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
Bin Mengbf0ba252015-10-07 20:19:16 -0700265
Bin Meng051f95e2018-04-11 22:02:21 -0700266 tmp = env_get("hostname");
267 if (tmp)
268 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000269
Bin Meng051f95e2018-04-11 22:02:21 -0700270 tmp = env_get("othbootargs");
271 if (tmp) {
272 strcpy(build_buf + ptr, tmp);
273 ptr += strlen(tmp);
Bin Mengfb694b92015-10-07 20:19:17 -0700274 }
275
Bin Meng051f95e2018-04-11 22:02:21 -0700276 bootline = build_buf;
wdenk458ded32002-09-20 10:59:08 +0000277 }
278
Bin Meng051f95e2018-04-11 22:02:21 -0700279 memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255));
280 flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
281 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr);
282
wdenk57b2d802003-06-27 21:31:46 +0000283 /*
284 * If the data at the load address is an elf image, then
285 * treat it like an elf image. Otherwise, assume that it is a
Bin Meng53f3afb2015-10-07 20:19:13 -0700286 * binary image.
wdenk458ded32002-09-20 10:59:08 +0000287 */
Bin Meng53f3afb2015-10-07 20:19:13 -0700288 if (valid_elf_image(addr))
Christian Gmeinerae7d4be2018-03-20 14:18:25 +0100289 addr = load_elf_image_phdr(addr);
Bin Meng53f3afb2015-10-07 20:19:13 -0700290 else
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000291 puts("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000292
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000293 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
Pali Rohár937b7d82022-09-05 11:31:21 +0200294 flush();
wdenk458ded32002-09-20 10:59:08 +0000295
Reinhard Arlt4fa117c2011-11-18 09:06:52 +0000296 dcache_disable();
Vasyl Vavrychuk70b551c2018-04-10 12:36:36 +0300297#if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
298 armv8_setup_psci();
299 smp_kick_all_cpus();
300#endif
301
Bin Mengf6900e22015-10-07 20:19:19 -0700302#ifdef CONFIG_X86
303 /* VxWorks on x86 uses stack to pass parameters */
304 ((asmlinkage void (*)(int))addr)(0);
305#else
Bin Meng53f3afb2015-10-07 20:19:13 -0700306 ((void (*)(int))addr)(0);
Bin Mengf6900e22015-10-07 20:19:19 -0700307#endif
wdenk458ded32002-09-20 10:59:08 +0000308
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000309 puts("## vxWorks terminated\n");
Bin Meng53f3afb2015-10-07 20:19:13 -0700310
wdenk458ded32002-09-20 10:59:08 +0000311 return 1;
312}
Daniel Palmer6218cf52024-09-29 18:27:39 +0900313#endif
wdenk458ded32002-09-20 10:59:08 +0000314
wdenkf287a242003-07-01 21:06:45 +0000315U_BOOT_CMD(
Tom Rini3c95c792017-05-18 17:03:07 -0400316 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600317 "Boot from an ELF image in memory",
Maxim Moskalets938f0112024-03-08 00:29:14 +0300318 "[-p|-s] "
319#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
320 "[-d fdt_addr_r] "
321#endif
322 "[address]\n"
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400323 "\t- load ELF image at [address] via program headers (-p)\n"
Maxim Moskalets938f0112024-03-08 00:29:14 +0300324 "\t or via section headers (-s)\n"
325#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
326 "\t- setup FDT image at [fdt_addr_r] (-d)"
327#endif
wdenk57b2d802003-06-27 21:31:46 +0000328);
329
Daniel Palmer6218cf52024-09-29 18:27:39 +0900330#ifdef CONFIG_CMD_ELF_BOOTVX
wdenkf287a242003-07-01 21:06:45 +0000331U_BOOT_CMD(
Bin Meng53f3afb2015-10-07 20:19:13 -0700332 bootvx, 2, 0, do_bootvx,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600333 "Boot vxWorks from an ELF image",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200334 " [address] - load address of vxWorks ELF image."
wdenk57b2d802003-06-27 21:31:46 +0000335);
Daniel Palmer6218cf52024-09-29 18:27:39 +0900336#endif