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
23 * A very simple elf loader, assumes the image is valid, returns the
24 * entry point address.
26 static unsigned long load_elf_image_phdr(unsigned long addr)
28 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
29 Elf32_Phdr *phdr; /* Program header structure pointer */
32 ehdr = (Elf32_Ehdr *)addr;
33 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
35 /* Load each program header */
36 for (i = 0; i < ehdr->e_phnum; ++i) {
37 void *dst = (void *)(uintptr_t)phdr->p_paddr;
38 void *src = (void *)addr + phdr->p_offset;
39 debug("Loading phdr %i to 0x%p (%i bytes)\n",
40 i, dst, phdr->p_filesz);
42 memcpy(dst, src, phdr->p_filesz);
43 if (phdr->p_filesz != phdr->p_memsz)
44 memset(dst + phdr->p_filesz, 0x00,
45 phdr->p_memsz - phdr->p_filesz);
46 flush_cache((unsigned long)dst, phdr->p_filesz);
53 static unsigned long load_elf_image_shdr(unsigned long addr)
55 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
56 Elf32_Shdr *shdr; /* Section header structure pointer */
57 unsigned char *strtab = 0; /* String table pointer */
58 unsigned char *image; /* Binary image pointer */
59 int i; /* Loop counter */
61 ehdr = (Elf32_Ehdr *)addr;
63 /* Find the section header string table for output info */
64 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
65 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
67 if (shdr->sh_type == SHT_STRTAB)
68 strtab = (unsigned char *)(addr + shdr->sh_offset);
70 /* Load each appropriate section */
71 for (i = 0; i < ehdr->e_shnum; ++i) {
72 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
73 (i * sizeof(Elf32_Shdr)));
75 if (!(shdr->sh_flags & SHF_ALLOC) ||
76 shdr->sh_addr == 0 || shdr->sh_size == 0) {
81 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
82 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
83 &strtab[shdr->sh_name],
84 (unsigned long)shdr->sh_addr,
88 if (shdr->sh_type == SHT_NOBITS) {
89 memset((void *)(uintptr_t)shdr->sh_addr, 0,
92 image = (unsigned char *)addr + shdr->sh_offset;
93 memcpy((void *)(uintptr_t)shdr->sh_addr,
94 (const void *)image, shdr->sh_size);
96 flush_cache(shdr->sh_addr, shdr->sh_size);
102 /* Allow ports to override the default behavior */
103 static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
104 int argc, char * const argv[])
109 * QNX images require the data cache is disabled.
110 * Data cache is already flushed, so just turn it off.
112 int dcache = dcache_status();
117 * pass address parameter as argv[0] (aka command name),
118 * and all remaining args
120 ret = entry(argc, argv);
129 * Determine if a valid ELF image exists at the given memory location.
130 * First look at the ELF header magic field, then make sure that it is
133 int valid_elf_image(unsigned long addr)
135 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
137 ehdr = (Elf32_Ehdr *)addr;
139 if (!IS_ELF(*ehdr)) {
140 printf("## No elf image at address 0x%08lx\n", addr);
144 if (ehdr->e_type != ET_EXEC) {
145 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
152 /* Interpreter command to boot an arbitrary ELF image from memory */
153 int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
155 unsigned long addr; /* Address of the ELF image */
156 unsigned long rc; /* Return value from user code */
158 const char *ep = getenv("autostart");
162 sload = saddr = NULL;
166 } else if (argc == 2) {
167 if (argv[1][0] == '-')
174 addr = simple_strtoul(saddr, NULL, 16);
178 if (!valid_elf_image(addr))
181 if (sload && sload[1] == 'p')
182 addr = load_elf_image_phdr(addr);
184 addr = load_elf_image_shdr(addr);
186 if (ep && !strcmp(ep, "no"))
189 printf("## Starting application at 0x%08lx ...\n", addr);
192 * pass address parameter as argv[0] (aka command name),
193 * and all remaining args
195 rc = do_bootelf_exec((void *)addr, argc - 1, argv + 1);
199 printf("## Application terminated, rc = 0x%lx\n", rc);
205 * Interpreter command to boot VxWorks from a memory image. The image can
206 * be either an ELF image or a raw binary. Will attempt to setup the
207 * bootline and other parameters correctly.
209 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
211 unsigned long addr; /* Address of image */
212 unsigned long bootaddr; /* Address to put the bootline */
213 char *bootline; /* Text of the bootline */
214 char *tmp; /* Temporary char pointer */
215 char build_buf[128]; /* Buffer for building the bootline */
218 * Check the loadaddr variable.
219 * If we don't know where the image is then we're done.
224 addr = simple_strtoul(argv[1], NULL, 16);
226 #if defined(CONFIG_CMD_NET)
228 * Check to see if we need to tftp the image ourselves
231 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
232 if (net_loop(TFTPGET) <= 0)
234 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
240 * This should equate to
241 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
242 * from the VxWorks BSP header files.
243 * This will vary from board to board
245 #if defined(CONFIG_WALNUT)
246 tmp = (char *)CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
247 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
248 memcpy(tmp, &build_buf[3], 3);
249 #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
250 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
251 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
252 memcpy(tmp, build_buf, 6);
254 puts("## Ethernet MAC address not copied to NV RAM\n");
258 * Use bootaddr to find the location in memory that VxWorks
259 * will look for the bootline string. The default value for
260 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
261 * defaults to 0x4200.
263 tmp = getenv("bootaddr");
265 bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR;
267 bootaddr = simple_strtoul(tmp, NULL, 16);
270 * Check to see if the bootline is defined in the 'bootargs'
271 * parameter. If it is not defined, we may be able to
272 * construct the info.
274 bootline = getenv("bootargs");
276 memcpy((void *)bootaddr, bootline,
277 max(strlen(bootline), (size_t)255));
278 flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
280 sprintf(build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE);
281 tmp = getenv("bootfile");
283 sprintf(&build_buf[strlen(build_buf)],
284 "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp);
286 sprintf(&build_buf[strlen(build_buf)],
287 "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME);
289 tmp = getenv("ipaddr");
291 sprintf(&build_buf[strlen(build_buf)], "e=%s ", tmp);
293 tmp = getenv("serverip");
295 sprintf(&build_buf[strlen(build_buf)], "h=%s ", tmp);
297 tmp = getenv("hostname");
299 sprintf(&build_buf[strlen(build_buf)], "tn=%s ", tmp);
301 #ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
302 sprintf(&build_buf[strlen(build_buf)],
303 CONFIG_SYS_VXWORKS_ADD_PARAMS);
306 memcpy((void *)bootaddr, build_buf,
307 max(strlen(build_buf), (size_t)255));
308 flush_cache(bootaddr, max(strlen(build_buf), (size_t)255));
312 * If the data at the load address is an elf image, then
313 * treat it like an elf image. Otherwise, assume that it is a
316 if (valid_elf_image(addr))
317 addr = load_elf_image_shdr(addr);
319 puts("## Not an ELF image, assuming binary\n");
321 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
323 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
326 ((void (*)(int))addr)(0);
328 puts("## vxWorks terminated\n");
334 bootelf, 3, 0, do_bootelf,
335 "Boot from an ELF image in memory",
336 "[-p|-s] [address]\n"
337 "\t- load ELF image at [address] via program headers (-p)\n"
338 "\t or via section headers (-s)"
342 bootvx, 2, 0, do_bootvx,
343 "Boot vxWorks from an ELF image",
344 " [address] - load address of vxWorks ELF image."