2 * Copyright (c) 2001 William L. Pitts
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
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
23 #include <linux/linkage.h>
27 * A very simple elf loader, assumes the image is valid, returns the
28 * entry point address.
30 static unsigned long load_elf_image_phdr(unsigned long addr
)
32 Elf32_Ehdr
*ehdr
; /* Elf header structure pointer */
33 Elf32_Phdr
*phdr
; /* Program header structure pointer */
36 ehdr
= (Elf32_Ehdr
*)addr
;
37 phdr
= (Elf32_Phdr
*)(addr
+ ehdr
->e_phoff
);
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
);
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
);
57 static unsigned long load_elf_image_shdr(unsigned long addr
)
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 */
65 ehdr
= (Elf32_Ehdr
*)addr
;
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
)));
71 if (shdr
->sh_type
== SHT_STRTAB
)
72 strtab
= (unsigned char *)(addr
+ shdr
->sh_offset
);
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
)));
79 if (!(shdr
->sh_flags
& SHF_ALLOC
) ||
80 shdr
->sh_addr
== 0 || shdr
->sh_size
== 0) {
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
,
92 if (shdr
->sh_type
== SHT_NOBITS
) {
93 memset((void *)(uintptr_t)shdr
->sh_addr
, 0,
96 image
= (unsigned char *)addr
+ shdr
->sh_offset
;
97 memcpy((void *)(uintptr_t)shdr
->sh_addr
,
98 (const void *)image
, shdr
->sh_size
);
100 flush_cache(shdr
->sh_addr
, shdr
->sh_size
);
103 return ehdr
->e_entry
;
106 /* Allow ports to override the default behavior */
107 static unsigned long do_bootelf_exec(ulong (*entry
)(int, char * const[]),
108 int argc
, char * const argv
[])
113 * QNX images require the data cache is disabled.
114 * Data cache is already flushed, so just turn it off.
116 int dcache
= dcache_status();
121 * pass address parameter as argv[0] (aka command name),
122 * and all remaining args
124 ret
= entry(argc
, argv
);
133 * Determine if a valid ELF image exists at the given memory location.
134 * First look at the ELF header magic field, then make sure that it is
137 int valid_elf_image(unsigned long addr
)
139 Elf32_Ehdr
*ehdr
; /* Elf header structure pointer */
141 ehdr
= (Elf32_Ehdr
*)addr
;
143 if (!IS_ELF(*ehdr
)) {
144 printf("## No elf image at address 0x%08lx\n", addr
);
148 if (ehdr
->e_type
!= ET_EXEC
) {
149 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr
);
156 /* Interpreter command to boot an arbitrary ELF image from memory */
157 int do_bootelf(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
159 unsigned long addr
; /* Address of the ELF image */
160 unsigned long rc
; /* Return value from user code */
162 const char *ep
= getenv("autostart");
166 sload
= saddr
= NULL
;
170 } else if (argc
== 2) {
171 if (argv
[1][0] == '-')
178 addr
= simple_strtoul(saddr
, NULL
, 16);
182 if (!valid_elf_image(addr
))
185 if (sload
&& sload
[1] == 'p')
186 addr
= load_elf_image_phdr(addr
);
188 addr
= load_elf_image_shdr(addr
);
190 if (ep
&& !strcmp(ep
, "no"))
193 printf("## Starting application at 0x%08lx ...\n", addr
);
196 * pass address parameter as argv[0] (aka command name),
197 * and all remaining args
199 rc
= do_bootelf_exec((void *)addr
, argc
- 1, argv
+ 1);
203 printf("## Application terminated, rc = 0x%lx\n", rc
);
209 * Interpreter command to boot VxWorks from a memory image. The image can
210 * be either an ELF image or a raw binary. Will attempt to setup the
211 * bootline and other parameters correctly.
213 int do_bootvx(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
215 unsigned long addr
; /* Address of image */
216 unsigned long bootaddr
; /* Address to put the bootline */
217 char *bootline
; /* Text of the bootline */
218 char *tmp
; /* Temporary char pointer */
219 char build_buf
[128]; /* Buffer for building the bootline */
222 struct e820info
*info
;
223 struct e820entry
*data
;
227 * Check the loadaddr variable.
228 * If we don't know where the image is then we're done.
233 addr
= simple_strtoul(argv
[1], NULL
, 16);
235 #if defined(CONFIG_CMD_NET)
237 * Check to see if we need to tftp the image ourselves
240 if ((argc
== 2) && (strcmp(argv
[1], "tftp") == 0)) {
241 if (net_loop(TFTPGET
) <= 0)
243 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
249 * This should equate to
250 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
251 * from the VxWorks BSP header files.
252 * This will vary from board to board
254 #if defined(CONFIG_WALNUT)
255 tmp
= (char *)CONFIG_SYS_NVRAM_BASE_ADDR
+ 0x500;
256 eth_getenv_enetaddr("ethaddr", (uchar
*)build_buf
);
257 memcpy(tmp
, &build_buf
[3], 3);
258 #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
259 tmp
= (char *)CONFIG_SYS_VXWORKS_MAC_PTR
;
260 eth_getenv_enetaddr("ethaddr", (uchar
*)build_buf
);
261 memcpy(tmp
, build_buf
, 6);
263 puts("## Ethernet MAC address not copied to NV RAM\n");
267 * Use bootaddr to find the location in memory that VxWorks
268 * will look for the bootline string. The default value is
269 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
270 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
272 tmp
= getenv("bootaddr");
274 printf("## VxWorks bootline address not specified\n");
276 bootaddr
= simple_strtoul(tmp
, NULL
, 16);
279 * Check to see if the bootline is defined in the 'bootargs'
280 * parameter. If it is not defined, we may be able to
281 * construct the info.
283 bootline
= getenv("bootargs");
285 memcpy((void *)bootaddr
, bootline
,
286 max(strlen(bootline
), (size_t)255));
287 flush_cache(bootaddr
, max(strlen(bootline
),
290 tmp
= getenv("bootdev");
292 strcpy(build_buf
, tmp
);
295 printf("## VxWorks boot device not specified\n");
297 tmp
= getenv("bootfile");
299 ptr
+= sprintf(build_buf
+ ptr
,
302 ptr
+= sprintf(build_buf
+ ptr
,
306 * The following parameters are only needed if 'bootdev'
307 * is an ethernet device, otherwise they are optional.
309 tmp
= getenv("ipaddr");
311 ptr
+= sprintf(build_buf
+ ptr
, "e=%s", tmp
);
312 tmp
= getenv("netmask");
314 u32 mask
= getenv_ip("netmask").s_addr
;
315 ptr
+= sprintf(build_buf
+ ptr
,
316 ":%08x ", ntohl(mask
));
318 ptr
+= sprintf(build_buf
+ ptr
, " ");
322 tmp
= getenv("serverip");
324 ptr
+= sprintf(build_buf
+ ptr
, "h=%s ", tmp
);
326 tmp
= getenv("gatewayip");
328 ptr
+= sprintf(build_buf
+ ptr
, "g=%s ", tmp
);
330 tmp
= getenv("hostname");
332 ptr
+= sprintf(build_buf
+ ptr
, "tn=%s ", tmp
);
334 tmp
= getenv("othbootargs");
336 strcpy(build_buf
+ ptr
, tmp
);
340 memcpy((void *)bootaddr
, build_buf
,
341 max(strlen(build_buf
), (size_t)255));
342 flush_cache(bootaddr
, max(strlen(build_buf
),
346 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr
,
352 * Since E820 information is critical to the kernel, if we don't
353 * specify these in the environments, use a default one.
355 tmp
= getenv("e820data");
357 data
= (struct e820entry
*)simple_strtoul(tmp
, NULL
, 16);
359 data
= (struct e820entry
*)VXWORKS_E820_DATA_ADDR
;
360 tmp
= getenv("e820info");
362 info
= (struct e820info
*)simple_strtoul(tmp
, NULL
, 16);
364 info
= (struct e820info
*)VXWORKS_E820_INFO_ADDR
;
366 memset(info
, 0, sizeof(struct e820info
));
367 info
->sign
= E820_SIGNATURE
;
368 info
->entries
= install_e820_map(E820MAX
, data
);
369 info
->addr
= (info
->entries
- 1) * sizeof(struct e820entry
) +
370 VXWORKS_E820_DATA_ADDR
;
374 * If the data at the load address is an elf image, then
375 * treat it like an elf image. Otherwise, assume that it is a
378 if (valid_elf_image(addr
))
379 addr
= load_elf_image_shdr(addr
);
381 puts("## Not an ELF image, assuming binary\n");
383 printf("## Starting vxWorks at 0x%08lx ...\n", addr
);
387 /* VxWorks on x86 uses stack to pass parameters */
388 ((asmlinkage
void (*)(int))addr
)(0);
390 ((void (*)(int))addr
)(0);
393 puts("## vxWorks terminated\n");
399 bootelf
, 3, 0, do_bootelf
,
400 "Boot from an ELF image in memory",
401 "[-p|-s] [address]\n"
402 "\t- load ELF image at [address] via program headers (-p)\n"
403 "\t or via section headers (-s)"
407 bootvx
, 2, 0, do_bootvx
,
408 "Boot vxWorks from an ELF image",
409 " [address] - load address of vxWorks ELF image."