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