elf.c 10 KB

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