cmd_elf.c 9.1 KB

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