• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 #ifndef _BOOTLOADER_MESSAGE_H
18 #define _BOOTLOADER_MESSAGE_H
19 
20 #include <assert.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 
24 // Spaces used by misc partition are as below:
25 // 0   - 2K     For bootloader_message
26 // 2K  - 16K    Used by Vendor's bootloader (the 2K - 4K range may be optionally used
27 //              as bootloader_message_ab struct)
28 // 16K - 64K    Used by uncrypt and recovery to store wipe_package for A/B devices
29 // Note that these offsets are admitted by bootloader,recovery and uncrypt, so they
30 // are not configurable without changing all of them.
31 constexpr size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0;
32 constexpr size_t VENDOR_SPACE_OFFSET_IN_MISC = 2 * 1024;
33 constexpr size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024;
34 
35 /* Bootloader Message (2-KiB)
36  *
37  * This structure describes the content of a block in flash
38  * that is used for recovery and the bootloader to talk to
39  * each other.
40  *
41  * The command field is updated by linux when it wants to
42  * reboot into recovery or to update radio or bootloader firmware.
43  * It is also updated by the bootloader when firmware update
44  * is complete (to boot into recovery for any final cleanup)
45  *
46  * The status field was used by the bootloader after the completion
47  * of an "update-radio" or "update-hboot" command, which has been
48  * deprecated since Froyo.
49  *
50  * The recovery field is only written by linux and used
51  * for the system to send a message to recovery or the
52  * other way around.
53  *
54  * The stage field is written by packages which restart themselves
55  * multiple times, so that the UI can reflect which invocation of the
56  * package it is.  If the value is of the format "#/#" (eg, "1/3"),
57  * the UI will add a simple indicator of that status.
58  *
59  * We used to have slot_suffix field for A/B boot control metadata in
60  * this struct, which gets unintentionally cleared by recovery or
61  * uncrypt. Move it into struct bootloader_message_ab to avoid the
62  * issue.
63  */
64 struct bootloader_message {
65     char command[32];
66     char status[32];
67     char recovery[768];
68 
69     // The 'recovery' field used to be 1024 bytes.  It has only ever
70     // been used to store the recovery command line, so 768 bytes
71     // should be plenty.  We carve off the last 256 bytes to store the
72     // stage string (for multistage packages) and possible future
73     // expansion.
74     char stage[32];
75 
76     // The 'reserved' field used to be 224 bytes when it was initially
77     // carved off from the 1024-byte recovery field. Bump it up to
78     // 1184-byte so that the entire bootloader_message struct rounds up
79     // to 2048-byte.
80     char reserved[1184];
81 };
82 
83 /**
84  * We must be cautious when changing the bootloader_message struct size,
85  * because A/B-specific fields may end up with different offsets.
86  */
87 #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
88 static_assert(sizeof(struct bootloader_message) == 2048,
89               "struct bootloader_message size changes, which may break A/B devices");
90 #endif
91 
92 /**
93  * The A/B-specific bootloader message structure (4-KiB).
94  *
95  * We separate A/B boot control metadata from the regular bootloader
96  * message struct and keep it here. Everything that's A/B-specific
97  * stays after struct bootloader_message, which should be managed by
98  * the A/B-bootloader or boot control HAL.
99  *
100  * The slot_suffix field is used for A/B implementations where the
101  * bootloader does not set the androidboot.ro.boot.slot_suffix kernel
102  * commandline parameter. This is used by fs_mgr to mount /system and
103  * other partitions with the slotselect flag set in fstab. A/B
104  * implementations are free to use all 32 bytes and may store private
105  * data past the first NUL-byte in this field. It is encouraged, but
106  * not mandatory, to use 'struct bootloader_control' described below.
107  *
108  * The update_channel field is used to store the Omaha update channel
109  * if update_engine is compiled with Omaha support.
110  */
111 struct bootloader_message_ab {
112     struct bootloader_message message;
113     char slot_suffix[32];
114     char update_channel[128];
115 
116     // Round up the entire struct to 4096-byte.
117     char reserved[1888];
118 };
119 
120 /**
121  * Be cautious about the struct size change, in case we put anything post
122  * bootloader_message_ab struct (b/29159185).
123  */
124 #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
125 static_assert(sizeof(struct bootloader_message_ab) == 4096,
126               "struct bootloader_message_ab size changes");
127 #endif
128 
129 #define BOOT_CTRL_MAGIC   0x42414342 /* Bootloader Control AB */
130 #define BOOT_CTRL_VERSION 1
131 
132 struct slot_metadata {
133     // Slot priority with 15 meaning highest priority, 1 lowest
134     // priority and 0 the slot is unbootable.
135     uint8_t priority : 4;
136     // Number of times left attempting to boot this slot.
137     uint8_t tries_remaining : 3;
138     // 1 if this slot has booted successfully, 0 otherwise.
139     uint8_t successful_boot : 1;
140     // 1 if this slot is corrupted from a dm-verity corruption, 0
141     // otherwise.
142     uint8_t verity_corrupted : 1;
143     // Reserved for further use.
144     uint8_t reserved : 7;
145 } __attribute__((packed));
146 
147 /* Bootloader Control AB
148  *
149  * This struct can be used to manage A/B metadata. It is designed to
150  * be put in the 'slot_suffix' field of the 'bootloader_message'
151  * structure described above. It is encouraged to use the
152  * 'bootloader_control' structure to store the A/B metadata, but not
153  * mandatory.
154  */
155 struct bootloader_control {
156     // NUL terminated active slot suffix.
157     char slot_suffix[4];
158     // Bootloader Control AB magic number (see BOOT_CTRL_MAGIC).
159     uint32_t magic;
160     // Version of struct being used (see BOOT_CTRL_VERSION).
161     uint8_t version;
162     // Number of slots being managed.
163     uint8_t nb_slot : 3;
164     // Number of times left attempting to boot recovery.
165     uint8_t recovery_tries_remaining : 3;
166     // Ensure 4-bytes alignment for slot_info field.
167     uint8_t reserved0[2];
168     // Per-slot information.  Up to 4 slots.
169     struct slot_metadata slot_info[4];
170     // Reserved for further use.
171     uint8_t reserved1[8];
172     // CRC32 of all 28 bytes preceding this field (little endian
173     // format).
174     uint32_t crc32_le;
175 } __attribute__((packed));
176 
177 #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
178 static_assert(sizeof(struct bootloader_control) ==
179               sizeof(((struct bootloader_message_ab *)0)->slot_suffix),
180               "struct bootloader_control has wrong size");
181 #endif
182 
183 #ifdef __cplusplus
184 
185 #include <string>
186 #include <vector>
187 
188 // Return the block device name for the bootloader message partition and waits
189 // for the device for up to 10 seconds. In case of error returns the empty
190 // string.
191 std::string get_bootloader_message_blk_device(std::string* err);
192 
193 // Read bootloader message into boot. Error message will be set in err.
194 bool read_bootloader_message(bootloader_message* boot, std::string* err);
195 
196 // Read bootloader message from the specified misc device into boot.
197 bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
198                                   std::string* err);
199 
200 // Write bootloader message to BCB.
201 bool write_bootloader_message(const bootloader_message& boot, std::string* err);
202 
203 // Write bootloader message to the specified BCB device.
204 bool write_bootloader_message_to(const bootloader_message& boot,
205                                  const std::string& misc_blk_device, std::string* err);
206 
207 // Write bootloader message (boots into recovery with the options) to BCB. Will
208 // set the command and recovery fields, and reset the rest.
209 bool write_bootloader_message(const std::vector<std::string>& options, std::string* err);
210 
211 // Update bootloader message (boots into recovery with the options) to BCB. Will
212 // only update the command and recovery fields.
213 bool update_bootloader_message(const std::vector<std::string>& options, std::string* err);
214 
215 // Update bootloader message (boots into recovery with the |options|) in |boot|. Will only update
216 // the command and recovery fields.
217 bool update_bootloader_message_in_struct(bootloader_message* boot,
218                                          const std::vector<std::string>& options);
219 
220 // Clear BCB.
221 bool clear_bootloader_message(std::string* err);
222 
223 // Writes the reboot-bootloader reboot reason to the bootloader_message.
224 bool write_reboot_bootloader(std::string* err);
225 
226 // Read the wipe package from BCB (from offset WIPE_PACKAGE_OFFSET_IN_MISC).
227 bool read_wipe_package(std::string* package_data, size_t size, std::string* err);
228 
229 // Write the wipe package into BCB (to offset WIPE_PACKAGE_OFFSET_IN_MISC).
230 bool write_wipe_package(const std::string& package_data, std::string* err);
231 
232 // Reads data from the vendor space in /misc partition, with the given offset and size. Note that
233 // offset is in relative to the start of vendor space.
234 bool ReadMiscPartitionVendorSpace(void* data, size_t size, size_t offset, std::string* err);
235 
236 // Writes the given data to the vendor space in /misc partition, at the given offset. Note that
237 // offset is in relative to the start of the vendor space.
238 bool WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset, std::string* err);
239 
240 #else
241 
242 #include <stdbool.h>
243 
244 // C Interface.
245 bool write_bootloader_message(const char* options);
246 bool write_reboot_bootloader(void);
247 
248 #endif  // ifdef __cplusplus
249 
250 #endif  // _BOOTLOADER_MESSAGE_H
251