elf.c 14 KB

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