elf.c 15 KB

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