booti.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. DECLARE_GLOBAL_DATA_PTR;
  14. /* See Documentation/arm64/booting.txt in the Linux kernel */
  15. struct Image_header {
  16. uint32_t code0; /* Executable code */
  17. uint32_t code1; /* Executable code */
  18. uint64_t text_offset; /* Image load offset, LE */
  19. uint64_t image_size; /* Effective Image size, LE */
  20. uint64_t res1; /* reserved */
  21. uint64_t res2; /* reserved */
  22. uint64_t res3; /* reserved */
  23. uint64_t res4; /* reserved */
  24. uint32_t magic; /* Magic number */
  25. uint32_t res5;
  26. };
  27. #define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
  28. static int booti_setup(bootm_headers_t *images)
  29. {
  30. struct Image_header *ih;
  31. uint64_t dst;
  32. uint64_t image_size;
  33. ih = (struct Image_header *)map_sysmem(images->ep, 0);
  34. if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
  35. puts("Bad Linux ARM64 Image magic!\n");
  36. return 1;
  37. }
  38. if (ih->image_size == 0) {
  39. puts("Image lacks image_size field, assuming 16MiB\n");
  40. image_size = 16 << 20;
  41. } else {
  42. image_size = le64_to_cpu(ih->image_size);
  43. }
  44. /*
  45. * If we are not at the correct run-time location, set the new
  46. * correct location and then move the image there.
  47. */
  48. dst = gd->bd->bi_dram[0].start + le64_to_cpu(ih->text_offset);
  49. unmap_sysmem(ih);
  50. if (images->ep != dst) {
  51. void *src;
  52. debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
  53. src = (void *)images->ep;
  54. images->ep = dst;
  55. memmove((void *)dst, src, image_size);
  56. }
  57. return 0;
  58. }
  59. /*
  60. * Image booting support
  61. */
  62. static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
  63. char * const argv[], bootm_headers_t *images)
  64. {
  65. int ret;
  66. struct Image_header *ih;
  67. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
  68. images, 1);
  69. /* Setup Linux kernel Image entry point */
  70. if (!argc) {
  71. images->ep = load_addr;
  72. debug("* kernel: default image load address = 0x%08lx\n",
  73. load_addr);
  74. } else {
  75. images->ep = simple_strtoul(argv[0], NULL, 16);
  76. debug("* kernel: cmdline image address = 0x%08lx\n",
  77. images->ep);
  78. }
  79. ret = booti_setup(images);
  80. if (ret != 0)
  81. return 1;
  82. ih = (struct Image_header *)map_sysmem(images->ep, 0);
  83. lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
  84. unmap_sysmem(ih);
  85. /*
  86. * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
  87. * have a header that provide this informaiton.
  88. */
  89. if (bootm_find_images(flag, argc, argv))
  90. return 1;
  91. return 0;
  92. }
  93. int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  94. {
  95. int ret;
  96. /* Consume 'booti' */
  97. argc--; argv++;
  98. if (booti_start(cmdtp, flag, argc, argv, &images))
  99. return 1;
  100. /*
  101. * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
  102. * disable interrupts ourselves
  103. */
  104. bootm_disable_interrupts();
  105. images.os.os = IH_OS_LINUX;
  106. ret = do_bootm_states(cmdtp, flag, argc, argv,
  107. BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
  108. BOOTM_STATE_OS_GO,
  109. &images, 1);
  110. return ret;
  111. }
  112. #ifdef CONFIG_SYS_LONGHELP
  113. static char booti_help_text[] =
  114. "[addr [initrd[:size]] [fdt]]\n"
  115. " - boot arm64 Linux Image stored in memory\n"
  116. "\tThe argument 'initrd' is optional and specifies the address\n"
  117. "\tof an initrd in memory. The optional parameter ':size' allows\n"
  118. "\tspecifying the size of a RAW initrd.\n"
  119. #if defined(CONFIG_OF_LIBFDT)
  120. "\tSince booting a Linux kernel requires a flat device-tree, a\n"
  121. "\tthird argument providing the address of the device-tree blob\n"
  122. "\tis required. To boot a kernel with a device-tree blob but\n"
  123. "\twithout an initrd image, use a '-' for the initrd argument.\n"
  124. #endif
  125. "";
  126. #endif
  127. U_BOOT_CMD(
  128. booti, CONFIG_SYS_MAXARGS, 1, do_booti,
  129. "boot arm64 Linux Image image from memory", booti_help_text
  130. );