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