blob: 8266bba0e6935063263e4db3bb8c983316be5731 [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 Frysinger017e9b72008-04-13 19:42:18 -040027int valid_elf_image (unsigned long addr);
Mike Frysingerf44a9282010-10-02 15:44:53 -040028static unsigned long load_elf_image_phdr(unsigned long addr);
29static unsigned long load_elf_image_shdr(unsigned long addr);
Mike Frysinger017e9b72008-04-13 19:42:18 -040030
31/* Allow ports to override the default behavior */
32__attribute__((weak))
Wolfgang Denk54841ab2010-06-28 22:00:46 +020033unsigned long do_bootelf_exec (ulong (*entry)(int, char * const[]),
34 int argc, char * const argv[])
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050035{
Mike Frysinger017e9b72008-04-13 19:42:18 -040036 unsigned long ret;
37
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050038 /*
39 * QNX images require the data cache is disabled.
40 * Data cache is already flushed, so just turn it off.
41 */
Mike Frysinger017e9b72008-04-13 19:42:18 -040042 int dcache = dcache_status ();
43 if (dcache)
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050044 dcache_disable ();
45
Mike Frysinger017e9b72008-04-13 19:42:18 -040046 /*
47 * pass address parameter as argv[0] (aka command name),
48 * and all remaining args
49 */
50 ret = entry (argc, argv);
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050051
Mike Frysinger017e9b72008-04-13 19:42:18 -040052 if (dcache)
53 dcache_enable ();
54
55 return ret;
56}
wdenk458ded32002-09-20 10:59:08 +000057
58/* ======================================================================
59 * Interpreter command to boot an arbitrary ELF image from memory.
60 * ====================================================================== */
Wolfgang Denk54841ab2010-06-28 22:00:46 +020061int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +000062{
63 unsigned long addr; /* Address of the ELF image */
64 unsigned long rc; /* Return value from user code */
Mike Frysingerf44a9282010-10-02 15:44:53 -040065 char *sload, *saddr;
wdenk458ded32002-09-20 10:59:08 +000066
67 /* -------------------------------------------------- */
68 int rcode = 0;
69
Mike Frysingerf44a9282010-10-02 15:44:53 -040070 sload = saddr = NULL;
71 if (argc == 3) {
72 sload = argv[1];
73 saddr = argv[2];
74 } else if (argc == 2) {
75 if (argv[1][0] == '-')
76 sload = argv[1];
77 else
78 saddr = argv[1];
79 }
80
81 if (saddr)
82 addr = simple_strtoul(saddr, NULL, 16);
wdenk458ded32002-09-20 10:59:08 +000083 else
Mike Frysingerf44a9282010-10-02 15:44:53 -040084 addr = load_addr;
wdenk458ded32002-09-20 10:59:08 +000085
86 if (!valid_elf_image (addr))
87 return 1;
88
Mike Frysingerf44a9282010-10-02 15:44:53 -040089 if (sload && sload[1] == 'p')
90 addr = load_elf_image_phdr(addr);
91 else
92 addr = load_elf_image_shdr(addr);
wdenk458ded32002-09-20 10:59:08 +000093
94 printf ("## Starting application at 0x%08lx ...\n", addr);
95
wdenk458ded32002-09-20 10:59:08 +000096 /*
97 * pass address parameter as argv[0] (aka command name),
98 * and all remaining args
99 */
Mike Frysinger017e9b72008-04-13 19:42:18 -0400100 rc = do_bootelf_exec ((void *)addr, argc - 1, argv + 1);
wdenk458ded32002-09-20 10:59:08 +0000101 if (rc != 0)
102 rcode = 1;
103
104 printf ("## Application terminated, rc = 0x%lx\n", rc);
105 return rcode;
106}
107
108/* ======================================================================
109 * Interpreter command to boot VxWorks from a memory image. The image can
110 * be either an ELF image or a raw binary. Will attempt to setup the
111 * bootline and other parameters correctly.
112 * ====================================================================== */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200113int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +0000114{
wdenk458ded32002-09-20 10:59:08 +0000115 unsigned long addr; /* Address of image */
116 unsigned long bootaddr; /* Address to put the bootline */
117 char *bootline; /* Text of the bootline */
118 char *tmp; /* Temporary char pointer */
Niklaus Giger29a4c242008-11-03 22:15:34 +0100119 char build_buf[128]; /* Buffer for building the bootline */
wdenk458ded32002-09-20 10:59:08 +0000120
Niklaus Giger29a4c242008-11-03 22:15:34 +0100121 /* ---------------------------------------------------
122 *
wdenk458ded32002-09-20 10:59:08 +0000123 * Check the loadaddr variable.
124 * If we don't know where the image is then we're done.
125 */
126
Stefan Roese1bdd4682006-11-29 12:53:15 +0100127 if (argc < 2)
128 addr = load_addr;
129 else
130 addr = simple_strtoul (argv[1], NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000131
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500132#if defined(CONFIG_CMD_NET)
wdenk458ded32002-09-20 10:59:08 +0000133 /* Check to see if we need to tftp the image ourselves before starting */
134
135 if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) {
Simon Glasse4bf0c52011-10-24 18:00:02 +0000136 if (NetLoop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000137 return 1;
Simon Glasse4bf0c52011-10-24 18:00:02 +0000138 printf("Automatic boot of VxWorks image at address 0x%08lx "
139 "...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000140 }
141#endif
142
143 /* This should equate
144 * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
145 * from the VxWorks BSP header files.
146 * This will vary from board to board
147 */
148
Stefan Roesec157d8e2005-08-01 16:41:48 +0200149#if defined(CONFIG_WALNUT)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200150 tmp = (char *) CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
Heiko Schocher76756e42009-03-26 07:33:59 +0100151 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger62c93d92009-02-11 18:51:43 -0500152 memcpy(tmp, &build_buf[3], 3);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200153#elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
154 tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR;
Heiko Schocher76756e42009-03-26 07:33:59 +0100155 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger62c93d92009-02-11 18:51:43 -0500156 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000157#else
wdenk4b9206e2004-03-23 22:14:11 +0000158 puts ("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000159#endif
160
wdenk8bde7f72003-06-27 21:31:46 +0000161 /*
162 * Use bootaddr to find the location in memory that VxWorks
163 * will look for the bootline string. The default value for
164 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
165 * defaults to 0x4200
wdenk458ded32002-09-20 10:59:08 +0000166 */
167
168 if ((tmp = getenv ("bootaddr")) == NULL)
Niklaus Giger29a4c242008-11-03 22:15:34 +0100169 bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR;
wdenk458ded32002-09-20 10:59:08 +0000170 else
171 bootaddr = simple_strtoul (tmp, NULL, 16);
172
wdenk8bde7f72003-06-27 21:31:46 +0000173 /*
174 * Check to see if the bootline is defined in the 'bootargs'
175 * parameter. If it is not defined, we may be able to
176 * construct the info
wdenk458ded32002-09-20 10:59:08 +0000177 */
178
179 if ((bootline = getenv ("bootargs")) != NULL) {
Niklaus Giger29a4c242008-11-03 22:15:34 +0100180 memcpy ((void *) bootaddr, bootline,
181 max (strlen (bootline), 255));
182 flush_cache (bootaddr, max (strlen (bootline), 255));
wdenk458ded32002-09-20 10:59:08 +0000183 } else {
wdenk458ded32002-09-20 10:59:08 +0000184
Niklaus Giger29a4c242008-11-03 22:15:34 +0100185
186 sprintf (build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE);
187 if ((tmp = getenv ("bootfile")) != NULL) {
188 sprintf (&build_buf[strlen (build_buf)],
189 "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp);
wdenk458ded32002-09-20 10:59:08 +0000190 } else {
Niklaus Giger29a4c242008-11-03 22:15:34 +0100191 sprintf (&build_buf[strlen (build_buf)],
192 "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME);
wdenk458ded32002-09-20 10:59:08 +0000193 }
194
195 if ((tmp = getenv ("ipaddr")) != NULL) {
Niklaus Giger29a4c242008-11-03 22:15:34 +0100196 sprintf (&build_buf[strlen (build_buf)], "e=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000197 }
Niklaus Giger29a4c242008-11-03 22:15:34 +0100198
199 if ((tmp = getenv ("serverip")) != NULL) {
200 sprintf (&build_buf[strlen (build_buf)], "h=%s ", tmp);
201 }
wdenk458ded32002-09-20 10:59:08 +0000202
203 if ((tmp = getenv ("hostname")) != NULL) {
Niklaus Giger29a4c242008-11-03 22:15:34 +0100204 sprintf (&build_buf[strlen (build_buf)], "tn=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000205 }
Niklaus Giger29a4c242008-11-03 22:15:34 +0100206#ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
207 sprintf (&build_buf[strlen (build_buf)],
208 CONFIG_SYS_VXWORKS_ADD_PARAMS);
wdenk458ded32002-09-20 10:59:08 +0000209#endif
Niklaus Giger29a4c242008-11-03 22:15:34 +0100210
211 memcpy ((void *) bootaddr, build_buf,
212 max (strlen (build_buf), 255));
213 flush_cache (bootaddr, max (strlen (build_buf), 255));
wdenk458ded32002-09-20 10:59:08 +0000214 }
215
wdenk8bde7f72003-06-27 21:31:46 +0000216 /*
217 * If the data at the load address is an elf image, then
218 * treat it like an elf image. Otherwise, assume that it is a
219 * binary image
wdenk458ded32002-09-20 10:59:08 +0000220 */
221
222 if (valid_elf_image (addr)) {
Mike Frysingerf44a9282010-10-02 15:44:53 -0400223 addr = load_elf_image_shdr (addr);
wdenk458ded32002-09-20 10:59:08 +0000224 } else {
wdenk4b9206e2004-03-23 22:14:11 +0000225 puts ("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000226 /* leave addr as load_addr */
227 }
228
229 printf ("## Using bootline (@ 0x%lx): %s\n", bootaddr,
230 (char *) bootaddr);
231 printf ("## Starting vxWorks at 0x%08lx ...\n", addr);
232
Reinhard Arlt6eee21d2011-11-18 09:06:52 +0000233 dcache_disable();
234 ((void (*)(int)) addr) (0);
wdenk458ded32002-09-20 10:59:08 +0000235
wdenk4b9206e2004-03-23 22:14:11 +0000236 puts ("## vxWorks terminated\n");
wdenk458ded32002-09-20 10:59:08 +0000237 return 1;
238}
239
240/* ======================================================================
241 * Determine if a valid ELF image exists at the given memory location.
242 * First looks at the ELF header magic field, the makes sure that it is
243 * executable and makes sure that it is for a PowerPC.
244 * ====================================================================== */
245int valid_elf_image (unsigned long addr)
246{
247 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
248
249 /* -------------------------------------------------- */
250
251 ehdr = (Elf32_Ehdr *) addr;
252
253 if (!IS_ELF (*ehdr)) {
254 printf ("## No elf image at address 0x%08lx\n", addr);
255 return 0;
256 }
257
258 if (ehdr->e_type != ET_EXEC) {
Niklaus Giger29a4c242008-11-03 22:15:34 +0100259 printf ("## Not a 32-bit elf image at address 0x%08lx\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000260 return 0;
261 }
262
wdenk6069ff22003-02-28 00:49:47 +0000263#if 0
wdenk458ded32002-09-20 10:59:08 +0000264 if (ehdr->e_machine != EM_PPC) {
265 printf ("## Not a PowerPC elf image at address 0x%08lx\n",
266 addr);
267 return 0;
268 }
wdenk6069ff22003-02-28 00:49:47 +0000269#endif
wdenk458ded32002-09-20 10:59:08 +0000270
271 return 1;
272}
273
wdenk458ded32002-09-20 10:59:08 +0000274/* ======================================================================
275 * A very simple elf loader, assumes the image is valid, returns the
276 * entry point address.
277 * ====================================================================== */
Mike Frysingerf44a9282010-10-02 15:44:53 -0400278static unsigned long load_elf_image_phdr(unsigned long addr)
279{
280 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
281 Elf32_Phdr *phdr; /* Program header structure pointer */
282 int i;
283
284 ehdr = (Elf32_Ehdr *) addr;
285 phdr = (Elf32_Phdr *) (addr + ehdr->e_phoff);
286
287 /* Load each program header */
288 for (i = 0; i < ehdr->e_phnum; ++i) {
289 void *dst = (void *) phdr->p_paddr;
290 void *src = (void *) addr + phdr->p_offset;
291 debug("Loading phdr %i to 0x%p (%i bytes)\n",
292 i, dst, phdr->p_filesz);
293 if (phdr->p_filesz)
294 memcpy(dst, src, phdr->p_filesz);
295 if (phdr->p_filesz != phdr->p_memsz)
296 memset(dst + phdr->p_filesz, 0x00, phdr->p_memsz - phdr->p_filesz);
297 flush_cache((unsigned long)dst, phdr->p_filesz);
298 ++phdr;
299 }
300
301 return ehdr->e_entry;
302}
303
304static unsigned long load_elf_image_shdr(unsigned long addr)
wdenk458ded32002-09-20 10:59:08 +0000305{
306 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
307 Elf32_Shdr *shdr; /* Section header structure pointer */
308 unsigned char *strtab = 0; /* String table pointer */
309 unsigned char *image; /* Binary image pointer */
310 int i; /* Loop counter */
311
312 /* -------------------------------------------------- */
313
314 ehdr = (Elf32_Ehdr *) addr;
315
316 /* Find the section header string table for output info */
317 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
318 (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
319
320 if (shdr->sh_type == SHT_STRTAB)
321 strtab = (unsigned char *) (addr + shdr->sh_offset);
322
323 /* Load each appropriate section */
324 for (i = 0; i < ehdr->e_shnum; ++i) {
325 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
326 (i * sizeof (Elf32_Shdr)));
327
328 if (!(shdr->sh_flags & SHF_ALLOC)
329 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
330 continue;
331 }
332
333 if (strtab) {
Niklaus Giger3c972842009-07-23 23:31:58 +0200334 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
wdenk458ded32002-09-20 10:59:08 +0000335 (shdr->sh_type == SHT_NOBITS) ?
336 "Clear" : "Load",
337 &strtab[shdr->sh_name],
338 (unsigned long) shdr->sh_addr,
339 (long) shdr->sh_size);
340 }
341
342 if (shdr->sh_type == SHT_NOBITS) {
343 memset ((void *)shdr->sh_addr, 0, shdr->sh_size);
344 } else {
345 image = (unsigned char *) addr + shdr->sh_offset;
346 memcpy ((void *) shdr->sh_addr,
347 (const void *) image,
348 shdr->sh_size);
349 }
350 flush_cache (shdr->sh_addr, shdr->sh_size);
351 }
352
353 return ehdr->e_entry;
354}
355
356/* ====================================================================== */
wdenk0d498392003-07-01 21:06:45 +0000357U_BOOT_CMD(
Mike Frysingerf44a9282010-10-02 15:44:53 -0400358 bootelf, 3, 0, do_bootelf,
Peter Tyser2fb26042009-01-27 18:03:12 -0600359 "Boot from an ELF image in memory",
Mike Frysingerf44a9282010-10-02 15:44:53 -0400360 "[-p|-s] [address]\n"
361 "\t- load ELF image at [address] via program headers (-p)\n"
362 "\t or via section headers (-s)"
wdenk8bde7f72003-06-27 21:31:46 +0000363);
364
wdenk0d498392003-07-01 21:06:45 +0000365U_BOOT_CMD(
366 bootvx, 2, 0, do_bootvx,
Peter Tyser2fb26042009-01-27 18:03:12 -0600367 "Boot vxWorks from an ELF image",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200368 " [address] - load address of vxWorks ELF image."
wdenk8bde7f72003-06-27 21:31:46 +0000369);