android_image.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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: cce5b1923e3cd2fcb765b512610bdc5c42bc501d
  7. *
  8. * Copyright (C) 2007 The Android Open Source Project
  9. */
  10. #ifndef _ANDROID_IMAGE_H_
  11. #define _ANDROID_IMAGE_H_
  12. #include <linux/compiler.h>
  13. #include <linux/types.h>
  14. #define ANDR_GKI_PAGE_SIZE 4096
  15. #define ANDR_BOOT_MAGIC "ANDROID!"
  16. #define ANDR_BOOT_MAGIC_SIZE 8
  17. #define ANDR_BOOT_NAME_SIZE 16
  18. #define ANDR_BOOT_ARGS_SIZE 512
  19. #define ANDR_BOOT_EXTRA_ARGS_SIZE 1024
  20. #define VENDOR_BOOT_MAGIC "VNDRBOOT"
  21. #define ANDR_VENDOR_BOOT_MAGIC_SIZE 8
  22. #define ANDR_VENDOR_BOOT_ARGS_SIZE 2048
  23. #define ANDR_VENDOR_BOOT_NAME_SIZE 16
  24. #define BOOTCONFIG_MAGIC "#BOOTCONFIG\n"
  25. #define BOOTCONFIG_MAGIC_SIZE 12
  26. #define BOOTCONFIG_SIZE_SIZE 4
  27. #define BOOTCONFIG_CHECKSUM_SIZE 4
  28. #define BOOTCONFIG_TRAILER_SIZE BOOTCONFIG_MAGIC_SIZE + \
  29. BOOTCONFIG_SIZE_SIZE + \
  30. BOOTCONFIG_CHECKSUM_SIZE
  31. struct andr_boot_img_hdr_v3 {
  32. u8 magic[ANDR_BOOT_MAGIC_SIZE];
  33. u32 kernel_size; /* size in bytes */
  34. u32 ramdisk_size; /* size in bytes */
  35. u32 os_version;
  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. u8 cmdline[ANDR_BOOT_ARGS_SIZE + ANDR_BOOT_EXTRA_ARGS_SIZE];
  40. /* for boot image header v4 only */
  41. u32 signature_size; /* size in bytes */
  42. };
  43. struct andr_vnd_boot_img_hdr {
  44. u8 magic[ANDR_VENDOR_BOOT_MAGIC_SIZE];
  45. u32 header_version;
  46. u32 page_size; /* flash page size we assume */
  47. u32 kernel_addr; /* physical load addr */
  48. u32 ramdisk_addr; /* physical load addr */
  49. u32 vendor_ramdisk_size; /* size in bytes */
  50. u8 cmdline[ANDR_VENDOR_BOOT_ARGS_SIZE];
  51. u32 tags_addr; /* physical addr for kernel tags */
  52. u8 name[ANDR_VENDOR_BOOT_NAME_SIZE]; /* asciiz product name */
  53. u32 header_size; /* size of vendor boot image header in bytes */
  54. u32 dtb_size; /* size of dtb image */
  55. u64 dtb_addr; /* physical load address */
  56. /* for boot image header v4 only */
  57. u32 vendor_ramdisk_table_size; /* size in bytes for the vendor ramdisk table */
  58. u32 vendor_ramdisk_table_entry_num; /* number of entries in the vendor ramdisk table */
  59. u32 vendor_ramdisk_table_entry_size; /* size in bytes for a vendor ramdisk table entry */
  60. u32 bootconfig_size; /* size in bytes for the bootconfig section */
  61. };
  62. /* The bootloader expects the structure of andr_boot_img_hdr_v0 with header
  63. * version 0 to be as follows: */
  64. struct andr_boot_img_hdr_v0 {
  65. /* Must be ANDR_BOOT_MAGIC. */
  66. char magic[ANDR_BOOT_MAGIC_SIZE];
  67. u32 kernel_size; /* size in bytes */
  68. u32 kernel_addr; /* physical load addr */
  69. u32 ramdisk_size; /* size in bytes */
  70. u32 ramdisk_addr; /* physical load addr */
  71. u32 second_size; /* size in bytes */
  72. u32 second_addr; /* physical load addr */
  73. u32 tags_addr; /* physical addr for kernel tags */
  74. u32 page_size; /* flash page size we assume */
  75. /* Version of the boot image header. */
  76. u32 header_version;
  77. /* Operating system version and security patch level.
  78. * For version "A.B.C" and patch level "Y-M-D":
  79. * (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M)
  80. * os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0] */
  81. u32 os_version;
  82. char name[ANDR_BOOT_NAME_SIZE]; /* asciiz product name */
  83. char cmdline[ANDR_BOOT_ARGS_SIZE];
  84. u32 id[8]; /* timestamp / checksum / sha1 / etc */
  85. /* Supplemental command line data; kept here to maintain
  86. * binary compatibility with older versions of mkbootimg. */
  87. char extra_cmdline[ANDR_BOOT_EXTRA_ARGS_SIZE];
  88. /* Fields in boot_img_hdr_v1 and newer. */
  89. u32 recovery_dtbo_size; /* size in bytes for recovery DTBO/ACPIO image */
  90. u64 recovery_dtbo_offset; /* offset to recovery dtbo/acpio in boot image */
  91. u32 header_size;
  92. /* Fields in boot_img_hdr_v2 and newer. */
  93. u32 dtb_size; /* size in bytes for DTB image */
  94. u64 dtb_addr; /* physical load address for DTB image */
  95. } __attribute__((packed));
  96. /* When a boot header is of version 0, the structure of boot image is as
  97. * follows:
  98. *
  99. * +-----------------+
  100. * | boot header | 1 page
  101. * +-----------------+
  102. * | kernel | n pages
  103. * +-----------------+
  104. * | ramdisk | m pages
  105. * +-----------------+
  106. * | second stage | o pages
  107. * +-----------------+
  108. *
  109. * n = (kernel_size + page_size - 1) / page_size
  110. * m = (ramdisk_size + page_size - 1) / page_size
  111. * o = (second_size + page_size - 1) / page_size
  112. *
  113. * 0. all entities are page_size aligned in flash
  114. * 1. kernel and ramdisk are required (size != 0)
  115. * 2. second is optional (second_size == 0 -> no second)
  116. * 3. load each element (kernel, ramdisk, second) at
  117. * the specified physical address (kernel_addr, etc)
  118. * 4. prepare tags at tag_addr. kernel_args[] is
  119. * appended to the kernel commandline in the tags.
  120. * 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
  121. * 6. if second_size != 0: jump to second_addr
  122. * else: jump to kernel_addr
  123. */
  124. /* When the boot image header has a version of 2, the structure of the boot
  125. * image is as follows:
  126. *
  127. * +---------------------+
  128. * | boot header | 1 page
  129. * +---------------------+
  130. * | kernel | n pages
  131. * +---------------------+
  132. * | ramdisk | m pages
  133. * +---------------------+
  134. * | second stage | o pages
  135. * +---------------------+
  136. * | recovery dtbo/acpio | p pages
  137. * +---------------------+
  138. * | dtb | q pages
  139. * +---------------------+
  140. *
  141. * n = (kernel_size + page_size - 1) / page_size
  142. * m = (ramdisk_size + page_size - 1) / page_size
  143. * o = (second_size + page_size - 1) / page_size
  144. * p = (recovery_dtbo_size + page_size - 1) / page_size
  145. * q = (dtb_size + page_size - 1) / page_size
  146. *
  147. * 0. all entities are page_size aligned in flash
  148. * 1. kernel, ramdisk and DTB are required (size != 0)
  149. * 2. recovery_dtbo/recovery_acpio is required for recovery.img in non-A/B
  150. * devices(recovery_dtbo_size != 0)
  151. * 3. second is optional (second_size == 0 -> no second)
  152. * 4. load each element (kernel, ramdisk, second, dtb) at
  153. * the specified physical address (kernel_addr, etc)
  154. * 5. If booting to recovery mode in a non-A/B device, extract recovery
  155. * dtbo/acpio and apply the correct set of overlays on the base device tree
  156. * depending on the hardware/product revision.
  157. * 6. prepare tags at tag_addr. kernel_args[] is
  158. * appended to the kernel commandline in the tags.
  159. * 7. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
  160. * 8. if second_size != 0: jump to second_addr
  161. * else: jump to kernel_addr
  162. */
  163. /* When the boot image header has a version of 3, the structure of the boot
  164. * image is as follows:
  165. *
  166. * +---------------------+
  167. * | boot header | 4096 bytes
  168. * +---------------------+
  169. * | kernel | m pages
  170. * +---------------------+
  171. * | ramdisk | n pages
  172. * +---------------------+
  173. *
  174. * m = (kernel_size + 4096 - 1) / 4096
  175. * n = (ramdisk_size + 4096 - 1) / 4096
  176. *
  177. * Note that in version 3 of the boot image header, page size is fixed at 4096 bytes.
  178. *
  179. * The structure of the vendor boot image (introduced with version 3 and
  180. * required to be present when a v3 boot image is used) is as follows:
  181. *
  182. * +---------------------+
  183. * | vendor boot header | o pages
  184. * +---------------------+
  185. * | vendor ramdisk | p pages
  186. * +---------------------+
  187. * | dtb | q pages
  188. * +---------------------+
  189. * o = (2112 + page_size - 1) / page_size
  190. * p = (vendor_ramdisk_size + page_size - 1) / page_size
  191. * q = (dtb_size + page_size - 1) / page_size
  192. *
  193. * 0. all entities in the boot image are 4096-byte aligned in flash, all
  194. * entities in the vendor boot image are page_size (determined by the vendor
  195. * and specified in the vendor boot image header) aligned in flash
  196. * 1. kernel, ramdisk, vendor ramdisk, and DTB are required (size != 0)
  197. * 2. load the kernel and DTB at the specified physical address (kernel_addr,
  198. * dtb_addr)
  199. * 3. load the vendor ramdisk at ramdisk_addr
  200. * 4. load the generic ramdisk immediately following the vendor ramdisk in
  201. * memory
  202. * 5. set up registers for kernel entry as required by your architecture
  203. * 6. if the platform has a second stage bootloader jump to it (must be
  204. * contained outside boot and vendor boot partitions), otherwise
  205. * jump to kernel_addr
  206. */
  207. /* When the boot image header has a version of 4, the structure of the boot
  208. * image is as follows:
  209. *
  210. * +---------------------+
  211. * | boot header | 4096 bytes
  212. * +---------------------+
  213. * | kernel | m pages
  214. * +---------------------+
  215. * | ramdisk | n pages
  216. * +---------------------+
  217. * | boot signature | g pages
  218. * +---------------------+
  219. *
  220. * m = (kernel_size + 4096 - 1) / 4096
  221. * n = (ramdisk_size + 4096 - 1) / 4096
  222. * g = (signature_size + 4096 - 1) / 4096
  223. *
  224. * Note that in version 4 of the boot image header, page size is fixed at 4096
  225. * bytes.
  226. *
  227. * The structure of the vendor boot image version 4, which is required to be
  228. * present when a version 4 boot image is used, is as follows:
  229. *
  230. * +------------------------+
  231. * | vendor boot header | o pages
  232. * +------------------------+
  233. * | vendor ramdisk section | p pages
  234. * +------------------------+
  235. * | dtb | q pages
  236. * +------------------------+
  237. * | vendor ramdisk table | r pages
  238. * +------------------------+
  239. * | bootconfig | s pages
  240. * +------------------------+
  241. *
  242. * o = (2128 + 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. * r = (vendor_ramdisk_table_size + page_size - 1) / page_size
  246. * s = (vendor_bootconfig_size + page_size - 1) / page_size
  247. *
  248. * Note that in version 4 of the vendor boot image, multiple vendor ramdisks can
  249. * be included in the vendor boot image. The bootloader can select a subset of
  250. * ramdisks to load at runtime. To help the bootloader select the ramdisks, each
  251. * ramdisk is tagged with a type tag and a set of hardware identifiers
  252. * describing the board, soc or platform that this ramdisk is intended for.
  253. *
  254. * The vendor ramdisk section is consist of multiple ramdisk images concatenated
  255. * one after another, and vendor_ramdisk_size is the size of the section, which
  256. * is the total size of all the ramdisks included in the vendor boot image.
  257. *
  258. * The vendor ramdisk table holds the size, offset, type, name and hardware
  259. * identifiers of each ramdisk. The type field denotes the type of its content.
  260. * The vendor ramdisk names are unique. The hardware identifiers are specified
  261. * in the board_id field in each table entry. The board_id field is consist of a
  262. * vector of unsigned integer words, and the encoding scheme is defined by the
  263. * hardware vendor.
  264. *
  265. * For the different type of ramdisks, there are:
  266. * - VENDOR_RAMDISK_TYPE_NONE indicates the value is unspecified.
  267. * - VENDOR_RAMDISK_TYPE_PLATFORM ramdisks contain platform specific bits, so
  268. * the bootloader should always load these into memory.
  269. * - VENDOR_RAMDISK_TYPE_RECOVERY ramdisks contain recovery resources, so
  270. * the bootloader should load these when booting into recovery.
  271. * - VENDOR_RAMDISK_TYPE_DLKM ramdisks contain 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 generic 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 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. /* Private struct */
  297. struct andr_image_data {
  298. ulong kernel_ptr; /* kernel address */
  299. u32 kernel_size; /* size in bytes */
  300. u32 ramdisk_size; /* size in bytes */
  301. ulong vendor_ramdisk_ptr; /* vendor ramdisk address */
  302. u32 vendor_ramdisk_size; /* vendor ramdisk size*/
  303. u32 boot_ramdisk_size; /* size in bytes */
  304. ulong second_ptr; /* secondary bootloader address */
  305. u32 second_size; /* secondary bootloader size */
  306. ulong dtb_ptr; /* address of dtb image */
  307. u32 dtb_size; /* size of dtb image */
  308. ulong recovery_dtbo_ptr; /* size in bytes for recovery DTBO/ACPIO image */
  309. u32 recovery_dtbo_size; /* offset to recovery dtbo/acpio in boot image */
  310. const char *kcmdline; /* boot kernel cmdline */
  311. const char *kcmdline_extra; /* vendor-boot extra kernel cmdline */
  312. const char *image_name; /* asciiz product name */
  313. ulong bootconfig_addr; /* bootconfig image address */
  314. ulong bootconfig_size; /* bootconfig image size */
  315. u32 kernel_addr; /* physical load addr */
  316. ulong ramdisk_addr; /* physical load addr */
  317. ulong ramdisk_ptr; /* ramdisk address */
  318. ulong dtb_load_addr; /* physical load address for DTB image */
  319. ulong tags_addr; /* physical addr for kernel tags */
  320. u32 header_version; /* version of the boot image header */
  321. u32 boot_img_total_size; /* boot image size */
  322. u32 vendor_boot_img_total_size; /* vendor boot image size */
  323. };
  324. #endif