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
19 #include <linux/ctype.h>
24 #if defined(CONFIG_WALNUT) || defined(CONFIG_SYS_VXWORKS_MAC_PTR)
25 DECLARE_GLOBAL_DATA_PTR
;
28 static unsigned long load_elf_image_phdr(unsigned long addr
);
29 static unsigned long load_elf_image_shdr(unsigned long addr
);
31 /* Allow ports to override the default behavior */
32 static unsigned long do_bootelf_exec(ulong (*entry
)(int, char * const[]),
33 int argc
, char * const argv
[])
38 * QNX images require the data cache is disabled.
39 * Data cache is already flushed, so just turn it off.
41 int dcache
= dcache_status();
46 * pass address parameter as argv[0] (aka command name),
47 * and all remaining args
49 ret
= entry(argc
, argv
);
57 /* ======================================================================
58 * Determine if a valid ELF image exists at the given memory location.
59 * First looks at the ELF header magic field, the makes sure that it is
60 * executable and makes sure that it is for a PowerPC.
61 * ====================================================================== */
62 int valid_elf_image(unsigned long addr
)
64 Elf32_Ehdr
*ehdr
; /* Elf header structure pointer */
66 /* -------------------------------------------------- */
68 ehdr
= (Elf32_Ehdr
*) addr
;
71 printf("## No elf image at address 0x%08lx\n", addr
);
75 if (ehdr
->e_type
!= ET_EXEC
) {
76 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr
);
81 if (ehdr
->e_machine
!= EM_PPC
) {
82 printf("## Not a PowerPC elf image at address 0x%08lx\n", addr
);
90 /* ======================================================================
91 * Interpreter command to boot an arbitrary ELF image from memory.
92 * ====================================================================== */
93 int do_bootelf(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
95 unsigned long addr
; /* Address of the ELF image */
96 unsigned long rc
; /* Return value from user code */
98 const char *ep
= getenv("autostart");
100 /* -------------------------------------------------- */
103 sload
= saddr
= NULL
;
107 } else if (argc
== 2) {
108 if (argv
[1][0] == '-')
115 addr
= simple_strtoul(saddr
, NULL
, 16);
119 if (!valid_elf_image(addr
))
122 if (sload
&& sload
[1] == 'p')
123 addr
= load_elf_image_phdr(addr
);
125 addr
= load_elf_image_shdr(addr
);
127 if (ep
&& !strcmp(ep
, "no"))
130 printf("## Starting application at 0x%08lx ...\n", addr
);
133 * pass address parameter as argv[0] (aka command name),
134 * and all remaining args
136 rc
= do_bootelf_exec((void *)addr
, argc
- 1, argv
+ 1);
140 printf("## Application terminated, rc = 0x%lx\n", rc
);
144 /* ======================================================================
145 * Interpreter command to boot VxWorks from a memory image. The image can
146 * be either an ELF image or a raw binary. Will attempt to setup the
147 * bootline and other parameters correctly.
148 * ====================================================================== */
149 int do_bootvx(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
151 unsigned long addr
; /* Address of image */
152 unsigned long bootaddr
; /* Address to put the bootline */
153 char *bootline
; /* Text of the bootline */
154 char *tmp
; /* Temporary char pointer */
155 char build_buf
[128]; /* Buffer for building the bootline */
157 /* ---------------------------------------------------
159 * Check the loadaddr variable.
160 * If we don't know where the image is then we're done.
166 addr
= simple_strtoul(argv
[1], NULL
, 16);
168 #if defined(CONFIG_CMD_NET)
170 * Check to see if we need to tftp the image ourselves before starting
172 if ((argc
== 2) && (strcmp(argv
[1], "tftp") == 0)) {
173 if (net_loop(TFTPGET
) <= 0)
175 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
180 /* This should equate
181 * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
182 * from the VxWorks BSP header files.
183 * This will vary from board to board
186 #if defined(CONFIG_WALNUT)
187 tmp
= (char *) CONFIG_SYS_NVRAM_BASE_ADDR
+ 0x500;
188 eth_getenv_enetaddr("ethaddr", (uchar
*)build_buf
);
189 memcpy(tmp
, &build_buf
[3], 3);
190 #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
191 tmp
= (char *) CONFIG_SYS_VXWORKS_MAC_PTR
;
192 eth_getenv_enetaddr("ethaddr", (uchar
*)build_buf
);
193 memcpy(tmp
, build_buf
, 6);
195 puts("## Ethernet MAC address not copied to NV RAM\n");
199 * Use bootaddr to find the location in memory that VxWorks
200 * will look for the bootline string. The default value for
201 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
204 tmp
= getenv("bootaddr");
206 bootaddr
= CONFIG_SYS_VXWORKS_BOOT_ADDR
;
208 bootaddr
= simple_strtoul(tmp
, NULL
, 16);
211 * Check to see if the bootline is defined in the 'bootargs'
212 * parameter. If it is not defined, we may be able to
215 bootline
= getenv("bootargs");
217 memcpy((void *)bootaddr
, bootline
,
218 max(strlen(bootline
), (size_t)255));
219 flush_cache(bootaddr
, max(strlen(bootline
), (size_t)255));
221 sprintf(build_buf
, CONFIG_SYS_VXWORKS_BOOT_DEVICE
);
222 tmp
= getenv("bootfile");
224 sprintf(&build_buf
[strlen(build_buf
)],
225 "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME
, tmp
);
227 sprintf(&build_buf
[strlen(build_buf
)],
228 "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME
);
230 tmp
= getenv("ipaddr");
232 sprintf(&build_buf
[strlen(build_buf
)], "e=%s ", tmp
);
234 tmp
= getenv("serverip");
236 sprintf(&build_buf
[strlen(build_buf
)], "h=%s ", tmp
);
238 tmp
= getenv("hostname");
240 sprintf(&build_buf
[strlen(build_buf
)], "tn=%s ", tmp
);
242 #ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
243 sprintf(&build_buf
[strlen(build_buf
)],
244 CONFIG_SYS_VXWORKS_ADD_PARAMS
);
247 memcpy((void *)bootaddr
, build_buf
,
248 max(strlen(build_buf
), (size_t)255));
249 flush_cache(bootaddr
, max(strlen(build_buf
), (size_t)255));
253 * If the data at the load address is an elf image, then
254 * treat it like an elf image. Otherwise, assume that it is a
258 if (valid_elf_image(addr
)) {
259 addr
= load_elf_image_shdr(addr
);
261 puts("## Not an ELF image, assuming binary\n");
262 /* leave addr as load_addr */
265 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr
,
267 printf("## Starting vxWorks at 0x%08lx ...\n", addr
);
270 ((void (*)(int)) addr
) (0);
272 puts("## vxWorks terminated\n");
276 /* ======================================================================
277 * A very simple elf loader, assumes the image is valid, returns the
278 * entry point address.
279 * ====================================================================== */
280 static unsigned long load_elf_image_phdr(unsigned long addr
)
282 Elf32_Ehdr
*ehdr
; /* Elf header structure pointer */
283 Elf32_Phdr
*phdr
; /* Program header structure pointer */
286 ehdr
= (Elf32_Ehdr
*) addr
;
287 phdr
= (Elf32_Phdr
*) (addr
+ ehdr
->e_phoff
);
289 /* Load each program header */
290 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
291 void *dst
= (void *)(uintptr_t) phdr
->p_paddr
;
292 void *src
= (void *) addr
+ phdr
->p_offset
;
293 debug("Loading phdr %i to 0x%p (%i bytes)\n",
294 i
, dst
, phdr
->p_filesz
);
296 memcpy(dst
, src
, phdr
->p_filesz
);
297 if (phdr
->p_filesz
!= phdr
->p_memsz
)
298 memset(dst
+ phdr
->p_filesz
, 0x00,
299 phdr
->p_memsz
- phdr
->p_filesz
);
300 flush_cache((unsigned long)dst
, phdr
->p_filesz
);
304 return ehdr
->e_entry
;
307 static unsigned long load_elf_image_shdr(unsigned long addr
)
309 Elf32_Ehdr
*ehdr
; /* Elf header structure pointer */
310 Elf32_Shdr
*shdr
; /* Section header structure pointer */
311 unsigned char *strtab
= 0; /* String table pointer */
312 unsigned char *image
; /* Binary image pointer */
313 int i
; /* Loop counter */
315 /* -------------------------------------------------- */
317 ehdr
= (Elf32_Ehdr
*) addr
;
319 /* Find the section header string table for output info */
320 shdr
= (Elf32_Shdr
*) (addr
+ ehdr
->e_shoff
+
321 (ehdr
->e_shstrndx
* sizeof(Elf32_Shdr
)));
323 if (shdr
->sh_type
== SHT_STRTAB
)
324 strtab
= (unsigned char *) (addr
+ shdr
->sh_offset
);
326 /* Load each appropriate section */
327 for (i
= 0; i
< ehdr
->e_shnum
; ++i
) {
328 shdr
= (Elf32_Shdr
*) (addr
+ ehdr
->e_shoff
+
329 (i
* sizeof(Elf32_Shdr
)));
331 if (!(shdr
->sh_flags
& SHF_ALLOC
)
332 || shdr
->sh_addr
== 0 || shdr
->sh_size
== 0) {
337 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
338 (shdr
->sh_type
== SHT_NOBITS
) ?
340 &strtab
[shdr
->sh_name
],
341 (unsigned long) shdr
->sh_addr
,
342 (long) shdr
->sh_size
);
345 if (shdr
->sh_type
== SHT_NOBITS
) {
346 memset((void *)(uintptr_t) shdr
->sh_addr
, 0,
349 image
= (unsigned char *) addr
+ shdr
->sh_offset
;
350 memcpy((void *)(uintptr_t) shdr
->sh_addr
,
351 (const void *) image
,
354 flush_cache(shdr
->sh_addr
, shdr
->sh_size
);
357 return ehdr
->e_entry
;
360 /* ====================================================================== */
362 bootelf
, 3, 0, do_bootelf
,
363 "Boot from an ELF image in memory",
364 "[-p|-s] [address]\n"
365 "\t- load ELF image at [address] via program headers (-p)\n"
366 "\t or via section headers (-s)"
370 bootvx
, 2, 0, do_bootvx
,
371 "Boot vxWorks from an ELF image",
372 " [address] - load address of vxWorks ELF image."