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
19 #include <environment.h>
24 #include <linux/linkage.h>
28 * A very simple elf loader, assumes the image is valid, returns the
29 * entry point address.
31 static unsigned long load_elf_image_phdr(unsigned long addr)
33 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
34 Elf32_Phdr *phdr; /* Program header structure pointer */
37 ehdr = (Elf32_Ehdr *)addr;
38 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
40 /* Load each program header */
41 for (i = 0; i < ehdr->e_phnum; ++i) {
42 void *dst = (void *)(uintptr_t)phdr->p_paddr;
43 void *src = (void *)addr + phdr->p_offset;
44 debug("Loading phdr %i to 0x%p (%i bytes)\n",
45 i, dst, phdr->p_filesz);
47 memcpy(dst, src, phdr->p_filesz);
48 if (phdr->p_filesz != phdr->p_memsz)
49 memset(dst + phdr->p_filesz, 0x00,
50 phdr->p_memsz - phdr->p_filesz);
51 flush_cache((unsigned long)dst, phdr->p_filesz);
58 static unsigned long load_elf_image_shdr(unsigned long addr)
60 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
61 Elf32_Shdr *shdr; /* Section header structure pointer */
62 unsigned char *strtab = 0; /* String table pointer */
63 unsigned char *image; /* Binary image pointer */
64 int i; /* Loop counter */
66 ehdr = (Elf32_Ehdr *)addr;
68 /* Find the section header string table for output info */
69 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
70 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
72 if (shdr->sh_type == SHT_STRTAB)
73 strtab = (unsigned char *)(addr + shdr->sh_offset);
75 /* Load each appropriate section */
76 for (i = 0; i < ehdr->e_shnum; ++i) {
77 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
78 (i * sizeof(Elf32_Shdr)));
80 if (!(shdr->sh_flags & SHF_ALLOC) ||
81 shdr->sh_addr == 0 || shdr->sh_size == 0) {
86 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
87 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
88 &strtab[shdr->sh_name],
89 (unsigned long)shdr->sh_addr,
93 if (shdr->sh_type == SHT_NOBITS) {
94 memset((void *)(uintptr_t)shdr->sh_addr, 0,
97 image = (unsigned char *)addr + shdr->sh_offset;
98 memcpy((void *)(uintptr_t)shdr->sh_addr,
99 (const void *)image, shdr->sh_size);
101 flush_cache(shdr->sh_addr, shdr->sh_size);
104 return ehdr->e_entry;
107 /* Allow ports to override the default behavior */
108 static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
109 int argc, char * const argv[])
114 * pass address parameter as argv[0] (aka command name),
115 * and all remaining args
117 ret = entry(argc, argv);
123 * Determine if a valid ELF image exists at the given memory location.
124 * First look at the ELF header magic field, then make sure that it is
127 int valid_elf_image(unsigned long addr)
129 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
131 ehdr = (Elf32_Ehdr *)addr;
133 if (!IS_ELF(*ehdr)) {
134 printf("## No elf image at address 0x%08lx\n", addr);
138 if (ehdr->e_type != ET_EXEC) {
139 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
146 /* Interpreter command to boot an arbitrary ELF image from memory */
147 int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
149 unsigned long addr; /* Address of the ELF image */
150 unsigned long rc; /* Return value from user code */
152 const char *ep = env_get("autostart");
155 /* Consume 'bootelf' */
158 /* Check for flag. */
159 if (argc >= 1 && (argv[0][0] == '-' && \
160 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
165 /* Check for address. */
166 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
167 /* Consume address */
172 if (!valid_elf_image(addr))
175 if (sload && sload[1] == 'p')
176 addr = load_elf_image_phdr(addr);
178 addr = load_elf_image_shdr(addr);
180 if (ep && !strcmp(ep, "no"))
183 printf("## Starting application at 0x%08lx ...\n", addr);
186 * pass address parameter as argv[0] (aka command name),
187 * and all remaining args
189 rc = do_bootelf_exec((void *)addr, argc, argv);
193 printf("## Application terminated, rc = 0x%lx\n", rc);
199 * Interpreter command to boot VxWorks from a memory image. The image can
200 * be either an ELF image or a raw binary. Will attempt to setup the
201 * bootline and other parameters correctly.
203 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
205 unsigned long addr; /* Address of image */
206 unsigned long bootaddr; /* Address to put the bootline */
207 char *bootline; /* Text of the bootline */
208 char *tmp; /* Temporary char pointer */
209 char build_buf[128]; /* Buffer for building the bootline */
212 struct e820info *info;
213 struct e820entry *data;
217 * Check the loadaddr variable.
218 * If we don't know where the image is then we're done.
223 addr = simple_strtoul(argv[1], NULL, 16);
225 #if defined(CONFIG_CMD_NET)
227 * Check to see if we need to tftp the image ourselves
230 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
231 if (net_loop(TFTPGET) <= 0)
233 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
239 * This should equate to
240 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
241 * from the VxWorks BSP header files.
242 * This will vary from board to board
244 #if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
245 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
246 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
247 memcpy(tmp, build_buf, 6);
249 puts("## Ethernet MAC address not copied to NV RAM\n");
253 * Use bootaddr to find the location in memory that VxWorks
254 * will look for the bootline string. The default value is
255 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
256 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
258 tmp = env_get("bootaddr");
260 printf("## VxWorks bootline address not specified\n");
262 bootaddr = simple_strtoul(tmp, NULL, 16);
265 * Check to see if the bootline is defined in the 'bootargs'
266 * parameter. If it is not defined, we may be able to
267 * construct the info.
269 bootline = env_get("bootargs");
271 memcpy((void *)bootaddr, bootline,
272 max(strlen(bootline), (size_t)255));
273 flush_cache(bootaddr, max(strlen(bootline),
276 tmp = env_get("bootdev");
278 strcpy(build_buf, tmp);
281 printf("## VxWorks boot device not specified\n");
283 tmp = env_get("bootfile");
285 ptr += sprintf(build_buf + ptr,
288 ptr += sprintf(build_buf + ptr,
292 * The following parameters are only needed if 'bootdev'
293 * is an ethernet device, otherwise they are optional.
295 tmp = env_get("ipaddr");
297 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
298 tmp = env_get("netmask");
300 u32 mask = env_get_ip("netmask").s_addr;
301 ptr += sprintf(build_buf + ptr,
302 ":%08x ", ntohl(mask));
304 ptr += sprintf(build_buf + ptr, " ");
308 tmp = env_get("serverip");
310 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
312 tmp = env_get("gatewayip");
314 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
316 tmp = env_get("hostname");
318 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
320 tmp = env_get("othbootargs");
322 strcpy(build_buf + ptr, tmp);
326 memcpy((void *)bootaddr, build_buf,
327 max(strlen(build_buf), (size_t)255));
328 flush_cache(bootaddr, max(strlen(build_buf),
332 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
338 * Since E820 information is critical to the kernel, if we don't
339 * specify these in the environments, use a default one.
341 tmp = env_get("e820data");
343 data = (struct e820entry *)simple_strtoul(tmp, NULL, 16);
345 data = (struct e820entry *)VXWORKS_E820_DATA_ADDR;
346 tmp = env_get("e820info");
348 info = (struct e820info *)simple_strtoul(tmp, NULL, 16);
350 info = (struct e820info *)VXWORKS_E820_INFO_ADDR;
352 memset(info, 0, sizeof(struct e820info));
353 info->sign = E820_SIGNATURE;
354 info->entries = install_e820_map(E820MAX, data);
355 info->addr = (info->entries - 1) * sizeof(struct e820entry) +
356 VXWORKS_E820_DATA_ADDR;
360 * If the data at the load address is an elf image, then
361 * treat it like an elf image. Otherwise, assume that it is a
364 if (valid_elf_image(addr))
365 addr = load_elf_image_phdr(addr);
367 puts("## Not an ELF image, assuming binary\n");
369 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
372 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
378 /* VxWorks on x86 uses stack to pass parameters */
379 ((asmlinkage void (*)(int))addr)(0);
381 ((void (*)(int))addr)(0);
384 puts("## vxWorks terminated\n");
390 bootelf, CONFIG_SYS_MAXARGS, 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."