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 */
219 * Check the loadaddr variable.
220 * If we don't know where the image is then we're done.
225 addr = simple_strtoul(argv[1], NULL, 16);
227 #if defined(CONFIG_CMD_NET)
229 * Check to see if we need to tftp the image ourselves
232 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
233 if (net_loop(TFTPGET) <= 0)
235 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
241 * This should equate to
242 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
243 * from the VxWorks BSP header files.
244 * This will vary from board to board
246 #if defined(CONFIG_WALNUT)
247 tmp = (char *)CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
248 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
249 memcpy(tmp, &build_buf[3], 3);
250 #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
251 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
252 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
253 memcpy(tmp, build_buf, 6);
255 puts("## Ethernet MAC address not copied to NV RAM\n");
259 * Use bootaddr to find the location in memory that VxWorks
260 * will look for the bootline string. The default value for
261 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
262 * defaults to 0x4200.
264 tmp = getenv("bootaddr");
266 bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR;
268 bootaddr = simple_strtoul(tmp, NULL, 16);
271 * Check to see if the bootline is defined in the 'bootargs'
272 * parameter. If it is not defined, we may be able to
273 * construct the info.
275 bootline = getenv("bootargs");
277 memcpy((void *)bootaddr, bootline,
278 max(strlen(bootline), (size_t)255));
279 flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
281 ptr = sprintf(build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE);
282 tmp = getenv("bootfile");
284 ptr += sprintf(build_buf + ptr, "%s:%s ",
285 CONFIG_SYS_VXWORKS_SERVERNAME, tmp);
287 ptr += sprintf(build_buf + ptr, "%s:file ",
288 CONFIG_SYS_VXWORKS_SERVERNAME);
290 tmp = getenv("ipaddr");
292 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
293 tmp = getenv("netmask");
295 __be32 addr = getenv_ip("netmask").s_addr;
296 ptr += sprintf(build_buf + ptr, ":%08x ",
299 ptr += sprintf(build_buf + ptr, " ");
303 tmp = getenv("serverip");
305 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
307 tmp = getenv("gatewayip");
309 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
311 tmp = getenv("hostname");
313 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
315 #ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
316 ptr += sprintf(build_buf + ptr, CONFIG_SYS_VXWORKS_ADD_PARAMS);
319 memcpy((void *)bootaddr, build_buf,
320 max(strlen(build_buf), (size_t)255));
321 flush_cache(bootaddr, max(strlen(build_buf), (size_t)255));
325 * If the data at the load address is an elf image, then
326 * treat it like an elf image. Otherwise, assume that it is a
329 if (valid_elf_image(addr))
330 addr = load_elf_image_shdr(addr);
332 puts("## Not an ELF image, assuming binary\n");
334 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
336 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
339 ((void (*)(int))addr)(0);
341 puts("## vxWorks terminated\n");
347 bootelf, 3, 0, do_bootelf,
348 "Boot from an ELF image in memory",
349 "[-p|-s] [address]\n"
350 "\t- load ELF image at [address] via program headers (-p)\n"
351 "\t or via section headers (-s)"
355 bootvx, 2, 0, do_bootvx,
356 "Boot vxWorks from an ELF image",
357 " [address] - load address of vxWorks ELF image."