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>
22 #if defined(CONFIG_WALNUT) || defined(CONFIG_SYS_VXWORKS_MAC_PTR)
23 DECLARE_GLOBAL_DATA_PTR;
26 int valid_elf_image (unsigned long addr);
27 unsigned long load_elf_image (unsigned long addr);
29 /* Allow ports to override the default behavior */
31 unsigned long do_bootelf_exec (ulong (*entry)(int, char *[]), int argc, char *argv[])
36 * QNX images require the data cache is disabled.
37 * Data cache is already flushed, so just turn it off.
39 int dcache = dcache_status ();
44 * pass address parameter as argv[0] (aka command name),
45 * and all remaining args
47 ret = entry (argc, argv);
55 /* ======================================================================
56 * Interpreter command to boot an arbitrary ELF image from memory.
57 * ====================================================================== */
58 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
60 unsigned long addr; /* Address of the ELF image */
61 unsigned long rc; /* Return value from user code */
63 /* -------------------------------------------------- */
69 addr = simple_strtoul (argv[1], NULL, 16);
71 if (!valid_elf_image (addr))
74 addr = load_elf_image (addr);
76 printf ("## Starting application at 0x%08lx ...\n", addr);
79 * pass address parameter as argv[0] (aka command name),
80 * and all remaining args
82 rc = do_bootelf_exec ((void *)addr, argc - 1, argv + 1);
86 printf ("## Application terminated, rc = 0x%lx\n", rc);
90 /* ======================================================================
91 * Interpreter command to boot VxWorks from a memory image. The image can
92 * be either an ELF image or a raw binary. Will attempt to setup the
93 * bootline and other parameters correctly.
94 * ====================================================================== */
95 int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
97 unsigned long addr; /* Address of image */
98 unsigned long bootaddr; /* Address to put the bootline */
99 char *bootline; /* Text of the bootline */
100 char *tmp; /* Temporary char pointer */
102 #if defined(CONFIG_4xx) || defined(CONFIG_IOP480)
103 char build_buf[80]; /* Buffer for building the bootline */
105 /* -------------------------------------------------- */
108 * Check the loadaddr variable.
109 * If we don't know where the image is then we're done.
115 addr = simple_strtoul (argv[1], NULL, 16);
117 #if defined(CONFIG_CMD_NET)
118 /* Check to see if we need to tftp the image ourselves before starting */
120 if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) {
121 if (NetLoop (TFTP) <= 0)
123 printf ("Automatic boot of VxWorks image at address 0x%08lx ... \n", addr);
127 /* This should equate
128 * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
129 * from the VxWorks BSP header files.
130 * This will vary from board to board
133 #if defined(CONFIG_WALNUT)
134 tmp = (char *) CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
135 memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[3], 3);
136 #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
137 tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR;
138 memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[0], 6);
140 puts ("## Ethernet MAC address not copied to NV RAM\n");
144 * Use bootaddr to find the location in memory that VxWorks
145 * will look for the bootline string. The default value for
146 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
150 if ((tmp = getenv ("bootaddr")) == NULL)
153 bootaddr = simple_strtoul (tmp, NULL, 16);
156 * Check to see if the bootline is defined in the 'bootargs'
157 * parameter. If it is not defined, we may be able to
161 if ((bootline = getenv ("bootargs")) != NULL) {
162 memcpy ((void *) bootaddr, bootline, MAX(strlen(bootline), 255));
163 flush_cache (bootaddr, MAX(strlen(bootline), 255));
165 #if defined(CONFIG_4xx)
166 sprintf (build_buf, "ibmEmac(0,0)");
168 if ((tmp = getenv ("hostname")) != NULL) {
169 sprintf (&build_buf[strlen (build_buf - 1)],
172 sprintf (&build_buf[strlen (build_buf - 1)],
176 if ((tmp = getenv ("ipaddr")) != NULL) {
177 sprintf (&build_buf[strlen (build_buf - 1)],
180 memcpy ((void *)bootaddr, build_buf, MAX(strlen(build_buf), 255));
181 flush_cache (bootaddr, MAX(strlen(build_buf), 255));
182 #elif defined(CONFIG_IOP480)
183 sprintf (build_buf, "dc(0,0)");
185 if ((tmp = getenv ("hostname")) != NULL) {
186 sprintf (&build_buf[strlen (build_buf - 1)],
189 sprintf (&build_buf[strlen (build_buf - 1)],
193 if ((tmp = getenv ("ipaddr")) != NULL) {
194 sprintf (&build_buf[strlen (build_buf - 1)],
197 memcpy ((void *) bootaddr, build_buf, MAX(strlen(build_buf), 255));
198 flush_cache (bootaddr, MAX(strlen(build_buf), 255));
202 * I'm not sure what the device should be for other
203 * PPC flavors, the hostname and ipaddr should be ok
207 puts ("No bootargs defined\n");
213 * If the data at the load address is an elf image, then
214 * treat it like an elf image. Otherwise, assume that it is a
218 if (valid_elf_image (addr)) {
219 addr = load_elf_image (addr);
221 puts ("## Not an ELF image, assuming binary\n");
222 /* leave addr as load_addr */
225 printf ("## Using bootline (@ 0x%lx): %s\n", bootaddr,
227 printf ("## Starting vxWorks at 0x%08lx ...\n", addr);
229 ((void (*)(void)) addr) ();
231 puts ("## vxWorks terminated\n");
235 /* ======================================================================
236 * Determine if a valid ELF image exists at the given memory location.
237 * First looks at the ELF header magic field, the makes sure that it is
238 * executable and makes sure that it is for a PowerPC.
239 * ====================================================================== */
240 int valid_elf_image (unsigned long addr)
242 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
244 /* -------------------------------------------------- */
246 ehdr = (Elf32_Ehdr *) addr;
248 if (!IS_ELF (*ehdr)) {
249 printf ("## No elf image at address 0x%08lx\n", addr);
253 if (ehdr->e_type != ET_EXEC) {
254 printf ("## Not a 32-bit elf image at address 0x%08lx\n",
260 if (ehdr->e_machine != EM_PPC) {
261 printf ("## Not a PowerPC elf image at address 0x%08lx\n",
271 /* ======================================================================
272 * A very simple elf loader, assumes the image is valid, returns the
273 * entry point address.
274 * ====================================================================== */
275 unsigned long load_elf_image (unsigned long addr)
277 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
278 Elf32_Shdr *shdr; /* Section header structure pointer */
279 unsigned char *strtab = 0; /* String table pointer */
280 unsigned char *image; /* Binary image pointer */
281 int i; /* Loop counter */
283 /* -------------------------------------------------- */
285 ehdr = (Elf32_Ehdr *) addr;
287 /* Find the section header string table for output info */
288 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
289 (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
291 if (shdr->sh_type == SHT_STRTAB)
292 strtab = (unsigned char *) (addr + shdr->sh_offset);
294 /* Load each appropriate section */
295 for (i = 0; i < ehdr->e_shnum; ++i) {
296 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
297 (i * sizeof (Elf32_Shdr)));
299 if (!(shdr->sh_flags & SHF_ALLOC)
300 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
305 printf ("%sing %s @ 0x%08lx (%ld bytes)\n",
306 (shdr->sh_type == SHT_NOBITS) ?
308 &strtab[shdr->sh_name],
309 (unsigned long) shdr->sh_addr,
310 (long) shdr->sh_size);
313 if (shdr->sh_type == SHT_NOBITS) {
314 memset ((void *)shdr->sh_addr, 0, shdr->sh_size);
316 image = (unsigned char *) addr + shdr->sh_offset;
317 memcpy ((void *) shdr->sh_addr,
318 (const void *) image,
321 flush_cache (shdr->sh_addr, shdr->sh_size);
324 return ehdr->e_entry;
327 /* ====================================================================== */
329 bootelf, 2, 0, do_bootelf,
330 "bootelf - Boot from an ELF image in memory\n",
331 " [address] - load address of ELF image.\n"
335 bootvx, 2, 0, do_bootvx,
336 "bootvx - Boot vxWorks from an ELF image\n",
337 " [address] - load address of vxWorks ELF image.\n"