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