booti.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2009
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <common.h>
  7. #include <bootm.h>
  8. #include <command.h>
  9. #include <image.h>
  10. #include <irq_func.h>
  11. #include <lmb.h>
  12. #include <log.h>
  13. #include <mapmem.h>
  14. #include <asm/global_data.h>
  15. #include <linux/kernel.h>
  16. #include <linux/sizes.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /*
  19. * Image booting support
  20. */
  21. static int booti_start(struct cmd_tbl *cmdtp, int flag, int argc,
  22. char *const argv[], bootm_headers_t *images)
  23. {
  24. int ret;
  25. ulong ld;
  26. ulong relocated_addr;
  27. ulong image_size;
  28. uint8_t *temp;
  29. ulong dest;
  30. ulong dest_end;
  31. unsigned long comp_len;
  32. unsigned long decomp_len;
  33. int ctype;
  34. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
  35. images, 1);
  36. /* Setup Linux kernel Image entry point */
  37. if (!argc) {
  38. ld = image_load_addr;
  39. debug("* kernel: default image load address = 0x%08lx\n",
  40. image_load_addr);
  41. } else {
  42. ld = hextoul(argv[0], NULL);
  43. debug("* kernel: cmdline image address = 0x%08lx\n", ld);
  44. }
  45. temp = map_sysmem(ld, 0);
  46. ctype = image_decomp_type(temp, 2);
  47. if (ctype > 0) {
  48. dest = env_get_ulong("kernel_comp_addr_r", 16, 0);
  49. comp_len = env_get_ulong("kernel_comp_size", 16, 0);
  50. if (!dest || !comp_len) {
  51. puts("kernel_comp_addr_r or kernel_comp_size is not provided!\n");
  52. return -EINVAL;
  53. }
  54. if (dest < gd->ram_base || dest > gd->ram_top) {
  55. puts("kernel_comp_addr_r is outside of DRAM range!\n");
  56. return -EINVAL;
  57. }
  58. debug("kernel image compression type %d size = 0x%08lx address = 0x%08lx\n",
  59. ctype, comp_len, (ulong)dest);
  60. decomp_len = comp_len * 10;
  61. ret = image_decomp(ctype, 0, ld, IH_TYPE_KERNEL,
  62. (void *)dest, (void *)ld, comp_len,
  63. decomp_len, &dest_end);
  64. if (ret)
  65. return ret;
  66. /* dest_end contains the uncompressed Image size */
  67. memmove((void *) ld, (void *)dest, dest_end);
  68. }
  69. unmap_sysmem((void *)ld);
  70. ret = booti_setup(ld, &relocated_addr, &image_size, false);
  71. if (ret != 0)
  72. return 1;
  73. /* Handle BOOTM_STATE_LOADOS */
  74. if (relocated_addr != ld) {
  75. printf("Moving Image from 0x%lx to 0x%lx, end=%lx\n", ld,
  76. relocated_addr, relocated_addr + image_size);
  77. memmove((void *)relocated_addr, (void *)ld, image_size);
  78. }
  79. images->ep = relocated_addr;
  80. images->os.start = relocated_addr;
  81. images->os.end = relocated_addr + image_size;
  82. lmb_reserve(&images->lmb, images->ep, le32_to_cpu(image_size));
  83. /*
  84. * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
  85. * have a header that provide this informaiton.
  86. */
  87. if (bootm_find_images(flag, argc, argv, relocated_addr, image_size))
  88. return 1;
  89. return 0;
  90. }
  91. int do_booti(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  92. {
  93. int ret;
  94. /* Consume 'booti' */
  95. argc--; argv++;
  96. if (booti_start(cmdtp, flag, argc, argv, &images))
  97. return 1;
  98. /*
  99. * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
  100. * disable interrupts ourselves
  101. */
  102. bootm_disable_interrupts();
  103. images.os.os = IH_OS_LINUX;
  104. #ifdef CONFIG_RISCV_SMODE
  105. images.os.arch = IH_ARCH_RISCV;
  106. #elif CONFIG_ARM64
  107. images.os.arch = IH_ARCH_ARM64;
  108. #endif
  109. ret = do_bootm_states(cmdtp, flag, argc, argv,
  110. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  111. BOOTM_STATE_RAMDISK |
  112. #endif
  113. BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
  114. BOOTM_STATE_OS_GO,
  115. &images, 1);
  116. return ret;
  117. }
  118. #ifdef CONFIG_SYS_LONGHELP
  119. static char booti_help_text[] =
  120. "[addr [initrd[:size]] [fdt]]\n"
  121. " - boot Linux flat or compressed 'Image' stored at 'addr'\n"
  122. "\tThe argument 'initrd' is optional and specifies the address\n"
  123. "\tof an initrd in memory. The optional parameter ':size' allows\n"
  124. "\tspecifying the size of a RAW initrd.\n"
  125. "\tCurrently only booting from gz, bz2, lzma and lz4 compression\n"
  126. "\ttypes are supported. In order to boot from any of these compressed\n"
  127. "\timages, user have to set kernel_comp_addr_r and kernel_comp_size environment\n"
  128. "\tvariables beforehand.\n"
  129. #if defined(CONFIG_OF_LIBFDT)
  130. "\tSince booting a Linux kernel requires a flat device-tree, a\n"
  131. "\tthird argument providing the address of the device-tree blob\n"
  132. "\tis required. To boot a kernel with a device-tree blob but\n"
  133. "\twithout an initrd image, use a '-' for the initrd argument.\n"
  134. #endif
  135. "";
  136. #endif
  137. U_BOOT_CMD(
  138. booti, CONFIG_SYS_MAXARGS, 1, do_booti,
  139. "boot Linux kernel 'Image' format from memory", booti_help_text
  140. );