cmd_elf.c 9.3 KB

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