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