elf.c 14 KB

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