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