blob: a07db930e2756770e35e5153ba92ec8e2d9a59bd [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 *
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005 * Copyright (c) 2014,2016 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
Brian Swetland9c4c0752009-01-25 16:23:50 -0800157boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
158 void *ramdisk, unsigned ramdisk_size,
159 void *second, unsigned second_size,
160 unsigned page_size,
161 unsigned *bootimg_size);
162
163void bootimg_set_cmdline(boot_img_hdr *hdr, const char *cmdline);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800164
165#define KERNEL64_HDR_MAGIC 0x644D5241 /* ARM64 */
166
167struct kernel64_hdr
168{
169 uint32_t insn;
170 uint32_t res1;
171 uint64_t text_offset;
172 uint64_t res2;
173 uint64_t res3;
174 uint64_t res4;
175 uint64_t res5;
176 uint64_t res6;
177 uint32_t magic_64;
178 uint32_t res7;
179};
180
Mayank Grover351a75e2017-05-30 20:06:08 +0530181enum
182{
183 ERR_NO_BOOT_PTN =1,
184 ERR_NO_RECOVERY_PTN,
185 ERR_MMC_READ_FAIL,
186 ERR_INVALID_BOOT_MAGIC,
187 ERR_INVALID_PAGE_SIZE,
188 ERR_ABOOT_ADDR_OVERLAP,
189 ERR_SEND_ROT_FAIL,
190 ERR_DECOMPRESSION,
191 ERR_DT_PARSE,
192 ERR_BOOT_SIGNATURE_MISMATCH,
193};
Brian Swetland9c4c0752009-01-25 16:23:50 -0800194#endif