elf.c 12 KB

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