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
22 static unsigned long load_elf_image_phdr(unsigned long addr);
23 static unsigned long load_elf_image_shdr(unsigned long addr);
25 /* Allow ports to override the default behavior */
26 static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
27 int argc, char * const argv[])
32 * QNX images require the data cache is disabled.
33 * Data cache is already flushed, so just turn it off.
35 int dcache = dcache_status();
40 * pass address parameter as argv[0] (aka command name),
41 * and all remaining args
43 ret = entry(argc, argv);
52 * Determine if a valid ELF image exists at the given memory location.
53 * First look at the ELF header magic field, then make sure that it is
56 int valid_elf_image(unsigned long addr)
58 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
60 ehdr = (Elf32_Ehdr *)addr;
63 printf("## No elf image at address 0x%08lx\n", addr);
67 if (ehdr->e_type != ET_EXEC) {
68 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
75 /* Interpreter command to boot an arbitrary ELF image from memory */
76 int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
78 unsigned long addr; /* Address of the ELF image */
79 unsigned long rc; /* Return value from user code */
81 const char *ep = getenv("autostart");
89 } else if (argc == 2) {
90 if (argv[1][0] == '-')
97 addr = simple_strtoul(saddr, NULL, 16);
101 if (!valid_elf_image(addr))
104 if (sload && sload[1] == 'p')
105 addr = load_elf_image_phdr(addr);
107 addr = load_elf_image_shdr(addr);
109 if (ep && !strcmp(ep, "no"))
112 printf("## Starting application at 0x%08lx ...\n", addr);
115 * pass address parameter as argv[0] (aka command name),
116 * and all remaining args
118 rc = do_bootelf_exec((void *)addr, argc - 1, argv + 1);
122 printf("## Application terminated, rc = 0x%lx\n", rc);
128 * Interpreter command to boot VxWorks from a memory image. The image can
129 * be either an ELF image or a raw binary. Will attempt to setup the
130 * bootline and other parameters correctly.
132 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
134 unsigned long addr; /* Address of image */
135 unsigned long bootaddr; /* Address to put the bootline */
136 char *bootline; /* Text of the bootline */
137 char *tmp; /* Temporary char pointer */
138 char build_buf[128]; /* Buffer for building the bootline */
141 * Check the loadaddr variable.
142 * If we don't know where the image is then we're done.
147 addr = simple_strtoul(argv[1], NULL, 16);
149 #if defined(CONFIG_CMD_NET)
151 * Check to see if we need to tftp the image ourselves
154 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
155 if (net_loop(TFTPGET) <= 0)
157 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
163 * This should equate to
164 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
165 * from the VxWorks BSP header files.
166 * This will vary from board to board
168 #if defined(CONFIG_WALNUT)
169 tmp = (char *)CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
170 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
171 memcpy(tmp, &build_buf[3], 3);
172 #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
173 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
174 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
175 memcpy(tmp, build_buf, 6);
177 puts("## Ethernet MAC address not copied to NV RAM\n");
181 * Use bootaddr to find the location in memory that VxWorks
182 * will look for the bootline string. The default value for
183 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
184 * defaults to 0x4200.
186 tmp = getenv("bootaddr");
188 bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR;
190 bootaddr = simple_strtoul(tmp, NULL, 16);
193 * Check to see if the bootline is defined in the 'bootargs'
194 * parameter. If it is not defined, we may be able to
195 * construct the info.
197 bootline = getenv("bootargs");
199 memcpy((void *)bootaddr, bootline,
200 max(strlen(bootline), (size_t)255));
201 flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
203 sprintf(build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE);
204 tmp = getenv("bootfile");
206 sprintf(&build_buf[strlen(build_buf)],
207 "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp);
209 sprintf(&build_buf[strlen(build_buf)],
210 "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME);
212 tmp = getenv("ipaddr");
214 sprintf(&build_buf[strlen(build_buf)], "e=%s ", tmp);
216 tmp = getenv("serverip");
218 sprintf(&build_buf[strlen(build_buf)], "h=%s ", tmp);
220 tmp = getenv("hostname");
222 sprintf(&build_buf[strlen(build_buf)], "tn=%s ", tmp);
224 #ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
225 sprintf(&build_buf[strlen(build_buf)],
226 CONFIG_SYS_VXWORKS_ADD_PARAMS);
229 memcpy((void *)bootaddr, build_buf,
230 max(strlen(build_buf), (size_t)255));
231 flush_cache(bootaddr, max(strlen(build_buf), (size_t)255));
235 * If the data at the load address is an elf image, then
236 * treat it like an elf image. Otherwise, assume that it is a
239 if (valid_elf_image(addr))
240 addr = load_elf_image_shdr(addr);
242 puts("## Not an ELF image, assuming binary\n");
244 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
246 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
249 ((void (*)(int))addr)(0);
251 puts("## vxWorks terminated\n");
257 * A very simple elf loader, assumes the image is valid, returns the
258 * entry point address.
260 static unsigned long load_elf_image_phdr(unsigned long addr)
262 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
263 Elf32_Phdr *phdr; /* Program header structure pointer */
266 ehdr = (Elf32_Ehdr *)addr;
267 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
269 /* Load each program header */
270 for (i = 0; i < ehdr->e_phnum; ++i) {
271 void *dst = (void *)(uintptr_t)phdr->p_paddr;
272 void *src = (void *)addr + phdr->p_offset;
273 debug("Loading phdr %i to 0x%p (%i bytes)\n",
274 i, dst, phdr->p_filesz);
276 memcpy(dst, src, phdr->p_filesz);
277 if (phdr->p_filesz != phdr->p_memsz)
278 memset(dst + phdr->p_filesz, 0x00,
279 phdr->p_memsz - phdr->p_filesz);
280 flush_cache((unsigned long)dst, phdr->p_filesz);
284 return ehdr->e_entry;
287 static unsigned long load_elf_image_shdr(unsigned long addr)
289 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
290 Elf32_Shdr *shdr; /* Section header structure pointer */
291 unsigned char *strtab = 0; /* String table pointer */
292 unsigned char *image; /* Binary image pointer */
293 int i; /* Loop counter */
295 ehdr = (Elf32_Ehdr *)addr;
297 /* Find the section header string table for output info */
298 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
299 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
301 if (shdr->sh_type == SHT_STRTAB)
302 strtab = (unsigned char *)(addr + shdr->sh_offset);
304 /* Load each appropriate section */
305 for (i = 0; i < ehdr->e_shnum; ++i) {
306 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
307 (i * sizeof(Elf32_Shdr)));
309 if (!(shdr->sh_flags & SHF_ALLOC) ||
310 shdr->sh_addr == 0 || shdr->sh_size == 0) {
315 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
316 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
317 &strtab[shdr->sh_name],
318 (unsigned long)shdr->sh_addr,
319 (long)shdr->sh_size);
322 if (shdr->sh_type == SHT_NOBITS) {
323 memset((void *)(uintptr_t)shdr->sh_addr, 0,
326 image = (unsigned char *)addr + shdr->sh_offset;
327 memcpy((void *)(uintptr_t)shdr->sh_addr,
328 (const void *)image, shdr->sh_size);
330 flush_cache(shdr->sh_addr, shdr->sh_size);
333 return ehdr->e_entry;
337 bootelf, 3, 0, do_bootelf,
338 "Boot from an ELF image in memory",
339 "[-p|-s] [address]\n"
340 "\t- load ELF image at [address] via program headers (-p)\n"
341 "\t or via section headers (-s)"
345 bootvx, 2, 0, do_bootvx,
346 "Boot vxWorks from an ELF image",
347 " [address] - load address of vxWorks ELF image."