blob: 2b33c50bd026b31ea359d323d783a44d89e8a2a1 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Bin Meng53f3afb2015-10-07 20:19:13 -070014#include <net.h>
Niklaus Gigerf662b232008-11-03 22:15:34 +010015#include <vxworks.h>
Bin Meng321e8602015-10-07 20:19:18 -070016#ifdef CONFIG_X86
Bin Meng71606c42018-04-11 22:02:19 -070017#include <vbe.h>
Simon Glass274e0b02020-05-10 11:39:56 -060018#include <asm/cache.h>
Bin Meng321e8602015-10-07 20:19:18 -070019#include <asm/e820.h>
Bin Mengf6900e22015-10-07 20:19:19 -070020#include <linux/linkage.h>
Bin Meng321e8602015-10-07 20:19:18 -070021#endif
wdenk458ded32002-09-20 10:59:08 +000022
Mike Frysingere60644a2008-04-13 19:42:18 -040023/* Allow ports to override the default behavior */
Jeroen Hofstee3cc2a742014-10-08 22:57:31 +020024static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
Simon Glassed38aef2020-05-10 11:40:03 -060025 int argc, char *const argv[])
Mike Frysingerf79507e2008-01-29 18:21:05 -050026{
Mike Frysingere60644a2008-04-13 19:42:18 -040027 unsigned long ret;
28
Mike Frysingerf79507e2008-01-29 18:21:05 -050029 /*
Mike Frysingere60644a2008-04-13 19:42:18 -040030 * pass address parameter as argv[0] (aka command name),
31 * and all remaining args
32 */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000033 ret = entry(argc, argv);
Mike Frysingerf79507e2008-01-29 18:21:05 -050034
Mike Frysingere60644a2008-04-13 19:42:18 -040035 return ret;
36}
wdenk458ded32002-09-20 10:59:08 +000037
Bin Meng53f3afb2015-10-07 20:19:13 -070038/* Interpreter command to boot an arbitrary ELF image from memory */
Simon Glassed38aef2020-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 Meng53f3afb2015-10-07 20:19:13 -070041 unsigned long addr; /* Address of the ELF image */
42 unsigned long rc; /* Return value from user code */
Tom Rini3c95c792017-05-18 17:03:07 -040043 char *sload = NULL;
wdenk458ded32002-09-20 10:59:08 +000044 int rcode = 0;
45
Tom Rini3c95c792017-05-18 17:03:07 -040046 /* Consume 'bootelf' */
47 argc--; argv++;
Mike Frysingere6dc29f2010-10-02 15:44:53 -040048
Tom Rini3c95c792017-05-18 17:03:07 -040049 /* Check for flag. */
50 if (argc >= 1 && (argv[0][0] == '-' && \
51 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
52 sload = argv[0];
53 /* Consume flag. */
54 argc--; argv++;
55 }
56 /* Check for address. */
57 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
58 /* Consume address */
59 argc--; argv++;
60 } else
Simon Glass892265d2019-12-28 10:45:02 -070061 addr = image_load_addr;
wdenk458ded32002-09-20 10:59:08 +000062
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000063 if (!valid_elf_image(addr))
wdenk458ded32002-09-20 10:59:08 +000064 return 1;
65
Mike Frysingere6dc29f2010-10-02 15:44:53 -040066 if (sload && sload[1] == 'p')
67 addr = load_elf_image_phdr(addr);
68 else
69 addr = load_elf_image_shdr(addr);
wdenk458ded32002-09-20 10:59:08 +000070
Simon Glass46b530a2021-10-21 21:08:52 -060071 if (!env_get_autostart())
Siva Durga Prasad Paladugu3825bfa2015-03-09 12:46:47 +010072 return rcode;
73
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000074 printf("## Starting application at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +000075
wdenk458ded32002-09-20 10:59:08 +000076 /*
77 * pass address parameter as argv[0] (aka command name),
78 * and all remaining args
79 */
Tom Rini3c95c792017-05-18 17:03:07 -040080 rc = do_bootelf_exec((void *)addr, argc, argv);
wdenk458ded32002-09-20 10:59:08 +000081 if (rc != 0)
82 rcode = 1;
83
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000084 printf("## Application terminated, rc = 0x%lx\n", rc);
Bin Meng53f3afb2015-10-07 20:19:13 -070085
wdenk458ded32002-09-20 10:59:08 +000086 return rcode;
87}
88
Bin Meng53f3afb2015-10-07 20:19:13 -070089/*
wdenk458ded32002-09-20 10:59:08 +000090 * Interpreter command to boot VxWorks from a memory image. The image can
91 * be either an ELF image or a raw binary. Will attempt to setup the
92 * bootline and other parameters correctly.
Bin Meng53f3afb2015-10-07 20:19:13 -070093 */
Simon Glassed38aef2020-05-10 11:40:03 -060094int do_bootvx(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk458ded32002-09-20 10:59:08 +000095{
Bin Meng53f3afb2015-10-07 20:19:13 -070096 unsigned long addr; /* Address of image */
Bin Meng75f4b722018-04-11 22:02:22 -070097 unsigned long bootaddr = 0; /* Address to put the bootline */
Bin Meng53f3afb2015-10-07 20:19:13 -070098 char *bootline; /* Text of the bootline */
99 char *tmp; /* Temporary char pointer */
100 char build_buf[128]; /* Buffer for building the bootline */
Bin Meng0401c862015-10-07 20:19:15 -0700101 int ptr = 0;
Bin Meng321e8602015-10-07 20:19:18 -0700102#ifdef CONFIG_X86
Bin Menge3be8902018-04-11 22:02:07 -0700103 ulong base;
Bin Mengdde4ebb2018-04-11 22:02:09 -0700104 struct e820_info *info;
Bin Meng4b8fc742018-04-11 22:02:11 -0700105 struct e820_entry *data;
Bin Meng71606c42018-04-11 22:02:19 -0700106 struct efi_gop_info *gop;
107 struct vesa_mode_info *vesa = &mode_info.vesa;
Bin Meng321e8602015-10-07 20:19:18 -0700108#endif
wdenk458ded32002-09-20 10:59:08 +0000109
Bin Meng53f3afb2015-10-07 20:19:13 -0700110 /*
wdenk458ded32002-09-20 10:59:08 +0000111 * Check the loadaddr variable.
112 * If we don't know where the image is then we're done.
113 */
Stany MARCELe5deaf582013-11-27 14:48:43 +0100114 if (argc < 2)
Simon Glass892265d2019-12-28 10:45:02 -0700115 addr = image_load_addr;
Stefan Roese00487782006-11-29 12:53:15 +0100116 else
Simon Glass3ff49ec2021-07-24 09:03:29 -0600117 addr = hextoul(argv[1], NULL);
wdenk458ded32002-09-20 10:59:08 +0000118
Jon Loeliger54324d02007-07-08 17:51:39 -0500119#if defined(CONFIG_CMD_NET)
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000120 /*
Bin Meng53f3afb2015-10-07 20:19:13 -0700121 * Check to see if we need to tftp the image ourselves
122 * before starting
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000123 */
Stany MARCELe5deaf582013-11-27 14:48:43 +0100124 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500125 if (net_loop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000126 return 1;
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000127 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
128 addr);
wdenk458ded32002-09-20 10:59:08 +0000129 }
130#endif
131
Bin Meng53f3afb2015-10-07 20:19:13 -0700132 /*
133 * This should equate to
134 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
wdenk458ded32002-09-20 10:59:08 +0000135 * from the VxWorks BSP header files.
136 * This will vary from board to board
137 */
Tuomas Tynkkynenf66276d2018-01-21 18:16:42 +0200138#if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Bin Meng53f3afb2015-10-07 20:19:13 -0700139 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
Simon Glass399a9ce2017-08-03 12:22:14 -0600140 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger8bcee042009-02-11 18:51:43 -0500141 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000142#else
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000143 puts("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000144#endif
145
Bin Meng75f4b722018-04-11 22:02:22 -0700146#ifdef CONFIG_X86
147 /*
148 * Get VxWorks's physical memory base address from environment,
149 * if we don't specify it in the environment, use a default one.
150 */
151 base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
152 data = (struct e820_entry *)(base + E820_DATA_OFFSET);
153 info = (struct e820_info *)(base + E820_INFO_OFFSET);
154
155 memset(info, 0, sizeof(struct e820_info));
156 info->sign = E820_SIGNATURE;
157 info->entries = install_e820_map(E820MAX, data);
158 info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
159 E820_DATA_OFFSET;
160
161 /*
162 * Explicitly clear the bootloader image size otherwise if memory
163 * at this offset happens to contain some garbage data, the final
164 * available memory size for the kernel is insane.
165 */
166 *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
167
168 /*
169 * Prepare compatible framebuffer information block.
170 * The VESA mode has to be 32-bit RGBA.
171 */
172 if (vesa->x_resolution && vesa->y_resolution) {
173 gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET);
174 gop->magic = EFI_GOP_INFO_MAGIC;
175 gop->info.version = 0;
176 gop->info.width = vesa->x_resolution;
177 gop->info.height = vesa->y_resolution;
178 gop->info.pixel_format = EFI_GOT_RGBA8;
179 gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4;
180 gop->fb_base = vesa->phys_base_ptr;
181 gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution;
182 }
183#endif
184
wdenk57b2d802003-06-27 21:31:46 +0000185 /*
186 * Use bootaddr to find the location in memory that VxWorks
Bin Mengfb694b92015-10-07 20:19:17 -0700187 * will look for the bootline string. The default value is
188 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
189 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
wdenk458ded32002-09-20 10:59:08 +0000190 */
Simon Glass64b723f2017-08-03 12:22:12 -0600191 tmp = env_get("bootaddr");
Bin Mengfb694b92015-10-07 20:19:17 -0700192 if (!tmp) {
Bin Meng75f4b722018-04-11 22:02:22 -0700193#ifdef CONFIG_X86
194 bootaddr = base + X86_BOOT_LINE_OFFSET;
195#else
Bin Mengfb694b92015-10-07 20:19:17 -0700196 printf("## VxWorks bootline address not specified\n");
Bin Meng051f95e2018-04-11 22:02:21 -0700197 return 1;
Bin Meng75f4b722018-04-11 22:02:22 -0700198#endif
Bin Meng051f95e2018-04-11 22:02:21 -0700199 }
200
Bin Meng75f4b722018-04-11 22:02:22 -0700201 if (!bootaddr)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600202 bootaddr = hextoul(tmp, NULL);
Bin Meng051f95e2018-04-11 22:02:21 -0700203
204 /*
205 * Check to see if the bootline is defined in the 'bootargs' parameter.
206 * If it is not defined, we may be able to construct the info.
207 */
208 bootline = env_get("bootargs");
209 if (!bootline) {
210 tmp = env_get("bootdev");
211 if (tmp) {
212 strcpy(build_buf, tmp);
213 ptr = strlen(tmp);
214 } else {
215 printf("## VxWorks boot device not specified\n");
216 }
217
218 tmp = env_get("bootfile");
219 if (tmp)
220 ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
221 else
222 ptr += sprintf(build_buf + ptr, "host:vxWorks ");
wdenk458ded32002-09-20 10:59:08 +0000223
Bin Mengfb694b92015-10-07 20:19:17 -0700224 /*
Bin Meng051f95e2018-04-11 22:02:21 -0700225 * The following parameters are only needed if 'bootdev'
226 * is an ethernet device, otherwise they are optional.
Bin Mengfb694b92015-10-07 20:19:17 -0700227 */
Bin Meng051f95e2018-04-11 22:02:21 -0700228 tmp = env_get("ipaddr");
229 if (tmp) {
230 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
231 tmp = env_get("netmask");
Ben Whitten34fd6c92015-12-30 13:05:58 +0000232 if (tmp) {
Bin Meng051f95e2018-04-11 22:02:21 -0700233 u32 mask = env_get_ip("netmask").s_addr;
Bin Mengfb694b92015-10-07 20:19:17 -0700234 ptr += sprintf(build_buf + ptr,
Bin Meng051f95e2018-04-11 22:02:21 -0700235 ":%08x ", ntohl(mask));
236 } else {
237 ptr += sprintf(build_buf + ptr, " ");
Bin Mengbf0ba252015-10-07 20:19:16 -0700238 }
Bin Meng051f95e2018-04-11 22:02:21 -0700239 }
wdenk458ded32002-09-20 10:59:08 +0000240
Bin Meng051f95e2018-04-11 22:02:21 -0700241 tmp = env_get("serverip");
242 if (tmp)
243 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000244
Bin Meng051f95e2018-04-11 22:02:21 -0700245 tmp = env_get("gatewayip");
246 if (tmp)
247 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
Bin Mengbf0ba252015-10-07 20:19:16 -0700248
Bin Meng051f95e2018-04-11 22:02:21 -0700249 tmp = env_get("hostname");
250 if (tmp)
251 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000252
Bin Meng051f95e2018-04-11 22:02:21 -0700253 tmp = env_get("othbootargs");
254 if (tmp) {
255 strcpy(build_buf + ptr, tmp);
256 ptr += strlen(tmp);
Bin Mengfb694b92015-10-07 20:19:17 -0700257 }
258
Bin Meng051f95e2018-04-11 22:02:21 -0700259 bootline = build_buf;
wdenk458ded32002-09-20 10:59:08 +0000260 }
261
Bin Meng051f95e2018-04-11 22:02:21 -0700262 memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255));
263 flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
264 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr);
265
wdenk57b2d802003-06-27 21:31:46 +0000266 /*
267 * If the data at the load address is an elf image, then
268 * treat it like an elf image. Otherwise, assume that it is a
Bin Meng53f3afb2015-10-07 20:19:13 -0700269 * binary image.
wdenk458ded32002-09-20 10:59:08 +0000270 */
Bin Meng53f3afb2015-10-07 20:19:13 -0700271 if (valid_elf_image(addr))
Christian Gmeinerae7d4be2018-03-20 14:18:25 +0100272 addr = load_elf_image_phdr(addr);
Bin Meng53f3afb2015-10-07 20:19:13 -0700273 else
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000274 puts("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000275
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000276 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000277
Reinhard Arlt4fa117c2011-11-18 09:06:52 +0000278 dcache_disable();
Vasyl Vavrychuk70b551c2018-04-10 12:36:36 +0300279#if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
280 armv8_setup_psci();
281 smp_kick_all_cpus();
282#endif
283
Bin Mengf6900e22015-10-07 20:19:19 -0700284#ifdef CONFIG_X86
285 /* VxWorks on x86 uses stack to pass parameters */
286 ((asmlinkage void (*)(int))addr)(0);
287#else
Bin Meng53f3afb2015-10-07 20:19:13 -0700288 ((void (*)(int))addr)(0);
Bin Mengf6900e22015-10-07 20:19:19 -0700289#endif
wdenk458ded32002-09-20 10:59:08 +0000290
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000291 puts("## vxWorks terminated\n");
Bin Meng53f3afb2015-10-07 20:19:13 -0700292
wdenk458ded32002-09-20 10:59:08 +0000293 return 1;
294}
295
wdenkf287a242003-07-01 21:06:45 +0000296U_BOOT_CMD(
Tom Rini3c95c792017-05-18 17:03:07 -0400297 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600298 "Boot from an ELF image in memory",
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400299 "[-p|-s] [address]\n"
300 "\t- load ELF image at [address] via program headers (-p)\n"
301 "\t or via section headers (-s)"
wdenk57b2d802003-06-27 21:31:46 +0000302);
303
wdenkf287a242003-07-01 21:06:45 +0000304U_BOOT_CMD(
Bin Meng53f3afb2015-10-07 20:19:13 -0700305 bootvx, 2, 0, do_bootvx,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600306 "Boot vxWorks from an ELF image",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200307 " [address] - load address of vxWorks ELF image."
wdenk57b2d802003-06-27 21:31:46 +0000308);