bootimg.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* Copyright Statement:
  17. *
  18. * This software/firmware and related documentation ("MediaTek Software") are
  19. * protected under relevant copyright laws. The information contained herein
  20. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  21. * Without the prior written permission of MediaTek inc. and/or its licensors,
  22. * any reproduction, modification, use or disclosure of MediaTek Software,
  23. * and information contained herein, in whole or in part, shall be strictly prohibited.
  24. */
  25. /* MediaTek Inc. (C) 2017. All rights reserved.
  26. *
  27. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  28. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  29. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  30. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  31. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  32. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  33. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  34. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  35. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  36. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  37. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  38. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  39. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  40. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  41. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  42. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  43. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  44. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  45. *
  46. * The following software/firmware and/or related documentation ("MediaTek Software")
  47. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  48. * applicable license agreements with MediaTek Inc.
  49. */
  50. #ifndef __BOOTIMG_H__
  51. #define __BOOTIMG_H__
  52. #include <pal_typedefs.h>
  53. #define BOOTIMG_MAGIC "ANDROID!"
  54. #define BOOTIMG_MAGIC_SZ (8)
  55. #define BOOTIMG_NAME_SZ (16)
  56. #define BOOTIMG_ARGS_SZ (512)
  57. #define BOOT_EXTRA_ARGS_SIZE (1024)
  58. #define VENDOR_BOOT_MAGIC "VNDRBOOT"
  59. #define VENDOR_BOOT_MAGIC_SIZE 8
  60. #define VENDOR_BOOT_ARGS_SIZE 2048
  61. #define VENDOR_BOOT_NAME_SIZE 16
  62. #define BOOT_HEADER_VERSION_ZERO 0
  63. #define BOOT_HEADER_VERSION_ONE 1
  64. #define BOOT_HEADER_VERSION_TWO 2
  65. #define BOOT_HEADER_VERSION_THREE 3
  66. #define BOOT_IMAGE_HEADER_V1_SIZE 1648
  67. #define BOOT_IMAGE_HEADER_V2_SIZE 1660
  68. #define BOOT_IMAGE_HEADER_V3_SIZE 1596
  69. #define VENDOR_BOOT_IMAGE_HEADER_V3_SIZE 2108
  70. #define BOOT_IMAGE_HEADER_V3_PAGESIZE 4096
  71. #define BOOTIMG_HDR_SZ (0x800)
  72. /* When the boot image header has a version of 2, the structure of the boot
  73. * image is as follows:
  74. *
  75. * +---------------------+
  76. * | boot header | 1 page
  77. * +---------------------+
  78. * | kernel | n pages
  79. * +---------------------+
  80. * | ramdisk | m pages
  81. * +---------------------+
  82. * | second stage | o pages
  83. * +---------------------+
  84. * | recovery dtbo/acpio | p pages
  85. * +---------------------+
  86. * | dtb | q pages
  87. * +---------------------+
  88. * n = (kernel_size + page_size - 1) / page_size
  89. * m = (ramdisk_size + page_size - 1) / page_size
  90. * o = (second_size + page_size - 1) / page_size
  91. * p = (recovery_dtbo_size + page_size - 1) / page_size
  92. * q = (dtb_size + page_size - 1) / page_size
  93. *
  94. * 0. all entities are page_size aligned in flash
  95. * 1. kernel, ramdisk and DTB are required (size != 0)
  96. * 2. recovery_dtbo/recovery_acpio is required for recovery.img in non-A/B
  97. * devices(recovery_dtbo_size != 0)
  98. * 3. second is optional (second_size == 0 -> no second)
  99. * 4. load each element (kernel, ramdisk, second, dtb) at
  100. * the specified physical address (kernel_addr, etc)
  101. * 5. If booting to recovery mode in a non-A/B device, extract recovery
  102. * dtbo/acpio and apply the correct set of overlays on the base device tree
  103. * depending on the hardware/product revision.
  104. * 6. prepare tags at tag_addr. kernel_args[] is
  105. * appended to the kernel commandline in the tags.
  106. * 7. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
  107. * 8. if second_size != 0: jump to second_addr
  108. * else: jump to kernel_addr
  109. */
  110. struct bootimg_hdr {
  111. uint8_t magic[BOOTIMG_MAGIC_SZ];
  112. uint32_t kernel_sz; /* size in bytes */
  113. uint32_t kernel_addr; /* physical load addr */
  114. uint32_t ramdisk_sz; /* size in bytes */
  115. uint32_t ramdisk_addr; /* physical load addr */
  116. uint32_t second_sz; /* size in bytes */
  117. uint32_t second_addr; /* physical load addr */
  118. uint32_t tags_addr; /* physical addr for kernel tags */
  119. uint32_t page_sz; /* flash page size we assume */
  120. uint32_t header_version;
  121. uint32_t os_version;
  122. uint8_t name[BOOTIMG_NAME_SZ]; /* asciiz product name */
  123. uint8_t cmdline[BOOTIMG_ARGS_SZ];
  124. uint32_t id[8]; /* timestamp / checksum / sha1 / etc */
  125. uint8_t extra_cmdline[BOOT_EXTRA_ARGS_SIZE];
  126. uint32_t recovery_dtbo_size; /* size of recovery dtbo image */
  127. uint64_t recovery_dtbo_offset; /* physical load addr */
  128. uint32_t header_size; /* size of boot image header in bytes */
  129. uint32_t dtb_size; /* size in bytes for DTB image */
  130. uint64_t dtb_addr; /* physical load address for DTB image */
  131. } __attribute__((packed));
  132. /* When the boot image header has a version of 3, the structure of the boot
  133. * image is as follows:
  134. *
  135. * +---------------------+
  136. * | boot header | 4096 bytes
  137. * +---------------------+
  138. * | kernel | m
  139. * +---------------------+
  140. * | ramdisk | n
  141. * +---------------------+
  142. *
  143. * m = (kernel_size + 4096 - 1) / 4096
  144. * n = (ramdisk_size + 4096 - 1) / 4096
  145. *
  146. * Note that in version 3 of the boot image header, page size is fixed at 4096 bytes.
  147. *
  148. * The structure of the vendor boot image (introduced with version 3 and
  149. * required to be present when a v3 boot image is used) is as follows:
  150. *
  151. * +---------------------+
  152. * | vendor boot header | 1 page
  153. * +---------------------+
  154. * | vendor ramdisk | o pages
  155. * +---------------------+
  156. * | dtb | p pages
  157. * +---------------------+
  158. * o = (vendor_ramdisk_size + page_size - 1) / page_size
  159. * p = (dtb_size + page_size - 1) / page_size
  160. *
  161. * 0. all entities in the boot image are 4096-byte aligned in flash, all
  162. * entities in the vendor boot image are page_size (determined by the vendor
  163. * and specified in the vendor boot image header) aligned in flash
  164. * 1. kernel, ramdisk, vendor ramdisk, and DTB are required (size != 0)
  165. * 2. load the kernel and DTB at the specified physical address (kernel_addr,
  166. * dtb_addr)
  167. * 3. load the vendor ramdisk at ramdisk_addr
  168. * 4. load the generic ramdisk immediately following the vendor ramdisk in
  169. * memory
  170. * 5. prepare tags at tag_addr. kernel_args[] is appended to the kernel
  171. * commandline in the tags.
  172. * 6. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
  173. * 7. if the platform has a second stage bootloader jump to it (must be
  174. * contained outside boot and vendor boot partitions), otherwise
  175. * jump to kernel_addr
  176. */
  177. struct boot_img_hdr_v3 {
  178. // Must be BOOT_MAGIC.
  179. uint8_t magic[BOOTIMG_MAGIC_SZ];
  180. uint32_t kernel_size; /* size in bytes */
  181. uint32_t ramdisk_size; /* size in bytes */
  182. // Operating system version and security patch level.
  183. // For version "A.B.C" and patch level "Y-M-D":
  184. // (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M)
  185. // os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0]
  186. uint32_t os_version;
  187. uint32_t header_size;
  188. uint32_t reserved[4];
  189. // Version of the boot image header.
  190. uint32_t header_version;
  191. uint8_t cmdline[BOOTIMG_ARGS_SZ + BOOT_EXTRA_ARGS_SIZE];
  192. } __attribute__((packed));
  193. struct vendor_boot_img_hdr_v3 {
  194. // Must be VENDOR_BOOT_MAGIC.
  195. uint8_t magic[VENDOR_BOOT_MAGIC_SIZE];
  196. // Version of the vendor boot image header.
  197. uint32_t header_version;
  198. uint32_t page_size; /* flash page size we assume */
  199. uint32_t kernel_addr; /* physical load addr */
  200. uint32_t ramdisk_addr; /* physical load addr */
  201. uint32_t vendor_ramdisk_size; /* size in bytes */
  202. uint8_t cmdline[VENDOR_BOOT_ARGS_SIZE];
  203. uint32_t tags_addr; /* physical addr for kernel tags */
  204. uint8_t name[VENDOR_BOOT_NAME_SIZE]; /* asciiz product name */
  205. uint32_t header_size;
  206. uint32_t dtb_size; /* size in bytes for DTB image */
  207. uint64_t dtb_addr; /* physical load address for DTB image */
  208. } __attribute__((packed));
  209. #endif /* __BOOTIMG_H__ */