elf: Add a very simple ELF64 loader
[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 <elf.h>
19 #include <environment.h>
20 #include <net.h>
21 #include <vxworks.h>
22 #ifdef CONFIG_X86
23 #include <asm/e820.h>
24 #include <linux/linkage.h>
25 #endif
26
27 /*
28  * A very simple ELF64 loader, assumes the image is valid, returns the
29  * entry point address.
30  *
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.
33  */
34 static unsigned long load_elf64_image_phdr(unsigned long addr)
35 {
36         Elf64_Ehdr *ehdr; /* Elf header structure pointer */
37         Elf64_Phdr *phdr; /* Program header structure pointer */
38         int i;
39
40         ehdr = (Elf64_Ehdr *)addr;
41         phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
42
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;
47
48                 debug("Loading phdr %i to 0x%p (%lu bytes)\n",
49                       i, dst, (ulong)phdr->p_filesz);
50                 if (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);
56                 ++phdr;
57         }
58
59         return ehdr->e_entry;
60 }
61
62 /*
63  * A very simple ELF loader, assumes the image is valid, returns the
64  * entry point address.
65  *
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.
68  */
69 static unsigned long load_elf_image_phdr(unsigned long addr)
70 {
71         Elf32_Ehdr *ehdr; /* Elf header structure pointer */
72         Elf32_Phdr *phdr; /* Program header structure pointer */
73         int i;
74
75         ehdr = (Elf32_Ehdr *)addr;
76         if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
77                 return load_elf64_image_phdr(addr);
78
79         phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
80
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;
85
86                 debug("Loading phdr %i to 0x%p (%i bytes)\n",
87                       i, dst, phdr->p_filesz);
88                 if (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);
94                 ++phdr;
95         }
96
97         return ehdr->e_entry;
98 }
99
100 static unsigned long load_elf_image_shdr(unsigned long addr)
101 {
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 */
107
108         ehdr = (Elf32_Ehdr *)addr;
109
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)));
113
114         if (shdr->sh_type == SHT_STRTAB)
115                 strtab = (unsigned char *)(addr + shdr->sh_offset);
116
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)));
121
122                 if (!(shdr->sh_flags & SHF_ALLOC) ||
123                     shdr->sh_addr == 0 || shdr->sh_size == 0) {
124                         continue;
125                 }
126
127                 if (strtab) {
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);
133                 }
134
135                 if (shdr->sh_type == SHT_NOBITS) {
136                         memset((void *)(uintptr_t)shdr->sh_addr, 0,
137                                shdr->sh_size);
138                 } else {
139                         image = (unsigned char *)addr + shdr->sh_offset;
140                         memcpy((void *)(uintptr_t)shdr->sh_addr,
141                                (const void *)image, shdr->sh_size);
142                 }
143                 flush_cache(shdr->sh_addr, shdr->sh_size);
144         }
145
146         return ehdr->e_entry;
147 }
148
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[])
152 {
153         unsigned long ret;
154
155         /*
156          * pass address parameter as argv[0] (aka command name),
157          * and all remaining args
158          */
159         ret = entry(argc, argv);
160
161         return ret;
162 }
163
164 /*
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
167  * executable.
168  */
169 int valid_elf_image(unsigned long addr)
170 {
171         Elf32_Ehdr *ehdr; /* Elf header structure pointer */
172
173         ehdr = (Elf32_Ehdr *)addr;
174
175         if (!IS_ELF(*ehdr)) {
176                 printf("## No elf image at address 0x%08lx\n", addr);
177                 return 0;
178         }
179
180         if (ehdr->e_type != ET_EXEC) {
181                 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
182                 return 0;
183         }
184
185         return 1;
186 }
187
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[])
190 {
191         unsigned long addr; /* Address of the ELF image */
192         unsigned long rc; /* Return value from user code */
193         char *sload = NULL;
194         const char *ep = env_get("autostart");
195         int rcode = 0;
196
197         /* Consume 'bootelf' */
198         argc--; argv++;
199
200         /* Check for flag. */
201         if (argc >= 1 && (argv[0][0] == '-' && \
202                                 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
203                 sload = argv[0];
204                 /* Consume flag. */
205                 argc--; argv++;
206         }
207         /* Check for address. */
208         if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
209                 /* Consume address */
210                 argc--; argv++;
211         } else
212                 addr = load_addr;
213
214         if (!valid_elf_image(addr))
215                 return 1;
216
217         if (sload && sload[1] == 'p')
218                 addr = load_elf_image_phdr(addr);
219         else
220                 addr = load_elf_image_shdr(addr);
221
222         if (ep && !strcmp(ep, "no"))
223                 return rcode;
224
225         printf("## Starting application at 0x%08lx ...\n", addr);
226
227         /*
228          * pass address parameter as argv[0] (aka command name),
229          * and all remaining args
230          */
231         rc = do_bootelf_exec((void *)addr, argc, argv);
232         if (rc != 0)
233                 rcode = 1;
234
235         printf("## Application terminated, rc = 0x%lx\n", rc);
236
237         return rcode;
238 }
239
240 /*
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.
244  */
245 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
246 {
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 */
252         int ptr = 0;
253 #ifdef CONFIG_X86
254         ulong base;
255         struct e820_info *info;
256         struct e820_entry *data;
257 #endif
258
259         /*
260          * Check the loadaddr variable.
261          * If we don't know where the image is then we're done.
262          */
263         if (argc < 2)
264                 addr = load_addr;
265         else
266                 addr = simple_strtoul(argv[1], NULL, 16);
267
268 #if defined(CONFIG_CMD_NET)
269         /*
270          * Check to see if we need to tftp the image ourselves
271          * before starting
272          */
273         if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
274                 if (net_loop(TFTPGET) <= 0)
275                         return 1;
276                 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
277                         addr);
278         }
279 #endif
280
281         /*
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
286          */
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);
291 #else
292         puts("## Ethernet MAC address not copied to NV RAM\n");
293 #endif
294
295         /*
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.
300          */
301         tmp = env_get("bootaddr");
302         if (!tmp) {
303                 printf("## VxWorks bootline address not specified\n");
304         } else {
305                 bootaddr = simple_strtoul(tmp, NULL, 16);
306
307                 /*
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.
311                  */
312                 bootline = env_get("bootargs");
313                 if (bootline) {
314                         memcpy((void *)bootaddr, bootline,
315                                max(strlen(bootline), (size_t)255));
316                         flush_cache(bootaddr, max(strlen(bootline),
317                                                   (size_t)255));
318                 } else {
319                         tmp = env_get("bootdev");
320                         if (tmp) {
321                                 strcpy(build_buf, tmp);
322                                 ptr = strlen(tmp);
323                         } else
324                                 printf("## VxWorks boot device not specified\n");
325
326                         tmp = env_get("bootfile");
327                         if (tmp)
328                                 ptr += sprintf(build_buf + ptr,
329                                                "host:%s ", tmp);
330                         else
331                                 ptr += sprintf(build_buf + ptr,
332                                                "host:vxWorks ");
333
334                         /*
335                          * The following parameters are only needed if 'bootdev'
336                          * is an ethernet device, otherwise they are optional.
337                          */
338                         tmp = env_get("ipaddr");
339                         if (tmp) {
340                                 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
341                                 tmp = env_get("netmask");
342                                 if (tmp) {
343                                         u32 mask = env_get_ip("netmask").s_addr;
344                                         ptr += sprintf(build_buf + ptr,
345                                                        ":%08x ", ntohl(mask));
346                                 } else {
347                                         ptr += sprintf(build_buf + ptr, " ");
348                                 }
349                         }
350
351                         tmp = env_get("serverip");
352                         if (tmp)
353                                 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
354
355                         tmp = env_get("gatewayip");
356                         if (tmp)
357                                 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
358
359                         tmp = env_get("hostname");
360                         if (tmp)
361                                 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
362
363                         tmp = env_get("othbootargs");
364                         if (tmp) {
365                                 strcpy(build_buf + ptr, tmp);
366                                 ptr += strlen(tmp);
367                         }
368
369                         memcpy((void *)bootaddr, build_buf,
370                                max(strlen(build_buf), (size_t)255));
371                         flush_cache(bootaddr, max(strlen(build_buf),
372                                                   (size_t)255));
373                 }
374
375                 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
376                        (char *)bootaddr);
377         }
378
379 #ifdef CONFIG_X86
380         /*
381          * Get VxWorks's physical memory base address from environment,
382          * if we don't specify it in the environment, use a default one.
383          */
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);
387
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) +
392                      E820_DATA_OFFSET;
393
394         /*
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.
398          */
399         *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
400 #endif
401
402         /*
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
405          * binary image.
406          */
407         if (valid_elf_image(addr))
408                 addr = load_elf_image_phdr(addr);
409         else
410                 puts("## Not an ELF image, assuming binary\n");
411
412         printf("## Starting vxWorks at 0x%08lx ...\n", addr);
413
414         dcache_disable();
415 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
416         armv8_setup_psci();
417         smp_kick_all_cpus();
418 #endif
419
420 #ifdef CONFIG_X86
421         /* VxWorks on x86 uses stack to pass parameters */
422         ((asmlinkage void (*)(int))addr)(0);
423 #else
424         ((void (*)(int))addr)(0);
425 #endif
426
427         puts("## vxWorks terminated\n");
428
429         return 1;
430 }
431
432 U_BOOT_CMD(
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)"
438 );
439
440 U_BOOT_CMD(
441         bootvx, 2, 0, do_bootvx,
442         "Boot vxWorks from an ELF image",
443         " [address] - load address of vxWorks ELF image."
444 );