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> |
Jeroen Hofstee | b869cae | 2014-10-08 22:57:51 +0200 | [diff] [blame] | 17 | #include <bootm.h> |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 18 | #include <command.h> |
| 19 | #include <linux/ctype.h> |
| 20 | #include <net.h> |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 21 | #include <elf.h> |
Niklaus Giger | f662b23 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 22 | #include <vxworks.h> |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 23 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 24 | #if defined(CONFIG_WALNUT) || defined(CONFIG_SYS_VXWORKS_MAC_PTR) |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | #endif |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 27 | |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 28 | static unsigned long load_elf_image_phdr(unsigned long addr); |
| 29 | static unsigned long load_elf_image_shdr(unsigned long addr); |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 30 | |
| 31 | /* Allow ports to override the default behavior */ |
Jeroen Hofstee | 3cc2a74 | 2014-10-08 22:57:31 +0200 | [diff] [blame] | 32 | static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]), |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 33 | int argc, char * const argv[]) |
Mike Frysinger | f79507e | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 34 | { |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 35 | unsigned long ret; |
| 36 | |
Mike Frysinger | f79507e | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 37 | /* |
| 38 | * QNX images require the data cache is disabled. |
| 39 | * Data cache is already flushed, so just turn it off. |
| 40 | */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 41 | int dcache = dcache_status(); |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 42 | if (dcache) |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 43 | dcache_disable(); |
Mike Frysinger | f79507e | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 44 | |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 45 | /* |
| 46 | * pass address parameter as argv[0] (aka command name), |
| 47 | * and all remaining args |
| 48 | */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 49 | ret = entry(argc, argv); |
Mike Frysinger | f79507e | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 50 | |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 51 | if (dcache) |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 52 | dcache_enable(); |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 53 | |
| 54 | return ret; |
| 55 | } |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 56 | |
| 57 | /* ====================================================================== |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 58 | * Determine if a valid ELF image exists at the given memory location. |
| 59 | * First looks at the ELF header magic field, the makes sure that it is |
| 60 | * executable and makes sure that it is for a PowerPC. |
| 61 | * ====================================================================== */ |
| 62 | int valid_elf_image(unsigned long addr) |
| 63 | { |
| 64 | Elf32_Ehdr *ehdr; /* Elf header structure pointer */ |
| 65 | |
| 66 | /* -------------------------------------------------- */ |
| 67 | |
| 68 | ehdr = (Elf32_Ehdr *) addr; |
| 69 | |
| 70 | if (!IS_ELF(*ehdr)) { |
| 71 | printf("## No elf image at address 0x%08lx\n", addr); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | if (ehdr->e_type != ET_EXEC) { |
| 76 | printf("## Not a 32-bit elf image at address 0x%08lx\n", addr); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | #if 0 |
| 81 | if (ehdr->e_machine != EM_PPC) { |
| 82 | printf("## Not a PowerPC elf image at address 0x%08lx\n", addr); |
| 83 | return 0; |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | /* ====================================================================== |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 91 | * Interpreter command to boot an arbitrary ELF image from memory. |
| 92 | * ====================================================================== */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 93 | 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] | 94 | { |
| 95 | unsigned long addr; /* Address of the ELF image */ |
| 96 | unsigned long rc; /* Return value from user code */ |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 97 | char *sload, *saddr; |
Siva Durga Prasad Paladugu | 3825bfa | 2015-03-09 12:46:47 +0100 | [diff] [blame] | 98 | const char *ep = getenv("autostart"); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 99 | |
| 100 | /* -------------------------------------------------- */ |
| 101 | int rcode = 0; |
| 102 | |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 103 | sload = saddr = NULL; |
| 104 | if (argc == 3) { |
| 105 | sload = argv[1]; |
| 106 | saddr = argv[2]; |
| 107 | } else if (argc == 2) { |
| 108 | if (argv[1][0] == '-') |
| 109 | sload = argv[1]; |
| 110 | else |
| 111 | saddr = argv[1]; |
| 112 | } |
| 113 | |
| 114 | if (saddr) |
| 115 | addr = simple_strtoul(saddr, NULL, 16); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 116 | else |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 117 | addr = load_addr; |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 118 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 119 | if (!valid_elf_image(addr)) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 120 | return 1; |
| 121 | |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 122 | if (sload && sload[1] == 'p') |
| 123 | addr = load_elf_image_phdr(addr); |
| 124 | else |
| 125 | addr = load_elf_image_shdr(addr); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 126 | |
Siva Durga Prasad Paladugu | 3825bfa | 2015-03-09 12:46:47 +0100 | [diff] [blame] | 127 | if (ep && !strcmp(ep, "no")) |
| 128 | return rcode; |
| 129 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 130 | printf("## Starting application at 0x%08lx ...\n", addr); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 131 | |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 132 | /* |
| 133 | * pass address parameter as argv[0] (aka command name), |
| 134 | * and all remaining args |
| 135 | */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 136 | rc = do_bootelf_exec((void *)addr, argc - 1, argv + 1); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 137 | if (rc != 0) |
| 138 | rcode = 1; |
| 139 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 140 | printf("## Application terminated, rc = 0x%lx\n", rc); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 141 | return rcode; |
| 142 | } |
| 143 | |
| 144 | /* ====================================================================== |
| 145 | * Interpreter command to boot VxWorks from a memory image. The image can |
| 146 | * be either an ELF image or a raw binary. Will attempt to setup the |
| 147 | * bootline and other parameters correctly. |
| 148 | * ====================================================================== */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 149 | 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] | 150 | { |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 151 | unsigned long addr; /* Address of image */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 152 | unsigned long bootaddr; /* Address to put the bootline */ |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 153 | char *bootline; /* Text of the bootline */ |
| 154 | char *tmp; /* Temporary char pointer */ |
Niklaus Giger | f662b23 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 155 | char build_buf[128]; /* Buffer for building the bootline */ |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 156 | |
Niklaus Giger | f662b23 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 157 | /* --------------------------------------------------- |
| 158 | * |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 159 | * Check the loadaddr variable. |
| 160 | * If we don't know where the image is then we're done. |
| 161 | */ |
| 162 | |
Stany MARCEL | e5deaf58 | 2013-11-27 14:48:43 +0100 | [diff] [blame] | 163 | if (argc < 2) |
Stefan Roese | 0048778 | 2006-11-29 12:53:15 +0100 | [diff] [blame] | 164 | addr = load_addr; |
| 165 | else |
Stany MARCEL | e5deaf58 | 2013-11-27 14:48:43 +0100 | [diff] [blame] | 166 | addr = simple_strtoul(argv[1], NULL, 16); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 167 | |
Jon Loeliger | 54324d0 | 2007-07-08 17:51:39 -0500 | [diff] [blame] | 168 | #if defined(CONFIG_CMD_NET) |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 169 | /* |
| 170 | * Check to see if we need to tftp the image ourselves before starting |
| 171 | */ |
Stany MARCEL | e5deaf58 | 2013-11-27 14:48:43 +0100 | [diff] [blame] | 172 | if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) { |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame^] | 173 | if (net_loop(TFTPGET) <= 0) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 174 | return 1; |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 175 | printf("Automatic boot of VxWorks image at address 0x%08lx ...\n", |
| 176 | addr); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 177 | } |
| 178 | #endif |
| 179 | |
| 180 | /* This should equate |
| 181 | * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET |
| 182 | * from the VxWorks BSP header files. |
| 183 | * This will vary from board to board |
| 184 | */ |
| 185 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 186 | #if defined(CONFIG_WALNUT) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 187 | tmp = (char *) CONFIG_SYS_NVRAM_BASE_ADDR + 0x500; |
Heiko Schocher | 50219e6 | 2009-03-26 07:33:59 +0100 | [diff] [blame] | 188 | eth_getenv_enetaddr("ethaddr", (uchar *)build_buf); |
Mike Frysinger | 8bcee04 | 2009-02-11 18:51:43 -0500 | [diff] [blame] | 189 | memcpy(tmp, &build_buf[3], 3); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 190 | #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR) |
| 191 | tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR; |
Heiko Schocher | 50219e6 | 2009-03-26 07:33:59 +0100 | [diff] [blame] | 192 | eth_getenv_enetaddr("ethaddr", (uchar *)build_buf); |
Mike Frysinger | 8bcee04 | 2009-02-11 18:51:43 -0500 | [diff] [blame] | 193 | memcpy(tmp, build_buf, 6); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 194 | #else |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 195 | puts("## Ethernet MAC address not copied to NV RAM\n"); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 196 | #endif |
| 197 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 198 | /* |
| 199 | * Use bootaddr to find the location in memory that VxWorks |
| 200 | * will look for the bootline string. The default value for |
| 201 | * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which |
| 202 | * defaults to 0x4200 |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 203 | */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 204 | tmp = getenv("bootaddr"); |
Reinhard Arlt | 41cfa2d | 2013-02-04 09:37:11 +0000 | [diff] [blame] | 205 | if (!tmp) |
Niklaus Giger | f662b23 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 206 | bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR; |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 207 | else |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 208 | bootaddr = simple_strtoul(tmp, NULL, 16); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 209 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 210 | /* |
| 211 | * Check to see if the bootline is defined in the 'bootargs' |
| 212 | * parameter. If it is not defined, we may be able to |
| 213 | * construct the info |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 214 | */ |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 215 | bootline = getenv("bootargs"); |
| 216 | if (bootline) { |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 217 | memcpy((void *)bootaddr, bootline, |
| 218 | max(strlen(bootline), (size_t)255)); |
| 219 | flush_cache(bootaddr, max(strlen(bootline), (size_t)255)); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 220 | } else { |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 221 | sprintf(build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE); |
| 222 | tmp = getenv("bootfile"); |
| 223 | if (tmp) |
| 224 | sprintf(&build_buf[strlen(build_buf)], |
Niklaus Giger | f662b23 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 225 | "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp); |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 226 | else |
| 227 | sprintf(&build_buf[strlen(build_buf)], |
Niklaus Giger | f662b23 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 228 | "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 229 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 230 | tmp = getenv("ipaddr"); |
| 231 | if (tmp) |
| 232 | sprintf(&build_buf[strlen(build_buf)], "e=%s ", tmp); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 233 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 234 | tmp = getenv("serverip"); |
| 235 | if (tmp) |
| 236 | sprintf(&build_buf[strlen(build_buf)], "h=%s ", tmp); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 237 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 238 | tmp = getenv("hostname"); |
| 239 | if (tmp) |
| 240 | sprintf(&build_buf[strlen(build_buf)], "tn=%s ", tmp); |
| 241 | |
Niklaus Giger | f662b23 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 242 | #ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 243 | sprintf(&build_buf[strlen(build_buf)], |
Niklaus Giger | f662b23 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 244 | CONFIG_SYS_VXWORKS_ADD_PARAMS); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 245 | #endif |
Niklaus Giger | f662b23 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 246 | |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 247 | memcpy((void *)bootaddr, build_buf, |
| 248 | max(strlen(build_buf), (size_t)255)); |
| 249 | flush_cache(bootaddr, max(strlen(build_buf), (size_t)255)); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 250 | } |
| 251 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 252 | /* |
| 253 | * If the data at the load address is an elf image, then |
| 254 | * treat it like an elf image. Otherwise, assume that it is a |
| 255 | * binary image |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 256 | */ |
| 257 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 258 | if (valid_elf_image(addr)) { |
| 259 | addr = load_elf_image_shdr(addr); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 260 | } else { |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 261 | puts("## Not an ELF image, assuming binary\n"); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 262 | /* leave addr as load_addr */ |
| 263 | } |
| 264 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 265 | printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 266 | (char *) bootaddr); |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 267 | printf("## Starting vxWorks at 0x%08lx ...\n", addr); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 268 | |
Reinhard Arlt | 4fa117c | 2011-11-18 09:06:52 +0000 | [diff] [blame] | 269 | dcache_disable(); |
| 270 | ((void (*)(int)) addr) (0); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 271 | |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 272 | puts("## vxWorks terminated\n"); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 273 | return 1; |
| 274 | } |
| 275 | |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 276 | /* ====================================================================== |
| 277 | * A very simple elf loader, assumes the image is valid, returns the |
| 278 | * entry point address. |
| 279 | * ====================================================================== */ |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 280 | static unsigned long load_elf_image_phdr(unsigned long addr) |
| 281 | { |
| 282 | Elf32_Ehdr *ehdr; /* Elf header structure pointer */ |
| 283 | Elf32_Phdr *phdr; /* Program header structure pointer */ |
| 284 | int i; |
| 285 | |
| 286 | ehdr = (Elf32_Ehdr *) addr; |
| 287 | phdr = (Elf32_Phdr *) (addr + ehdr->e_phoff); |
| 288 | |
| 289 | /* Load each program header */ |
| 290 | for (i = 0; i < ehdr->e_phnum; ++i) { |
Daniel Schwierzeck | c336f87 | 2012-09-16 06:55:06 +0000 | [diff] [blame] | 291 | void *dst = (void *)(uintptr_t) phdr->p_paddr; |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 292 | void *src = (void *) addr + phdr->p_offset; |
| 293 | debug("Loading phdr %i to 0x%p (%i bytes)\n", |
| 294 | i, dst, phdr->p_filesz); |
| 295 | if (phdr->p_filesz) |
| 296 | memcpy(dst, src, phdr->p_filesz); |
| 297 | if (phdr->p_filesz != phdr->p_memsz) |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 298 | memset(dst + phdr->p_filesz, 0x00, |
| 299 | phdr->p_memsz - phdr->p_filesz); |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 300 | flush_cache((unsigned long)dst, phdr->p_filesz); |
| 301 | ++phdr; |
| 302 | } |
| 303 | |
| 304 | return ehdr->e_entry; |
| 305 | } |
| 306 | |
| 307 | static unsigned long load_elf_image_shdr(unsigned long addr) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 308 | { |
| 309 | Elf32_Ehdr *ehdr; /* Elf header structure pointer */ |
| 310 | Elf32_Shdr *shdr; /* Section header structure pointer */ |
| 311 | unsigned char *strtab = 0; /* String table pointer */ |
| 312 | unsigned char *image; /* Binary image pointer */ |
| 313 | int i; /* Loop counter */ |
| 314 | |
| 315 | /* -------------------------------------------------- */ |
| 316 | |
| 317 | ehdr = (Elf32_Ehdr *) addr; |
| 318 | |
| 319 | /* Find the section header string table for output info */ |
| 320 | shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff + |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 321 | (ehdr->e_shstrndx * sizeof(Elf32_Shdr))); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 322 | |
| 323 | if (shdr->sh_type == SHT_STRTAB) |
| 324 | strtab = (unsigned char *) (addr + shdr->sh_offset); |
| 325 | |
| 326 | /* Load each appropriate section */ |
| 327 | for (i = 0; i < ehdr->e_shnum; ++i) { |
| 328 | shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff + |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 329 | (i * sizeof(Elf32_Shdr))); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 330 | |
| 331 | if (!(shdr->sh_flags & SHF_ALLOC) |
| 332 | || shdr->sh_addr == 0 || shdr->sh_size == 0) { |
| 333 | continue; |
| 334 | } |
| 335 | |
| 336 | if (strtab) { |
Niklaus Giger | daff3fe | 2009-07-23 23:31:58 +0200 | [diff] [blame] | 337 | debug("%sing %s @ 0x%08lx (%ld bytes)\n", |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 338 | (shdr->sh_type == SHT_NOBITS) ? |
| 339 | "Clear" : "Load", |
| 340 | &strtab[shdr->sh_name], |
| 341 | (unsigned long) shdr->sh_addr, |
| 342 | (long) shdr->sh_size); |
| 343 | } |
| 344 | |
| 345 | if (shdr->sh_type == SHT_NOBITS) { |
Daniel Schwierzeck | c336f87 | 2012-09-16 06:55:06 +0000 | [diff] [blame] | 346 | memset((void *)(uintptr_t) shdr->sh_addr, 0, |
| 347 | shdr->sh_size); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 348 | } else { |
| 349 | image = (unsigned char *) addr + shdr->sh_offset; |
Daniel Schwierzeck | c336f87 | 2012-09-16 06:55:06 +0000 | [diff] [blame] | 350 | memcpy((void *)(uintptr_t) shdr->sh_addr, |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 351 | (const void *) image, |
| 352 | shdr->sh_size); |
| 353 | } |
Daniel Schwierzeck | fa87af3 | 2012-09-16 06:55:05 +0000 | [diff] [blame] | 354 | flush_cache(shdr->sh_addr, shdr->sh_size); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | return ehdr->e_entry; |
| 358 | } |
| 359 | |
| 360 | /* ====================================================================== */ |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 361 | U_BOOT_CMD( |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 362 | bootelf, 3, 0, do_bootelf, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 363 | "Boot from an ELF image in memory", |
Mike Frysinger | e6dc29f | 2010-10-02 15:44:53 -0400 | [diff] [blame] | 364 | "[-p|-s] [address]\n" |
| 365 | "\t- load ELF image at [address] via program headers (-p)\n" |
| 366 | "\t or via section headers (-s)" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 367 | ); |
| 368 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 369 | U_BOOT_CMD( |
| 370 | bootvx, 2, 0, do_bootvx, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 371 | "Boot vxWorks from an ELF image", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 372 | " [address] - load address of vxWorks ELF image." |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 373 | ); |