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