elf.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. * pass address parameter as argv[0] (aka command name),
  98. * and all remaining args
  99. */
  100. ret = entry(argc, argv);
  101. return ret;
  102. }
  103. /*
  104. * Determine if a valid ELF image exists at the given memory location.
  105. * First look at the ELF header magic field, then make sure that it is
  106. * executable.
  107. */
  108. int valid_elf_image(unsigned long addr)
  109. {
  110. Elf32_Ehdr *ehdr; /* Elf header structure pointer */
  111. ehdr = (Elf32_Ehdr *)addr;
  112. if (!IS_ELF(*ehdr)) {
  113. printf("## No elf image at address 0x%08lx\n", addr);
  114. return 0;
  115. }
  116. if (ehdr->e_type != ET_EXEC) {
  117. printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
  118. return 0;
  119. }
  120. return 1;
  121. }
  122. /* Interpreter command to boot an arbitrary ELF image from memory */
  123. int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  124. {
  125. unsigned long addr; /* Address of the ELF image */
  126. unsigned long rc; /* Return value from user code */
  127. char *sload, *saddr;
  128. const char *ep = getenv("autostart");
  129. int rcode = 0;
  130. sload = saddr = NULL;
  131. if (argc == 3) {
  132. sload = argv[1];
  133. saddr = argv[2];
  134. } else if (argc == 2) {
  135. if (argv[1][0] == '-')
  136. sload = argv[1];
  137. else
  138. saddr = argv[1];
  139. }
  140. if (saddr)
  141. addr = simple_strtoul(saddr, NULL, 16);
  142. else
  143. addr = load_addr;
  144. if (!valid_elf_image(addr))
  145. return 1;
  146. if (sload && sload[1] == 'p')
  147. addr = load_elf_image_phdr(addr);
  148. else
  149. addr = load_elf_image_shdr(addr);
  150. if (ep && !strcmp(ep, "no"))
  151. return rcode;
  152. printf("## Starting application at 0x%08lx ...\n", addr);
  153. /*
  154. * pass address parameter as argv[0] (aka command name),
  155. * and all remaining args
  156. */
  157. rc = do_bootelf_exec((void *)addr, argc - 1, argv + 1);
  158. if (rc != 0)
  159. rcode = 1;
  160. printf("## Application terminated, rc = 0x%lx\n", rc);
  161. return rcode;
  162. }
  163. /*
  164. * Interpreter command to boot VxWorks from a memory image. The image can
  165. * be either an ELF image or a raw binary. Will attempt to setup the
  166. * bootline and other parameters correctly.
  167. */
  168. int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  169. {
  170. unsigned long addr; /* Address of image */
  171. unsigned long bootaddr; /* Address to put the bootline */
  172. char *bootline; /* Text of the bootline */
  173. char *tmp; /* Temporary char pointer */
  174. char build_buf[128]; /* Buffer for building the bootline */
  175. int ptr = 0;
  176. #ifdef CONFIG_X86
  177. struct e820info *info;
  178. struct e820entry *data;
  179. #endif
  180. /*
  181. * Check the loadaddr variable.
  182. * If we don't know where the image is then we're done.
  183. */
  184. if (argc < 2)
  185. addr = load_addr;
  186. else
  187. addr = simple_strtoul(argv[1], NULL, 16);
  188. #if defined(CONFIG_CMD_NET)
  189. /*
  190. * Check to see if we need to tftp the image ourselves
  191. * before starting
  192. */
  193. if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
  194. if (net_loop(TFTPGET) <= 0)
  195. return 1;
  196. printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
  197. addr);
  198. }
  199. #endif
  200. /*
  201. * This should equate to
  202. * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
  203. * from the VxWorks BSP header files.
  204. * This will vary from board to board
  205. */
  206. #if defined(CONFIG_WALNUT)
  207. tmp = (char *)CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
  208. eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
  209. memcpy(tmp, &build_buf[3], 3);
  210. #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
  211. tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
  212. eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
  213. memcpy(tmp, build_buf, 6);
  214. #else
  215. puts("## Ethernet MAC address not copied to NV RAM\n");
  216. #endif
  217. /*
  218. * Use bootaddr to find the location in memory that VxWorks
  219. * will look for the bootline string. The default value is
  220. * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
  221. * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
  222. */
  223. tmp = getenv("bootaddr");
  224. if (!tmp) {
  225. printf("## VxWorks bootline address not specified\n");
  226. } else {
  227. bootaddr = simple_strtoul(tmp, NULL, 16);
  228. /*
  229. * Check to see if the bootline is defined in the 'bootargs'
  230. * parameter. If it is not defined, we may be able to
  231. * construct the info.
  232. */
  233. bootline = getenv("bootargs");
  234. if (bootline) {
  235. memcpy((void *)bootaddr, bootline,
  236. max(strlen(bootline), (size_t)255));
  237. flush_cache(bootaddr, max(strlen(bootline),
  238. (size_t)255));
  239. } else {
  240. tmp = getenv("bootdev");
  241. if (tmp) {
  242. strcpy(build_buf, tmp);
  243. ptr = strlen(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. strcpy(build_buf + ptr, tmp);
  281. ptr += strlen(tmp);
  282. }
  283. memcpy((void *)bootaddr, build_buf,
  284. max(strlen(build_buf), (size_t)255));
  285. flush_cache(bootaddr, max(strlen(build_buf),
  286. (size_t)255));
  287. }
  288. printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
  289. (char *)bootaddr);
  290. }
  291. #ifdef CONFIG_X86
  292. /*
  293. * Since E820 information is critical to the kernel, if we don't
  294. * specify these in the environments, use a default one.
  295. */
  296. tmp = getenv("e820data");
  297. if (tmp)
  298. data = (struct e820entry *)simple_strtoul(tmp, NULL, 16);
  299. else
  300. data = (struct e820entry *)VXWORKS_E820_DATA_ADDR;
  301. tmp = getenv("e820info");
  302. if (tmp)
  303. info = (struct e820info *)simple_strtoul(tmp, NULL, 16);
  304. else
  305. info = (struct e820info *)VXWORKS_E820_INFO_ADDR;
  306. memset(info, 0, sizeof(struct e820info));
  307. info->sign = E820_SIGNATURE;
  308. info->entries = install_e820_map(E820MAX, data);
  309. info->addr = (info->entries - 1) * sizeof(struct e820entry) +
  310. VXWORKS_E820_DATA_ADDR;
  311. #endif
  312. /*
  313. * If the data at the load address is an elf image, then
  314. * treat it like an elf image. Otherwise, assume that it is a
  315. * binary image.
  316. */
  317. if (valid_elf_image(addr))
  318. addr = load_elf_image_shdr(addr);
  319. else
  320. puts("## Not an ELF image, assuming binary\n");
  321. printf("## Starting vxWorks at 0x%08lx ...\n", addr);
  322. dcache_disable();
  323. #ifdef CONFIG_X86
  324. /* VxWorks on x86 uses stack to pass parameters */
  325. ((asmlinkage void (*)(int))addr)(0);
  326. #else
  327. ((void (*)(int))addr)(0);
  328. #endif
  329. puts("## vxWorks terminated\n");
  330. return 1;
  331. }
  332. U_BOOT_CMD(
  333. bootelf, 3, 0, do_bootelf,
  334. "Boot from an ELF image in memory",
  335. "[-p|-s] [address]\n"
  336. "\t- load ELF image at [address] via program headers (-p)\n"
  337. "\t or via section headers (-s)"
  338. );
  339. U_BOOT_CMD(
  340. bootvx, 2, 0, do_bootvx,
  341. "Boot vxWorks from an ELF image",
  342. " [address] - load address of vxWorks ELF image."
  343. );