blob: 19479bb706e06c9ccd97f6c855e39d54778c7c5e [file] [log] [blame]
wdenk458ded32002-09-20 10:59:08 +00001/*
2 * Copyright (c) 2001 William L. Pitts
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are freely
6 * permitted provided that the above copyright notice and this
7 * paragraph and the following disclaimer are duplicated in all
8 * such forms.
9 *
10 * This software is provided "AS IS" and without any express or
11 * implied warranties, including, without limitation, the implied
12 * warranties of merchantability and fitness for a particular
13 * purpose.
14 */
15
16#include <common.h>
17#include <command.h>
wdenk458ded32002-09-20 10:59:08 +000018#include <elf.h>
Alex Kiernan9c215492018-04-01 09:22:38 +000019#include <environment.h>
Bin Meng53f3afb2015-10-07 20:19:13 -070020#include <net.h>
Niklaus Gigerf662b232008-11-03 22:15:34 +010021#include <vxworks.h>
Bin Meng321e8602015-10-07 20:19:18 -070022#ifdef CONFIG_X86
23#include <asm/e820.h>
Bin Mengf6900e22015-10-07 20:19:19 -070024#include <linux/linkage.h>
Bin Meng321e8602015-10-07 20:19:18 -070025#endif
wdenk458ded32002-09-20 10:59:08 +000026
Bin Meng8c77af62015-10-07 20:19:14 -070027/*
28 * A very simple elf loader, assumes the image is valid, returns the
29 * entry point address.
30 */
31static unsigned long load_elf_image_phdr(unsigned long addr)
32{
33 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
34 Elf32_Phdr *phdr; /* Program header structure pointer */
35 int i;
36
37 ehdr = (Elf32_Ehdr *)addr;
38 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
39
40 /* Load each program header */
41 for (i = 0; i < ehdr->e_phnum; ++i) {
42 void *dst = (void *)(uintptr_t)phdr->p_paddr;
43 void *src = (void *)addr + phdr->p_offset;
44 debug("Loading phdr %i to 0x%p (%i bytes)\n",
45 i, dst, phdr->p_filesz);
46 if (phdr->p_filesz)
47 memcpy(dst, src, phdr->p_filesz);
48 if (phdr->p_filesz != phdr->p_memsz)
49 memset(dst + phdr->p_filesz, 0x00,
50 phdr->p_memsz - phdr->p_filesz);
51 flush_cache((unsigned long)dst, phdr->p_filesz);
52 ++phdr;
53 }
54
55 return ehdr->e_entry;
56}
57
58static unsigned long load_elf_image_shdr(unsigned long addr)
59{
60 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
61 Elf32_Shdr *shdr; /* Section header structure pointer */
62 unsigned char *strtab = 0; /* String table pointer */
63 unsigned char *image; /* Binary image pointer */
64 int i; /* Loop counter */
65
66 ehdr = (Elf32_Ehdr *)addr;
67
68 /* Find the section header string table for output info */
69 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
70 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
71
72 if (shdr->sh_type == SHT_STRTAB)
73 strtab = (unsigned char *)(addr + shdr->sh_offset);
74
75 /* Load each appropriate section */
76 for (i = 0; i < ehdr->e_shnum; ++i) {
77 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
78 (i * sizeof(Elf32_Shdr)));
79
80 if (!(shdr->sh_flags & SHF_ALLOC) ||
81 shdr->sh_addr == 0 || shdr->sh_size == 0) {
82 continue;
83 }
84
85 if (strtab) {
86 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
87 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
88 &strtab[shdr->sh_name],
89 (unsigned long)shdr->sh_addr,
90 (long)shdr->sh_size);
91 }
92
93 if (shdr->sh_type == SHT_NOBITS) {
94 memset((void *)(uintptr_t)shdr->sh_addr, 0,
95 shdr->sh_size);
96 } else {
97 image = (unsigned char *)addr + shdr->sh_offset;
98 memcpy((void *)(uintptr_t)shdr->sh_addr,
99 (const void *)image, shdr->sh_size);
100 }
101 flush_cache(shdr->sh_addr, shdr->sh_size);
102 }
103
104 return ehdr->e_entry;
105}
Mike Frysingere60644a2008-04-13 19:42:18 -0400106
107/* Allow ports to override the default behavior */
Jeroen Hofstee3cc2a742014-10-08 22:57:31 +0200108static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
Bin Meng53f3afb2015-10-07 20:19:13 -0700109 int argc, char * const argv[])
Mike Frysingerf79507e2008-01-29 18:21:05 -0500110{
Mike Frysingere60644a2008-04-13 19:42:18 -0400111 unsigned long ret;
112
Mike Frysingerf79507e2008-01-29 18:21:05 -0500113 /*
Mike Frysingere60644a2008-04-13 19:42:18 -0400114 * pass address parameter as argv[0] (aka command name),
115 * and all remaining args
116 */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000117 ret = entry(argc, argv);
Mike Frysingerf79507e2008-01-29 18:21:05 -0500118
Mike Frysingere60644a2008-04-13 19:42:18 -0400119 return ret;
120}
wdenk458ded32002-09-20 10:59:08 +0000121
Bin Meng53f3afb2015-10-07 20:19:13 -0700122/*
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000123 * Determine if a valid ELF image exists at the given memory location.
Bin Meng53f3afb2015-10-07 20:19:13 -0700124 * First look at the ELF header magic field, then make sure that it is
125 * executable.
126 */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000127int valid_elf_image(unsigned long addr)
128{
Bin Meng53f3afb2015-10-07 20:19:13 -0700129 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000130
Bin Meng53f3afb2015-10-07 20:19:13 -0700131 ehdr = (Elf32_Ehdr *)addr;
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000132
133 if (!IS_ELF(*ehdr)) {
134 printf("## No elf image at address 0x%08lx\n", addr);
135 return 0;
136 }
137
138 if (ehdr->e_type != ET_EXEC) {
139 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
140 return 0;
141 }
142
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000143 return 1;
144}
145
Bin Meng53f3afb2015-10-07 20:19:13 -0700146/* Interpreter command to boot an arbitrary ELF image from memory */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000147int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +0000148{
Bin Meng53f3afb2015-10-07 20:19:13 -0700149 unsigned long addr; /* Address of the ELF image */
150 unsigned long rc; /* Return value from user code */
Tom Rini3c95c792017-05-18 17:03:07 -0400151 char *sload = NULL;
Simon Glass64b723f2017-08-03 12:22:12 -0600152 const char *ep = env_get("autostart");
wdenk458ded32002-09-20 10:59:08 +0000153 int rcode = 0;
154
Tom Rini3c95c792017-05-18 17:03:07 -0400155 /* Consume 'bootelf' */
156 argc--; argv++;
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400157
Tom Rini3c95c792017-05-18 17:03:07 -0400158 /* Check for flag. */
159 if (argc >= 1 && (argv[0][0] == '-' && \
160 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
161 sload = argv[0];
162 /* Consume flag. */
163 argc--; argv++;
164 }
165 /* Check for address. */
166 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
167 /* Consume address */
168 argc--; argv++;
169 } else
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400170 addr = load_addr;
wdenk458ded32002-09-20 10:59:08 +0000171
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000172 if (!valid_elf_image(addr))
wdenk458ded32002-09-20 10:59:08 +0000173 return 1;
174
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400175 if (sload && sload[1] == 'p')
176 addr = load_elf_image_phdr(addr);
177 else
178 addr = load_elf_image_shdr(addr);
wdenk458ded32002-09-20 10:59:08 +0000179
Siva Durga Prasad Paladugu3825bfa2015-03-09 12:46:47 +0100180 if (ep && !strcmp(ep, "no"))
181 return rcode;
182
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000183 printf("## Starting application at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000184
wdenk458ded32002-09-20 10:59:08 +0000185 /*
186 * pass address parameter as argv[0] (aka command name),
187 * and all remaining args
188 */
Tom Rini3c95c792017-05-18 17:03:07 -0400189 rc = do_bootelf_exec((void *)addr, argc, argv);
wdenk458ded32002-09-20 10:59:08 +0000190 if (rc != 0)
191 rcode = 1;
192
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000193 printf("## Application terminated, rc = 0x%lx\n", rc);
Bin Meng53f3afb2015-10-07 20:19:13 -0700194
wdenk458ded32002-09-20 10:59:08 +0000195 return rcode;
196}
197
Bin Meng53f3afb2015-10-07 20:19:13 -0700198/*
wdenk458ded32002-09-20 10:59:08 +0000199 * Interpreter command to boot VxWorks from a memory image. The image can
200 * be either an ELF image or a raw binary. Will attempt to setup the
201 * bootline and other parameters correctly.
Bin Meng53f3afb2015-10-07 20:19:13 -0700202 */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000203int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +0000204{
Bin Meng53f3afb2015-10-07 20:19:13 -0700205 unsigned long addr; /* Address of image */
206 unsigned long bootaddr; /* Address to put the bootline */
207 char *bootline; /* Text of the bootline */
208 char *tmp; /* Temporary char pointer */
209 char build_buf[128]; /* Buffer for building the bootline */
Bin Meng0401c862015-10-07 20:19:15 -0700210 int ptr = 0;
Bin Meng321e8602015-10-07 20:19:18 -0700211#ifdef CONFIG_X86
212 struct e820info *info;
213 struct e820entry *data;
214#endif
wdenk458ded32002-09-20 10:59:08 +0000215
Bin Meng53f3afb2015-10-07 20:19:13 -0700216 /*
wdenk458ded32002-09-20 10:59:08 +0000217 * Check the loadaddr variable.
218 * If we don't know where the image is then we're done.
219 */
Stany MARCELe5deaf582013-11-27 14:48:43 +0100220 if (argc < 2)
Stefan Roese00487782006-11-29 12:53:15 +0100221 addr = load_addr;
222 else
Stany MARCELe5deaf582013-11-27 14:48:43 +0100223 addr = simple_strtoul(argv[1], NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000224
Jon Loeliger54324d02007-07-08 17:51:39 -0500225#if defined(CONFIG_CMD_NET)
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000226 /*
Bin Meng53f3afb2015-10-07 20:19:13 -0700227 * Check to see if we need to tftp the image ourselves
228 * before starting
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000229 */
Stany MARCELe5deaf582013-11-27 14:48:43 +0100230 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500231 if (net_loop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000232 return 1;
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000233 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
234 addr);
wdenk458ded32002-09-20 10:59:08 +0000235 }
236#endif
237
Bin Meng53f3afb2015-10-07 20:19:13 -0700238 /*
239 * This should equate to
240 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
wdenk458ded32002-09-20 10:59:08 +0000241 * from the VxWorks BSP header files.
242 * This will vary from board to board
243 */
Tuomas Tynkkynenf66276d2018-01-21 18:16:42 +0200244#if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Bin Meng53f3afb2015-10-07 20:19:13 -0700245 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
Simon Glass399a9ce2017-08-03 12:22:14 -0600246 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger8bcee042009-02-11 18:51:43 -0500247 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000248#else
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000249 puts("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000250#endif
251
wdenk57b2d802003-06-27 21:31:46 +0000252 /*
253 * Use bootaddr to find the location in memory that VxWorks
Bin Mengfb694b92015-10-07 20:19:17 -0700254 * will look for the bootline string. The default value is
255 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
256 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
wdenk458ded32002-09-20 10:59:08 +0000257 */
Simon Glass64b723f2017-08-03 12:22:12 -0600258 tmp = env_get("bootaddr");
Bin Mengfb694b92015-10-07 20:19:17 -0700259 if (!tmp) {
260 printf("## VxWorks bootline address not specified\n");
261 } else {
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000262 bootaddr = simple_strtoul(tmp, NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000263
Bin Mengfb694b92015-10-07 20:19:17 -0700264 /*
265 * Check to see if the bootline is defined in the 'bootargs'
266 * parameter. If it is not defined, we may be able to
267 * construct the info.
268 */
Simon Glass64b723f2017-08-03 12:22:12 -0600269 bootline = env_get("bootargs");
Bin Mengfb694b92015-10-07 20:19:17 -0700270 if (bootline) {
271 memcpy((void *)bootaddr, bootline,
272 max(strlen(bootline), (size_t)255));
273 flush_cache(bootaddr, max(strlen(bootline),
274 (size_t)255));
275 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600276 tmp = env_get("bootdev");
Ben Whitten34fd6c92015-12-30 13:05:58 +0000277 if (tmp) {
278 strcpy(build_buf, tmp);
279 ptr = strlen(tmp);
280 } else
Bin Mengfb694b92015-10-07 20:19:17 -0700281 printf("## VxWorks boot device not specified\n");
wdenk458ded32002-09-20 10:59:08 +0000282
Simon Glass64b723f2017-08-03 12:22:12 -0600283 tmp = env_get("bootfile");
Bin Mengfb694b92015-10-07 20:19:17 -0700284 if (tmp)
285 ptr += sprintf(build_buf + ptr,
286 "host:%s ", tmp);
287 else
288 ptr += sprintf(build_buf + ptr,
289 "host:vxWorks ");
290
291 /*
292 * The following parameters are only needed if 'bootdev'
293 * is an ethernet device, otherwise they are optional.
294 */
Simon Glass64b723f2017-08-03 12:22:12 -0600295 tmp = env_get("ipaddr");
Bin Mengbf0ba252015-10-07 20:19:16 -0700296 if (tmp) {
Bin Mengfb694b92015-10-07 20:19:17 -0700297 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
Simon Glass64b723f2017-08-03 12:22:12 -0600298 tmp = env_get("netmask");
Bin Mengfb694b92015-10-07 20:19:17 -0700299 if (tmp) {
Simon Glassda1a1342017-08-03 12:22:15 -0600300 u32 mask = env_get_ip("netmask").s_addr;
Bin Mengfb694b92015-10-07 20:19:17 -0700301 ptr += sprintf(build_buf + ptr,
302 ":%08x ", ntohl(mask));
303 } else {
304 ptr += sprintf(build_buf + ptr, " ");
305 }
Bin Mengbf0ba252015-10-07 20:19:16 -0700306 }
wdenk458ded32002-09-20 10:59:08 +0000307
Simon Glass64b723f2017-08-03 12:22:12 -0600308 tmp = env_get("serverip");
Bin Mengfb694b92015-10-07 20:19:17 -0700309 if (tmp)
310 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000311
Simon Glass64b723f2017-08-03 12:22:12 -0600312 tmp = env_get("gatewayip");
Bin Mengfb694b92015-10-07 20:19:17 -0700313 if (tmp)
314 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
Bin Mengbf0ba252015-10-07 20:19:16 -0700315
Simon Glass64b723f2017-08-03 12:22:12 -0600316 tmp = env_get("hostname");
Bin Mengfb694b92015-10-07 20:19:17 -0700317 if (tmp)
318 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000319
Simon Glass64b723f2017-08-03 12:22:12 -0600320 tmp = env_get("othbootargs");
Ben Whitten34fd6c92015-12-30 13:05:58 +0000321 if (tmp) {
322 strcpy(build_buf + ptr, tmp);
323 ptr += strlen(tmp);
324 }
Niklaus Gigerf662b232008-11-03 22:15:34 +0100325
Bin Mengfb694b92015-10-07 20:19:17 -0700326 memcpy((void *)bootaddr, build_buf,
327 max(strlen(build_buf), (size_t)255));
328 flush_cache(bootaddr, max(strlen(build_buf),
329 (size_t)255));
330 }
331
332 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
333 (char *)bootaddr);
wdenk458ded32002-09-20 10:59:08 +0000334 }
335
Bin Meng321e8602015-10-07 20:19:18 -0700336#ifdef CONFIG_X86
337 /*
338 * Since E820 information is critical to the kernel, if we don't
339 * specify these in the environments, use a default one.
340 */
Simon Glass64b723f2017-08-03 12:22:12 -0600341 tmp = env_get("e820data");
Bin Meng321e8602015-10-07 20:19:18 -0700342 if (tmp)
343 data = (struct e820entry *)simple_strtoul(tmp, NULL, 16);
344 else
345 data = (struct e820entry *)VXWORKS_E820_DATA_ADDR;
Simon Glass64b723f2017-08-03 12:22:12 -0600346 tmp = env_get("e820info");
Bin Meng321e8602015-10-07 20:19:18 -0700347 if (tmp)
348 info = (struct e820info *)simple_strtoul(tmp, NULL, 16);
349 else
350 info = (struct e820info *)VXWORKS_E820_INFO_ADDR;
351
352 memset(info, 0, sizeof(struct e820info));
353 info->sign = E820_SIGNATURE;
354 info->entries = install_e820_map(E820MAX, data);
355 info->addr = (info->entries - 1) * sizeof(struct e820entry) +
356 VXWORKS_E820_DATA_ADDR;
357#endif
358
wdenk57b2d802003-06-27 21:31:46 +0000359 /*
360 * If the data at the load address is an elf image, then
361 * treat it like an elf image. Otherwise, assume that it is a
Bin Meng53f3afb2015-10-07 20:19:13 -0700362 * binary image.
wdenk458ded32002-09-20 10:59:08 +0000363 */
Bin Meng53f3afb2015-10-07 20:19:13 -0700364 if (valid_elf_image(addr))
Christian Gmeinerae7d4be2018-03-20 14:18:25 +0100365 addr = load_elf_image_phdr(addr);
Bin Meng53f3afb2015-10-07 20:19:13 -0700366 else
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000367 puts("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000368
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000369 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000370
Reinhard Arlt4fa117c2011-11-18 09:06:52 +0000371 dcache_disable();
Vasyl Vavrychuk70b551c2018-04-10 12:36:36 +0300372#if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
373 armv8_setup_psci();
374 smp_kick_all_cpus();
375#endif
376
Bin Mengf6900e22015-10-07 20:19:19 -0700377#ifdef CONFIG_X86
378 /* VxWorks on x86 uses stack to pass parameters */
379 ((asmlinkage void (*)(int))addr)(0);
380#else
Bin Meng53f3afb2015-10-07 20:19:13 -0700381 ((void (*)(int))addr)(0);
Bin Mengf6900e22015-10-07 20:19:19 -0700382#endif
wdenk458ded32002-09-20 10:59:08 +0000383
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000384 puts("## vxWorks terminated\n");
Bin Meng53f3afb2015-10-07 20:19:13 -0700385
wdenk458ded32002-09-20 10:59:08 +0000386 return 1;
387}
388
wdenkf287a242003-07-01 21:06:45 +0000389U_BOOT_CMD(
Tom Rini3c95c792017-05-18 17:03:07 -0400390 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600391 "Boot from an ELF image in memory",
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400392 "[-p|-s] [address]\n"
393 "\t- load ELF image at [address] via program headers (-p)\n"
394 "\t or via section headers (-s)"
wdenk57b2d802003-06-27 21:31:46 +0000395);
396
wdenkf287a242003-07-01 21:06:45 +0000397U_BOOT_CMD(
Bin Meng53f3afb2015-10-07 20:19:13 -0700398 bootvx, 2, 0, do_bootvx,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600399 "Boot vxWorks from an ELF image",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200400 " [address] - load address of vxWorks ELF image."
wdenk57b2d802003-06-27 21:31:46 +0000401);