blob: df4354d37428b89bb7dec8431e502ed6eda83a6c [file] [log] [blame]
Tom Rini89789eb2020-01-29 11:03:34 -05001// SPDX-License-Identifier: BSD-2-Clause
wdenk458ded32002-09-20 10:59:08 +00002/*
3 * Copyright (c) 2001 William L. Pitts
4 * All rights reserved.
wdenk458ded32002-09-20 10:59:08 +00005 */
6
Tom Rinid678a592024-05-18 20:20:43 -06007#include <common.h>
wdenk458ded32002-09-20 10:59:08 +00008#include <command.h>
Simon Glass62270f42019-11-14 12:57:35 -07009#include <cpu_func.h>
wdenk458ded32002-09-20 10:59:08 +000010#include <elf.h>
Simon Glasscdbff9f2019-08-01 09:46:50 -060011#include <env.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070012#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Bin Mengebca3df2015-10-07 20:19:13 -070014#include <net.h>
Niklaus Giger29a4c242008-11-03 22:15:34 +010015#include <vxworks.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070016#ifdef CONFIG_X86
Simon Glasscafe8712022-07-30 15:52:04 -060017#include <vesa.h>
Simon Glass90526e92020-05-10 11:39:56 -060018#include <asm/cache.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070019#include <asm/e820.h>
Bin Meng9aa12802015-10-07 20:19:19 -070020#include <linux/linkage.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070021#endif
wdenk458ded32002-09-20 10:59:08 +000022
Mike Frysinger017e9b72008-04-13 19:42:18 -040023/* Allow ports to override the default behavior */
Jeroen Hofstee553d8c32014-10-08 22:57:31 +020024static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
Simon Glass09140112020-05-10 11:40:03 -060025 int argc, char *const argv[])
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050026{
Mike Frysinger017e9b72008-04-13 19:42:18 -040027 unsigned long ret;
28
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050029 /*
Mike Frysinger017e9b72008-04-13 19:42:18 -040030 * pass address parameter as argv[0] (aka command name),
31 * and all remaining args
32 */
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000033 ret = entry(argc, argv);
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050034
Mike Frysinger017e9b72008-04-13 19:42:18 -040035 return ret;
36}
wdenk458ded32002-09-20 10:59:08 +000037
Bin Mengebca3df2015-10-07 20:19:13 -070038/* Interpreter command to boot an arbitrary ELF image from memory */
Simon Glass09140112020-05-10 11:40:03 -060039int do_bootelf(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk458ded32002-09-20 10:59:08 +000040{
Maxim Moskalets6d1734f2024-03-08 00:29:14 +030041#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
42 struct bootm_headers img = {0};
43 unsigned long fdt_addr = 0; /* Address of the FDT */
44#endif
Bin Mengebca3df2015-10-07 20:19:13 -070045 unsigned long addr; /* Address of the ELF image */
46 unsigned long rc; /* Return value from user code */
Tom Rinibe1b8672017-05-18 17:03:07 -040047 char *sload = NULL;
wdenk458ded32002-09-20 10:59:08 +000048 int rcode = 0;
49
Tom Rinibe1b8672017-05-18 17:03:07 -040050 /* Consume 'bootelf' */
51 argc--; argv++;
Mike Frysingerf44a9282010-10-02 15:44:53 -040052
Maxim Moskalets6d1734f2024-03-08 00:29:14 +030053 /* Check for [-p|-s] flag. */
Tom Rinibe1b8672017-05-18 17:03:07 -040054 if (argc >= 1 && (argv[0][0] == '-' && \
55 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
56 sload = argv[0];
57 /* Consume flag. */
58 argc--; argv++;
59 }
Maxim Moskalets6d1734f2024-03-08 00:29:14 +030060
61#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
62 /* Check for [-d fdt_addr_r] option. */
63 if ((argc >= 2) && (argv[0][0] == '-') && (argv[0][1] == 'd')) {
64 if (strict_strtoul(argv[1], 16, &fdt_addr) != 0)
65 return CMD_RET_USAGE;
66 /* Consume option. */
67 argc -= 2;
68 argv += 2;
69 }
70#endif
71
Tom Rinibe1b8672017-05-18 17:03:07 -040072 /* Check for address. */
73 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
74 /* Consume address */
75 argc--; argv++;
76 } else
Simon Glassbb872dd2019-12-28 10:45:02 -070077 addr = image_load_addr;
wdenk458ded32002-09-20 10:59:08 +000078
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +000079 if (!valid_elf_image(addr))
wdenk458ded32002-09-20 10:59:08 +000080 return 1;
81
Mike Frysingerf44a9282010-10-02 15:44:53 -040082 if (sload && sload[1] == 'p')
83 addr = load_elf_image_phdr(addr);
84 else
85 addr = load_elf_image_shdr(addr);
wdenk458ded32002-09-20 10:59:08 +000086
Maxim Moskalets6d1734f2024-03-08 00:29:14 +030087#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
88 if (fdt_addr) {
89 printf("## Setting up FDT at 0x%08lx ...\n", fdt_addr);
90 flush();
91
92 if (image_setup_libfdt(&img, (void *)fdt_addr, NULL))
93 return 1;
94 }
95#endif
96
Simon Glass78398652021-10-21 21:08:52 -060097 if (!env_get_autostart())
Siva Durga Prasad Paladugu44c8fd32015-03-09 12:46:47 +010098 return rcode;
99
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000100 printf("## Starting application at 0x%08lx ...\n", addr);
Pali Rohárefc3f952022-09-05 11:31:21 +0200101 flush();
wdenk458ded32002-09-20 10:59:08 +0000102
wdenk458ded32002-09-20 10:59:08 +0000103 /*
104 * pass address parameter as argv[0] (aka command name),
105 * and all remaining args
106 */
Tom Rinibe1b8672017-05-18 17:03:07 -0400107 rc = do_bootelf_exec((void *)addr, argc, argv);
wdenk458ded32002-09-20 10:59:08 +0000108 if (rc != 0)
109 rcode = 1;
110
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000111 printf("## Application terminated, rc = 0x%lx\n", rc);
Bin Mengebca3df2015-10-07 20:19:13 -0700112
wdenk458ded32002-09-20 10:59:08 +0000113 return rcode;
114}
115
Bin Mengebca3df2015-10-07 20:19:13 -0700116/*
wdenk458ded32002-09-20 10:59:08 +0000117 * Interpreter command to boot VxWorks from a memory image. The image can
118 * be either an ELF image or a raw binary. Will attempt to setup the
119 * bootline and other parameters correctly.
Bin Mengebca3df2015-10-07 20:19:13 -0700120 */
Simon Glass09140112020-05-10 11:40:03 -0600121int do_bootvx(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk458ded32002-09-20 10:59:08 +0000122{
Bin Mengebca3df2015-10-07 20:19:13 -0700123 unsigned long addr; /* Address of image */
Bin Meng79c584e2018-04-11 22:02:22 -0700124 unsigned long bootaddr = 0; /* Address to put the bootline */
Bin Mengebca3df2015-10-07 20:19:13 -0700125 char *bootline; /* Text of the bootline */
126 char *tmp; /* Temporary char pointer */
127 char build_buf[128]; /* Buffer for building the bootline */
Bin Meng7f0c3c52015-10-07 20:19:15 -0700128 int ptr = 0;
Bin Mengb90ff0f2015-10-07 20:19:18 -0700129#ifdef CONFIG_X86
Bin Meng2902be82018-04-11 22:02:07 -0700130 ulong base;
Bin Mengfa5e91f2018-04-11 22:02:09 -0700131 struct e820_info *info;
Bin Meng45519922018-04-11 22:02:11 -0700132 struct e820_entry *data;
Bin Meng447ae4f2018-04-11 22:02:19 -0700133 struct efi_gop_info *gop;
134 struct vesa_mode_info *vesa = &mode_info.vesa;
Bin Mengb90ff0f2015-10-07 20:19:18 -0700135#endif
wdenk458ded32002-09-20 10:59:08 +0000136
Bin Mengebca3df2015-10-07 20:19:13 -0700137 /*
wdenk458ded32002-09-20 10:59:08 +0000138 * Check the loadaddr variable.
139 * If we don't know where the image is then we're done.
140 */
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100141 if (argc < 2)
Simon Glassbb872dd2019-12-28 10:45:02 -0700142 addr = image_load_addr;
Stefan Roese1bdd4682006-11-29 12:53:15 +0100143 else
Simon Glass7e5f4602021-07-24 09:03:29 -0600144 addr = hextoul(argv[1], NULL);
wdenk458ded32002-09-20 10:59:08 +0000145
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500146#if defined(CONFIG_CMD_NET)
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000147 /*
Bin Mengebca3df2015-10-07 20:19:13 -0700148 * Check to see if we need to tftp the image ourselves
149 * before starting
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000150 */
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100151 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500152 if (net_loop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000153 return 1;
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000154 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
155 addr);
wdenk458ded32002-09-20 10:59:08 +0000156 }
157#endif
158
Bin Mengebca3df2015-10-07 20:19:13 -0700159 /*
160 * This should equate to
161 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
wdenk458ded32002-09-20 10:59:08 +0000162 * from the VxWorks BSP header files.
163 * This will vary from board to board
164 */
Tuomas Tynkkynen89969752018-01-21 18:16:42 +0200165#if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Bin Mengebca3df2015-10-07 20:19:13 -0700166 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
Simon Glass35affd72017-08-03 12:22:14 -0600167 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger62c93d92009-02-11 18:51:43 -0500168 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000169#else
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000170 puts("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000171#endif
172
Bin Meng79c584e2018-04-11 22:02:22 -0700173#ifdef CONFIG_X86
174 /*
175 * Get VxWorks's physical memory base address from environment,
176 * if we don't specify it in the environment, use a default one.
177 */
178 base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
179 data = (struct e820_entry *)(base + E820_DATA_OFFSET);
180 info = (struct e820_info *)(base + E820_INFO_OFFSET);
181
182 memset(info, 0, sizeof(struct e820_info));
183 info->sign = E820_SIGNATURE;
184 info->entries = install_e820_map(E820MAX, data);
185 info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
186 E820_DATA_OFFSET;
187
188 /*
189 * Explicitly clear the bootloader image size otherwise if memory
190 * at this offset happens to contain some garbage data, the final
191 * available memory size for the kernel is insane.
192 */
193 *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
194
195 /*
196 * Prepare compatible framebuffer information block.
197 * The VESA mode has to be 32-bit RGBA.
198 */
199 if (vesa->x_resolution && vesa->y_resolution) {
200 gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET);
201 gop->magic = EFI_GOP_INFO_MAGIC;
202 gop->info.version = 0;
203 gop->info.width = vesa->x_resolution;
204 gop->info.height = vesa->y_resolution;
205 gop->info.pixel_format = EFI_GOT_RGBA8;
206 gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4;
207 gop->fb_base = vesa->phys_base_ptr;
208 gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution;
209 }
210#endif
211
wdenk8bde7f72003-06-27 21:31:46 +0000212 /*
213 * Use bootaddr to find the location in memory that VxWorks
Bin Meng9e98b7e2015-10-07 20:19:17 -0700214 * will look for the bootline string. The default value is
215 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
216 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
wdenk458ded32002-09-20 10:59:08 +0000217 */
Simon Glass00caae62017-08-03 12:22:12 -0600218 tmp = env_get("bootaddr");
Bin Meng9e98b7e2015-10-07 20:19:17 -0700219 if (!tmp) {
Bin Meng79c584e2018-04-11 22:02:22 -0700220#ifdef CONFIG_X86
221 bootaddr = base + X86_BOOT_LINE_OFFSET;
222#else
Bin Meng9e98b7e2015-10-07 20:19:17 -0700223 printf("## VxWorks bootline address not specified\n");
Bin Mengced71a22018-04-11 22:02:21 -0700224 return 1;
Bin Meng79c584e2018-04-11 22:02:22 -0700225#endif
Bin Mengced71a22018-04-11 22:02:21 -0700226 }
wdenk458ded32002-09-20 10:59:08 +0000227
Bin Meng79c584e2018-04-11 22:02:22 -0700228 if (!bootaddr)
Simon Glass7e5f4602021-07-24 09:03:29 -0600229 bootaddr = hextoul(tmp, NULL);
wdenk458ded32002-09-20 10:59:08 +0000230
Bin Mengced71a22018-04-11 22:02:21 -0700231 /*
232 * Check to see if the bootline is defined in the 'bootargs' parameter.
233 * If it is not defined, we may be able to construct the info.
234 */
235 bootline = env_get("bootargs");
236 if (!bootline) {
237 tmp = env_get("bootdev");
238 if (tmp) {
239 strcpy(build_buf, tmp);
240 ptr = strlen(tmp);
241 } else {
242 printf("## VxWorks boot device not specified\n");
Bin Menga4092db2015-10-07 20:19:16 -0700243 }
Niklaus Giger29a4c242008-11-03 22:15:34 +0100244
Bin Mengced71a22018-04-11 22:02:21 -0700245 tmp = env_get("bootfile");
246 if (tmp)
247 ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
248 else
249 ptr += sprintf(build_buf + ptr, "host:vxWorks ");
250
251 /*
252 * The following parameters are only needed if 'bootdev'
253 * is an ethernet device, otherwise they are optional.
254 */
255 tmp = env_get("ipaddr");
256 if (tmp) {
257 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
258 tmp = env_get("netmask");
259 if (tmp) {
260 u32 mask = env_get_ip("netmask").s_addr;
261 ptr += sprintf(build_buf + ptr,
262 ":%08x ", ntohl(mask));
263 } else {
264 ptr += sprintf(build_buf + ptr, " ");
265 }
266 }
267
268 tmp = env_get("serverip");
269 if (tmp)
270 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
271
272 tmp = env_get("gatewayip");
273 if (tmp)
274 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
275
276 tmp = env_get("hostname");
277 if (tmp)
278 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
279
280 tmp = env_get("othbootargs");
281 if (tmp) {
282 strcpy(build_buf + ptr, tmp);
283 ptr += strlen(tmp);
284 }
285
286 bootline = build_buf;
wdenk458ded32002-09-20 10:59:08 +0000287 }
288
Bin Mengced71a22018-04-11 22:02:21 -0700289 memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255));
290 flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
291 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr);
292
wdenk8bde7f72003-06-27 21:31:46 +0000293 /*
294 * If the data at the load address is an elf image, then
295 * treat it like an elf image. Otherwise, assume that it is a
Bin Mengebca3df2015-10-07 20:19:13 -0700296 * binary image.
wdenk458ded32002-09-20 10:59:08 +0000297 */
Bin Mengebca3df2015-10-07 20:19:13 -0700298 if (valid_elf_image(addr))
Christian Gmeiner476c2fc2018-03-20 14:18:25 +0100299 addr = load_elf_image_phdr(addr);
Bin Mengebca3df2015-10-07 20:19:13 -0700300 else
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000301 puts("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000302
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000303 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
Pali Rohárefc3f952022-09-05 11:31:21 +0200304 flush();
wdenk458ded32002-09-20 10:59:08 +0000305
Reinhard Arlt6eee21d2011-11-18 09:06:52 +0000306 dcache_disable();
Vasyl Vavrychuk3194daa2018-04-10 12:36:36 +0300307#if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
308 armv8_setup_psci();
309 smp_kick_all_cpus();
310#endif
311
Bin Meng9aa12802015-10-07 20:19:19 -0700312#ifdef CONFIG_X86
313 /* VxWorks on x86 uses stack to pass parameters */
314 ((asmlinkage void (*)(int))addr)(0);
315#else
Bin Mengebca3df2015-10-07 20:19:13 -0700316 ((void (*)(int))addr)(0);
Bin Meng9aa12802015-10-07 20:19:19 -0700317#endif
wdenk458ded32002-09-20 10:59:08 +0000318
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000319 puts("## vxWorks terminated\n");
Bin Mengebca3df2015-10-07 20:19:13 -0700320
wdenk458ded32002-09-20 10:59:08 +0000321 return 1;
322}
323
wdenk0d498392003-07-01 21:06:45 +0000324U_BOOT_CMD(
Tom Rinibe1b8672017-05-18 17:03:07 -0400325 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
Peter Tyser2fb26042009-01-27 18:03:12 -0600326 "Boot from an ELF image in memory",
Maxim Moskalets6d1734f2024-03-08 00:29:14 +0300327 "[-p|-s] "
328#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
329 "[-d fdt_addr_r] "
330#endif
331 "[address]\n"
Mike Frysingerf44a9282010-10-02 15:44:53 -0400332 "\t- load ELF image at [address] via program headers (-p)\n"
Maxim Moskalets6d1734f2024-03-08 00:29:14 +0300333 "\t or via section headers (-s)\n"
334#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
335 "\t- setup FDT image at [fdt_addr_r] (-d)"
336#endif
wdenk8bde7f72003-06-27 21:31:46 +0000337);
338
wdenk0d498392003-07-01 21:06:45 +0000339U_BOOT_CMD(
Bin Mengebca3df2015-10-07 20:19:13 -0700340 bootvx, 2, 0, do_bootvx,
Peter Tyser2fb26042009-01-27 18:03:12 -0600341 "Boot vxWorks from an ELF image",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200342 " [address] - load address of vxWorks ELF image."
wdenk8bde7f72003-06-27 21:31:46 +0000343);