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