Merge tag 'u-boot-imx-20200121' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[oweals/u-boot.git] / cmd / elf.c
1 /*
2  * Copyright (c) 2001 William L. Pitts
3  * All rights reserved.
4  *
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
8  * such forms.
9  *
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
13  * purpose.
14  */
15
16 #include <common.h>
17 #include <command.h>
18 #include <cpu_func.h>
19 #include <elf.h>
20 #include <env.h>
21 #include <image.h>
22 #include <net.h>
23 #include <vxworks.h>
24 #ifdef CONFIG_X86
25 #include <vbe.h>
26 #include <asm/e820.h>
27 #include <linux/linkage.h>
28 #endif
29
30 /*
31  * A very simple ELF64 loader, assumes the image is valid, returns the
32  * entry point address.
33  *
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.
36  */
37 static unsigned long load_elf64_image_phdr(unsigned long addr)
38 {
39         Elf64_Ehdr *ehdr; /* Elf header structure pointer */
40         Elf64_Phdr *phdr; /* Program header structure pointer */
41         int i;
42
43         ehdr = (Elf64_Ehdr *)addr;
44         phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
45
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;
50
51                 debug("Loading phdr %i to 0x%p (%lu bytes)\n",
52                       i, dst, (ulong)phdr->p_filesz);
53                 if (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));
60                 ++phdr;
61         }
62
63         if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
64                                             EF_PPC64_ELFV1_ABI)) {
65                 /*
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.
69                  */
70                 uintptr_t addr = ehdr->e_entry;
71
72                 return *(Elf64_Addr *)addr;
73         }
74
75         return ehdr->e_entry;
76 }
77
78 static unsigned long load_elf64_image_shdr(unsigned long addr)
79 {
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 */
85
86         ehdr = (Elf64_Ehdr *)addr;
87
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)));
91
92         if (shdr->sh_type == SHT_STRTAB)
93                 strtab = (unsigned char *)(addr + (ulong)shdr->sh_offset);
94
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)));
99
100                 if (!(shdr->sh_flags & SHF_ALLOC) ||
101                     shdr->sh_addr == 0 || shdr->sh_size == 0) {
102                         continue;
103                 }
104
105                 if (strtab) {
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);
111                 }
112
113                 if (shdr->sh_type == SHT_NOBITS) {
114                         memset((void *)(uintptr_t)shdr->sh_addr, 0,
115                                shdr->sh_size);
116                 } else {
117                         image = (unsigned char *)addr + (ulong)shdr->sh_offset;
118                         memcpy((void *)(uintptr_t)shdr->sh_addr,
119                                (const void *)image, shdr->sh_size);
120                 }
121                 flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
122                             roundup((shdr->sh_addr + shdr->sh_size),
123                                      ARCH_DMA_MINALIGN) -
124                                     rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
125         }
126
127         if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
128                                             EF_PPC64_ELFV1_ABI)) {
129                 /*
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.
133                  */
134                 uintptr_t addr = ehdr->e_entry;
135
136                 return *(Elf64_Addr *)addr;
137         }
138
139         return ehdr->e_entry;
140 }
141
142 /*
143  * A very simple ELF loader, assumes the image is valid, returns the
144  * entry point address.
145  *
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.
148  */
149 static unsigned long load_elf_image_phdr(unsigned long addr)
150 {
151         Elf32_Ehdr *ehdr; /* Elf header structure pointer */
152         Elf32_Phdr *phdr; /* Program header structure pointer */
153         int i;
154
155         ehdr = (Elf32_Ehdr *)addr;
156         if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
157                 return load_elf64_image_phdr(addr);
158
159         phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
160
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;
165
166                 debug("Loading phdr %i to 0x%p (%i bytes)\n",
167                       i, dst, phdr->p_filesz);
168                 if (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));
175                 ++phdr;
176         }
177
178         return ehdr->e_entry;
179 }
180
181 static unsigned long load_elf_image_shdr(unsigned long addr)
182 {
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 */
188
189         ehdr = (Elf32_Ehdr *)addr;
190         if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
191                 return load_elf64_image_shdr(addr);
192
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)));
196
197         if (shdr->sh_type == SHT_STRTAB)
198                 strtab = (unsigned char *)(addr + shdr->sh_offset);
199
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)));
204
205                 if (!(shdr->sh_flags & SHF_ALLOC) ||
206                     shdr->sh_addr == 0 || shdr->sh_size == 0) {
207                         continue;
208                 }
209
210                 if (strtab) {
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);
216                 }
217
218                 if (shdr->sh_type == SHT_NOBITS) {
219                         memset((void *)(uintptr_t)shdr->sh_addr, 0,
220                                shdr->sh_size);
221                 } else {
222                         image = (unsigned char *)addr + shdr->sh_offset;
223                         memcpy((void *)(uintptr_t)shdr->sh_addr,
224                                (const void *)image, shdr->sh_size);
225                 }
226                 flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
227                             roundup((shdr->sh_addr + shdr->sh_size),
228                                     ARCH_DMA_MINALIGN) -
229                             rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
230         }
231
232         return ehdr->e_entry;
233 }
234
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[])
238 {
239         unsigned long ret;
240
241         /*
242          * pass address parameter as argv[0] (aka command name),
243          * and all remaining args
244          */
245         ret = entry(argc, argv);
246
247         return ret;
248 }
249
250 /*
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
253  * executable.
254  */
255 int valid_elf_image(unsigned long addr)
256 {
257         Elf32_Ehdr *ehdr; /* Elf header structure pointer */
258
259         ehdr = (Elf32_Ehdr *)addr;
260
261         if (!IS_ELF(*ehdr)) {
262                 printf("## No elf image at address 0x%08lx\n", addr);
263                 return 0;
264         }
265
266         if (ehdr->e_type != ET_EXEC) {
267                 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
268                 return 0;
269         }
270
271         return 1;
272 }
273
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[])
276 {
277         unsigned long addr; /* Address of the ELF image */
278         unsigned long rc; /* Return value from user code */
279         char *sload = NULL;
280         const char *ep = env_get("autostart");
281         int rcode = 0;
282
283         /* Consume 'bootelf' */
284         argc--; argv++;
285
286         /* Check for flag. */
287         if (argc >= 1 && (argv[0][0] == '-' && \
288                                 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
289                 sload = argv[0];
290                 /* Consume flag. */
291                 argc--; argv++;
292         }
293         /* Check for address. */
294         if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
295                 /* Consume address */
296                 argc--; argv++;
297         } else
298                 addr = image_load_addr;
299
300         if (!valid_elf_image(addr))
301                 return 1;
302
303         if (sload && sload[1] == 'p')
304                 addr = load_elf_image_phdr(addr);
305         else
306                 addr = load_elf_image_shdr(addr);
307
308         if (ep && !strcmp(ep, "no"))
309                 return rcode;
310
311         printf("## Starting application at 0x%08lx ...\n", addr);
312
313         /*
314          * pass address parameter as argv[0] (aka command name),
315          * and all remaining args
316          */
317         rc = do_bootelf_exec((void *)addr, argc, argv);
318         if (rc != 0)
319                 rcode = 1;
320
321         printf("## Application terminated, rc = 0x%lx\n", rc);
322
323         return rcode;
324 }
325
326 /*
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.
330  */
331 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
332 {
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 */
338         int ptr = 0;
339 #ifdef CONFIG_X86
340         ulong base;
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;
345 #endif
346
347         /*
348          * Check the loadaddr variable.
349          * If we don't know where the image is then we're done.
350          */
351         if (argc < 2)
352                 addr = image_load_addr;
353         else
354                 addr = simple_strtoul(argv[1], NULL, 16);
355
356 #if defined(CONFIG_CMD_NET)
357         /*
358          * Check to see if we need to tftp the image ourselves
359          * before starting
360          */
361         if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
362                 if (net_loop(TFTPGET) <= 0)
363                         return 1;
364                 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
365                         addr);
366         }
367 #endif
368
369         /*
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
374          */
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);
379 #else
380         puts("## Ethernet MAC address not copied to NV RAM\n");
381 #endif
382
383 #ifdef CONFIG_X86
384         /*
385          * Get VxWorks's physical memory base address from environment,
386          * if we don't specify it in the environment, use a default one.
387          */
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);
391
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) +
396                      E820_DATA_OFFSET;
397
398         /*
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.
402          */
403         *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
404
405         /*
406          * Prepare compatible framebuffer information block.
407          * The VESA mode has to be 32-bit RGBA.
408          */
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;
419         }
420 #endif
421
422         /*
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.
427          */
428         tmp = env_get("bootaddr");
429         if (!tmp) {
430 #ifdef CONFIG_X86
431                 bootaddr = base + X86_BOOT_LINE_OFFSET;
432 #else
433                 printf("## VxWorks bootline address not specified\n");
434                 return 1;
435 #endif
436         }
437
438         if (!bootaddr)
439                 bootaddr = simple_strtoul(tmp, NULL, 16);
440
441         /*
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.
444          */
445         bootline = env_get("bootargs");
446         if (!bootline) {
447                 tmp = env_get("bootdev");
448                 if (tmp) {
449                         strcpy(build_buf, tmp);
450                         ptr = strlen(tmp);
451                 } else {
452                         printf("## VxWorks boot device not specified\n");
453                 }
454
455                 tmp = env_get("bootfile");
456                 if (tmp)
457                         ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
458                 else
459                         ptr += sprintf(build_buf + ptr, "host:vxWorks ");
460
461                 /*
462                  * The following parameters are only needed if 'bootdev'
463                  * is an ethernet device, otherwise they are optional.
464                  */
465                 tmp = env_get("ipaddr");
466                 if (tmp) {
467                         ptr += sprintf(build_buf + ptr, "e=%s", tmp);
468                         tmp = env_get("netmask");
469                         if (tmp) {
470                                 u32 mask = env_get_ip("netmask").s_addr;
471                                 ptr += sprintf(build_buf + ptr,
472                                                ":%08x ", ntohl(mask));
473                         } else {
474                                 ptr += sprintf(build_buf + ptr, " ");
475                         }
476                 }
477
478                 tmp = env_get("serverip");
479                 if (tmp)
480                         ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
481
482                 tmp = env_get("gatewayip");
483                 if (tmp)
484                         ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
485
486                 tmp = env_get("hostname");
487                 if (tmp)
488                         ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
489
490                 tmp = env_get("othbootargs");
491                 if (tmp) {
492                         strcpy(build_buf + ptr, tmp);
493                         ptr += strlen(tmp);
494                 }
495
496                 bootline = build_buf;
497         }
498
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);
502
503         /*
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
506          * binary image.
507          */
508         if (valid_elf_image(addr))
509                 addr = load_elf_image_phdr(addr);
510         else
511                 puts("## Not an ELF image, assuming binary\n");
512
513         printf("## Starting vxWorks at 0x%08lx ...\n", addr);
514
515         dcache_disable();
516 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
517         armv8_setup_psci();
518         smp_kick_all_cpus();
519 #endif
520
521 #ifdef CONFIG_X86
522         /* VxWorks on x86 uses stack to pass parameters */
523         ((asmlinkage void (*)(int))addr)(0);
524 #else
525         ((void (*)(int))addr)(0);
526 #endif
527
528         puts("## vxWorks terminated\n");
529
530         return 1;
531 }
532
533 U_BOOT_CMD(
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)"
539 );
540
541 U_BOOT_CMD(
542         bootvx, 2, 0, do_bootvx,
543         "Boot vxWorks from an ELF image",
544         " [address] - load address of vxWorks ELF image."
545 );