blob: a02361f9f512eca9d1113a0181fe6fab330cc86e [file] [log] [blame]
Tom Rini8abb4cb2020-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
wdenk458ded32002-09-20 10:59:08 +00007#include <command.h>
Simon Glass370382c2019-11-14 12:57:35 -07008#include <cpu_func.h>
wdenk458ded32002-09-20 10:59:08 +00009#include <elf.h>
Simon Glass83c2e492019-08-01 09:46:50 -060010#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070011#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Bin Meng53f3afb2015-10-07 20:19:13 -070013#include <net.h>
Niklaus Gigerf662b232008-11-03 22:15:34 +010014#include <vxworks.h>
Bin Meng321e8602015-10-07 20:19:18 -070015#ifdef CONFIG_X86
Simon Glassec86bc62022-07-30 15:52:04 -060016#include <vesa.h>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <asm/cache.h>
Bin Meng321e8602015-10-07 20:19:18 -070018#include <asm/e820.h>
Bin Mengf6900e22015-10-07 20:19:19 -070019#include <linux/linkage.h>
Bin Meng321e8602015-10-07 20:19:18 -070020#endif
wdenk458ded32002-09-20 10:59:08 +000021
Mike Frysingere60644a2008-04-13 19:42:18 -040022/* Allow ports to override the default behavior */
Jeroen Hofstee3cc2a742014-10-08 22:57:31 +020023static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
Simon Glassed38aef2020-05-10 11:40:03 -060024 int argc, char *const argv[])
Mike Frysingerf79507e2008-01-29 18:21:05 -050025{
Mike Frysingere60644a2008-04-13 19:42:18 -040026 unsigned long ret;
27
Mike Frysingerf79507e2008-01-29 18:21:05 -050028 /*
Mike Frysingere60644a2008-04-13 19:42:18 -040029 * pass address parameter as argv[0] (aka command name),
30 * and all remaining args
31 */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000032 ret = entry(argc, argv);
Mike Frysingerf79507e2008-01-29 18:21:05 -050033
Mike Frysingere60644a2008-04-13 19:42:18 -040034 return ret;
35}
wdenk458ded32002-09-20 10:59:08 +000036
Bin Meng53f3afb2015-10-07 20:19:13 -070037/* Interpreter command to boot an arbitrary ELF image from memory */
Simon Glassed38aef2020-05-10 11:40:03 -060038int do_bootelf(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk458ded32002-09-20 10:59:08 +000039{
Maxim Moskalets938f0112024-03-08 00:29:14 +030040#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
Bin Meng53f3afb2015-10-07 20:19:13 -070044 unsigned long addr; /* Address of the ELF image */
45 unsigned long rc; /* Return value from user code */
Tom Rini3c95c792017-05-18 17:03:07 -040046 char *sload = NULL;
wdenk458ded32002-09-20 10:59:08 +000047 int rcode = 0;
48
Tom Rini3c95c792017-05-18 17:03:07 -040049 /* Consume 'bootelf' */
50 argc--; argv++;
Mike Frysingere6dc29f2010-10-02 15:44:53 -040051
Maxim Moskalets938f0112024-03-08 00:29:14 +030052 /* Check for [-p|-s] flag. */
Tom Rini3c95c792017-05-18 17:03:07 -040053 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 }
Maxim Moskalets938f0112024-03-08 00:29:14 +030059
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
Tom Rini3c95c792017-05-18 17:03:07 -040071 /* Check for address. */
72 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
73 /* Consume address */
74 argc--; argv++;
75 } else
Simon Glass892265d2019-12-28 10:45:02 -070076 addr = image_load_addr;
wdenk458ded32002-09-20 10:59:08 +000077
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000078 if (!valid_elf_image(addr))
wdenk458ded32002-09-20 10:59:08 +000079 return 1;
80
Mike Frysingere6dc29f2010-10-02 15:44:53 -040081 if (sload && sload[1] == 'p')
82 addr = load_elf_image_phdr(addr);
83 else
84 addr = load_elf_image_shdr(addr);
wdenk458ded32002-09-20 10:59:08 +000085
Maxim Moskalets938f0112024-03-08 00:29:14 +030086#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
Simon Glass46b530a2021-10-21 21:08:52 -060096 if (!env_get_autostart())
Siva Durga Prasad Paladugu3825bfa2015-03-09 12:46:47 +010097 return rcode;
98
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000099 printf("## Starting application at 0x%08lx ...\n", addr);
Pali Rohár937b7d82022-09-05 11:31:21 +0200100 flush();
wdenk458ded32002-09-20 10:59:08 +0000101
wdenk458ded32002-09-20 10:59:08 +0000102 /*
103 * pass address parameter as argv[0] (aka command name),
104 * and all remaining args
105 */
Tom Rini3c95c792017-05-18 17:03:07 -0400106 rc = do_bootelf_exec((void *)addr, argc, argv);
wdenk458ded32002-09-20 10:59:08 +0000107 if (rc != 0)
108 rcode = 1;
109
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000110 printf("## Application terminated, rc = 0x%lx\n", rc);
Bin Meng53f3afb2015-10-07 20:19:13 -0700111
wdenk458ded32002-09-20 10:59:08 +0000112 return rcode;
113}
114
Bin Meng53f3afb2015-10-07 20:19:13 -0700115/*
wdenk458ded32002-09-20 10:59:08 +0000116 * 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.
Bin Meng53f3afb2015-10-07 20:19:13 -0700119 */
Simon Glassed38aef2020-05-10 11:40:03 -0600120int do_bootvx(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk458ded32002-09-20 10:59:08 +0000121{
Bin Meng53f3afb2015-10-07 20:19:13 -0700122 unsigned long addr; /* Address of image */
Bin Meng75f4b722018-04-11 22:02:22 -0700123 unsigned long bootaddr = 0; /* Address to put the bootline */
Bin Meng53f3afb2015-10-07 20:19:13 -0700124 char *bootline; /* Text of the bootline */
125 char *tmp; /* Temporary char pointer */
126 char build_buf[128]; /* Buffer for building the bootline */
Bin Meng0401c862015-10-07 20:19:15 -0700127 int ptr = 0;
Bin Meng321e8602015-10-07 20:19:18 -0700128#ifdef CONFIG_X86
Bin Menge3be8902018-04-11 22:02:07 -0700129 ulong base;
Bin Mengdde4ebb2018-04-11 22:02:09 -0700130 struct e820_info *info;
Bin Meng4b8fc742018-04-11 22:02:11 -0700131 struct e820_entry *data;
Bin Meng71606c42018-04-11 22:02:19 -0700132 struct efi_gop_info *gop;
133 struct vesa_mode_info *vesa = &mode_info.vesa;
Bin Meng321e8602015-10-07 20:19:18 -0700134#endif
wdenk458ded32002-09-20 10:59:08 +0000135
Bin Meng53f3afb2015-10-07 20:19:13 -0700136 /*
wdenk458ded32002-09-20 10:59:08 +0000137 * Check the loadaddr variable.
138 * If we don't know where the image is then we're done.
139 */
Stany MARCELe5deaf582013-11-27 14:48:43 +0100140 if (argc < 2)
Simon Glass892265d2019-12-28 10:45:02 -0700141 addr = image_load_addr;
Stefan Roese00487782006-11-29 12:53:15 +0100142 else
Simon Glass3ff49ec2021-07-24 09:03:29 -0600143 addr = hextoul(argv[1], NULL);
wdenk458ded32002-09-20 10:59:08 +0000144
Jon Loeliger54324d02007-07-08 17:51:39 -0500145#if defined(CONFIG_CMD_NET)
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000146 /*
Bin Meng53f3afb2015-10-07 20:19:13 -0700147 * Check to see if we need to tftp the image ourselves
148 * before starting
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000149 */
Stany MARCELe5deaf582013-11-27 14:48:43 +0100150 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500151 if (net_loop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000152 return 1;
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000153 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
154 addr);
wdenk458ded32002-09-20 10:59:08 +0000155 }
156#endif
157
Bin Meng53f3afb2015-10-07 20:19:13 -0700158 /*
159 * This should equate to
160 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
wdenk458ded32002-09-20 10:59:08 +0000161 * from the VxWorks BSP header files.
162 * This will vary from board to board
163 */
Tuomas Tynkkynenf66276d2018-01-21 18:16:42 +0200164#if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Bin Meng53f3afb2015-10-07 20:19:13 -0700165 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
Simon Glass399a9ce2017-08-03 12:22:14 -0600166 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger8bcee042009-02-11 18:51:43 -0500167 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000168#else
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000169 puts("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000170#endif
171
Bin Meng75f4b722018-04-11 22:02:22 -0700172#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
wdenk57b2d802003-06-27 21:31:46 +0000211 /*
212 * Use bootaddr to find the location in memory that VxWorks
Bin Mengfb694b92015-10-07 20:19:17 -0700213 * 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.
wdenk458ded32002-09-20 10:59:08 +0000216 */
Simon Glass64b723f2017-08-03 12:22:12 -0600217 tmp = env_get("bootaddr");
Bin Mengfb694b92015-10-07 20:19:17 -0700218 if (!tmp) {
Bin Meng75f4b722018-04-11 22:02:22 -0700219#ifdef CONFIG_X86
220 bootaddr = base + X86_BOOT_LINE_OFFSET;
221#else
Bin Mengfb694b92015-10-07 20:19:17 -0700222 printf("## VxWorks bootline address not specified\n");
Bin Meng051f95e2018-04-11 22:02:21 -0700223 return 1;
Bin Meng75f4b722018-04-11 22:02:22 -0700224#endif
Bin Meng051f95e2018-04-11 22:02:21 -0700225 }
226
Bin Meng75f4b722018-04-11 22:02:22 -0700227 if (!bootaddr)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600228 bootaddr = hextoul(tmp, NULL);
Bin Meng051f95e2018-04-11 22:02:21 -0700229
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 ");
wdenk458ded32002-09-20 10:59:08 +0000249
Bin Mengfb694b92015-10-07 20:19:17 -0700250 /*
Bin Meng051f95e2018-04-11 22:02:21 -0700251 * The following parameters are only needed if 'bootdev'
252 * is an ethernet device, otherwise they are optional.
Bin Mengfb694b92015-10-07 20:19:17 -0700253 */
Bin Meng051f95e2018-04-11 22:02:21 -0700254 tmp = env_get("ipaddr");
255 if (tmp) {
256 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
257 tmp = env_get("netmask");
Ben Whitten34fd6c92015-12-30 13:05:58 +0000258 if (tmp) {
Bin Meng051f95e2018-04-11 22:02:21 -0700259 u32 mask = env_get_ip("netmask").s_addr;
Bin Mengfb694b92015-10-07 20:19:17 -0700260 ptr += sprintf(build_buf + ptr,
Bin Meng051f95e2018-04-11 22:02:21 -0700261 ":%08x ", ntohl(mask));
262 } else {
263 ptr += sprintf(build_buf + ptr, " ");
Bin Mengbf0ba252015-10-07 20:19:16 -0700264 }
Bin Meng051f95e2018-04-11 22:02:21 -0700265 }
wdenk458ded32002-09-20 10:59:08 +0000266
Bin Meng051f95e2018-04-11 22:02:21 -0700267 tmp = env_get("serverip");
268 if (tmp)
269 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000270
Bin Meng051f95e2018-04-11 22:02:21 -0700271 tmp = env_get("gatewayip");
272 if (tmp)
273 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
Bin Mengbf0ba252015-10-07 20:19:16 -0700274
Bin Meng051f95e2018-04-11 22:02:21 -0700275 tmp = env_get("hostname");
276 if (tmp)
277 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000278
Bin Meng051f95e2018-04-11 22:02:21 -0700279 tmp = env_get("othbootargs");
280 if (tmp) {
281 strcpy(build_buf + ptr, tmp);
282 ptr += strlen(tmp);
Bin Mengfb694b92015-10-07 20:19:17 -0700283 }
284
Bin Meng051f95e2018-04-11 22:02:21 -0700285 bootline = build_buf;
wdenk458ded32002-09-20 10:59:08 +0000286 }
287
Bin Meng051f95e2018-04-11 22:02:21 -0700288 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
wdenk57b2d802003-06-27 21:31:46 +0000292 /*
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
Bin Meng53f3afb2015-10-07 20:19:13 -0700295 * binary image.
wdenk458ded32002-09-20 10:59:08 +0000296 */
Bin Meng53f3afb2015-10-07 20:19:13 -0700297 if (valid_elf_image(addr))
Christian Gmeinerae7d4be2018-03-20 14:18:25 +0100298 addr = load_elf_image_phdr(addr);
Bin Meng53f3afb2015-10-07 20:19:13 -0700299 else
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000300 puts("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000301
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000302 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
Pali Rohár937b7d82022-09-05 11:31:21 +0200303 flush();
wdenk458ded32002-09-20 10:59:08 +0000304
Reinhard Arlt4fa117c2011-11-18 09:06:52 +0000305 dcache_disable();
Vasyl Vavrychuk70b551c2018-04-10 12:36:36 +0300306#if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
307 armv8_setup_psci();
308 smp_kick_all_cpus();
309#endif
310
Bin Mengf6900e22015-10-07 20:19:19 -0700311#ifdef CONFIG_X86
312 /* VxWorks on x86 uses stack to pass parameters */
313 ((asmlinkage void (*)(int))addr)(0);
314#else
Bin Meng53f3afb2015-10-07 20:19:13 -0700315 ((void (*)(int))addr)(0);
Bin Mengf6900e22015-10-07 20:19:19 -0700316#endif
wdenk458ded32002-09-20 10:59:08 +0000317
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000318 puts("## vxWorks terminated\n");
Bin Meng53f3afb2015-10-07 20:19:13 -0700319
wdenk458ded32002-09-20 10:59:08 +0000320 return 1;
321}
322
wdenkf287a242003-07-01 21:06:45 +0000323U_BOOT_CMD(
Tom Rini3c95c792017-05-18 17:03:07 -0400324 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600325 "Boot from an ELF image in memory",
Maxim Moskalets938f0112024-03-08 00:29:14 +0300326 "[-p|-s] "
327#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
328 "[-d fdt_addr_r] "
329#endif
330 "[address]\n"
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400331 "\t- load ELF image at [address] via program headers (-p)\n"
Maxim Moskalets938f0112024-03-08 00:29:14 +0300332 "\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
wdenk57b2d802003-06-27 21:31:46 +0000336);
337
wdenkf287a242003-07-01 21:06:45 +0000338U_BOOT_CMD(
Bin Meng53f3afb2015-10-07 20:19:13 -0700339 bootvx, 2, 0, do_bootvx,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600340 "Boot vxWorks from an ELF image",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200341 " [address] - load address of vxWorks ELF image."
wdenk57b2d802003-06-27 21:31:46 +0000342);