blob: 4683554040caaab119b5d4d6bda7799b279e3e66 [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>
21
Wolfgang Denk6405a152006-03-31 18:32:53 +020022#if defined(CONFIG_WALNUT) || defined(CFG_VXWORKS_MAC_PTR)
23DECLARE_GLOBAL_DATA_PTR;
24#endif
wdenk458ded32002-09-20 10:59:08 +000025
wdenk458ded32002-09-20 10:59:08 +000026#ifndef MAX
27#define MAX(a,b) ((a) > (b) ? (a) : (b))
28#endif
29
Mike Frysingerf79507e2008-01-29 18:21:05 -050030static inline void bootelf_setup(int argc, char *argv[])
31{
32 /*
33 * QNX images require the data cache is disabled.
34 * Data cache is already flushed, so just turn it off.
35 */
36 if (dcache_status ())
37 dcache_disable ();
38
39#ifdef CONFIG_BLACKFIN
40 if (icache_status ())
41 icache_disable ();
42#endif
43}
44
wdenk57b2d802003-06-27 21:31:46 +000045int valid_elf_image (unsigned long addr);
46unsigned long load_elf_image (unsigned long addr);
wdenk458ded32002-09-20 10:59:08 +000047
48/* ======================================================================
49 * Interpreter command to boot an arbitrary ELF image from memory.
50 * ====================================================================== */
51int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
52{
53 unsigned long addr; /* Address of the ELF image */
54 unsigned long rc; /* Return value from user code */
55
56 /* -------------------------------------------------- */
57 int rcode = 0;
58
59 if (argc < 2)
60 addr = load_addr;
61 else
62 addr = simple_strtoul (argv[1], NULL, 16);
63
64 if (!valid_elf_image (addr))
65 return 1;
66
67 addr = load_elf_image (addr);
68
69 printf ("## Starting application at 0x%08lx ...\n", addr);
70
Mike Frysingerf79507e2008-01-29 18:21:05 -050071 bootelf_setup(argc, argv);
wdenk458ded32002-09-20 10:59:08 +000072
73 /*
74 * pass address parameter as argv[0] (aka command name),
75 * and all remaining args
76 */
77 rc = ((ulong (*)(int, char *[])) addr) (--argc, &argv[1]);
78 if (rc != 0)
79 rcode = 1;
80
81 printf ("## Application terminated, rc = 0x%lx\n", rc);
82 return rcode;
83}
84
85/* ======================================================================
86 * Interpreter command to boot VxWorks from a memory image. The image can
87 * be either an ELF image or a raw binary. Will attempt to setup the
88 * bootline and other parameters correctly.
89 * ====================================================================== */
Stefan Roese00487782006-11-29 12:53:15 +010090int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenk458ded32002-09-20 10:59:08 +000091{
wdenk458ded32002-09-20 10:59:08 +000092 unsigned long addr; /* Address of image */
93 unsigned long bootaddr; /* Address to put the bootline */
94 char *bootline; /* Text of the bootline */
95 char *tmp; /* Temporary char pointer */
96
97#if defined(CONFIG_4xx) || defined(CONFIG_IOP480)
98 char build_buf[80]; /* Buffer for building the bootline */
99#endif
100 /* -------------------------------------------------- */
101
102 /*
103 * Check the loadaddr variable.
104 * If we don't know where the image is then we're done.
105 */
106
Stefan Roese00487782006-11-29 12:53:15 +0100107 if (argc < 2)
108 addr = load_addr;
109 else
110 addr = simple_strtoul (argv[1], NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000111
Jon Loeliger54324d02007-07-08 17:51:39 -0500112#if defined(CONFIG_CMD_NET)
wdenk458ded32002-09-20 10:59:08 +0000113 /* Check to see if we need to tftp the image ourselves before starting */
114
115 if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) {
wdenkc217f6d2002-11-11 02:11:37 +0000116 if (NetLoop (TFTP) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000117 return 1;
118 printf ("Automatic boot of VxWorks image at address 0x%08lx ... \n", addr);
119 }
120#endif
121
122 /* This should equate
123 * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
124 * from the VxWorks BSP header files.
125 * This will vary from board to board
126 */
127
Stefan Roese326c9712005-08-01 16:41:48 +0200128#if defined(CONFIG_WALNUT)
wdenk458ded32002-09-20 10:59:08 +0000129 tmp = (char *) CFG_NVRAM_BASE_ADDR + 0x500;
130 memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[3], 3);
stroeseedd35852004-12-16 17:37:54 +0000131#elif defined(CFG_VXWORKS_MAC_PTR)
132 tmp = (char *) CFG_VXWORKS_MAC_PTR;
wdenk458ded32002-09-20 10:59:08 +0000133 memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[0], 6);
134#else
wdenk42c05472004-03-23 22:14:11 +0000135 puts ("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000136#endif
137
wdenk57b2d802003-06-27 21:31:46 +0000138 /*
139 * Use bootaddr to find the location in memory that VxWorks
140 * will look for the bootline string. The default value for
141 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
142 * defaults to 0x4200
wdenk458ded32002-09-20 10:59:08 +0000143 */
144
145 if ((tmp = getenv ("bootaddr")) == NULL)
146 bootaddr = 0x4200;
147 else
148 bootaddr = simple_strtoul (tmp, NULL, 16);
149
wdenk57b2d802003-06-27 21:31:46 +0000150 /*
151 * Check to see if the bootline is defined in the 'bootargs'
152 * parameter. If it is not defined, we may be able to
153 * construct the info
wdenk458ded32002-09-20 10:59:08 +0000154 */
155
156 if ((bootline = getenv ("bootargs")) != NULL) {
157 memcpy ((void *) bootaddr, bootline, MAX(strlen(bootline), 255));
158 flush_cache (bootaddr, MAX(strlen(bootline), 255));
159 } else {
160#if defined(CONFIG_4xx)
161 sprintf (build_buf, "ibmEmac(0,0)");
162
163 if ((tmp = getenv ("hostname")) != NULL) {
164 sprintf (&build_buf[strlen (build_buf - 1)],
165 "host:%s ", tmp);
166 } else {
167 sprintf (&build_buf[strlen (build_buf - 1)],
168 ": ");
169 }
170
171 if ((tmp = getenv ("ipaddr")) != NULL) {
172 sprintf (&build_buf[strlen (build_buf - 1)],
173 "e=%s ", tmp);
174 }
175 memcpy ((void *)bootaddr, build_buf, MAX(strlen(build_buf), 255));
176 flush_cache (bootaddr, MAX(strlen(build_buf), 255));
177#elif defined(CONFIG_IOP480)
178 sprintf (build_buf, "dc(0,0)");
179
180 if ((tmp = getenv ("hostname")) != NULL) {
181 sprintf (&build_buf[strlen (build_buf - 1)],
182 "host:%s ", tmp);
183 } else {
184 sprintf (&build_buf[strlen (build_buf - 1)],
185 ": ");
186 }
187
188 if ((tmp = getenv ("ipaddr")) != NULL) {
189 sprintf (&build_buf[strlen (build_buf - 1)],
190 "e=%s ", tmp);
191 }
192 memcpy ((void *) bootaddr, build_buf, MAX(strlen(build_buf), 255));
193 flush_cache (bootaddr, MAX(strlen(build_buf), 255));
194#else
195
wdenk57b2d802003-06-27 21:31:46 +0000196 /*
197 * I'm not sure what the device should be for other
198 * PPC flavors, the hostname and ipaddr should be ok
199 * to just copy
wdenk458ded32002-09-20 10:59:08 +0000200 */
201
wdenk42c05472004-03-23 22:14:11 +0000202 puts ("No bootargs defined\n");
wdenk458ded32002-09-20 10:59:08 +0000203 return 1;
204#endif
205 }
206
wdenk57b2d802003-06-27 21:31:46 +0000207 /*
208 * If the data at the load address is an elf image, then
209 * treat it like an elf image. Otherwise, assume that it is a
210 * binary image
wdenk458ded32002-09-20 10:59:08 +0000211 */
212
213 if (valid_elf_image (addr)) {
214 addr = load_elf_image (addr);
215 } else {
wdenk42c05472004-03-23 22:14:11 +0000216 puts ("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000217 /* leave addr as load_addr */
218 }
219
220 printf ("## Using bootline (@ 0x%lx): %s\n", bootaddr,
221 (char *) bootaddr);
222 printf ("## Starting vxWorks at 0x%08lx ...\n", addr);
223
224 ((void (*)(void)) addr) ();
225
wdenk42c05472004-03-23 22:14:11 +0000226 puts ("## vxWorks terminated\n");
wdenk458ded32002-09-20 10:59:08 +0000227 return 1;
228}
229
230/* ======================================================================
231 * Determine if a valid ELF image exists at the given memory location.
232 * First looks at the ELF header magic field, the makes sure that it is
233 * executable and makes sure that it is for a PowerPC.
234 * ====================================================================== */
235int valid_elf_image (unsigned long addr)
236{
237 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
238
239 /* -------------------------------------------------- */
240
241 ehdr = (Elf32_Ehdr *) addr;
242
243 if (!IS_ELF (*ehdr)) {
244 printf ("## No elf image at address 0x%08lx\n", addr);
245 return 0;
246 }
247
248 if (ehdr->e_type != ET_EXEC) {
249 printf ("## Not a 32-bit elf image at address 0x%08lx\n",
250 addr);
251 return 0;
252 }
253
wdenk4fc95692003-02-28 00:49:47 +0000254#if 0
wdenk458ded32002-09-20 10:59:08 +0000255 if (ehdr->e_machine != EM_PPC) {
256 printf ("## Not a PowerPC elf image at address 0x%08lx\n",
257 addr);
258 return 0;
259 }
wdenk4fc95692003-02-28 00:49:47 +0000260#endif
wdenk458ded32002-09-20 10:59:08 +0000261
262 return 1;
263}
264
265
266/* ======================================================================
267 * A very simple elf loader, assumes the image is valid, returns the
268 * entry point address.
269 * ====================================================================== */
270unsigned long load_elf_image (unsigned long addr)
271{
272 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
273 Elf32_Shdr *shdr; /* Section header structure pointer */
274 unsigned char *strtab = 0; /* String table pointer */
275 unsigned char *image; /* Binary image pointer */
276 int i; /* Loop counter */
277
278 /* -------------------------------------------------- */
279
280 ehdr = (Elf32_Ehdr *) addr;
281
282 /* Find the section header string table for output info */
283 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
284 (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
285
286 if (shdr->sh_type == SHT_STRTAB)
287 strtab = (unsigned char *) (addr + shdr->sh_offset);
288
289 /* Load each appropriate section */
290 for (i = 0; i < ehdr->e_shnum; ++i) {
291 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
292 (i * sizeof (Elf32_Shdr)));
293
294 if (!(shdr->sh_flags & SHF_ALLOC)
295 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
296 continue;
297 }
298
299 if (strtab) {
300 printf ("%sing %s @ 0x%08lx (%ld bytes)\n",
301 (shdr->sh_type == SHT_NOBITS) ?
302 "Clear" : "Load",
303 &strtab[shdr->sh_name],
304 (unsigned long) shdr->sh_addr,
305 (long) shdr->sh_size);
306 }
307
308 if (shdr->sh_type == SHT_NOBITS) {
309 memset ((void *)shdr->sh_addr, 0, shdr->sh_size);
310 } else {
311 image = (unsigned char *) addr + shdr->sh_offset;
312 memcpy ((void *) shdr->sh_addr,
313 (const void *) image,
314 shdr->sh_size);
315 }
316 flush_cache (shdr->sh_addr, shdr->sh_size);
317 }
318
319 return ehdr->e_entry;
320}
321
322/* ====================================================================== */
wdenkf287a242003-07-01 21:06:45 +0000323U_BOOT_CMD(
324 bootelf, 2, 0, do_bootelf,
wdenk57b2d802003-06-27 21:31:46 +0000325 "bootelf - Boot from an ELF image in memory\n",
326 " [address] - load address of ELF image.\n"
327);
328
wdenkf287a242003-07-01 21:06:45 +0000329U_BOOT_CMD(
330 bootvx, 2, 0, do_bootvx,
wdenk57b2d802003-06-27 21:31:46 +0000331 "bootvx - Boot vxWorks from an ELF image\n",
332 " [address] - load address of vxWorks ELF image.\n"
333);