blob: 2f229d739ac9c0e668735168f664e1a547c85f09 [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>
18#include <linux/ctype.h>
19#include <net.h>
wdenk458ded32002-09-20 10:59:08 +000020#include <elf.h>
Niklaus Giger29a4c242008-11-03 22:15:34 +010021#include <vxworks.h>
wdenk458ded32002-09-20 10:59:08 +000022
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020023#if defined(CONFIG_WALNUT) || defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Wolfgang Denkd87080b2006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
25#endif
wdenk458ded32002-09-20 10:59:08 +000026
Mike Frysingerf44a9282010-10-02 15:44:53 -040027static unsigned long load_elf_image_phdr(unsigned long addr);
28static unsigned long load_elf_image_shdr(unsigned long addr);
Mike Frysinger017e9b72008-04-13 19:42:18 -040029
30/* Allow ports to override the default behavior */
Jeroen Hofstee553d8c32014-10-08 22:57:31 +020031static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
Wolfgang Denk54841ab2010-06-28 22:00:46 +020032 int argc, char * const argv[])
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050033{
Mike Frysinger017e9b72008-04-13 19:42:18 -040034 unsigned long ret;
35
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050036 /*
37 * QNX images require the data cache is disabled.
38 * Data cache is already flushed, so just turn it off.
39 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000040 int dcache = dcache_status();
Mike Frysinger017e9b72008-04-13 19:42:18 -040041 if (dcache)
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000042 dcache_disable();
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050043
Mike Frysinger017e9b72008-04-13 19:42:18 -040044 /*
45 * pass address parameter as argv[0] (aka command name),
46 * and all remaining args
47 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000048 ret = entry(argc, argv);
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050049
Mike Frysinger017e9b72008-04-13 19:42:18 -040050 if (dcache)
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000051 dcache_enable();
Mike Frysinger017e9b72008-04-13 19:42:18 -040052
53 return ret;
54}
wdenk458ded32002-09-20 10:59:08 +000055
56/* ======================================================================
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000057 * Determine if a valid ELF image exists at the given memory location.
58 * First looks at the ELF header magic field, the makes sure that it is
59 * executable and makes sure that it is for a PowerPC.
60 * ====================================================================== */
61int valid_elf_image(unsigned long addr)
62{
63 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
64
65 /* -------------------------------------------------- */
66
67 ehdr = (Elf32_Ehdr *) addr;
68
69 if (!IS_ELF(*ehdr)) {
70 printf("## No elf image at address 0x%08lx\n", addr);
71 return 0;
72 }
73
74 if (ehdr->e_type != ET_EXEC) {
75 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
76 return 0;
77 }
78
79#if 0
80 if (ehdr->e_machine != EM_PPC) {
81 printf("## Not a PowerPC elf image at address 0x%08lx\n", addr);
82 return 0;
83 }
84#endif
85
86 return 1;
87}
88
89/* ======================================================================
wdenk458ded32002-09-20 10:59:08 +000090 * Interpreter command to boot an arbitrary ELF image from memory.
91 * ====================================================================== */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000092int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +000093{
94 unsigned long addr; /* Address of the ELF image */
95 unsigned long rc; /* Return value from user code */
Mike Frysingerf44a9282010-10-02 15:44:53 -040096 char *sload, *saddr;
wdenk458ded32002-09-20 10:59:08 +000097
98 /* -------------------------------------------------- */
99 int rcode = 0;
100
Mike Frysingerf44a9282010-10-02 15:44:53 -0400101 sload = saddr = NULL;
102 if (argc == 3) {
103 sload = argv[1];
104 saddr = argv[2];
105 } else if (argc == 2) {
106 if (argv[1][0] == '-')
107 sload = argv[1];
108 else
109 saddr = argv[1];
110 }
111
112 if (saddr)
113 addr = simple_strtoul(saddr, NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000114 else
Mike Frysingerf44a9282010-10-02 15:44:53 -0400115 addr = load_addr;
wdenk458ded32002-09-20 10:59:08 +0000116
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000117 if (!valid_elf_image(addr))
wdenk458ded32002-09-20 10:59:08 +0000118 return 1;
119
Mike Frysingerf44a9282010-10-02 15:44:53 -0400120 if (sload && sload[1] == 'p')
121 addr = load_elf_image_phdr(addr);
122 else
123 addr = load_elf_image_shdr(addr);
wdenk458ded32002-09-20 10:59:08 +0000124
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000125 printf("## Starting application at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000126
wdenk458ded32002-09-20 10:59:08 +0000127 /*
128 * pass address parameter as argv[0] (aka command name),
129 * and all remaining args
130 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000131 rc = do_bootelf_exec((void *)addr, argc - 1, argv + 1);
wdenk458ded32002-09-20 10:59:08 +0000132 if (rc != 0)
133 rcode = 1;
134
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000135 printf("## Application terminated, rc = 0x%lx\n", rc);
wdenk458ded32002-09-20 10:59:08 +0000136 return rcode;
137}
138
139/* ======================================================================
140 * Interpreter command to boot VxWorks from a memory image. The image can
141 * be either an ELF image or a raw binary. Will attempt to setup the
142 * bootline and other parameters correctly.
143 * ====================================================================== */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000144int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +0000145{
wdenk458ded32002-09-20 10:59:08 +0000146 unsigned long addr; /* Address of image */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000147 unsigned long bootaddr; /* Address to put the bootline */
wdenk458ded32002-09-20 10:59:08 +0000148 char *bootline; /* Text of the bootline */
149 char *tmp; /* Temporary char pointer */
Niklaus Giger29a4c242008-11-03 22:15:34 +0100150 char build_buf[128]; /* Buffer for building the bootline */
wdenk458ded32002-09-20 10:59:08 +0000151
Niklaus Giger29a4c242008-11-03 22:15:34 +0100152 /* ---------------------------------------------------
153 *
wdenk458ded32002-09-20 10:59:08 +0000154 * Check the loadaddr variable.
155 * If we don't know where the image is then we're done.
156 */
157
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100158 if (argc < 2)
Stefan Roese1bdd4682006-11-29 12:53:15 +0100159 addr = load_addr;
160 else
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100161 addr = simple_strtoul(argv[1], NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000162
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500163#if defined(CONFIG_CMD_NET)
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000164 /*
165 * Check to see if we need to tftp the image ourselves before starting
166 */
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100167 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
Simon Glasse4bf0c52011-10-24 18:00:02 +0000168 if (NetLoop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000169 return 1;
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000170 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
171 addr);
wdenk458ded32002-09-20 10:59:08 +0000172 }
173#endif
174
175 /* This should equate
176 * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
177 * from the VxWorks BSP header files.
178 * This will vary from board to board
179 */
180
Stefan Roesec157d8e2005-08-01 16:41:48 +0200181#if defined(CONFIG_WALNUT)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200182 tmp = (char *) CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
Heiko Schocher76756e42009-03-26 07:33:59 +0100183 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger62c93d92009-02-11 18:51:43 -0500184 memcpy(tmp, &build_buf[3], 3);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200185#elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
186 tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR;
Heiko Schocher76756e42009-03-26 07:33:59 +0100187 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger62c93d92009-02-11 18:51:43 -0500188 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000189#else
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000190 puts("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000191#endif
192
wdenk8bde7f72003-06-27 21:31:46 +0000193 /*
194 * Use bootaddr to find the location in memory that VxWorks
195 * will look for the bootline string. The default value for
196 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
197 * defaults to 0x4200
wdenk458ded32002-09-20 10:59:08 +0000198 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000199 tmp = getenv("bootaddr");
Reinhard Arlt1b3e0b12013-02-04 09:37:11 +0000200 if (!tmp)
Niklaus Giger29a4c242008-11-03 22:15:34 +0100201 bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR;
wdenk458ded32002-09-20 10:59:08 +0000202 else
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000203 bootaddr = simple_strtoul(tmp, NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000204
wdenk8bde7f72003-06-27 21:31:46 +0000205 /*
206 * Check to see if the bootline is defined in the 'bootargs'
207 * parameter. If it is not defined, we may be able to
208 * construct the info
wdenk458ded32002-09-20 10:59:08 +0000209 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000210 bootline = getenv("bootargs");
211 if (bootline) {
212 memcpy((void *) bootaddr, bootline,
213 max(strlen(bootline), 255));
214 flush_cache(bootaddr, max(strlen(bootline), 255));
wdenk458ded32002-09-20 10:59:08 +0000215 } else {
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000216 sprintf(build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE);
217 tmp = getenv("bootfile");
218 if (tmp)
219 sprintf(&build_buf[strlen(build_buf)],
Niklaus Giger29a4c242008-11-03 22:15:34 +0100220 "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp);
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000221 else
222 sprintf(&build_buf[strlen(build_buf)],
Niklaus Giger29a4c242008-11-03 22:15:34 +0100223 "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME);
wdenk458ded32002-09-20 10:59:08 +0000224
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000225 tmp = getenv("ipaddr");
226 if (tmp)
227 sprintf(&build_buf[strlen(build_buf)], "e=%s ", tmp);
Niklaus Giger29a4c242008-11-03 22:15:34 +0100228
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000229 tmp = getenv("serverip");
230 if (tmp)
231 sprintf(&build_buf[strlen(build_buf)], "h=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000232
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000233 tmp = getenv("hostname");
234 if (tmp)
235 sprintf(&build_buf[strlen(build_buf)], "tn=%s ", tmp);
236
Niklaus Giger29a4c242008-11-03 22:15:34 +0100237#ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000238 sprintf(&build_buf[strlen(build_buf)],
Niklaus Giger29a4c242008-11-03 22:15:34 +0100239 CONFIG_SYS_VXWORKS_ADD_PARAMS);
wdenk458ded32002-09-20 10:59:08 +0000240#endif
Niklaus Giger29a4c242008-11-03 22:15:34 +0100241
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000242 memcpy((void *) bootaddr, build_buf,
243 max(strlen(build_buf), 255));
244 flush_cache(bootaddr, max(strlen(build_buf), 255));
wdenk458ded32002-09-20 10:59:08 +0000245 }
246
wdenk8bde7f72003-06-27 21:31:46 +0000247 /*
248 * If the data at the load address is an elf image, then
249 * treat it like an elf image. Otherwise, assume that it is a
250 * binary image
wdenk458ded32002-09-20 10:59:08 +0000251 */
252
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000253 if (valid_elf_image(addr)) {
254 addr = load_elf_image_shdr(addr);
wdenk458ded32002-09-20 10:59:08 +0000255 } else {
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000256 puts("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000257 /* leave addr as load_addr */
258 }
259
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000260 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
wdenk458ded32002-09-20 10:59:08 +0000261 (char *) bootaddr);
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000262 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000263
Reinhard Arlt6eee21d2011-11-18 09:06:52 +0000264 dcache_disable();
265 ((void (*)(int)) addr) (0);
wdenk458ded32002-09-20 10:59:08 +0000266
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000267 puts("## vxWorks terminated\n");
wdenk458ded32002-09-20 10:59:08 +0000268 return 1;
269}
270
wdenk458ded32002-09-20 10:59:08 +0000271/* ======================================================================
272 * A very simple elf loader, assumes the image is valid, returns the
273 * entry point address.
274 * ====================================================================== */
Mike Frysingerf44a9282010-10-02 15:44:53 -0400275static unsigned long load_elf_image_phdr(unsigned long addr)
276{
277 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
278 Elf32_Phdr *phdr; /* Program header structure pointer */
279 int i;
280
281 ehdr = (Elf32_Ehdr *) addr;
282 phdr = (Elf32_Phdr *) (addr + ehdr->e_phoff);
283
284 /* Load each program header */
285 for (i = 0; i < ehdr->e_phnum; ++i) {
Daniel Schwierzeckb0d60a92012-09-16 06:55:06 +0000286 void *dst = (void *)(uintptr_t) phdr->p_paddr;
Mike Frysingerf44a9282010-10-02 15:44:53 -0400287 void *src = (void *) addr + phdr->p_offset;
288 debug("Loading phdr %i to 0x%p (%i bytes)\n",
289 i, dst, phdr->p_filesz);
290 if (phdr->p_filesz)
291 memcpy(dst, src, phdr->p_filesz);
292 if (phdr->p_filesz != phdr->p_memsz)
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000293 memset(dst + phdr->p_filesz, 0x00,
294 phdr->p_memsz - phdr->p_filesz);
Mike Frysingerf44a9282010-10-02 15:44:53 -0400295 flush_cache((unsigned long)dst, phdr->p_filesz);
296 ++phdr;
297 }
298
299 return ehdr->e_entry;
300}
301
302static unsigned long load_elf_image_shdr(unsigned long addr)
wdenk458ded32002-09-20 10:59:08 +0000303{
304 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
305 Elf32_Shdr *shdr; /* Section header structure pointer */
306 unsigned char *strtab = 0; /* String table pointer */
307 unsigned char *image; /* Binary image pointer */
308 int i; /* Loop counter */
309
310 /* -------------------------------------------------- */
311
312 ehdr = (Elf32_Ehdr *) addr;
313
314 /* Find the section header string table for output info */
315 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000316 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
wdenk458ded32002-09-20 10:59:08 +0000317
318 if (shdr->sh_type == SHT_STRTAB)
319 strtab = (unsigned char *) (addr + shdr->sh_offset);
320
321 /* Load each appropriate section */
322 for (i = 0; i < ehdr->e_shnum; ++i) {
323 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000324 (i * sizeof(Elf32_Shdr)));
wdenk458ded32002-09-20 10:59:08 +0000325
326 if (!(shdr->sh_flags & SHF_ALLOC)
327 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
328 continue;
329 }
330
331 if (strtab) {
Niklaus Giger3c972842009-07-23 23:31:58 +0200332 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
wdenk458ded32002-09-20 10:59:08 +0000333 (shdr->sh_type == SHT_NOBITS) ?
334 "Clear" : "Load",
335 &strtab[shdr->sh_name],
336 (unsigned long) shdr->sh_addr,
337 (long) shdr->sh_size);
338 }
339
340 if (shdr->sh_type == SHT_NOBITS) {
Daniel Schwierzeckb0d60a92012-09-16 06:55:06 +0000341 memset((void *)(uintptr_t) shdr->sh_addr, 0,
342 shdr->sh_size);
wdenk458ded32002-09-20 10:59:08 +0000343 } else {
344 image = (unsigned char *) addr + shdr->sh_offset;
Daniel Schwierzeckb0d60a92012-09-16 06:55:06 +0000345 memcpy((void *)(uintptr_t) shdr->sh_addr,
wdenk458ded32002-09-20 10:59:08 +0000346 (const void *) image,
347 shdr->sh_size);
348 }
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000349 flush_cache(shdr->sh_addr, shdr->sh_size);
wdenk458ded32002-09-20 10:59:08 +0000350 }
351
352 return ehdr->e_entry;
353}
354
355/* ====================================================================== */
wdenk0d498392003-07-01 21:06:45 +0000356U_BOOT_CMD(
Mike Frysingerf44a9282010-10-02 15:44:53 -0400357 bootelf, 3, 0, do_bootelf,
Peter Tyser2fb26042009-01-27 18:03:12 -0600358 "Boot from an ELF image in memory",
Mike Frysingerf44a9282010-10-02 15:44:53 -0400359 "[-p|-s] [address]\n"
360 "\t- load ELF image at [address] via program headers (-p)\n"
361 "\t or via section headers (-s)"
wdenk8bde7f72003-06-27 21:31:46 +0000362);
363
wdenk0d498392003-07-01 21:06:45 +0000364U_BOOT_CMD(
365 bootvx, 2, 0, do_bootvx,
Peter Tyser2fb26042009-01-27 18:03:12 -0600366 "Boot vxWorks from an ELF image",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200367 " [address] - load address of vxWorks ELF image."
wdenk8bde7f72003-06-27 21:31:46 +0000368);