elf.c 15 KB

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