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