blob: 149ffa3daaeecd296a047d2ebb539b582a3a4349 [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>
20
21#include <cmd_elf.h>
22#include <elf.h>
23
24
25#if (CONFIG_COMMANDS & CFG_CMD_ELF)
26
27#ifndef MAX
28#define MAX(a,b) ((a) > (b) ? (a) : (b))
29#endif
30
31
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 {
106 printf ("No load address provided\n");
107 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)) {
wdenkeb9401e2002-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
136 printf ("## Ethernet MAC address not copied to NV RAM\n");
137#endif
138
139 /*
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
144 */
145
146 if ((tmp = getenv ("bootaddr")) == NULL)
147 bootaddr = 0x4200;
148 else
149 bootaddr = simple_strtoul (tmp, NULL, 16);
150
151 /*
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
155 */
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
197 /*
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
201 */
202
203 printf ("No bootargs defined\n");
204 return 1;
205#endif
206 }
207
208 /*
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
212 */
213
214 if (valid_elf_image (addr)) {
215 addr = load_elf_image (addr);
216 } else {
217 printf ("## Not an ELF image, assuming binary\n");
218 /* 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
227 printf ("## vxWorks terminated\n");
228 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
255 if (ehdr->e_machine != EM_PPC) {
256 printf ("## Not a PowerPC elf image at address 0x%08lx\n",
257 addr);
258 return 0;
259 }
260
261 return 1;
262}
263
264
265/* ======================================================================
266 * A very simple elf loader, assumes the image is valid, returns the
267 * entry point address.
268 * ====================================================================== */
269unsigned long load_elf_image (unsigned long addr)
270{
271 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
272 Elf32_Shdr *shdr; /* Section header structure pointer */
273 unsigned char *strtab = 0; /* String table pointer */
274 unsigned char *image; /* Binary image pointer */
275 int i; /* Loop counter */
276
277 /* -------------------------------------------------- */
278
279 ehdr = (Elf32_Ehdr *) addr;
280
281 /* Find the section header string table for output info */
282 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
283 (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
284
285 if (shdr->sh_type == SHT_STRTAB)
286 strtab = (unsigned char *) (addr + shdr->sh_offset);
287
288 /* Load each appropriate section */
289 for (i = 0; i < ehdr->e_shnum; ++i) {
290 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
291 (i * sizeof (Elf32_Shdr)));
292
293 if (!(shdr->sh_flags & SHF_ALLOC)
294 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
295 continue;
296 }
297
298 if (strtab) {
299 printf ("%sing %s @ 0x%08lx (%ld bytes)\n",
300 (shdr->sh_type == SHT_NOBITS) ?
301 "Clear" : "Load",
302 &strtab[shdr->sh_name],
303 (unsigned long) shdr->sh_addr,
304 (long) shdr->sh_size);
305 }
306
307 if (shdr->sh_type == SHT_NOBITS) {
308 memset ((void *)shdr->sh_addr, 0, shdr->sh_size);
309 } else {
310 image = (unsigned char *) addr + shdr->sh_offset;
311 memcpy ((void *) shdr->sh_addr,
312 (const void *) image,
313 shdr->sh_size);
314 }
315 flush_cache (shdr->sh_addr, shdr->sh_size);
316 }
317
318 return ehdr->e_entry;
319}
320
321/* ====================================================================== */
322#endif /* CFG_CMD_ELF */