]> Git Repo - u-boot.git/blob - cmd/elf.c
Merge patch series "Enable OF_UPSTREAM for J721s2 and AM68"
[u-boot.git] / cmd / elf.c
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2001 William L. Pitts
4  * All rights reserved.
5  */
6
7 #include <command.h>
8 #include <cpu_func.h>
9 #include <elf.h>
10 #include <env.h>
11 #include <image.h>
12 #include <log.h>
13 #include <net.h>
14 #include <vxworks.h>
15 #ifdef CONFIG_X86
16 #include <vesa.h>
17 #include <asm/cache.h>
18 #include <asm/e820.h>
19 #include <linux/linkage.h>
20 #endif
21
22 /* Allow ports to override the default behavior */
23 static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
24                                      int argc, char *const argv[])
25 {
26         unsigned long ret;
27
28         /*
29          * pass address parameter as argv[0] (aka command name),
30          * and all remaining args
31          */
32         ret = entry(argc, argv);
33
34         return ret;
35 }
36
37 /* Interpreter command to boot an arbitrary ELF image from memory */
38 int do_bootelf(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
39 {
40 #if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
41         struct bootm_headers img = {0};
42         unsigned long fdt_addr = 0; /* Address of the FDT */
43 #endif
44         unsigned long addr; /* Address of the ELF image */
45         unsigned long rc; /* Return value from user code */
46         char *sload = NULL;
47         int rcode = 0;
48
49         /* Consume 'bootelf' */
50         argc--; argv++;
51
52         /* Check for [-p|-s] flag. */
53         if (argc >= 1 && (argv[0][0] == '-' && \
54                                 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
55                 sload = argv[0];
56                 /* Consume flag. */
57                 argc--; argv++;
58         }
59
60 #if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
61         /* Check for [-d fdt_addr_r] option. */
62         if ((argc >= 2) && (argv[0][0] == '-') && (argv[0][1] == 'd')) {
63                 if (strict_strtoul(argv[1], 16, &fdt_addr) != 0)
64                         return CMD_RET_USAGE;
65                 /* Consume option. */
66                 argc -= 2;
67                 argv += 2;
68         }
69 #endif
70
71         /* Check for address. */
72         if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
73                 /* Consume address */
74                 argc--; argv++;
75         } else
76                 addr = image_load_addr;
77
78         if (!valid_elf_image(addr))
79                 return 1;
80
81         if (sload && sload[1] == 'p')
82                 addr = load_elf_image_phdr(addr);
83         else
84                 addr = load_elf_image_shdr(addr);
85
86 #if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
87         if (fdt_addr) {
88                 printf("## Setting up FDT at 0x%08lx ...\n", fdt_addr);
89                 flush();
90
91                 if (image_setup_libfdt(&img, (void *)fdt_addr, NULL))
92                         return 1;
93         }
94 #endif
95
96         if (!env_get_autostart())
97                 return rcode;
98
99         printf("## Starting application at 0x%08lx ...\n", addr);
100         flush();
101
102         /*
103          * pass address parameter as argv[0] (aka command name),
104          * and all remaining args
105          */
106         rc = do_bootelf_exec((void *)addr, argc, argv);
107         if (rc != 0)
108                 rcode = 1;
109
110         printf("## Application terminated, rc = 0x%lx\n", rc);
111
112         return rcode;
113 }
114
115 /*
116  * Interpreter command to boot VxWorks from a memory image.  The image can
117  * be either an ELF image or a raw binary.  Will attempt to setup the
118  * bootline and other parameters correctly.
119  */
120 int do_bootvx(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
121 {
122         unsigned long addr; /* Address of image */
123         unsigned long bootaddr = 0; /* Address to put the bootline */
124         char *bootline; /* Text of the bootline */
125         char *tmp; /* Temporary char pointer */
126         char build_buf[128]; /* Buffer for building the bootline */
127         int ptr = 0;
128 #ifdef CONFIG_X86
129         ulong base;
130         struct e820_info *info;
131         struct e820_entry *data;
132         struct efi_gop_info *gop;
133         struct vesa_mode_info *vesa = &mode_info.vesa;
134 #endif
135
136         /*
137          * Check the loadaddr variable.
138          * If we don't know where the image is then we're done.
139          */
140         if (argc < 2)
141                 addr = image_load_addr;
142         else
143                 addr = hextoul(argv[1], NULL);
144
145 #if defined(CONFIG_CMD_NET)
146         /*
147          * Check to see if we need to tftp the image ourselves
148          * before starting
149          */
150         if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
151                 if (net_loop(TFTPGET) <= 0)
152                         return 1;
153                 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
154                         addr);
155         }
156 #endif
157
158         /*
159          * This should equate to
160          * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
161          * from the VxWorks BSP header files.
162          * This will vary from board to board
163          */
164 #if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
165         tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
166         eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
167         memcpy(tmp, build_buf, 6);
168 #else
169         puts("## Ethernet MAC address not copied to NV RAM\n");
170 #endif
171
172 #ifdef CONFIG_X86
173         /*
174          * Get VxWorks's physical memory base address from environment,
175          * if we don't specify it in the environment, use a default one.
176          */
177         base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
178         data = (struct e820_entry *)(base + E820_DATA_OFFSET);
179         info = (struct e820_info *)(base + E820_INFO_OFFSET);
180
181         memset(info, 0, sizeof(struct e820_info));
182         info->sign = E820_SIGNATURE;
183         info->entries = install_e820_map(E820MAX, data);
184         info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
185                      E820_DATA_OFFSET;
186
187         /*
188          * Explicitly clear the bootloader image size otherwise if memory
189          * at this offset happens to contain some garbage data, the final
190          * available memory size for the kernel is insane.
191          */
192         *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
193
194         /*
195          * Prepare compatible framebuffer information block.
196          * The VESA mode has to be 32-bit RGBA.
197          */
198         if (vesa->x_resolution && vesa->y_resolution) {
199                 gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET);
200                 gop->magic = EFI_GOP_INFO_MAGIC;
201                 gop->info.version = 0;
202                 gop->info.width = vesa->x_resolution;
203                 gop->info.height = vesa->y_resolution;
204                 gop->info.pixel_format = EFI_GOT_RGBA8;
205                 gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4;
206                 gop->fb_base = vesa->phys_base_ptr;
207                 gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution;
208         }
209 #endif
210
211         /*
212          * Use bootaddr to find the location in memory that VxWorks
213          * will look for the bootline string. The default value is
214          * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
215          * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
216          */
217         tmp = env_get("bootaddr");
218         if (!tmp) {
219 #ifdef CONFIG_X86
220                 bootaddr = base + X86_BOOT_LINE_OFFSET;
221 #else
222                 printf("## VxWorks bootline address not specified\n");
223                 return 1;
224 #endif
225         }
226
227         if (!bootaddr)
228                 bootaddr = hextoul(tmp, NULL);
229
230         /*
231          * Check to see if the bootline is defined in the 'bootargs' parameter.
232          * If it is not defined, we may be able to construct the info.
233          */
234         bootline = env_get("bootargs");
235         if (!bootline) {
236                 tmp = env_get("bootdev");
237                 if (tmp) {
238                         strcpy(build_buf, tmp);
239                         ptr = strlen(tmp);
240                 } else {
241                         printf("## VxWorks boot device not specified\n");
242                 }
243
244                 tmp = env_get("bootfile");
245                 if (tmp)
246                         ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
247                 else
248                         ptr += sprintf(build_buf + ptr, "host:vxWorks ");
249
250                 /*
251                  * The following parameters are only needed if 'bootdev'
252                  * is an ethernet device, otherwise they are optional.
253                  */
254                 tmp = env_get("ipaddr");
255                 if (tmp) {
256                         ptr += sprintf(build_buf + ptr, "e=%s", tmp);
257                         tmp = env_get("netmask");
258                         if (tmp) {
259                                 u32 mask = env_get_ip("netmask").s_addr;
260                                 ptr += sprintf(build_buf + ptr,
261                                                ":%08x ", ntohl(mask));
262                         } else {
263                                 ptr += sprintf(build_buf + ptr, " ");
264                         }
265                 }
266
267                 tmp = env_get("serverip");
268                 if (tmp)
269                         ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
270
271                 tmp = env_get("gatewayip");
272                 if (tmp)
273                         ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
274
275                 tmp = env_get("hostname");
276                 if (tmp)
277                         ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
278
279                 tmp = env_get("othbootargs");
280                 if (tmp) {
281                         strcpy(build_buf + ptr, tmp);
282                         ptr += strlen(tmp);
283                 }
284
285                 bootline = build_buf;
286         }
287
288         memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255));
289         flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
290         printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr);
291
292         /*
293          * If the data at the load address is an elf image, then
294          * treat it like an elf image. Otherwise, assume that it is a
295          * binary image.
296          */
297         if (valid_elf_image(addr))
298                 addr = load_elf_image_phdr(addr);
299         else
300                 puts("## Not an ELF image, assuming binary\n");
301
302         printf("## Starting vxWorks at 0x%08lx ...\n", addr);
303         flush();
304
305         dcache_disable();
306 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
307         armv8_setup_psci();
308         smp_kick_all_cpus();
309 #endif
310
311 #ifdef CONFIG_X86
312         /* VxWorks on x86 uses stack to pass parameters */
313         ((asmlinkage void (*)(int))addr)(0);
314 #else
315         ((void (*)(int))addr)(0);
316 #endif
317
318         puts("## vxWorks terminated\n");
319
320         return 1;
321 }
322
323 U_BOOT_CMD(
324         bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
325         "Boot from an ELF image in memory",
326         "[-p|-s] "
327 #if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
328         "[-d fdt_addr_r] "
329 #endif
330         "[address]\n"
331         "\t- load ELF image at [address] via program headers (-p)\n"
332         "\t  or via section headers (-s)\n"
333 #if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
334         "\t- setup FDT image at [fdt_addr_r] (-d)"
335 #endif
336 );
337
338 U_BOOT_CMD(
339         bootvx, 2, 0, do_bootvx,
340         "Boot vxWorks from an ELF image",
341         " [address] - load address of vxWorks ELF image."
342 );
This page took 0.044439 seconds and 4 git commands to generate.