cmd_elf.c 9.2 KB

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