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 unsigned long load_elf_image (unsigned long addr);
30 /* Allow ports to override the default behavior */
32 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 * Interpreter command to boot an arbitrary ELF image from memory.
59 * ====================================================================== */
60 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
62 unsigned long addr; /* Address of the ELF image */
63 unsigned long rc; /* Return value from user code */
65 /* -------------------------------------------------- */
71 addr = simple_strtoul (argv[1], NULL, 16);
73 if (!valid_elf_image (addr))
76 addr = load_elf_image (addr);
78 printf ("## Starting application at 0x%08lx ...\n", addr);
81 * pass address parameter as argv[0] (aka command name),
82 * and all remaining args
84 rc = do_bootelf_exec ((void *)addr, argc - 1, argv + 1);
88 printf ("## Application terminated, rc = 0x%lx\n", rc);
92 /* ======================================================================
93 * Interpreter command to boot VxWorks from a memory image. The image can
94 * be either an ELF image or a raw binary. Will attempt to setup the
95 * bootline and other parameters correctly.
96 * ====================================================================== */
97 int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
99 unsigned long addr; /* Address of image */
100 unsigned long bootaddr; /* Address to put the bootline */
101 char *bootline; /* Text of the bootline */
102 char *tmp; /* Temporary char pointer */
103 char build_buf[128]; /* Buffer for building the bootline */
105 /* ---------------------------------------------------
107 * Check the loadaddr variable.
108 * If we don't know where the image is then we're done.
114 addr = simple_strtoul (argv[1], NULL, 16);
116 #if defined(CONFIG_CMD_NET)
117 /* Check to see if we need to tftp the image ourselves before starting */
119 if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) {
120 if (NetLoop (TFTP) <= 0)
122 printf ("Automatic boot of VxWorks image at address 0x%08lx ... \n",
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 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
136 memcpy(tmp, &build_buf[3], 3);
137 #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
138 tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR;
139 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
140 memcpy(tmp, build_buf, 6);
142 puts ("## Ethernet MAC address not copied to NV RAM\n");
146 * Use bootaddr to find the location in memory that VxWorks
147 * will look for the bootline string. The default value for
148 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
152 if ((tmp = getenv ("bootaddr")) == NULL)
153 bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR;
155 bootaddr = simple_strtoul (tmp, NULL, 16);
158 * Check to see if the bootline is defined in the 'bootargs'
159 * parameter. If it is not defined, we may be able to
163 if ((bootline = getenv ("bootargs")) != NULL) {
164 memcpy ((void *) bootaddr, bootline,
165 max (strlen (bootline), 255));
166 flush_cache (bootaddr, max (strlen (bootline), 255));
170 sprintf (build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE);
171 if ((tmp = getenv ("bootfile")) != NULL) {
172 sprintf (&build_buf[strlen (build_buf)],
173 "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp);
175 sprintf (&build_buf[strlen (build_buf)],
176 "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME);
179 if ((tmp = getenv ("ipaddr")) != NULL) {
180 sprintf (&build_buf[strlen (build_buf)], "e=%s ", tmp);
183 if ((tmp = getenv ("serverip")) != NULL) {
184 sprintf (&build_buf[strlen (build_buf)], "h=%s ", tmp);
187 if ((tmp = getenv ("hostname")) != NULL) {
188 sprintf (&build_buf[strlen (build_buf)], "tn=%s ", tmp);
190 #ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
191 sprintf (&build_buf[strlen (build_buf)],
192 CONFIG_SYS_VXWORKS_ADD_PARAMS);
195 memcpy ((void *) bootaddr, build_buf,
196 max (strlen (build_buf), 255));
197 flush_cache (bootaddr, max (strlen (build_buf), 255));
201 * If the data at the load address is an elf image, then
202 * treat it like an elf image. Otherwise, assume that it is a
206 if (valid_elf_image (addr)) {
207 addr = load_elf_image (addr);
209 puts ("## Not an ELF image, assuming binary\n");
210 /* leave addr as load_addr */
213 printf ("## Using bootline (@ 0x%lx): %s\n", bootaddr,
215 printf ("## Starting vxWorks at 0x%08lx ...\n", addr);
217 ((void (*)(void)) addr) ();
219 puts ("## vxWorks terminated\n");
223 /* ======================================================================
224 * Determine if a valid ELF image exists at the given memory location.
225 * First looks at the ELF header magic field, the makes sure that it is
226 * executable and makes sure that it is for a PowerPC.
227 * ====================================================================== */
228 int valid_elf_image (unsigned long addr)
230 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
232 /* -------------------------------------------------- */
234 ehdr = (Elf32_Ehdr *) addr;
236 if (!IS_ELF (*ehdr)) {
237 printf ("## No elf image at address 0x%08lx\n", addr);
241 if (ehdr->e_type != ET_EXEC) {
242 printf ("## Not a 32-bit elf image at address 0x%08lx\n", addr);
247 if (ehdr->e_machine != EM_PPC) {
248 printf ("## Not a PowerPC elf image at address 0x%08lx\n",
257 /* ======================================================================
258 * A very simple elf loader, assumes the image is valid, returns the
259 * entry point address.
260 * ====================================================================== */
261 unsigned long load_elf_image (unsigned long addr)
263 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
264 Elf32_Shdr *shdr; /* Section header structure pointer */
265 unsigned char *strtab = 0; /* String table pointer */
266 unsigned char *image; /* Binary image pointer */
267 int i; /* Loop counter */
269 /* -------------------------------------------------- */
271 ehdr = (Elf32_Ehdr *) addr;
273 /* Find the section header string table for output info */
274 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
275 (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
277 if (shdr->sh_type == SHT_STRTAB)
278 strtab = (unsigned char *) (addr + shdr->sh_offset);
280 /* Load each appropriate section */
281 for (i = 0; i < ehdr->e_shnum; ++i) {
282 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
283 (i * sizeof (Elf32_Shdr)));
285 if (!(shdr->sh_flags & SHF_ALLOC)
286 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
291 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
292 (shdr->sh_type == SHT_NOBITS) ?
294 &strtab[shdr->sh_name],
295 (unsigned long) shdr->sh_addr,
296 (long) shdr->sh_size);
299 if (shdr->sh_type == SHT_NOBITS) {
300 memset ((void *)shdr->sh_addr, 0, shdr->sh_size);
302 image = (unsigned char *) addr + shdr->sh_offset;
303 memcpy ((void *) shdr->sh_addr,
304 (const void *) image,
307 flush_cache (shdr->sh_addr, shdr->sh_size);
310 return ehdr->e_entry;
313 /* ====================================================================== */
315 bootelf, 2, 0, do_bootelf,
316 "Boot from an ELF image in memory",
317 " [address] - load address of ELF image."
321 bootvx, 2, 0, do_bootvx,
322 "Boot vxWorks from an ELF image",
323 " [address] - load address of vxWorks ELF image."