xref: /rk3399_rockchip-uboot/include/android_bootloader_message.h (revision 67dac67453c08a760320a21ccd076abd47c9c9f1)
1*67dac674SAlex Deymo /*
2*67dac674SAlex Deymo  * This is from the Android Project,
3*67dac674SAlex Deymo  * Repository: https://android.googlesource.com/platform/bootable/recovery/
4*67dac674SAlex Deymo  * File: bootloader_message/include/bootloader_message/bootloader_message.h
5*67dac674SAlex Deymo  * Commit: 8b309f6970ab3b7c53cc529c51a2cb44e1c7a7e1
6*67dac674SAlex Deymo  *
7*67dac674SAlex Deymo  * Copyright (C) 2008 The Android Open Source Project
8*67dac674SAlex Deymo  *
9*67dac674SAlex Deymo  * SPDX-License-Identifier: BSD-2-Clause
10*67dac674SAlex Deymo  */
11*67dac674SAlex Deymo 
12*67dac674SAlex Deymo #ifndef __ANDROID_BOOTLOADER_MESSAGE_H
13*67dac674SAlex Deymo #define __ANDROID_BOOTLOADER_MESSAGE_H
14*67dac674SAlex Deymo 
15*67dac674SAlex Deymo /* compiler.h defines the types that otherwise are included from stdint.h and
16*67dac674SAlex Deymo  * stddef.h
17*67dac674SAlex Deymo  */
18*67dac674SAlex Deymo #include <compiler.h>
19*67dac674SAlex Deymo 
20*67dac674SAlex Deymo /* Spaces used by misc partition are as below:
21*67dac674SAlex Deymo  * 0   - 2K     Bootloader Message
22*67dac674SAlex Deymo  * 2K  - 16K    Used by Vendor's bootloader (the 2K - 4K range may be optionally used
23*67dac674SAlex Deymo  *              as bootloader_message_ab struct)
24*67dac674SAlex Deymo  * 16K - 64K    Used by uncrypt and recovery to store wipe_package for A/B devices
25*67dac674SAlex Deymo  * Note that these offsets are admitted by bootloader,recovery and uncrypt, so they
26*67dac674SAlex Deymo  * are not configurable without changing all of them.
27*67dac674SAlex Deymo  */
28*67dac674SAlex Deymo static const size_t ANDROID_BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0;
29*67dac674SAlex Deymo static const size_t ANDROID_WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024;
30*67dac674SAlex Deymo 
31*67dac674SAlex Deymo /* Bootloader Message (2-KiB)
32*67dac674SAlex Deymo  *
33*67dac674SAlex Deymo  * This structure describes the content of a block in flash
34*67dac674SAlex Deymo  * that is used for recovery and the bootloader to talk to
35*67dac674SAlex Deymo  * each other.
36*67dac674SAlex Deymo  *
37*67dac674SAlex Deymo  * The command field is updated by linux when it wants to
38*67dac674SAlex Deymo  * reboot into recovery or to update radio or bootloader firmware.
39*67dac674SAlex Deymo  * It is also updated by the bootloader when firmware update
40*67dac674SAlex Deymo  * is complete (to boot into recovery for any final cleanup)
41*67dac674SAlex Deymo  *
42*67dac674SAlex Deymo  * The status field is written by the bootloader after the
43*67dac674SAlex Deymo  * completion of an "update-radio" or "update-hboot" command.
44*67dac674SAlex Deymo  *
45*67dac674SAlex Deymo  * The recovery field is only written by linux and used
46*67dac674SAlex Deymo  * for the system to send a message to recovery or the
47*67dac674SAlex Deymo  * other way around.
48*67dac674SAlex Deymo  *
49*67dac674SAlex Deymo  * The stage field is written by packages which restart themselves
50*67dac674SAlex Deymo  * multiple times, so that the UI can reflect which invocation of the
51*67dac674SAlex Deymo  * package it is.  If the value is of the format "#/#" (eg, "1/3"),
52*67dac674SAlex Deymo  * the UI will add a simple indicator of that status.
53*67dac674SAlex Deymo  *
54*67dac674SAlex Deymo  * We used to have slot_suffix field for A/B boot control metadata in
55*67dac674SAlex Deymo  * this struct, which gets unintentionally cleared by recovery or
56*67dac674SAlex Deymo  * uncrypt. Move it into struct bootloader_message_ab to avoid the
57*67dac674SAlex Deymo  * issue.
58*67dac674SAlex Deymo  */
59*67dac674SAlex Deymo struct android_bootloader_message {
60*67dac674SAlex Deymo     char command[32];
61*67dac674SAlex Deymo     char status[32];
62*67dac674SAlex Deymo     char recovery[768];
63*67dac674SAlex Deymo 
64*67dac674SAlex Deymo     /* The 'recovery' field used to be 1024 bytes.  It has only ever
65*67dac674SAlex Deymo      * been used to store the recovery command line, so 768 bytes
66*67dac674SAlex Deymo      * should be plenty.  We carve off the last 256 bytes to store the
67*67dac674SAlex Deymo      * stage string (for multistage packages) and possible future
68*67dac674SAlex Deymo      * expansion. */
69*67dac674SAlex Deymo     char stage[32];
70*67dac674SAlex Deymo 
71*67dac674SAlex Deymo     /* The 'reserved' field used to be 224 bytes when it was initially
72*67dac674SAlex Deymo      * carved off from the 1024-byte recovery field. Bump it up to
73*67dac674SAlex Deymo      * 1184-byte so that the entire bootloader_message struct rounds up
74*67dac674SAlex Deymo      * to 2048-byte. */
75*67dac674SAlex Deymo     char reserved[1184];
76*67dac674SAlex Deymo };
77*67dac674SAlex Deymo 
78*67dac674SAlex Deymo /**
79*67dac674SAlex Deymo  * We must be cautious when changing the bootloader_message struct size,
80*67dac674SAlex Deymo  * because A/B-specific fields may end up with different offsets.
81*67dac674SAlex Deymo  */
82*67dac674SAlex Deymo #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
83*67dac674SAlex Deymo static_assert(sizeof(struct android_bootloader_message) == 2048,
84*67dac674SAlex Deymo               "struct bootloader_message size changes, which may break A/B devices");
85*67dac674SAlex Deymo #endif
86*67dac674SAlex Deymo 
87*67dac674SAlex Deymo /**
88*67dac674SAlex Deymo  * The A/B-specific bootloader message structure (4-KiB).
89*67dac674SAlex Deymo  *
90*67dac674SAlex Deymo  * We separate A/B boot control metadata from the regular bootloader
91*67dac674SAlex Deymo  * message struct and keep it here. Everything that's A/B-specific
92*67dac674SAlex Deymo  * stays after struct bootloader_message, which should be managed by
93*67dac674SAlex Deymo  * the A/B-bootloader or boot control HAL.
94*67dac674SAlex Deymo  *
95*67dac674SAlex Deymo  * The slot_suffix field is used for A/B implementations where the
96*67dac674SAlex Deymo  * bootloader does not set the androidboot.ro.boot.slot_suffix kernel
97*67dac674SAlex Deymo  * commandline parameter. This is used by fs_mgr to mount /system and
98*67dac674SAlex Deymo  * other partitions with the slotselect flag set in fstab. A/B
99*67dac674SAlex Deymo  * implementations are free to use all 32 bytes and may store private
100*67dac674SAlex Deymo  * data past the first NUL-byte in this field. It is encouraged, but
101*67dac674SAlex Deymo  * not mandatory, to use 'struct bootloader_control' described below.
102*67dac674SAlex Deymo  */
103*67dac674SAlex Deymo struct android_bootloader_message_ab {
104*67dac674SAlex Deymo     struct android_bootloader_message message;
105*67dac674SAlex Deymo     char slot_suffix[32];
106*67dac674SAlex Deymo 
107*67dac674SAlex Deymo     /* Round up the entire struct to 4096-byte. */
108*67dac674SAlex Deymo     char reserved[2016];
109*67dac674SAlex Deymo };
110*67dac674SAlex Deymo 
111*67dac674SAlex Deymo /**
112*67dac674SAlex Deymo  * Be cautious about the struct size change, in case we put anything post
113*67dac674SAlex Deymo  * bootloader_message_ab struct (b/29159185).
114*67dac674SAlex Deymo  */
115*67dac674SAlex Deymo #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
116*67dac674SAlex Deymo static_assert(sizeof(struct android_bootloader_message_ab) == 4096,
117*67dac674SAlex Deymo               "struct bootloader_message_ab size changes");
118*67dac674SAlex Deymo #endif
119*67dac674SAlex Deymo 
120*67dac674SAlex Deymo #define ANDROID_BOOT_CTRL_MAGIC   0x42414342 /* Bootloader Control AB */
121*67dac674SAlex Deymo #define ANDROID_BOOT_CTRL_VERSION 1
122*67dac674SAlex Deymo 
123*67dac674SAlex Deymo struct android_slot_metadata {
124*67dac674SAlex Deymo     /* Slot priority with 15 meaning highest priority, 1 lowest
125*67dac674SAlex Deymo      * priority and 0 the slot is unbootable. */
126*67dac674SAlex Deymo     uint8_t priority : 4;
127*67dac674SAlex Deymo     /* Number of times left attempting to boot this slot. */
128*67dac674SAlex Deymo     uint8_t tries_remaining : 3;
129*67dac674SAlex Deymo     /* 1 if this slot has booted successfully, 0 otherwise. */
130*67dac674SAlex Deymo     uint8_t successful_boot : 1;
131*67dac674SAlex Deymo     /* 1 if this slot is corrupted from a dm-verity corruption, 0 */
132*67dac674SAlex Deymo     /* otherwise. */
133*67dac674SAlex Deymo     uint8_t verity_corrupted : 1;
134*67dac674SAlex Deymo     /* Reserved for further use. */
135*67dac674SAlex Deymo     uint8_t reserved : 7;
136*67dac674SAlex Deymo } __attribute__((packed));
137*67dac674SAlex Deymo 
138*67dac674SAlex Deymo /* Bootloader Control AB
139*67dac674SAlex Deymo  *
140*67dac674SAlex Deymo  * This struct can be used to manage A/B metadata. It is designed to
141*67dac674SAlex Deymo  * be put in the 'slot_suffix' field of the 'bootloader_message'
142*67dac674SAlex Deymo  * structure described above. It is encouraged to use the
143*67dac674SAlex Deymo  * 'bootloader_control' structure to store the A/B metadata, but not
144*67dac674SAlex Deymo  * mandatory.
145*67dac674SAlex Deymo  */
146*67dac674SAlex Deymo struct android_bootloader_control {
147*67dac674SAlex Deymo     /* NUL terminated active slot suffix. */
148*67dac674SAlex Deymo     char slot_suffix[4];
149*67dac674SAlex Deymo     /* Bootloader Control AB magic number (see BOOT_CTRL_MAGIC). */
150*67dac674SAlex Deymo     uint32_t magic;
151*67dac674SAlex Deymo     /* Version of struct being used (see BOOT_CTRL_VERSION). */
152*67dac674SAlex Deymo     uint8_t version;
153*67dac674SAlex Deymo     /* Number of slots being managed. */
154*67dac674SAlex Deymo     uint8_t nb_slot : 3;
155*67dac674SAlex Deymo     /* Number of times left attempting to boot recovery. */
156*67dac674SAlex Deymo     uint8_t recovery_tries_remaining : 3;
157*67dac674SAlex Deymo     /* Ensure 4-bytes alignment for slot_info field. */
158*67dac674SAlex Deymo     uint8_t reserved0[2];
159*67dac674SAlex Deymo     /* Per-slot information.  Up to 4 slots. */
160*67dac674SAlex Deymo     struct android_slot_metadata slot_info[4];
161*67dac674SAlex Deymo     /* Reserved for further use. */
162*67dac674SAlex Deymo     uint8_t reserved1[8];
163*67dac674SAlex Deymo     /* CRC32 of all 28 bytes preceding this field (little endian
164*67dac674SAlex Deymo      * format). */
165*67dac674SAlex Deymo     uint32_t crc32_le;
166*67dac674SAlex Deymo } __attribute__((packed));
167*67dac674SAlex Deymo 
168*67dac674SAlex Deymo #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
169*67dac674SAlex Deymo static_assert(sizeof(struct android_bootloader_control) ==
170*67dac674SAlex Deymo               sizeof(((struct android_bootloader_message_ab *)0)->slot_suffix),
171*67dac674SAlex Deymo               "struct bootloader_control has wrong size");
172*67dac674SAlex Deymo #endif
173*67dac674SAlex Deymo 
174*67dac674SAlex Deymo #endif  /* __ANDROID_BOOTLOADER_MESSAGE_H */
175