cmd_elf.c 9.4 KB

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