Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2009 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 7 | #include <bootm.h> |
| 8 | #include <command.h> |
| 9 | #include <image.h> |
Simon Glass | 8f3f761 | 2019-11-14 12:57:42 -0700 | [diff] [blame] | 10 | #include <irq_func.h> |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 11 | #include <lmb.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 13 | #include <mapmem.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Masahiro Yamada | 56c4ed6 | 2017-03-09 16:28:25 +0900 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/sizes.h> |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 17 | |
Atish Patra | 99c469c | 2020-03-05 16:24:23 -0800 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 19 | /* |
| 20 | * Image booting support |
| 21 | */ |
Simon Glass | 50fa2fe | 2023-12-15 20:14:18 -0700 | [diff] [blame] | 22 | static int booti_start(struct bootm_info *bmi) |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 23 | { |
Simon Glass | 50fa2fe | 2023-12-15 20:14:18 -0700 | [diff] [blame] | 24 | struct bootm_headers *images = bmi->images; |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 25 | int ret; |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 26 | ulong ld; |
| 27 | ulong relocated_addr; |
| 28 | ulong image_size; |
Atish Patra | 99c469c | 2020-03-05 16:24:23 -0800 | [diff] [blame] | 29 | uint8_t *temp; |
| 30 | ulong dest; |
| 31 | ulong dest_end; |
| 32 | unsigned long comp_len; |
| 33 | unsigned long decomp_len; |
| 34 | int ctype; |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 35 | |
Simon Glass | d90f94f | 2023-12-15 20:14:19 -0700 | [diff] [blame] | 36 | ret = bootm_run_states(bmi, BOOTM_STATE_START); |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 37 | |
| 38 | /* Setup Linux kernel Image entry point */ |
Simon Glass | 50fa2fe | 2023-12-15 20:14:18 -0700 | [diff] [blame] | 39 | if (!bmi->addr_img) { |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 40 | ld = image_load_addr; |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 41 | debug("* kernel: default image load address = 0x%08lx\n", |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 42 | image_load_addr); |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 43 | } else { |
Simon Glass | 50fa2fe | 2023-12-15 20:14:18 -0700 | [diff] [blame] | 44 | ld = hextoul(bmi->addr_img, NULL); |
Masahiro Yamada | 7691325 | 2018-02-13 12:10:02 +0900 | [diff] [blame] | 45 | debug("* kernel: cmdline image address = 0x%08lx\n", ld); |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Atish Patra | 99c469c | 2020-03-05 16:24:23 -0800 | [diff] [blame] | 48 | temp = map_sysmem(ld, 0); |
| 49 | ctype = image_decomp_type(temp, 2); |
| 50 | if (ctype > 0) { |
| 51 | dest = env_get_ulong("kernel_comp_addr_r", 16, 0); |
| 52 | comp_len = env_get_ulong("kernel_comp_size", 16, 0); |
| 53 | if (!dest || !comp_len) { |
| 54 | puts("kernel_comp_addr_r or kernel_comp_size is not provided!\n"); |
| 55 | return -EINVAL; |
| 56 | } |
| 57 | if (dest < gd->ram_base || dest > gd->ram_top) { |
| 58 | puts("kernel_comp_addr_r is outside of DRAM range!\n"); |
| 59 | return -EINVAL; |
| 60 | } |
| 61 | |
| 62 | debug("kernel image compression type %d size = 0x%08lx address = 0x%08lx\n", |
| 63 | ctype, comp_len, (ulong)dest); |
| 64 | decomp_len = comp_len * 10; |
| 65 | ret = image_decomp(ctype, 0, ld, IH_TYPE_KERNEL, |
| 66 | (void *)dest, (void *)ld, comp_len, |
| 67 | decomp_len, &dest_end); |
| 68 | if (ret) |
| 69 | return ret; |
| 70 | /* dest_end contains the uncompressed Image size */ |
| 71 | memmove((void *) ld, (void *)dest, dest_end); |
| 72 | } |
| 73 | unmap_sysmem((void *)ld); |
| 74 | |
Marek Vasut | 85ed289 | 2018-06-13 06:13:32 +0200 | [diff] [blame] | 75 | ret = booti_setup(ld, &relocated_addr, &image_size, false); |
Heinrich Schuchardt | 34bc960 | 2024-01-11 09:03:43 +0100 | [diff] [blame] | 76 | if (ret) |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 77 | return 1; |
| 78 | |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 79 | /* Handle BOOTM_STATE_LOADOS */ |
| 80 | if (relocated_addr != ld) { |
Dario Binacchi | dccc4d3 | 2024-08-25 14:26:08 +0200 | [diff] [blame] | 81 | printf("Moving Image from 0x%lx to 0x%lx, end=0x%lx\n", ld, |
Tero Kristo | 210c036 | 2020-06-12 15:41:21 +0300 | [diff] [blame] | 82 | relocated_addr, relocated_addr + image_size); |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 83 | memmove((void *)relocated_addr, (void *)ld, image_size); |
| 84 | } |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 85 | |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 86 | images->ep = relocated_addr; |
Lokesh Vutla | 62dd16e | 2019-10-07 13:52:16 +0530 | [diff] [blame] | 87 | images->os.start = relocated_addr; |
| 88 | images->os.end = relocated_addr + image_size; |
| 89 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 90 | lmb_reserve(images->ep, le32_to_cpu(image_size)); |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not |
| 94 | * have a header that provide this informaiton. |
| 95 | */ |
Simon Glass | 50fa2fe | 2023-12-15 20:14:18 -0700 | [diff] [blame] | 96 | if (bootm_find_images(image_load_addr, bmi->conf_ramdisk, bmi->conf_fdt, |
| 97 | relocated_addr, image_size)) |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 98 | return 1; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 103 | int do_booti(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 104 | { |
Simon Glass | 50fa2fe | 2023-12-15 20:14:18 -0700 | [diff] [blame] | 105 | struct bootm_info bmi; |
Simon Glass | 7c2a3f6 | 2023-12-15 20:14:16 -0700 | [diff] [blame] | 106 | int states; |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 107 | int ret; |
| 108 | |
| 109 | /* Consume 'booti' */ |
| 110 | argc--; argv++; |
| 111 | |
Simon Glass | 50fa2fe | 2023-12-15 20:14:18 -0700 | [diff] [blame] | 112 | bootm_init(&bmi); |
| 113 | if (argc) |
| 114 | bmi.addr_img = argv[0]; |
| 115 | if (argc > 1) |
| 116 | bmi.conf_ramdisk = argv[1]; |
| 117 | if (argc > 2) |
| 118 | bmi.conf_fdt = argv[2]; |
| 119 | bmi.boot_progress = true; |
| 120 | bmi.cmd_name = "booti"; |
| 121 | /* do not set up argc and argv[] since nothing uses them */ |
| 122 | |
| 123 | if (booti_start(&bmi)) |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 124 | return 1; |
| 125 | |
| 126 | /* |
| 127 | * We are doing the BOOTM_STATE_LOADOS state ourselves, so must |
| 128 | * disable interrupts ourselves |
| 129 | */ |
| 130 | bootm_disable_interrupts(); |
| 131 | |
| 132 | images.os.os = IH_OS_LINUX; |
Simon Glass | 7c2a3f6 | 2023-12-15 20:14:16 -0700 | [diff] [blame] | 133 | if (IS_ENABLED(CONFIG_RISCV_SMODE)) |
| 134 | images.os.arch = IH_ARCH_RISCV; |
| 135 | else if (IS_ENABLED(CONFIG_ARM64)) |
| 136 | images.os.arch = IH_ARCH_ARM64; |
| 137 | |
| 138 | states = BOOTM_STATE_MEASURE | BOOTM_STATE_OS_PREP | |
| 139 | BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO; |
| 140 | if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH)) |
| 141 | states |= BOOTM_STATE_RAMDISK; |
Simon Glass | 50fa2fe | 2023-12-15 20:14:18 -0700 | [diff] [blame] | 142 | |
Simon Glass | d90f94f | 2023-12-15 20:14:19 -0700 | [diff] [blame] | 143 | ret = bootm_run_states(&bmi, states); |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 148 | U_BOOT_LONGHELP(booti, |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 149 | "[addr [initrd[:size]] [fdt]]\n" |
Atish Patra | 99c469c | 2020-03-05 16:24:23 -0800 | [diff] [blame] | 150 | " - boot Linux flat or compressed 'Image' stored at 'addr'\n" |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 151 | "\tThe argument 'initrd' is optional and specifies the address\n" |
| 152 | "\tof an initrd in memory. The optional parameter ':size' allows\n" |
| 153 | "\tspecifying the size of a RAW initrd.\n" |
Atish Patra | 99c469c | 2020-03-05 16:24:23 -0800 | [diff] [blame] | 154 | "\tCurrently only booting from gz, bz2, lzma and lz4 compression\n" |
| 155 | "\ttypes are supported. In order to boot from any of these compressed\n" |
Vagrant Cascadian | 3dd1678 | 2020-06-04 10:42:01 -0700 | [diff] [blame] | 156 | "\timages, user have to set kernel_comp_addr_r and kernel_comp_size environment\n" |
Atish Patra | 99c469c | 2020-03-05 16:24:23 -0800 | [diff] [blame] | 157 | "\tvariables beforehand.\n" |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 158 | #if defined(CONFIG_OF_LIBFDT) |
| 159 | "\tSince booting a Linux kernel requires a flat device-tree, a\n" |
| 160 | "\tthird argument providing the address of the device-tree blob\n" |
| 161 | "\tis required. To boot a kernel with a device-tree blob but\n" |
| 162 | "\twithout an initrd image, use a '-' for the initrd argument.\n" |
| 163 | #endif |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 164 | ); |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 165 | |
| 166 | U_BOOT_CMD( |
| 167 | booti, CONFIG_SYS_MAXARGS, 1, do_booti, |
Atish Patra | 583b409 | 2019-05-06 17:49:39 -0700 | [diff] [blame] | 168 | "boot Linux kernel 'Image' format from memory", booti_help_text |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 169 | ); |