blob: da6fb01c11de68c57f9b109cdc5e93084ae0e449 [file] [log] [blame]
Tom Rini5db28902016-08-12 08:31:15 -04001/*
2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <bootm.h>
10#include <command.h>
11#include <image.h>
12#include <lmb.h>
13#include <mapmem.h>
Masahiro Yamada28085762017-03-09 16:28:25 +090014#include <linux/kernel.h>
15#include <linux/sizes.h>
Tom Rini5db28902016-08-12 08:31:15 -040016
17DECLARE_GLOBAL_DATA_PTR;
18
19/* See Documentation/arm64/booting.txt in the Linux kernel */
20struct Image_header {
21 uint32_t code0; /* Executable code */
22 uint32_t code1; /* Executable code */
23 uint64_t text_offset; /* Image load offset, LE */
24 uint64_t image_size; /* Effective Image size, LE */
Masahiro Yamada28085762017-03-09 16:28:25 +090025 uint64_t flags; /* Kernel flags, LE */
Tom Rini5db28902016-08-12 08:31:15 -040026 uint64_t res2; /* reserved */
27 uint64_t res3; /* reserved */
28 uint64_t res4; /* reserved */
29 uint32_t magic; /* Magic number */
30 uint32_t res5;
31};
32
33#define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
34
35static int booti_setup(bootm_headers_t *images)
36{
37 struct Image_header *ih;
38 uint64_t dst;
Masahiro Yamada28085762017-03-09 16:28:25 +090039 uint64_t image_size, text_offset;
Tom Rini5db28902016-08-12 08:31:15 -040040
41 ih = (struct Image_header *)map_sysmem(images->ep, 0);
42
43 if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
44 puts("Bad Linux ARM64 Image magic!\n");
45 return 1;
46 }
Masahiro Yamada28085762017-03-09 16:28:25 +090047
48 /*
49 * Prior to Linux commit a2c1d73b94ed, the text_offset field
50 * is of unknown endianness. In these cases, the image_size
51 * field is zero, and we can assume a fixed value of 0x80000.
52 */
Tom Rini5db28902016-08-12 08:31:15 -040053 if (ih->image_size == 0) {
54 puts("Image lacks image_size field, assuming 16MiB\n");
55 image_size = 16 << 20;
Masahiro Yamada28085762017-03-09 16:28:25 +090056 text_offset = 0x80000;
Tom Rini5db28902016-08-12 08:31:15 -040057 } else {
58 image_size = le64_to_cpu(ih->image_size);
Masahiro Yamada28085762017-03-09 16:28:25 +090059 text_offset = le64_to_cpu(ih->text_offset);
Tom Rini5db28902016-08-12 08:31:15 -040060 }
61
62 /*
Masahiro Yamada28085762017-03-09 16:28:25 +090063 * If bit 3 of the flags field is set, the 2MB aligned base of the
64 * kernel image can be anywhere in physical memory, so respect
65 * images->ep. Otherwise, relocate the image to the base of RAM
66 * since memory below it is not accessible via the linear mapping.
Tom Rini5db28902016-08-12 08:31:15 -040067 */
Masahiro Yamada28085762017-03-09 16:28:25 +090068 if (le64_to_cpu(ih->flags) & BIT(3))
69 dst = images->ep - text_offset;
70 else
71 dst = gd->bd->bi_dram[0].start;
72
73 dst = ALIGN(dst, SZ_2M) + text_offset;
Tom Rini5db28902016-08-12 08:31:15 -040074
75 unmap_sysmem(ih);
76
77 if (images->ep != dst) {
78 void *src;
79
80 debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
81
82 src = (void *)images->ep;
83 images->ep = dst;
84 memmove((void *)dst, src, image_size);
85 }
86
87 return 0;
88}
89
90/*
91 * Image booting support
92 */
93static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
94 char * const argv[], bootm_headers_t *images)
95{
96 int ret;
97 struct Image_header *ih;
98
99 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
100 images, 1);
101
102 /* Setup Linux kernel Image entry point */
103 if (!argc) {
104 images->ep = load_addr;
105 debug("* kernel: default image load address = 0x%08lx\n",
106 load_addr);
107 } else {
108 images->ep = simple_strtoul(argv[0], NULL, 16);
109 debug("* kernel: cmdline image address = 0x%08lx\n",
110 images->ep);
111 }
112
113 ret = booti_setup(images);
114 if (ret != 0)
115 return 1;
116
117 ih = (struct Image_header *)map_sysmem(images->ep, 0);
118
119 lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
120
121 unmap_sysmem(ih);
122
123 /*
124 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
125 * have a header that provide this informaiton.
126 */
127 if (bootm_find_images(flag, argc, argv))
128 return 1;
129
130 return 0;
131}
132
133int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
134{
135 int ret;
136
137 /* Consume 'booti' */
138 argc--; argv++;
139
140 if (booti_start(cmdtp, flag, argc, argv, &images))
141 return 1;
142
143 /*
144 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
145 * disable interrupts ourselves
146 */
147 bootm_disable_interrupts();
148
149 images.os.os = IH_OS_LINUX;
Scott Wood0fff19a2017-01-26 16:55:44 -0600150 images.os.arch = IH_ARCH_ARM64;
Tom Rini5db28902016-08-12 08:31:15 -0400151 ret = do_bootm_states(cmdtp, flag, argc, argv,
Cédric Schieli4943dc22017-01-23 16:51:45 +0100152#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
153 BOOTM_STATE_RAMDISK |
154#endif
Tom Rini5db28902016-08-12 08:31:15 -0400155 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
156 BOOTM_STATE_OS_GO,
157 &images, 1);
158
159 return ret;
160}
161
162#ifdef CONFIG_SYS_LONGHELP
163static char booti_help_text[] =
164 "[addr [initrd[:size]] [fdt]]\n"
165 " - boot arm64 Linux Image stored in memory\n"
166 "\tThe argument 'initrd' is optional and specifies the address\n"
167 "\tof an initrd in memory. The optional parameter ':size' allows\n"
168 "\tspecifying the size of a RAW initrd.\n"
169#if defined(CONFIG_OF_LIBFDT)
170 "\tSince booting a Linux kernel requires a flat device-tree, a\n"
171 "\tthird argument providing the address of the device-tree blob\n"
172 "\tis required. To boot a kernel with a device-tree blob but\n"
173 "\twithout an initrd image, use a '-' for the initrd argument.\n"
174#endif
175 "";
176#endif
177
178U_BOOT_CMD(
179 booti, CONFIG_SYS_MAXARGS, 1, do_booti,
180 "boot arm64 Linux Image image from memory", booti_help_text
181);