booti.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * (C) Copyright 2000-2009
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <bootm.h>
  9. #include <command.h>
  10. #include <image.h>
  11. #include <lmb.h>
  12. #include <mapmem.h>
  13. #include <linux/kernel.h>
  14. #include <linux/sizes.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. /* See Documentation/arm64/booting.txt in the Linux kernel */
  17. struct Image_header {
  18. uint32_t code0; /* Executable code */
  19. uint32_t code1; /* Executable code */
  20. uint64_t text_offset; /* Image load offset, LE */
  21. uint64_t image_size; /* Effective Image size, LE */
  22. uint64_t flags; /* Kernel flags, LE */
  23. uint64_t res2; /* reserved */
  24. uint64_t res3; /* reserved */
  25. uint64_t res4; /* reserved */
  26. uint32_t magic; /* Magic number */
  27. uint32_t res5;
  28. };
  29. #define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
  30. static int booti_setup(bootm_headers_t *images)
  31. {
  32. struct Image_header *ih;
  33. uint64_t dst;
  34. uint64_t image_size, text_offset;
  35. ih = (struct Image_header *)map_sysmem(images->ep, 0);
  36. if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
  37. puts("Bad Linux ARM64 Image magic!\n");
  38. return 1;
  39. }
  40. /*
  41. * Prior to Linux commit a2c1d73b94ed, the text_offset field
  42. * is of unknown endianness. In these cases, the image_size
  43. * field is zero, and we can assume a fixed value of 0x80000.
  44. */
  45. if (ih->image_size == 0) {
  46. puts("Image lacks image_size field, assuming 16MiB\n");
  47. image_size = 16 << 20;
  48. text_offset = 0x80000;
  49. } else {
  50. image_size = le64_to_cpu(ih->image_size);
  51. text_offset = le64_to_cpu(ih->text_offset);
  52. }
  53. /*
  54. * If bit 3 of the flags field is set, the 2MB aligned base of the
  55. * kernel image can be anywhere in physical memory, so respect
  56. * images->ep. Otherwise, relocate the image to the base of RAM
  57. * since memory below it is not accessible via the linear mapping.
  58. */
  59. if (le64_to_cpu(ih->flags) & BIT(3))
  60. dst = images->ep - text_offset;
  61. else
  62. dst = gd->bd->bi_dram[0].start;
  63. dst = ALIGN(dst, SZ_2M) + text_offset;
  64. unmap_sysmem(ih);
  65. if (images->ep != dst) {
  66. void *src;
  67. debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
  68. src = (void *)images->ep;
  69. images->ep = dst;
  70. memmove((void *)dst, src, image_size);
  71. }
  72. return 0;
  73. }
  74. /*
  75. * Image booting support
  76. */
  77. static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
  78. char * const argv[], bootm_headers_t *images)
  79. {
  80. int ret;
  81. struct Image_header *ih;
  82. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
  83. images, 1);
  84. /* Setup Linux kernel Image entry point */
  85. if (!argc) {
  86. images->ep = load_addr;
  87. debug("* kernel: default image load address = 0x%08lx\n",
  88. load_addr);
  89. } else {
  90. images->ep = simple_strtoul(argv[0], NULL, 16);
  91. debug("* kernel: cmdline image address = 0x%08lx\n",
  92. images->ep);
  93. }
  94. ret = booti_setup(images);
  95. if (ret != 0)
  96. return 1;
  97. ih = (struct Image_header *)map_sysmem(images->ep, 0);
  98. lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
  99. unmap_sysmem(ih);
  100. /*
  101. * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
  102. * have a header that provide this informaiton.
  103. */
  104. if (bootm_find_images(flag, argc, argv))
  105. return 1;
  106. return 0;
  107. }
  108. int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  109. {
  110. int ret;
  111. /* Consume 'booti' */
  112. argc--; argv++;
  113. if (booti_start(cmdtp, flag, argc, argv, &images))
  114. return 1;
  115. /*
  116. * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
  117. * disable interrupts ourselves
  118. */
  119. bootm_disable_interrupts();
  120. images.os.os = IH_OS_LINUX;
  121. images.os.arch = IH_ARCH_ARM64;
  122. ret = do_bootm_states(cmdtp, flag, argc, argv,
  123. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  124. BOOTM_STATE_RAMDISK |
  125. #endif
  126. BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
  127. BOOTM_STATE_OS_GO,
  128. &images, 1);
  129. return ret;
  130. }
  131. #ifdef CONFIG_SYS_LONGHELP
  132. static char booti_help_text[] =
  133. "[addr [initrd[:size]] [fdt]]\n"
  134. " - boot arm64 Linux Image stored in memory\n"
  135. "\tThe argument 'initrd' is optional and specifies the address\n"
  136. "\tof an initrd in memory. The optional parameter ':size' allows\n"
  137. "\tspecifying the size of a RAW initrd.\n"
  138. #if defined(CONFIG_OF_LIBFDT)
  139. "\tSince booting a Linux kernel requires a flat device-tree, a\n"
  140. "\tthird argument providing the address of the device-tree blob\n"
  141. "\tis required. To boot a kernel with a device-tree blob but\n"
  142. "\twithout an initrd image, use a '-' for the initrd argument.\n"
  143. #endif
  144. "";
  145. #endif
  146. U_BOOT_CMD(
  147. booti, CONFIG_SYS_MAXARGS, 1, do_booti,
  148. "boot arm64 Linux Image image from memory", booti_help_text
  149. );