blob: 19e12493a2fd6aca245140e5faa75850fd62dcae [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 Gigerf662b232008-11-03 22:15:34 +010021#include <vxworks.h>
wdenk458ded32002-09-20 10:59:08 +000022
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020023#if defined(CONFIG_WALNUT) || defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Wolfgang Denk6405a152006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
25#endif
wdenk458ded32002-09-20 10:59:08 +000026
Mike Frysingere60644a2008-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))
32unsigned long do_bootelf_exec (ulong (*entry)(int, char *[]), int argc, char *argv[])
Mike Frysingerf79507e2008-01-29 18:21:05 -050033{
Mike Frysingere60644a2008-04-13 19:42:18 -040034 unsigned long ret;
35
Mike Frysingerf79507e2008-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 */
Mike Frysingere60644a2008-04-13 19:42:18 -040040 int dcache = dcache_status ();
41 if (dcache)
Mike Frysingerf79507e2008-01-29 18:21:05 -050042 dcache_disable ();
43
Mike Frysingere60644a2008-04-13 19:42:18 -040044 /*
45 * pass address parameter as argv[0] (aka command name),
46 * and all remaining args
47 */
48 ret = entry (argc, argv);
Mike Frysingerf79507e2008-01-29 18:21:05 -050049
Mike Frysingere60644a2008-04-13 19:42:18 -040050 if (dcache)
51 dcache_enable ();
52
53 return ret;
54}
wdenk458ded32002-09-20 10:59:08 +000055
56/* ======================================================================
57 * Interpreter command to boot an arbitrary ELF image from memory.
58 * ====================================================================== */
59int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
60{
61 unsigned long addr; /* Address of the ELF image */
62 unsigned long rc; /* Return value from user code */
63
64 /* -------------------------------------------------- */
65 int rcode = 0;
66
67 if (argc < 2)
68 addr = load_addr;
69 else
70 addr = simple_strtoul (argv[1], NULL, 16);
71
72 if (!valid_elf_image (addr))
73 return 1;
74
75 addr = load_elf_image (addr);
76
77 printf ("## Starting application at 0x%08lx ...\n", addr);
78
wdenk458ded32002-09-20 10:59:08 +000079 /*
80 * pass address parameter as argv[0] (aka command name),
81 * and all remaining args
82 */
Mike Frysingere60644a2008-04-13 19:42:18 -040083 rc = do_bootelf_exec ((void *)addr, argc - 1, argv + 1);
wdenk458ded32002-09-20 10:59:08 +000084 if (rc != 0)
85 rcode = 1;
86
87 printf ("## Application terminated, rc = 0x%lx\n", rc);
88 return rcode;
89}
90
91/* ======================================================================
92 * Interpreter command to boot VxWorks from a memory image. The image can
93 * be either an ELF image or a raw binary. Will attempt to setup the
94 * bootline and other parameters correctly.
95 * ====================================================================== */
Stefan Roese00487782006-11-29 12:53:15 +010096int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenk458ded32002-09-20 10:59:08 +000097{
wdenk458ded32002-09-20 10:59:08 +000098 unsigned long addr; /* Address of image */
99 unsigned long bootaddr; /* Address to put the bootline */
100 char *bootline; /* Text of the bootline */
101 char *tmp; /* Temporary char pointer */
Niklaus Gigerf662b232008-11-03 22:15:34 +0100102 char build_buf[128]; /* Buffer for building the bootline */
wdenk458ded32002-09-20 10:59:08 +0000103
Niklaus Gigerf662b232008-11-03 22:15:34 +0100104 /* ---------------------------------------------------
105 *
wdenk458ded32002-09-20 10:59:08 +0000106 * Check the loadaddr variable.
107 * If we don't know where the image is then we're done.
108 */
109
Stefan Roese00487782006-11-29 12:53:15 +0100110 if (argc < 2)
111 addr = load_addr;
112 else
113 addr = simple_strtoul (argv[1], NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000114
Jon Loeliger54324d02007-07-08 17:51:39 -0500115#if defined(CONFIG_CMD_NET)
wdenk458ded32002-09-20 10:59:08 +0000116 /* Check to see if we need to tftp the image ourselves before starting */
117
118 if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) {
wdenkc217f6d2002-11-11 02:11:37 +0000119 if (NetLoop (TFTP) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000120 return 1;
Niklaus Gigerf662b232008-11-03 22:15:34 +0100121 printf ("Automatic boot of VxWorks image at address 0x%08lx ... \n",
122 addr);
wdenk458ded32002-09-20 10:59:08 +0000123 }
124#endif
125
126 /* This should equate
127 * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
128 * from the VxWorks BSP header files.
129 * This will vary from board to board
130 */
131
Stefan Roese326c9712005-08-01 16:41:48 +0200132#if defined(CONFIG_WALNUT)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200133 tmp = (char *) CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
wdenk458ded32002-09-20 10:59:08 +0000134 memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[3], 3);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200135#elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
136 tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR;
wdenk458ded32002-09-20 10:59:08 +0000137 memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[0], 6);
138#else
wdenk42c05472004-03-23 22:14:11 +0000139 puts ("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000140#endif
141
wdenk57b2d802003-06-27 21:31:46 +0000142 /*
143 * Use bootaddr to find the location in memory that VxWorks
144 * will look for the bootline string. The default value for
145 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
146 * defaults to 0x4200
wdenk458ded32002-09-20 10:59:08 +0000147 */
148
149 if ((tmp = getenv ("bootaddr")) == NULL)
Niklaus Gigerf662b232008-11-03 22:15:34 +0100150 bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR;
wdenk458ded32002-09-20 10:59:08 +0000151 else
152 bootaddr = simple_strtoul (tmp, NULL, 16);
153
wdenk57b2d802003-06-27 21:31:46 +0000154 /*
155 * Check to see if the bootline is defined in the 'bootargs'
156 * parameter. If it is not defined, we may be able to
157 * construct the info
wdenk458ded32002-09-20 10:59:08 +0000158 */
159
160 if ((bootline = getenv ("bootargs")) != NULL) {
Niklaus Gigerf662b232008-11-03 22:15:34 +0100161 memcpy ((void *) bootaddr, bootline,
162 max (strlen (bootline), 255));
163 flush_cache (bootaddr, max (strlen (bootline), 255));
wdenk458ded32002-09-20 10:59:08 +0000164 } else {
wdenk458ded32002-09-20 10:59:08 +0000165
Niklaus Gigerf662b232008-11-03 22:15:34 +0100166
167 sprintf (build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE);
168 if ((tmp = getenv ("bootfile")) != NULL) {
169 sprintf (&build_buf[strlen (build_buf)],
170 "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp);
wdenk458ded32002-09-20 10:59:08 +0000171 } else {
Niklaus Gigerf662b232008-11-03 22:15:34 +0100172 sprintf (&build_buf[strlen (build_buf)],
173 "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME);
wdenk458ded32002-09-20 10:59:08 +0000174 }
175
176 if ((tmp = getenv ("ipaddr")) != NULL) {
Niklaus Gigerf662b232008-11-03 22:15:34 +0100177 sprintf (&build_buf[strlen (build_buf)], "e=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000178 }
wdenk458ded32002-09-20 10:59:08 +0000179
Niklaus Gigerf662b232008-11-03 22:15:34 +0100180 if ((tmp = getenv ("serverip")) != NULL) {
181 sprintf (&build_buf[strlen (build_buf)], "h=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000182 }
183
Niklaus Gigerf662b232008-11-03 22:15:34 +0100184 if ((tmp = getenv ("hostname")) != NULL) {
185 sprintf (&build_buf[strlen (build_buf)], "tn=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000186 }
Niklaus Gigerf662b232008-11-03 22:15:34 +0100187#ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
188 sprintf (&build_buf[strlen (build_buf)],
189 CONFIG_SYS_VXWORKS_ADD_PARAMS);
wdenk458ded32002-09-20 10:59:08 +0000190#endif
Niklaus Gigerf662b232008-11-03 22:15:34 +0100191
192 memcpy ((void *) bootaddr, build_buf,
193 max (strlen (build_buf), 255));
194 flush_cache (bootaddr, max (strlen (build_buf), 255));
wdenk458ded32002-09-20 10:59:08 +0000195 }
196
wdenk57b2d802003-06-27 21:31:46 +0000197 /*
198 * If the data at the load address is an elf image, then
199 * treat it like an elf image. Otherwise, assume that it is a
200 * binary image
wdenk458ded32002-09-20 10:59:08 +0000201 */
202
203 if (valid_elf_image (addr)) {
204 addr = load_elf_image (addr);
205 } else {
wdenk42c05472004-03-23 22:14:11 +0000206 puts ("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000207 /* leave addr as load_addr */
208 }
209
210 printf ("## Using bootline (@ 0x%lx): %s\n", bootaddr,
211 (char *) bootaddr);
212 printf ("## Starting vxWorks at 0x%08lx ...\n", addr);
213
214 ((void (*)(void)) addr) ();
215
wdenk42c05472004-03-23 22:14:11 +0000216 puts ("## vxWorks terminated\n");
wdenk458ded32002-09-20 10:59:08 +0000217 return 1;
218}
219
220/* ======================================================================
221 * Determine if a valid ELF image exists at the given memory location.
222 * First looks at the ELF header magic field, the makes sure that it is
223 * executable and makes sure that it is for a PowerPC.
224 * ====================================================================== */
225int valid_elf_image (unsigned long addr)
226{
227 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
228
229 /* -------------------------------------------------- */
230
231 ehdr = (Elf32_Ehdr *) addr;
232
233 if (!IS_ELF (*ehdr)) {
234 printf ("## No elf image at address 0x%08lx\n", addr);
235 return 0;
236 }
237
238 if (ehdr->e_type != ET_EXEC) {
Niklaus Gigerf662b232008-11-03 22:15:34 +0100239 printf ("## Not a 32-bit elf image at address 0x%08lx\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000240 return 0;
241 }
242
wdenk4fc95692003-02-28 00:49:47 +0000243#if 0
wdenk458ded32002-09-20 10:59:08 +0000244 if (ehdr->e_machine != EM_PPC) {
245 printf ("## Not a PowerPC elf image at address 0x%08lx\n",
246 addr);
247 return 0;
248 }
wdenk4fc95692003-02-28 00:49:47 +0000249#endif
wdenk458ded32002-09-20 10:59:08 +0000250
251 return 1;
252}
253
wdenk458ded32002-09-20 10:59:08 +0000254/* ======================================================================
255 * A very simple elf loader, assumes the image is valid, returns the
256 * entry point address.
257 * ====================================================================== */
258unsigned long load_elf_image (unsigned long addr)
259{
260 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
261 Elf32_Shdr *shdr; /* Section header structure pointer */
262 unsigned char *strtab = 0; /* String table pointer */
263 unsigned char *image; /* Binary image pointer */
264 int i; /* Loop counter */
265
266 /* -------------------------------------------------- */
267
268 ehdr = (Elf32_Ehdr *) addr;
269
270 /* Find the section header string table for output info */
271 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
272 (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
273
274 if (shdr->sh_type == SHT_STRTAB)
275 strtab = (unsigned char *) (addr + shdr->sh_offset);
276
277 /* Load each appropriate section */
278 for (i = 0; i < ehdr->e_shnum; ++i) {
279 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
280 (i * sizeof (Elf32_Shdr)));
281
282 if (!(shdr->sh_flags & SHF_ALLOC)
283 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
284 continue;
285 }
286
287 if (strtab) {
288 printf ("%sing %s @ 0x%08lx (%ld bytes)\n",
289 (shdr->sh_type == SHT_NOBITS) ?
290 "Clear" : "Load",
291 &strtab[shdr->sh_name],
292 (unsigned long) shdr->sh_addr,
293 (long) shdr->sh_size);
294 }
295
296 if (shdr->sh_type == SHT_NOBITS) {
297 memset ((void *)shdr->sh_addr, 0, shdr->sh_size);
298 } else {
299 image = (unsigned char *) addr + shdr->sh_offset;
300 memcpy ((void *) shdr->sh_addr,
301 (const void *) image,
302 shdr->sh_size);
303 }
304 flush_cache (shdr->sh_addr, shdr->sh_size);
305 }
306
307 return ehdr->e_entry;
308}
309
310/* ====================================================================== */
wdenkf287a242003-07-01 21:06:45 +0000311U_BOOT_CMD(
312 bootelf, 2, 0, do_bootelf,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600313 "Boot from an ELF image in memory",
wdenk57b2d802003-06-27 21:31:46 +0000314 " [address] - load address of ELF image.\n"
315);
316
wdenkf287a242003-07-01 21:06:45 +0000317U_BOOT_CMD(
318 bootvx, 2, 0, do_bootvx,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600319 "Boot vxWorks from an ELF image",
wdenk57b2d802003-06-27 21:31:46 +0000320 " [address] - load address of vxWorks ELF image.\n"
321);