blob: e6f67d6e13687a48e8018d9a247df4c0ac502847 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Rini36f067f2016-08-12 08:31:15 -04002/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Tom Rini36f067f2016-08-12 08:31:15 -04005 */
6
Tom Rini36f067f2016-08-12 08:31:15 -04007#include <bootm.h>
8#include <command.h>
Tom Rini22856382025-05-14 16:46:03 -06009#include <env.h>
Tom Rini36f067f2016-08-12 08:31:15 -040010#include <image.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070011#include <irq_func.h>
Tom Rini36f067f2016-08-12 08:31:15 -040012#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Tom Rini36f067f2016-08-12 08:31:15 -040014#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Masahiro Yamada56c4ed62017-03-09 16:28:25 +090016#include <linux/kernel.h>
17#include <linux/sizes.h>
Tom Rini36f067f2016-08-12 08:31:15 -040018
Atish Patra99c469c2020-03-05 16:24:23 -080019DECLARE_GLOBAL_DATA_PTR;
Tom Rini36f067f2016-08-12 08:31:15 -040020/*
21 * Image booting support
22 */
Simon Glass50fa2fe2023-12-15 20:14:18 -070023static int booti_start(struct bootm_info *bmi)
Tom Rini36f067f2016-08-12 08:31:15 -040024{
Simon Glass50fa2fe2023-12-15 20:14:18 -070025 struct bootm_headers *images = bmi->images;
Tom Rini36f067f2016-08-12 08:31:15 -040026 int ret;
Bin Chendac184b2018-01-27 16:59:09 +110027 ulong ld;
28 ulong relocated_addr;
29 ulong image_size;
Atish Patra99c469c2020-03-05 16:24:23 -080030 uint8_t *temp;
31 ulong dest;
32 ulong dest_end;
Sughosh Ganu9b0765a2025-06-17 16:13:40 +053033 phys_addr_t ep_addr;
Atish Patra99c469c2020-03-05 16:24:23 -080034 unsigned long comp_len;
35 unsigned long decomp_len;
36 int ctype;
Tom Rini36f067f2016-08-12 08:31:15 -040037
Simon Glassd90f94f2023-12-15 20:14:19 -070038 ret = bootm_run_states(bmi, BOOTM_STATE_START);
Tom Rini36f067f2016-08-12 08:31:15 -040039
40 /* Setup Linux kernel Image entry point */
Simon Glass50fa2fe2023-12-15 20:14:18 -070041 if (!bmi->addr_img) {
Simon Glass892265d2019-12-28 10:45:02 -070042 ld = image_load_addr;
Tom Rini36f067f2016-08-12 08:31:15 -040043 debug("* kernel: default image load address = 0x%08lx\n",
Simon Glass892265d2019-12-28 10:45:02 -070044 image_load_addr);
Tom Rini36f067f2016-08-12 08:31:15 -040045 } else {
Simon Glass50fa2fe2023-12-15 20:14:18 -070046 ld = hextoul(bmi->addr_img, NULL);
Masahiro Yamada76913252018-02-13 12:10:02 +090047 debug("* kernel: cmdline image address = 0x%08lx\n", ld);
Tom Rini36f067f2016-08-12 08:31:15 -040048 }
49
Atish Patra99c469c2020-03-05 16:24:23 -080050 temp = map_sysmem(ld, 0);
51 ctype = image_decomp_type(temp, 2);
52 if (ctype > 0) {
53 dest = env_get_ulong("kernel_comp_addr_r", 16, 0);
54 comp_len = env_get_ulong("kernel_comp_size", 16, 0);
55 if (!dest || !comp_len) {
56 puts("kernel_comp_addr_r or kernel_comp_size is not provided!\n");
57 return -EINVAL;
58 }
59 if (dest < gd->ram_base || dest > gd->ram_top) {
60 puts("kernel_comp_addr_r is outside of DRAM range!\n");
61 return -EINVAL;
62 }
63
64 debug("kernel image compression type %d size = 0x%08lx address = 0x%08lx\n",
65 ctype, comp_len, (ulong)dest);
66 decomp_len = comp_len * 10;
67 ret = image_decomp(ctype, 0, ld, IH_TYPE_KERNEL,
68 (void *)dest, (void *)ld, comp_len,
69 decomp_len, &dest_end);
70 if (ret)
71 return ret;
72 /* dest_end contains the uncompressed Image size */
73 memmove((void *) ld, (void *)dest, dest_end);
74 }
75 unmap_sysmem((void *)ld);
76
Marek Vasut85ed2892018-06-13 06:13:32 +020077 ret = booti_setup(ld, &relocated_addr, &image_size, false);
Heinrich Schuchardt34bc9602024-01-11 09:03:43 +010078 if (ret)
Tom Rini36f067f2016-08-12 08:31:15 -040079 return 1;
80
Bin Chendac184b2018-01-27 16:59:09 +110081 /* Handle BOOTM_STATE_LOADOS */
82 if (relocated_addr != ld) {
Dario Binacchidccc4d32024-08-25 14:26:08 +020083 printf("Moving Image from 0x%lx to 0x%lx, end=0x%lx\n", ld,
Tero Kristo210c0362020-06-12 15:41:21 +030084 relocated_addr, relocated_addr + image_size);
Bin Chendac184b2018-01-27 16:59:09 +110085 memmove((void *)relocated_addr, (void *)ld, image_size);
86 }
Tom Rini36f067f2016-08-12 08:31:15 -040087
Bin Chendac184b2018-01-27 16:59:09 +110088 images->ep = relocated_addr;
Lokesh Vutla62dd16e2019-10-07 13:52:16 +053089 images->os.start = relocated_addr;
90 images->os.end = relocated_addr + image_size;
91
Sughosh Ganu9b0765a2025-06-17 16:13:40 +053092 ep_addr = (phys_addr_t)images->ep;
93 ret = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &ep_addr,
94 le32_to_cpu(image_size), LMB_NONE);
95 if (ret) {
96 printf("Failed to allocate memory for the image at %#llx\n",
97 (unsigned long long)images->ep);
98 return 1;
99 }
Tom Rini36f067f2016-08-12 08:31:15 -0400100
101 /*
102 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
103 * have a header that provide this informaiton.
104 */
Simon Glass50fa2fe2023-12-15 20:14:18 -0700105 if (bootm_find_images(image_load_addr, bmi->conf_ramdisk, bmi->conf_fdt,
106 relocated_addr, image_size))
Tom Rini36f067f2016-08-12 08:31:15 -0400107 return 1;
108
109 return 0;
110}
111
Simon Glassed38aef2020-05-10 11:40:03 -0600112int do_booti(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Tom Rini36f067f2016-08-12 08:31:15 -0400113{
Simon Glass50fa2fe2023-12-15 20:14:18 -0700114 struct bootm_info bmi;
Simon Glass7c2a3f62023-12-15 20:14:16 -0700115 int states;
Tom Rini36f067f2016-08-12 08:31:15 -0400116 int ret;
117
118 /* Consume 'booti' */
119 argc--; argv++;
120
Simon Glass50fa2fe2023-12-15 20:14:18 -0700121 bootm_init(&bmi);
122 if (argc)
123 bmi.addr_img = argv[0];
124 if (argc > 1)
125 bmi.conf_ramdisk = argv[1];
126 if (argc > 2)
127 bmi.conf_fdt = argv[2];
128 bmi.boot_progress = true;
129 bmi.cmd_name = "booti";
130 /* do not set up argc and argv[] since nothing uses them */
131
132 if (booti_start(&bmi))
Tom Rini36f067f2016-08-12 08:31:15 -0400133 return 1;
134
135 /*
136 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
137 * disable interrupts ourselves
138 */
139 bootm_disable_interrupts();
140
141 images.os.os = IH_OS_LINUX;
Mayuresh Chitale0290e632025-05-29 03:30:49 +0000142 if (IS_ENABLED(CONFIG_RISCV_SMODE))
143 images.os.arch = IH_ARCH_RISCV;
Simon Glass7c2a3f62023-12-15 20:14:16 -0700144 else if (IS_ENABLED(CONFIG_ARM64))
145 images.os.arch = IH_ARCH_ARM64;
146
147 states = BOOTM_STATE_MEASURE | BOOTM_STATE_OS_PREP |
148 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO;
149 if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
150 states |= BOOTM_STATE_RAMDISK;
Simon Glass50fa2fe2023-12-15 20:14:18 -0700151
Simon Glassd90f94f2023-12-15 20:14:19 -0700152 ret = bootm_run_states(&bmi, states);
Tom Rini36f067f2016-08-12 08:31:15 -0400153
154 return ret;
155}
156
Tom Rini03f146c2023-10-07 15:13:08 -0400157U_BOOT_LONGHELP(booti,
Tom Rini36f067f2016-08-12 08:31:15 -0400158 "[addr [initrd[:size]] [fdt]]\n"
Atish Patra99c469c2020-03-05 16:24:23 -0800159 " - boot Linux flat or compressed 'Image' stored at 'addr'\n"
Tom Rini36f067f2016-08-12 08:31:15 -0400160 "\tThe argument 'initrd' is optional and specifies the address\n"
161 "\tof an initrd in memory. The optional parameter ':size' allows\n"
162 "\tspecifying the size of a RAW initrd.\n"
Atish Patra99c469c2020-03-05 16:24:23 -0800163 "\tCurrently only booting from gz, bz2, lzma and lz4 compression\n"
164 "\ttypes are supported. In order to boot from any of these compressed\n"
Vagrant Cascadian3dd16782020-06-04 10:42:01 -0700165 "\timages, user have to set kernel_comp_addr_r and kernel_comp_size environment\n"
Atish Patra99c469c2020-03-05 16:24:23 -0800166 "\tvariables beforehand.\n"
Tom Rini36f067f2016-08-12 08:31:15 -0400167#if defined(CONFIG_OF_LIBFDT)
168 "\tSince booting a Linux kernel requires a flat device-tree, a\n"
169 "\tthird argument providing the address of the device-tree blob\n"
170 "\tis required. To boot a kernel with a device-tree blob but\n"
171 "\twithout an initrd image, use a '-' for the initrd argument.\n"
172#endif
Tom Rini03f146c2023-10-07 15:13:08 -0400173 );
Tom Rini36f067f2016-08-12 08:31:15 -0400174
175U_BOOT_CMD(
176 booti, CONFIG_SYS_MAXARGS, 1, do_booti,
Atish Patra583b4092019-05-06 17:49:39 -0700177 "boot Linux kernel 'Image' format from memory", booti_help_text
Tom Rini36f067f2016-08-12 08:31:15 -0400178);