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