bootm.c 4.9 KB

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