1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (c) 2001 William L. Pitts
18 #include <linux/linkage.h>
22 * A very simple ELF64 loader, assumes the image is valid, returns the
23 * entry point address.
25 * Note if U-Boot is 32-bit, the loader assumes the to segment's
26 * physical address and size is within the lower 32-bit address space.
28 static unsigned long load_elf64_image_phdr(unsigned long addr)
30 Elf64_Ehdr *ehdr; /* Elf header structure pointer */
31 Elf64_Phdr *phdr; /* Program header structure pointer */
34 ehdr = (Elf64_Ehdr *)addr;
35 phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
37 /* Load each program header */
38 for (i = 0; i < ehdr->e_phnum; ++i) {
39 void *dst = (void *)(ulong)phdr->p_paddr;
40 void *src = (void *)addr + phdr->p_offset;
42 debug("Loading phdr %i to 0x%p (%lu bytes)\n",
43 i, dst, (ulong)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(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
50 roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
54 if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
55 EF_PPC64_ELFV1_ABI)) {
57 * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
58 * descriptor pointer with the first double word being the
59 * address of the entry point of the function.
61 uintptr_t addr = ehdr->e_entry;
63 return *(Elf64_Addr *)addr;
69 static unsigned long load_elf64_image_shdr(unsigned long addr)
71 Elf64_Ehdr *ehdr; /* Elf header structure pointer */
72 Elf64_Shdr *shdr; /* Section header structure pointer */
73 unsigned char *strtab = 0; /* String table pointer */
74 unsigned char *image; /* Binary image pointer */
75 int i; /* Loop counter */
77 ehdr = (Elf64_Ehdr *)addr;
79 /* Find the section header string table for output info */
80 shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff +
81 (ehdr->e_shstrndx * sizeof(Elf64_Shdr)));
83 if (shdr->sh_type == SHT_STRTAB)
84 strtab = (unsigned char *)(addr + (ulong)shdr->sh_offset);
86 /* Load each appropriate section */
87 for (i = 0; i < ehdr->e_shnum; ++i) {
88 shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff +
89 (i * sizeof(Elf64_Shdr)));
91 if (!(shdr->sh_flags & SHF_ALLOC) ||
92 shdr->sh_addr == 0 || shdr->sh_size == 0) {
97 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
98 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
99 &strtab[shdr->sh_name],
100 (unsigned long)shdr->sh_addr,
101 (long)shdr->sh_size);
104 if (shdr->sh_type == SHT_NOBITS) {
105 memset((void *)(uintptr_t)shdr->sh_addr, 0,
108 image = (unsigned char *)addr + (ulong)shdr->sh_offset;
109 memcpy((void *)(uintptr_t)shdr->sh_addr,
110 (const void *)image, shdr->sh_size);
112 flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
113 roundup((shdr->sh_addr + shdr->sh_size),
115 rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
118 if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
119 EF_PPC64_ELFV1_ABI)) {
121 * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
122 * descriptor pointer with the first double word being the
123 * address of the entry point of the function.
125 uintptr_t addr = ehdr->e_entry;
127 return *(Elf64_Addr *)addr;
130 return ehdr->e_entry;
134 * A very simple ELF loader, assumes the image is valid, returns the
135 * entry point address.
137 * The loader firstly reads the EFI class to see if it's a 64-bit image.
138 * If yes, call the ELF64 loader. Otherwise continue with the ELF32 loader.
140 static unsigned long load_elf_image_phdr(unsigned long addr)
142 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
143 Elf32_Phdr *phdr; /* Program header structure pointer */
146 ehdr = (Elf32_Ehdr *)addr;
147 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
148 return load_elf64_image_phdr(addr);
150 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
152 /* Load each program header */
153 for (i = 0; i < ehdr->e_phnum; ++i) {
154 void *dst = (void *)(uintptr_t)phdr->p_paddr;
155 void *src = (void *)addr + phdr->p_offset;
157 debug("Loading phdr %i to 0x%p (%i bytes)\n",
158 i, dst, phdr->p_filesz);
160 memcpy(dst, src, phdr->p_filesz);
161 if (phdr->p_filesz != phdr->p_memsz)
162 memset(dst + phdr->p_filesz, 0x00,
163 phdr->p_memsz - phdr->p_filesz);
164 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
165 roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
169 return ehdr->e_entry;
172 static unsigned long load_elf_image_shdr(unsigned long addr)
174 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
175 Elf32_Shdr *shdr; /* Section header structure pointer */
176 unsigned char *strtab = 0; /* String table pointer */
177 unsigned char *image; /* Binary image pointer */
178 int i; /* Loop counter */
180 ehdr = (Elf32_Ehdr *)addr;
181 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
182 return load_elf64_image_shdr(addr);
184 /* Find the section header string table for output info */
185 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
186 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
188 if (shdr->sh_type == SHT_STRTAB)
189 strtab = (unsigned char *)(addr + shdr->sh_offset);
191 /* Load each appropriate section */
192 for (i = 0; i < ehdr->e_shnum; ++i) {
193 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
194 (i * sizeof(Elf32_Shdr)));
196 if (!(shdr->sh_flags & SHF_ALLOC) ||
197 shdr->sh_addr == 0 || shdr->sh_size == 0) {
202 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
203 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
204 &strtab[shdr->sh_name],
205 (unsigned long)shdr->sh_addr,
206 (long)shdr->sh_size);
209 if (shdr->sh_type == SHT_NOBITS) {
210 memset((void *)(uintptr_t)shdr->sh_addr, 0,
213 image = (unsigned char *)addr + shdr->sh_offset;
214 memcpy((void *)(uintptr_t)shdr->sh_addr,
215 (const void *)image, shdr->sh_size);
217 flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
218 roundup((shdr->sh_addr + shdr->sh_size),
220 rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
223 return ehdr->e_entry;
226 /* Allow ports to override the default behavior */
227 static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
228 int argc, char * const argv[])
233 * pass address parameter as argv[0] (aka command name),
234 * and all remaining args
236 ret = entry(argc, argv);
242 * Determine if a valid ELF image exists at the given memory location.
243 * First look at the ELF header magic field, then make sure that it is
246 int valid_elf_image(unsigned long addr)
248 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
250 ehdr = (Elf32_Ehdr *)addr;
252 if (!IS_ELF(*ehdr)) {
253 printf("## No elf image at address 0x%08lx\n", addr);
257 if (ehdr->e_type != ET_EXEC) {
258 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
265 /* Interpreter command to boot an arbitrary ELF image from memory */
266 int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
268 unsigned long addr; /* Address of the ELF image */
269 unsigned long rc; /* Return value from user code */
271 const char *ep = env_get("autostart");
274 /* Consume 'bootelf' */
277 /* Check for flag. */
278 if (argc >= 1 && (argv[0][0] == '-' && \
279 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
284 /* Check for address. */
285 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
286 /* Consume address */
289 addr = image_load_addr;
291 if (!valid_elf_image(addr))
294 if (sload && sload[1] == 'p')
295 addr = load_elf_image_phdr(addr);
297 addr = load_elf_image_shdr(addr);
299 if (ep && !strcmp(ep, "no"))
302 printf("## Starting application at 0x%08lx ...\n", addr);
305 * pass address parameter as argv[0] (aka command name),
306 * and all remaining args
308 rc = do_bootelf_exec((void *)addr, argc, argv);
312 printf("## Application terminated, rc = 0x%lx\n", rc);
318 * Interpreter command to boot VxWorks from a memory image. The image can
319 * be either an ELF image or a raw binary. Will attempt to setup the
320 * bootline and other parameters correctly.
322 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
324 unsigned long addr; /* Address of image */
325 unsigned long bootaddr = 0; /* Address to put the bootline */
326 char *bootline; /* Text of the bootline */
327 char *tmp; /* Temporary char pointer */
328 char build_buf[128]; /* Buffer for building the bootline */
332 struct e820_info *info;
333 struct e820_entry *data;
334 struct efi_gop_info *gop;
335 struct vesa_mode_info *vesa = &mode_info.vesa;
339 * Check the loadaddr variable.
340 * If we don't know where the image is then we're done.
343 addr = image_load_addr;
345 addr = simple_strtoul(argv[1], NULL, 16);
347 #if defined(CONFIG_CMD_NET)
349 * Check to see if we need to tftp the image ourselves
352 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
353 if (net_loop(TFTPGET) <= 0)
355 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
361 * This should equate to
362 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
363 * from the VxWorks BSP header files.
364 * This will vary from board to board
366 #if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
367 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
368 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
369 memcpy(tmp, build_buf, 6);
371 puts("## Ethernet MAC address not copied to NV RAM\n");
376 * Get VxWorks's physical memory base address from environment,
377 * if we don't specify it in the environment, use a default one.
379 base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
380 data = (struct e820_entry *)(base + E820_DATA_OFFSET);
381 info = (struct e820_info *)(base + E820_INFO_OFFSET);
383 memset(info, 0, sizeof(struct e820_info));
384 info->sign = E820_SIGNATURE;
385 info->entries = install_e820_map(E820MAX, data);
386 info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
390 * Explicitly clear the bootloader image size otherwise if memory
391 * at this offset happens to contain some garbage data, the final
392 * available memory size for the kernel is insane.
394 *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
397 * Prepare compatible framebuffer information block.
398 * The VESA mode has to be 32-bit RGBA.
400 if (vesa->x_resolution && vesa->y_resolution) {
401 gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET);
402 gop->magic = EFI_GOP_INFO_MAGIC;
403 gop->info.version = 0;
404 gop->info.width = vesa->x_resolution;
405 gop->info.height = vesa->y_resolution;
406 gop->info.pixel_format = EFI_GOT_RGBA8;
407 gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4;
408 gop->fb_base = vesa->phys_base_ptr;
409 gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution;
414 * Use bootaddr to find the location in memory that VxWorks
415 * will look for the bootline string. The default value is
416 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
417 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
419 tmp = env_get("bootaddr");
422 bootaddr = base + X86_BOOT_LINE_OFFSET;
424 printf("## VxWorks bootline address not specified\n");
430 bootaddr = simple_strtoul(tmp, NULL, 16);
433 * Check to see if the bootline is defined in the 'bootargs' parameter.
434 * If it is not defined, we may be able to construct the info.
436 bootline = env_get("bootargs");
438 tmp = env_get("bootdev");
440 strcpy(build_buf, tmp);
443 printf("## VxWorks boot device not specified\n");
446 tmp = env_get("bootfile");
448 ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
450 ptr += sprintf(build_buf + ptr, "host:vxWorks ");
453 * The following parameters are only needed if 'bootdev'
454 * is an ethernet device, otherwise they are optional.
456 tmp = env_get("ipaddr");
458 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
459 tmp = env_get("netmask");
461 u32 mask = env_get_ip("netmask").s_addr;
462 ptr += sprintf(build_buf + ptr,
463 ":%08x ", ntohl(mask));
465 ptr += sprintf(build_buf + ptr, " ");
469 tmp = env_get("serverip");
471 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
473 tmp = env_get("gatewayip");
475 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
477 tmp = env_get("hostname");
479 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
481 tmp = env_get("othbootargs");
483 strcpy(build_buf + ptr, tmp);
487 bootline = build_buf;
490 memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255));
491 flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
492 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr);
495 * If the data at the load address is an elf image, then
496 * treat it like an elf image. Otherwise, assume that it is a
499 if (valid_elf_image(addr))
500 addr = load_elf_image_phdr(addr);
502 puts("## Not an ELF image, assuming binary\n");
504 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
507 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
513 /* VxWorks on x86 uses stack to pass parameters */
514 ((asmlinkage void (*)(int))addr)(0);
516 ((void (*)(int))addr)(0);
519 puts("## vxWorks terminated\n");
525 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
526 "Boot from an ELF image in memory",
527 "[-p|-s] [address]\n"
528 "\t- load ELF image at [address] via program headers (-p)\n"
529 "\t or via section headers (-s)"
533 bootvx, 2, 0, do_bootvx,
534 "Boot vxWorks from an ELF image",
535 " [address] - load address of vxWorks ELF image."