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
26 * A very simple elf loader, assumes the image is valid, returns the
27 * entry point address.
29 static unsigned long load_elf_image_phdr(unsigned long addr)
31 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
32 Elf32_Phdr *phdr; /* Program header structure pointer */
35 ehdr = (Elf32_Ehdr *)addr;
36 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
38 /* Load each program header */
39 for (i = 0; i < ehdr->e_phnum; ++i) {
40 void *dst = (void *)(uintptr_t)phdr->p_paddr;
41 void *src = (void *)addr + phdr->p_offset;
42 debug("Loading phdr %i to 0x%p (%i bytes)\n",
43 i, dst, phdr->p_filesz);
45 memcpy(dst, src, phdr->p_filesz);
46 if (phdr->p_filesz != phdr->p_memsz)
47 memset(dst + phdr->p_filesz, 0x00,
48 phdr->p_memsz - phdr->p_filesz);
49 flush_cache((unsigned long)dst, phdr->p_filesz);
56 static unsigned long load_elf_image_shdr(unsigned long addr)
58 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
59 Elf32_Shdr *shdr; /* Section header structure pointer */
60 unsigned char *strtab = 0; /* String table pointer */
61 unsigned char *image; /* Binary image pointer */
62 int i; /* Loop counter */
64 ehdr = (Elf32_Ehdr *)addr;
66 /* Find the section header string table for output info */
67 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
68 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
70 if (shdr->sh_type == SHT_STRTAB)
71 strtab = (unsigned char *)(addr + shdr->sh_offset);
73 /* Load each appropriate section */
74 for (i = 0; i < ehdr->e_shnum; ++i) {
75 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
76 (i * sizeof(Elf32_Shdr)));
78 if (!(shdr->sh_flags & SHF_ALLOC) ||
79 shdr->sh_addr == 0 || shdr->sh_size == 0) {
84 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
85 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
86 &strtab[shdr->sh_name],
87 (unsigned long)shdr->sh_addr,
91 if (shdr->sh_type == SHT_NOBITS) {
92 memset((void *)(uintptr_t)shdr->sh_addr, 0,
95 image = (unsigned char *)addr + shdr->sh_offset;
96 memcpy((void *)(uintptr_t)shdr->sh_addr,
97 (const void *)image, shdr->sh_size);
99 flush_cache(shdr->sh_addr, shdr->sh_size);
102 return ehdr->e_entry;
105 /* Allow ports to override the default behavior */
106 static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
107 int argc, char * const argv[])
112 * QNX images require the data cache is disabled.
113 * Data cache is already flushed, so just turn it off.
115 int dcache = dcache_status();
120 * pass address parameter as argv[0] (aka command name),
121 * and all remaining args
123 ret = entry(argc, argv);
132 * Determine if a valid ELF image exists at the given memory location.
133 * First look at the ELF header magic field, then make sure that it is
136 int valid_elf_image(unsigned long addr)
138 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
140 ehdr = (Elf32_Ehdr *)addr;
142 if (!IS_ELF(*ehdr)) {
143 printf("## No elf image at address 0x%08lx\n", addr);
147 if (ehdr->e_type != ET_EXEC) {
148 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
155 /* Interpreter command to boot an arbitrary ELF image from memory */
156 int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
158 unsigned long addr; /* Address of the ELF image */
159 unsigned long rc; /* Return value from user code */
161 const char *ep = getenv("autostart");
165 sload = saddr = NULL;
169 } else if (argc == 2) {
170 if (argv[1][0] == '-')
177 addr = simple_strtoul(saddr, NULL, 16);
181 if (!valid_elf_image(addr))
184 if (sload && sload[1] == 'p')
185 addr = load_elf_image_phdr(addr);
187 addr = load_elf_image_shdr(addr);
189 if (ep && !strcmp(ep, "no"))
192 printf("## Starting application at 0x%08lx ...\n", addr);
195 * pass address parameter as argv[0] (aka command name),
196 * and all remaining args
198 rc = do_bootelf_exec((void *)addr, argc - 1, argv + 1);
202 printf("## Application terminated, rc = 0x%lx\n", rc);
208 * Interpreter command to boot VxWorks from a memory image. The image can
209 * be either an ELF image or a raw binary. Will attempt to setup the
210 * bootline and other parameters correctly.
212 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
214 unsigned long addr; /* Address of image */
215 unsigned long bootaddr; /* Address to put the bootline */
216 char *bootline; /* Text of the bootline */
217 char *tmp; /* Temporary char pointer */
218 char build_buf[128]; /* Buffer for building the bootline */
221 struct e820info *info;
222 struct e820entry *data;
226 * Check the loadaddr variable.
227 * If we don't know where the image is then we're done.
232 addr = simple_strtoul(argv[1], NULL, 16);
234 #if defined(CONFIG_CMD_NET)
236 * Check to see if we need to tftp the image ourselves
239 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
240 if (net_loop(TFTPGET) <= 0)
242 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
248 * This should equate to
249 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
250 * from the VxWorks BSP header files.
251 * This will vary from board to board
253 #if defined(CONFIG_WALNUT)
254 tmp = (char *)CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
255 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
256 memcpy(tmp, &build_buf[3], 3);
257 #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
258 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
259 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
260 memcpy(tmp, build_buf, 6);
262 puts("## Ethernet MAC address not copied to NV RAM\n");
266 * Use bootaddr to find the location in memory that VxWorks
267 * will look for the bootline string. The default value is
268 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
269 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
271 tmp = getenv("bootaddr");
273 printf("## VxWorks bootline address not specified\n");
275 bootaddr = simple_strtoul(tmp, NULL, 16);
278 * Check to see if the bootline is defined in the 'bootargs'
279 * parameter. If it is not defined, we may be able to
280 * construct the info.
282 bootline = getenv("bootargs");
284 memcpy((void *)bootaddr, bootline,
285 max(strlen(bootline), (size_t)255));
286 flush_cache(bootaddr, max(strlen(bootline),
289 tmp = getenv("bootdev");
291 ptr = sprintf(build_buf, tmp);
293 printf("## VxWorks boot device not specified\n");
295 tmp = getenv("bootfile");
297 ptr += sprintf(build_buf + ptr,
300 ptr += sprintf(build_buf + ptr,
304 * The following parameters are only needed if 'bootdev'
305 * is an ethernet device, otherwise they are optional.
307 tmp = getenv("ipaddr");
309 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
310 tmp = getenv("netmask");
312 u32 mask = getenv_ip("netmask").s_addr;
313 ptr += sprintf(build_buf + ptr,
314 ":%08x ", ntohl(mask));
316 ptr += sprintf(build_buf + ptr, " ");
320 tmp = getenv("serverip");
322 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
324 tmp = getenv("gatewayip");
326 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
328 tmp = getenv("hostname");
330 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
332 tmp = getenv("othbootargs");
334 ptr += sprintf(build_buf + ptr, tmp);
336 memcpy((void *)bootaddr, build_buf,
337 max(strlen(build_buf), (size_t)255));
338 flush_cache(bootaddr, max(strlen(build_buf),
342 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
348 * Since E820 information is critical to the kernel, if we don't
349 * specify these in the environments, use a default one.
351 tmp = getenv("e820data");
353 data = (struct e820entry *)simple_strtoul(tmp, NULL, 16);
355 data = (struct e820entry *)VXWORKS_E820_DATA_ADDR;
356 tmp = getenv("e820info");
358 info = (struct e820info *)simple_strtoul(tmp, NULL, 16);
360 info = (struct e820info *)VXWORKS_E820_INFO_ADDR;
362 memset(info, 0, sizeof(struct e820info));
363 info->sign = E820_SIGNATURE;
364 info->entries = install_e820_map(E820MAX, data);
365 info->addr = (info->entries - 1) * sizeof(struct e820entry) +
366 VXWORKS_E820_DATA_ADDR;
370 * If the data at the load address is an elf image, then
371 * treat it like an elf image. Otherwise, assume that it is a
374 if (valid_elf_image(addr))
375 addr = load_elf_image_shdr(addr);
377 puts("## Not an ELF image, assuming binary\n");
379 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
382 ((void (*)(int))addr)(0);
384 puts("## vxWorks terminated\n");
390 bootelf, 3, 0, do_bootelf,
391 "Boot from an ELF image in memory",
392 "[-p|-s] [address]\n"
393 "\t- load ELF image at [address] via program headers (-p)\n"
394 "\t or via section headers (-s)"
398 bootvx, 2, 0, do_bootvx,
399 "Boot vxWorks from an ELF image",
400 " [address] - load address of vxWorks ELF image."