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 = NULL;
  128. const char *ep = env_get("autostart");
  129. int rcode = 0;
  130. /* Consume 'bootelf' */
  131. argc--; argv++;
  132. /* Check for flag. */
  133. if (argc >= 1 && (argv[0][0] == '-' && \
  134. (argv[0][1] == 'p' || argv[0][1] == 's'))) {
  135. sload = argv[0];
  136. /* Consume flag. */
  137. argc--; argv++;
  138. }
  139. /* Check for address. */
  140. if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
  141. /* Consume address */
  142. argc--; argv++;
  143. } else
  144. addr = load_addr;
  145. if (!valid_elf_image(addr))
  146. return 1;
  147. if (sload && sload[1] == 'p')
  148. addr = load_elf_image_phdr(addr);
  149. else
  150. addr = load_elf_image_shdr(addr);
  151. if (ep && !strcmp(ep, "no"))
  152. return rcode;
  153. printf("## Starting application at 0x%08lx ...\n", addr);
  154. /*
  155. * pass address parameter as argv[0] (aka command name),
  156. * and all remaining args
  157. */
  158. rc = do_bootelf_exec((void *)addr, argc, argv);
  159. if (rc != 0)
  160. rcode = 1;
  161. printf("## Application terminated, rc = 0x%lx\n", rc);
  162. return rcode;
  163. }
  164. /*
  165. * Interpreter command to boot VxWorks from a memory image. The image can
  166. * be either an ELF image or a raw binary. Will attempt to setup the
  167. * bootline and other parameters correctly.
  168. */
  169. int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  170. {
  171. unsigned long addr; /* Address of image */
  172. unsigned long bootaddr; /* Address to put the bootline */
  173. char *bootline; /* Text of the bootline */
  174. char *tmp; /* Temporary char pointer */
  175. char build_buf[128]; /* Buffer for building the bootline */
  176. int ptr = 0;
  177. #ifdef CONFIG_X86
  178. struct e820info *info;
  179. struct e820entry *data;
  180. #endif
  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_env_get_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_env_get_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 = env_get("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 = env_get("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 = env_get("bootdev");
  242. if (tmp) {
  243. strcpy(build_buf, tmp);
  244. ptr = strlen(tmp);
  245. } else
  246. printf("## VxWorks boot device not specified\n");
  247. tmp = env_get("bootfile");
  248. if (tmp)
  249. ptr += sprintf(build_buf + ptr,
  250. "host:%s ", tmp);
  251. else
  252. ptr += sprintf(build_buf + ptr,
  253. "host:vxWorks ");
  254. /*
  255. * The following parameters are only needed if 'bootdev'
  256. * is an ethernet device, otherwise they are optional.
  257. */
  258. tmp = env_get("ipaddr");
  259. if (tmp) {
  260. ptr += sprintf(build_buf + ptr, "e=%s", tmp);
  261. tmp = env_get("netmask");
  262. if (tmp) {
  263. u32 mask = env_get_ip("netmask").s_addr;
  264. ptr += sprintf(build_buf + ptr,
  265. ":%08x ", ntohl(mask));
  266. } else {
  267. ptr += sprintf(build_buf + ptr, " ");
  268. }
  269. }
  270. tmp = env_get("serverip");
  271. if (tmp)
  272. ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
  273. tmp = env_get("gatewayip");
  274. if (tmp)
  275. ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
  276. tmp = env_get("hostname");
  277. if (tmp)
  278. ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
  279. tmp = env_get("othbootargs");
  280. if (tmp) {
  281. strcpy(build_buf + ptr, tmp);
  282. ptr += strlen(tmp);
  283. }
  284. memcpy((void *)bootaddr, build_buf,
  285. max(strlen(build_buf), (size_t)255));
  286. flush_cache(bootaddr, max(strlen(build_buf),
  287. (size_t)255));
  288. }
  289. printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
  290. (char *)bootaddr);
  291. }
  292. #ifdef CONFIG_X86
  293. /*
  294. * Since E820 information is critical to the kernel, if we don't
  295. * specify these in the environments, use a default one.
  296. */
  297. tmp = env_get("e820data");
  298. if (tmp)
  299. data = (struct e820entry *)simple_strtoul(tmp, NULL, 16);
  300. else
  301. data = (struct e820entry *)VXWORKS_E820_DATA_ADDR;
  302. tmp = env_get("e820info");
  303. if (tmp)
  304. info = (struct e820info *)simple_strtoul(tmp, NULL, 16);
  305. else
  306. info = (struct e820info *)VXWORKS_E820_INFO_ADDR;
  307. memset(info, 0, sizeof(struct e820info));
  308. info->sign = E820_SIGNATURE;
  309. info->entries = install_e820_map(E820MAX, data);
  310. info->addr = (info->entries - 1) * sizeof(struct e820entry) +
  311. VXWORKS_E820_DATA_ADDR;
  312. #endif
  313. /*
  314. * If the data at the load address is an elf image, then
  315. * treat it like an elf image. Otherwise, assume that it is a
  316. * binary image.
  317. */
  318. if (valid_elf_image(addr))
  319. addr = load_elf_image_shdr(addr);
  320. else
  321. puts("## Not an ELF image, assuming binary\n");
  322. printf("## Starting vxWorks at 0x%08lx ...\n", addr);
  323. dcache_disable();
  324. #ifdef CONFIG_X86
  325. /* VxWorks on x86 uses stack to pass parameters */
  326. ((asmlinkage void (*)(int))addr)(0);
  327. #else
  328. ((void (*)(int))addr)(0);
  329. #endif
  330. puts("## vxWorks terminated\n");
  331. return 1;
  332. }
  333. U_BOOT_CMD(
  334. bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
  335. "Boot from an ELF image in memory",
  336. "[-p|-s] [address]\n"
  337. "\t- load ELF image at [address] via program headers (-p)\n"
  338. "\t or via section headers (-s)"
  339. );
  340. U_BOOT_CMD(
  341. bootvx, 2, 0, do_bootvx,
  342. "Boot vxWorks from an ELF image",
  343. " [address] - load address of vxWorks ELF image."
  344. );