bootm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2002
  4. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  5. * Marius Groeger <mgroeger@sysgo.de>
  6. *
  7. * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  8. */
  9. #include <common.h>
  10. #include <bootstage.h>
  11. #include <command.h>
  12. #include <hang.h>
  13. #include <log.h>
  14. #include <dm/device.h>
  15. #include <dm/root.h>
  16. #include <errno.h>
  17. #include <fdt_support.h>
  18. #include <image.h>
  19. #include <u-boot/zlib.h>
  20. #include <asm/bootparam.h>
  21. #include <asm/cpu.h>
  22. #include <asm/byteorder.h>
  23. #include <asm/zimage.h>
  24. #ifdef CONFIG_SYS_COREBOOT
  25. #include <asm/arch/timestamp.h>
  26. #endif
  27. DECLARE_GLOBAL_DATA_PTR;
  28. #define COMMAND_LINE_OFFSET 0x9000
  29. void bootm_announce_and_cleanup(void)
  30. {
  31. printf("\nStarting kernel ...\n\n");
  32. #ifdef CONFIG_SYS_COREBOOT
  33. timestamp_add_now(TS_U_BOOT_START_KERNEL);
  34. #endif
  35. bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
  36. #if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
  37. bootstage_report();
  38. #endif
  39. /*
  40. * Call remove function of all devices with a removal flag set.
  41. * This may be useful for last-stage operations, like cancelling
  42. * of DMA operation or releasing device internal buffers.
  43. */
  44. dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
  45. }
  46. #if defined(CONFIG_OF_LIBFDT) && !defined(CONFIG_OF_NO_KERNEL)
  47. int arch_fixup_memory_node(void *blob)
  48. {
  49. struct bd_info *bd = gd->bd;
  50. int bank;
  51. u64 start[CONFIG_NR_DRAM_BANKS];
  52. u64 size[CONFIG_NR_DRAM_BANKS];
  53. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  54. start[bank] = bd->bi_dram[bank].start;
  55. size[bank] = bd->bi_dram[bank].size;
  56. }
  57. return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
  58. }
  59. #endif
  60. /* Subcommand: PREP */
  61. static int boot_prep_linux(bootm_headers_t *images)
  62. {
  63. char *cmd_line_dest = NULL;
  64. image_header_t *hdr;
  65. int is_zimage = 0;
  66. void *data = NULL;
  67. size_t len;
  68. int ret;
  69. #ifdef CONFIG_OF_LIBFDT
  70. if (images->ft_len) {
  71. debug("using: FDT\n");
  72. if (image_setup_linux(images)) {
  73. puts("FDT creation failed! hanging...");
  74. hang();
  75. }
  76. }
  77. #endif
  78. if (images->legacy_hdr_valid) {
  79. hdr = images->legacy_hdr_os;
  80. if (image_check_type(hdr, IH_TYPE_MULTI)) {
  81. ulong os_data, os_len;
  82. /* if multi-part image, we need to get first subimage */
  83. image_multi_getimg(hdr, 0, &os_data, &os_len);
  84. data = (void *)os_data;
  85. len = os_len;
  86. } else {
  87. /* otherwise get image data */
  88. data = (void *)image_get_data(hdr);
  89. len = image_get_data_size(hdr);
  90. }
  91. is_zimage = 1;
  92. #if defined(CONFIG_FIT)
  93. } else if (images->fit_uname_os && is_zimage) {
  94. ret = fit_image_get_data(images->fit_hdr_os,
  95. images->fit_noffset_os,
  96. (const void **)&data, &len);
  97. if (ret) {
  98. puts("Can't get image data/size!\n");
  99. goto error;
  100. }
  101. is_zimage = 1;
  102. #endif
  103. }
  104. if (is_zimage) {
  105. ulong load_address;
  106. char *base_ptr;
  107. base_ptr = (char *)load_zimage(data, len, &load_address);
  108. if (!base_ptr) {
  109. puts("## Kernel loading failed ...\n");
  110. goto error;
  111. }
  112. images->os.load = load_address;
  113. cmd_line_dest = base_ptr + COMMAND_LINE_OFFSET;
  114. images->ep = (ulong)base_ptr;
  115. } else if (images->ep) {
  116. cmd_line_dest = (void *)images->ep + COMMAND_LINE_OFFSET;
  117. } else {
  118. printf("## Kernel loading failed (missing x86 kernel setup) ...\n");
  119. goto error;
  120. }
  121. printf("Setup at %#08lx\n", images->ep);
  122. ret = setup_zimage((void *)images->ep, cmd_line_dest,
  123. 0, images->rd_start,
  124. images->rd_end - images->rd_start);
  125. if (ret) {
  126. printf("## Setting up boot parameters failed ...\n");
  127. return 1;
  128. }
  129. return 0;
  130. error:
  131. return 1;
  132. }
  133. int boot_linux_kernel(ulong setup_base, ulong load_address, bool image_64bit)
  134. {
  135. bootm_announce_and_cleanup();
  136. #ifdef CONFIG_SYS_COREBOOT
  137. timestamp_add_now(TS_U_BOOT_START_KERNEL);
  138. #endif
  139. if (image_64bit) {
  140. if (!cpu_has_64bit()) {
  141. puts("Cannot boot 64-bit kernel on 32-bit machine\n");
  142. return -EFAULT;
  143. }
  144. /* At present 64-bit U-Boot does not support booting a
  145. * kernel.
  146. * TODO(sjg@chromium.org): Support booting both 32-bit and
  147. * 64-bit kernels from 64-bit U-Boot.
  148. */
  149. #if !CONFIG_IS_ENABLED(X86_64)
  150. return cpu_jump_to_64bit(setup_base, load_address);
  151. #endif
  152. } else {
  153. /*
  154. * Set %ebx, %ebp, and %edi to 0, %esi to point to the
  155. * boot_params structure, and then jump to the kernel. We
  156. * assume that %cs is 0x10, 4GB flat, and read/execute, and
  157. * the data segments are 0x18, 4GB flat, and read/write.
  158. * U-Boot is setting them up that way for itself in
  159. * arch/i386/cpu/cpu.c.
  160. *
  161. * Note that we cannot currently boot a kernel while running as
  162. * an EFI application. Please use the payload option for that.
  163. */
  164. #ifndef CONFIG_EFI_APP
  165. __asm__ __volatile__ (
  166. "movl $0, %%ebp\n"
  167. "cli\n"
  168. "jmp *%[kernel_entry]\n"
  169. :: [kernel_entry]"a"(load_address),
  170. [boot_params] "S"(setup_base),
  171. "b"(0), "D"(0)
  172. );
  173. #endif
  174. }
  175. /* We can't get to here */
  176. return -EFAULT;
  177. }
  178. /* Subcommand: GO */
  179. static int boot_jump_linux(bootm_headers_t *images)
  180. {
  181. debug("## Transferring control to Linux (at address %08lx, kernel %08lx) ...\n",
  182. images->ep, images->os.load);
  183. return boot_linux_kernel(images->ep, images->os.load,
  184. images->os.arch == IH_ARCH_X86_64);
  185. }
  186. int do_bootm_linux(int flag, int argc, char *const argv[],
  187. bootm_headers_t *images)
  188. {
  189. /* No need for those on x86 */
  190. if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
  191. return -1;
  192. if (flag & BOOTM_STATE_OS_PREP)
  193. return boot_prep_linux(images);
  194. if (flag & BOOTM_STATE_OS_GO)
  195. return boot_jump_linux(images);
  196. return boot_jump_linux(images);
  197. }