cmd_elf.c 8.9 KB

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