elf.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. /* Allow ports to override the default behavior */
  20. static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
  21. int argc, char * const argv[])
  22. {
  23. unsigned long ret;
  24. /*
  25. * pass address parameter as argv[0] (aka command name),
  26. * and all remaining args
  27. */
  28. ret = entry(argc, argv);
  29. return ret;
  30. }
  31. /* Interpreter command to boot an arbitrary ELF image from memory */
  32. int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  33. {
  34. unsigned long addr; /* Address of the ELF image */
  35. unsigned long rc; /* Return value from user code */
  36. char *sload = NULL;
  37. const char *ep = env_get("autostart");
  38. int rcode = 0;
  39. /* Consume 'bootelf' */
  40. argc--; argv++;
  41. /* Check for flag. */
  42. if (argc >= 1 && (argv[0][0] == '-' && \
  43. (argv[0][1] == 'p' || argv[0][1] == 's'))) {
  44. sload = argv[0];
  45. /* Consume flag. */
  46. argc--; argv++;
  47. }
  48. /* Check for address. */
  49. if (argc >= 1 && strict_strtoul(argv[0], 16, &addr) != -EINVAL) {
  50. /* Consume address */
  51. argc--; argv++;
  52. } else
  53. addr = image_load_addr;
  54. if (!valid_elf_image(addr))
  55. return 1;
  56. if (sload && sload[1] == 'p')
  57. addr = load_elf_image_phdr(addr);
  58. else
  59. addr = load_elf_image_shdr(addr);
  60. if (ep && !strcmp(ep, "no"))
  61. return rcode;
  62. printf("## Starting application at 0x%08lx ...\n", addr);
  63. /*
  64. * pass address parameter as argv[0] (aka command name),
  65. * and all remaining args
  66. */
  67. rc = do_bootelf_exec((void *)addr, argc, argv);
  68. if (rc != 0)
  69. rcode = 1;
  70. printf("## Application terminated, rc = 0x%lx\n", rc);
  71. return rcode;
  72. }
  73. /*
  74. * Interpreter command to boot VxWorks from a memory image. The image can
  75. * be either an ELF image or a raw binary. Will attempt to setup the
  76. * bootline and other parameters correctly.
  77. */
  78. int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  79. {
  80. unsigned long addr; /* Address of image */
  81. unsigned long bootaddr = 0; /* Address to put the bootline */
  82. char *bootline; /* Text of the bootline */
  83. char *tmp; /* Temporary char pointer */
  84. char build_buf[128]; /* Buffer for building the bootline */
  85. int ptr = 0;
  86. #ifdef CONFIG_X86
  87. ulong base;
  88. struct e820_info *info;
  89. struct e820_entry *data;
  90. struct efi_gop_info *gop;
  91. struct vesa_mode_info *vesa = &mode_info.vesa;
  92. #endif
  93. /*
  94. * Check the loadaddr variable.
  95. * If we don't know where the image is then we're done.
  96. */
  97. if (argc < 2)
  98. addr = image_load_addr;
  99. else
  100. addr = simple_strtoul(argv[1], NULL, 16);
  101. #if defined(CONFIG_CMD_NET)
  102. /*
  103. * Check to see if we need to tftp the image ourselves
  104. * before starting
  105. */
  106. if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
  107. if (net_loop(TFTPGET) <= 0)
  108. return 1;
  109. printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
  110. addr);
  111. }
  112. #endif
  113. /*
  114. * This should equate to
  115. * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
  116. * from the VxWorks BSP header files.
  117. * This will vary from board to board
  118. */
  119. #if defined(CONFIG_SYS_VXWORKS_MAC_PTR)
  120. tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR;
  121. eth_env_get_enetaddr("ethaddr", (uchar *)build_buf);
  122. memcpy(tmp, build_buf, 6);
  123. #else
  124. puts("## Ethernet MAC address not copied to NV RAM\n");
  125. #endif
  126. #ifdef CONFIG_X86
  127. /*
  128. * Get VxWorks's physical memory base address from environment,
  129. * if we don't specify it in the environment, use a default one.
  130. */
  131. base = env_get_hex("vx_phys_mem_base", VXWORKS_PHYS_MEM_BASE);
  132. data = (struct e820_entry *)(base + E820_DATA_OFFSET);
  133. info = (struct e820_info *)(base + E820_INFO_OFFSET);
  134. memset(info, 0, sizeof(struct e820_info));
  135. info->sign = E820_SIGNATURE;
  136. info->entries = install_e820_map(E820MAX, data);
  137. info->addr = (info->entries - 1) * sizeof(struct e820_entry) +
  138. E820_DATA_OFFSET;
  139. /*
  140. * Explicitly clear the bootloader image size otherwise if memory
  141. * at this offset happens to contain some garbage data, the final
  142. * available memory size for the kernel is insane.
  143. */
  144. *(u32 *)(base + BOOT_IMAGE_SIZE_OFFSET) = 0;
  145. /*
  146. * Prepare compatible framebuffer information block.
  147. * The VESA mode has to be 32-bit RGBA.
  148. */
  149. if (vesa->x_resolution && vesa->y_resolution) {
  150. gop = (struct efi_gop_info *)(base + EFI_GOP_INFO_OFFSET);
  151. gop->magic = EFI_GOP_INFO_MAGIC;
  152. gop->info.version = 0;
  153. gop->info.width = vesa->x_resolution;
  154. gop->info.height = vesa->y_resolution;
  155. gop->info.pixel_format = EFI_GOT_RGBA8;
  156. gop->info.pixels_per_scanline = vesa->bytes_per_scanline / 4;
  157. gop->fb_base = vesa->phys_base_ptr;
  158. gop->fb_size = vesa->bytes_per_scanline * vesa->y_resolution;
  159. }
  160. #endif
  161. /*
  162. * Use bootaddr to find the location in memory that VxWorks
  163. * will look for the bootline string. The default value is
  164. * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by
  165. * VxWorks BSP. For example, on PowerPC it defaults to 0x4200.
  166. */
  167. tmp = env_get("bootaddr");
  168. if (!tmp) {
  169. #ifdef CONFIG_X86
  170. bootaddr = base + X86_BOOT_LINE_OFFSET;
  171. #else
  172. printf("## VxWorks bootline address not specified\n");
  173. return 1;
  174. #endif
  175. }
  176. if (!bootaddr)
  177. bootaddr = simple_strtoul(tmp, NULL, 16);
  178. /*
  179. * Check to see if the bootline is defined in the 'bootargs' parameter.
  180. * If it is not defined, we may be able to construct the info.
  181. */
  182. bootline = env_get("bootargs");
  183. if (!bootline) {
  184. tmp = env_get("bootdev");
  185. if (tmp) {
  186. strcpy(build_buf, tmp);
  187. ptr = strlen(tmp);
  188. } else {
  189. printf("## VxWorks boot device not specified\n");
  190. }
  191. tmp = env_get("bootfile");
  192. if (tmp)
  193. ptr += sprintf(build_buf + ptr, "host:%s ", tmp);
  194. else
  195. ptr += sprintf(build_buf + ptr, "host:vxWorks ");
  196. /*
  197. * The following parameters are only needed if 'bootdev'
  198. * is an ethernet device, otherwise they are optional.
  199. */
  200. tmp = env_get("ipaddr");
  201. if (tmp) {
  202. ptr += sprintf(build_buf + ptr, "e=%s", tmp);
  203. tmp = env_get("netmask");
  204. if (tmp) {
  205. u32 mask = env_get_ip("netmask").s_addr;
  206. ptr += sprintf(build_buf + ptr,
  207. ":%08x ", ntohl(mask));
  208. } else {
  209. ptr += sprintf(build_buf + ptr, " ");
  210. }
  211. }
  212. tmp = env_get("serverip");
  213. if (tmp)
  214. ptr += sprintf(build_buf + ptr, "h=%s ", tmp);
  215. tmp = env_get("gatewayip");
  216. if (tmp)
  217. ptr += sprintf(build_buf + ptr, "g=%s ", tmp);
  218. tmp = env_get("hostname");
  219. if (tmp)
  220. ptr += sprintf(build_buf + ptr, "tn=%s ", tmp);
  221. tmp = env_get("othbootargs");
  222. if (tmp) {
  223. strcpy(build_buf + ptr, tmp);
  224. ptr += strlen(tmp);
  225. }
  226. bootline = build_buf;
  227. }
  228. memcpy((void *)bootaddr, bootline, max(strlen(bootline), (size_t)255));
  229. flush_cache(bootaddr, max(strlen(bootline), (size_t)255));
  230. printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, (char *)bootaddr);
  231. /*
  232. * If the data at the load address is an elf image, then
  233. * treat it like an elf image. Otherwise, assume that it is a
  234. * binary image.
  235. */
  236. if (valid_elf_image(addr))
  237. addr = load_elf_image_phdr(addr);
  238. else
  239. puts("## Not an ELF image, assuming binary\n");
  240. printf("## Starting vxWorks at 0x%08lx ...\n", addr);
  241. dcache_disable();
  242. #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
  243. armv8_setup_psci();
  244. smp_kick_all_cpus();
  245. #endif
  246. #ifdef CONFIG_X86
  247. /* VxWorks on x86 uses stack to pass parameters */
  248. ((asmlinkage void (*)(int))addr)(0);
  249. #else
  250. ((void (*)(int))addr)(0);
  251. #endif
  252. puts("## vxWorks terminated\n");
  253. return 1;
  254. }
  255. U_BOOT_CMD(
  256. bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
  257. "Boot from an ELF image in memory",
  258. "[-p|-s] [address]\n"
  259. "\t- load ELF image at [address] via program headers (-p)\n"
  260. "\t or via section headers (-s)"
  261. );
  262. U_BOOT_CMD(
  263. bootvx, 2, 0, do_bootvx,
  264. "Boot vxWorks from an ELF image",
  265. " [address] - load address of vxWorks ELF image."
  266. );