blob: 10e8264d3c7d14e5e09ed5ed3511848b4e7458ee [file] [log] [blame]
Tom Rini89789eb2020-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 Glass62270f42019-11-14 12:57:35 -07009#include <cpu_func.h>
wdenk458ded32002-09-20 10:59:08 +000010#include <elf.h>
Simon Glasscdbff9f2019-08-01 09:46:50 -060011#include <env.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070012#include <image.h>
Bin Mengebca3df2015-10-07 20:19:13 -070013#include <net.h>
Niklaus Giger29a4c242008-11-03 22:15:34 +010014#include <vxworks.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070015#ifdef CONFIG_X86
Bin Meng447ae4f2018-04-11 22:02:19 -070016#include <vbe.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070017#include <asm/e820.h>
Bin Meng9aa12802015-10-07 20:19:19 -070018#include <linux/linkage.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070019#endif
wdenk458ded32002-09-20 10:59:08 +000020
Mike Frysinger017e9b72008-04-13 19:42:18 -040021/* Allow ports to override the default behavior */
Jeroen Hofstee553d8c32014-10-08 22:57:31 +020022static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
Bin Mengebca3df2015-10-07 20:19:13 -070023 int argc, char * const argv[])
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050024{
Mike Frysinger017e9b72008-04-13 19:42:18 -040025 unsigned long ret;
26
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050027 /*
Mike Frysinger017e9b72008-04-13 19:42:18 -040028 * pass address parameter as argv[0] (aka command name),
29 * and all remaining args
30 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000031 ret = entry(argc, argv);
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050032
Mike Frysinger017e9b72008-04-13 19:42:18 -040033 return ret;
34}
wdenk458ded32002-09-20 10:59:08 +000035
Bin Mengebca3df2015-10-07 20:19:13 -070036/* Interpreter command to boot an arbitrary ELF image from memory */
Daniel Schwierzeck62e03d32012-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 Mengebca3df2015-10-07 20:19:13 -070039 unsigned long addr; /* Address of the ELF image */
40 unsigned long rc; /* Return value from user code */
Tom Rinibe1b8672017-05-18 17:03:07 -040041 char *sload = NULL;
Simon Glass00caae62017-08-03 12:22:12 -060042 const char *ep = env_get("autostart");
wdenk458ded32002-09-20 10:59:08 +000043 int rcode = 0;
44
Tom Rinibe1b8672017-05-18 17:03:07 -040045 /* Consume 'bootelf' */
46 argc--; argv++;
Mike Frysingerf44a9282010-10-02 15:44:53 -040047
Tom Rinibe1b8672017-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 Glassbb872dd2019-12-28 10:45:02 -070060 addr = image_load_addr;
wdenk458ded32002-09-20 10:59:08 +000061
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000062 if (!valid_elf_image(addr))
wdenk458ded32002-09-20 10:59:08 +000063 return 1;
64
Mike Frysingerf44a9282010-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 Paladugu44c8fd32015-03-09 12:46:47 +010070 if (ep && !strcmp(ep, "no"))
71 return rcode;
72
Daniel Schwierzeck62e03d32012-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 Rinibe1b8672017-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 Schwierzeck62e03d32012-09-16 06:55:05 +000083 printf("## Application terminated, rc = 0x%lx\n", rc);
Bin Mengebca3df2015-10-07 20:19:13 -070084
wdenk458ded32002-09-20 10:59:08 +000085 return rcode;
86}
87
Bin Mengebca3df2015-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 Mengebca3df2015-10-07 20:19:13 -070092 */
Daniel Schwierzeck62e03d32012-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 Mengebca3df2015-10-07 20:19:13 -070095 unsigned long addr; /* Address of image */
Bin Meng79c584e2018-04-11 22:02:22 -070096 unsigned long bootaddr = 0; /* Address to put the bootline */
Bin Mengebca3df2015-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 Meng7f0c3c52015-10-07 20:19:15 -0700100 int ptr = 0;
Bin Mengb90ff0f2015-10-07 20:19:18 -0700101#ifdef CONFIG_X86
Bin Meng2902be82018-04-11 22:02:07 -0700102 ulong base;
Bin Mengfa5e91f2018-04-11 22:02:09 -0700103 struct e820_info *info;
Bin Meng45519922018-04-11 22:02:11 -0700104 struct e820_entry *data;
Bin Meng447ae4f2018-04-11 22:02:19 -0700105 struct efi_gop_info *gop;
106 struct vesa_mode_info *vesa = &mode_info.vesa;
Bin Mengb90ff0f2015-10-07 20:19:18 -0700107#endif
wdenk458ded32002-09-20 10:59:08 +0000108
Bin Mengebca3df2015-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 MARCEL07a1a0c2013-11-27 14:48:43 +0100113 if (argc < 2)
Simon Glassbb872dd2019-12-28 10:45:02 -0700114 addr = image_load_addr;
Stefan Roese1bdd4682006-11-29 12:53:15 +0100115 else
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100116 addr = simple_strtoul(argv[1], NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000117
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500118#if defined(CONFIG_CMD_NET)
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000119 /*
Bin Mengebca3df2015-10-07 20:19:13 -0700120 * Check to see if we need to tftp the image ourselves
121 * before starting
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000122 */
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100123 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500124 if (net_loop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000125 return 1;
Daniel Schwierzeck62e03d32012-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 Mengebca3df2015-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 Tynkkynen89969752018-01-21 18:16:42 +0200137#if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Bin Mengebca3df2015-10-07 20:19:13 -0700138 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
Simon Glass35affd72017-08-03 12:22:14 -0600139 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger62c93d92009-02-11 18:51:43 -0500140 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000141#else
Daniel Schwierzeck62e03d32012-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 Meng79c584e2018-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
wdenk8bde7f72003-06-27 21:31:46 +0000184 /*
185 * Use bootaddr to find the location in memory that VxWorks
Bin Meng9e98b7e2015-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 Glass00caae62017-08-03 12:22:12 -0600190 tmp = env_get("bootaddr");
Bin Meng9e98b7e2015-10-07 20:19:17 -0700191 if (!tmp) {
Bin Meng79c584e2018-04-11 22:02:22 -0700192#ifdef CONFIG_X86
193 bootaddr = base + X86_BOOT_LINE_OFFSET;
194#else
Bin Meng9e98b7e2015-10-07 20:19:17 -0700195 printf("## VxWorks bootline address not specified\n");
Bin Mengced71a22018-04-11 22:02:21 -0700196 return 1;
Bin Meng79c584e2018-04-11 22:02:22 -0700197#endif
Bin Mengced71a22018-04-11 22:02:21 -0700198 }
wdenk458ded32002-09-20 10:59:08 +0000199
Bin Meng79c584e2018-04-11 22:02:22 -0700200 if (!bootaddr)
201 bootaddr = simple_strtoul(tmp, NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000202
Bin Mengced71a22018-04-11 22:02:21 -0700203 /*
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");
Bin Menga4092db2015-10-07 20:19:16 -0700215 }
Niklaus Giger29a4c242008-11-03 22:15:34 +0100216
Bin Mengced71a22018-04-11 22:02:21 -0700217 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 ");
222
223 /*
224 * The following parameters are only needed if 'bootdev'
225 * is an ethernet device, otherwise they are optional.
226 */
227 tmp = env_get("ipaddr");
228 if (tmp) {
229 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
230 tmp = env_get("netmask");
231 if (tmp) {
232 u32 mask = env_get_ip("netmask").s_addr;
233 ptr += sprintf(build_buf + ptr,
234 ":%08x ", ntohl(mask));
235 } else {
236 ptr += sprintf(build_buf + ptr, " ");
237 }
238 }
239
240 tmp = env_get("serverip");
241 if (tmp)
242 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
243
244 tmp = env_get("gatewayip");
245 if (tmp)
246 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
247
248 tmp = env_get("hostname");
249 if (tmp)
250 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
251
252 tmp = env_get("othbootargs");
253 if (tmp) {
254 strcpy(build_buf + ptr, tmp);
255 ptr += strlen(tmp);
256 }
257
258 bootline = build_buf;
wdenk458ded32002-09-20 10:59:08 +0000259 }
260
Bin Mengced71a22018-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
wdenk8bde7f72003-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 Mengebca3df2015-10-07 20:19:13 -0700268 * binary image.
wdenk458ded32002-09-20 10:59:08 +0000269 */
Bin Mengebca3df2015-10-07 20:19:13 -0700270 if (valid_elf_image(addr))
Christian Gmeiner476c2fc2018-03-20 14:18:25 +0100271 addr = load_elf_image_phdr(addr);
Bin Mengebca3df2015-10-07 20:19:13 -0700272 else
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000273 puts("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000274
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000275 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000276
Reinhard Arlt6eee21d2011-11-18 09:06:52 +0000277 dcache_disable();
Vasyl Vavrychuk3194daa2018-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 Meng9aa12802015-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 Mengebca3df2015-10-07 20:19:13 -0700287 ((void (*)(int))addr)(0);
Bin Meng9aa12802015-10-07 20:19:19 -0700288#endif
wdenk458ded32002-09-20 10:59:08 +0000289
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000290 puts("## vxWorks terminated\n");
Bin Mengebca3df2015-10-07 20:19:13 -0700291
wdenk458ded32002-09-20 10:59:08 +0000292 return 1;
293}
294
wdenk0d498392003-07-01 21:06:45 +0000295U_BOOT_CMD(
Tom Rinibe1b8672017-05-18 17:03:07 -0400296 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
Peter Tyser2fb26042009-01-27 18:03:12 -0600297 "Boot from an ELF image in memory",
Mike Frysingerf44a9282010-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)"
wdenk8bde7f72003-06-27 21:31:46 +0000301);
302
wdenk0d498392003-07-01 21:06:45 +0000303U_BOOT_CMD(
Bin Mengebca3df2015-10-07 20:19:13 -0700304 bootvx, 2, 0, do_bootvx,
Peter Tyser2fb26042009-01-27 18:03:12 -0600305 "Boot vxWorks from an ELF image",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200306 " [address] - load address of vxWorks ELF image."
wdenk8bde7f72003-06-27 21:31:46 +0000307);