Tom Rini | 8abb4cb | 2020-01-29 11:03:34 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-2-Clause |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2001 William L. Pitts |
| 4 | * All rights reserved. |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
Simon Glass | 370382c | 2019-11-14 12:57:35 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 10 | #include <elf.h> |
Simon Glass | 83c2e49 | 2019-08-01 09:46:50 -0600 | [diff] [blame] | 11 | #include <env.h> |
Simon Glass | 85f1378 | 2019-12-28 10:45:03 -0700 | [diff] [blame] | 12 | #include <image.h> |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 13 | #include <net.h> |
Niklaus Giger | f662b23 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 14 | #include <vxworks.h> |
Bin Meng | 321e860 | 2015-10-07 20:19:18 -0700 | [diff] [blame] | 15 | #ifdef CONFIG_X86 |
Bin Meng | 71606c4 | 2018-04-11 22:02:19 -0700 | [diff] [blame] | 16 | #include <vbe.h> |
Bin Meng | 321e860 | 2015-10-07 20:19:18 -0700 | [diff] [blame] | 17 | #include <asm/e820.h> |
Bin Meng | f6900e2 | 2015-10-07 20:19:19 -0700 | [diff] [blame] | 18 | #include <linux/linkage.h> |
Bin Meng | 321e860 | 2015-10-07 20:19:18 -0700 | [diff] [blame] | 19 | #endif |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 20 | |
Bin Meng | 8c77af6 | 2015-10-07 20:19:14 -0700 | [diff] [blame] | 21 | /* |
Bin Meng | c71073f | 2018-04-11 22:02:14 -0700 | [diff] [blame] | 22 | * A very simple ELF64 loader, assumes the image is valid, returns the |
Bin Meng | 8c77af6 | 2015-10-07 20:19:14 -0700 | [diff] [blame] | 23 | * entry point address. |
Bin Meng | c71073f | 2018-04-11 22:02:14 -0700 | [diff] [blame] | 24 | * |
| 25 | * Note if U-Boot is 32-bit, the loader assumes the to segment's |
| 26 | * physical address and size is within the lower 32-bit address space. |
| 27 | */ |
| 28 | static unsigned long load_elf64_image_phdr(unsigned long addr) |
| 29 | { |
| 30 | Elf64_Ehdr *ehdr; /* Elf header structure pointer */ |
| 31 | Elf64_Phdr *phdr; /* Program header structure pointer */ |
| 32 | int i; |
| 33 | |
| 34 | ehdr = (Elf64_Ehdr *)addr; |
| 35 | phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff); |
| 36 | |
| 37 | /* Load each program header */ |
| 38 | for (i = 0; i < ehdr->e_phnum; ++i) { |
| 39 | void *dst = (void *)(ulong)phdr->p_paddr; |
| 40 | void *src = (void *)addr + phdr->p_offset; |
| 41 | |
| 42 | debug("Loading phdr %i to 0x%p (%lu bytes)\n", |
| 43 | i, dst, (ulong)phdr->p_filesz); |
| 44 | if (phdr->p_filesz) |
| 45 | memcpy(dst, src, phdr->p_filesz); |
| 46 | if (phdr->p_filesz != phdr->p_memsz) |
| 47 | memset(dst + phdr->p_filesz, 0x00, |
| 48 | phdr->p_memsz - phdr->p_filesz); |
Kurban Mallachiev | a064f19 | 2019-02-07 14:19:45 +0300 | [diff] [blame] | 49 | flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN), |
| 50 | roundup(phdr->p_memsz, ARCH_DMA_MINALIGN)); |
Bin Meng | c71073f | 2018-04-11 22:02:14 -0700 | [diff] [blame] | 51 | ++phdr; |
| 52 | } |
| 53 | |
Rob Bracero | 7c8b325 | 2018-07-31 22:57:42 -0400 | [diff] [blame] | 54 | if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags & |
| 55 | EF_PPC64_ELFV1_ABI)) { |
| 56 | /* |
| 57 | * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function |
| 58 | * descriptor pointer with the first double word being the |
| 59 | * address of the entry point of the function. |
| 60 | */ |
| 61 | uintptr_t addr = ehdr->e_entry; |
| 62 | |
| 63 | return *(Elf64_Addr *)addr; |
| 64 | } |
| 65 | |
Bin Meng | c71073f | 2018-04-11 22:02:14 -0700 | [diff] [blame] | 66 | return ehdr->e_entry; |
| 67 | } |
| 68 | |
Rob Bracero | 7c8b325 | 2018-07-31 22:57:42 -0400 | [diff] [blame] | 69 | static unsigned long load_elf64_image_shdr(unsigned long addr) |
| 70 | { |
| 71 | Elf64_Ehdr *ehdr; /* Elf header structure pointer */ |
| 72 | Elf64_Shdr *shdr; /* Section header structure pointer */ |
| 73 | unsigned char *strtab = 0; /* String table pointer */ |
| 74 | unsigned char *image; /* Binary image pointer */ |
| 75 | int i; /* Loop counter */ |
| 76 | |
| 77 | ehdr = (Elf64_Ehdr *)addr; |
| 78 | |
| 79 | /* Find the section header string table for output info */ |
| 80 | shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff + |
| 81 | (ehdr->e_shstrndx * sizeof(Elf64_Shdr))); |
| 82 | |
| 83 | if (shdr->sh_type == SHT_STRTAB) |
| 84 | strtab = (unsigned char *)(addr + (ulong)shdr->sh_offset); |
| 85 | |
| 86 | /* Load each appropriate section */ |
| 87 | for (i = 0; i < ehdr->e_shnum; ++i) { |
| 88 | shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff + |
| 89 | (i * sizeof(Elf64_Shdr))); |
| 90 | |
| 91 | if (!(shdr->sh_flags & SHF_ALLOC) || |
| 92 | shdr->sh_addr == 0 || shdr->sh_size == 0) { |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | if (strtab) { |
| 97 | debug("%sing %s @ 0x%08lx (%ld bytes)\n", |
| 98 | (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load", |
| 99 | &strtab[shdr->sh_name], |
| 100 | (unsigned long)shdr->sh_addr, |
| 101 | (long)shdr->sh_size); |
| 102 | } |
| 103 | |
| 104 | if (shdr->sh_type == SHT_NOBITS) { |
| 105 | memset((void *)(uintptr_t)shdr->sh_addr, 0, |
| 106 | shdr->sh_size); |
| 107 | } else { |
| 108 | image = (unsigned char *)addr + (ulong)shdr->sh_offset; |
| 109 | memcpy((void *)(uintptr_t)shdr->sh_addr, |
| 110 | (const void *)image, shdr->sh_size); |
| 111 | } |
Neil Stainton | 6e91bd9 | 2018-08-20 15:46:19 +0000 | [diff] [blame] | 112 | flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN), |
| 113 | roundup((shdr->sh_addr + shdr->sh_size), |
| 114 | ARCH_DMA_MINALIGN) - |
| 115 | rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN)); |
Rob Bracero | 7c8b325 | 2018-07-31 22:57:42 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags & |
| 119 | EF_PPC64_ELFV1_ABI)) { |
| 120 | /* |
| 121 | * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function |
| 122 | * descriptor pointer with the first double word being the |
| 123 | * address of the entry point of the function. |
| 124 | */ |
| 125 | uintptr_t addr = ehdr->e_entry; |
| 126 | |
| 127 | return *(Elf64_Addr *)addr; |
| 128 | } |
| 129 | |
| 130 | return ehdr->e_entry; |
| 131 | } |
| 132 | |
Bin Meng | c71073f | 2018-04-11 22:02:14 -0700 | [diff] [blame] | 133 | /* |
| 134 | * A very simple ELF loader, assumes the image is valid, returns the |
| 135 | * entry point address. |
| 136 | * |
| 137 | * The loader firstly reads the EFI class to see if it's a 64-bit image. |
| 138 | * If yes, call the ELF64 loader. Otherwise continue with the ELF32 loader. |
Bin Meng | 8c77af6 | 2015-10-07 20:19:14 -0700 | [diff] [blame] | 139 | */ |
| 140 | static unsigned long load_elf_image_phdr(unsigned long addr) |
| 141 | { |
| 142 | Elf32_Ehdr *ehdr; /* Elf header structure pointer */ |
| 143 | Elf32_Phdr *phdr; /* Program header structure pointer */ |
| 144 | int i; |
| 145 | |
| 146 | ehdr = (Elf32_Ehdr *)addr; |
Bin Meng | c71073f | 2018-04-11 22:02:14 -0700 | [diff] [blame] | 147 | if (ehdr->e_ident[EI_CLASS] == ELFCLASS64) |
| 148 | return load_elf64_image_phdr(addr); |
| 149 | |
Bin Meng | 8c77af6 | 2015-10-07 20:19:14 -0700 | [diff] [blame] | 150 | phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff); |
| 151 | |
| 152 | /* Load each program header */ |
| 153 | for (i = 0; i < ehdr->e_phnum; ++i) { |
| 154 | void *dst = (void *)(uintptr_t)phdr->p_paddr; |
| 155 | void *src = (void *)addr + phdr->p_offset; |
Bin Meng | c71073f | 2018-04-11 22:02:14 -0700 | [diff] [blame] | 156 | |
Bin Meng | 8c77af6 | 2015-10-07 20:19:14 -0700 | [diff] [blame] | 157 | debug("Loading phdr %i to 0x%p (%i bytes)\n", |
| 158 | i, dst, phdr->p_filesz); |
| 159 | if (phdr->p_filesz) |
| 160 | memcpy(dst, src, phdr->p_filesz); |
| 161 | if (phdr->p_filesz != phdr->p_memsz) |
| 162 | memset(dst + phdr->p_filesz, 0x00, |
| 163 | phdr->p_memsz - phdr->p_filesz); |
Kurban Mallachiev | a064f19 | 2019-02-07 14:19:45 +0300 | [diff] [blame] | 164 | flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN), |
| 165 | roundup(phdr->p_memsz, ARCH_DMA_MINALIGN)); |
Bin Meng | 8c77af6 | 2015-10-07 20:19:14 -0700 | [diff] [blame] | 166 | ++phdr; |
| 167 | } |
| 168 | |
| 169 | return ehdr->e_entry; |
| 170 | } |
| 171 | |
| 172 | static unsigned long load_elf_image_shdr(unsigned long addr) |
| 173 | { |
| 174 | Elf32_Ehdr *ehdr; /* Elf header structure pointer */ |
| 175 | Elf32_Shdr *shdr; /* Section header structure pointer */ |
| 176 | unsigned char *strtab = 0; /* String table pointer */ |
| 177 | unsigned char *image; /* Binary image pointer */ |
| 178 | int i; /* Loop counter */ |
| 179 | |
| 180 | ehdr = (Elf32_Ehdr *)addr; |
Rob Bracero | 7c8b325 | 2018-07-31 22:57:42 -0400 | [diff] [blame] | 181 | if (ehdr->e_ident[EI_CLASS] == ELFCLASS64) |
| 182 | return load_elf64_image_shdr(addr); |
Bin Meng | 8c77af6 | 2015-10-07 20:19:14 -0700 | [diff] [blame] | 183 | |
| 184 | /* Find the section header string table for output info */ |
| 185 | shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff + |
| 186 | (ehdr->e_shstrndx * sizeof(Elf32_Shdr))); |
| 187 | |
| 188 | if (shdr->sh_type == SHT_STRTAB) |
| 189 | strtab = (unsigned char *)(addr + shdr->sh_offset); |
| 190 | |
| 191 | /* Load each appropriate section */ |
| 192 | for (i = 0; i < ehdr->e_shnum; ++i) { |
| 193 | shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff + |
| 194 | (i * sizeof(Elf32_Shdr))); |
| 195 | |
| 196 | if (!(shdr->sh_flags & SHF_ALLOC) || |
| 197 | shdr->sh_addr == 0 || shdr->sh_size == 0) { |
| 198 | continue; |
| 199 | } |
| 200 | |
| 201 | if (strtab) { |
| 202 | debug("%sing %s @ 0x%08lx (%ld bytes)\n", |
| 203 | (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load", |
| 204 | &strtab[shdr->sh_name], |
| 205 | (unsigned long)shdr->sh_addr, |
| 206 | (long)shdr->sh_size); |
| 207 | } |
| 208 | |
| 209 | if (shdr->sh_type == SHT_NOBITS) { |
| 210 | memset((void *)(uintptr_t)shdr->sh_addr, 0, |
| 211 | shdr->sh_size); |
| 212 | } else { |
| 213 | image = (unsigned char *)addr + shdr->sh_offset; |
| 214 | memcpy((void *)(uintptr_t)shdr->sh_addr, |
| 215 | (const void *)image, shdr->sh_size); |
| 216 | } |
Neil Stainton | f92c83b | 2018-09-19 10:38:07 +0100 | [diff] [blame] | 217 | flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN), |
| 218 | roundup((shdr->sh_addr + shdr->sh_size), |
| 219 | ARCH_DMA_MINALIGN) - |
| 220 | rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN)); |
Bin Meng | 8c77af6 | 2015-10-07 20:19:14 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | return ehdr->e_entry; |
| 224 | } |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 225 | |
| 226 | /* Allow ports to override the default behavior */ |
Jeroen Hofstee | 3cc2a74 | 2014-10-08 22:57:31 +0200 | [diff] [blame] | 227 | static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]), |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 228 | int argc, char * const argv[]) |
Mike Frysinger | f79507e | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 229 | { |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 230 | unsigned long ret; |
| 231 | |
Mike Frysinger | f79507e | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 232 | /* |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 233 | * pass address parameter as argv[0] (aka command name), |
| 234 | * and all remaining args |
| 235 | */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 236 | ret = entry(argc, argv); |
Mike Frysinger | f79507e | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 237 | |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 238 | return ret; |
| 239 | } |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 240 | |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 241 | /* |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 242 | * Determine if a valid ELF image exists at the given memory location. |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 243 | * First look at the ELF header magic field, then make sure that it is |
| 244 | * executable. |
| 245 | */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 246 | int valid_elf_image(unsigned long addr) |
| 247 | { |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 248 | Elf32_Ehdr *ehdr; /* Elf header structure pointer */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 249 | |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 250 | ehdr = (Elf32_Ehdr *)addr; |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 251 | |
| 252 | if (!IS_ELF(*ehdr)) { |
| 253 | printf("## No elf image at address 0x%08lx\n", addr); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | if (ehdr->e_type != ET_EXEC) { |
| 258 | printf("## Not a 32-bit elf image at address 0x%08lx\n", addr); |
| 259 | return 0; |
| 260 | } |
| 261 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 262 | return 1; |
| 263 | } |
| 264 | |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 265 | /* Interpreter command to boot an arbitrary ELF image from memory */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 266 | int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 267 | { |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 268 | unsigned long addr; /* Address of the ELF image */ |
| 269 | unsigned long rc; /* Return value from user code */ |
Tom Rini | 3c95c79 | 2017-05-18 17:03:07 -0400 | [diff] [blame] | 270 | char *sload = NULL; |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 271 | const char *ep = env_get("autostart"); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 272 | int rcode = 0; |
| 273 | |
Tom Rini | 3c95c79 | 2017-05-18 17:03:07 -0400 | [diff] [blame] | 274 | /* Consume 'bootelf' */ |
| 275 | argc--; argv++; |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 276 | |
Tom Rini | 3c95c79 | 2017-05-18 17:03:07 -0400 | [diff] [blame] | 277 | /* Check for flag. */ |
| 278 | if (argc >= 1 && (argv[0][0] == '-' && \ |
| 279 | (argv[0][1] == 'p' || argv[0][1] == 's'))) { |
| 280 | sload = argv[0]; |
| 281 | /* Consume flag. */ |
| 282 | argc--; argv++; |
| 283 | } |
| 284 | /* Check for address. */ |
| 285 | if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) { |
| 286 | /* Consume address */ |
| 287 | argc--; argv++; |
| 288 | } else |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 289 | addr = image_load_addr; |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 290 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 291 | if (!valid_elf_image(addr)) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 292 | return 1; |
| 293 | |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 294 | if (sload && sload[1] == 'p') |
| 295 | addr = load_elf_image_phdr(addr); |
| 296 | else |
| 297 | addr = load_elf_image_shdr(addr); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 298 | |
Siva Durga Prasad Paladugu | 3825bfa | 2015-03-09 12:46:47 +0100 | [diff] [blame] | 299 | if (ep && !strcmp(ep, "no")) |
| 300 | return rcode; |
| 301 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 302 | printf("## Starting application at 0x%08lx ...\n", addr); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 303 | |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 304 | /* |
| 305 | * pass address parameter as argv[0] (aka command name), |
| 306 | * and all remaining args |
| 307 | */ |
Tom Rini | 3c95c79 | 2017-05-18 17:03:07 -0400 | [diff] [blame] | 308 | rc = do_bootelf_exec((void *)addr, argc, argv); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 309 | if (rc != 0) |
| 310 | rcode = 1; |
| 311 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 312 | printf("## Application terminated, rc = 0x%lx\n", rc); |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 313 | |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 314 | return rcode; |
| 315 | } |
| 316 | |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 317 | /* |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 318 | * Interpreter command to boot VxWorks from a memory image. The image can |
| 319 | * be either an ELF image or a raw binary. Will attempt to setup the |
| 320 | * bootline and other parameters correctly. |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 321 | */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 322 | int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 323 | { |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 324 | unsigned long addr; /* Address of image */ |
Bin Meng | 75f4b72 | 2018-04-11 22:02:22 -0700 | [diff] [blame] | 325 | unsigned long bootaddr = 0; /* Address to put the bootline */ |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 326 | char *bootline; /* Text of the bootline */ |
| 327 | char *tmp; /* Temporary char pointer */ |
| 328 | char build_buf[128]; /* Buffer for building the bootline */ |
Bin Meng | 0401c86 | 2015-10-07 20:19:15 -0700 | [diff] [blame] | 329 | int ptr = 0; |
Bin Meng | 321e860 | 2015-10-07 20:19:18 -0700 | [diff] [blame] | 330 | #ifdef CONFIG_X86 |
Bin Meng | e3be890 | 2018-04-11 22:02:07 -0700 | [diff] [blame] | 331 | ulong base; |
Bin Meng | dde4ebb | 2018-04-11 22:02:09 -0700 | [diff] [blame] | 332 | struct e820_info *info; |
Bin Meng | 4b8fc74 | 2018-04-11 22:02:11 -0700 | [diff] [blame] | 333 | struct e820_entry *data; |
Bin Meng | 71606c4 | 2018-04-11 22:02:19 -0700 | [diff] [blame] | 334 | struct efi_gop_info *gop; |
| 335 | struct vesa_mode_info *vesa = &mode_info.vesa; |
Bin Meng | 321e860 | 2015-10-07 20:19:18 -0700 | [diff] [blame] | 336 | #endif |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 337 | |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 338 | /* |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 339 | * Check the loadaddr variable. |
| 340 | * If we don't know where the image is then we're done. |
| 341 | */ |
Stany MARCEL | e5deaf58 | 2013-11-27 14:48:43 +0100 | [diff] [blame] | 342 | if (argc < 2) |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 343 | addr = image_load_addr; |
Stefan Roese | 0048778 | 2006-11-29 12:53:15 +0100 | [diff] [blame] | 344 | else |
Stany MARCEL | e5deaf58 | 2013-11-27 14:48:43 +0100 | [diff] [blame] | 345 | addr = simple_strtoul(argv[1], NULL, 16); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 346 | |
Jon Loeliger | 54324d0 | 2007-07-08 17:51:39 -0500 | [diff] [blame] | 347 | #if defined(CONFIG_CMD_NET) |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 348 | /* |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 349 | * Check to see if we need to tftp the image ourselves |
| 350 | * before starting |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 351 | */ |
Stany MARCEL | e5deaf58 | 2013-11-27 14:48:43 +0100 | [diff] [blame] | 352 | if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) { |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 353 | if (net_loop(TFTPGET) <= 0) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 354 | return 1; |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 355 | printf("Automatic boot of VxWorks image at address 0x%08lx ...\n", |
| 356 | addr); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 357 | } |
| 358 | #endif |
| 359 | |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 360 | /* |
| 361 | * This should equate to |
| 362 | * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 363 | * from the VxWorks BSP header files. |
| 364 | * This will vary from board to board |
| 365 | */ |
Tuomas Tynkkynen | f66276d | 2018-01-21 18:16:42 +0200 | [diff] [blame] | 366 | #if defined(CONFIG_SYS_VXWORKS_MAC_PTR) |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 367 | tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR; |
Simon Glass | 399a9ce | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 368 | eth_env_get_enetaddr("ethaddr", (uchar *)build_buf); |
Mike Frysinger | 8bcee04 | 2009-02-11 18:51:43 -0500 | [diff] [blame] | 369 | memcpy(tmp, build_buf, 6); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 370 | #else |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 371 | puts("## Ethernet MAC address not copied to NV RAM\n"); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 372 | #endif |
| 373 | |
Bin Meng | 75f4b72 | 2018-04-11 22:02:22 -0700 | [diff] [blame] | 374 | #ifdef CONFIG_X86 |
| 375 | /* |
| 376 | * Get VxWorks's physical memory base address from environment, |
| 377 | * if we don't specify it in the environment, use a default one. |
| 378 | */ |
| 379 | base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE); |
| 380 | data = (struct e820_entry *)(base + E820_DATA_OFFSET); |
| 381 | info = (struct e820_info *)(base + E820_INFO_OFFSET); |
| 382 | |
| 383 | memset(info, 0, sizeof(struct e820_info)); |
| 384 | info->sign = E820_SIGNATURE; |
| 385 | info->entries = install_e820_map(E820MAX, data); |
| 386 | info->addr = (info->entries - 1) * sizeof(struct e820_entry) + |
| 387 | E820_DATA_OFFSET; |
| 388 | |
| 389 | /* |
| 390 | * Explicitly clear the bootloader image size otherwise if memory |
| 391 | * at this offset happens to contain some garbage data, the final |
| 392 | * available memory size for the kernel is insane. |
| 393 | */ |
| 394 | *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0; |
| 395 | |
| 396 | /* |
| 397 | * Prepare compatible framebuffer information block. |
| 398 | * The VESA mode has to be 32-bit RGBA. |
| 399 | */ |
| 400 | if (vesa->x_resolution && vesa->y_resolution) { |
| 401 | gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET); |
| 402 | gop->magic = EFI_GOP_INFO_MAGIC; |
| 403 | gop->info.version = 0; |
| 404 | gop->info.width = vesa->x_resolution; |
| 405 | gop->info.height = vesa->y_resolution; |
| 406 | gop->info.pixel_format = EFI_GOT_RGBA8; |
| 407 | gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4; |
| 408 | gop->fb_base = vesa->phys_base_ptr; |
| 409 | gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution; |
| 410 | } |
| 411 | #endif |
| 412 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 413 | /* |
| 414 | * Use bootaddr to find the location in memory that VxWorks |
Bin Meng | fb694b9 | 2015-10-07 20:19:17 -0700 | [diff] [blame] | 415 | * will look for the bootline string. The default value is |
| 416 | * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by |
| 417 | * VxWorks BSP. For example, on PowerPC it defaults to 0x4200. |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 418 | */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 419 | tmp = env_get("bootaddr"); |
Bin Meng | fb694b9 | 2015-10-07 20:19:17 -0700 | [diff] [blame] | 420 | if (!tmp) { |
Bin Meng | 75f4b72 | 2018-04-11 22:02:22 -0700 | [diff] [blame] | 421 | #ifdef CONFIG_X86 |
| 422 | bootaddr = base + X86_BOOT_LINE_OFFSET; |
| 423 | #else |
Bin Meng | fb694b9 | 2015-10-07 20:19:17 -0700 | [diff] [blame] | 424 | printf("## VxWorks bootline address not specified\n"); |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 425 | return 1; |
Bin Meng | 75f4b72 | 2018-04-11 22:02:22 -0700 | [diff] [blame] | 426 | #endif |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Bin Meng | 75f4b72 | 2018-04-11 22:02:22 -0700 | [diff] [blame] | 429 | if (!bootaddr) |
| 430 | bootaddr = simple_strtoul(tmp, NULL, 16); |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 431 | |
| 432 | /* |
| 433 | * Check to see if the bootline is defined in the 'bootargs' parameter. |
| 434 | * If it is not defined, we may be able to construct the info. |
| 435 | */ |
| 436 | bootline = env_get("bootargs"); |
| 437 | if (!bootline) { |
| 438 | tmp = env_get("bootdev"); |
| 439 | if (tmp) { |
| 440 | strcpy(build_buf, tmp); |
| 441 | ptr = strlen(tmp); |
| 442 | } else { |
| 443 | printf("## VxWorks boot device not specified\n"); |
| 444 | } |
| 445 | |
| 446 | tmp = env_get("bootfile"); |
| 447 | if (tmp) |
| 448 | ptr += sprintf(build_buf + ptr, "host:%s ", tmp); |
| 449 | else |
| 450 | ptr += sprintf(build_buf + ptr, "host:vxWorks "); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 451 | |
Bin Meng | fb694b9 | 2015-10-07 20:19:17 -0700 | [diff] [blame] | 452 | /* |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 453 | * The following parameters are only needed if 'bootdev' |
| 454 | * is an ethernet device, otherwise they are optional. |
Bin Meng | fb694b9 | 2015-10-07 20:19:17 -0700 | [diff] [blame] | 455 | */ |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 456 | tmp = env_get("ipaddr"); |
| 457 | if (tmp) { |
| 458 | ptr += sprintf(build_buf + ptr, "e=%s", tmp); |
| 459 | tmp = env_get("netmask"); |
Ben Whitten | 34fd6c9 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 460 | if (tmp) { |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 461 | u32 mask = env_get_ip("netmask").s_addr; |
Bin Meng | fb694b9 | 2015-10-07 20:19:17 -0700 | [diff] [blame] | 462 | ptr += sprintf(build_buf + ptr, |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 463 | ":%08x ", ntohl(mask)); |
| 464 | } else { |
| 465 | ptr += sprintf(build_buf + ptr, " "); |
Bin Meng | bf0ba25 | 2015-10-07 20:19:16 -0700 | [diff] [blame] | 466 | } |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 467 | } |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 468 | |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 469 | tmp = env_get("serverip"); |
| 470 | if (tmp) |
| 471 | ptr += sprintf(build_buf + ptr, "h=%s ", tmp); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 472 | |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 473 | tmp = env_get("gatewayip"); |
| 474 | if (tmp) |
| 475 | ptr += sprintf(build_buf + ptr, "g=%s ", tmp); |
Bin Meng | bf0ba25 | 2015-10-07 20:19:16 -0700 | [diff] [blame] | 476 | |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 477 | tmp = env_get("hostname"); |
| 478 | if (tmp) |
| 479 | ptr += sprintf(build_buf + ptr, "tn=%s ", tmp); |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 480 | |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 481 | tmp = env_get("othbootargs"); |
| 482 | if (tmp) { |
| 483 | strcpy(build_buf + ptr, tmp); |
| 484 | ptr += strlen(tmp); |
Bin Meng | fb694b9 | 2015-10-07 20:19:17 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 487 | bootline = build_buf; |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Bin Meng | 051f95e | 2018-04-11 22:02:21 -0700 | [diff] [blame] | 490 | memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255)); |
| 491 | flush_cache(bootaddr, max(strlen(bootline), (size_t)255)); |
| 492 | printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr); |
| 493 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 494 | /* |
| 495 | * If the data at the load address is an elf image, then |
| 496 | * treat it like an elf image. Otherwise, assume that it is a |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 497 | * binary image. |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 498 | */ |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 499 | if (valid_elf_image(addr)) |
Christian Gmeiner | ae7d4be | 2018-03-20 14:18:25 +0100 | [diff] [blame] | 500 | addr = load_elf_image_phdr(addr); |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 501 | else |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 502 | puts("## Not an ELF image, assuming binary\n"); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 503 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 504 | printf("## Starting vxWorks at 0x%08lx ...\n", addr); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 505 | |
Reinhard Arlt | 4fa117c | 2011-11-18 09:06:52 +0000 | [diff] [blame] | 506 | dcache_disable(); |
Vasyl Vavrychuk | 70b551c | 2018-04-10 12:36:36 +0300 | [diff] [blame] | 507 | #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI) |
| 508 | armv8_setup_psci(); |
| 509 | smp_kick_all_cpus(); |
| 510 | #endif |
| 511 | |
Bin Meng | f6900e2 | 2015-10-07 20:19:19 -0700 | [diff] [blame] | 512 | #ifdef CONFIG_X86 |
| 513 | /* VxWorks on x86 uses stack to pass parameters */ |
| 514 | ((asmlinkage void (*)(int))addr)(0); |
| 515 | #else |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 516 | ((void (*)(int))addr)(0); |
Bin Meng | f6900e2 | 2015-10-07 20:19:19 -0700 | [diff] [blame] | 517 | #endif |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 518 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 519 | puts("## vxWorks terminated\n"); |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 520 | |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 521 | return 1; |
| 522 | } |
| 523 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 524 | U_BOOT_CMD( |
Tom Rini | 3c95c79 | 2017-05-18 17:03:07 -0400 | [diff] [blame] | 525 | bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 526 | "Boot from an ELF image in memory", |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 527 | "[-p|-s] [address]\n" |
| 528 | "\t- load ELF image at [address] via program headers (-p)\n" |
| 529 | "\t or via section headers (-s)" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 530 | ); |
| 531 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 532 | U_BOOT_CMD( |
Bin Meng | 53f3afb | 2015-10-07 20:19:13 -0700 | [diff] [blame] | 533 | bootvx, 2, 0, do_bootvx, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 534 | "Boot vxWorks from an ELF image", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 535 | " [address] - load address of vxWorks ELF image." |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 536 | ); |