bootm.c 4.8 KB

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