booti.c 4.3 KB

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