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 | |
| 7 | #include <common.h> |
| 8 | #include <bootm.h> |
| 9 | #include <command.h> |
| 10 | #include <image.h> |
Simon Glass | 8f3f761 | 2019-11-14 12:57:42 -0700 | [diff] [blame] | 11 | #include <irq_func.h> |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 12 | #include <lmb.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 14 | #include <mapmem.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Masahiro Yamada | 56c4ed6 | 2017-03-09 16:28:25 +0900 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/sizes.h> |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 18 | |
Atish Patra | 99c469c | 2020-03-05 16:24:23 -0800 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 20 | /* |
| 21 | * Image booting support |
| 22 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 23 | static int booti_start(struct cmd_tbl *cmdtp, int flag, int argc, |
Simon Glass | df00afa | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 24 | char *const argv[], struct bootm_headers *images) |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 25 | { |
| 26 | int ret; |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 27 | ulong ld; |
| 28 | ulong relocated_addr; |
| 29 | ulong image_size; |
Atish Patra | 99c469c | 2020-03-05 16:24:23 -0800 | [diff] [blame] | 30 | uint8_t *temp; |
| 31 | ulong dest; |
| 32 | ulong dest_end; |
| 33 | unsigned long comp_len; |
| 34 | unsigned long decomp_len; |
| 35 | int ctype; |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 36 | |
| 37 | ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START, |
| 38 | images, 1); |
| 39 | |
| 40 | /* Setup Linux kernel Image entry point */ |
| 41 | if (!argc) { |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 42 | ld = image_load_addr; |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 43 | debug("* kernel: default image load address = 0x%08lx\n", |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 44 | image_load_addr); |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 45 | } else { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 46 | ld = hextoul(argv[0], NULL); |
Masahiro Yamada | 7691325 | 2018-02-13 12:10:02 +0900 | [diff] [blame] | 47 | debug("* kernel: cmdline image address = 0x%08lx\n", ld); |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Atish Patra | 99c469c | 2020-03-05 16:24:23 -0800 | [diff] [blame] | 50 | 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 Vasut | 85ed289 | 2018-06-13 06:13:32 +0200 | [diff] [blame] | 77 | ret = booti_setup(ld, &relocated_addr, &image_size, false); |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 78 | if (ret != 0) |
| 79 | return 1; |
| 80 | |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 81 | /* Handle BOOTM_STATE_LOADOS */ |
| 82 | if (relocated_addr != ld) { |
Tero Kristo | 210c036 | 2020-06-12 15:41:21 +0300 | [diff] [blame] | 83 | printf("Moving Image from 0x%lx to 0x%lx, end=%lx\n", ld, |
| 84 | relocated_addr, relocated_addr + image_size); |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 85 | memmove((void *)relocated_addr, (void *)ld, image_size); |
| 86 | } |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 87 | |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 88 | images->ep = relocated_addr; |
Lokesh Vutla | 62dd16e | 2019-10-07 13:52:16 +0530 | [diff] [blame] | 89 | images->os.start = relocated_addr; |
| 90 | images->os.end = relocated_addr + image_size; |
| 91 | |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 92 | lmb_reserve(&images->lmb, images->ep, le32_to_cpu(image_size)); |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not |
| 96 | * have a header that provide this informaiton. |
| 97 | */ |
Tero Kristo | 0608694 | 2020-06-12 15:41:20 +0300 | [diff] [blame] | 98 | if (bootm_find_images(flag, argc, argv, relocated_addr, image_size)) |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 99 | return 1; |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 104 | 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] | 105 | { |
| 106 | int ret; |
| 107 | |
| 108 | /* Consume 'booti' */ |
| 109 | argc--; argv++; |
| 110 | |
| 111 | if (booti_start(cmdtp, flag, argc, argv, &images)) |
| 112 | return 1; |
| 113 | |
| 114 | /* |
| 115 | * We are doing the BOOTM_STATE_LOADOS state ourselves, so must |
| 116 | * disable interrupts ourselves |
| 117 | */ |
| 118 | bootm_disable_interrupts(); |
| 119 | |
| 120 | images.os.os = IH_OS_LINUX; |
Atish Patra | 583b409 | 2019-05-06 17:49:39 -0700 | [diff] [blame] | 121 | #ifdef CONFIG_RISCV_SMODE |
| 122 | images.os.arch = IH_ARCH_RISCV; |
| 123 | #elif CONFIG_ARM64 |
Scott Wood | 4ba6a86 | 2017-01-26 16:55:44 -0600 | [diff] [blame] | 124 | images.os.arch = IH_ARCH_ARM64; |
Atish Patra | 583b409 | 2019-05-06 17:49:39 -0700 | [diff] [blame] | 125 | #endif |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 126 | ret = do_bootm_states(cmdtp, flag, argc, argv, |
Cédric Schieli | 80d5e6b | 2017-01-23 16:51:45 +0100 | [diff] [blame] | 127 | #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH |
| 128 | BOOTM_STATE_RAMDISK | |
| 129 | #endif |
Eddie James | 32401ba | 2023-10-24 10:43:50 -0500 | [diff] [blame] | 130 | BOOTM_STATE_MEASURE | |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 131 | BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO | |
| 132 | BOOTM_STATE_OS_GO, |
| 133 | &images, 1); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 138 | U_BOOT_LONGHELP(booti, |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 139 | "[addr [initrd[:size]] [fdt]]\n" |
Atish Patra | 99c469c | 2020-03-05 16:24:23 -0800 | [diff] [blame] | 140 | " - boot Linux flat or compressed 'Image' stored at 'addr'\n" |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 141 | "\tThe argument 'initrd' is optional and specifies the address\n" |
| 142 | "\tof an initrd in memory. The optional parameter ':size' allows\n" |
| 143 | "\tspecifying the size of a RAW initrd.\n" |
Atish Patra | 99c469c | 2020-03-05 16:24:23 -0800 | [diff] [blame] | 144 | "\tCurrently only booting from gz, bz2, lzma and lz4 compression\n" |
| 145 | "\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] | 146 | "\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] | 147 | "\tvariables beforehand.\n" |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 148 | #if defined(CONFIG_OF_LIBFDT) |
| 149 | "\tSince booting a Linux kernel requires a flat device-tree, a\n" |
| 150 | "\tthird argument providing the address of the device-tree blob\n" |
| 151 | "\tis required. To boot a kernel with a device-tree blob but\n" |
| 152 | "\twithout an initrd image, use a '-' for the initrd argument.\n" |
| 153 | #endif |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 154 | ); |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 155 | |
| 156 | U_BOOT_CMD( |
| 157 | booti, CONFIG_SYS_MAXARGS, 1, do_booti, |
Atish Patra | 583b409 | 2019-05-06 17:49:39 -0700 | [diff] [blame] | 158 | "boot Linux kernel 'Image' format from memory", booti_help_text |
Tom Rini | 36f067f | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 159 | ); |