blob: d6a2036a992c4dd23072265cc15dbd503302d942 [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>
Bin Mengebca3df2015-10-07 20:19:13 -070019#include <net.h>
Niklaus Giger29a4c242008-11-03 22:15:34 +010020#include <vxworks.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070021#ifdef CONFIG_X86
22#include <asm/e820.h>
23#endif
wdenk458ded32002-09-20 10:59:08 +000024
Bin Meng9dffa522015-10-07 20:19:14 -070025/*
26 * A very simple elf loader, assumes the image is valid, returns the
27 * entry point address.
28 */
29static unsigned long load_elf_image_phdr(unsigned long addr)
30{
31 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
32 Elf32_Phdr *phdr; /* Program header structure pointer */
33 int i;
34
35 ehdr = (Elf32_Ehdr *)addr;
36 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
37
38 /* Load each program header */
39 for (i = 0; i < ehdr->e_phnum; ++i) {
40 void *dst = (void *)(uintptr_t)phdr->p_paddr;
41 void *src = (void *)addr + phdr->p_offset;
42 debug("Loading phdr %i to 0x%p (%i bytes)\n",
43 i, dst, phdr->p_filesz);
44 if (phdr->p_filesz)
45 memcpy(dst, src, phdr->p_filesz);
46 if (phdr->p_filesz != phdr->p_memsz)
47 memset(dst + phdr->p_filesz, 0x00,
48 phdr->p_memsz - phdr->p_filesz);
49 flush_cache((unsigned long)dst, phdr->p_filesz);
50 ++phdr;
51 }
52
53 return ehdr->e_entry;
54}
55
56static unsigned long load_elf_image_shdr(unsigned long addr)
57{
58 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
59 Elf32_Shdr *shdr; /* Section header structure pointer */
60 unsigned char *strtab = 0; /* String table pointer */
61 unsigned char *image; /* Binary image pointer */
62 int i; /* Loop counter */
63
64 ehdr = (Elf32_Ehdr *)addr;
65
66 /* Find the section header string table for output info */
67 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
68 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
69
70 if (shdr->sh_type == SHT_STRTAB)
71 strtab = (unsigned char *)(addr + shdr->sh_offset);
72
73 /* Load each appropriate section */
74 for (i = 0; i < ehdr->e_shnum; ++i) {
75 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
76 (i * sizeof(Elf32_Shdr)));
77
78 if (!(shdr->sh_flags & SHF_ALLOC) ||
79 shdr->sh_addr == 0 || shdr->sh_size == 0) {
80 continue;
81 }
82
83 if (strtab) {
84 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
85 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
86 &strtab[shdr->sh_name],
87 (unsigned long)shdr->sh_addr,
88 (long)shdr->sh_size);
89 }
90
91 if (shdr->sh_type == SHT_NOBITS) {
92 memset((void *)(uintptr_t)shdr->sh_addr, 0,
93 shdr->sh_size);
94 } else {
95 image = (unsigned char *)addr + shdr->sh_offset;
96 memcpy((void *)(uintptr_t)shdr->sh_addr,
97 (const void *)image, shdr->sh_size);
98 }
99 flush_cache(shdr->sh_addr, shdr->sh_size);
100 }
101
102 return ehdr->e_entry;
103}
Mike Frysinger017e9b72008-04-13 19:42:18 -0400104
105/* Allow ports to override the default behavior */
Jeroen Hofstee553d8c32014-10-08 22:57:31 +0200106static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
Bin Mengebca3df2015-10-07 20:19:13 -0700107 int argc, char * const argv[])
Mike Frysinger1f1d88d2008-01-29 18:21:05 -0500108{
Mike Frysinger017e9b72008-04-13 19:42:18 -0400109 unsigned long ret;
110
Mike Frysinger1f1d88d2008-01-29 18:21:05 -0500111 /*
112 * QNX images require the data cache is disabled.
113 * Data cache is already flushed, so just turn it off.
114 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000115 int dcache = dcache_status();
Mike Frysinger017e9b72008-04-13 19:42:18 -0400116 if (dcache)
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000117 dcache_disable();
Mike Frysinger1f1d88d2008-01-29 18:21:05 -0500118
Mike Frysinger017e9b72008-04-13 19:42:18 -0400119 /*
120 * pass address parameter as argv[0] (aka command name),
121 * and all remaining args
122 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000123 ret = entry(argc, argv);
Mike Frysinger1f1d88d2008-01-29 18:21:05 -0500124
Mike Frysinger017e9b72008-04-13 19:42:18 -0400125 if (dcache)
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000126 dcache_enable();
Mike Frysinger017e9b72008-04-13 19:42:18 -0400127
128 return ret;
129}
wdenk458ded32002-09-20 10:59:08 +0000130
Bin Mengebca3df2015-10-07 20:19:13 -0700131/*
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000132 * Determine if a valid ELF image exists at the given memory location.
Bin Mengebca3df2015-10-07 20:19:13 -0700133 * First look at the ELF header magic field, then make sure that it is
134 * executable.
135 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000136int valid_elf_image(unsigned long addr)
137{
Bin Mengebca3df2015-10-07 20:19:13 -0700138 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000139
Bin Mengebca3df2015-10-07 20:19:13 -0700140 ehdr = (Elf32_Ehdr *)addr;
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000141
142 if (!IS_ELF(*ehdr)) {
143 printf("## No elf image at address 0x%08lx\n", addr);
144 return 0;
145 }
146
147 if (ehdr->e_type != ET_EXEC) {
148 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
149 return 0;
150 }
151
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000152 return 1;
153}
154
Bin Mengebca3df2015-10-07 20:19:13 -0700155/* Interpreter command to boot an arbitrary ELF image from memory */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000156int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +0000157{
Bin Mengebca3df2015-10-07 20:19:13 -0700158 unsigned long addr; /* Address of the ELF image */
159 unsigned long rc; /* Return value from user code */
Mike Frysingerf44a9282010-10-02 15:44:53 -0400160 char *sload, *saddr;
Siva Durga Prasad Paladugu44c8fd32015-03-09 12:46:47 +0100161 const char *ep = getenv("autostart");
wdenk458ded32002-09-20 10:59:08 +0000162
wdenk458ded32002-09-20 10:59:08 +0000163 int rcode = 0;
164
Mike Frysingerf44a9282010-10-02 15:44:53 -0400165 sload = saddr = NULL;
166 if (argc == 3) {
167 sload = argv[1];
168 saddr = argv[2];
169 } else if (argc == 2) {
170 if (argv[1][0] == '-')
171 sload = argv[1];
172 else
173 saddr = argv[1];
174 }
175
176 if (saddr)
177 addr = simple_strtoul(saddr, NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000178 else
Mike Frysingerf44a9282010-10-02 15:44:53 -0400179 addr = load_addr;
wdenk458ded32002-09-20 10:59:08 +0000180
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000181 if (!valid_elf_image(addr))
wdenk458ded32002-09-20 10:59:08 +0000182 return 1;
183
Mike Frysingerf44a9282010-10-02 15:44:53 -0400184 if (sload && sload[1] == 'p')
185 addr = load_elf_image_phdr(addr);
186 else
187 addr = load_elf_image_shdr(addr);
wdenk458ded32002-09-20 10:59:08 +0000188
Siva Durga Prasad Paladugu44c8fd32015-03-09 12:46:47 +0100189 if (ep && !strcmp(ep, "no"))
190 return rcode;
191
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000192 printf("## Starting application at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000193
wdenk458ded32002-09-20 10:59:08 +0000194 /*
195 * pass address parameter as argv[0] (aka command name),
196 * and all remaining args
197 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000198 rc = do_bootelf_exec((void *)addr, argc - 1, argv + 1);
wdenk458ded32002-09-20 10:59:08 +0000199 if (rc != 0)
200 rcode = 1;
201
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000202 printf("## Application terminated, rc = 0x%lx\n", rc);
Bin Mengebca3df2015-10-07 20:19:13 -0700203
wdenk458ded32002-09-20 10:59:08 +0000204 return rcode;
205}
206
Bin Mengebca3df2015-10-07 20:19:13 -0700207/*
wdenk458ded32002-09-20 10:59:08 +0000208 * Interpreter command to boot VxWorks from a memory image. The image can
209 * be either an ELF image or a raw binary. Will attempt to setup the
210 * bootline and other parameters correctly.
Bin Mengebca3df2015-10-07 20:19:13 -0700211 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000212int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +0000213{
Bin Mengebca3df2015-10-07 20:19:13 -0700214 unsigned long addr; /* Address of image */
215 unsigned long bootaddr; /* Address to put the bootline */
216 char *bootline; /* Text of the bootline */
217 char *tmp; /* Temporary char pointer */
218 char build_buf[128]; /* Buffer for building the bootline */
Bin Meng7f0c3c52015-10-07 20:19:15 -0700219 int ptr = 0;
Bin Mengb90ff0f2015-10-07 20:19:18 -0700220#ifdef CONFIG_X86
221 struct e820info *info;
222 struct e820entry *data;
223#endif
wdenk458ded32002-09-20 10:59:08 +0000224
Bin Mengebca3df2015-10-07 20:19:13 -0700225 /*
wdenk458ded32002-09-20 10:59:08 +0000226 * Check the loadaddr variable.
227 * If we don't know where the image is then we're done.
228 */
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100229 if (argc < 2)
Stefan Roese1bdd4682006-11-29 12:53:15 +0100230 addr = load_addr;
231 else
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100232 addr = simple_strtoul(argv[1], NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000233
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500234#if defined(CONFIG_CMD_NET)
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000235 /*
Bin Mengebca3df2015-10-07 20:19:13 -0700236 * Check to see if we need to tftp the image ourselves
237 * before starting
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000238 */
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100239 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500240 if (net_loop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000241 return 1;
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000242 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
243 addr);
wdenk458ded32002-09-20 10:59:08 +0000244 }
245#endif
246
Bin Mengebca3df2015-10-07 20:19:13 -0700247 /*
248 * This should equate to
249 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
wdenk458ded32002-09-20 10:59:08 +0000250 * from the VxWorks BSP header files.
251 * This will vary from board to board
252 */
Stefan Roesec157d8e2005-08-01 16:41:48 +0200253#if defined(CONFIG_WALNUT)
Bin Mengebca3df2015-10-07 20:19:13 -0700254 tmp = (char *)CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
Heiko Schocher76756e42009-03-26 07:33:59 +0100255 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger62c93d92009-02-11 18:51:43 -0500256 memcpy(tmp, &build_buf[3], 3);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200257#elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Bin Mengebca3df2015-10-07 20:19:13 -0700258 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
Heiko Schocher76756e42009-03-26 07:33:59 +0100259 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger62c93d92009-02-11 18:51:43 -0500260 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000261#else
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000262 puts("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000263#endif
264
wdenk8bde7f72003-06-27 21:31:46 +0000265 /*
266 * Use bootaddr to find the location in memory that VxWorks
Bin Meng9e98b7e2015-10-07 20:19:17 -0700267 * will look for the bootline string. The default value is
268 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
269 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
wdenk458ded32002-09-20 10:59:08 +0000270 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000271 tmp = getenv("bootaddr");
Bin Meng9e98b7e2015-10-07 20:19:17 -0700272 if (!tmp) {
273 printf("## VxWorks bootline address not specified\n");
274 } else {
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000275 bootaddr = simple_strtoul(tmp, NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000276
Bin Meng9e98b7e2015-10-07 20:19:17 -0700277 /*
278 * Check to see if the bootline is defined in the 'bootargs'
279 * parameter. If it is not defined, we may be able to
280 * construct the info.
281 */
282 bootline = getenv("bootargs");
283 if (bootline) {
284 memcpy((void *)bootaddr, bootline,
285 max(strlen(bootline), (size_t)255));
286 flush_cache(bootaddr, max(strlen(bootline),
287 (size_t)255));
288 } else {
289 tmp = getenv("bootdev");
290 if (tmp)
291 ptr = sprintf(build_buf, tmp);
292 else
293 printf("## VxWorks boot device not specified\n");
wdenk458ded32002-09-20 10:59:08 +0000294
Bin Meng9e98b7e2015-10-07 20:19:17 -0700295 tmp = getenv("bootfile");
296 if (tmp)
297 ptr += sprintf(build_buf + ptr,
298 "host:%s ", tmp);
299 else
300 ptr += sprintf(build_buf + ptr,
301 "host:vxWorks ");
302
303 /*
304 * The following parameters are only needed if 'bootdev'
305 * is an ethernet device, otherwise they are optional.
306 */
307 tmp = getenv("ipaddr");
Bin Menga4092db2015-10-07 20:19:16 -0700308 if (tmp) {
Bin Meng9e98b7e2015-10-07 20:19:17 -0700309 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
310 tmp = getenv("netmask");
311 if (tmp) {
312 u32 mask = getenv_ip("netmask").s_addr;
313 ptr += sprintf(build_buf + ptr,
314 ":%08x ", ntohl(mask));
315 } else {
316 ptr += sprintf(build_buf + ptr, " ");
317 }
Bin Menga4092db2015-10-07 20:19:16 -0700318 }
Bin Meng9e98b7e2015-10-07 20:19:17 -0700319
320 tmp = getenv("serverip");
321 if (tmp)
322 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
323
324 tmp = getenv("gatewayip");
325 if (tmp)
326 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
327
328 tmp = getenv("hostname");
329 if (tmp)
330 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
331
332 tmp = getenv("othbootargs");
333 if (tmp)
334 ptr += sprintf(build_buf + ptr, tmp);
335
336 memcpy((void *)bootaddr, build_buf,
337 max(strlen(build_buf), (size_t)255));
338 flush_cache(bootaddr, max(strlen(build_buf),
339 (size_t)255));
Bin Menga4092db2015-10-07 20:19:16 -0700340 }
Niklaus Giger29a4c242008-11-03 22:15:34 +0100341
Bin Meng9e98b7e2015-10-07 20:19:17 -0700342 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
343 (char *)bootaddr);
wdenk458ded32002-09-20 10:59:08 +0000344 }
345
Bin Mengb90ff0f2015-10-07 20:19:18 -0700346#ifdef CONFIG_X86
347 /*
348 * Since E820 information is critical to the kernel, if we don't
349 * specify these in the environments, use a default one.
350 */
351 tmp = getenv("e820data");
352 if (tmp)
353 data = (struct e820entry *)simple_strtoul(tmp, NULL, 16);
354 else
355 data = (struct e820entry *)VXWORKS_E820_DATA_ADDR;
356 tmp = getenv("e820info");
357 if (tmp)
358 info = (struct e820info *)simple_strtoul(tmp, NULL, 16);
359 else
360 info = (struct e820info *)VXWORKS_E820_INFO_ADDR;
361
362 memset(info, 0, sizeof(struct e820info));
363 info->sign = E820_SIGNATURE;
364 info->entries = install_e820_map(E820MAX, data);
365 info->addr = (info->entries - 1) * sizeof(struct e820entry) +
366 VXWORKS_E820_DATA_ADDR;
367#endif
368
wdenk8bde7f72003-06-27 21:31:46 +0000369 /*
370 * If the data at the load address is an elf image, then
371 * treat it like an elf image. Otherwise, assume that it is a
Bin Mengebca3df2015-10-07 20:19:13 -0700372 * binary image.
wdenk458ded32002-09-20 10:59:08 +0000373 */
Bin Mengebca3df2015-10-07 20:19:13 -0700374 if (valid_elf_image(addr))
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000375 addr = load_elf_image_shdr(addr);
Bin Mengebca3df2015-10-07 20:19:13 -0700376 else
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000377 puts("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000378
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000379 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000380
Reinhard Arlt6eee21d2011-11-18 09:06:52 +0000381 dcache_disable();
Bin Mengebca3df2015-10-07 20:19:13 -0700382 ((void (*)(int))addr)(0);
wdenk458ded32002-09-20 10:59:08 +0000383
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000384 puts("## vxWorks terminated\n");
Bin Mengebca3df2015-10-07 20:19:13 -0700385
wdenk458ded32002-09-20 10:59:08 +0000386 return 1;
387}
388
wdenk0d498392003-07-01 21:06:45 +0000389U_BOOT_CMD(
Bin Mengebca3df2015-10-07 20:19:13 -0700390 bootelf, 3, 0, do_bootelf,
Peter Tyser2fb26042009-01-27 18:03:12 -0600391 "Boot from an ELF image in memory",
Mike Frysingerf44a9282010-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)"
wdenk8bde7f72003-06-27 21:31:46 +0000395);
396
wdenk0d498392003-07-01 21:06:45 +0000397U_BOOT_CMD(
Bin Mengebca3df2015-10-07 20:19:13 -0700398 bootvx, 2, 0, do_bootvx,
Peter Tyser2fb26042009-01-27 18:03:12 -0600399 "Boot vxWorks from an ELF image",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200400 " [address] - load address of vxWorks ELF image."
wdenk8bde7f72003-06-27 21:31:46 +0000401);