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 #include <linux/linkage.h>
30 * A very simple ELF64 loader, assumes the image is valid, returns the
31 * entry point address.
33 * Note if U-Boot is 32-bit, the loader assumes the to segment's
34 * physical address and size is within the lower 32-bit address space.
36 static unsigned long load_elf64_image_phdr(unsigned long addr)
38 Elf64_Ehdr *ehdr; /* Elf header structure pointer */
39 Elf64_Phdr *phdr; /* Program header structure pointer */
42 ehdr = (Elf64_Ehdr *)addr;
43 phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
45 /* Load each program header */
46 for (i = 0; i < ehdr->e_phnum; ++i) {
47 void *dst = (void *)(ulong)phdr->p_paddr;
48 void *src = (void *)addr + phdr->p_offset;
50 debug("Loading phdr %i to 0x%p (%lu bytes)\n",
51 i, dst, (ulong)phdr->p_filesz);
53 memcpy(dst, src, phdr->p_filesz);
54 if (phdr->p_filesz != phdr->p_memsz)
55 memset(dst + phdr->p_filesz, 0x00,
56 phdr->p_memsz - phdr->p_filesz);
57 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
58 roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
62 if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
63 EF_PPC64_ELFV1_ABI)) {
65 * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
66 * descriptor pointer with the first double word being the
67 * address of the entry point of the function.
69 uintptr_t addr = ehdr->e_entry;
71 return *(Elf64_Addr *)addr;
77 static unsigned long load_elf64_image_shdr(unsigned long addr)
79 Elf64_Ehdr *ehdr; /* Elf header structure pointer */
80 Elf64_Shdr *shdr; /* Section header structure pointer */
81 unsigned char *strtab = 0; /* String table pointer */
82 unsigned char *image; /* Binary image pointer */
83 int i; /* Loop counter */
85 ehdr = (Elf64_Ehdr *)addr;
87 /* Find the section header string table for output info */
88 shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff +
89 (ehdr->e_shstrndx * sizeof(Elf64_Shdr)));
91 if (shdr->sh_type == SHT_STRTAB)
92 strtab = (unsigned char *)(addr + (ulong)shdr->sh_offset);
94 /* Load each appropriate section */
95 for (i = 0; i < ehdr->e_shnum; ++i) {
96 shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff +
97 (i * sizeof(Elf64_Shdr)));
99 if (!(shdr->sh_flags & SHF_ALLOC) ||
100 shdr->sh_addr == 0 || shdr->sh_size == 0) {
105 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
106 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
107 &strtab[shdr->sh_name],
108 (unsigned long)shdr->sh_addr,
109 (long)shdr->sh_size);
112 if (shdr->sh_type == SHT_NOBITS) {
113 memset((void *)(uintptr_t)shdr->sh_addr, 0,
116 image = (unsigned char *)addr + (ulong)shdr->sh_offset;
117 memcpy((void *)(uintptr_t)shdr->sh_addr,
118 (const void *)image, shdr->sh_size);
120 flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
121 roundup((shdr->sh_addr + shdr->sh_size),
123 rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
126 if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
127 EF_PPC64_ELFV1_ABI)) {
129 * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
130 * descriptor pointer with the first double word being the
131 * address of the entry point of the function.
133 uintptr_t addr = ehdr->e_entry;
135 return *(Elf64_Addr *)addr;
138 return ehdr->e_entry;
142 * A very simple ELF loader, assumes the image is valid, returns the
143 * entry point address.
145 * The loader firstly reads the EFI class to see if it's a 64-bit image.
146 * If yes, call the ELF64 loader. Otherwise continue with the ELF32 loader.
148 static unsigned long load_elf_image_phdr(unsigned long addr)
150 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
151 Elf32_Phdr *phdr; /* Program header structure pointer */
154 ehdr = (Elf32_Ehdr *)addr;
155 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
156 return load_elf64_image_phdr(addr);
158 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
160 /* Load each program header */
161 for (i = 0; i < ehdr->e_phnum; ++i) {
162 void *dst = (void *)(uintptr_t)phdr->p_paddr;
163 void *src = (void *)addr + phdr->p_offset;
165 debug("Loading phdr %i to 0x%p (%i bytes)\n",
166 i, dst, phdr->p_filesz);
168 memcpy(dst, src, phdr->p_filesz);
169 if (phdr->p_filesz != phdr->p_memsz)
170 memset(dst + phdr->p_filesz, 0x00,
171 phdr->p_memsz - phdr->p_filesz);
172 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
173 roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
177 return ehdr->e_entry;
180 static unsigned long load_elf_image_shdr(unsigned long addr)
182 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
183 Elf32_Shdr *shdr; /* Section header structure pointer */
184 unsigned char *strtab = 0; /* String table pointer */
185 unsigned char *image; /* Binary image pointer */
186 int i; /* Loop counter */
188 ehdr = (Elf32_Ehdr *)addr;
189 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
190 return load_elf64_image_shdr(addr);
192 /* Find the section header string table for output info */
193 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
194 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
196 if (shdr->sh_type == SHT_STRTAB)
197 strtab = (unsigned char *)(addr + shdr->sh_offset);
199 /* Load each appropriate section */
200 for (i = 0; i < ehdr->e_shnum; ++i) {
201 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
202 (i * sizeof(Elf32_Shdr)));
204 if (!(shdr->sh_flags & SHF_ALLOC) ||
205 shdr->sh_addr == 0 || shdr->sh_size == 0) {
210 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
211 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
212 &strtab[shdr->sh_name],
213 (unsigned long)shdr->sh_addr,
214 (long)shdr->sh_size);
217 if (shdr->sh_type == SHT_NOBITS) {
218 memset((void *)(uintptr_t)shdr->sh_addr, 0,
221 image = (unsigned char *)addr + shdr->sh_offset;
222 memcpy((void *)(uintptr_t)shdr->sh_addr,
223 (const void *)image, shdr->sh_size);
225 flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
226 roundup((shdr->sh_addr + shdr->sh_size),
228 rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
231 return ehdr->e_entry;
234 /* Allow ports to override the default behavior */
235 static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
236 int argc, char * const argv[])
241 * pass address parameter as argv[0] (aka command name),
242 * and all remaining args
244 ret = entry(argc, argv);
250 * Determine if a valid ELF image exists at the given memory location.
251 * First look at the ELF header magic field, then make sure that it is
254 int valid_elf_image(unsigned long addr)
256 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
258 ehdr = (Elf32_Ehdr *)addr;
260 if (!IS_ELF(*ehdr)) {
261 printf("## No elf image at address 0x%08lx\n", addr);
265 if (ehdr->e_type != ET_EXEC) {
266 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
273 /* Interpreter command to boot an arbitrary ELF image from memory */
274 int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
276 unsigned long addr; /* Address of the ELF image */
277 unsigned long rc; /* Return value from user code */
279 const char *ep = env_get("autostart");
282 /* Consume 'bootelf' */
285 /* Check for flag. */
286 if (argc >= 1 && (argv[0][0] == '-' && \
287 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
292 /* Check for address. */
293 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
294 /* Consume address */
297 addr = image_load_addr;
299 if (!valid_elf_image(addr))
302 if (sload && sload[1] == 'p')
303 addr = load_elf_image_phdr(addr);
305 addr = load_elf_image_shdr(addr);
307 if (ep && !strcmp(ep, "no"))
310 printf("## Starting application at 0x%08lx ...\n", addr);
313 * pass address parameter as argv[0] (aka command name),
314 * and all remaining args
316 rc = do_bootelf_exec((void *)addr, argc, argv);
320 printf("## Application terminated, rc = 0x%lx\n", rc);
326 * Interpreter command to boot VxWorks from a memory image. The image can
327 * be either an ELF image or a raw binary. Will attempt to setup the
328 * bootline and other parameters correctly.
330 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
332 unsigned long addr; /* Address of image */
333 unsigned long bootaddr = 0; /* Address to put the bootline */
334 char *bootline; /* Text of the bootline */
335 char *tmp; /* Temporary char pointer */
336 char build_buf[128]; /* Buffer for building the bootline */
340 struct e820_info *info;
341 struct e820_entry *data;
342 struct efi_gop_info *gop;
343 struct vesa_mode_info *vesa = &mode_info.vesa;
347 * Check the loadaddr variable.
348 * If we don't know where the image is then we're done.
351 addr = image_load_addr;
353 addr = simple_strtoul(argv[1], NULL, 16);
355 #if defined(CONFIG_CMD_NET)
357 * Check to see if we need to tftp the image ourselves
360 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
361 if (net_loop(TFTPGET) <= 0)
363 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
369 * This should equate to
370 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
371 * from the VxWorks BSP header files.
372 * This will vary from board to board
374 #if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
375 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
376 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
377 memcpy(tmp, build_buf, 6);
379 puts("## Ethernet MAC address not copied to NV RAM\n");
384 * Get VxWorks's physical memory base address from environment,
385 * if we don't specify it in the environment, use a default one.
387 base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
388 data = (struct e820_entry *)(base + E820_DATA_OFFSET);
389 info = (struct e820_info *)(base + E820_INFO_OFFSET);
391 memset(info, 0, sizeof(struct e820_info));
392 info->sign = E820_SIGNATURE;
393 info->entries = install_e820_map(E820MAX, data);
394 info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
398 * Explicitly clear the bootloader image size otherwise if memory
399 * at this offset happens to contain some garbage data, the final
400 * available memory size for the kernel is insane.
402 *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
405 * Prepare compatible framebuffer information block.
406 * The VESA mode has to be 32-bit RGBA.
408 if (vesa->x_resolution && vesa->y_resolution) {
409 gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET);
410 gop->magic = EFI_GOP_INFO_MAGIC;
411 gop->info.version = 0;
412 gop->info.width = vesa->x_resolution;
413 gop->info.height = vesa->y_resolution;
414 gop->info.pixel_format = EFI_GOT_RGBA8;
415 gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4;
416 gop->fb_base = vesa->phys_base_ptr;
417 gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution;
422 * Use bootaddr to find the location in memory that VxWorks
423 * will look for the bootline string. The default value is
424 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
425 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
427 tmp = env_get("bootaddr");
430 bootaddr = base + X86_BOOT_LINE_OFFSET;
432 printf("## VxWorks bootline address not specified\n");
438 bootaddr = simple_strtoul(tmp, NULL, 16);
441 * Check to see if the bootline is defined in the 'bootargs' parameter.
442 * If it is not defined, we may be able to construct the info.
444 bootline = env_get("bootargs");
446 tmp = env_get("bootdev");
448 strcpy(build_buf, tmp);
451 printf("## VxWorks boot device not specified\n");
454 tmp = env_get("bootfile");
456 ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
458 ptr += sprintf(build_buf + ptr, "host:vxWorks ");
461 * The following parameters are only needed if 'bootdev'
462 * is an ethernet device, otherwise they are optional.
464 tmp = env_get("ipaddr");
466 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
467 tmp = env_get("netmask");
469 u32 mask = env_get_ip("netmask").s_addr;
470 ptr += sprintf(build_buf + ptr,
471 ":%08x ", ntohl(mask));
473 ptr += sprintf(build_buf + ptr, " ");
477 tmp = env_get("serverip");
479 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
481 tmp = env_get("gatewayip");
483 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
485 tmp = env_get("hostname");
487 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
489 tmp = env_get("othbootargs");
491 strcpy(build_buf + ptr, tmp);
495 bootline = build_buf;
498 memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255));
499 flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
500 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr);
503 * If the data at the load address is an elf image, then
504 * treat it like an elf image. Otherwise, assume that it is a
507 if (valid_elf_image(addr))
508 addr = load_elf_image_phdr(addr);
510 puts("## Not an ELF image, assuming binary\n");
512 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
515 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
521 /* VxWorks on x86 uses stack to pass parameters */
522 ((asmlinkage void (*)(int))addr)(0);
524 ((void (*)(int))addr)(0);
527 puts("## vxWorks terminated\n");
533 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
534 "Boot from an ELF image in memory",
535 "[-p|-s] [address]\n"
536 "\t- load ELF image at [address] via program headers (-p)\n"
537 "\t or via section headers (-s)"
541 bootvx, 2, 0, do_bootvx,
542 "Boot vxWorks from an ELF image",
543 " [address] - load address of vxWorks ELF image."