cmd_elf.c 9.0 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. #include <vxworks.h>
  21. #if defined(CONFIG_WALNUT) || defined(CONFIG_SYS_VXWORKS_MAC_PTR)
  22. DECLARE_GLOBAL_DATA_PTR;
  23. #endif
  24. int valid_elf_image (unsigned long addr);
  25. unsigned long load_elf_image (unsigned long addr);
  26. /* Allow ports to override the default behavior */
  27. __attribute__((weak))
  28. unsigned long do_bootelf_exec (ulong (*entry)(int, char *[]), int argc, char *argv[])
  29. {
  30. unsigned long ret;
  31. /*
  32. * QNX images require the data cache is disabled.
  33. * Data cache is already flushed, so just turn it off.
  34. */
  35. int dcache = dcache_status ();
  36. if (dcache)
  37. dcache_disable ();
  38. /*
  39. * pass address parameter as argv[0] (aka command name),
  40. * and all remaining args
  41. */
  42. ret = entry (argc, argv);
  43. if (dcache)
  44. dcache_enable ();
  45. return ret;
  46. }
  47. /* ======================================================================
  48. * Interpreter command to boot an arbitrary ELF image from memory.
  49. * ====================================================================== */
  50. int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  51. {
  52. unsigned long addr; /* Address of the ELF image */
  53. unsigned long rc; /* Return value from user code */
  54. /* -------------------------------------------------- */
  55. int rcode = 0;
  56. if (argc < 2)
  57. addr = load_addr;
  58. else
  59. addr = simple_strtoul (argv[1], NULL, 16);
  60. if (!valid_elf_image (addr))
  61. return 1;
  62. addr = load_elf_image (addr);
  63. printf ("## Starting application at 0x%08lx ...\n", addr);
  64. /*
  65. * pass address parameter as argv[0] (aka command name),
  66. * and all remaining args
  67. */
  68. rc = do_bootelf_exec ((void *)addr, argc - 1, argv + 1);
  69. if (rc != 0)
  70. rcode = 1;
  71. printf ("## Application terminated, rc = 0x%lx\n", rc);
  72. return rcode;
  73. }
  74. /* ======================================================================
  75. * Interpreter command to boot VxWorks from a memory image. The image can
  76. * be either an ELF image or a raw binary. Will attempt to setup the
  77. * bootline and other parameters correctly.
  78. * ====================================================================== */
  79. int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  80. {
  81. unsigned long addr; /* Address of image */
  82. unsigned long bootaddr; /* Address to put the bootline */
  83. char *bootline; /* Text of the bootline */
  84. char *tmp; /* Temporary char pointer */
  85. char build_buf[128]; /* Buffer for building the bootline */
  86. /* ---------------------------------------------------
  87. *
  88. * Check the loadaddr variable.
  89. * If we don't know where the image is then we're done.
  90. */
  91. if (argc < 2)
  92. addr = load_addr;
  93. else
  94. addr = simple_strtoul (argv[1], NULL, 16);
  95. #if defined(CONFIG_CMD_NET)
  96. /* Check to see if we need to tftp the image ourselves before starting */
  97. if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) {
  98. if (NetLoop (TFTP) <= 0)
  99. return 1;
  100. printf ("Automatic boot of VxWorks image at address 0x%08lx ... \n",
  101. 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. eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
  112. memcpy(tmp, &build_buf[3], 3);
  113. #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
  114. tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR;
  115. eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
  116. memcpy(tmp, build_buf, 6);
  117. #else
  118. puts ("## Ethernet MAC address not copied to NV RAM\n");
  119. #endif
  120. /*
  121. * Use bootaddr to find the location in memory that VxWorks
  122. * will look for the bootline string. The default value for
  123. * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
  124. * defaults to 0x4200
  125. */
  126. if ((tmp = getenv ("bootaddr")) == NULL)
  127. bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR;
  128. else
  129. bootaddr = simple_strtoul (tmp, NULL, 16);
  130. /*
  131. * Check to see if the bootline is defined in the 'bootargs'
  132. * parameter. If it is not defined, we may be able to
  133. * construct the info
  134. */
  135. if ((bootline = getenv ("bootargs")) != NULL) {
  136. memcpy ((void *) bootaddr, bootline,
  137. max (strlen (bootline), 255));
  138. flush_cache (bootaddr, max (strlen (bootline), 255));
  139. } else {
  140. sprintf (build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE);
  141. if ((tmp = getenv ("bootfile")) != NULL) {
  142. sprintf (&build_buf[strlen (build_buf)],
  143. "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp);
  144. } else {
  145. sprintf (&build_buf[strlen (build_buf)],
  146. "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME);
  147. }
  148. if ((tmp = getenv ("ipaddr")) != NULL) {
  149. sprintf (&build_buf[strlen (build_buf)], "e=%s ", tmp);
  150. }
  151. if ((tmp = getenv ("serverip")) != NULL) {
  152. sprintf (&build_buf[strlen (build_buf)], "h=%s ", tmp);
  153. }
  154. if ((tmp = getenv ("hostname")) != NULL) {
  155. sprintf (&build_buf[strlen (build_buf)], "tn=%s ", tmp);
  156. }
  157. #ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
  158. sprintf (&build_buf[strlen (build_buf)],
  159. CONFIG_SYS_VXWORKS_ADD_PARAMS);
  160. #endif
  161. memcpy ((void *) bootaddr, build_buf,
  162. max (strlen (build_buf), 255));
  163. flush_cache (bootaddr, max (strlen (build_buf), 255));
  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", addr);
  199. return 0;
  200. }
  201. #if 0
  202. if (ehdr->e_machine != EM_PPC) {
  203. printf ("## Not a PowerPC elf image at address 0x%08lx\n",
  204. addr);
  205. return 0;
  206. }
  207. #endif
  208. return 1;
  209. }
  210. /* ======================================================================
  211. * A very simple elf loader, assumes the image is valid, returns the
  212. * entry point address.
  213. * ====================================================================== */
  214. unsigned long load_elf_image (unsigned long addr)
  215. {
  216. Elf32_Ehdr *ehdr; /* Elf header structure pointer */
  217. Elf32_Shdr *shdr; /* Section header structure pointer */
  218. unsigned char *strtab = 0; /* String table pointer */
  219. unsigned char *image; /* Binary image pointer */
  220. int i; /* Loop counter */
  221. /* -------------------------------------------------- */
  222. ehdr = (Elf32_Ehdr *) addr;
  223. /* Find the section header string table for output info */
  224. shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
  225. (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
  226. if (shdr->sh_type == SHT_STRTAB)
  227. strtab = (unsigned char *) (addr + shdr->sh_offset);
  228. /* Load each appropriate section */
  229. for (i = 0; i < ehdr->e_shnum; ++i) {
  230. shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
  231. (i * sizeof (Elf32_Shdr)));
  232. if (!(shdr->sh_flags & SHF_ALLOC)
  233. || shdr->sh_addr == 0 || shdr->sh_size == 0) {
  234. continue;
  235. }
  236. if (strtab) {
  237. debug("%sing %s @ 0x%08lx (%ld bytes)\n",
  238. (shdr->sh_type == SHT_NOBITS) ?
  239. "Clear" : "Load",
  240. &strtab[shdr->sh_name],
  241. (unsigned long) shdr->sh_addr,
  242. (long) shdr->sh_size);
  243. }
  244. if (shdr->sh_type == SHT_NOBITS) {
  245. memset ((void *)shdr->sh_addr, 0, shdr->sh_size);
  246. } else {
  247. image = (unsigned char *) addr + shdr->sh_offset;
  248. memcpy ((void *) shdr->sh_addr,
  249. (const void *) image,
  250. shdr->sh_size);
  251. }
  252. flush_cache (shdr->sh_addr, shdr->sh_size);
  253. }
  254. return ehdr->e_entry;
  255. }
  256. /* ====================================================================== */
  257. U_BOOT_CMD(
  258. bootelf, 2, 0, do_bootelf,
  259. "Boot from an ELF image in memory",
  260. " [address] - load address of ELF image."
  261. );
  262. U_BOOT_CMD(
  263. bootvx, 2, 0, do_bootvx,
  264. "Boot vxWorks from an ELF image",
  265. " [address] - load address of vxWorks ELF image."
  266. );