blob: 975e94db43a0f9f2001bd64cadaaf466ef5900a2 [file] [log] [blame]
Yabin Cui2f272c02016-06-24 18:22:02 -07001/*
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
Yabin Cui9b15ba82016-06-30 14:36:59 -070020#include <assert.h>
Yabin Cui2f272c02016-06-24 18:22:02 -070021#include <stddef.h>
Yabin Cui9b15ba82016-06-30 14:36:59 -070022#include <stdint.h>
Yabin Cui2f272c02016-06-24 18:22:02 -070023
Yabin Cui9b15ba82016-06-30 14:36:59 -070024/* Bootloader Message (2-KiB)
Yabin Cui2f272c02016-06-24 18:22:02 -070025 *
26 * This structure describes the content of a block in flash
27 * that is used for recovery and the bootloader to talk to
28 * each other.
29 *
30 * The command field is updated by linux when it wants to
31 * reboot into recovery or to update radio or bootloader firmware.
32 * It is also updated by the bootloader when firmware update
33 * is complete (to boot into recovery for any final cleanup)
34 *
Tao Baoc0336392016-12-13 22:29:49 -080035 * The status field was used by the bootloader after the completion
36 * of an "update-radio" or "update-hboot" command, which has been
37 * deprecated since Froyo.
Yabin Cui2f272c02016-06-24 18:22:02 -070038 *
39 * The recovery field is only written by linux and used
40 * for the system to send a message to recovery or the
41 * other way around.
42 *
43 * The stage field is written by packages which restart themselves
44 * multiple times, so that the UI can reflect which invocation of the
45 * package it is. If the value is of the format "#/#" (eg, "1/3"),
46 * the UI will add a simple indicator of that status.
47 *
Yabin Cui9b15ba82016-06-30 14:36:59 -070048 * We used to have slot_suffix field for A/B boot control metadata in
49 * this struct, which gets unintentionally cleared by recovery or
50 * uncrypt. Move it into struct bootloader_message_ab to avoid the
51 * issue.
Yabin Cui2f272c02016-06-24 18:22:02 -070052 */
53struct bootloader_message {
54 char command[32];
55 char status[32];
56 char recovery[768];
57
58 // The 'recovery' field used to be 1024 bytes. It has only ever
59 // been used to store the recovery command line, so 768 bytes
60 // should be plenty. We carve off the last 256 bytes to store the
61 // stage string (for multistage packages) and possible future
62 // expansion.
63 char stage[32];
Yabin Cui9b15ba82016-06-30 14:36:59 -070064
65 // The 'reserved' field used to be 224 bytes when it was initially
66 // carved off from the 1024-byte recovery field. Bump it up to
67 // 1184-byte so that the entire bootloader_message struct rounds up
68 // to 2048-byte.
69 char reserved[1184];
Yabin Cui2f272c02016-06-24 18:22:02 -070070};
71
Yabin Cui9b15ba82016-06-30 14:36:59 -070072/**
73 * We must be cautious when changing the bootloader_message struct size,
74 * because A/B-specific fields may end up with different offsets.
75 */
76#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
77static_assert(sizeof(struct bootloader_message) == 2048,
78 "struct bootloader_message size changes, which may break A/B devices");
79#endif
80
81/**
82 * The A/B-specific bootloader message structure (4-KiB).
83 *
84 * We separate A/B boot control metadata from the regular bootloader
85 * message struct and keep it here. Everything that's A/B-specific
86 * stays after struct bootloader_message, which should be managed by
87 * the A/B-bootloader or boot control HAL.
88 *
89 * The slot_suffix field is used for A/B implementations where the
90 * bootloader does not set the androidboot.ro.boot.slot_suffix kernel
91 * commandline parameter. This is used by fs_mgr to mount /system and
92 * other partitions with the slotselect flag set in fstab. A/B
93 * implementations are free to use all 32 bytes and may store private
94 * data past the first NUL-byte in this field. It is encouraged, but
95 * not mandatory, to use 'struct bootloader_control' described below.
Sen Jiang7191bf02018-01-22 15:01:48 -080096 *
97 * The update_channel field is used to store the Omaha update channel
98 * if update_engine is compiled with Omaha support.
Yabin Cui9b15ba82016-06-30 14:36:59 -070099 */
100struct bootloader_message_ab {
101 struct bootloader_message message;
102 char slot_suffix[32];
Sen Jiang7191bf02018-01-22 15:01:48 -0800103 char update_channel[128];
Yabin Cui9b15ba82016-06-30 14:36:59 -0700104
105 // Round up the entire struct to 4096-byte.
Sen Jiang7191bf02018-01-22 15:01:48 -0800106 char reserved[1888];
Yabin Cui9b15ba82016-06-30 14:36:59 -0700107};
108
109/**
110 * Be cautious about the struct size change, in case we put anything post
111 * bootloader_message_ab struct (b/29159185).
112 */
113#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
114static_assert(sizeof(struct bootloader_message_ab) == 4096,
115 "struct bootloader_message_ab size changes");
116#endif
117
118#define BOOT_CTRL_MAGIC 0x42414342 /* Bootloader Control AB */
119#define BOOT_CTRL_VERSION 1
120
121struct slot_metadata {
122 // Slot priority with 15 meaning highest priority, 1 lowest
123 // priority and 0 the slot is unbootable.
124 uint8_t priority : 4;
125 // Number of times left attempting to boot this slot.
126 uint8_t tries_remaining : 3;
127 // 1 if this slot has booted successfully, 0 otherwise.
128 uint8_t successful_boot : 1;
129 // 1 if this slot is corrupted from a dm-verity corruption, 0
130 // otherwise.
131 uint8_t verity_corrupted : 1;
132 // Reserved for further use.
133 uint8_t reserved : 7;
134} __attribute__((packed));
135
136/* Bootloader Control AB
137 *
138 * This struct can be used to manage A/B metadata. It is designed to
139 * be put in the 'slot_suffix' field of the 'bootloader_message'
140 * structure described above. It is encouraged to use the
141 * 'bootloader_control' structure to store the A/B metadata, but not
142 * mandatory.
143 */
144struct bootloader_control {
145 // NUL terminated active slot suffix.
146 char slot_suffix[4];
147 // Bootloader Control AB magic number (see BOOT_CTRL_MAGIC).
148 uint32_t magic;
149 // Version of struct being used (see BOOT_CTRL_VERSION).
150 uint8_t version;
151 // Number of slots being managed.
152 uint8_t nb_slot : 3;
153 // Number of times left attempting to boot recovery.
154 uint8_t recovery_tries_remaining : 3;
155 // Ensure 4-bytes alignment for slot_info field.
156 uint8_t reserved0[2];
157 // Per-slot information. Up to 4 slots.
158 struct slot_metadata slot_info[4];
159 // Reserved for further use.
160 uint8_t reserved1[8];
161 // CRC32 of all 28 bytes preceding this field (little endian
162 // format).
163 uint32_t crc32_le;
164} __attribute__((packed));
165
166#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
167static_assert(sizeof(struct bootloader_control) ==
168 sizeof(((struct bootloader_message_ab *)0)->slot_suffix),
169 "struct bootloader_control has wrong size");
170#endif
171
Yabin Cui2f272c02016-06-24 18:22:02 -0700172#ifdef __cplusplus
173
174#include <string>
175#include <vector>
176
Alex Deymofb00d822016-11-08 15:46:07 -0800177// Return the block device name for the bootloader message partition and waits
178// for the device for up to 10 seconds. In case of error returns the empty
179// string.
180std::string get_bootloader_message_blk_device(std::string* err);
181
Tao Baobedf5fc2016-11-18 12:01:26 -0800182// Read bootloader message into boot. Error message will be set in err.
Yabin Cui2f272c02016-06-24 18:22:02 -0700183bool read_bootloader_message(bootloader_message* boot, std::string* err);
Tao Baobedf5fc2016-11-18 12:01:26 -0800184
185// Read bootloader message from the specified misc device into boot.
186bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
187 std::string* err);
188
189// Write bootloader message to BCB.
Yabin Cui2f272c02016-06-24 18:22:02 -0700190bool write_bootloader_message(const bootloader_message& boot, std::string* err);
Tao Baobedf5fc2016-11-18 12:01:26 -0800191
192// Write bootloader message to the specified BCB device.
193bool write_bootloader_message_to(const bootloader_message& boot,
194 const std::string& misc_blk_device, std::string* err);
195
Tao Bao2292db82016-12-13 21:53:31 -0800196// Write bootloader message (boots into recovery with the options) to BCB. Will
197// set the command and recovery fields, and reset the rest.
Yabin Cui2f272c02016-06-24 18:22:02 -0700198bool write_bootloader_message(const std::vector<std::string>& options, std::string* err);
Tao Baobedf5fc2016-11-18 12:01:26 -0800199
Tao Bao2292db82016-12-13 21:53:31 -0800200// Update bootloader message (boots into recovery with the options) to BCB. Will
201// only update the command and recovery fields.
202bool update_bootloader_message(const std::vector<std::string>& options, std::string* err);
203
Tianjie Xua88cc542017-10-25 13:16:54 -0700204// Update bootloader message (boots into recovery with the |options|) in |boot|. Will only update
205// the command and recovery fields.
206bool update_bootloader_message_in_struct(bootloader_message* boot,
207 const std::vector<std::string>& options);
208
Tao Baobedf5fc2016-11-18 12:01:26 -0800209// Clear BCB.
Yabin Cui2f272c02016-06-24 18:22:02 -0700210bool clear_bootloader_message(std::string* err);
211
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700212// Writes the reboot-bootloader reboot reason to the bootloader_message.
213bool write_reboot_bootloader(std::string* err);
214
Tao Baobedf5fc2016-11-18 12:01:26 -0800215// Read the wipe package from BCB (from offset WIPE_PACKAGE_OFFSET_IN_MISC).
Yabin Cui2f272c02016-06-24 18:22:02 -0700216bool read_wipe_package(std::string* package_data, size_t size, std::string* err);
Tao Baobedf5fc2016-11-18 12:01:26 -0800217
218// Write the wipe package into BCB (to offset WIPE_PACKAGE_OFFSET_IN_MISC).
Yabin Cui2f272c02016-06-24 18:22:02 -0700219bool write_wipe_package(const std::string& package_data, std::string* err);
220
Yabin Cui2f272c02016-06-24 18:22:02 -0700221#else
222
223#include <stdbool.h>
224
225// C Interface.
226bool write_bootloader_message(const char* options);
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700227bool write_reboot_bootloader(void);
Yabin Cui2f272c02016-06-24 18:22:02 -0700228
229#endif // ifdef __cplusplus
230
231#endif // _BOOTLOADER_MESSAGE_H