blob: 1ad5a6427aa6c852b923098ad26f639b3012d68c [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
Parth Dixit1375d9e2019-07-08 20:56:13 +05305 * Copyright (c) 2014,2016,2019 The Linux Foundation. All rights reserved.
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08006 *
Brian Swetland9c4c0752009-01-25 16:23:50 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#ifndef _BOOT_IMAGE_H_
32#define _BOOT_IMAGE_H_
33
Brian Swetland9c4c0752009-01-25 16:23:50 -080034#define BOOT_MAGIC "ANDROID!"
35#define BOOT_MAGIC_SIZE 8
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -080036#define BOOT_NAME_SIZE 16
37#define BOOT_ARGS_SIZE 512
38#define BOOT_IMG_MAX_PAGE_SIZE 4096
Mayank Grovere1ab96c2018-09-04 20:31:31 +053039#define BOOT_EXTRA_ARGS_SIZE 1024
Brian Swetland9c4c0752009-01-25 16:23:50 -080040
Mayank Grovere1ab96c2018-09-04 20:31:31 +053041#define BOOT_HEADER_VERSION_ZERO 0
42/* Struct def for boot image header
43 * Bootloader expects the structure of boot_img_hdr with header version
44 * BOOT_HEADER_VERSION_ZERO to be as follows:
45 */
46struct boot_img_hdr_v0 {
Brian Swetland9c4c0752009-01-25 16:23:50 -080047 unsigned char magic[BOOT_MAGIC_SIZE];
48
49 unsigned kernel_size; /* size in bytes */
50 unsigned kernel_addr; /* physical load addr */
51
52 unsigned ramdisk_size; /* size in bytes */
53 unsigned ramdisk_addr; /* physical load addr */
54
55 unsigned second_size; /* size in bytes */
56 unsigned second_addr; /* physical load addr */
57
58 unsigned tags_addr; /* physical addr for kernel tags */
59 unsigned page_size; /* flash page size we assume */
Channagoud Kadabi86b0c112016-03-16 19:23:16 -070060#if OSVERSION_IN_BOOTIMAGE
Mayank Grovere1ab96c2018-09-04 20:31:31 +053061 uint32_t header_version; /* version for the boot image header */
Channagoud Kadabi86b0c112016-03-16 19:23:16 -070062 uint32_t os_version; /* version << 11 | patch_level */
63#else
Neeti Desai465491e2012-07-31 12:53:35 -070064 unsigned dt_size; /* device_tree in bytes */
65 unsigned unused; /* future expansion: should be 0 */
Channagoud Kadabi86b0c112016-03-16 19:23:16 -070066#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -080067 unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */
68
69 unsigned char cmdline[BOOT_ARGS_SIZE];
70
71 unsigned id[8]; /* timestamp / checksum / sha1 / etc */
Mayank Grovere1ab96c2018-09-04 20:31:31 +053072
73 /* Supplemental command line data; kept here to maintain
74 * binary compatibility with older versions of mkbootimg
75 */
76 uint8_t extra_cmdline[BOOT_EXTRA_ARGS_SIZE];
77} __attribute__((packed));
78
79/*
80 * It is expected that callers would explicitly specify which version of the
81 * boot image header they need to use.
82 */
83typedef struct boot_img_hdr_v0 boot_img_hdr;
84
85/**
86 * Offset of recovery DTBO length in a boot image header of version V1 or
87 * above.
88 */
89#define BOOT_IMAGE_HEADER_V1_RECOVERY_DTBO_SIZE_OFFSET sizeof (boot_img_hdr)
Brian Swetland9c4c0752009-01-25 16:23:50 -080090
91/*
92** +-----------------+
93** | boot header | 1 page
94** +-----------------+
95** | kernel | n pages
96** +-----------------+
97** | ramdisk | m pages
98** +-----------------+
99** | second stage | o pages
100** +-----------------+
Brian Swetland9c4c0752009-01-25 16:23:50 -0800101** n = (kernel_size + page_size - 1) / page_size
102** m = (ramdisk_size + page_size - 1) / page_size
103** o = (second_size + page_size - 1) / page_size
Brian Swetland9c4c0752009-01-25 16:23:50 -0800104** 0. all entities are page_size aligned in flash
105** 1. kernel and ramdisk are required (size != 0)
106** 2. second is optional (second_size == 0 -> no second)
107** 3. load each element (kernel, ramdisk, second) at
108** the specified physical address (kernel_addr, etc)
109** 4. prepare tags at tag_addr. kernel_args[] is
110** appended to the kernel commandline in the tags.
111** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
112** 6. if second_size != 0: jump to second_addr
113** else: jump to kernel_addr
114*/
115
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530116#define BOOT_HEADER_VERSION_ONE 1
117
118struct boot_img_hdr_v1 {
119 uint32_t recovery_dtbo_size; /* size in bytes for recovery DTBO image */
120 uint64_t recovery_dtbo_offset; /* physical load addr */
121 uint32_t header_size;
122} __attribute__((packed));
123
124/* When the boot image header has a version of BOOT_HEADER_VERSION_ONE,
125 * the structure of the boot image is as follows:
126 *
127 * +-----------------+
128 * | boot header | 1 page
129 * +-----------------+
130 * | kernel | n pages
131 * +-----------------+
132 * | ramdisk | m pages
133 * +-----------------+
134 * | second stage | o pages
135 * +-----------------+
136 * | recovery dtbo | p pages
137 * +-----------------+
138 * n = (kernel_size + page_size - 1) / page_size
139 * m = (ramdisk_size + page_size - 1) / page_size
140 * o = (second_size + page_size - 1) / page_size
141 * p = (recovery_dtbo_size + page_size - 1) / page_size
142 *
143 * 0. all entities are page_size aligned in flash
144 * 1. kernel and ramdisk are required (size != 0)
145 * 2. recovery_dtbo is required for recovery.img
146 * in non-A/B devices(recovery_dtbo_size != 0)
147 * 3. second is optional (second_size == 0 -> no second)
148 * 4. load each element (kernel, ramdisk, second, recovery_dtbo) at
149 * the specified physical address (kernel_addr, etc)
150 * 5. prepare tags at tag_addr. kernel_args[] is
151 * appended to the kernel commandline in the tags.
152 * 6. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
153 * 7. if second_size != 0: jump to second_addr
154 * else: jump to kernel_addr
155 */
156
Parth Dixit1375d9e2019-07-08 20:56:13 +0530157#define BOOT_IMAGE_HEADER_V2_OFFSET sizeof (struct boot_img_hdr_v1)
158#define BOOT_HEADER_VERSION_TWO 2
159
160struct boot_img_hdr_v2 {
161 uint32_t dtb_size; /* size in bytes for DTB image */
162 uint64_t dtb_addr; /* physical load address for DTB image */
163} __attribute__((packed));
164
165/* When the boot image header has a version of BOOT_HEADER_VERSION_TWO,
166 * the structure of the boot image is as follows:
167 *
168 * +-----------------+
169 * | boot header | 1 page
170 * +-----------------+
171 * | kernel | n pages
172 * +-----------------+
173 * | ramdisk | m pages
174 * +-----------------+
175 * | second stage | o pages
176 * +-----------------+
177 * | recovery dtbo | p pages
178 * +-----------------+
179 * | dtb.img | q pages
180 * +-----------------+
181 * n = (kernel_size + page_size - 1) / page_size
182 * m = (ramdisk_size + page_size - 1) / page_size
183 * o = (second_size + page_size - 1) / page_size
184 * p = (recovery_dtbo_size + page_size - 1) / page_size
185 * q = (dtb_size + page_size - 1) / page_size
186 *
187 * 0. all entities are page_size aligned in flash
188 * 1. kernel and ramdisk are required (size != 0)
189 * 2. recovery_dtbo is required for recovery.img
190 * in non-A/B devices(recovery_dtbo_size != 0)
191 * 3. second is optional (second_size == 0 -> no second)
192 * 4. load each element (kernel, ramdisk, second, recovery_dtbo) at
193 * the specified physical address (kernel_addr, etc)
194 * 5. prepare tags at tag_addr. kernel_args[] is
195 * appended to the kernel commandline in the tags.
196 * 6. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
197 * 7. if second_size != 0: jump to second_addr
198 * else: jump to kernel_addr
199 */
200
Brian Swetland9c4c0752009-01-25 16:23:50 -0800201boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
202 void *ramdisk, unsigned ramdisk_size,
203 void *second, unsigned second_size,
204 unsigned page_size,
205 unsigned *bootimg_size);
206
207void bootimg_set_cmdline(boot_img_hdr *hdr, const char *cmdline);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800208
209#define KERNEL64_HDR_MAGIC 0x644D5241 /* ARM64 */
210
211struct kernel64_hdr
212{
213 uint32_t insn;
214 uint32_t res1;
215 uint64_t text_offset;
216 uint64_t res2;
217 uint64_t res3;
218 uint64_t res4;
219 uint64_t res5;
220 uint64_t res6;
221 uint32_t magic_64;
222 uint32_t res7;
223};
224
Mayank Grover351a75e2017-05-30 20:06:08 +0530225enum
226{
227 ERR_NO_BOOT_PTN =1,
228 ERR_NO_RECOVERY_PTN,
229 ERR_MMC_READ_FAIL,
230 ERR_INVALID_BOOT_MAGIC,
231 ERR_INVALID_PAGE_SIZE,
232 ERR_ABOOT_ADDR_OVERLAP,
233 ERR_SEND_ROT_FAIL,
234 ERR_DECOMPRESSION,
235 ERR_DT_PARSE,
236 ERR_BOOT_SIGNATURE_MISMATCH,
237};
Brian Swetland9c4c0752009-01-25 16:23:50 -0800238#endif