bootm.c 5.3 KB

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