blob: 32b7462f92aff4325e2cafc64a2eb5d01b66bfac [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
wdenk458ded32002-09-20 10:59:08 +00007#include <command.h>
Simon Glass62270f42019-11-14 12:57:35 -07008#include <cpu_func.h>
wdenk458ded32002-09-20 10:59:08 +00009#include <elf.h>
Simon Glasscdbff9f2019-08-01 09:46:50 -060010#include <env.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070011#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Bin Mengebca3df2015-10-07 20:19:13 -070013#include <net.h>
Niklaus Giger29a4c242008-11-03 22:15:34 +010014#include <vxworks.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070015#ifdef CONFIG_X86
Simon Glasscafe8712022-07-30 15:52:04 -060016#include <vesa.h>
Simon Glass90526e92020-05-10 11:39:56 -060017#include <asm/cache.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070018#include <asm/e820.h>
Bin Meng9aa12802015-10-07 20:19:19 -070019#include <linux/linkage.h>
Bin Mengb90ff0f2015-10-07 20:19:18 -070020#endif
wdenk458ded32002-09-20 10:59:08 +000021
Bin Mengebca3df2015-10-07 20:19:13 -070022/* Interpreter command to boot an arbitrary ELF image from memory */
Simon Glass09140112020-05-10 11:40:03 -060023int do_bootelf(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk458ded32002-09-20 10:59:08 +000024{
Maxim Moskalets6d1734f2024-03-08 00:29:14 +030025#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
26 struct bootm_headers img = {0};
27 unsigned long fdt_addr = 0; /* Address of the FDT */
28#endif
Bin Mengebca3df2015-10-07 20:19:13 -070029 unsigned long addr; /* Address of the ELF image */
30 unsigned long rc; /* Return value from user code */
Maxim Moskalets46b6a3e2024-06-05 21:43:34 +030031 int rcode = CMD_RET_SUCCESS;
32 Bootelf_flags flags = {0};
wdenk458ded32002-09-20 10:59:08 +000033
Tom Rinibe1b8672017-05-18 17:03:07 -040034 /* Consume 'bootelf' */
35 argc--; argv++;
Mike Frysingerf44a9282010-10-02 15:44:53 -040036
Maxim Moskalets6d1734f2024-03-08 00:29:14 +030037 /* Check for [-p|-s] flag. */
Tom Rinibe1b8672017-05-18 17:03:07 -040038 if (argc >= 1 && (argv[0][0] == '-' && \
39 (argv[0][1] == 'p' || argv[0][1] == 's'))) {
Maxim Moskalets46b6a3e2024-06-05 21:43:34 +030040 if (argv[0][1] == 'p')
41 flags.phdr = 1;
42 log_debug("Using ELF header format %s\n",
43 flags.phdr ? "phdr" : "shdr");
Tom Rinibe1b8672017-05-18 17:03:07 -040044 /* Consume flag. */
45 argc--; argv++;
46 }
Maxim Moskalets6d1734f2024-03-08 00:29:14 +030047
48#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
49 /* Check for [-d fdt_addr_r] option. */
50 if ((argc >= 2) && (argv[0][0] == '-') && (argv[0][1] == 'd')) {
51 if (strict_strtoul(argv[1], 16, &fdt_addr) != 0)
52 return CMD_RET_USAGE;
53 /* Consume option. */
54 argc -= 2;
55 argv += 2;
56 }
57#endif
58
Tom Rinibe1b8672017-05-18 17:03:07 -040059 /* Check for address. */
60 if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
61 /* Consume address */
62 argc--; argv++;
63 } else
Simon Glassbb872dd2019-12-28 10:45:02 -070064 addr = image_load_addr;
wdenk458ded32002-09-20 10:59:08 +000065
Maxim Moskalets6d1734f2024-03-08 00:29:14 +030066#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
67 if (fdt_addr) {
Maxim Moskalets46b6a3e2024-06-05 21:43:34 +030068 log_debug("Setting up FDT at 0x%08lx ...\n", fdt_addr);
Maxim Moskalets6d1734f2024-03-08 00:29:14 +030069 flush();
70
71 if (image_setup_libfdt(&img, (void *)fdt_addr, NULL))
72 return 1;
73 }
74#endif
75
Maxim Moskalets46b6a3e2024-06-05 21:43:34 +030076 if (env_get_autostart()) {
77 flags.autostart = 1;
78 log_debug("Starting application at 0x%08lx ...\n", addr);
79 flush();
80 }
wdenk458ded32002-09-20 10:59:08 +000081
wdenk458ded32002-09-20 10:59:08 +000082 /*
83 * pass address parameter as argv[0] (aka command name),
Maxim Moskalets46b6a3e2024-06-05 21:43:34 +030084 * and all remaining arguments
wdenk458ded32002-09-20 10:59:08 +000085 */
Maxim Moskalets46b6a3e2024-06-05 21:43:34 +030086 rc = bootelf(addr, flags, argc, argv);
wdenk458ded32002-09-20 10:59:08 +000087 if (rc != 0)
Maxim Moskalets46b6a3e2024-06-05 21:43:34 +030088 rcode = CMD_RET_FAILURE;
wdenk458ded32002-09-20 10:59:08 +000089
Maxim Moskalets46b6a3e2024-06-05 21:43:34 +030090 if (flags.autostart)
91 {
92 if (ENOEXEC == errno)
93 log_err("Invalid ELF image\n");
94 else
95 log_debug("## Application terminated, rc = 0x%lx\n", rc);
96 }
Bin Mengebca3df2015-10-07 20:19:13 -070097
wdenk458ded32002-09-20 10:59:08 +000098 return rcode;
99}
100
Bin Mengebca3df2015-10-07 20:19:13 -0700101/*
wdenk458ded32002-09-20 10:59:08 +0000102 * Interpreter command to boot VxWorks from a memory image. The image can
103 * be either an ELF image or a raw binary. Will attempt to setup the
104 * bootline and other parameters correctly.
Bin Mengebca3df2015-10-07 20:19:13 -0700105 */
Simon Glass09140112020-05-10 11:40:03 -0600106int do_bootvx(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk458ded32002-09-20 10:59:08 +0000107{
Bin Mengebca3df2015-10-07 20:19:13 -0700108 unsigned long addr; /* Address of image */
Bin Meng79c584e2018-04-11 22:02:22 -0700109 unsigned long bootaddr = 0; /* Address to put the bootline */
Bin Mengebca3df2015-10-07 20:19:13 -0700110 char *bootline; /* Text of the bootline */
111 char *tmp; /* Temporary char pointer */
112 char build_buf[128]; /* Buffer for building the bootline */
Bin Meng7f0c3c52015-10-07 20:19:15 -0700113 int ptr = 0;
Bin Mengb90ff0f2015-10-07 20:19:18 -0700114#ifdef CONFIG_X86
Bin Meng2902be82018-04-11 22:02:07 -0700115 ulong base;
Bin Mengfa5e91f2018-04-11 22:02:09 -0700116 struct e820_info *info;
Bin Meng45519922018-04-11 22:02:11 -0700117 struct e820_entry *data;
Bin Meng447ae4f2018-04-11 22:02:19 -0700118 struct efi_gop_info *gop;
119 struct vesa_mode_info *vesa = &mode_info.vesa;
Bin Mengb90ff0f2015-10-07 20:19:18 -0700120#endif
wdenk458ded32002-09-20 10:59:08 +0000121
Bin Mengebca3df2015-10-07 20:19:13 -0700122 /*
wdenk458ded32002-09-20 10:59:08 +0000123 * Check the loadaddr variable.
124 * If we don't know where the image is then we're done.
125 */
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100126 if (argc < 2)
Simon Glassbb872dd2019-12-28 10:45:02 -0700127 addr = image_load_addr;
Stefan Roese1bdd4682006-11-29 12:53:15 +0100128 else
Simon Glass7e5f4602021-07-24 09:03:29 -0600129 addr = hextoul(argv[1], NULL);
wdenk458ded32002-09-20 10:59:08 +0000130
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500131#if defined(CONFIG_CMD_NET)
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000132 /*
Bin Mengebca3df2015-10-07 20:19:13 -0700133 * Check to see if we need to tftp the image ourselves
134 * before starting
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000135 */
Stany MARCEL07a1a0c2013-11-27 14:48:43 +0100136 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500137 if (net_loop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000138 return 1;
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000139 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
140 addr);
wdenk458ded32002-09-20 10:59:08 +0000141 }
142#endif
143
Bin Mengebca3df2015-10-07 20:19:13 -0700144 /*
145 * This should equate to
146 * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
wdenk458ded32002-09-20 10:59:08 +0000147 * from the VxWorks BSP header files.
148 * This will vary from board to board
149 */
Tuomas Tynkkynen89969752018-01-21 18:16:42 +0200150#if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Bin Mengebca3df2015-10-07 20:19:13 -0700151 tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
Simon Glass35affd72017-08-03 12:22:14 -0600152 eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger62c93d92009-02-11 18:51:43 -0500153 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000154#else
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000155 puts("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000156#endif
157
Bin Meng79c584e2018-04-11 22:02:22 -0700158#ifdef CONFIG_X86
159 /*
160 * Get VxWorks's physical memory base address from environment,
161 * if we don't specify it in the environment, use a default one.
162 */
163 base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
164 data = (struct e820_entry *)(base + E820_DATA_OFFSET);
165 info = (struct e820_info *)(base + E820_INFO_OFFSET);
166
167 memset(info, 0, sizeof(struct e820_info));
168 info->sign = E820_SIGNATURE;
169 info->entries = install_e820_map(E820MAX, data);
170 info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
171 E820_DATA_OFFSET;
172
173 /*
174 * Explicitly clear the bootloader image size otherwise if memory
175 * at this offset happens to contain some garbage data, the final
176 * available memory size for the kernel is insane.
177 */
178 *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
179
180 /*
181 * Prepare compatible framebuffer information block.
182 * The VESA mode has to be 32-bit RGBA.
183 */
184 if (vesa->x_resolution && vesa->y_resolution) {
185 gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET);
186 gop->magic = EFI_GOP_INFO_MAGIC;
187 gop->info.version = 0;
188 gop->info.width = vesa->x_resolution;
189 gop->info.height = vesa->y_resolution;
190 gop->info.pixel_format = EFI_GOT_RGBA8;
191 gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4;
192 gop->fb_base = vesa->phys_base_ptr;
193 gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution;
194 }
195#endif
196
wdenk8bde7f72003-06-27 21:31:46 +0000197 /*
198 * Use bootaddr to find the location in memory that VxWorks
Bin Meng9e98b7e2015-10-07 20:19:17 -0700199 * will look for the bootline string. The default value is
200 * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
201 * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
wdenk458ded32002-09-20 10:59:08 +0000202 */
Simon Glass00caae62017-08-03 12:22:12 -0600203 tmp = env_get("bootaddr");
Bin Meng9e98b7e2015-10-07 20:19:17 -0700204 if (!tmp) {
Bin Meng79c584e2018-04-11 22:02:22 -0700205#ifdef CONFIG_X86
206 bootaddr = base + X86_BOOT_LINE_OFFSET;
207#else
Bin Meng9e98b7e2015-10-07 20:19:17 -0700208 printf("## VxWorks bootline address not specified\n");
Bin Mengced71a22018-04-11 22:02:21 -0700209 return 1;
Bin Meng79c584e2018-04-11 22:02:22 -0700210#endif
Bin Mengced71a22018-04-11 22:02:21 -0700211 }
wdenk458ded32002-09-20 10:59:08 +0000212
Bin Meng79c584e2018-04-11 22:02:22 -0700213 if (!bootaddr)
Simon Glass7e5f4602021-07-24 09:03:29 -0600214 bootaddr = hextoul(tmp, NULL);
wdenk458ded32002-09-20 10:59:08 +0000215
Bin Mengced71a22018-04-11 22:02:21 -0700216 /*
217 * Check to see if the bootline is defined in the 'bootargs' parameter.
218 * If it is not defined, we may be able to construct the info.
219 */
220 bootline = env_get("bootargs");
221 if (!bootline) {
222 tmp = env_get("bootdev");
223 if (tmp) {
224 strcpy(build_buf, tmp);
225 ptr = strlen(tmp);
226 } else {
227 printf("## VxWorks boot device not specified\n");
Bin Menga4092db2015-10-07 20:19:16 -0700228 }
Niklaus Giger29a4c242008-11-03 22:15:34 +0100229
Bin Mengced71a22018-04-11 22:02:21 -0700230 tmp = env_get("bootfile");
231 if (tmp)
232 ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
233 else
234 ptr += sprintf(build_buf + ptr, "host:vxWorks ");
235
236 /*
237 * The following parameters are only needed if 'bootdev'
238 * is an ethernet device, otherwise they are optional.
239 */
240 tmp = env_get("ipaddr");
241 if (tmp) {
242 ptr += sprintf(build_buf + ptr, "e=%s", tmp);
243 tmp = env_get("netmask");
244 if (tmp) {
245 u32 mask = env_get_ip("netmask").s_addr;
246 ptr += sprintf(build_buf + ptr,
247 ":%08x ", ntohl(mask));
248 } else {
249 ptr += sprintf(build_buf + ptr, " ");
250 }
251 }
252
253 tmp = env_get("serverip");
254 if (tmp)
255 ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
256
257 tmp = env_get("gatewayip");
258 if (tmp)
259 ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
260
261 tmp = env_get("hostname");
262 if (tmp)
263 ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
264
265 tmp = env_get("othbootargs");
266 if (tmp) {
267 strcpy(build_buf + ptr, tmp);
268 ptr += strlen(tmp);
269 }
270
271 bootline = build_buf;
wdenk458ded32002-09-20 10:59:08 +0000272 }
273
Bin Mengced71a22018-04-11 22:02:21 -0700274 memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255));
275 flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
276 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr);
277
wdenk8bde7f72003-06-27 21:31:46 +0000278 /*
279 * If the data at the load address is an elf image, then
280 * treat it like an elf image. Otherwise, assume that it is a
Bin Mengebca3df2015-10-07 20:19:13 -0700281 * binary image.
wdenk458ded32002-09-20 10:59:08 +0000282 */
Bin Mengebca3df2015-10-07 20:19:13 -0700283 if (valid_elf_image(addr))
Christian Gmeiner476c2fc2018-03-20 14:18:25 +0100284 addr = load_elf_image_phdr(addr);
Bin Mengebca3df2015-10-07 20:19:13 -0700285 else
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000286 puts("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000287
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000288 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
Pali Rohárefc3f952022-09-05 11:31:21 +0200289 flush();
wdenk458ded32002-09-20 10:59:08 +0000290
Reinhard Arlt6eee21d2011-11-18 09:06:52 +0000291 dcache_disable();
Vasyl Vavrychuk3194daa2018-04-10 12:36:36 +0300292#if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
293 armv8_setup_psci();
294 smp_kick_all_cpus();
295#endif
296
Bin Meng9aa12802015-10-07 20:19:19 -0700297#ifdef CONFIG_X86
298 /* VxWorks on x86 uses stack to pass parameters */
299 ((asmlinkage void (*)(int))addr)(0);
300#else
Bin Mengebca3df2015-10-07 20:19:13 -0700301 ((void (*)(int))addr)(0);
Bin Meng9aa12802015-10-07 20:19:19 -0700302#endif
wdenk458ded32002-09-20 10:59:08 +0000303
Daniel Schwierzeck62e03d32012-09-16 06:55:05 +0000304 puts("## vxWorks terminated\n");
Bin Mengebca3df2015-10-07 20:19:13 -0700305
wdenk458ded32002-09-20 10:59:08 +0000306 return 1;
307}
308
wdenk0d498392003-07-01 21:06:45 +0000309U_BOOT_CMD(
Tom Rinibe1b8672017-05-18 17:03:07 -0400310 bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
Peter Tyser2fb26042009-01-27 18:03:12 -0600311 "Boot from an ELF image in memory",
Maxim Moskalets6d1734f2024-03-08 00:29:14 +0300312 "[-p|-s] "
313#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
314 "[-d fdt_addr_r] "
315#endif
316 "[address]\n"
Mike Frysingerf44a9282010-10-02 15:44:53 -0400317 "\t- load ELF image at [address] via program headers (-p)\n"
Maxim Moskalets6d1734f2024-03-08 00:29:14 +0300318 "\t or via section headers (-s)\n"
319#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
320 "\t- setup FDT image at [fdt_addr_r] (-d)"
321#endif
wdenk8bde7f72003-06-27 21:31:46 +0000322);
323
wdenk0d498392003-07-01 21:06:45 +0000324U_BOOT_CMD(
Bin Mengebca3df2015-10-07 20:19:13 -0700325 bootvx, 2, 0, do_bootvx,
Peter Tyser2fb26042009-01-27 18:03:12 -0600326 "Boot vxWorks from an ELF image",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200327 " [address] - load address of vxWorks ELF image."
wdenk8bde7f72003-06-27 21:31:46 +0000328);