1/* SPDX-License-Identifier: BSD-3-Clause */
2/*
3 * This is from the Android Project,
4 * Repository: https://android.googlesource.com/platform/system/tools/mkbootimg
5 * File: include/bootimg/bootimg.h
6 * Commit: cce5b1923e3cd2fcb765b512610bdc5c42bc501d
7 *
8 * Copyright (C) 2007 The Android Open Source Project
9 */
10
11#ifndef _ANDROID_IMAGE_H_
12#define _ANDROID_IMAGE_H_
13
14#include <linux/compiler.h>
15#include <linux/types.h>
16
17#define ANDR_GKI_PAGE_SIZE 4096
18#define ANDR_BOOT_MAGIC "ANDROID!"
19#define ANDR_BOOT_MAGIC_SIZE 8
20#define ANDR_BOOT_NAME_SIZE 16
21#define ANDR_BOOT_ARGS_SIZE 512
22#define ANDR_BOOT_EXTRA_ARGS_SIZE 1024
23#define VENDOR_BOOT_MAGIC "VNDRBOOT"
24#define ANDR_VENDOR_BOOT_MAGIC_SIZE 8
25#define ANDR_VENDOR_BOOT_ARGS_SIZE 2048
26#define ANDR_VENDOR_BOOT_NAME_SIZE 16
27
28#define BOOTCONFIG_MAGIC "#BOOTCONFIG\n"
29#define BOOTCONFIG_MAGIC_SIZE 12
30#define BOOTCONFIG_SIZE_SIZE 4
31#define BOOTCONFIG_CHECKSUM_SIZE 4
32#define BOOTCONFIG_TRAILER_SIZE BOOTCONFIG_MAGIC_SIZE + \
33				BOOTCONFIG_SIZE_SIZE + \
34				BOOTCONFIG_CHECKSUM_SIZE
35
36struct andr_boot_img_hdr_v3 {
37	u8 magic[ANDR_BOOT_MAGIC_SIZE];
38
39	u32 kernel_size;    /* size in bytes */
40	u32 ramdisk_size;   /* size in bytes */
41
42	u32 os_version;
43
44	u32 header_size;    /* size of boot image header in bytes */
45	u32 reserved[4];
46	u32 header_version; /* offset remains constant for version check */
47
48	u8 cmdline[ANDR_BOOT_ARGS_SIZE + ANDR_BOOT_EXTRA_ARGS_SIZE];
49	/* for boot image header v4 only */
50	u32 signature_size; /* size in bytes */
51};
52
53struct andr_vnd_boot_img_hdr {
54	u8 magic[ANDR_VENDOR_BOOT_MAGIC_SIZE];
55	u32 header_version;
56	u32 page_size;           /* flash page size we assume */
57
58	u32 kernel_addr;         /* physical load addr */
59	u32 ramdisk_addr;        /* physical load addr */
60
61	u32 vendor_ramdisk_size; /* size in bytes */
62
63	u8 cmdline[ANDR_VENDOR_BOOT_ARGS_SIZE];
64
65	u32 tags_addr;           /* physical addr for kernel tags */
66
67	u8 name[ANDR_VENDOR_BOOT_NAME_SIZE]; /* asciiz product name */
68	u32 header_size;         /* size of vendor boot image header in bytes */
69	u32 dtb_size;            /* size of dtb image */
70	u64 dtb_addr;            /* physical load address */
71	/* for boot image header v4 only */
72	u32 vendor_ramdisk_table_size; /* size in bytes for the vendor ramdisk table */
73	u32 vendor_ramdisk_table_entry_num; /* number of entries in the vendor ramdisk table */
74	u32 vendor_ramdisk_table_entry_size; /* size in bytes for a vendor ramdisk table entry */
75	u32 bootconfig_size; /* size in bytes for the bootconfig section */
76};
77
78/* The bootloader expects the structure of andr_boot_img_hdr_v0 with header
79 * version 0 to be as follows: */
80struct andr_boot_img_hdr_v0 {
81    /* Must be ANDR_BOOT_MAGIC. */
82    char magic[ANDR_BOOT_MAGIC_SIZE];
83
84    u32 kernel_size; /* size in bytes */
85    u32 kernel_addr; /* physical load addr */
86
87    u32 ramdisk_size; /* size in bytes */
88    u32 ramdisk_addr; /* physical load addr */
89
90    u32 second_size; /* size in bytes */
91    u32 second_addr; /* physical load addr */
92
93    u32 tags_addr; /* physical addr for kernel tags */
94    u32 page_size; /* flash page size we assume */
95
96    /* Version of the boot image header. */
97    u32 header_version;
98
99    /* Operating system version and security patch level.
100     * For version "A.B.C" and patch level "Y-M-D":
101     *   (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M)
102     *   os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0] */
103    u32 os_version;
104
105    char name[ANDR_BOOT_NAME_SIZE]; /* asciiz product name */
106
107    char cmdline[ANDR_BOOT_ARGS_SIZE];
108
109    u32 id[8]; /* timestamp / checksum / sha1 / etc */
110
111    /* Supplemental command line data; kept here to maintain
112     * binary compatibility with older versions of mkbootimg. */
113    char extra_cmdline[ANDR_BOOT_EXTRA_ARGS_SIZE];
114
115    /* Fields in boot_img_hdr_v1 and newer. */
116    u32 recovery_dtbo_size;   /* size in bytes for recovery DTBO/ACPIO image */
117    u64 recovery_dtbo_offset; /* offset to recovery dtbo/acpio in boot image */
118    u32 header_size;
119
120    /* Fields in boot_img_hdr_v2 and newer. */
121    u32 dtb_size; /* size in bytes for DTB image */
122    u64 dtb_addr; /* physical load address for DTB image */
123} __attribute__((packed));
124
125/* When a boot header is of version 0, the structure of boot image is as
126 * follows:
127 *
128 * +-----------------+
129 * | boot header     | 1 page
130 * +-----------------+
131 * | kernel          | n pages
132 * +-----------------+
133 * | ramdisk         | m pages
134 * +-----------------+
135 * | second stage    | o pages
136 * +-----------------+
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 *
142 * 0. all entities are page_size aligned in flash
143 * 1. kernel and ramdisk are required (size != 0)
144 * 2. second is optional (second_size == 0 -> no second)
145 * 3. load each element (kernel, ramdisk, second) at
146 *    the specified physical address (kernel_addr, etc)
147 * 4. prepare tags at tag_addr.  kernel_args[] is
148 *    appended to the kernel commandline in the tags.
149 * 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
150 * 6. if second_size != 0: jump to second_addr
151 *    else: jump to kernel_addr
152 */
153
154/* When the boot image header has a version of 2, the structure of the boot
155 * image is as follows:
156 *
157 * +---------------------+
158 * | boot header         | 1 page
159 * +---------------------+
160 * | kernel              | n pages
161 * +---------------------+
162 * | ramdisk             | m pages
163 * +---------------------+
164 * | second stage        | o pages
165 * +---------------------+
166 * | recovery dtbo/acpio | p pages
167 * +---------------------+
168 * | dtb                 | q pages
169 * +---------------------+
170 *
171 * n = (kernel_size + page_size - 1) / page_size
172 * m = (ramdisk_size + page_size - 1) / page_size
173 * o = (second_size + page_size - 1) / page_size
174 * p = (recovery_dtbo_size + page_size - 1) / page_size
175 * q = (dtb_size + page_size - 1) / page_size
176 *
177 * 0. all entities are page_size aligned in flash
178 * 1. kernel, ramdisk and DTB are required (size != 0)
179 * 2. recovery_dtbo/recovery_acpio is required for recovery.img in non-A/B
180 *    devices(recovery_dtbo_size != 0)
181 * 3. second is optional (second_size == 0 -> no second)
182 * 4. load each element (kernel, ramdisk, second, dtb) at
183 *    the specified physical address (kernel_addr, etc)
184 * 5. If booting to recovery mode in a non-A/B device, extract recovery
185 *    dtbo/acpio and apply the correct set of overlays on the base device tree
186 *    depending on the hardware/product revision.
187 * 6. prepare tags at tag_addr.  kernel_args[] is
188 *    appended to the kernel commandline in the tags.
189 * 7. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
190 * 8. if second_size != 0: jump to second_addr
191 *    else: jump to kernel_addr
192 */
193
194/* When the boot image header has a version of 3, the structure of the boot
195 * image is as follows:
196 *
197 * +---------------------+
198 * | boot header         | 4096 bytes
199 * +---------------------+
200 * | kernel              | m pages
201 * +---------------------+
202 * | ramdisk             | n pages
203 * +---------------------+
204 *
205 * m = (kernel_size + 4096 - 1) / 4096
206 * n = (ramdisk_size + 4096 - 1) / 4096
207 *
208 * Note that in version 3 of the boot image header, page size is fixed at 4096 bytes.
209 *
210 * The structure of the vendor boot image (introduced with version 3 and
211 * required to be present when a v3 boot image is used) is as follows:
212 *
213 * +---------------------+
214 * | vendor boot header  | o pages
215 * +---------------------+
216 * | vendor ramdisk      | p pages
217 * +---------------------+
218 * | dtb                 | q pages
219 * +---------------------+
220 * o = (2112 + page_size - 1) / page_size
221 * p = (vendor_ramdisk_size + page_size - 1) / page_size
222 * q = (dtb_size + page_size - 1) / page_size
223 *
224 * 0. all entities in the boot image are 4096-byte aligned in flash, all
225 *    entities in the vendor boot image are page_size (determined by the vendor
226 *    and specified in the vendor boot image header) aligned in flash
227 * 1. kernel, ramdisk, vendor ramdisk, and DTB are required (size != 0)
228 * 2. load the kernel and DTB at the specified physical address (kernel_addr,
229 *    dtb_addr)
230 * 3. load the vendor ramdisk at ramdisk_addr
231 * 4. load the generic ramdisk immediately following the vendor ramdisk in
232 *    memory
233 * 5. set up registers for kernel entry as required by your architecture
234 * 6. if the platform has a second stage bootloader jump to it (must be
235 *    contained outside boot and vendor boot partitions), otherwise
236 *    jump to kernel_addr
237 */
238
239/* When the boot image header has a version of 4, the structure of the boot
240 * image is as follows:
241 *
242 * +---------------------+
243 * | boot header         | 4096 bytes
244 * +---------------------+
245 * | kernel              | m pages
246 * +---------------------+
247 * | ramdisk             | n pages
248 * +---------------------+
249 * | boot signature      | g pages
250 * +---------------------+
251 *
252 * m = (kernel_size + 4096 - 1) / 4096
253 * n = (ramdisk_size + 4096 - 1) / 4096
254 * g = (signature_size + 4096 - 1) / 4096
255 *
256 * Note that in version 4 of the boot image header, page size is fixed at 4096
257 * bytes.
258 *
259 * The structure of the vendor boot image version 4, which is required to be
260 * present when a version 4 boot image is used, is as follows:
261 *
262 * +------------------------+
263 * | vendor boot header     | o pages
264 * +------------------------+
265 * | vendor ramdisk section | p pages
266 * +------------------------+
267 * | dtb                    | q pages
268 * +------------------------+
269 * | vendor ramdisk table   | r pages
270 * +------------------------+
271 * | bootconfig             | s pages
272 * +------------------------+
273 *
274 * o = (2128 + page_size - 1) / page_size
275 * p = (vendor_ramdisk_size + page_size - 1) / page_size
276 * q = (dtb_size + page_size - 1) / page_size
277 * r = (vendor_ramdisk_table_size + page_size - 1) / page_size
278 * s = (vendor_bootconfig_size + page_size - 1) / page_size
279 *
280 * Note that in version 4 of the vendor boot image, multiple vendor ramdisks can
281 * be included in the vendor boot image. The bootloader can select a subset of
282 * ramdisks to load at runtime. To help the bootloader select the ramdisks, each
283 * ramdisk is tagged with a type tag and a set of hardware identifiers
284 * describing the board, soc or platform that this ramdisk is intended for.
285 *
286 * The vendor ramdisk section is consist of multiple ramdisk images concatenated
287 * one after another, and vendor_ramdisk_size is the size of the section, which
288 * is the total size of all the ramdisks included in the vendor boot image.
289 *
290 * The vendor ramdisk table holds the size, offset, type, name and hardware
291 * identifiers of each ramdisk. The type field denotes the type of its content.
292 * The vendor ramdisk names are unique. The hardware identifiers are specified
293 * in the board_id field in each table entry. The board_id field is consist of a
294 * vector of unsigned integer words, and the encoding scheme is defined by the
295 * hardware vendor.
296 *
297 * For the different type of ramdisks, there are:
298 *    - VENDOR_RAMDISK_TYPE_NONE indicates the value is unspecified.
299 *    - VENDOR_RAMDISK_TYPE_PLATFORM ramdisks contain platform specific bits, so
300 *      the bootloader should always load these into memory.
301 *    - VENDOR_RAMDISK_TYPE_RECOVERY ramdisks contain recovery resources, so
302 *      the bootloader should load these when booting into recovery.
303 *    - VENDOR_RAMDISK_TYPE_DLKM ramdisks contain dynamic loadable kernel
304 *      modules.
305 *
306 * Version 4 of the vendor boot image also adds a bootconfig section to the end
307 * of the image. This section contains Boot Configuration parameters known at
308 * build time. The bootloader is responsible for placing this section directly
309 * after the generic ramdisk, followed by the bootconfig trailer, before
310 * entering the kernel.
311 *
312 * 0. all entities in the boot image are 4096-byte aligned in flash, all
313 *    entities in the vendor boot image are page_size (determined by the vendor
314 *    and specified in the vendor boot image header) aligned in flash
315 * 1. kernel, ramdisk, and DTB are required (size != 0)
316 * 2. load the kernel and DTB at the specified physical address (kernel_addr,
317 *    dtb_addr)
318 * 3. load the vendor ramdisks at ramdisk_addr
319 * 4. load the generic ramdisk immediately following the vendor ramdisk in
320 *    memory
321 * 5. load the bootconfig immediately following the generic ramdisk. Add
322 *    additional bootconfig parameters followed by the bootconfig trailer.
323 * 6. set up registers for kernel entry as required by your architecture
324 * 7. if the platform has a second stage bootloader jump to it (must be
325 *    contained outside boot and vendor boot partitions), otherwise
326 *    jump to kernel_addr
327 */
328
329/* Private struct */
330struct andr_image_data {
331	ulong kernel_ptr;  /* kernel address */
332	u32 kernel_size;  /* size in bytes */
333	u32 ramdisk_size;  /* size in bytes */
334	ulong vendor_ramdisk_ptr;  /* vendor ramdisk address */
335	u32 vendor_ramdisk_size;  /* vendor ramdisk size*/
336	u32 boot_ramdisk_size;  /* size in bytes */
337	ulong second_ptr;  /* secondary bootloader address */
338	u32 second_size;  /* secondary bootloader size */
339	ulong dtb_ptr;  /* address of dtb image */
340	u32 dtb_size;  /* size of dtb image */
341	ulong recovery_dtbo_ptr;  /* size in bytes for recovery DTBO/ACPIO image */
342	u32 recovery_dtbo_size;  /* offset to recovery dtbo/acpio in boot image */
343
344	const char *kcmdline;  /* boot kernel cmdline */
345	const char *kcmdline_extra;  /* vendor-boot extra kernel cmdline */
346	const char *image_name;  /* asciiz product name */
347
348	ulong bootconfig_addr;  /* bootconfig image address */
349	ulong bootconfig_size;  /* bootconfig image size */
350
351	u32 kernel_addr;  /* physical load addr */
352	ulong ramdisk_addr;  /* physical load addr */
353	ulong ramdisk_ptr;  /* ramdisk address */
354	ulong dtb_load_addr;  /* physical load address for DTB image */
355	ulong tags_addr;  /* physical addr for kernel tags */
356	u32 header_version;  /* version of the boot image header */
357	u32 boot_img_total_size;  /* boot image size */
358	u32 vendor_boot_img_total_size;  /* vendor boot image size */
359};
360
361#endif
362