elf.c 15 KB

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