bootm.c 4.7 KB

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