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