blob: d75b21461c2096f5b4deb1d9211d4c485947ed22 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Bin Mengebca3df2015-10-07 20:19:13 -070014#include <net.h>
Niklaus Giger29a4c242008-11-03 22:15:34 +010015#include <vxworks.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070016#ifdef CONFIG_X86
Bin Meng447ae4f2018-04-11 22:02:19 -070017#include <vbe.h>
Simon Glass90526e92020-05-10 11:39:56 -060018#include <asm/cache.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070019#include <asm/e820.h>
Bin Meng9aa12802015-10-07 20:19:19 -070020#include <linux/linkage.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070021#endif
wdenk458ded32002-09-20 10:59:08 +000022
Mike Frysinger017e9b72008-04-13 19:42:18 -040023/* Allow ports to override the default behavior */
Jeroen Hofstee553d8c32014-10-08 22:57:31 +020024static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
Simon Glass09140112020-05-10 11:40:03 -060025 int argc, char *const argv[])
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050026{
Mike Frysinger017e9b72008-04-13 19:42:18 -040027 unsigned long ret;
28
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050029 /*
Mike Frysinger017e9b72008-04-13 19:42:18 -040030 * pass address parameter as argv[0] (aka command name),
31 * and all remaining args
32 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000033 ret = entry(argc, argv);
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050034
Mike Frysinger017e9b72008-04-13 19:42:18 -040035 return ret;
36}
wdenk458ded32002-09-20 10:59:08 +000037
Bin Mengebca3df2015-10-07 20:19:13 -070038/* Interpreter command to boot an arbitrary ELF image from memory */
Simon Glass09140112020-05-10 11:40:03 -060039int do_bootelf(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk458ded32002-09-20 10:59:08 +000040{
Bin Mengebca3df2015-10-07 20:19:13 -070041 unsigned long addr; /* Address of the ELF image */
42 unsigned long rc; /* Return value from user code */
Tom Rinibe1b8672017-05-18 17:03:07 -040043 char *sload = NULL;
Simon Glass00caae62017-08-03 12:22:12 -060044 const char *ep = env_get("autostart");
wdenk458ded32002-09-20 10:59:08 +000045 int rcode = 0;
46
Tom Rinibe1b8672017-05-18 17:03:07 -040047 /* Consume 'bootelf' */
48 argc--; argv++;
Mike Frysingerf44a9282010-10-02 15:44:53 -040049
Tom Rinibe1b8672017-05-18 17:03:07 -040050 /* Check for flag. */
51 if (argc >= 1 && (argv[0][0] == '-' && \
52 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
53 sload = argv[0];
54 /* Consume flag. */
55 argc--; argv++;
56 }
57 /* Check for address. */
58 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
59 /* Consume address */
60 argc--; argv++;
61 } else
Simon Glassbb872dd2019-12-28 10:45:02 -070062 addr = image_load_addr;
wdenk458ded32002-09-20 10:59:08 +000063
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000064 if (!valid_elf_image(addr))
wdenk458ded32002-09-20 10:59:08 +000065 return 1;
66
Mike Frysingerf44a9282010-10-02 15:44:53 -040067 if (sload && sload[1] == 'p')
68 addr = load_elf_image_phdr(addr);
69 else
70 addr = load_elf_image_shdr(addr);
wdenk458ded32002-09-20 10:59:08 +000071
Siva Durga Prasad Paladugu44c8fd32015-03-09 12:46:47 +010072 if (ep && !strcmp(ep, "no"))
73 return rcode;
74
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000075 printf("## Starting application at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +000076
wdenk458ded32002-09-20 10:59:08 +000077 /*
78 * pass address parameter as argv[0] (aka command name),
79 * and all remaining args
80 */
Tom Rinibe1b8672017-05-18 17:03:07 -040081 rc = do_bootelf_exec((void *)addr, argc, argv);
wdenk458ded32002-09-20 10:59:08 +000082 if (rc != 0)
83 rcode = 1;
84
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000085 printf("## Application terminated, rc = 0x%lx\n", rc);
Bin Mengebca3df2015-10-07 20:19:13 -070086
wdenk458ded32002-09-20 10:59:08 +000087 return rcode;
88}
89
Bin Mengebca3df2015-10-07 20:19:13 -070090/*
wdenk458ded32002-09-20 10:59:08 +000091 * Interpreter command to boot VxWorks from a memory image. The image can
92 * be either an ELF image or a raw binary. Will attempt to setup the
93 * bootline and other parameters correctly.
Bin Mengebca3df2015-10-07 20:19:13 -070094 */
Simon Glass09140112020-05-10 11:40:03 -060095int do_bootvx(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk458ded32002-09-20 10:59:08 +000096{
Bin Mengebca3df2015-10-07 20:19:13 -070097 unsigned long addr; /* Address of image */
Bin Meng79c584e2018-04-11 22:02:22 -070098 unsigned long bootaddr = 0; /* Address to put the bootline */
Bin Mengebca3df2015-10-07 20:19:13 -070099 char *bootline; /* Text of the bootline */
100 char *tmp; /* Temporary char pointer */
101 char build_buf[128]; /* Buffer for building the bootline */
Bin Meng7f0c3c52015-10-07 20:19:15 -0700102 int ptr = 0;
Bin Mengb90ff0f2015-10-07 20:19:18 -0700103#ifdef CONFIG_X86
Bin Meng2902be82018-04-11 22:02:07 -0700104 ulong base;
Bin Mengfa5e91f2018-04-11 22:02:09 -0700105 struct e820_info *info;
Bin Meng45519922018-04-11 22:02:11 -0700106 struct e820_entry *data;
Bin Meng447ae4f2018-04-11 22:02:19 -0700107 struct efi_gop_info *gop;
108 struct vesa_mode_info *vesa = &mode_info.vesa;
Bin Mengb90ff0f2015-10-07 20:19:18 -0700109#endif
wdenk458ded32002-09-20 10:59:08 +0000110
Bin Mengebca3df2015-10-07 20:19:13 -0700111 /*
wdenk458ded32002-09-20 10:59:08 +0000112 * Check the loadaddr variable.
113 * If we don't know where the image is then we're done.
114 */
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100115 if (argc < 2)
Simon Glassbb872dd2019-12-28 10:45:02 -0700116 addr = image_load_addr;
Stefan Roese1bdd4682006-11-29 12:53:15 +0100117 else
Simon Glass7e5f4602021-07-24 09:03:29 -0600118 addr = hextoul(argv[1], NULL);
wdenk458ded32002-09-20 10:59:08 +0000119
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500120#if defined(CONFIG_CMD_NET)
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000121 /*
Bin Mengebca3df2015-10-07 20:19:13 -0700122 * Check to see if we need to tftp the image ourselves
123 * before starting
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000124 */
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100125 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500126 if (net_loop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000127 return 1;
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000128 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
129 addr);
wdenk458ded32002-09-20 10:59:08 +0000130 }
131#endif
132
Bin Mengebca3df2015-10-07 20:19:13 -0700133 /*
134 * This should equate to
135 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
wdenk458ded32002-09-20 10:59:08 +0000136 * from the VxWorks BSP header files.
137 * This will vary from board to board
138 */
Tuomas Tynkkynen89969752018-01-21 18:16:42 +0200139#if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Bin Mengebca3df2015-10-07 20:19:13 -0700140 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
Simon Glass35affd72017-08-03 12:22:14 -0600141 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger62c93d92009-02-11 18:51:43 -0500142 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000143#else
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000144 puts("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000145#endif
146
Bin Meng79c584e2018-04-11 22:02:22 -0700147#ifdef CONFIG_X86
148 /*
149 * Get VxWorks's physical memory base address from environment,
150 * if we don't specify it in the environment, use a default one.
151 */
152 base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
153 data = (struct e820_entry *)(base + E820_DATA_OFFSET);
154 info = (struct e820_info *)(base + E820_INFO_OFFSET);
155
156 memset(info, 0, sizeof(struct e820_info));
157 info->sign = E820_SIGNATURE;
158 info->entries = install_e820_map(E820MAX, data);
159 info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
160 E820_DATA_OFFSET;
161
162 /*
163 * Explicitly clear the bootloader image size otherwise if memory
164 * at this offset happens to contain some garbage data, the final
165 * available memory size for the kernel is insane.
166 */
167 *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
168
169 /*
170 * Prepare compatible framebuffer information block.
171 * The VESA mode has to be 32-bit RGBA.
172 */
173 if (vesa->x_resolution && vesa->y_resolution) {
174 gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET);
175 gop->magic = EFI_GOP_INFO_MAGIC;
176 gop->info.version = 0;
177 gop->info.width = vesa->x_resolution;
178 gop->info.height = vesa->y_resolution;
179 gop->info.pixel_format = EFI_GOT_RGBA8;
180 gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4;
181 gop->fb_base = vesa->phys_base_ptr;
182 gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution;
183 }
184#endif
185
wdenk8bde7f72003-06-27 21:31:46 +0000186 /*
187 * Use bootaddr to find the location in memory that VxWorks
Bin Meng9e98b7e2015-10-07 20:19:17 -0700188 * will look for the bootline string. The default value is
189 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
190 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
wdenk458ded32002-09-20 10:59:08 +0000191 */
Simon Glass00caae62017-08-03 12:22:12 -0600192 tmp = env_get("bootaddr");
Bin Meng9e98b7e2015-10-07 20:19:17 -0700193 if (!tmp) {
Bin Meng79c584e2018-04-11 22:02:22 -0700194#ifdef CONFIG_X86
195 bootaddr = base + X86_BOOT_LINE_OFFSET;
196#else
Bin Meng9e98b7e2015-10-07 20:19:17 -0700197 printf("## VxWorks bootline address not specified\n");
Bin Mengced71a22018-04-11 22:02:21 -0700198 return 1;
Bin Meng79c584e2018-04-11 22:02:22 -0700199#endif
Bin Mengced71a22018-04-11 22:02:21 -0700200 }
wdenk458ded32002-09-20 10:59:08 +0000201
Bin Meng79c584e2018-04-11 22:02:22 -0700202 if (!bootaddr)
Simon Glass7e5f4602021-07-24 09:03:29 -0600203 bootaddr = hextoul(tmp, NULL);
wdenk458ded32002-09-20 10:59:08 +0000204
Bin Mengced71a22018-04-11 22:02:21 -0700205 /*
206 * Check to see if the bootline is defined in the 'bootargs' parameter.
207 * If it is not defined, we may be able to construct the info.
208 */
209 bootline = env_get("bootargs");
210 if (!bootline) {
211 tmp = env_get("bootdev");
212 if (tmp) {
213 strcpy(build_buf, tmp);
214 ptr = strlen(tmp);
215 } else {
216 printf("## VxWorks boot device not specified\n");
Bin Menga4092db2015-10-07 20:19:16 -0700217 }
Niklaus Giger29a4c242008-11-03 22:15:34 +0100218
Bin Mengced71a22018-04-11 22:02:21 -0700219 tmp = env_get("bootfile");
220 if (tmp)
221 ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
222 else
223 ptr += sprintf(build_buf + ptr, "host:vxWorks ");
224
225 /*
226 * The following parameters are only needed if 'bootdev'
227 * is an ethernet device, otherwise they are optional.
228 */
229 tmp = env_get("ipaddr");
230 if (tmp) {
231 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
232 tmp = env_get("netmask");
233 if (tmp) {
234 u32 mask = env_get_ip("netmask").s_addr;
235 ptr += sprintf(build_buf + ptr,
236 ":%08x ", ntohl(mask));
237 } else {
238 ptr += sprintf(build_buf + ptr, " ");
239 }
240 }
241
242 tmp = env_get("serverip");
243 if (tmp)
244 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
245
246 tmp = env_get("gatewayip");
247 if (tmp)
248 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
249
250 tmp = env_get("hostname");
251 if (tmp)
252 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
253
254 tmp = env_get("othbootargs");
255 if (tmp) {
256 strcpy(build_buf + ptr, tmp);
257 ptr += strlen(tmp);
258 }
259
260 bootline = build_buf;
wdenk458ded32002-09-20 10:59:08 +0000261 }
262
Bin Mengced71a22018-04-11 22:02:21 -0700263 memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255));
264 flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
265 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr);
266
wdenk8bde7f72003-06-27 21:31:46 +0000267 /*
268 * If the data at the load address is an elf image, then
269 * treat it like an elf image. Otherwise, assume that it is a
Bin Mengebca3df2015-10-07 20:19:13 -0700270 * binary image.
wdenk458ded32002-09-20 10:59:08 +0000271 */
Bin Mengebca3df2015-10-07 20:19:13 -0700272 if (valid_elf_image(addr))
Christian Gmeiner476c2fc2018-03-20 14:18:25 +0100273 addr = load_elf_image_phdr(addr);
Bin Mengebca3df2015-10-07 20:19:13 -0700274 else
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000275 puts("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000276
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000277 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000278
Reinhard Arlt6eee21d2011-11-18 09:06:52 +0000279 dcache_disable();
Vasyl Vavrychuk3194daa2018-04-10 12:36:36 +0300280#if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
281 armv8_setup_psci();
282 smp_kick_all_cpus();
283#endif
284
Bin Meng9aa12802015-10-07 20:19:19 -0700285#ifdef CONFIG_X86
286 /* VxWorks on x86 uses stack to pass parameters */
287 ((asmlinkage void (*)(int))addr)(0);
288#else
Bin Mengebca3df2015-10-07 20:19:13 -0700289 ((void (*)(int))addr)(0);
Bin Meng9aa12802015-10-07 20:19:19 -0700290#endif
wdenk458ded32002-09-20 10:59:08 +0000291
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000292 puts("## vxWorks terminated\n");
Bin Mengebca3df2015-10-07 20:19:13 -0700293
wdenk458ded32002-09-20 10:59:08 +0000294 return 1;
295}
296
wdenk0d498392003-07-01 21:06:45 +0000297U_BOOT_CMD(
Tom Rinibe1b8672017-05-18 17:03:07 -0400298 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
Peter Tyser2fb26042009-01-27 18:03:12 -0600299 "Boot from an ELF image in memory",
Mike Frysingerf44a9282010-10-02 15:44:53 -0400300 "[-p|-s] [address]\n"
301 "\t- load ELF image at [address] via program headers (-p)\n"
302 "\t or via section headers (-s)"
wdenk8bde7f72003-06-27 21:31:46 +0000303);
304
wdenk0d498392003-07-01 21:06:45 +0000305U_BOOT_CMD(
Bin Mengebca3df2015-10-07 20:19:13 -0700306 bootvx, 2, 0, do_bootvx,
Peter Tyser2fb26042009-01-27 18:03:12 -0600307 "Boot vxWorks from an ELF image",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200308 " [address] - load address of vxWorks ELF image."
wdenk8bde7f72003-06-27 21:31:46 +0000309);