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
27 #include <linux/linkage.h>
31 * A very simple ELF64 loader, assumes the image is valid, returns the
32 * entry point address.
34 * Note if U-Boot is 32-bit, the loader assumes the to segment's
35 * physical address and size is within the lower 32-bit address space.
37 static unsigned long load_elf64_image_phdr(unsigned long addr)
39 Elf64_Ehdr *ehdr; /* Elf header structure pointer */
40 Elf64_Phdr *phdr; /* Program header structure pointer */
43 ehdr = (Elf64_Ehdr *)addr;
44 phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
46 /* Load each program header */
47 for (i = 0; i < ehdr->e_phnum; ++i) {
48 void *dst = (void *)(ulong)phdr->p_paddr;
49 void *src = (void *)addr + phdr->p_offset;
51 debug("Loading phdr %i to 0x%p (%lu bytes)\n",
52 i, dst, (ulong)phdr->p_filesz);
54 memcpy(dst, src, phdr->p_filesz);
55 if (phdr->p_filesz != phdr->p_memsz)
56 memset(dst + phdr->p_filesz, 0x00,
57 phdr->p_memsz - phdr->p_filesz);
58 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
59 roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
63 if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
64 EF_PPC64_ELFV1_ABI)) {
66 * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
67 * descriptor pointer with the first double word being the
68 * address of the entry point of the function.
70 uintptr_t addr = ehdr->e_entry;
72 return *(Elf64_Addr *)addr;
78 static unsigned long load_elf64_image_shdr(unsigned long addr)
80 Elf64_Ehdr *ehdr; /* Elf header structure pointer */
81 Elf64_Shdr *shdr; /* Section header structure pointer */
82 unsigned char *strtab = 0; /* String table pointer */
83 unsigned char *image; /* Binary image pointer */
84 int i; /* Loop counter */
86 ehdr = (Elf64_Ehdr *)addr;
88 /* Find the section header string table for output info */
89 shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff +
90 (ehdr->e_shstrndx * sizeof(Elf64_Shdr)));
92 if (shdr->sh_type == SHT_STRTAB)
93 strtab = (unsigned char *)(addr + (ulong)shdr->sh_offset);
95 /* Load each appropriate section */
96 for (i = 0; i < ehdr->e_shnum; ++i) {
97 shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff +
98 (i * sizeof(Elf64_Shdr)));
100 if (!(shdr->sh_flags & SHF_ALLOC) ||
101 shdr->sh_addr == 0 || shdr->sh_size == 0) {
106 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
107 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
108 &strtab[shdr->sh_name],
109 (unsigned long)shdr->sh_addr,
110 (long)shdr->sh_size);
113 if (shdr->sh_type == SHT_NOBITS) {
114 memset((void *)(uintptr_t)shdr->sh_addr, 0,
117 image = (unsigned char *)addr + (ulong)shdr->sh_offset;
118 memcpy((void *)(uintptr_t)shdr->sh_addr,
119 (const void *)image, shdr->sh_size);
121 flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
122 roundup((shdr->sh_addr + shdr->sh_size),
124 rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
127 if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
128 EF_PPC64_ELFV1_ABI)) {
130 * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
131 * descriptor pointer with the first double word being the
132 * address of the entry point of the function.
134 uintptr_t addr = ehdr->e_entry;
136 return *(Elf64_Addr *)addr;
139 return ehdr->e_entry;
143 * A very simple ELF loader, assumes the image is valid, returns the
144 * entry point address.
146 * The loader firstly reads the EFI class to see if it's a 64-bit image.
147 * If yes, call the ELF64 loader. Otherwise continue with the ELF32 loader.
149 static unsigned long load_elf_image_phdr(unsigned long addr)
151 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
152 Elf32_Phdr *phdr; /* Program header structure pointer */
155 ehdr = (Elf32_Ehdr *)addr;
156 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
157 return load_elf64_image_phdr(addr);
159 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
161 /* Load each program header */
162 for (i = 0; i < ehdr->e_phnum; ++i) {
163 void *dst = (void *)(uintptr_t)phdr->p_paddr;
164 void *src = (void *)addr + phdr->p_offset;
166 debug("Loading phdr %i to 0x%p (%i bytes)\n",
167 i, dst, phdr->p_filesz);
169 memcpy(dst, src, phdr->p_filesz);
170 if (phdr->p_filesz != phdr->p_memsz)
171 memset(dst + phdr->p_filesz, 0x00,
172 phdr->p_memsz - phdr->p_filesz);
173 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
174 roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
178 return ehdr->e_entry;
181 static unsigned long load_elf_image_shdr(unsigned long addr)
183 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
184 Elf32_Shdr *shdr; /* Section header structure pointer */
185 unsigned char *strtab = 0; /* String table pointer */
186 unsigned char *image; /* Binary image pointer */
187 int i; /* Loop counter */
189 ehdr = (Elf32_Ehdr *)addr;
190 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
191 return load_elf64_image_shdr(addr);
193 /* Find the section header string table for output info */
194 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
195 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
197 if (shdr->sh_type == SHT_STRTAB)
198 strtab = (unsigned char *)(addr + shdr->sh_offset);
200 /* Load each appropriate section */
201 for (i = 0; i < ehdr->e_shnum; ++i) {
202 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
203 (i * sizeof(Elf32_Shdr)));
205 if (!(shdr->sh_flags & SHF_ALLOC) ||
206 shdr->sh_addr == 0 || shdr->sh_size == 0) {
211 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
212 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
213 &strtab[shdr->sh_name],
214 (unsigned long)shdr->sh_addr,
215 (long)shdr->sh_size);
218 if (shdr->sh_type == SHT_NOBITS) {
219 memset((void *)(uintptr_t)shdr->sh_addr, 0,
222 image = (unsigned char *)addr + shdr->sh_offset;
223 memcpy((void *)(uintptr_t)shdr->sh_addr,
224 (const void *)image, shdr->sh_size);
226 flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
227 roundup((shdr->sh_addr + shdr->sh_size),
229 rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
232 return ehdr->e_entry;
235 /* Allow ports to override the default behavior */
236 static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
237 int argc, char * const argv[])
242 * pass address parameter as argv[0] (aka command name),
243 * and all remaining args
245 ret = entry(argc, argv);
251 * Determine if a valid ELF image exists at the given memory location.
252 * First look at the ELF header magic field, then make sure that it is
255 int valid_elf_image(unsigned long addr)
257 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
259 ehdr = (Elf32_Ehdr *)addr;
261 if (!IS_ELF(*ehdr)) {
262 printf("## No elf image at address 0x%08lx\n", addr);
266 if (ehdr->e_type != ET_EXEC) {
267 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
274 /* Interpreter command to boot an arbitrary ELF image from memory */
275 int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
277 unsigned long addr; /* Address of the ELF image */
278 unsigned long rc; /* Return value from user code */
280 const char *ep = env_get("autostart");
283 /* Consume 'bootelf' */
286 /* Check for flag. */
287 if (argc >= 1 && (argv[0][0] == '-' && \
288 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
293 /* Check for address. */
294 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
295 /* Consume address */
298 addr = image_load_addr;
300 if (!valid_elf_image(addr))
303 if (sload && sload[1] == 'p')
304 addr = load_elf_image_phdr(addr);
306 addr = load_elf_image_shdr(addr);
308 if (ep && !strcmp(ep, "no"))
311 printf("## Starting application at 0x%08lx ...\n", addr);
314 * pass address parameter as argv[0] (aka command name),
315 * and all remaining args
317 rc = do_bootelf_exec((void *)addr, argc, argv);
321 printf("## Application terminated, rc = 0x%lx\n", rc);
327 * Interpreter command to boot VxWorks from a memory image. The image can
328 * be either an ELF image or a raw binary. Will attempt to setup the
329 * bootline and other parameters correctly.
331 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
333 unsigned long addr; /* Address of image */
334 unsigned long bootaddr = 0; /* Address to put the bootline */
335 char *bootline; /* Text of the bootline */
336 char *tmp; /* Temporary char pointer */
337 char build_buf[128]; /* Buffer for building the bootline */
341 struct e820_info *info;
342 struct e820_entry *data;
343 struct efi_gop_info *gop;
344 struct vesa_mode_info *vesa = &mode_info.vesa;
348 * Check the loadaddr variable.
349 * If we don't know where the image is then we're done.
352 addr = image_load_addr;
354 addr = simple_strtoul(argv[1], NULL, 16);
356 #if defined(CONFIG_CMD_NET)
358 * Check to see if we need to tftp the image ourselves
361 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
362 if (net_loop(TFTPGET) <= 0)
364 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
370 * This should equate to
371 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
372 * from the VxWorks BSP header files.
373 * This will vary from board to board
375 #if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
376 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
377 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
378 memcpy(tmp, build_buf, 6);
380 puts("## Ethernet MAC address not copied to NV RAM\n");
385 * Get VxWorks's physical memory base address from environment,
386 * if we don't specify it in the environment, use a default one.
388 base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
389 data = (struct e820_entry *)(base + E820_DATA_OFFSET);
390 info = (struct e820_info *)(base + E820_INFO_OFFSET);
392 memset(info, 0, sizeof(struct e820_info));
393 info->sign = E820_SIGNATURE;
394 info->entries = install_e820_map(E820MAX, data);
395 info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
399 * Explicitly clear the bootloader image size otherwise if memory
400 * at this offset happens to contain some garbage data, the final
401 * available memory size for the kernel is insane.
403 *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
406 * Prepare compatible framebuffer information block.
407 * The VESA mode has to be 32-bit RGBA.
409 if (vesa->x_resolution && vesa->y_resolution) {
410 gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET);
411 gop->magic = EFI_GOP_INFO_MAGIC;
412 gop->info.version = 0;
413 gop->info.width = vesa->x_resolution;
414 gop->info.height = vesa->y_resolution;
415 gop->info.pixel_format = EFI_GOT_RGBA8;
416 gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4;
417 gop->fb_base = vesa->phys_base_ptr;
418 gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution;
423 * Use bootaddr to find the location in memory that VxWorks
424 * will look for the bootline string. The default value is
425 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
426 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
428 tmp = env_get("bootaddr");
431 bootaddr = base + X86_BOOT_LINE_OFFSET;
433 printf("## VxWorks bootline address not specified\n");
439 bootaddr = simple_strtoul(tmp, NULL, 16);
442 * Check to see if the bootline is defined in the 'bootargs' parameter.
443 * If it is not defined, we may be able to construct the info.
445 bootline = env_get("bootargs");
447 tmp = env_get("bootdev");
449 strcpy(build_buf, tmp);
452 printf("## VxWorks boot device not specified\n");
455 tmp = env_get("bootfile");
457 ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
459 ptr += sprintf(build_buf + ptr, "host:vxWorks ");
462 * The following parameters are only needed if 'bootdev'
463 * is an ethernet device, otherwise they are optional.
465 tmp = env_get("ipaddr");
467 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
468 tmp = env_get("netmask");
470 u32 mask = env_get_ip("netmask").s_addr;
471 ptr += sprintf(build_buf + ptr,
472 ":%08x ", ntohl(mask));
474 ptr += sprintf(build_buf + ptr, " ");
478 tmp = env_get("serverip");
480 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
482 tmp = env_get("gatewayip");
484 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
486 tmp = env_get("hostname");
488 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
490 tmp = env_get("othbootargs");
492 strcpy(build_buf + ptr, tmp);
496 bootline = build_buf;
499 memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255));
500 flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
501 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr);
504 * If the data at the load address is an elf image, then
505 * treat it like an elf image. Otherwise, assume that it is a
508 if (valid_elf_image(addr))
509 addr = load_elf_image_phdr(addr);
511 puts("## Not an ELF image, assuming binary\n");
513 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
516 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
522 /* VxWorks on x86 uses stack to pass parameters */
523 ((asmlinkage void (*)(int))addr)(0);
525 ((void (*)(int))addr)(0);
528 puts("## vxWorks terminated\n");
534 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
535 "Boot from an ELF image in memory",
536 "[-p|-s] [address]\n"
537 "\t- load ELF image at [address] via program headers (-p)\n"
538 "\t or via section headers (-s)"
542 bootvx, 2, 0, do_bootvx,
543 "Boot vxWorks from an ELF image",
544 " [address] - load address of vxWorks ELF image."