elf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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(shdr->sh_addr, shdr->sh_size);
  190. }
  191. return ehdr->e_entry;
  192. }
  193. /* Allow ports to override the default behavior */
  194. static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
  195. int argc, char * const argv[])
  196. {
  197. unsigned long ret;
  198. /*
  199. * pass address parameter as argv[0] (aka command name),
  200. * and all remaining args
  201. */
  202. ret = entry(argc, argv);
  203. return ret;
  204. }
  205. /*
  206. * Determine if a valid ELF image exists at the given memory location.
  207. * First look at the ELF header magic field, then make sure that it is
  208. * executable.
  209. */
  210. int valid_elf_image(unsigned long addr)
  211. {
  212. Elf32_Ehdr *ehdr; /* Elf header structure pointer */
  213. ehdr = (Elf32_Ehdr *)addr;
  214. if (!IS_ELF(*ehdr)) {
  215. printf("## No elf image at address 0x%08lx\n", addr);
  216. return 0;
  217. }
  218. if (ehdr->e_type != ET_EXEC) {
  219. printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
  220. return 0;
  221. }
  222. return 1;
  223. }
  224. /* Interpreter command to boot an arbitrary ELF image from memory */
  225. int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  226. {
  227. unsigned long addr; /* Address of the ELF image */
  228. unsigned long rc; /* Return value from user code */
  229. char *sload = NULL;
  230. const char *ep = env_get("autostart");
  231. int rcode = 0;
  232. /* Consume 'bootelf' */
  233. argc--; argv++;
  234. /* Check for flag. */
  235. if (argc >= 1 && (argv[0][0] == '-' && \
  236. (argv[0][1] == 'p' || argv[0][1] == 's'))) {
  237. sload = argv[0];
  238. /* Consume flag. */
  239. argc--; argv++;
  240. }
  241. /* Check for address. */
  242. if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
  243. /* Consume address */
  244. argc--; argv++;
  245. } else
  246. addr = load_addr;
  247. if (!valid_elf_image(addr))
  248. return 1;
  249. if (sload && sload[1] == 'p')
  250. addr = load_elf_image_phdr(addr);
  251. else
  252. addr = load_elf_image_shdr(addr);
  253. if (ep && !strcmp(ep, "no"))
  254. return rcode;
  255. printf("## Starting application at 0x%08lx ...\n", addr);
  256. /*
  257. * pass address parameter as argv[0] (aka command name),
  258. * and all remaining args
  259. */
  260. rc = do_bootelf_exec((void *)addr, argc, argv);
  261. if (rc != 0)
  262. rcode = 1;
  263. printf("## Application terminated, rc = 0x%lx\n", rc);
  264. return rcode;
  265. }
  266. /*
  267. * Interpreter command to boot VxWorks from a memory image. The image can
  268. * be either an ELF image or a raw binary. Will attempt to setup the
  269. * bootline and other parameters correctly.
  270. */
  271. int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  272. {
  273. unsigned long addr; /* Address of image */
  274. unsigned long bootaddr = 0; /* Address to put the bootline */
  275. char *bootline; /* Text of the bootline */
  276. char *tmp; /* Temporary char pointer */
  277. char build_buf[128]; /* Buffer for building the bootline */
  278. int ptr = 0;
  279. #ifdef CONFIG_X86
  280. ulong base;
  281. struct e820_info *info;
  282. struct e820_entry *data;
  283. struct efi_gop_info *gop;
  284. struct vesa_mode_info *vesa = &mode_info.vesa;
  285. #endif
  286. /*
  287. * Check the loadaddr variable.
  288. * If we don't know where the image is then we're done.
  289. */
  290. if (argc < 2)
  291. addr = load_addr;
  292. else
  293. addr = simple_strtoul(argv[1], NULL, 16);
  294. #if defined(CONFIG_CMD_NET)
  295. /*
  296. * Check to see if we need to tftp the image ourselves
  297. * before starting
  298. */
  299. if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
  300. if (net_loop(TFTPGET) <= 0)
  301. return 1;
  302. printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
  303. addr);
  304. }
  305. #endif
  306. /*
  307. * This should equate to
  308. * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
  309. * from the VxWorks BSP header files.
  310. * This will vary from board to board
  311. */
  312. #if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
  313. tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
  314. eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
  315. memcpy(tmp, build_buf, 6);
  316. #else
  317. puts("## Ethernet MAC address not copied to NV RAM\n");
  318. #endif
  319. #ifdef CONFIG_X86
  320. /*
  321. * Get VxWorks's physical memory base address from environment,
  322. * if we don't specify it in the environment, use a default one.
  323. */
  324. base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
  325. data = (struct e820_entry *)(base + E820_DATA_OFFSET);
  326. info = (struct e820_info *)(base + E820_INFO_OFFSET);
  327. memset(info, 0, sizeof(struct e820_info));
  328. info->sign = E820_SIGNATURE;
  329. info->entries = install_e820_map(E820MAX, data);
  330. info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
  331. E820_DATA_OFFSET;
  332. /*
  333. * Explicitly clear the bootloader image size otherwise if memory
  334. * at this offset happens to contain some garbage data, the final
  335. * available memory size for the kernel is insane.
  336. */
  337. *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
  338. /*
  339. * Prepare compatible framebuffer information block.
  340. * The VESA mode has to be 32-bit RGBA.
  341. */
  342. if (vesa->x_resolution && vesa->y_resolution) {
  343. gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET);
  344. gop->magic = EFI_GOP_INFO_MAGIC;
  345. gop->info.version = 0;
  346. gop->info.width = vesa->x_resolution;
  347. gop->info.height = vesa->y_resolution;
  348. gop->info.pixel_format = EFI_GOT_RGBA8;
  349. gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4;
  350. gop->fb_base = vesa->phys_base_ptr;
  351. gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution;
  352. }
  353. #endif
  354. /*
  355. * Use bootaddr to find the location in memory that VxWorks
  356. * will look for the bootline string. The default value is
  357. * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
  358. * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
  359. */
  360. tmp = env_get("bootaddr");
  361. if (!tmp) {
  362. #ifdef CONFIG_X86
  363. bootaddr = base + X86_BOOT_LINE_OFFSET;
  364. #else
  365. printf("## VxWorks bootline address not specified\n");
  366. return 1;
  367. #endif
  368. }
  369. if (!bootaddr)
  370. bootaddr = simple_strtoul(tmp, NULL, 16);
  371. /*
  372. * Check to see if the bootline is defined in the 'bootargs' parameter.
  373. * If it is not defined, we may be able to construct the info.
  374. */
  375. bootline = env_get("bootargs");
  376. if (!bootline) {
  377. tmp = env_get("bootdev");
  378. if (tmp) {
  379. strcpy(build_buf, tmp);
  380. ptr = strlen(tmp);
  381. } else {
  382. printf("## VxWorks boot device not specified\n");
  383. }
  384. tmp = env_get("bootfile");
  385. if (tmp)
  386. ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
  387. else
  388. ptr += sprintf(build_buf + ptr, "host:vxWorks ");
  389. /*
  390. * The following parameters are only needed if 'bootdev'
  391. * is an ethernet device, otherwise they are optional.
  392. */
  393. tmp = env_get("ipaddr");
  394. if (tmp) {
  395. ptr += sprintf(build_buf + ptr, "e=%s", tmp);
  396. tmp = env_get("netmask");
  397. if (tmp) {
  398. u32 mask = env_get_ip("netmask").s_addr;
  399. ptr += sprintf(build_buf + ptr,
  400. ":%08x ", ntohl(mask));
  401. } else {
  402. ptr += sprintf(build_buf + ptr, " ");
  403. }
  404. }
  405. tmp = env_get("serverip");
  406. if (tmp)
  407. ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
  408. tmp = env_get("gatewayip");
  409. if (tmp)
  410. ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
  411. tmp = env_get("hostname");
  412. if (tmp)
  413. ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
  414. tmp = env_get("othbootargs");
  415. if (tmp) {
  416. strcpy(build_buf + ptr, tmp);
  417. ptr += strlen(tmp);
  418. }
  419. bootline = build_buf;
  420. }
  421. memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255));
  422. flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
  423. printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr);
  424. /*
  425. * If the data at the load address is an elf image, then
  426. * treat it like an elf image. Otherwise, assume that it is a
  427. * binary image.
  428. */
  429. if (valid_elf_image(addr))
  430. addr = load_elf_image_phdr(addr);
  431. else
  432. puts("## Not an ELF image, assuming binary\n");
  433. printf("## Starting vxWorks at 0x%08lx ...\n", addr);
  434. dcache_disable();
  435. #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
  436. armv8_setup_psci();
  437. smp_kick_all_cpus();
  438. #endif
  439. #ifdef CONFIG_X86
  440. /* VxWorks on x86 uses stack to pass parameters */
  441. ((asmlinkage void (*)(int))addr)(0);
  442. #else
  443. ((void (*)(int))addr)(0);
  444. #endif
  445. puts("## vxWorks terminated\n");
  446. return 1;
  447. }
  448. U_BOOT_CMD(
  449. bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
  450. "Boot from an ELF image in memory",
  451. "[-p|-s] [address]\n"
  452. "\t- load ELF image at [address] via program headers (-p)\n"
  453. "\t or via section headers (-s)"
  454. );
  455. U_BOOT_CMD(
  456. bootvx, 2, 0, do_bootvx,
  457. "Boot vxWorks from an ELF image",
  458. " [address] - load address of vxWorks ELF image."
  459. );