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
18 #include <linux/ctype.h>
23 #if defined(CONFIG_WALNUT) || defined(CONFIG_SYS_VXWORKS_MAC_PTR)
24 DECLARE_GLOBAL_DATA_PTR
;
27 static unsigned long load_elf_image_phdr(unsigned long addr
);
28 static unsigned long load_elf_image_shdr(unsigned long addr
);
30 /* Allow ports to override the default behavior */
31 static unsigned long do_bootelf_exec(ulong (*entry
)(int, char * const[]),
32 int argc
, char * const argv
[])
37 * QNX images require the data cache is disabled.
38 * Data cache is already flushed, so just turn it off.
40 int dcache
= dcache_status();
45 * pass address parameter as argv[0] (aka command name),
46 * and all remaining args
48 ret
= entry(argc
, argv
);
56 /* ======================================================================
57 * Determine if a valid ELF image exists at the given memory location.
58 * First looks at the ELF header magic field, the makes sure that it is
59 * executable and makes sure that it is for a PowerPC.
60 * ====================================================================== */
61 int valid_elf_image(unsigned long addr
)
63 Elf32_Ehdr
*ehdr
; /* Elf header structure pointer */
65 /* -------------------------------------------------- */
67 ehdr
= (Elf32_Ehdr
*) addr
;
70 printf("## No elf image at address 0x%08lx\n", addr
);
74 if (ehdr
->e_type
!= ET_EXEC
) {
75 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr
);
80 if (ehdr
->e_machine
!= EM_PPC
) {
81 printf("## Not a PowerPC elf image at address 0x%08lx\n", addr
);
89 /* ======================================================================
90 * Interpreter command to boot an arbitrary ELF image from memory.
91 * ====================================================================== */
92 int do_bootelf(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
94 unsigned long addr
; /* Address of the ELF image */
95 unsigned long rc
; /* Return value from user code */
98 /* -------------------------------------------------- */
101 sload
= saddr
= NULL
;
105 } else if (argc
== 2) {
106 if (argv
[1][0] == '-')
113 addr
= simple_strtoul(saddr
, NULL
, 16);
117 if (!valid_elf_image(addr
))
120 if (sload
&& sload
[1] == 'p')
121 addr
= load_elf_image_phdr(addr
);
123 addr
= load_elf_image_shdr(addr
);
125 printf("## Starting application at 0x%08lx ...\n", addr
);
128 * pass address parameter as argv[0] (aka command name),
129 * and all remaining args
131 rc
= do_bootelf_exec((void *)addr
, argc
- 1, argv
+ 1);
135 printf("## Application terminated, rc = 0x%lx\n", rc
);
139 /* ======================================================================
140 * Interpreter command to boot VxWorks from a memory image. The image can
141 * be either an ELF image or a raw binary. Will attempt to setup the
142 * bootline and other parameters correctly.
143 * ====================================================================== */
144 int do_bootvx(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
146 unsigned long addr
; /* Address of image */
147 unsigned long bootaddr
; /* Address to put the bootline */
148 char *bootline
; /* Text of the bootline */
149 char *tmp
; /* Temporary char pointer */
150 char build_buf
[128]; /* Buffer for building the bootline */
152 /* ---------------------------------------------------
154 * Check the loadaddr variable.
155 * If we don't know where the image is then we're done.
161 addr
= simple_strtoul(argv
[1], NULL
, 16);
163 #if defined(CONFIG_CMD_NET)
165 * Check to see if we need to tftp the image ourselves before starting
167 if ((argc
== 2) && (strcmp(argv
[1], "tftp") == 0)) {
168 if (NetLoop(TFTPGET
) <= 0)
170 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
175 /* This should equate
176 * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
177 * from the VxWorks BSP header files.
178 * This will vary from board to board
181 #if defined(CONFIG_WALNUT)
182 tmp
= (char *) CONFIG_SYS_NVRAM_BASE_ADDR
+ 0x500;
183 eth_getenv_enetaddr("ethaddr", (uchar
*)build_buf
);
184 memcpy(tmp
, &build_buf
[3], 3);
185 #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
186 tmp
= (char *) CONFIG_SYS_VXWORKS_MAC_PTR
;
187 eth_getenv_enetaddr("ethaddr", (uchar
*)build_buf
);
188 memcpy(tmp
, build_buf
, 6);
190 puts("## Ethernet MAC address not copied to NV RAM\n");
194 * Use bootaddr to find the location in memory that VxWorks
195 * will look for the bootline string. The default value for
196 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
199 tmp
= getenv("bootaddr");
201 bootaddr
= CONFIG_SYS_VXWORKS_BOOT_ADDR
;
203 bootaddr
= simple_strtoul(tmp
, NULL
, 16);
206 * Check to see if the bootline is defined in the 'bootargs'
207 * parameter. If it is not defined, we may be able to
210 bootline
= getenv("bootargs");
212 memcpy((void *) bootaddr
, bootline
,
213 max(strlen(bootline
), 255));
214 flush_cache(bootaddr
, max(strlen(bootline
), 255));
216 sprintf(build_buf
, CONFIG_SYS_VXWORKS_BOOT_DEVICE
);
217 tmp
= getenv("bootfile");
219 sprintf(&build_buf
[strlen(build_buf
)],
220 "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME
, tmp
);
222 sprintf(&build_buf
[strlen(build_buf
)],
223 "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME
);
225 tmp
= getenv("ipaddr");
227 sprintf(&build_buf
[strlen(build_buf
)], "e=%s ", tmp
);
229 tmp
= getenv("serverip");
231 sprintf(&build_buf
[strlen(build_buf
)], "h=%s ", tmp
);
233 tmp
= getenv("hostname");
235 sprintf(&build_buf
[strlen(build_buf
)], "tn=%s ", tmp
);
237 #ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
238 sprintf(&build_buf
[strlen(build_buf
)],
239 CONFIG_SYS_VXWORKS_ADD_PARAMS
);
242 memcpy((void *) bootaddr
, build_buf
,
243 max(strlen(build_buf
), 255));
244 flush_cache(bootaddr
, max(strlen(build_buf
), 255));
248 * If the data at the load address is an elf image, then
249 * treat it like an elf image. Otherwise, assume that it is a
253 if (valid_elf_image(addr
)) {
254 addr
= load_elf_image_shdr(addr
);
256 puts("## Not an ELF image, assuming binary\n");
257 /* leave addr as load_addr */
260 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr
,
262 printf("## Starting vxWorks at 0x%08lx ...\n", addr
);
265 ((void (*)(int)) addr
) (0);
267 puts("## vxWorks terminated\n");
271 /* ======================================================================
272 * A very simple elf loader, assumes the image is valid, returns the
273 * entry point address.
274 * ====================================================================== */
275 static unsigned long load_elf_image_phdr(unsigned long addr
)
277 Elf32_Ehdr
*ehdr
; /* Elf header structure pointer */
278 Elf32_Phdr
*phdr
; /* Program header structure pointer */
281 ehdr
= (Elf32_Ehdr
*) addr
;
282 phdr
= (Elf32_Phdr
*) (addr
+ ehdr
->e_phoff
);
284 /* Load each program header */
285 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
286 void *dst
= (void *)(uintptr_t) phdr
->p_paddr
;
287 void *src
= (void *) addr
+ phdr
->p_offset
;
288 debug("Loading phdr %i to 0x%p (%i bytes)\n",
289 i
, dst
, phdr
->p_filesz
);
291 memcpy(dst
, src
, phdr
->p_filesz
);
292 if (phdr
->p_filesz
!= phdr
->p_memsz
)
293 memset(dst
+ phdr
->p_filesz
, 0x00,
294 phdr
->p_memsz
- phdr
->p_filesz
);
295 flush_cache((unsigned long)dst
, phdr
->p_filesz
);
299 return ehdr
->e_entry
;
302 static unsigned long load_elf_image_shdr(unsigned long addr
)
304 Elf32_Ehdr
*ehdr
; /* Elf header structure pointer */
305 Elf32_Shdr
*shdr
; /* Section header structure pointer */
306 unsigned char *strtab
= 0; /* String table pointer */
307 unsigned char *image
; /* Binary image pointer */
308 int i
; /* Loop counter */
310 /* -------------------------------------------------- */
312 ehdr
= (Elf32_Ehdr
*) addr
;
314 /* Find the section header string table for output info */
315 shdr
= (Elf32_Shdr
*) (addr
+ ehdr
->e_shoff
+
316 (ehdr
->e_shstrndx
* sizeof(Elf32_Shdr
)));
318 if (shdr
->sh_type
== SHT_STRTAB
)
319 strtab
= (unsigned char *) (addr
+ shdr
->sh_offset
);
321 /* Load each appropriate section */
322 for (i
= 0; i
< ehdr
->e_shnum
; ++i
) {
323 shdr
= (Elf32_Shdr
*) (addr
+ ehdr
->e_shoff
+
324 (i
* sizeof(Elf32_Shdr
)));
326 if (!(shdr
->sh_flags
& SHF_ALLOC
)
327 || shdr
->sh_addr
== 0 || shdr
->sh_size
== 0) {
332 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
333 (shdr
->sh_type
== SHT_NOBITS
) ?
335 &strtab
[shdr
->sh_name
],
336 (unsigned long) shdr
->sh_addr
,
337 (long) shdr
->sh_size
);
340 if (shdr
->sh_type
== SHT_NOBITS
) {
341 memset((void *)(uintptr_t) shdr
->sh_addr
, 0,
344 image
= (unsigned char *) addr
+ shdr
->sh_offset
;
345 memcpy((void *)(uintptr_t) shdr
->sh_addr
,
346 (const void *) image
,
349 flush_cache(shdr
->sh_addr
, shdr
->sh_size
);
352 return ehdr
->e_entry
;
355 /* ====================================================================== */
357 bootelf
, 3, 0, do_bootelf
,
358 "Boot from an ELF image in memory",
359 "[-p|-s] [address]\n"
360 "\t- load ELF image at [address] via program headers (-p)\n"
361 "\t or via section headers (-s)"
365 bootvx
, 2, 0, do_bootvx
,
366 "Boot vxWorks from an ELF image",
367 " [address] - load address of vxWorks ELF image."