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