xref: /OK3568_Linux_fs/u-boot/include/android_image.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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: e55998a0f2b61b685d5eb4a486ca3a0c680b1a2f
7  *
8  * Copyright (C) 2007 The Android Open Source Project
9  */
10 
11 #ifndef _ANDROID_IMAGE_H_
12 #define _ANDROID_IMAGE_H_
13 
14 #define ANDROID_PARTITION_BOOT "boot"
15 #define ANDROID_PARTITION_VENDOR_BOOT "vendor_boot"
16 #define ANDROID_PARTITION_INIT_BOOT "init_boot"
17 #define ANDROID_PARTITION_MISC "misc"
18 #define ANDROID_PARTITION_OEM  "oem"
19 #define ANDROID_PARTITION_RESOURCE  "resource"
20 #define ANDROID_PARTITION_RECOVERY  "recovery"
21 #define ANDROID_PARTITION_SYSTEM "system"
22 #define ANDROID_PARTITION_VBMETA "vbmeta"
23 #define ANDROID_PARTITION_SUPER "super"
24 
25 #define ANDROID_ARG_SLOT_SUFFIX "androidboot.slot_suffix="
26 #define ANDROID_ARG_ROOT "root="
27 #define ANDROID_ARG_SERIALNO "androidboot.serialno="
28 #define ANDROID_VERIFY_STATE "androidboot.verifiedbootstate="
29 #ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE
30 #define ANDROID_ARG_FDT_FILENAME "rk-kernel.dtb"
31 #else
32 #define ANDROID_ARG_FDT_FILENAME "kernel.dtb"
33 #endif
34 #define OEM_UNLOCK_ARG_SIZE 30
35 #define UUID_SIZE 37
36 
37 #define ANDR_BOOT_MAGIC "ANDROID!"
38 #define VENDOR_BOOT_MAGIC "VNDRBOOT"
39 #define ANDR_BOOT_MAGIC_SIZE 8
40 #define VENDOR_BOOT_MAGIC_SIZE 8
41 #define ANDR_BOOT_NAME_SIZE 16
42 #define VENDOR_BOOT_NAME_SIZE 16
43 #define ANDR_BOOT_ARGS_SIZE 512
44 #define ANDR_BOOT_EXTRA_ARGS_SIZE 1024
45 #define VENDOR_BOOT_ARGS_SIZE 2048
46 #define ANDR_BOOT_IMG_PAGE_SIZE 4096
47 #define ANDR_BOOT_IMG_HDR_SIZE (ANDR_BOOT_IMG_PAGE_SIZE)
48 #define TOTAL_BOOT_ARGS_SIZE (ANDR_BOOT_ARGS_SIZE + ANDR_BOOT_EXTRA_ARGS_SIZE + \
49                               VENDOR_BOOT_ARGS_SIZE + 1)
50 #define VENDOR_BOOT_HDRv3_SIZE	2112
51 #define VENDOR_BOOT_HDRv4_SIZE	2124
52 
53 #define VENDOR_RAMDISK_TYPE_NONE 0
54 #define VENDOR_RAMDISK_TYPE_PLATFORM 1
55 #define VENDOR_RAMDISK_TYPE_RECOVERY 2
56 #define VENDOR_RAMDISK_TYPE_DLKM 3
57 #define VENDOR_RAMDISK_NAME_SIZE 32
58 #define VENDOR_RAMDISK_TABLE_ENTRY_BOARD_ID_SIZE 16
59 
60 /* defined by rockchip but not google, it's adjustable */
61 #define ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE SZ_8K
62 
63 /*
64  * It is expected that callers would explicitly specify which version of the
65  * boot image header they need to use.
66  */
67 typedef struct andr_img_hdr andr_img_hdr;
68 
69 /* The bootloader expects the structure of andr_img_hdr with header
70  * version 0 to be as follows: */
71 struct andr_img_hdr {
72     /* Must be ANDR_BOOT_MAGIC. */
73     char magic[ANDR_BOOT_MAGIC_SIZE];
74 
75     u32 kernel_size; /* size in bytes */
76     u32 kernel_addr; /* physical load addr */
77 
78     u32 ramdisk_size; /* size in bytes */
79     u32 ramdisk_addr; /* physical load addr */
80 
81     u32 second_size; /* size in bytes */
82     u32 second_addr; /* physical load addr */
83 
84     u32 tags_addr; /* physical addr for kernel tags */
85     u32 page_size; /* flash page size we assume */
86 
87     /* Version of the boot image header. */
88     u32 header_version;
89 
90     /* Operating system version and security patch level.
91      * For version "A.B.C" and patch level "Y-M-D":
92      *   (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M)
93      *   os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0] */
94     u32 os_version;
95 
96     char name[ANDR_BOOT_NAME_SIZE]; /* asciiz product name */
97 
98     char cmdline[ANDR_BOOT_ARGS_SIZE];
99 
100     u32 id[8]; /* timestamp / checksum / sha1 / etc */
101 
102     /* Supplemental command line data; kept here to maintain
103      * binary compatibility with older versions of mkbootimg. */
104     char extra_cmdline[ANDR_BOOT_EXTRA_ARGS_SIZE];
105 
106     /* Fields in boot_img_hdr_v1(Android-9) and newer. */
107     u32 recovery_dtbo_size;   /* size in bytes for recovery DTBO/ACPIO image */
108     u64 recovery_dtbo_offset; /* offset to recovery dtbo/acpio in boot image */
109     u32 header_size;
110 
111     /* Fields in boot_img_hdr_v2(Android-10) and newer. */
112     u32 dtb_size; /* size in bytes for DTB image */
113     u64 dtb_addr; /* physical load address for DTB image */
114 
115     /*
116      * [Rockchip compatibility Android v3]
117      *
118      * boot_img_hdr_v3(Android-11) is not compatible with boot_img_hdr_v012,
119      * we have to partly merge fields from boot_img_hdr_v34 and vendor_boot_img_hdr_v34
120      * into this structure to compatible with boot_img_hdr_v012.
121      */
122     u32 vendor_ramdisk_size; /* size in bytes */
123     u32 vendor_page_size;
124     u32 vendor_header_version;
125     u32 vendor_header_size;
126 
127     /*
128      * [Rockchip compatibility Android v4]
129      *
130      * boot_img_hdr_v4(Android-12) is not compatible with boot_img_hdr_v012,
131      * we have to partly merge fields from boot_img_hdr_v34 and vendor_boot_img_hdr_v34
132      * into this structure to compatible with boot_img_hdr_v012.
133      */
134     u32 vendor_ramdisk_table_size;
135     u32 vendor_ramdisk_table_entry_num;
136     u32 vendor_ramdisk_table_entry_size;
137     u32 vendor_bootconfig_size; /* size in bytes for bootconfig image */
138 
139     void *init_boot_buf;
140     void *vendor_boot_buf;
141 
142     /*
143      * Don't define 'char total_cmdline[TOTAL_BOOT_ARGS_SIZE]' to avoid
144      * this structrue is over size than page_size.
145      */
146     char *total_cmdline;
147 } __attribute__((packed));
148 
149 struct boot_img_hdr_v34 {
150     /* Must be ANDR_BOOT_MAGIC. */
151     uint8_t magic[ANDR_BOOT_MAGIC_SIZE];
152 
153     uint32_t kernel_size; /* size in bytes */
154     uint32_t ramdisk_size; /* size in bytes */
155 
156     /* Operating system version and security patch level.
157      * For version "A.B.C" and patch level "Y-M-D":
158      *   (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M)
159      *   os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0] */
160     uint32_t os_version;
161 
162     uint32_t header_size;
163 
164     uint32_t reserved[4];
165 
166     uint32_t header_version;
167 
168     uint8_t cmdline[ANDR_BOOT_ARGS_SIZE + ANDR_BOOT_EXTRA_ARGS_SIZE];
169 } __attribute__((packed));
170 
171 struct vendor_boot_img_hdr_v34 {
172     /* Must be VENDOR_BOOT_MAGIC. */
173     uint8_t magic[VENDOR_BOOT_MAGIC_SIZE];
174 
175     /* Version of the vendor boot image header. */
176     uint32_t header_version;
177 
178     uint32_t page_size; /* flash page size we assume */
179 
180     uint32_t kernel_addr; /* physical load addr */
181     uint32_t ramdisk_addr; /* physical load addr */
182 
183     uint32_t vendor_ramdisk_size; /* size in bytes */
184 
185     uint8_t cmdline[VENDOR_BOOT_ARGS_SIZE];
186 
187     uint32_t tags_addr; /* physical addr for kernel tags (if required) */
188     uint8_t name[VENDOR_BOOT_NAME_SIZE]; /* asciiz product name */
189 
190     uint32_t header_size;
191 
192     uint32_t dtb_size; /* size in bytes for DTB image */
193     uint64_t dtb_addr; /* physical load address for DTB image */
194 
195     /* new for v4 */
196     uint32_t vendor_ramdisk_table_size; /* size in bytes for the vendor ramdisk table */
197     uint32_t vendor_ramdisk_table_entry_num; /* number of entries in the vendor ramdisk table */
198     uint32_t vendor_ramdisk_table_entry_size;
199     uint32_t vendor_bootconfig_size; /* size in bytes for bootconfig image */
200 } __attribute__((packed));
201 
202 struct vendor_ramdisk_table_entry_v4 {
203     uint32_t ramdisk_size; /* size in bytes for the ramdisk image */
204     uint32_t ramdisk_offset; /* offset to the ramdisk image in vendor ramdisk section */
205     uint32_t ramdisk_type; /* type of the ramdisk */
206     uint8_t ramdisk_name[VENDOR_RAMDISK_NAME_SIZE]; /* asciiz ramdisk name */
207 
208     // Hardware identifiers describing the board, soc or platform which this
209     // ramdisk is intended to be loaded on.
210     uint32_t board_id[VENDOR_RAMDISK_TABLE_ENTRY_BOARD_ID_SIZE];
211 } __attribute__((packed));
212 
213 /* When the boot image header has a version of 4, the structure of the boot
214  * image is the same as version 3:
215  *
216  * +---------------------+
217  * | boot header         | 4096 bytes
218  * +---------------------+
219  * | kernel              | m pages
220  * +---------------------+
221  * | ramdisk             | n pages
222  * +---------------------+
223  *
224  * m = (kernel_size + 4096 - 1) / 4096
225  * n = (ramdisk_size + 4096 - 1) / 4096
226  *
227  * Note that in version 4 of the boot image header, page size is fixed at 4096
228  * bytes.
229  *
230  * The structure of the vendor boot image version 4, which is required to be
231  * present when a version 4 boot image is used, is as follows:
232  *
233  * +------------------------+
234  * | vendor boot header     | o pages
235  * +------------------------+
236  * | vendor ramdisk section | p pages
237  * +------------------------+
238  * | dtb                    | q pages
239  * +------------------------+
240  * | vendor ramdisk table   | r pages
241  * +------------------------+
242  * | bootconfig             | s pages
243  * +------------------------+
244  *
245  * o = (2124 + page_size - 1) / page_size
246  * p = (vendor_ramdisk_size + page_size - 1) / page_size
247  * q = (dtb_size + page_size - 1) / page_size
248  * r = (vendor_ramdisk_table_size + page_size - 1) / page_size
249  * s = (vendor_bootconfig_size + page_size - 1) / page_size
250  *
251  * Note that in version 4 of the vendor boot image, multiple vendor ramdisks can
252  * be included in the vendor boot image. The bootloader can select a subset of
253  * ramdisks to load at runtime. To help the bootloader select the ramdisks, each
254  * ramdisk is tagged with a type tag and a set of hardware identifiers
255  * describing the board, soc or platform that this ramdisk is intended for.
256  *
257  * The vendor ramdisk section is consist of multiple ramdisk images concatenated
258  * one after another, and vendor_ramdisk_size is the size of the section, which
259  * is the total size of all the ramdisks included in the vendor boot image.
260  *
261  * The vendor ramdisk table holds the size, offset, type, name and hardware
262  * identifiers of each ramdisk. The type field denotes the type of its content.
263  * The hardware identifiers are specified in the board_id field in each table
264  * entry. The board_id field is consist of a vector of unsigned integer words,
265  * and the encoding scheme is defined by the hardware vendor.
266  *
267  * For the different type of ramdisks, there are:
268  *    - VENDOR_RAMDISK_TYPE_NONE indicates the value is unspecified.
269  *    - VENDOR_RAMDISK_TYPE_PLATFORM ramdisk contains platform specific bits.
270  *    - VENDOR_RAMDISK_TYPE_RECOVERY ramdisk contains recovery resources.
271  *    - VENDOR_RAMDISK_TYPE_DLKM ramdisk contains dynamic loadable kernel
272  *      modules.
273  *
274  * Version 4 of the vendor boot image also adds a bootconfig section to the end
275  * of the image. This section contains Boot Configuration parameters known at
276  * build time. The bootloader is responsible for placing this section directly
277  * after the boot image ramdisk, followed by the bootconfig trailer, before
278  * entering the kernel.
279  *
280  * 0. all entities in the boot image are 4096-byte aligned in flash, all
281  *    entities in the vendor boot image are page_size (determined by the vendor
282  *    and specified in the vendor boot image header) aligned in flash
283  * 1. kernel, ramdisk, and DTB are required (size != 0)
284  * 2. load the kernel and DTB at the specified physical address (kernel_addr,
285  *    dtb_addr)
286  * 3. load the vendor ramdisks at ramdisk_addr
287  * 4. load the generic ramdisk immediately following the vendor ramdisk in
288  *    memory
289  * 5. load the vendor bootconfig immediately following the generic ramdisk. Add
290  *    additional bootconfig parameters followed by the bootconfig trailer.
291  * 6. set up registers for kernel entry as required by your architecture
292  * 7. if the platform has a second stage bootloader jump to it (must be
293  *    contained outside boot and vendor boot partitions), otherwise
294  *    jump to kernel_addr
295  *
296  * When a boot header is of version 0, the structure of boot image is as
297  * follows:
298  *
299  * +-----------------+
300  * | boot header     | 1 page
301  * +-----------------+
302  * | kernel          | n pages
303  * +-----------------+
304  * | ramdisk         | m pages
305  * +-----------------+
306  * | second stage    | o pages
307  * +-----------------+
308  *
309  * n = (kernel_size + page_size - 1) / page_size
310  * m = (ramdisk_size + page_size - 1) / page_size
311  * o = (second_size + page_size - 1) / page_size
312  *
313  * 0. all entities are page_size aligned in flash
314  * 1. kernel and ramdisk are required (size != 0)
315  * 2. second is optional (second_size == 0 -> no second)
316  * 3. load each element (kernel, ramdisk, second) at
317  *    the specified physical address (kernel_addr, etc)
318  * 4. prepare tags at tag_addr.  kernel_args[] is
319  *    appended to the kernel commandline in the tags.
320  * 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
321  * 6. if second_size != 0: jump to second_addr
322  *    else: jump to kernel_addr
323  */
324 
325 /* When the boot image header has a version of 2, the structure of the boot
326  * image is as follows:
327  *
328  * +---------------------+
329  * | boot header         | 1 page
330  * +---------------------+
331  * | kernel              | n pages
332  * +---------------------+
333  * | ramdisk             | m pages
334  * +---------------------+
335  * | second stage        | o pages
336  * +---------------------+
337  * | recovery dtbo/acpio | p pages
338  * +---------------------+
339  * | dtb                 | q pages
340  * +---------------------+
341 
342  * n = (kernel_size + page_size - 1) / page_size
343  * m = (ramdisk_size + page_size - 1) / page_size
344  * o = (second_size + page_size - 1) / page_size
345  * p = (recovery_dtbo_size + page_size - 1) / page_size
346  * q = (dtb_size + page_size - 1) / page_size
347  *
348  * 0. all entities are page_size aligned in flash
349  * 1. kernel, ramdisk and DTB are required (size != 0)
350  * 2. recovery_dtbo/recovery_acpio is required for recovery.img in non-A/B
351  *    devices(recovery_dtbo_size != 0)
352  * 3. second is optional (second_size == 0 -> no second)
353  * 4. load each element (kernel, ramdisk, second, dtb) at
354  *    the specified physical address (kernel_addr, etc)
355  * 5. If booting to recovery mode in a non-A/B device, extract recovery
356  *    dtbo/acpio and apply the correct set of overlays on the base device tree
357  *    depending on the hardware/product revision.
358  * 6. prepare tags at tag_addr.  kernel_args[] is
359  *    appended to the kernel commandline in the tags.
360  * 7. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
361  * 8. if second_size != 0: jump to second_addr
362  *    else: jump to kernel_addr
363  */
364 
365 /* When the boot image header has a version of 3, the structure of the boot
366  * image is as follows:
367  *
368  * +---------------------+
369  * | boot header         | 4096 bytes
370  * +---------------------+
371  * | kernel              | m pages
372  * +---------------------+
373  * | ramdisk             | n pages
374  * +---------------------+
375  *
376  * m = (kernel_size + 4096 - 1) / 4096
377  * n = (ramdisk_size + 4096 - 1) / 4096
378  *
379  * Note that in version 3 of the boot image header, page size is fixed at 4096 bytes.
380  *
381  * The structure of the vendor boot image (introduced with version 3 and
382  * required to be present when a v3 boot image is used) is as follows:
383  *
384  * +---------------------+
385  * | vendor boot header  | o pages
386  * +---------------------+
387  * | vendor ramdisk      | p pages
388  * +---------------------+
389  * | dtb                 | q pages
390  * +---------------------+
391  *
392  * o = (2112 + page_size - 1) / page_size
393  * p = (vendor_ramdisk_size + page_size - 1) / page_size
394  * q = (dtb_size + page_size - 1) / page_size
395  *
396  * 0. all entities in the boot image are 4096-byte aligned in flash, all
397  *    entities in the vendor boot image are page_size (determined by the vendor
398  *    and specified in the vendor boot image header) aligned in flash
399  * 1. kernel, ramdisk, vendor ramdisk, and DTB are required (size != 0)
400  * 2. load the kernel and DTB at the specified physical address (kernel_addr,
401  *    dtb_addr)
402  * 3. load the vendor ramdisk at ramdisk_addr
403  * 4. load the generic ramdisk immediately following the vendor ramdisk in
404  *    memory
405  * 5. set up registers for kernel entry as required by your architecture
406  * 6. if the platform has a second stage bootloader jump to it (must be
407  *    contained outside boot and vendor boot partitions), otherwise
408  *    jump to kernel_addr
409  */
410 
411 #endif
412