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> |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 21 | #include <vxworks.h> |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 22 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 23 | #if defined(CONFIG_WALNUT) || defined(CONFIG_SYS_VXWORKS_MAC_PTR) |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | #endif |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 26 | |
Mike Frysinger | 017e9b7 | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 27 | int valid_elf_image (unsigned long addr); |
| 28 | unsigned long load_elf_image (unsigned long addr); |
| 29 | |
| 30 | /* Allow ports to override the default behavior */ |
| 31 | __attribute__((weak)) |
| 32 | unsigned long do_bootelf_exec (ulong (*entry)(int, char *[]), int argc, char *argv[]) |
Mike Frysinger | 1f1d88d | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 33 | { |
Mike Frysinger | 017e9b7 | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 34 | unsigned long ret; |
| 35 | |
Mike Frysinger | 1f1d88d | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 36 | /* |
| 37 | * QNX images require the data cache is disabled. |
| 38 | * Data cache is already flushed, so just turn it off. |
| 39 | */ |
Mike Frysinger | 017e9b7 | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 40 | int dcache = dcache_status (); |
| 41 | if (dcache) |
Mike Frysinger | 1f1d88d | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 42 | dcache_disable (); |
| 43 | |
Mike Frysinger | 017e9b7 | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 44 | /* |
| 45 | * pass address parameter as argv[0] (aka command name), |
| 46 | * and all remaining args |
| 47 | */ |
| 48 | ret = entry (argc, argv); |
Mike Frysinger | 1f1d88d | 2008-01-29 18:21:05 -0500 | [diff] [blame] | 49 | |
Mike Frysinger | 017e9b7 | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 50 | if (dcache) |
| 51 | dcache_enable (); |
| 52 | |
| 53 | return ret; |
| 54 | } |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 55 | |
| 56 | /* ====================================================================== |
| 57 | * Interpreter command to boot an arbitrary ELF image from memory. |
| 58 | * ====================================================================== */ |
| 59 | int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 60 | { |
| 61 | unsigned long addr; /* Address of the ELF image */ |
| 62 | unsigned long rc; /* Return value from user code */ |
| 63 | |
| 64 | /* -------------------------------------------------- */ |
| 65 | int rcode = 0; |
| 66 | |
| 67 | if (argc < 2) |
| 68 | addr = load_addr; |
| 69 | else |
| 70 | addr = simple_strtoul (argv[1], NULL, 16); |
| 71 | |
| 72 | if (!valid_elf_image (addr)) |
| 73 | return 1; |
| 74 | |
| 75 | addr = load_elf_image (addr); |
| 76 | |
| 77 | printf ("## Starting application at 0x%08lx ...\n", addr); |
| 78 | |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 79 | /* |
| 80 | * pass address parameter as argv[0] (aka command name), |
| 81 | * and all remaining args |
| 82 | */ |
Mike Frysinger | 017e9b7 | 2008-04-13 19:42:18 -0400 | [diff] [blame] | 83 | rc = do_bootelf_exec ((void *)addr, argc - 1, argv + 1); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 84 | if (rc != 0) |
| 85 | rcode = 1; |
| 86 | |
| 87 | printf ("## Application terminated, rc = 0x%lx\n", rc); |
| 88 | return rcode; |
| 89 | } |
| 90 | |
| 91 | /* ====================================================================== |
| 92 | * Interpreter command to boot VxWorks from a memory image. The image can |
| 93 | * be either an ELF image or a raw binary. Will attempt to setup the |
| 94 | * bootline and other parameters correctly. |
| 95 | * ====================================================================== */ |
Stefan Roese | 1bdd468 | 2006-11-29 12:53:15 +0100 | [diff] [blame] | 96 | int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 97 | { |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 98 | unsigned long addr; /* Address of image */ |
| 99 | unsigned long bootaddr; /* Address to put the bootline */ |
| 100 | char *bootline; /* Text of the bootline */ |
| 101 | char *tmp; /* Temporary char pointer */ |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 102 | char build_buf[128]; /* Buffer for building the bootline */ |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 103 | |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 104 | /* --------------------------------------------------- |
| 105 | * |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 106 | * Check the loadaddr variable. |
| 107 | * If we don't know where the image is then we're done. |
| 108 | */ |
| 109 | |
Stefan Roese | 1bdd468 | 2006-11-29 12:53:15 +0100 | [diff] [blame] | 110 | if (argc < 2) |
| 111 | addr = load_addr; |
| 112 | else |
| 113 | addr = simple_strtoul (argv[1], NULL, 16); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 114 | |
Jon Loeliger | baa26db | 2007-07-08 17:51:39 -0500 | [diff] [blame] | 115 | #if defined(CONFIG_CMD_NET) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 116 | /* Check to see if we need to tftp the image ourselves before starting */ |
| 117 | |
| 118 | if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) { |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 119 | if (NetLoop (TFTP) <= 0) |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 120 | return 1; |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 121 | printf ("Automatic boot of VxWorks image at address 0x%08lx ... \n", |
| 122 | addr); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 123 | } |
| 124 | #endif |
| 125 | |
| 126 | /* This should equate |
| 127 | * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET |
| 128 | * from the VxWorks BSP header files. |
| 129 | * This will vary from board to board |
| 130 | */ |
| 131 | |
Stefan Roese | c157d8e | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 132 | #if defined(CONFIG_WALNUT) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 133 | tmp = (char *) CONFIG_SYS_NVRAM_BASE_ADDR + 0x500; |
Heiko Schocher | 76756e4 | 2009-03-26 07:33:59 +0100 | [diff] [blame] | 134 | eth_getenv_enetaddr("ethaddr", (uchar *)build_buf); |
Mike Frysinger | 62c93d9 | 2009-02-11 18:51:43 -0500 | [diff] [blame] | 135 | memcpy(tmp, &build_buf[3], 3); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 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; |
Heiko Schocher | 76756e4 | 2009-03-26 07:33:59 +0100 | [diff] [blame] | 138 | eth_getenv_enetaddr("ethaddr", (uchar *)build_buf); |
Mike Frysinger | 62c93d9 | 2009-02-11 18:51:43 -0500 | [diff] [blame] | 139 | memcpy(tmp, build_buf, 6); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 140 | #else |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 141 | puts ("## Ethernet MAC address not copied to NV RAM\n"); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 142 | #endif |
| 143 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 144 | /* |
| 145 | * Use bootaddr to find the location in memory that VxWorks |
| 146 | * will look for the bootline string. The default value for |
| 147 | * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which |
| 148 | * defaults to 0x4200 |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 149 | */ |
| 150 | |
| 151 | if ((tmp = getenv ("bootaddr")) == NULL) |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 152 | bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR; |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 153 | else |
| 154 | bootaddr = simple_strtoul (tmp, NULL, 16); |
| 155 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 156 | /* |
| 157 | * Check to see if the bootline is defined in the 'bootargs' |
| 158 | * parameter. If it is not defined, we may be able to |
| 159 | * construct the info |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 160 | */ |
| 161 | |
| 162 | if ((bootline = getenv ("bootargs")) != NULL) { |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 163 | memcpy ((void *) bootaddr, bootline, |
| 164 | max (strlen (bootline), 255)); |
| 165 | flush_cache (bootaddr, max (strlen (bootline), 255)); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 166 | } else { |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 167 | |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 168 | |
| 169 | sprintf (build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE); |
| 170 | if ((tmp = getenv ("bootfile")) != NULL) { |
| 171 | sprintf (&build_buf[strlen (build_buf)], |
| 172 | "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 173 | } else { |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 174 | sprintf (&build_buf[strlen (build_buf)], |
| 175 | "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | if ((tmp = getenv ("ipaddr")) != NULL) { |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 179 | sprintf (&build_buf[strlen (build_buf)], "e=%s ", tmp); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 180 | } |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 181 | |
| 182 | if ((tmp = getenv ("serverip")) != NULL) { |
| 183 | sprintf (&build_buf[strlen (build_buf)], "h=%s ", tmp); |
| 184 | } |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 185 | |
| 186 | if ((tmp = getenv ("hostname")) != NULL) { |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 187 | sprintf (&build_buf[strlen (build_buf)], "tn=%s ", tmp); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 188 | } |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 189 | #ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS |
| 190 | sprintf (&build_buf[strlen (build_buf)], |
| 191 | CONFIG_SYS_VXWORKS_ADD_PARAMS); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 192 | #endif |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 193 | |
| 194 | memcpy ((void *) bootaddr, build_buf, |
| 195 | max (strlen (build_buf), 255)); |
| 196 | flush_cache (bootaddr, max (strlen (build_buf), 255)); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 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) { |
Niklaus Giger | 29a4c24 | 2008-11-03 22:15:34 +0100 | [diff] [blame] | 241 | printf ("## Not a 32-bit elf image at address 0x%08lx\n", addr); |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 245 | #if 0 |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 246 | if (ehdr->e_machine != EM_PPC) { |
| 247 | printf ("## Not a PowerPC elf image at address 0x%08lx\n", |
| 248 | addr); |
| 249 | return 0; |
| 250 | } |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 251 | #endif |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 252 | |
| 253 | return 1; |
| 254 | } |
| 255 | |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 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) { |
Niklaus Giger | 3c97284 | 2009-07-23 23:31:58 +0200 | [diff] [blame] | 290 | debug("%sing %s @ 0x%08lx (%ld bytes)\n", |
wdenk | 458ded3 | 2002-09-20 10:59:08 +0000 | [diff] [blame] | 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 | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 313 | U_BOOT_CMD( |
| 314 | bootelf, 2, 0, do_bootelf, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 315 | "Boot from an ELF image in memory", |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 316 | " [address] - load address of ELF image." |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 317 | ); |
| 318 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 319 | U_BOOT_CMD( |
| 320 | bootvx, 2, 0, do_bootvx, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 321 | "Boot vxWorks from an ELF image", |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 322 | " [address] - load address of vxWorks ELF image." |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 323 | ); |