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 boot_ramdisk_size; /* size in bytes */ 89 u32 vendor_ramdisk_size; /* size in bytes */ 90 u32 vendor_page_size; 91 u32 vendor_header_version; 92 u32 vendor_header_size; 93 /* 94 * Don't define 'char total_cmdline[TOTAL_BOOT_ARGS_SIZE]' to avoid 95 * this structrue is over size than page_size. 96 */ 97 char *total_cmdline; 98 } __attribute__((packed)); 99 100 struct boot_img_hdr_v3 { 101 /* Must be ANDR_BOOT_MAGIC. */ 102 uint8_t magic[ANDR_BOOT_MAGIC_SIZE]; 103 104 uint32_t kernel_size; /* size in bytes */ 105 uint32_t ramdisk_size; /* size in bytes */ 106 107 /* Operating system version and security patch level. 108 * For version "A.B.C" and patch level "Y-M-D": 109 * (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M) 110 * os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0] */ 111 uint32_t os_version; 112 113 uint32_t header_size; 114 115 uint32_t reserved[4]; 116 117 uint32_t header_version; 118 119 uint8_t cmdline[ANDR_BOOT_ARGS_SIZE + ANDR_BOOT_EXTRA_ARGS_SIZE]; 120 } __attribute__((packed)); 121 122 struct vendor_boot_img_hdr_v3 { 123 /* Must be VENDOR_BOOT_MAGIC. */ 124 uint8_t magic[VENDOR_BOOT_MAGIC_SIZE]; 125 126 /* Version of the vendor boot image header. */ 127 uint32_t header_version; 128 129 uint32_t page_size; /* flash page size we assume */ 130 131 uint32_t kernel_addr; /* physical load addr */ 132 uint32_t ramdisk_addr; /* physical load addr */ 133 134 uint32_t vendor_ramdisk_size; /* size in bytes */ 135 136 uint8_t cmdline[VENDOR_BOOT_ARGS_SIZE]; 137 138 uint32_t tags_addr; /* physical addr for kernel tags (if required) */ 139 uint8_t name[VENDOR_BOOT_NAME_SIZE]; /* asciiz product name */ 140 141 uint32_t header_size; 142 143 uint32_t dtb_size; /* size in bytes for DTB image */ 144 uint64_t dtb_addr; /* physical load address for DTB image */ 145 } __attribute__((packed)); 146 147 /* When a boot header is of version 0, the structure of boot image is as 148 * follows: 149 * 150 * +-----------------+ 151 * | boot header | 1 page 152 * +-----------------+ 153 * | kernel | n pages 154 * +-----------------+ 155 * | ramdisk | m pages 156 * +-----------------+ 157 * | second stage | o pages 158 * +-----------------+ 159 * 160 * n = (kernel_size + page_size - 1) / page_size 161 * m = (ramdisk_size + page_size - 1) / page_size 162 * o = (second_size + page_size - 1) / page_size 163 * 164 * 0. all entities are page_size aligned in flash 165 * 1. kernel and ramdisk are required (size != 0) 166 * 2. second is optional (second_size == 0 -> no second) 167 * 3. load each element (kernel, ramdisk, second) at 168 * the specified physical address (kernel_addr, etc) 169 * 4. prepare tags at tag_addr. kernel_args[] is 170 * appended to the kernel commandline in the tags. 171 * 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr 172 * 6. if second_size != 0: jump to second_addr 173 * else: jump to kernel_addr 174 */ 175 176 /* When the boot image header has a version of 2, the structure of the boot 177 * image is as follows: 178 * 179 * +---------------------+ 180 * | boot header | 1 page 181 * +---------------------+ 182 * | kernel | n pages 183 * +---------------------+ 184 * | ramdisk | m pages 185 * +---------------------+ 186 * | second stage | o pages 187 * +---------------------+ 188 * | recovery dtbo/acpio | p pages 189 * +---------------------+ 190 * | dtb | q pages 191 * +---------------------+ 192 193 * n = (kernel_size + page_size - 1) / page_size 194 * m = (ramdisk_size + page_size - 1) / page_size 195 * o = (second_size + page_size - 1) / page_size 196 * p = (recovery_dtbo_size + page_size - 1) / page_size 197 * q = (dtb_size + page_size - 1) / page_size 198 * 199 * 0. all entities are page_size aligned in flash 200 * 1. kernel, ramdisk and DTB are required (size != 0) 201 * 2. recovery_dtbo/recovery_acpio is required for recovery.img in non-A/B 202 * devices(recovery_dtbo_size != 0) 203 * 3. second is optional (second_size == 0 -> no second) 204 * 4. load each element (kernel, ramdisk, second, dtb) at 205 * the specified physical address (kernel_addr, etc) 206 * 5. If booting to recovery mode in a non-A/B device, extract recovery 207 * dtbo/acpio and apply the correct set of overlays on the base device tree 208 * depending on the hardware/product revision. 209 * 6. prepare tags at tag_addr. kernel_args[] is 210 * appended to the kernel commandline in the tags. 211 * 7. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr 212 * 8. if second_size != 0: jump to second_addr 213 * else: jump to kernel_addr 214 */ 215 216 /* When the boot image header has a version of 3, the structure of the boot 217 * image is as follows: 218 * 219 * +---------------------+ 220 * | boot header | 4096 bytes 221 * +---------------------+ 222 * | kernel | m pages 223 * +---------------------+ 224 * | ramdisk | n pages 225 * +---------------------+ 226 * 227 * m = (kernel_size + 4096 - 1) / 4096 228 * n = (ramdisk_size + 4096 - 1) / 4096 229 * 230 * Note that in version 3 of the boot image header, page size is fixed at 4096 bytes. 231 * 232 * The structure of the vendor boot image (introduced with version 3 and 233 * required to be present when a v3 boot image is used) is as follows: 234 * 235 * +---------------------+ 236 * | vendor boot header | o pages 237 * +---------------------+ 238 * | vendor ramdisk | p pages 239 * +---------------------+ 240 * | dtb | q pages 241 * +---------------------+ 242 * 243 * o = (2112 + page_size - 1) / page_size 244 * p = (vendor_ramdisk_size + page_size - 1) / page_size 245 * q = (dtb_size + page_size - 1) / page_size 246 * 247 * 0. all entities in the boot image are 4096-byte aligned in flash, all 248 * entities in the vendor boot image are page_size (determined by the vendor 249 * and specified in the vendor boot image header) aligned in flash 250 * 1. kernel, ramdisk, vendor ramdisk, and DTB are required (size != 0) 251 * 2. load the kernel and DTB at the specified physical address (kernel_addr, 252 * dtb_addr) 253 * 3. load the vendor ramdisk at ramdisk_addr 254 * 4. load the generic ramdisk immediately following the vendor ramdisk in 255 * memory 256 * 5. set up registers for kernel entry as required by your architecture 257 * 6. if the platform has a second stage bootloader jump to it (must be 258 * contained outside boot and vendor boot partitions), otherwise 259 * jump to kernel_addr 260 */ 261 262 #endif 263