]> git.ipfire.org Git - people/ms/u-boot.git/blame - cmd/elf.c
Merge git://git.denx.de/u-boot-mmc
[people/ms/u-boot.git] / 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>
458ded34 18#include <elf.h>
ebca3df7 19#include <net.h>
29a4c24d 20#include <vxworks.h>
b90ff0fd
BM
21#ifdef CONFIG_X86
22#include <asm/e820.h>
9aa1280a 23#include <linux/linkage.h>
b90ff0fd 24#endif
458ded34 25
9dffa52d
BM
26/*
27 * A very simple elf loader, assumes the image is valid, returns the
28 * entry point address.
29 */
30static unsigned long load_elf_image_phdr(unsigned long addr)
31{
32 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
33 Elf32_Phdr *phdr; /* Program header structure pointer */
34 int i;
35
36 ehdr = (Elf32_Ehdr *)addr;
37 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
38
39 /* Load each program header */
40 for (i = 0; i < ehdr->e_phnum; ++i) {
41 void *dst = (void *)(uintptr_t)phdr->p_paddr;
42 void *src = (void *)addr + phdr->p_offset;
43 debug("Loading phdr %i to 0x%p (%i bytes)\n",
44 i, dst, phdr->p_filesz);
45 if (phdr->p_filesz)
46 memcpy(dst, src, phdr->p_filesz);
47 if (phdr->p_filesz != phdr->p_memsz)
48 memset(dst + phdr->p_filesz, 0x00,
49 phdr->p_memsz - phdr->p_filesz);
50 flush_cache((unsigned long)dst, phdr->p_filesz);
51 ++phdr;
52 }
53
54 return ehdr->e_entry;
55}
56
57static unsigned long load_elf_image_shdr(unsigned long addr)
58{
59 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
60 Elf32_Shdr *shdr; /* Section header structure pointer */
61 unsigned char *strtab = 0; /* String table pointer */
62 unsigned char *image; /* Binary image pointer */
63 int i; /* Loop counter */
64
65 ehdr = (Elf32_Ehdr *)addr;
66
67 /* Find the section header string table for output info */
68 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
69 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
70
71 if (shdr->sh_type == SHT_STRTAB)
72 strtab = (unsigned char *)(addr + shdr->sh_offset);
73
74 /* Load each appropriate section */
75 for (i = 0; i < ehdr->e_shnum; ++i) {
76 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
77 (i * sizeof(Elf32_Shdr)));
78
79 if (!(shdr->sh_flags & SHF_ALLOC) ||
80 shdr->sh_addr == 0 || shdr->sh_size == 0) {
81 continue;
82 }
83
84 if (strtab) {
85 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
86 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
87 &strtab[shdr->sh_name],
88 (unsigned long)shdr->sh_addr,
89 (long)shdr->sh_size);
90 }
91
92 if (shdr->sh_type == SHT_NOBITS) {
93 memset((void *)(uintptr_t)shdr->sh_addr, 0,
94 shdr->sh_size);
95 } else {
96 image = (unsigned char *)addr + shdr->sh_offset;
97 memcpy((void *)(uintptr_t)shdr->sh_addr,
98 (const void *)image, shdr->sh_size);
99 }
100 flush_cache(shdr->sh_addr, shdr->sh_size);
101 }
102
103 return ehdr->e_entry;
104}
017e9b79
MF
105
106/* Allow ports to override the default behavior */
553d8c3a 107static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
ebca3df7 108 int argc, char * const argv[])
1f1d88dd 109{
017e9b79
MF
110 unsigned long ret;
111
017e9b79
MF
112 /*
113 * pass address parameter as argv[0] (aka command name),
114 * and all remaining args
115 */
62e03d33 116 ret = entry(argc, argv);
1f1d88dd 117
017e9b79
MF
118 return ret;
119}
458ded34 120
ebca3df7 121/*
62e03d33 122 * Determine if a valid ELF image exists at the given memory location.
ebca3df7
BM
123 * First look at the ELF header magic field, then make sure that it is
124 * executable.
125 */
62e03d33
DS
126int valid_elf_image(unsigned long addr)
127{
ebca3df7 128 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
62e03d33 129
ebca3df7 130 ehdr = (Elf32_Ehdr *)addr;
62e03d33
DS
131
132 if (!IS_ELF(*ehdr)) {
133 printf("## No elf image at address 0x%08lx\n", addr);
134 return 0;
135 }
136
137 if (ehdr->e_type != ET_EXEC) {
138 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
139 return 0;
140 }
141
62e03d33
DS
142 return 1;
143}
144
ebca3df7 145/* Interpreter command to boot an arbitrary ELF image from memory */
62e03d33 146int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
458ded34 147{
ebca3df7
BM
148 unsigned long addr; /* Address of the ELF image */
149 unsigned long rc; /* Return value from user code */
be1b8679 150 char *sload = NULL;
00caae6d 151 const char *ep = env_get("autostart");
458ded34
WD
152 int rcode = 0;
153
be1b8679
TR
154 /* Consume 'bootelf' */
155 argc--; argv++;
f44a928e 156
be1b8679
TR
157 /* Check for flag. */
158 if (argc >= 1 && (argv[0][0] == '-' && \
159 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
160 sload = argv[0];
161 /* Consume flag. */
162 argc--; argv++;
163 }
164 /* Check for address. */
165 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
166 /* Consume address */
167 argc--; argv++;
168 } else
f44a928e 169 addr = load_addr;
458ded34 170
62e03d33 171 if (!valid_elf_image(addr))
458ded34
WD
172 return 1;
173
f44a928e
MF
174 if (sload && sload[1] == 'p')
175 addr = load_elf_image_phdr(addr);
176 else
177 addr = load_elf_image_shdr(addr);
458ded34 178
44c8fd3a
SDPP
179 if (ep && !strcmp(ep, "no"))
180 return rcode;
181
62e03d33 182 printf("## Starting application at 0x%08lx ...\n", addr);
458ded34 183
458ded34
WD
184 /*
185 * pass address parameter as argv[0] (aka command name),
186 * and all remaining args
187 */
be1b8679 188 rc = do_bootelf_exec((void *)addr, argc, argv);
458ded34
WD
189 if (rc != 0)
190 rcode = 1;
191
62e03d33 192 printf("## Application terminated, rc = 0x%lx\n", rc);
ebca3df7 193
458ded34
WD
194 return rcode;
195}
196
ebca3df7 197/*
458ded34
WD
198 * Interpreter command to boot VxWorks from a memory image. The image can
199 * be either an ELF image or a raw binary. Will attempt to setup the
200 * bootline and other parameters correctly.
ebca3df7 201 */
62e03d33 202int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
458ded34 203{
ebca3df7
BM
204 unsigned long addr; /* Address of image */
205 unsigned long bootaddr; /* Address to put the bootline */
206 char *bootline; /* Text of the bootline */
207 char *tmp; /* Temporary char pointer */
208 char build_buf[128]; /* Buffer for building the bootline */
7f0c3c51 209 int ptr = 0;
b90ff0fd
BM
210#ifdef CONFIG_X86
211 struct e820info *info;
212 struct e820entry *data;
213#endif
ebca3df7
BM
214
215 /*
458ded34
WD
216 * Check the loadaddr variable.
217 * If we don't know where the image is then we're done.
218 */
07a1a0c9 219 if (argc < 2)
1bdd4683
SR
220 addr = load_addr;
221 else
07a1a0c9 222 addr = simple_strtoul(argv[1], NULL, 16);
458ded34 223
baa26db4 224#if defined(CONFIG_CMD_NET)
62e03d33 225 /*
ebca3df7
BM
226 * Check to see if we need to tftp the image ourselves
227 * before starting
62e03d33 228 */
07a1a0c9 229 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
bc0571fc 230 if (net_loop(TFTPGET) <= 0)
458ded34 231 return 1;
62e03d33
DS
232 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
233 addr);
458ded34
WD
234 }
235#endif
236
ebca3df7
BM
237 /*
238 * This should equate to
239 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
458ded34
WD
240 * from the VxWorks BSP header files.
241 * This will vary from board to board
242 */
8996975f 243#if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
ebca3df7 244 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
35affd7a 245 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
62c93d92 246 memcpy(tmp, build_buf, 6);
458ded34 247#else
62e03d33 248 puts("## Ethernet MAC address not copied to NV RAM\n");
458ded34
WD
249#endif
250
8bde7f77
WD
251 /*
252 * Use bootaddr to find the location in memory that VxWorks
9e98b7e3
BM
253 * will look for the bootline string. The default value is
254 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
255 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
458ded34 256 */
00caae6d 257 tmp = env_get("bootaddr");
9e98b7e3
BM
258 if (!tmp) {
259 printf("## VxWorks bootline address not specified\n");
458ded34 260 } else {
9e98b7e3 261 bootaddr = simple_strtoul(tmp, NULL, 16);
458ded34 262
9e98b7e3
BM
263 /*
264 * Check to see if the bootline is defined in the 'bootargs'
265 * parameter. If it is not defined, we may be able to
266 * construct the info.
267 */
00caae6d 268 bootline = env_get("bootargs");
9e98b7e3
BM
269 if (bootline) {
270 memcpy((void *)bootaddr, bootline,
271 max(strlen(bootline), (size_t)255));
272 flush_cache(bootaddr, max(strlen(bootline),
273 (size_t)255));
274 } else {
00caae6d 275 tmp = env_get("bootdev");
192bc694
BW
276 if (tmp) {
277 strcpy(build_buf, tmp);
278 ptr = strlen(tmp);
279 } else
9e98b7e3
BM
280 printf("## VxWorks boot device not specified\n");
281
00caae6d 282 tmp = env_get("bootfile");
9e98b7e3
BM
283 if (tmp)
284 ptr += sprintf(build_buf + ptr,
285 "host:%s ", tmp);
286 else
287 ptr += sprintf(build_buf + ptr,
288 "host:vxWorks ");
289
290 /*
291 * The following parameters are only needed if 'bootdev'
292 * is an ethernet device, otherwise they are optional.
293 */
00caae6d 294 tmp = env_get("ipaddr");
a4092dbd 295 if (tmp) {
9e98b7e3 296 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
00caae6d 297 tmp = env_get("netmask");
9e98b7e3 298 if (tmp) {
723806cc 299 u32 mask = env_get_ip("netmask").s_addr;
9e98b7e3
BM
300 ptr += sprintf(build_buf + ptr,
301 ":%08x ", ntohl(mask));
302 } else {
303 ptr += sprintf(build_buf + ptr, " ");
304 }
a4092dbd 305 }
458ded34 306
00caae6d 307 tmp = env_get("serverip");
9e98b7e3
BM
308 if (tmp)
309 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
62e03d33 310
00caae6d 311 tmp = env_get("gatewayip");
9e98b7e3
BM
312 if (tmp)
313 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
a4092dbd 314
00caae6d 315 tmp = env_get("hostname");
9e98b7e3
BM
316 if (tmp)
317 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
458ded34 318
00caae6d 319 tmp = env_get("othbootargs");
192bc694
BW
320 if (tmp) {
321 strcpy(build_buf + ptr, tmp);
322 ptr += strlen(tmp);
323 }
9e98b7e3
BM
324
325 memcpy((void *)bootaddr, build_buf,
326 max(strlen(build_buf), (size_t)255));
327 flush_cache(bootaddr, max(strlen(build_buf),
328 (size_t)255));
329 }
29a4c24d 330
9e98b7e3
BM
331 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
332 (char *)bootaddr);
458ded34
WD
333 }
334
b90ff0fd
BM
335#ifdef CONFIG_X86
336 /*
337 * Since E820 information is critical to the kernel, if we don't
338 * specify these in the environments, use a default one.
339 */
00caae6d 340 tmp = env_get("e820data");
b90ff0fd
BM
341 if (tmp)
342 data = (struct e820entry *)simple_strtoul(tmp, NULL, 16);
343 else
344 data = (struct e820entry *)VXWORKS_E820_DATA_ADDR;
00caae6d 345 tmp = env_get("e820info");
b90ff0fd
BM
346 if (tmp)
347 info = (struct e820info *)simple_strtoul(tmp, NULL, 16);
348 else
349 info = (struct e820info *)VXWORKS_E820_INFO_ADDR;
350
351 memset(info, 0, sizeof(struct e820info));
352 info->sign = E820_SIGNATURE;
353 info->entries = install_e820_map(E820MAX, data);
354 info->addr = (info->entries - 1) * sizeof(struct e820entry) +
355 VXWORKS_E820_DATA_ADDR;
356#endif
357
8bde7f77
WD
358 /*
359 * If the data at the load address is an elf image, then
360 * treat it like an elf image. Otherwise, assume that it is a
ebca3df7 361 * binary image.
458ded34 362 */
ebca3df7 363 if (valid_elf_image(addr))
62e03d33 364 addr = load_elf_image_shdr(addr);
ebca3df7 365 else
62e03d33 366 puts("## Not an ELF image, assuming binary\n");
458ded34 367
62e03d33 368 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
458ded34 369
6eee21da 370 dcache_disable();
9aa1280a
BM
371#ifdef CONFIG_X86
372 /* VxWorks on x86 uses stack to pass parameters */
373 ((asmlinkage void (*)(int))addr)(0);
374#else
ebca3df7 375 ((void (*)(int))addr)(0);
9aa1280a 376#endif
458ded34 377
62e03d33 378 puts("## vxWorks terminated\n");
ebca3df7 379
458ded34
WD
380 return 1;
381}
382
0d498393 383U_BOOT_CMD(
be1b8679 384 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
2fb2604d 385 "Boot from an ELF image in memory",
f44a928e
MF
386 "[-p|-s] [address]\n"
387 "\t- load ELF image at [address] via program headers (-p)\n"
388 "\t or via section headers (-s)"
8bde7f77
WD
389);
390
0d498393 391U_BOOT_CMD(
ebca3df7 392 bootvx, 2, 0, do_bootvx,
2fb2604d 393 "Boot vxWorks from an ELF image",
a89c33db 394 " [address] - load address of vxWorks ELF image."
8bde7f77 395);