cmd_elf.c 8.9 KB

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