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 
17 #pragma once
18 
19 #include <stdint.h>
20 
21 #define BOOT_MAGIC "ANDROID!"
22 #define BOOT_MAGIC_SIZE 8
23 #define BOOT_NAME_SIZE 16
24 #define BOOT_ARGS_SIZE 512
25 #define BOOT_EXTRA_ARGS_SIZE 1024
26 
27 #define VENDOR_BOOT_MAGIC "VNDRBOOT"
28 #define VENDOR_BOOT_MAGIC_SIZE 8
29 #define VENDOR_BOOT_ARGS_SIZE 2048
30 #define VENDOR_BOOT_NAME_SIZE 16
31 
32 /* When a boot header is of version 0, the structure of boot image is as
33  * follows:
34  *
35  * +-----------------+
36  * | boot header     | 1 page
37  * +-----------------+
38  * | kernel          | n pages
39  * +-----------------+
40  * | ramdisk         | m pages
41  * +-----------------+
42  * | second stage    | o pages
43  * +-----------------+
44  *
45  * n = (kernel_size + page_size - 1) / page_size
46  * m = (ramdisk_size + page_size - 1) / page_size
47  * o = (second_size + page_size - 1) / page_size
48  *
49  * 0. all entities are page_size aligned in flash
50  * 1. kernel and ramdisk are required (size != 0)
51  * 2. second is optional (second_size == 0 -> no second)
52  * 3. load each element (kernel, ramdisk, second) at
53  *    the specified physical address (kernel_addr, etc)
54  * 4. prepare tags at tag_addr.  kernel_args[] is
55  *    appended to the kernel commandline in the tags.
56  * 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
57  * 6. if second_size != 0: jump to second_addr
58  *    else: jump to kernel_addr
59  */
60 struct boot_img_hdr_v0 {
61     // Must be BOOT_MAGIC.
62     uint8_t magic[BOOT_MAGIC_SIZE];
63 
64     uint32_t kernel_size; /* size in bytes */
65     uint32_t kernel_addr; /* physical load addr */
66 
67     uint32_t ramdisk_size; /* size in bytes */
68     uint32_t ramdisk_addr; /* physical load addr */
69 
70     uint32_t second_size; /* size in bytes */
71     uint32_t second_addr; /* physical load addr */
72 
73     uint32_t tags_addr; /* physical addr for kernel tags (if required) */
74     uint32_t page_size; /* flash page size we assume */
75 
76     // Version of the boot image header.
77     uint32_t header_version;
78 
79     // Operating system version and security patch level.
80     // For version "A.B.C" and patch level "Y-M-D":
81     //   (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M)
82     //   os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0]
83     uint32_t os_version;
84 
85 #if __cplusplus
SetOsVersionboot_img_hdr_v086     void SetOsVersion(unsigned major, unsigned minor, unsigned patch) {
87         os_version &= ((1 << 11) - 1);
88         os_version |= (((major & 0x7f) << 25) | ((minor & 0x7f) << 18) | ((patch & 0x7f) << 11));
89     }
90 
SetOsPatchLevelboot_img_hdr_v091     void SetOsPatchLevel(unsigned year, unsigned month) {
92         os_version &= ~((1 << 11) - 1);
93         os_version |= (((year - 2000) & 0x7f) << 4) | ((month & 0xf) << 0);
94     }
95 #endif
96 
97     uint8_t name[BOOT_NAME_SIZE]; /* asciiz product name */
98 
99     uint8_t cmdline[BOOT_ARGS_SIZE];
100 
101     uint32_t 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     uint8_t extra_cmdline[BOOT_EXTRA_ARGS_SIZE];
106 } __attribute__((packed));
107 
108 /*
109  * It is expected that callers would explicitly specify which version of the
110  * boot image header they need to use.
111  */
112 typedef struct boot_img_hdr_v0 boot_img_hdr;
113 
114 /* When a boot header is of version 1, the structure of boot image is as
115  * follows:
116  *
117  * +---------------------+
118  * | boot header         | 1 page
119  * +---------------------+
120  * | kernel              | n pages
121  * +---------------------+
122  * | ramdisk             | m pages
123  * +---------------------+
124  * | second stage        | o pages
125  * +---------------------+
126  * | recovery dtbo/acpio | p pages
127  * +---------------------+
128  *
129  * n = (kernel_size + page_size - 1) / page_size
130  * m = (ramdisk_size + page_size - 1) / page_size
131  * o = (second_size + page_size - 1) / page_size
132  * p = (recovery_dtbo_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. recovery_dtbo/recovery_acpio is required for recovery.img in non-A/B
137  *    devices(recovery_dtbo_size != 0)
138  * 3. second is optional (second_size == 0 -> no second)
139  * 4. load each element (kernel, ramdisk, second) at
140  *    the specified physical address (kernel_addr, etc)
141  * 5. If booting to recovery mode in a non-A/B device, extract recovery
142  *    dtbo/acpio and apply the correct set of overlays on the base device tree
143  *    depending on the hardware/product revision.
144  * 6. set up registers for kernel entry as required by your architecture
145  * 7. if second_size != 0: jump to second_addr
146  *    else: jump to kernel_addr
147  */
148 struct boot_img_hdr_v1 : public boot_img_hdr_v0 {
149     uint32_t recovery_dtbo_size;   /* size in bytes for recovery DTBO/ACPIO image */
150     uint64_t recovery_dtbo_offset; /* offset to recovery dtbo/acpio in boot image */
151     uint32_t header_size;
152 } __attribute__((packed));
153 
154 /* When the boot image header has a version of 2, the structure of the boot
155  * image is as follows:
156  *
157  * +---------------------+
158  * | boot header         | 1 page
159  * +---------------------+
160  * | kernel              | n pages
161  * +---------------------+
162  * | ramdisk             | m pages
163  * +---------------------+
164  * | second stage        | o pages
165  * +---------------------+
166  * | recovery dtbo/acpio | p pages
167  * +---------------------+
168  * | dtb                 | q pages
169  * +---------------------+
170 
171  * n = (kernel_size + page_size - 1) / page_size
172  * m = (ramdisk_size + page_size - 1) / page_size
173  * o = (second_size + page_size - 1) / page_size
174  * p = (recovery_dtbo_size + page_size - 1) / page_size
175  * q = (dtb_size + page_size - 1) / page_size
176  *
177  * 0. all entities are page_size aligned in flash
178  * 1. kernel, ramdisk and DTB are required (size != 0)
179  * 2. recovery_dtbo/recovery_acpio is required for recovery.img in non-A/B
180  *    devices(recovery_dtbo_size != 0)
181  * 3. second is optional (second_size == 0 -> no second)
182  * 4. load each element (kernel, ramdisk, second, dtb) at
183  *    the specified physical address (kernel_addr, etc)
184  * 5. If booting to recovery mode in a non-A/B device, extract recovery
185  *    dtbo/acpio and apply the correct set of overlays on the base device tree
186  *    depending on the hardware/product revision.
187  * 6. set up registers for kernel entry as required by your architecture
188  * 7. if second_size != 0: jump to second_addr
189  *    else: jump to kernel_addr
190  */
191 struct boot_img_hdr_v2 : public boot_img_hdr_v1 {
192     uint32_t dtb_size; /* size in bytes for DTB image */
193     uint64_t dtb_addr; /* physical load address for DTB image */
194 } __attribute__((packed));
195 
196 
197 /* When the boot image header has a version of 3, the structure of the boot
198  * image is as follows:
199  *
200  * +---------------------+
201  * | boot header         | 4096 bytes
202  * +---------------------+
203  * | kernel              | m pages
204  * +---------------------+
205  * | ramdisk             | n pages
206  * +---------------------+
207  *
208  * m = (kernel_size + 4096 - 1) / 4096
209  * n = (ramdisk_size + 4096 - 1) / 4096
210  *
211  * Note that in version 3 of the boot image header, page size is fixed at 4096 bytes.
212  *
213  * The structure of the vendor boot image (introduced with version 3 and
214  * required to be present when a v3 boot image is used) is as follows:
215  *
216  * +---------------------+
217  * | vendor boot header  | o pages
218  * +---------------------+
219  * | vendor ramdisk      | p pages
220  * +---------------------+
221  * | dtb                 | q pages
222  * +---------------------+
223 
224  * o = (2112 + page_size - 1) / page_size
225  * p = (vendor_ramdisk_size + page_size - 1) / page_size
226  * q = (dtb_size + page_size - 1) / page_size
227  *
228  * 0. all entities in the boot image are 4096-byte aligned in flash, all
229  *    entities in the vendor boot image are page_size (determined by the vendor
230  *    and specified in the vendor boot image header) aligned in flash
231  * 1. kernel, ramdisk, vendor ramdisk, and DTB are required (size != 0)
232  * 2. load the kernel and DTB at the specified physical address (kernel_addr,
233  *    dtb_addr)
234  * 3. load the vendor ramdisk at ramdisk_addr
235  * 4. load the generic ramdisk immediately following the vendor ramdisk in
236  *    memory
237  * 5. set up registers for kernel entry as required by your architecture
238  * 6. if the platform has a second stage bootloader jump to it (must be
239  *    contained outside boot and vendor boot partitions), otherwise
240  *    jump to kernel_addr
241  */
242 struct boot_img_hdr_v3 {
243     // Must be BOOT_MAGIC.
244     uint8_t magic[BOOT_MAGIC_SIZE];
245 
246     uint32_t kernel_size; /* size in bytes */
247     uint32_t ramdisk_size; /* size in bytes */
248 
249     // Operating system version and security patch level.
250     // For version "A.B.C" and patch level "Y-M-D":
251     //   (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M)
252     //   os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0]
253     uint32_t os_version;
254 
255 #if __cplusplus
SetOsVersionboot_img_hdr_v3256     void SetOsVersion(unsigned major, unsigned minor, unsigned patch) {
257         os_version &= ((1 << 11) - 1);
258         os_version |= (((major & 0x7f) << 25) | ((minor & 0x7f) << 18) | ((patch & 0x7f) << 11));
259     }
260 
SetOsPatchLevelboot_img_hdr_v3261     void SetOsPatchLevel(unsigned year, unsigned month) {
262         os_version &= ~((1 << 11) - 1);
263         os_version |= (((year - 2000) & 0x7f) << 4) | ((month & 0xf) << 0);
264     }
265 #endif
266 
267     uint32_t header_size;
268 
269     uint32_t reserved[4];
270 
271     // Version of the boot image header.
272     uint32_t header_version;
273 
274     uint8_t cmdline[BOOT_ARGS_SIZE + BOOT_EXTRA_ARGS_SIZE];
275 } __attribute__((packed));
276 
277 struct vendor_boot_img_hdr_v3 {
278     // Must be VENDOR_BOOT_MAGIC.
279     uint8_t magic[VENDOR_BOOT_MAGIC_SIZE];
280 
281     // Version of the vendor boot image header.
282     uint32_t header_version;
283 
284     uint32_t page_size; /* flash page size we assume */
285 
286     uint32_t kernel_addr; /* physical load addr */
287     uint32_t ramdisk_addr; /* physical load addr */
288 
289     uint32_t vendor_ramdisk_size; /* size in bytes */
290 
291     uint8_t cmdline[VENDOR_BOOT_ARGS_SIZE];
292 
293     uint32_t tags_addr; /* physical addr for kernel tags (if required) */
294     uint8_t name[VENDOR_BOOT_NAME_SIZE]; /* asciiz product name */
295 
296     uint32_t header_size;
297 
298     uint32_t dtb_size; /* size in bytes for DTB image */
299     uint64_t dtb_addr; /* physical load address for DTB image */
300 } __attribute__((packed));
301