blob: eb0cc71fb360fd139cc72bcc46800d546a92bdd5 [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
22
23#if (CONFIG_COMMANDS & CFG_CMD_ELF)
24
25#ifndef MAX
26#define MAX(a,b) ((a) > (b) ? (a) : (b))
27#endif
28
wdenk57b2d802003-06-27 21:31:46 +000029int valid_elf_image (unsigned long addr);
30unsigned long load_elf_image (unsigned long addr);
wdenk458ded32002-09-20 10:59:08 +000031
32/* ======================================================================
33 * Interpreter command to boot an arbitrary ELF image from memory.
34 * ====================================================================== */
35int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
36{
37 unsigned long addr; /* Address of the ELF image */
38 unsigned long rc; /* Return value from user code */
39
40 /* -------------------------------------------------- */
41 int rcode = 0;
42
43 if (argc < 2)
44 addr = load_addr;
45 else
46 addr = simple_strtoul (argv[1], NULL, 16);
47
48 if (!valid_elf_image (addr))
49 return 1;
50
51 addr = load_elf_image (addr);
52
53 printf ("## Starting application at 0x%08lx ...\n", addr);
54
55 /*
56 * QNX images require the data cache is disabled.
57 * Data cache is already flushed, so just turn it off.
58 */
59 if (dcache_status ())
60 dcache_disable ();
61
62 /*
63 * pass address parameter as argv[0] (aka command name),
64 * and all remaining args
65 */
66 rc = ((ulong (*)(int, char *[])) addr) (--argc, &argv[1]);
67 if (rc != 0)
68 rcode = 1;
69
70 printf ("## Application terminated, rc = 0x%lx\n", rc);
71 return rcode;
72}
73
74/* ======================================================================
75 * Interpreter command to boot VxWorks from a memory image. The image can
76 * be either an ELF image or a raw binary. Will attempt to setup the
77 * bootline and other parameters correctly.
78 * ====================================================================== */
79int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
80{
81#if defined(CONFIG_WALNUT405) || \
82 defined(CONFIG_CPCI405) || \
83 defined(CONFIG_OCRTC) || \
84 defined(CONFIG_ORSG)
85 DECLARE_GLOBAL_DATA_PTR;
86#endif
87
88 unsigned long addr; /* Address of image */
89 unsigned long bootaddr; /* Address to put the bootline */
90 char *bootline; /* Text of the bootline */
91 char *tmp; /* Temporary char pointer */
92
93#if defined(CONFIG_4xx) || defined(CONFIG_IOP480)
94 char build_buf[80]; /* Buffer for building the bootline */
95#endif
96 /* -------------------------------------------------- */
97
98 /*
99 * Check the loadaddr variable.
100 * If we don't know where the image is then we're done.
101 */
102
103 if ((tmp = getenv ("loadaddr")) != NULL) {
104 addr = simple_strtoul (tmp, NULL, 16);
105 } else {
wdenk42c05472004-03-23 22:14:11 +0000106 puts ("No load address provided\n");
wdenk458ded32002-09-20 10:59:08 +0000107 return 1;
108 }
109
110#if (CONFIG_COMMANDS & CFG_CMD_NET)
111 /* Check to see if we need to tftp the image ourselves before starting */
112
113 if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) {
wdenkc217f6d2002-11-11 02:11:37 +0000114 if (NetLoop (TFTP) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000115 return 1;
116 printf ("Automatic boot of VxWorks image at address 0x%08lx ... \n", addr);
117 }
118#endif
119
120 /* This should equate
121 * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
122 * from the VxWorks BSP header files.
123 * This will vary from board to board
124 */
125
126#if defined(CONFIG_WALNUT405)
127 tmp = (char *) CFG_NVRAM_BASE_ADDR + 0x500;
128 memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[3], 3);
129#elif defined(CONFIG_CPCI405)
130 tmp = (char *) CFG_NVRAM_BASE_ADDR + CFG_NVRAM_VXWORKS_OFFS;
131 memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[0], 6);
132#elif defined(CONFIG_OCRTC) || defined(CONFIG_ORSG)
133 tmp = (char *) CFG_ETHERNET_MAC_ADDR;
134 memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[0], 6);
135#else
wdenk42c05472004-03-23 22:14:11 +0000136 puts ("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000137#endif
138
wdenk57b2d802003-06-27 21:31:46 +0000139 /*
140 * Use bootaddr to find the location in memory that VxWorks
141 * will look for the bootline string. The default value for
142 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
143 * defaults to 0x4200
wdenk458ded32002-09-20 10:59:08 +0000144 */
145
146 if ((tmp = getenv ("bootaddr")) == NULL)
147 bootaddr = 0x4200;
148 else
149 bootaddr = simple_strtoul (tmp, NULL, 16);
150
wdenk57b2d802003-06-27 21:31:46 +0000151 /*
152 * Check to see if the bootline is defined in the 'bootargs'
153 * parameter. If it is not defined, we may be able to
154 * construct the info
wdenk458ded32002-09-20 10:59:08 +0000155 */
156
157 if ((bootline = getenv ("bootargs")) != NULL) {
158 memcpy ((void *) bootaddr, bootline, MAX(strlen(bootline), 255));
159 flush_cache (bootaddr, MAX(strlen(bootline), 255));
160 } else {
161#if defined(CONFIG_4xx)
162 sprintf (build_buf, "ibmEmac(0,0)");
163
164 if ((tmp = getenv ("hostname")) != NULL) {
165 sprintf (&build_buf[strlen (build_buf - 1)],
166 "host:%s ", tmp);
167 } else {
168 sprintf (&build_buf[strlen (build_buf - 1)],
169 ": ");
170 }
171
172 if ((tmp = getenv ("ipaddr")) != NULL) {
173 sprintf (&build_buf[strlen (build_buf - 1)],
174 "e=%s ", tmp);
175 }
176 memcpy ((void *)bootaddr, build_buf, MAX(strlen(build_buf), 255));
177 flush_cache (bootaddr, MAX(strlen(build_buf), 255));
178#elif defined(CONFIG_IOP480)
179 sprintf (build_buf, "dc(0,0)");
180
181 if ((tmp = getenv ("hostname")) != NULL) {
182 sprintf (&build_buf[strlen (build_buf - 1)],
183 "host:%s ", tmp);
184 } else {
185 sprintf (&build_buf[strlen (build_buf - 1)],
186 ": ");
187 }
188
189 if ((tmp = getenv ("ipaddr")) != NULL) {
190 sprintf (&build_buf[strlen (build_buf - 1)],
191 "e=%s ", tmp);
192 }
193 memcpy ((void *) bootaddr, build_buf, MAX(strlen(build_buf), 255));
194 flush_cache (bootaddr, MAX(strlen(build_buf), 255));
195#else
196
wdenk57b2d802003-06-27 21:31:46 +0000197 /*
198 * I'm not sure what the device should be for other
199 * PPC flavors, the hostname and ipaddr should be ok
200 * to just copy
wdenk458ded32002-09-20 10:59:08 +0000201 */
202
wdenk42c05472004-03-23 22:14:11 +0000203 puts ("No bootargs defined\n");
wdenk458ded32002-09-20 10:59:08 +0000204 return 1;
205#endif
206 }
207
wdenk57b2d802003-06-27 21:31:46 +0000208 /*
209 * If the data at the load address is an elf image, then
210 * treat it like an elf image. Otherwise, assume that it is a
211 * binary image
wdenk458ded32002-09-20 10:59:08 +0000212 */
213
214 if (valid_elf_image (addr)) {
215 addr = load_elf_image (addr);
216 } else {
wdenk42c05472004-03-23 22:14:11 +0000217 puts ("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000218 /* leave addr as load_addr */
219 }
220
221 printf ("## Using bootline (@ 0x%lx): %s\n", bootaddr,
222 (char *) bootaddr);
223 printf ("## Starting vxWorks at 0x%08lx ...\n", addr);
224
225 ((void (*)(void)) addr) ();
226
wdenk42c05472004-03-23 22:14:11 +0000227 puts ("## vxWorks terminated\n");
wdenk458ded32002-09-20 10:59:08 +0000228 return 1;
229}
230
231/* ======================================================================
232 * Determine if a valid ELF image exists at the given memory location.
233 * First looks at the ELF header magic field, the makes sure that it is
234 * executable and makes sure that it is for a PowerPC.
235 * ====================================================================== */
236int valid_elf_image (unsigned long addr)
237{
238 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
239
240 /* -------------------------------------------------- */
241
242 ehdr = (Elf32_Ehdr *) addr;
243
244 if (!IS_ELF (*ehdr)) {
245 printf ("## No elf image at address 0x%08lx\n", addr);
246 return 0;
247 }
248
249 if (ehdr->e_type != ET_EXEC) {
250 printf ("## Not a 32-bit elf image at address 0x%08lx\n",
251 addr);
252 return 0;
253 }
254
wdenk4fc95692003-02-28 00:49:47 +0000255#if 0
wdenk458ded32002-09-20 10:59:08 +0000256 if (ehdr->e_machine != EM_PPC) {
257 printf ("## Not a PowerPC elf image at address 0x%08lx\n",
258 addr);
259 return 0;
260 }
wdenk4fc95692003-02-28 00:49:47 +0000261#endif
wdenk458ded32002-09-20 10:59:08 +0000262
263 return 1;
264}
265
266
267/* ======================================================================
268 * A very simple elf loader, assumes the image is valid, returns the
269 * entry point address.
270 * ====================================================================== */
271unsigned long load_elf_image (unsigned long addr)
272{
273 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
274 Elf32_Shdr *shdr; /* Section header structure pointer */
275 unsigned char *strtab = 0; /* String table pointer */
276 unsigned char *image; /* Binary image pointer */
277 int i; /* Loop counter */
278
279 /* -------------------------------------------------- */
280
281 ehdr = (Elf32_Ehdr *) addr;
282
283 /* Find the section header string table for output info */
284 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
285 (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
286
287 if (shdr->sh_type == SHT_STRTAB)
288 strtab = (unsigned char *) (addr + shdr->sh_offset);
289
290 /* Load each appropriate section */
291 for (i = 0; i < ehdr->e_shnum; ++i) {
292 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
293 (i * sizeof (Elf32_Shdr)));
294
295 if (!(shdr->sh_flags & SHF_ALLOC)
296 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
297 continue;
298 }
299
300 if (strtab) {
301 printf ("%sing %s @ 0x%08lx (%ld bytes)\n",
302 (shdr->sh_type == SHT_NOBITS) ?
303 "Clear" : "Load",
304 &strtab[shdr->sh_name],
305 (unsigned long) shdr->sh_addr,
306 (long) shdr->sh_size);
307 }
308
309 if (shdr->sh_type == SHT_NOBITS) {
310 memset ((void *)shdr->sh_addr, 0, shdr->sh_size);
311 } else {
312 image = (unsigned char *) addr + shdr->sh_offset;
313 memcpy ((void *) shdr->sh_addr,
314 (const void *) image,
315 shdr->sh_size);
316 }
317 flush_cache (shdr->sh_addr, shdr->sh_size);
318 }
319
320 return ehdr->e_entry;
321}
322
323/* ====================================================================== */
wdenkf287a242003-07-01 21:06:45 +0000324U_BOOT_CMD(
325 bootelf, 2, 0, do_bootelf,
wdenk57b2d802003-06-27 21:31:46 +0000326 "bootelf - Boot from an ELF image in memory\n",
327 " [address] - load address of ELF image.\n"
328);
329
wdenkf287a242003-07-01 21:06:45 +0000330U_BOOT_CMD(
331 bootvx, 2, 0, do_bootvx,
wdenk57b2d802003-06-27 21:31:46 +0000332 "bootvx - Boot vxWorks from an ELF image\n",
333 " [address] - load address of vxWorks ELF image.\n"
334);
335
wdenk458ded32002-09-20 10:59:08 +0000336#endif /* CFG_CMD_ELF */