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> |
| 18 | #include <linux/ctype.h> |
| 19 | #include <net.h> |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 20 | #include <elf.h> |
| 21 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 22 | #if defined(CONFIG_WALNUT) || defined(CFG_VXWORKS_MAC_PTR) |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | #endif |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 25 | |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 26 | #ifndef MAX |
| 27 | #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
| 28 | #endif |
| 29 | |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 30 | int valid_elf_image (unsigned long addr); |
| 31 | unsigned long load_elf_image (unsigned long addr); |
| 32 | |
| 33 | /* Allow ports to override the default behavior */ |
| 34 | __attribute__((weak)) |
| 35 | unsigned long do_bootelf_exec (ulong (*entry)(int, char *[]), int argc, char *argv[]) |
Mike Frysinger | f79507e | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 36 | { |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 37 | unsigned long ret; |
| 38 | |
Mike Frysinger | f79507e | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 39 | /* |
| 40 | * QNX images require the data cache is disabled. |
| 41 | * Data cache is already flushed, so just turn it off. |
| 42 | */ |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 43 | int dcache = dcache_status (); |
| 44 | if (dcache) |
Mike Frysinger | f79507e | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 45 | dcache_disable (); |
| 46 | |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 47 | /* |
| 48 | * pass address parameter as argv[0] (aka command name), |
| 49 | * and all remaining args |
| 50 | */ |
| 51 | ret = entry (argc, argv); |
Mike Frysinger | f79507e | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 52 | |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 53 | if (dcache) |
| 54 | dcache_enable (); |
| 55 | |
| 56 | return ret; |
| 57 | } |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 58 | |
| 59 | /* ====================================================================== |
| 60 | * Interpreter command to boot an arbitrary ELF image from memory. |
| 61 | * ====================================================================== */ |
| 62 | int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 63 | { |
| 64 | unsigned long addr; /* Address of the ELF image */ |
| 65 | unsigned long rc; /* Return value from user code */ |
| 66 | |
| 67 | /* -------------------------------------------------- */ |
| 68 | int rcode = 0; |
| 69 | |
| 70 | if (argc < 2) |
| 71 | addr = load_addr; |
| 72 | else |
| 73 | addr = simple_strtoul (argv[1], NULL, 16); |
| 74 | |
| 75 | if (!valid_elf_image (addr)) |
| 76 | return 1; |
| 77 | |
| 78 | addr = load_elf_image (addr); |
| 79 | |
| 80 | printf ("## Starting application at 0x%08lx ...\n", addr); |
| 81 | |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 82 | /* |
| 83 | * pass address parameter as argv[0] (aka command name), |
| 84 | * and all remaining args |
| 85 | */ |
Mike Frysinger | e60644a | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 86 | rc = do_bootelf_exec ((void *)addr, argc - 1, argv + 1); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 87 | if (rc != 0) |
| 88 | rcode = 1; |
| 89 | |
| 90 | printf ("## Application terminated, rc = 0x%lx\n", rc); |
| 91 | return rcode; |
| 92 | } |
| 93 | |
| 94 | /* ====================================================================== |
| 95 | * Interpreter command to boot VxWorks from a memory image. The image can |
| 96 | * be either an ELF image or a raw binary. Will attempt to setup the |
| 97 | * bootline and other parameters correctly. |
| 98 | * ====================================================================== */ |
Stefan Roese | 0048778 | 2006-11-29 12:53:15 +0100 | [diff] [blame] | 99 | int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 100 | { |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 101 | unsigned long addr; /* Address of image */ |
| 102 | unsigned long bootaddr; /* Address to put the bootline */ |
| 103 | char *bootline; /* Text of the bootline */ |
| 104 | char *tmp; /* Temporary char pointer */ |
| 105 | |
| 106 | #if defined(CONFIG_4xx) || defined(CONFIG_IOP480) |
| 107 | char build_buf[80]; /* Buffer for building the bootline */ |
| 108 | #endif |
| 109 | /* -------------------------------------------------- */ |
| 110 | |
| 111 | /* |
| 112 | * Check the loadaddr variable. |
| 113 | * If we don't know where the image is then we're done. |
| 114 | */ |
| 115 | |
Stefan Roese | 0048778 | 2006-11-29 12:53:15 +0100 | [diff] [blame] | 116 | if (argc < 2) |
| 117 | addr = load_addr; |
| 118 | else |
| 119 | addr = simple_strtoul (argv[1], NULL, 16); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 120 | |
Jon Loeliger | 54324d0 | 2007-07-08 17:51:39 -0500 | [diff] [blame] | 121 | #if defined(CONFIG_CMD_NET) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 122 | /* Check to see if we need to tftp the image ourselves before starting */ |
| 123 | |
| 124 | if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) { |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 125 | if (NetLoop (TFTP) <= 0) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 126 | return 1; |
| 127 | printf ("Automatic boot of VxWorks image at address 0x%08lx ... \n", addr); |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | /* This should equate |
| 132 | * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET |
| 133 | * from the VxWorks BSP header files. |
| 134 | * This will vary from board to board |
| 135 | */ |
| 136 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 137 | #if defined(CONFIG_WALNUT) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 138 | tmp = (char *) CFG_NVRAM_BASE_ADDR + 0x500; |
| 139 | memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[3], 3); |
stroese | edd3585 | 2004-12-16 17:37:54 +0000 | [diff] [blame] | 140 | #elif defined(CFG_VXWORKS_MAC_PTR) |
| 141 | tmp = (char *) CFG_VXWORKS_MAC_PTR; |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 142 | memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[0], 6); |
| 143 | #else |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 144 | puts ("## Ethernet MAC address not copied to NV RAM\n"); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 145 | #endif |
| 146 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 147 | /* |
| 148 | * Use bootaddr to find the location in memory that VxWorks |
| 149 | * will look for the bootline string. The default value for |
| 150 | * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which |
| 151 | * defaults to 0x4200 |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 152 | */ |
| 153 | |
| 154 | if ((tmp = getenv ("bootaddr")) == NULL) |
| 155 | bootaddr = 0x4200; |
| 156 | else |
| 157 | bootaddr = simple_strtoul (tmp, NULL, 16); |
| 158 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 159 | /* |
| 160 | * Check to see if the bootline is defined in the 'bootargs' |
| 161 | * parameter. If it is not defined, we may be able to |
| 162 | * construct the info |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 163 | */ |
| 164 | |
| 165 | if ((bootline = getenv ("bootargs")) != NULL) { |
| 166 | memcpy ((void *) bootaddr, bootline, MAX(strlen(bootline), 255)); |
| 167 | flush_cache (bootaddr, MAX(strlen(bootline), 255)); |
| 168 | } else { |
| 169 | #if defined(CONFIG_4xx) |
| 170 | sprintf (build_buf, "ibmEmac(0,0)"); |
| 171 | |
| 172 | if ((tmp = getenv ("hostname")) != NULL) { |
| 173 | sprintf (&build_buf[strlen (build_buf - 1)], |
| 174 | "host:%s ", tmp); |
| 175 | } else { |
| 176 | sprintf (&build_buf[strlen (build_buf - 1)], |
| 177 | ": "); |
| 178 | } |
| 179 | |
| 180 | if ((tmp = getenv ("ipaddr")) != NULL) { |
| 181 | sprintf (&build_buf[strlen (build_buf - 1)], |
| 182 | "e=%s ", tmp); |
| 183 | } |
| 184 | memcpy ((void *)bootaddr, build_buf, MAX(strlen(build_buf), 255)); |
| 185 | flush_cache (bootaddr, MAX(strlen(build_buf), 255)); |
| 186 | #elif defined(CONFIG_IOP480) |
| 187 | sprintf (build_buf, "dc(0,0)"); |
| 188 | |
| 189 | if ((tmp = getenv ("hostname")) != NULL) { |
| 190 | sprintf (&build_buf[strlen (build_buf - 1)], |
| 191 | "host:%s ", tmp); |
| 192 | } else { |
| 193 | sprintf (&build_buf[strlen (build_buf - 1)], |
| 194 | ": "); |
| 195 | } |
| 196 | |
| 197 | if ((tmp = getenv ("ipaddr")) != NULL) { |
| 198 | sprintf (&build_buf[strlen (build_buf - 1)], |
| 199 | "e=%s ", tmp); |
| 200 | } |
| 201 | memcpy ((void *) bootaddr, build_buf, MAX(strlen(build_buf), 255)); |
| 202 | flush_cache (bootaddr, MAX(strlen(build_buf), 255)); |
| 203 | #else |
| 204 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 205 | /* |
| 206 | * I'm not sure what the device should be for other |
| 207 | * PPC flavors, the hostname and ipaddr should be ok |
| 208 | * to just copy |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 209 | */ |
| 210 | |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 211 | puts ("No bootargs defined\n"); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 212 | return 1; |
| 213 | #endif |
| 214 | } |
| 215 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 216 | /* |
| 217 | * If the data at the load address is an elf image, then |
| 218 | * treat it like an elf image. Otherwise, assume that it is a |
| 219 | * binary image |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 220 | */ |
| 221 | |
| 222 | if (valid_elf_image (addr)) { |
| 223 | addr = load_elf_image (addr); |
| 224 | } else { |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 225 | puts ("## Not an ELF image, assuming binary\n"); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 226 | /* leave addr as load_addr */ |
| 227 | } |
| 228 | |
| 229 | printf ("## Using bootline (@ 0x%lx): %s\n", bootaddr, |
| 230 | (char *) bootaddr); |
| 231 | printf ("## Starting vxWorks at 0x%08lx ...\n", addr); |
| 232 | |
| 233 | ((void (*)(void)) addr) (); |
| 234 | |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 235 | puts ("## vxWorks terminated\n"); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | /* ====================================================================== |
| 240 | * Determine if a valid ELF image exists at the given memory location. |
| 241 | * First looks at the ELF header magic field, the makes sure that it is |
| 242 | * executable and makes sure that it is for a PowerPC. |
| 243 | * ====================================================================== */ |
| 244 | int valid_elf_image (unsigned long addr) |
| 245 | { |
| 246 | Elf32_Ehdr *ehdr; /* Elf header structure pointer */ |
| 247 | |
| 248 | /* -------------------------------------------------- */ |
| 249 | |
| 250 | ehdr = (Elf32_Ehdr *) addr; |
| 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", |
| 259 | addr); |
| 260 | return 0; |
| 261 | } |
| 262 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 263 | #if 0 |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 264 | if (ehdr->e_machine != EM_PPC) { |
| 265 | printf ("## Not a PowerPC elf image at address 0x%08lx\n", |
| 266 | addr); |
| 267 | return 0; |
| 268 | } |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 269 | #endif |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 270 | |
| 271 | return 1; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | /* ====================================================================== |
| 276 | * A very simple elf loader, assumes the image is valid, returns the |
| 277 | * entry point address. |
| 278 | * ====================================================================== */ |
| 279 | unsigned long load_elf_image (unsigned long addr) |
| 280 | { |
| 281 | Elf32_Ehdr *ehdr; /* Elf header structure pointer */ |
| 282 | Elf32_Shdr *shdr; /* Section header structure pointer */ |
| 283 | unsigned char *strtab = 0; /* String table pointer */ |
| 284 | unsigned char *image; /* Binary image pointer */ |
| 285 | int i; /* Loop counter */ |
| 286 | |
| 287 | /* -------------------------------------------------- */ |
| 288 | |
| 289 | ehdr = (Elf32_Ehdr *) addr; |
| 290 | |
| 291 | /* Find the section header string table for output info */ |
| 292 | shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff + |
| 293 | (ehdr->e_shstrndx * sizeof (Elf32_Shdr))); |
| 294 | |
| 295 | if (shdr->sh_type == SHT_STRTAB) |
| 296 | strtab = (unsigned char *) (addr + shdr->sh_offset); |
| 297 | |
| 298 | /* Load each appropriate section */ |
| 299 | for (i = 0; i < ehdr->e_shnum; ++i) { |
| 300 | shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff + |
| 301 | (i * sizeof (Elf32_Shdr))); |
| 302 | |
| 303 | if (!(shdr->sh_flags & SHF_ALLOC) |
| 304 | || shdr->sh_addr == 0 || shdr->sh_size == 0) { |
| 305 | continue; |
| 306 | } |
| 307 | |
| 308 | if (strtab) { |
| 309 | printf ("%sing %s @ 0x%08lx (%ld bytes)\n", |
| 310 | (shdr->sh_type == SHT_NOBITS) ? |
| 311 | "Clear" : "Load", |
| 312 | &strtab[shdr->sh_name], |
| 313 | (unsigned long) shdr->sh_addr, |
| 314 | (long) shdr->sh_size); |
| 315 | } |
| 316 | |
| 317 | if (shdr->sh_type == SHT_NOBITS) { |
| 318 | memset ((void *)shdr->sh_addr, 0, shdr->sh_size); |
| 319 | } else { |
| 320 | image = (unsigned char *) addr + shdr->sh_offset; |
| 321 | memcpy ((void *) shdr->sh_addr, |
| 322 | (const void *) image, |
| 323 | shdr->sh_size); |
| 324 | } |
| 325 | flush_cache (shdr->sh_addr, shdr->sh_size); |
| 326 | } |
| 327 | |
| 328 | return ehdr->e_entry; |
| 329 | } |
| 330 | |
| 331 | /* ====================================================================== */ |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 332 | U_BOOT_CMD( |
| 333 | bootelf, 2, 0, do_bootelf, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 334 | "bootelf - Boot from an ELF image in memory\n", |
| 335 | " [address] - load address of ELF image.\n" |
| 336 | ); |
| 337 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 338 | U_BOOT_CMD( |
| 339 | bootvx, 2, 0, do_bootvx, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 340 | "bootvx - Boot vxWorks from an ELF image\n", |
| 341 | " [address] - load address of vxWorks ELF image.\n" |
| 342 | ); |