blob: 104d6e68b3cf308930c4ae8ef4613469459ed16a [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);
28unsigned long load_elf_image (unsigned long addr);
29
30/* Allow ports to override the default behavior */
31__attribute__((weak))
Wolfgang Denk54841ab2010-06-28 22:00:46 +020032unsigned long do_bootelf_exec (ulong (*entry)(int, char * const[]),
33 int argc, char * const argv[])
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050034{
Mike Frysinger017e9b72008-04-13 19:42:18 -040035 unsigned long ret;
36
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050037 /*
38 * QNX images require the data cache is disabled.
39 * Data cache is already flushed, so just turn it off.
40 */
Mike Frysinger017e9b72008-04-13 19:42:18 -040041 int dcache = dcache_status ();
42 if (dcache)
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050043 dcache_disable ();
44
Mike Frysinger017e9b72008-04-13 19:42:18 -040045 /*
46 * pass address parameter as argv[0] (aka command name),
47 * and all remaining args
48 */
49 ret = entry (argc, argv);
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050050
Mike Frysinger017e9b72008-04-13 19:42:18 -040051 if (dcache)
52 dcache_enable ();
53
54 return ret;
55}
wdenk458ded32002-09-20 10:59:08 +000056
57/* ======================================================================
58 * Interpreter command to boot an arbitrary ELF image from memory.
59 * ====================================================================== */
Wolfgang Denk54841ab2010-06-28 22:00:46 +020060int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +000061{
62 unsigned long addr; /* Address of the ELF image */
63 unsigned long rc; /* Return value from user code */
64
65 /* -------------------------------------------------- */
66 int rcode = 0;
67
68 if (argc < 2)
69 addr = load_addr;
70 else
71 addr = simple_strtoul (argv[1], NULL, 16);
72
73 if (!valid_elf_image (addr))
74 return 1;
75
76 addr = load_elf_image (addr);
77
78 printf ("## Starting application at 0x%08lx ...\n", addr);
79
wdenk458ded32002-09-20 10:59:08 +000080 /*
81 * pass address parameter as argv[0] (aka command name),
82 * and all remaining args
83 */
Mike Frysinger017e9b72008-04-13 19:42:18 -040084 rc = do_bootelf_exec ((void *)addr, argc - 1, argv + 1);
wdenk458ded32002-09-20 10:59:08 +000085 if (rc != 0)
86 rcode = 1;
87
88 printf ("## Application terminated, rc = 0x%lx\n", rc);
89 return rcode;
90}
91
92/* ======================================================================
93 * Interpreter command to boot VxWorks from a memory image. The image can
94 * be either an ELF image or a raw binary. Will attempt to setup the
95 * bootline and other parameters correctly.
96 * ====================================================================== */
Wolfgang Denk54841ab2010-06-28 22:00:46 +020097int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +000098{
wdenk458ded32002-09-20 10:59:08 +000099 unsigned long addr; /* Address of image */
100 unsigned long bootaddr; /* Address to put the bootline */
101 char *bootline; /* Text of the bootline */
102 char *tmp; /* Temporary char pointer */
Niklaus Giger29a4c242008-11-03 22:15:34 +0100103 char build_buf[128]; /* Buffer for building the bootline */
wdenk458ded32002-09-20 10:59:08 +0000104
Niklaus Giger29a4c242008-11-03 22:15:34 +0100105 /* ---------------------------------------------------
106 *
wdenk458ded32002-09-20 10:59:08 +0000107 * Check the loadaddr variable.
108 * If we don't know where the image is then we're done.
109 */
110
Stefan Roese1bdd4682006-11-29 12:53:15 +0100111 if (argc < 2)
112 addr = load_addr;
113 else
114 addr = simple_strtoul (argv[1], NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000115
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500116#if defined(CONFIG_CMD_NET)
wdenk458ded32002-09-20 10:59:08 +0000117 /* Check to see if we need to tftp the image ourselves before starting */
118
119 if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) {
wdenkeb9401e2002-11-11 02:11:37 +0000120 if (NetLoop (TFTP) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000121 return 1;
Niklaus Giger29a4c242008-11-03 22:15:34 +0100122 printf ("Automatic boot of VxWorks image at address 0x%08lx ... \n",
123 addr);
wdenk458ded32002-09-20 10:59:08 +0000124 }
125#endif
126
127 /* This should equate
128 * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
129 * from the VxWorks BSP header files.
130 * This will vary from board to board
131 */
132
Stefan Roesec157d8e2005-08-01 16:41:48 +0200133#if defined(CONFIG_WALNUT)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200134 tmp = (char *) CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
Heiko Schocher76756e42009-03-26 07:33:59 +0100135 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger62c93d92009-02-11 18:51:43 -0500136 memcpy(tmp, &build_buf[3], 3);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200137#elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
138 tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR;
Heiko Schocher76756e42009-03-26 07:33:59 +0100139 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger62c93d92009-02-11 18:51:43 -0500140 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000141#else
wdenk4b9206e2004-03-23 22:14:11 +0000142 puts ("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000143#endif
144
wdenk8bde7f72003-06-27 21:31:46 +0000145 /*
146 * Use bootaddr to find the location in memory that VxWorks
147 * will look for the bootline string. The default value for
148 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
149 * defaults to 0x4200
wdenk458ded32002-09-20 10:59:08 +0000150 */
151
152 if ((tmp = getenv ("bootaddr")) == NULL)
Niklaus Giger29a4c242008-11-03 22:15:34 +0100153 bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR;
wdenk458ded32002-09-20 10:59:08 +0000154 else
155 bootaddr = simple_strtoul (tmp, NULL, 16);
156
wdenk8bde7f72003-06-27 21:31:46 +0000157 /*
158 * Check to see if the bootline is defined in the 'bootargs'
159 * parameter. If it is not defined, we may be able to
160 * construct the info
wdenk458ded32002-09-20 10:59:08 +0000161 */
162
163 if ((bootline = getenv ("bootargs")) != NULL) {
Niklaus Giger29a4c242008-11-03 22:15:34 +0100164 memcpy ((void *) bootaddr, bootline,
165 max (strlen (bootline), 255));
166 flush_cache (bootaddr, max (strlen (bootline), 255));
wdenk458ded32002-09-20 10:59:08 +0000167 } else {
wdenk458ded32002-09-20 10:59:08 +0000168
Niklaus Giger29a4c242008-11-03 22:15:34 +0100169
170 sprintf (build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE);
171 if ((tmp = getenv ("bootfile")) != NULL) {
172 sprintf (&build_buf[strlen (build_buf)],
173 "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp);
wdenk458ded32002-09-20 10:59:08 +0000174 } else {
Niklaus Giger29a4c242008-11-03 22:15:34 +0100175 sprintf (&build_buf[strlen (build_buf)],
176 "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME);
wdenk458ded32002-09-20 10:59:08 +0000177 }
178
179 if ((tmp = getenv ("ipaddr")) != NULL) {
Niklaus Giger29a4c242008-11-03 22:15:34 +0100180 sprintf (&build_buf[strlen (build_buf)], "e=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000181 }
Niklaus Giger29a4c242008-11-03 22:15:34 +0100182
183 if ((tmp = getenv ("serverip")) != NULL) {
184 sprintf (&build_buf[strlen (build_buf)], "h=%s ", tmp);
185 }
wdenk458ded32002-09-20 10:59:08 +0000186
187 if ((tmp = getenv ("hostname")) != NULL) {
Niklaus Giger29a4c242008-11-03 22:15:34 +0100188 sprintf (&build_buf[strlen (build_buf)], "tn=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000189 }
Niklaus Giger29a4c242008-11-03 22:15:34 +0100190#ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
191 sprintf (&build_buf[strlen (build_buf)],
192 CONFIG_SYS_VXWORKS_ADD_PARAMS);
wdenk458ded32002-09-20 10:59:08 +0000193#endif
Niklaus Giger29a4c242008-11-03 22:15:34 +0100194
195 memcpy ((void *) bootaddr, build_buf,
196 max (strlen (build_buf), 255));
197 flush_cache (bootaddr, max (strlen (build_buf), 255));
wdenk458ded32002-09-20 10:59:08 +0000198 }
199
wdenk8bde7f72003-06-27 21:31:46 +0000200 /*
201 * If the data at the load address is an elf image, then
202 * treat it like an elf image. Otherwise, assume that it is a
203 * binary image
wdenk458ded32002-09-20 10:59:08 +0000204 */
205
206 if (valid_elf_image (addr)) {
207 addr = load_elf_image (addr);
208 } else {
wdenk4b9206e2004-03-23 22:14:11 +0000209 puts ("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000210 /* leave addr as load_addr */
211 }
212
213 printf ("## Using bootline (@ 0x%lx): %s\n", bootaddr,
214 (char *) bootaddr);
215 printf ("## Starting vxWorks at 0x%08lx ...\n", addr);
216
217 ((void (*)(void)) addr) ();
218
wdenk4b9206e2004-03-23 22:14:11 +0000219 puts ("## vxWorks terminated\n");
wdenk458ded32002-09-20 10:59:08 +0000220 return 1;
221}
222
223/* ======================================================================
224 * Determine if a valid ELF image exists at the given memory location.
225 * First looks at the ELF header magic field, the makes sure that it is
226 * executable and makes sure that it is for a PowerPC.
227 * ====================================================================== */
228int valid_elf_image (unsigned long addr)
229{
230 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
231
232 /* -------------------------------------------------- */
233
234 ehdr = (Elf32_Ehdr *) addr;
235
236 if (!IS_ELF (*ehdr)) {
237 printf ("## No elf image at address 0x%08lx\n", addr);
238 return 0;
239 }
240
241 if (ehdr->e_type != ET_EXEC) {
Niklaus Giger29a4c242008-11-03 22:15:34 +0100242 printf ("## Not a 32-bit elf image at address 0x%08lx\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000243 return 0;
244 }
245
wdenk6069ff22003-02-28 00:49:47 +0000246#if 0
wdenk458ded32002-09-20 10:59:08 +0000247 if (ehdr->e_machine != EM_PPC) {
248 printf ("## Not a PowerPC elf image at address 0x%08lx\n",
249 addr);
250 return 0;
251 }
wdenk6069ff22003-02-28 00:49:47 +0000252#endif
wdenk458ded32002-09-20 10:59:08 +0000253
254 return 1;
255}
256
wdenk458ded32002-09-20 10:59:08 +0000257/* ======================================================================
258 * A very simple elf loader, assumes the image is valid, returns the
259 * entry point address.
260 * ====================================================================== */
261unsigned long load_elf_image (unsigned long addr)
262{
263 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
264 Elf32_Shdr *shdr; /* Section header structure pointer */
265 unsigned char *strtab = 0; /* String table pointer */
266 unsigned char *image; /* Binary image pointer */
267 int i; /* Loop counter */
268
269 /* -------------------------------------------------- */
270
271 ehdr = (Elf32_Ehdr *) addr;
272
273 /* Find the section header string table for output info */
274 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
275 (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
276
277 if (shdr->sh_type == SHT_STRTAB)
278 strtab = (unsigned char *) (addr + shdr->sh_offset);
279
280 /* Load each appropriate section */
281 for (i = 0; i < ehdr->e_shnum; ++i) {
282 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
283 (i * sizeof (Elf32_Shdr)));
284
285 if (!(shdr->sh_flags & SHF_ALLOC)
286 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
287 continue;
288 }
289
290 if (strtab) {
Niklaus Giger3c972842009-07-23 23:31:58 +0200291 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
wdenk458ded32002-09-20 10:59:08 +0000292 (shdr->sh_type == SHT_NOBITS) ?
293 "Clear" : "Load",
294 &strtab[shdr->sh_name],
295 (unsigned long) shdr->sh_addr,
296 (long) shdr->sh_size);
297 }
298
299 if (shdr->sh_type == SHT_NOBITS) {
300 memset ((void *)shdr->sh_addr, 0, shdr->sh_size);
301 } else {
302 image = (unsigned char *) addr + shdr->sh_offset;
303 memcpy ((void *) shdr->sh_addr,
304 (const void *) image,
305 shdr->sh_size);
306 }
307 flush_cache (shdr->sh_addr, shdr->sh_size);
308 }
309
310 return ehdr->e_entry;
311}
312
313/* ====================================================================== */
wdenk0d498392003-07-01 21:06:45 +0000314U_BOOT_CMD(
315 bootelf, 2, 0, do_bootelf,
Peter Tyser2fb26042009-01-27 18:03:12 -0600316 "Boot from an ELF image in memory",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200317 " [address] - load address of ELF image."
wdenk8bde7f72003-06-27 21:31:46 +0000318);
319
wdenk0d498392003-07-01 21:06:45 +0000320U_BOOT_CMD(
321 bootvx, 2, 0, do_bootvx,
Peter Tyser2fb26042009-01-27 18:03:12 -0600322 "Boot vxWorks from an ELF image",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200323 " [address] - load address of vxWorks ELF image."
wdenk8bde7f72003-06-27 21:31:46 +0000324);