blob: 10e8264d3c7d14e5e09ed5ed3511848b4e7458ee [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
7#include <common.h>
8#include <command.h>
Simon Glass370382c2019-11-14 12:57:35 -07009#include <cpu_func.h>
wdenk458ded32002-09-20 10:59:08 +000010#include <elf.h>
Simon Glass83c2e492019-08-01 09:46:50 -060011#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070012#include <image.h>
Bin Meng53f3afb2015-10-07 20:19:13 -070013#include <net.h>
Niklaus Gigerf662b232008-11-03 22:15:34 +010014#include <vxworks.h>
Bin Meng321e8602015-10-07 20:19:18 -070015#ifdef CONFIG_X86
Bin Meng71606c42018-04-11 22:02:19 -070016#include <vbe.h>
Bin Meng321e8602015-10-07 20:19:18 -070017#include <asm/e820.h>
Bin Mengf6900e22015-10-07 20:19:19 -070018#include <linux/linkage.h>
Bin Meng321e8602015-10-07 20:19:18 -070019#endif
wdenk458ded32002-09-20 10:59:08 +000020
Mike Frysingere60644a2008-04-13 19:42:18 -040021/* Allow ports to override the default behavior */
Jeroen Hofstee3cc2a742014-10-08 22:57:31 +020022static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
Bin Meng53f3afb2015-10-07 20:19:13 -070023 int argc, char * const argv[])
Mike Frysingerf79507e2008-01-29 18:21:05 -050024{
Mike Frysingere60644a2008-04-13 19:42:18 -040025 unsigned long ret;
26
Mike Frysingerf79507e2008-01-29 18:21:05 -050027 /*
Mike Frysingere60644a2008-04-13 19:42:18 -040028 * pass address parameter as argv[0] (aka command name),
29 * and all remaining args
30 */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000031 ret = entry(argc, argv);
Mike Frysingerf79507e2008-01-29 18:21:05 -050032
Mike Frysingere60644a2008-04-13 19:42:18 -040033 return ret;
34}
wdenk458ded32002-09-20 10:59:08 +000035
Bin Meng53f3afb2015-10-07 20:19:13 -070036/* Interpreter command to boot an arbitrary ELF image from memory */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000037int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +000038{
Bin Meng53f3afb2015-10-07 20:19:13 -070039 unsigned long addr; /* Address of the ELF image */
40 unsigned long rc; /* Return value from user code */
Tom Rini3c95c792017-05-18 17:03:07 -040041 char *sload = NULL;
Simon Glass64b723f2017-08-03 12:22:12 -060042 const char *ep = env_get("autostart");
wdenk458ded32002-09-20 10:59:08 +000043 int rcode = 0;
44
Tom Rini3c95c792017-05-18 17:03:07 -040045 /* Consume 'bootelf' */
46 argc--; argv++;
Mike Frysingere6dc29f2010-10-02 15:44:53 -040047
Tom Rini3c95c792017-05-18 17:03:07 -040048 /* Check for flag. */
49 if (argc >= 1 && (argv[0][0] == '-' && \
50 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
51 sload = argv[0];
52 /* Consume flag. */
53 argc--; argv++;
54 }
55 /* Check for address. */
56 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
57 /* Consume address */
58 argc--; argv++;
59 } else
Simon Glass892265d2019-12-28 10:45:02 -070060 addr = image_load_addr;
wdenk458ded32002-09-20 10:59:08 +000061
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000062 if (!valid_elf_image(addr))
wdenk458ded32002-09-20 10:59:08 +000063 return 1;
64
Mike Frysingere6dc29f2010-10-02 15:44:53 -040065 if (sload && sload[1] == 'p')
66 addr = load_elf_image_phdr(addr);
67 else
68 addr = load_elf_image_shdr(addr);
wdenk458ded32002-09-20 10:59:08 +000069
Siva Durga Prasad Paladugu3825bfa2015-03-09 12:46:47 +010070 if (ep && !strcmp(ep, "no"))
71 return rcode;
72
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000073 printf("## Starting application at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +000074
wdenk458ded32002-09-20 10:59:08 +000075 /*
76 * pass address parameter as argv[0] (aka command name),
77 * and all remaining args
78 */
Tom Rini3c95c792017-05-18 17:03:07 -040079 rc = do_bootelf_exec((void *)addr, argc, argv);
wdenk458ded32002-09-20 10:59:08 +000080 if (rc != 0)
81 rcode = 1;
82
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000083 printf("## Application terminated, rc = 0x%lx\n", rc);
Bin Meng53f3afb2015-10-07 20:19:13 -070084
wdenk458ded32002-09-20 10:59:08 +000085 return rcode;
86}
87
Bin Meng53f3afb2015-10-07 20:19:13 -070088/*
wdenk458ded32002-09-20 10:59:08 +000089 * Interpreter command to boot VxWorks from a memory image. The image can
90 * be either an ELF image or a raw binary. Will attempt to setup the
91 * bootline and other parameters correctly.
Bin Meng53f3afb2015-10-07 20:19:13 -070092 */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000093int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +000094{
Bin Meng53f3afb2015-10-07 20:19:13 -070095 unsigned long addr; /* Address of image */
Bin Meng75f4b722018-04-11 22:02:22 -070096 unsigned long bootaddr = 0; /* Address to put the bootline */
Bin Meng53f3afb2015-10-07 20:19:13 -070097 char *bootline; /* Text of the bootline */
98 char *tmp; /* Temporary char pointer */
99 char build_buf[128]; /* Buffer for building the bootline */
Bin Meng0401c862015-10-07 20:19:15 -0700100 int ptr = 0;
Bin Meng321e8602015-10-07 20:19:18 -0700101#ifdef CONFIG_X86
Bin Menge3be8902018-04-11 22:02:07 -0700102 ulong base;
Bin Mengdde4ebb2018-04-11 22:02:09 -0700103 struct e820_info *info;
Bin Meng4b8fc742018-04-11 22:02:11 -0700104 struct e820_entry *data;
Bin Meng71606c42018-04-11 22:02:19 -0700105 struct efi_gop_info *gop;
106 struct vesa_mode_info *vesa = &mode_info.vesa;
Bin Meng321e8602015-10-07 20:19:18 -0700107#endif
wdenk458ded32002-09-20 10:59:08 +0000108
Bin Meng53f3afb2015-10-07 20:19:13 -0700109 /*
wdenk458ded32002-09-20 10:59:08 +0000110 * Check the loadaddr variable.
111 * If we don't know where the image is then we're done.
112 */
Stany MARCELe5deaf582013-11-27 14:48:43 +0100113 if (argc < 2)
Simon Glass892265d2019-12-28 10:45:02 -0700114 addr = image_load_addr;
Stefan Roese00487782006-11-29 12:53:15 +0100115 else
Stany MARCELe5deaf582013-11-27 14:48:43 +0100116 addr = simple_strtoul(argv[1], NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000117
Jon Loeliger54324d02007-07-08 17:51:39 -0500118#if defined(CONFIG_CMD_NET)
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000119 /*
Bin Meng53f3afb2015-10-07 20:19:13 -0700120 * Check to see if we need to tftp the image ourselves
121 * before starting
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000122 */
Stany MARCELe5deaf582013-11-27 14:48:43 +0100123 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500124 if (net_loop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000125 return 1;
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000126 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
127 addr);
wdenk458ded32002-09-20 10:59:08 +0000128 }
129#endif
130
Bin Meng53f3afb2015-10-07 20:19:13 -0700131 /*
132 * This should equate to
133 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
wdenk458ded32002-09-20 10:59:08 +0000134 * from the VxWorks BSP header files.
135 * This will vary from board to board
136 */
Tuomas Tynkkynenf66276d2018-01-21 18:16:42 +0200137#if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Bin Meng53f3afb2015-10-07 20:19:13 -0700138 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
Simon Glass399a9ce2017-08-03 12:22:14 -0600139 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger8bcee042009-02-11 18:51:43 -0500140 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000141#else
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000142 puts("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000143#endif
144
Bin Meng75f4b722018-04-11 22:02:22 -0700145#ifdef CONFIG_X86
146 /*
147 * Get VxWorks's physical memory base address from environment,
148 * if we don't specify it in the environment, use a default one.
149 */
150 base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
151 data = (struct e820_entry *)(base + E820_DATA_OFFSET);
152 info = (struct e820_info *)(base + E820_INFO_OFFSET);
153
154 memset(info, 0, sizeof(struct e820_info));
155 info->sign = E820_SIGNATURE;
156 info->entries = install_e820_map(E820MAX, data);
157 info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
158 E820_DATA_OFFSET;
159
160 /*
161 * Explicitly clear the bootloader image size otherwise if memory
162 * at this offset happens to contain some garbage data, the final
163 * available memory size for the kernel is insane.
164 */
165 *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
166
167 /*
168 * Prepare compatible framebuffer information block.
169 * The VESA mode has to be 32-bit RGBA.
170 */
171 if (vesa->x_resolution && vesa->y_resolution) {
172 gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET);
173 gop->magic = EFI_GOP_INFO_MAGIC;
174 gop->info.version = 0;
175 gop->info.width = vesa->x_resolution;
176 gop->info.height = vesa->y_resolution;
177 gop->info.pixel_format = EFI_GOT_RGBA8;
178 gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4;
179 gop->fb_base = vesa->phys_base_ptr;
180 gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution;
181 }
182#endif
183
wdenk57b2d802003-06-27 21:31:46 +0000184 /*
185 * Use bootaddr to find the location in memory that VxWorks
Bin Mengfb694b92015-10-07 20:19:17 -0700186 * will look for the bootline string. The default value is
187 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
188 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
wdenk458ded32002-09-20 10:59:08 +0000189 */
Simon Glass64b723f2017-08-03 12:22:12 -0600190 tmp = env_get("bootaddr");
Bin Mengfb694b92015-10-07 20:19:17 -0700191 if (!tmp) {
Bin Meng75f4b722018-04-11 22:02:22 -0700192#ifdef CONFIG_X86
193 bootaddr = base + X86_BOOT_LINE_OFFSET;
194#else
Bin Mengfb694b92015-10-07 20:19:17 -0700195 printf("## VxWorks bootline address not specified\n");
Bin Meng051f95e2018-04-11 22:02:21 -0700196 return 1;
Bin Meng75f4b722018-04-11 22:02:22 -0700197#endif
Bin Meng051f95e2018-04-11 22:02:21 -0700198 }
199
Bin Meng75f4b722018-04-11 22:02:22 -0700200 if (!bootaddr)
201 bootaddr = simple_strtoul(tmp, NULL, 16);
Bin Meng051f95e2018-04-11 22:02:21 -0700202
203 /*
204 * Check to see if the bootline is defined in the 'bootargs' parameter.
205 * If it is not defined, we may be able to construct the info.
206 */
207 bootline = env_get("bootargs");
208 if (!bootline) {
209 tmp = env_get("bootdev");
210 if (tmp) {
211 strcpy(build_buf, tmp);
212 ptr = strlen(tmp);
213 } else {
214 printf("## VxWorks boot device not specified\n");
215 }
216
217 tmp = env_get("bootfile");
218 if (tmp)
219 ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
220 else
221 ptr += sprintf(build_buf + ptr, "host:vxWorks ");
wdenk458ded32002-09-20 10:59:08 +0000222
Bin Mengfb694b92015-10-07 20:19:17 -0700223 /*
Bin Meng051f95e2018-04-11 22:02:21 -0700224 * The following parameters are only needed if 'bootdev'
225 * is an ethernet device, otherwise they are optional.
Bin Mengfb694b92015-10-07 20:19:17 -0700226 */
Bin Meng051f95e2018-04-11 22:02:21 -0700227 tmp = env_get("ipaddr");
228 if (tmp) {
229 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
230 tmp = env_get("netmask");
Ben Whitten34fd6c92015-12-30 13:05:58 +0000231 if (tmp) {
Bin Meng051f95e2018-04-11 22:02:21 -0700232 u32 mask = env_get_ip("netmask").s_addr;
Bin Mengfb694b92015-10-07 20:19:17 -0700233 ptr += sprintf(build_buf + ptr,
Bin Meng051f95e2018-04-11 22:02:21 -0700234 ":%08x ", ntohl(mask));
235 } else {
236 ptr += sprintf(build_buf + ptr, " ");
Bin Mengbf0ba252015-10-07 20:19:16 -0700237 }
Bin Meng051f95e2018-04-11 22:02:21 -0700238 }
wdenk458ded32002-09-20 10:59:08 +0000239
Bin Meng051f95e2018-04-11 22:02:21 -0700240 tmp = env_get("serverip");
241 if (tmp)
242 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000243
Bin Meng051f95e2018-04-11 22:02:21 -0700244 tmp = env_get("gatewayip");
245 if (tmp)
246 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
Bin Mengbf0ba252015-10-07 20:19:16 -0700247
Bin Meng051f95e2018-04-11 22:02:21 -0700248 tmp = env_get("hostname");
249 if (tmp)
250 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000251
Bin Meng051f95e2018-04-11 22:02:21 -0700252 tmp = env_get("othbootargs");
253 if (tmp) {
254 strcpy(build_buf + ptr, tmp);
255 ptr += strlen(tmp);
Bin Mengfb694b92015-10-07 20:19:17 -0700256 }
257
Bin Meng051f95e2018-04-11 22:02:21 -0700258 bootline = build_buf;
wdenk458ded32002-09-20 10:59:08 +0000259 }
260
Bin Meng051f95e2018-04-11 22:02:21 -0700261 memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255));
262 flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
263 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr);
264
wdenk57b2d802003-06-27 21:31:46 +0000265 /*
266 * If the data at the load address is an elf image, then
267 * treat it like an elf image. Otherwise, assume that it is a
Bin Meng53f3afb2015-10-07 20:19:13 -0700268 * binary image.
wdenk458ded32002-09-20 10:59:08 +0000269 */
Bin Meng53f3afb2015-10-07 20:19:13 -0700270 if (valid_elf_image(addr))
Christian Gmeinerae7d4be2018-03-20 14:18:25 +0100271 addr = load_elf_image_phdr(addr);
Bin Meng53f3afb2015-10-07 20:19:13 -0700272 else
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000273 puts("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000274
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000275 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000276
Reinhard Arlt4fa117c2011-11-18 09:06:52 +0000277 dcache_disable();
Vasyl Vavrychuk70b551c2018-04-10 12:36:36 +0300278#if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
279 armv8_setup_psci();
280 smp_kick_all_cpus();
281#endif
282
Bin Mengf6900e22015-10-07 20:19:19 -0700283#ifdef CONFIG_X86
284 /* VxWorks on x86 uses stack to pass parameters */
285 ((asmlinkage void (*)(int))addr)(0);
286#else
Bin Meng53f3afb2015-10-07 20:19:13 -0700287 ((void (*)(int))addr)(0);
Bin Mengf6900e22015-10-07 20:19:19 -0700288#endif
wdenk458ded32002-09-20 10:59:08 +0000289
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000290 puts("## vxWorks terminated\n");
Bin Meng53f3afb2015-10-07 20:19:13 -0700291
wdenk458ded32002-09-20 10:59:08 +0000292 return 1;
293}
294
wdenkf287a242003-07-01 21:06:45 +0000295U_BOOT_CMD(
Tom Rini3c95c792017-05-18 17:03:07 -0400296 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600297 "Boot from an ELF image in memory",
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400298 "[-p|-s] [address]\n"
299 "\t- load ELF image at [address] via program headers (-p)\n"
300 "\t or via section headers (-s)"
wdenk57b2d802003-06-27 21:31:46 +0000301);
302
wdenkf287a242003-07-01 21:06:45 +0000303U_BOOT_CMD(
Bin Meng53f3afb2015-10-07 20:19:13 -0700304 bootvx, 2, 0, do_bootvx,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600305 "Boot vxWorks from an ELF image",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200306 " [address] - load address of vxWorks ELF image."
wdenk57b2d802003-06-27 21:31:46 +0000307);