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 ANDR_BOOT_MAGIC "ANDROID!" 15 #define VENDOR_BOOT_MAGIC "VNDRBOOT" 16 #define ANDR_BOOT_MAGIC_SIZE 8 17 #define VENDOR_BOOT_MAGIC_SIZE 8 18 #define ANDR_BOOT_NAME_SIZE 16 19 #define VENDOR_BOOT_NAME_SIZE 16 20 #define ANDR_BOOT_ARGS_SIZE 512 21 #define ANDR_BOOT_EXTRA_ARGS_SIZE 1024 22 #define VENDOR_BOOT_ARGS_SIZE 2048 23 #define ANDR_BOOT_IMG_PAGE_SIZE 4096 24 #define ANDR_BOOT_IMG_HDR_SIZE (ANDR_BOOT_IMG_PAGE_SIZE) 25 #define TOTAL_BOOT_ARGS_SIZE (ANDR_BOOT_ARGS_SIZE + ANDR_BOOT_EXTRA_ARGS_SIZE + \ 26 VENDOR_BOOT_ARGS_SIZE + 1) 27 #define VENDOR_BOOT_HDR_SIZE 2112 28 29 /* 30 * It is expected that callers would explicitly specify which version of the 31 * boot image header they need to use. 32 */ 33 typedef struct andr_img_hdr andr_img_hdr; 34 35 /* The bootloader expects the structure of andr_img_hdr with header 36 * version 0 to be as follows: */ 37 struct andr_img_hdr { 38 /* Must be ANDR_BOOT_MAGIC. */ 39 char magic[ANDR_BOOT_MAGIC_SIZE]; 40 41 u32 kernel_size; /* size in bytes */ 42 u32 kernel_addr; /* physical load addr */ 43 44 u32 ramdisk_size; /* size in bytes */ 45 u32 ramdisk_addr; /* physical load addr */ 46 47 u32 second_size; /* size in bytes */ 48 u32 second_addr; /* physical load addr */ 49 50 u32 tags_addr; /* physical addr for kernel tags */ 51 u32 page_size; /* flash page size we assume */ 52 53 /* Version of the boot image header. */ 54 u32 header_version; 55 56 /* Operating system version and security patch level. 57 * For version "A.B.C" and patch level "Y-M-D": 58 * (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M) 59 * os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0] */ 60 u32 os_version; 61 62 char name[ANDR_BOOT_NAME_SIZE]; /* asciiz product name */ 63 64 char cmdline[ANDR_BOOT_ARGS_SIZE]; 65 66 u32 id[8]; /* timestamp / checksum / sha1 / etc */ 67 68 /* Supplemental command line data; kept here to maintain 69 * binary compatibility with older versions of mkbootimg. */ 70 char extra_cmdline[ANDR_BOOT_EXTRA_ARGS_SIZE]; 71 72 /* Fields in boot_img_hdr_v1(Android-9) and newer. */ 73 u32 recovery_dtbo_size; /* size in bytes for recovery DTBO/ACPIO image */ 74 u64 recovery_dtbo_offset; /* offset to recovery dtbo/acpio in boot image */ 75 u32 header_size; 76 77 /* Fields in boot_img_hdr_v2(Android-10) and newer. */ 78 u32 dtb_size; /* size in bytes for DTB image */ 79 u64 dtb_addr; /* physical load address for DTB image */ 80 81 /* 82 * [Rockchip compatibility] 83 * 84 * boot_img_hdr_v3(Android-11) is not compatible with boot_img_hdr_v012, 85 * we have to partly merge fields from boot_img_hdr_v3 and vendor_boot_img_hdr_v3 86 * into this structure to compatible with boot_img_hdr_v012. 87 */ 88 u32 vendor_ramdisk_size; /* size in bytes */ 89 u32 vendor_page_size; 90 u32 vendor_header_version; 91 u32 vendor_header_size; 92 /* 93 * Don't define 'char total_cmdline[TOTAL_BOOT_ARGS_SIZE]' to avoid 94 * this structrue is over size than page_size. 95 */ 96 char *total_cmdline; 97 } __attribute__((packed)); 98 99 struct boot_img_hdr_v3 { 100 /* Must be ANDR_BOOT_MAGIC. */ 101 uint8_t magic[ANDR_BOOT_MAGIC_SIZE]; 102 103 uint32_t kernel_size; /* size in bytes */ 104 uint32_t ramdisk_size; /* size in bytes */ 105 106 /* Operating system version and security patch level. 107 * For version "A.B.C" and patch level "Y-M-D": 108 * (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M) 109 * os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0] */ 110 uint32_t os_version; 111 112 uint32_t header_size; 113 114 uint32_t reserved[4]; 115 116 uint32_t header_version; 117 118 uint8_t cmdline[ANDR_BOOT_ARGS_SIZE + ANDR_BOOT_EXTRA_ARGS_SIZE]; 119 } __attribute__((packed)); 120 121 struct vendor_boot_img_hdr_v3 { 122 /* Must be VENDOR_BOOT_MAGIC. */ 123 uint8_t magic[VENDOR_BOOT_MAGIC_SIZE]; 124 125 /* Version of the vendor boot image header. */ 126 uint32_t header_version; 127 128 uint32_t page_size; /* flash page size we assume */ 129 130 uint32_t kernel_addr; /* physical load addr */ 131 uint32_t ramdisk_addr; /* physical load addr */ 132 133 uint32_t vendor_ramdisk_size; /* size in bytes */ 134 135 uint8_t cmdline[VENDOR_BOOT_ARGS_SIZE]; 136 137 uint32_t tags_addr; /* physical addr for kernel tags (if required) */ 138 uint8_t name[VENDOR_BOOT_NAME_SIZE]; /* asciiz product name */ 139 140 uint32_t header_size; 141 142 uint32_t dtb_size; /* size in bytes for DTB image */ 143 uint64_t dtb_addr; /* physical load address for DTB image */ 144 } __attribute__((packed)); 145 146 /* When a boot header is of version 0, the structure of boot image is as 147 * follows: 148 * 149 * +-----------------+ 150 * | boot header | 1 page 151 * +-----------------+ 152 * | kernel | n pages 153 * +-----------------+ 154 * | ramdisk | m pages 155 * +-----------------+ 156 * | second stage | o pages 157 * +-----------------+ 158 * 159 * n = (kernel_size + page_size - 1) / page_size 160 * m = (ramdisk_size + page_size - 1) / page_size 161 * o = (second_size + page_size - 1) / page_size 162 * 163 * 0. all entities are page_size aligned in flash 164 * 1. kernel and ramdisk are required (size != 0) 165 * 2. second is optional (second_size == 0 -> no second) 166 * 3. load each element (kernel, ramdisk, second) at 167 * the specified physical address (kernel_addr, etc) 168 * 4. prepare tags at tag_addr. kernel_args[] is 169 * appended to the kernel commandline in the tags. 170 * 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr 171 * 6. if second_size != 0: jump to second_addr 172 * else: jump to kernel_addr 173 */ 174 175 /* When the boot image header has a version of 2, the structure of the boot 176 * image is as follows: 177 * 178 * +---------------------+ 179 * | boot header | 1 page 180 * +---------------------+ 181 * | kernel | n pages 182 * +---------------------+ 183 * | ramdisk | m pages 184 * +---------------------+ 185 * | second stage | o pages 186 * +---------------------+ 187 * | recovery dtbo/acpio | p pages 188 * +---------------------+ 189 * | dtb | q pages 190 * +---------------------+ 191 192 * n = (kernel_size + page_size - 1) / page_size 193 * m = (ramdisk_size + page_size - 1) / page_size 194 * o = (second_size + page_size - 1) / page_size 195 * p = (recovery_dtbo_size + page_size - 1) / page_size 196 * q = (dtb_size + page_size - 1) / page_size 197 * 198 * 0. all entities are page_size aligned in flash 199 * 1. kernel, ramdisk and DTB are required (size != 0) 200 * 2. recovery_dtbo/recovery_acpio is required for recovery.img in non-A/B 201 * devices(recovery_dtbo_size != 0) 202 * 3. second is optional (second_size == 0 -> no second) 203 * 4. load each element (kernel, ramdisk, second, dtb) at 204 * the specified physical address (kernel_addr, etc) 205 * 5. If booting to recovery mode in a non-A/B device, extract recovery 206 * dtbo/acpio and apply the correct set of overlays on the base device tree 207 * depending on the hardware/product revision. 208 * 6. prepare tags at tag_addr. kernel_args[] is 209 * appended to the kernel commandline in the tags. 210 * 7. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr 211 * 8. if second_size != 0: jump to second_addr 212 * else: jump to kernel_addr 213 */ 214 215 /* When the boot image header has a version of 3, the structure of the boot 216 * image is as follows: 217 * 218 * +---------------------+ 219 * | boot header | 4096 bytes 220 * +---------------------+ 221 * | kernel | m pages 222 * +---------------------+ 223 * | ramdisk | n pages 224 * +---------------------+ 225 * 226 * m = (kernel_size + 4096 - 1) / 4096 227 * n = (ramdisk_size + 4096 - 1) / 4096 228 * 229 * Note that in version 3 of the boot image header, page size is fixed at 4096 bytes. 230 * 231 * The structure of the vendor boot image (introduced with version 3 and 232 * required to be present when a v3 boot image is used) is as follows: 233 * 234 * +---------------------+ 235 * | vendor boot header | o pages 236 * +---------------------+ 237 * | vendor ramdisk | p pages 238 * +---------------------+ 239 * | dtb | q pages 240 * +---------------------+ 241 * 242 * o = (2112 + page_size - 1) / page_size 243 * p = (vendor_ramdisk_size + page_size - 1) / page_size 244 * q = (dtb_size + page_size - 1) / page_size 245 * 246 * 0. all entities in the boot image are 4096-byte aligned in flash, all 247 * entities in the vendor boot image are page_size (determined by the vendor 248 * and specified in the vendor boot image header) aligned in flash 249 * 1. kernel, ramdisk, vendor ramdisk, and DTB are required (size != 0) 250 * 2. load the kernel and DTB at the specified physical address (kernel_addr, 251 * dtb_addr) 252 * 3. load the vendor ramdisk at ramdisk_addr 253 * 4. load the generic ramdisk immediately following the vendor ramdisk in 254 * memory 255 * 5. set up registers for kernel entry as required by your architecture 256 * 6. if the platform has a second stage bootloader jump to it (must be 257 * contained outside boot and vendor boot partitions), otherwise 258 * jump to kernel_addr 259 */ 260 261 #endif 262