cmd_elf.c 10 KB

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