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 int valid_elf_image (unsigned long addr);
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 */
33 unsigned long do_bootelf_exec (ulong (*entry)(int, char * const[]),
34 int argc, char * const argv[])
39 * QNX images require the data cache is disabled.
40 * Data cache is already flushed, so just turn it off.
42 int dcache = dcache_status ();
47 * pass address parameter as argv[0] (aka command name),
48 * and all remaining args
50 ret = entry (argc, argv);
58 /* ======================================================================
59 * Interpreter command to boot an arbitrary ELF image from memory.
60 * ====================================================================== */
61 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
63 unsigned long addr; /* Address of the ELF image */
64 unsigned long rc; /* Return value from user code */
67 /* -------------------------------------------------- */
74 } else if (argc == 2) {
75 if (argv[1][0] == '-')
82 addr = simple_strtoul(saddr, NULL, 16);
86 if (!valid_elf_image (addr))
89 if (sload && sload[1] == 'p')
90 addr = load_elf_image_phdr(addr);
92 addr = load_elf_image_shdr(addr);
94 printf ("## Starting application at 0x%08lx ...\n", addr);
97 * pass address parameter as argv[0] (aka command name),
98 * and all remaining args
100 rc = do_bootelf_exec ((void *)addr, argc - 1, argv + 1);
104 printf ("## Application terminated, rc = 0x%lx\n", rc);
108 /* ======================================================================
109 * Interpreter command to boot VxWorks from a memory image. The image can
110 * be either an ELF image or a raw binary. Will attempt to setup the
111 * bootline and other parameters correctly.
112 * ====================================================================== */
113 int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
115 unsigned long addr; /* Address of image */
116 unsigned long bootaddr; /* Address to put the bootline */
117 char *bootline; /* Text of the bootline */
118 char *tmp; /* Temporary char pointer */
119 char build_buf[128]; /* Buffer for building the bootline */
121 /* ---------------------------------------------------
123 * Check the loadaddr variable.
124 * If we don't know where the image is then we're done.
130 addr = simple_strtoul (argv[1], NULL, 16);
132 #if defined(CONFIG_CMD_NET)
133 /* Check to see if we need to tftp the image ourselves before starting */
135 if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) {
136 if (NetLoop (TFTP) <= 0)
138 printf ("Automatic boot of VxWorks image at address 0x%08lx ... \n",
143 /* This should equate
144 * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
145 * from the VxWorks BSP header files.
146 * This will vary from board to board
149 #if defined(CONFIG_WALNUT)
150 tmp = (char *) CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
151 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
152 memcpy(tmp, &build_buf[3], 3);
153 #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
154 tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR;
155 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
156 memcpy(tmp, build_buf, 6);
158 puts ("## Ethernet MAC address not copied to NV RAM\n");
162 * Use bootaddr to find the location in memory that VxWorks
163 * will look for the bootline string. The default value for
164 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
168 if ((tmp = getenv ("bootaddr")) == NULL)
169 bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR;
171 bootaddr = simple_strtoul (tmp, NULL, 16);
174 * Check to see if the bootline is defined in the 'bootargs'
175 * parameter. If it is not defined, we may be able to
179 if ((bootline = getenv ("bootargs")) != NULL) {
180 memcpy ((void *) bootaddr, bootline,
181 max (strlen (bootline), 255));
182 flush_cache (bootaddr, max (strlen (bootline), 255));
186 sprintf (build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE);
187 if ((tmp = getenv ("bootfile")) != NULL) {
188 sprintf (&build_buf[strlen (build_buf)],
189 "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp);
191 sprintf (&build_buf[strlen (build_buf)],
192 "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME);
195 if ((tmp = getenv ("ipaddr")) != NULL) {
196 sprintf (&build_buf[strlen (build_buf)], "e=%s ", tmp);
199 if ((tmp = getenv ("serverip")) != NULL) {
200 sprintf (&build_buf[strlen (build_buf)], "h=%s ", tmp);
203 if ((tmp = getenv ("hostname")) != NULL) {
204 sprintf (&build_buf[strlen (build_buf)], "tn=%s ", tmp);
206 #ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
207 sprintf (&build_buf[strlen (build_buf)],
208 CONFIG_SYS_VXWORKS_ADD_PARAMS);
211 memcpy ((void *) bootaddr, build_buf,
212 max (strlen (build_buf), 255));
213 flush_cache (bootaddr, max (strlen (build_buf), 255));
217 * If the data at the load address is an elf image, then
218 * treat it like an elf image. Otherwise, assume that it is a
222 if (valid_elf_image (addr)) {
223 addr = load_elf_image_shdr (addr);
225 puts ("## Not an ELF image, assuming binary\n");
226 /* leave addr as load_addr */
229 printf ("## Using bootline (@ 0x%lx): %s\n", bootaddr,
231 printf ("## Starting vxWorks at 0x%08lx ...\n", addr);
233 ((void (*)(void)) addr) ();
235 puts ("## vxWorks terminated\n");
239 /* ======================================================================
240 * Determine if a valid ELF image exists at the given memory location.
241 * First looks at the ELF header magic field, the makes sure that it is
242 * executable and makes sure that it is for a PowerPC.
243 * ====================================================================== */
244 int valid_elf_image (unsigned long addr)
246 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
248 /* -------------------------------------------------- */
250 ehdr = (Elf32_Ehdr *) addr;
252 if (!IS_ELF (*ehdr)) {
253 printf ("## No elf image at address 0x%08lx\n", addr);
257 if (ehdr->e_type != ET_EXEC) {
258 printf ("## Not a 32-bit elf image at address 0x%08lx\n", addr);
263 if (ehdr->e_machine != EM_PPC) {
264 printf ("## Not a PowerPC elf image at address 0x%08lx\n",
273 /* ======================================================================
274 * A very simple elf loader, assumes the image is valid, returns the
275 * entry point address.
276 * ====================================================================== */
277 static unsigned long load_elf_image_phdr(unsigned long addr)
279 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
280 Elf32_Phdr *phdr; /* Program header structure pointer */
283 ehdr = (Elf32_Ehdr *) addr;
284 phdr = (Elf32_Phdr *) (addr + ehdr->e_phoff);
286 /* Load each program header */
287 for (i = 0; i < ehdr->e_phnum; ++i) {
288 void *dst = (void *) phdr->p_paddr;
289 void *src = (void *) addr + phdr->p_offset;
290 debug("Loading phdr %i to 0x%p (%i bytes)\n",
291 i, dst, phdr->p_filesz);
293 memcpy(dst, src, phdr->p_filesz);
294 if (phdr->p_filesz != phdr->p_memsz)
295 memset(dst + phdr->p_filesz, 0x00, phdr->p_memsz - phdr->p_filesz);
296 flush_cache((unsigned long)dst, phdr->p_filesz);
300 return ehdr->e_entry;
303 static unsigned long load_elf_image_shdr(unsigned long addr)
305 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
306 Elf32_Shdr *shdr; /* Section header structure pointer */
307 unsigned char *strtab = 0; /* String table pointer */
308 unsigned char *image; /* Binary image pointer */
309 int i; /* Loop counter */
311 /* -------------------------------------------------- */
313 ehdr = (Elf32_Ehdr *) addr;
315 /* Find the section header string table for output info */
316 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
317 (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
319 if (shdr->sh_type == SHT_STRTAB)
320 strtab = (unsigned char *) (addr + shdr->sh_offset);
322 /* Load each appropriate section */
323 for (i = 0; i < ehdr->e_shnum; ++i) {
324 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
325 (i * sizeof (Elf32_Shdr)));
327 if (!(shdr->sh_flags & SHF_ALLOC)
328 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
333 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
334 (shdr->sh_type == SHT_NOBITS) ?
336 &strtab[shdr->sh_name],
337 (unsigned long) shdr->sh_addr,
338 (long) shdr->sh_size);
341 if (shdr->sh_type == SHT_NOBITS) {
342 memset ((void *)shdr->sh_addr, 0, shdr->sh_size);
344 image = (unsigned char *) addr + shdr->sh_offset;
345 memcpy ((void *) 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."