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