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 ELF64 loader, assumes the image is valid, returns the
29 * entry point address.
31 * Note if U-Boot is 32-bit, the loader assumes the to segment's
32 * physical address and size is within the lower 32-bit address space.
34 static unsigned long load_elf64_image_phdr(unsigned long addr)
36 Elf64_Ehdr *ehdr; /* Elf header structure pointer */
37 Elf64_Phdr *phdr; /* Program header structure pointer */
40 ehdr = (Elf64_Ehdr *)addr;
41 phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
43 /* Load each program header */
44 for (i = 0; i < ehdr->e_phnum; ++i) {
45 void *dst = (void *)(ulong)phdr->p_paddr;
46 void *src = (void *)addr + phdr->p_offset;
48 debug("Loading phdr %i to 0x%p (%lu bytes)\n",
49 i, dst, (ulong)phdr->p_filesz);
51 memcpy(dst, src, phdr->p_filesz);
52 if (phdr->p_filesz != phdr->p_memsz)
53 memset(dst + phdr->p_filesz, 0x00,
54 phdr->p_memsz - phdr->p_filesz);
55 flush_cache((unsigned long)dst, phdr->p_filesz);
63 * A very simple ELF loader, assumes the image is valid, returns the
64 * entry point address.
66 * The loader firstly reads the EFI class to see if it's a 64-bit image.
67 * If yes, call the ELF64 loader. Otherwise continue with the ELF32 loader.
69 static unsigned long load_elf_image_phdr(unsigned long addr)
71 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
72 Elf32_Phdr *phdr; /* Program header structure pointer */
75 ehdr = (Elf32_Ehdr *)addr;
76 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
77 return load_elf64_image_phdr(addr);
79 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
81 /* Load each program header */
82 for (i = 0; i < ehdr->e_phnum; ++i) {
83 void *dst = (void *)(uintptr_t)phdr->p_paddr;
84 void *src = (void *)addr + phdr->p_offset;
86 debug("Loading phdr %i to 0x%p (%i bytes)\n",
87 i, dst, phdr->p_filesz);
89 memcpy(dst, src, phdr->p_filesz);
90 if (phdr->p_filesz != phdr->p_memsz)
91 memset(dst + phdr->p_filesz, 0x00,
92 phdr->p_memsz - phdr->p_filesz);
93 flush_cache((unsigned long)dst, phdr->p_filesz);
100 static unsigned long load_elf_image_shdr(unsigned long addr)
102 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
103 Elf32_Shdr *shdr; /* Section header structure pointer */
104 unsigned char *strtab = 0; /* String table pointer */
105 unsigned char *image; /* Binary image pointer */
106 int i; /* Loop counter */
108 ehdr = (Elf32_Ehdr *)addr;
110 /* Find the section header string table for output info */
111 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
112 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
114 if (shdr->sh_type == SHT_STRTAB)
115 strtab = (unsigned char *)(addr + shdr->sh_offset);
117 /* Load each appropriate section */
118 for (i = 0; i < ehdr->e_shnum; ++i) {
119 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
120 (i * sizeof(Elf32_Shdr)));
122 if (!(shdr->sh_flags & SHF_ALLOC) ||
123 shdr->sh_addr == 0 || shdr->sh_size == 0) {
128 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
129 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
130 &strtab[shdr->sh_name],
131 (unsigned long)shdr->sh_addr,
132 (long)shdr->sh_size);
135 if (shdr->sh_type == SHT_NOBITS) {
136 memset((void *)(uintptr_t)shdr->sh_addr, 0,
139 image = (unsigned char *)addr + shdr->sh_offset;
140 memcpy((void *)(uintptr_t)shdr->sh_addr,
141 (const void *)image, shdr->sh_size);
143 flush_cache(shdr->sh_addr, shdr->sh_size);
146 return ehdr->e_entry;
149 /* Allow ports to override the default behavior */
150 static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
151 int argc, char * const argv[])
156 * pass address parameter as argv[0] (aka command name),
157 * and all remaining args
159 ret = entry(argc, argv);
165 * Determine if a valid ELF image exists at the given memory location.
166 * First look at the ELF header magic field, then make sure that it is
169 int valid_elf_image(unsigned long addr)
171 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
173 ehdr = (Elf32_Ehdr *)addr;
175 if (!IS_ELF(*ehdr)) {
176 printf("## No elf image at address 0x%08lx\n", addr);
180 if (ehdr->e_type != ET_EXEC) {
181 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
188 /* Interpreter command to boot an arbitrary ELF image from memory */
189 int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
191 unsigned long addr; /* Address of the ELF image */
192 unsigned long rc; /* Return value from user code */
194 const char *ep = env_get("autostart");
197 /* Consume 'bootelf' */
200 /* Check for flag. */
201 if (argc >= 1 && (argv[0][0] == '-' && \
202 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
207 /* Check for address. */
208 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
209 /* Consume address */
214 if (!valid_elf_image(addr))
217 if (sload && sload[1] == 'p')
218 addr = load_elf_image_phdr(addr);
220 addr = load_elf_image_shdr(addr);
222 if (ep && !strcmp(ep, "no"))
225 printf("## Starting application at 0x%08lx ...\n", addr);
228 * pass address parameter as argv[0] (aka command name),
229 * and all remaining args
231 rc = do_bootelf_exec((void *)addr, argc, argv);
235 printf("## Application terminated, rc = 0x%lx\n", rc);
241 * Interpreter command to boot VxWorks from a memory image. The image can
242 * be either an ELF image or a raw binary. Will attempt to setup the
243 * bootline and other parameters correctly.
245 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
247 unsigned long addr; /* Address of image */
248 unsigned long bootaddr; /* Address to put the bootline */
249 char *bootline; /* Text of the bootline */
250 char *tmp; /* Temporary char pointer */
251 char build_buf[128]; /* Buffer for building the bootline */
255 struct e820_info *info;
256 struct e820_entry *data;
260 * Check the loadaddr variable.
261 * If we don't know where the image is then we're done.
266 addr = simple_strtoul(argv[1], NULL, 16);
268 #if defined(CONFIG_CMD_NET)
270 * Check to see if we need to tftp the image ourselves
273 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
274 if (net_loop(TFTPGET) <= 0)
276 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
282 * This should equate to
283 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
284 * from the VxWorks BSP header files.
285 * This will vary from board to board
287 #if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
288 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
289 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
290 memcpy(tmp, build_buf, 6);
292 puts("## Ethernet MAC address not copied to NV RAM\n");
296 * Use bootaddr to find the location in memory that VxWorks
297 * will look for the bootline string. The default value is
298 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
299 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
301 tmp = env_get("bootaddr");
303 printf("## VxWorks bootline address not specified\n");
305 bootaddr = simple_strtoul(tmp, NULL, 16);
308 * Check to see if the bootline is defined in the 'bootargs'
309 * parameter. If it is not defined, we may be able to
310 * construct the info.
312 bootline = env_get("bootargs");
314 memcpy((void *)bootaddr, bootline,
315 max(strlen(bootline), (size_t)255));
316 flush_cache(bootaddr, max(strlen(bootline),
319 tmp = env_get("bootdev");
321 strcpy(build_buf, tmp);
324 printf("## VxWorks boot device not specified\n");
326 tmp = env_get("bootfile");
328 ptr += sprintf(build_buf + ptr,
331 ptr += sprintf(build_buf + ptr,
335 * The following parameters are only needed if 'bootdev'
336 * is an ethernet device, otherwise they are optional.
338 tmp = env_get("ipaddr");
340 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
341 tmp = env_get("netmask");
343 u32 mask = env_get_ip("netmask").s_addr;
344 ptr += sprintf(build_buf + ptr,
345 ":%08x ", ntohl(mask));
347 ptr += sprintf(build_buf + ptr, " ");
351 tmp = env_get("serverip");
353 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
355 tmp = env_get("gatewayip");
357 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
359 tmp = env_get("hostname");
361 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
363 tmp = env_get("othbootargs");
365 strcpy(build_buf + ptr, tmp);
369 memcpy((void *)bootaddr, build_buf,
370 max(strlen(build_buf), (size_t)255));
371 flush_cache(bootaddr, max(strlen(build_buf),
375 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
381 * Get VxWorks's physical memory base address from environment,
382 * if we don't specify it in the environment, use a default one.
384 base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
385 data = (struct e820_entry *)(base + E820_DATA_OFFSET);
386 info = (struct e820_info *)(base + E820_INFO_OFFSET);
388 memset(info, 0, sizeof(struct e820_info));
389 info->sign = E820_SIGNATURE;
390 info->entries = install_e820_map(E820MAX, data);
391 info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
395 * Explicitly clear the bootloader image size otherwise if memory
396 * at this offset happens to contain some garbage data, the final
397 * available memory size for the kernel is insane.
399 *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
403 * If the data at the load address is an elf image, then
404 * treat it like an elf image. Otherwise, assume that it is a
407 if (valid_elf_image(addr))
408 addr = load_elf_image_phdr(addr);
410 puts("## Not an ELF image, assuming binary\n");
412 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
415 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
421 /* VxWorks on x86 uses stack to pass parameters */
422 ((asmlinkage void (*)(int))addr)(0);
424 ((void (*)(int))addr)(0);
427 puts("## vxWorks terminated\n");
433 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
434 "Boot from an ELF image in memory",
435 "[-p|-s] [address]\n"
436 "\t- load ELF image at [address] via program headers (-p)\n"
437 "\t or via section headers (-s)"
441 bootvx, 2, 0, do_bootvx,
442 "Boot vxWorks from an ELF image",
443 " [address] - load address of vxWorks ELF image."