blob: 286d7ab0f31e91bfe7b2a85c5ca7d3cdd865e34d [file] [log] [blame]
Alex Deymo27d15612019-05-14 21:05:26 +02001/*
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
Eugeniu Rosca0381b712019-05-23 17:32:21 +02005 * Commit: See U-Boot commit description
Alex Deymo27d15612019-05-14 21:05:26 +02006 *
7 * Copyright (C) 2008 The Android Open Source Project
8 *
9 * SPDX-License-Identifier: BSD-3-Clause
10 */
11
12#ifndef __ANDROID_BOOTLOADER_MESSAGE_H
13#define __ANDROID_BOOTLOADER_MESSAGE_H
14
Eugeniu Rosca0381b712019-05-23 17:32:21 +020015#ifndef __UBOOT__
16#include <assert.h>
17#include <stddef.h>
18#include <stdint.h>
19#else
Alex Deymo27d15612019-05-14 21:05:26 +020020/* compiler.h defines the types that otherwise are included from stdint.h and
21 * stddef.h
22 */
23#include <compiler.h>
Eugeniu Rosca0381b712019-05-23 17:32:21 +020024#endif
Alex Deymo27d15612019-05-14 21:05:26 +020025
Eugeniu Rosca0381b712019-05-23 17:32:21 +020026// Spaces used by misc partition are as below:
27// 0 - 2K For bootloader_message
28// 2K - 16K Used by Vendor's bootloader (the 2K - 4K range may be optionally used
29// as bootloader_message_ab struct)
30// 16K - 64K Used by uncrypt and recovery to store wipe_package for A/B devices
31// Note that these offsets are admitted by bootloader,recovery and uncrypt, so they
32// are not configurable without changing all of them.
Alex Deymo27d15612019-05-14 21:05:26 +020033static const size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0;
34static const size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024;
35
36/* Bootloader Message (2-KiB)
37 *
38 * This structure describes the content of a block in flash
39 * that is used for recovery and the bootloader to talk to
40 * each other.
41 *
42 * The command field is updated by linux when it wants to
43 * reboot into recovery or to update radio or bootloader firmware.
44 * It is also updated by the bootloader when firmware update
45 * is complete (to boot into recovery for any final cleanup)
46 *
47 * The status field was used by the bootloader after the completion
48 * of an "update-radio" or "update-hboot" command, which has been
49 * deprecated since Froyo.
50 *
51 * The recovery field is only written by linux and used
52 * for the system to send a message to recovery or the
53 * other way around.
54 *
55 * The stage field is written by packages which restart themselves
56 * multiple times, so that the UI can reflect which invocation of the
57 * package it is. If the value is of the format "#/#" (eg, "1/3"),
58 * the UI will add a simple indicator of that status.
59 *
60 * We used to have slot_suffix field for A/B boot control metadata in
61 * this struct, which gets unintentionally cleared by recovery or
62 * uncrypt. Move it into struct bootloader_message_ab to avoid the
63 * issue.
64 */
65struct bootloader_message {
66 char command[32];
67 char status[32];
68 char recovery[768];
69
Eugeniu Rosca0381b712019-05-23 17:32:21 +020070 // The 'recovery' field used to be 1024 bytes. It has only ever
71 // been used to store the recovery command line, so 768 bytes
72 // should be plenty. We carve off the last 256 bytes to store the
73 // stage string (for multistage packages) and possible future
74 // expansion.
Alex Deymo27d15612019-05-14 21:05:26 +020075 char stage[32];
76
Eugeniu Rosca0381b712019-05-23 17:32:21 +020077 // The 'reserved' field used to be 224 bytes when it was initially
78 // carved off from the 1024-byte recovery field. Bump it up to
79 // 1184-byte so that the entire bootloader_message struct rounds up
80 // to 2048-byte.
Alex Deymo27d15612019-05-14 21:05:26 +020081 char reserved[1184];
82};
83
84/**
85 * We must be cautious when changing the bootloader_message struct size,
86 * because A/B-specific fields may end up with different offsets.
87 */
Eugeniu Rosca0381b712019-05-23 17:32:21 +020088#ifndef __UBOOT__
Alex Deymo27d15612019-05-14 21:05:26 +020089#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
90static_assert(sizeof(struct bootloader_message) == 2048,
91 "struct bootloader_message size changes, which may break A/B devices");
92#endif
Eugeniu Rosca0381b712019-05-23 17:32:21 +020093#endif /* __UBOOT__ */
Alex Deymo27d15612019-05-14 21:05:26 +020094
95/**
96 * The A/B-specific bootloader message structure (4-KiB).
97 *
98 * We separate A/B boot control metadata from the regular bootloader
99 * message struct and keep it here. Everything that's A/B-specific
100 * stays after struct bootloader_message, which should be managed by
101 * the A/B-bootloader or boot control HAL.
102 *
103 * The slot_suffix field is used for A/B implementations where the
104 * bootloader does not set the androidboot.ro.boot.slot_suffix kernel
105 * commandline parameter. This is used by fs_mgr to mount /system and
106 * other partitions with the slotselect flag set in fstab. A/B
107 * implementations are free to use all 32 bytes and may store private
108 * data past the first NUL-byte in this field. It is encouraged, but
109 * not mandatory, to use 'struct bootloader_control' described below.
110 *
111 * The update_channel field is used to store the Omaha update channel
112 * if update_engine is compiled with Omaha support.
113 */
114struct bootloader_message_ab {
115 struct bootloader_message message;
116 char slot_suffix[32];
117 char update_channel[128];
118
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200119 // Round up the entire struct to 4096-byte.
Alex Deymo27d15612019-05-14 21:05:26 +0200120 char reserved[1888];
121};
122
123/**
124 * Be cautious about the struct size change, in case we put anything post
125 * bootloader_message_ab struct (b/29159185).
126 */
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200127#ifndef __UBOOT__
Alex Deymo27d15612019-05-14 21:05:26 +0200128#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
129static_assert(sizeof(struct bootloader_message_ab) == 4096,
130 "struct bootloader_message_ab size changes");
131#endif
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200132#endif /* __UBOOT__ */
Alex Deymo27d15612019-05-14 21:05:26 +0200133
134#define BOOT_CTRL_MAGIC 0x42414342 /* Bootloader Control AB */
135#define BOOT_CTRL_VERSION 1
136
137struct slot_metadata {
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200138 // Slot priority with 15 meaning highest priority, 1 lowest
139 // priority and 0 the slot is unbootable.
Alex Deymo27d15612019-05-14 21:05:26 +0200140 uint8_t priority : 4;
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200141 // Number of times left attempting to boot this slot.
Alex Deymo27d15612019-05-14 21:05:26 +0200142 uint8_t tries_remaining : 3;
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200143 // 1 if this slot has booted successfully, 0 otherwise.
Alex Deymo27d15612019-05-14 21:05:26 +0200144 uint8_t successful_boot : 1;
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200145 // 1 if this slot is corrupted from a dm-verity corruption, 0
146 // otherwise.
Alex Deymo27d15612019-05-14 21:05:26 +0200147 uint8_t verity_corrupted : 1;
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200148 // Reserved for further use.
Alex Deymo27d15612019-05-14 21:05:26 +0200149 uint8_t reserved : 7;
150} __attribute__((packed));
151
152/* Bootloader Control AB
153 *
154 * This struct can be used to manage A/B metadata. It is designed to
155 * be put in the 'slot_suffix' field of the 'bootloader_message'
156 * structure described above. It is encouraged to use the
157 * 'bootloader_control' structure to store the A/B metadata, but not
158 * mandatory.
159 */
160struct bootloader_control {
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200161 // NUL terminated active slot suffix.
Alex Deymo27d15612019-05-14 21:05:26 +0200162 char slot_suffix[4];
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200163 // Bootloader Control AB magic number (see BOOT_CTRL_MAGIC).
Alex Deymo27d15612019-05-14 21:05:26 +0200164 uint32_t magic;
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200165 // Version of struct being used (see BOOT_CTRL_VERSION).
Alex Deymo27d15612019-05-14 21:05:26 +0200166 uint8_t version;
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200167 // Number of slots being managed.
Alex Deymo27d15612019-05-14 21:05:26 +0200168 uint8_t nb_slot : 3;
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200169 // Number of times left attempting to boot recovery.
Alex Deymo27d15612019-05-14 21:05:26 +0200170 uint8_t recovery_tries_remaining : 3;
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200171 // Ensure 4-bytes alignment for slot_info field.
Alex Deymo27d15612019-05-14 21:05:26 +0200172 uint8_t reserved0[2];
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200173 // Per-slot information. Up to 4 slots.
Alex Deymo27d15612019-05-14 21:05:26 +0200174 struct slot_metadata slot_info[4];
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200175 // Reserved for further use.
Alex Deymo27d15612019-05-14 21:05:26 +0200176 uint8_t reserved1[8];
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200177 // CRC32 of all 28 bytes preceding this field (little endian
178 // format).
Alex Deymo27d15612019-05-14 21:05:26 +0200179 uint32_t crc32_le;
180} __attribute__((packed));
181
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200182#ifndef __UBOOT__
Alex Deymo27d15612019-05-14 21:05:26 +0200183#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
184static_assert(sizeof(struct bootloader_control) ==
185 sizeof(((struct bootloader_message_ab *)0)->slot_suffix),
186 "struct bootloader_control has wrong size");
187#endif
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200188#endif /* __UBOOT__ */
Alex Deymo27d15612019-05-14 21:05:26 +0200189
190#ifndef __UBOOT__
Alex Deymo27d15612019-05-14 21:05:26 +0200191#ifdef __cplusplus
192
193#include <string>
194#include <vector>
195
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200196// Return the block device name for the bootloader message partition and waits
197// for the device for up to 10 seconds. In case of error returns the empty
198// string.
Alex Deymo27d15612019-05-14 21:05:26 +0200199std::string get_bootloader_message_blk_device(std::string* err);
200
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200201// Read bootloader message into boot. Error message will be set in err.
Alex Deymo27d15612019-05-14 21:05:26 +0200202bool read_bootloader_message(bootloader_message* boot, std::string* err);
203
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200204// Read bootloader message from the specified misc device into boot.
Alex Deymo27d15612019-05-14 21:05:26 +0200205bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
206 std::string* err);
207
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200208// Write bootloader message to BCB.
Alex Deymo27d15612019-05-14 21:05:26 +0200209bool write_bootloader_message(const bootloader_message& boot, std::string* err);
210
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200211// Write bootloader message to the specified BCB device.
Alex Deymo27d15612019-05-14 21:05:26 +0200212bool write_bootloader_message_to(const bootloader_message& boot,
213 const std::string& misc_blk_device, std::string* err);
214
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200215// Write bootloader message (boots into recovery with the options) to BCB. Will
216// set the command and recovery fields, and reset the rest.
Alex Deymo27d15612019-05-14 21:05:26 +0200217bool write_bootloader_message(const std::vector<std::string>& options, std::string* err);
218
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200219// Write bootloader message (boots into recovery with the options) to the specific BCB device. Will
220// set the command and recovery fields, and reset the rest.
Alex Deymo27d15612019-05-14 21:05:26 +0200221bool write_bootloader_message_to(const std::vector<std::string>& options,
222 const std::string& misc_blk_device, std::string* err);
223
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200224// Update bootloader message (boots into recovery with the options) to BCB. Will
225// only update the command and recovery fields.
Alex Deymo27d15612019-05-14 21:05:26 +0200226bool update_bootloader_message(const std::vector<std::string>& options, std::string* err);
227
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200228// Update bootloader message (boots into recovery with the |options|) in |boot|. Will only update
229// the command and recovery fields.
Alex Deymo27d15612019-05-14 21:05:26 +0200230bool update_bootloader_message_in_struct(bootloader_message* boot,
231 const std::vector<std::string>& options);
232
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200233// Clear BCB.
Alex Deymo27d15612019-05-14 21:05:26 +0200234bool clear_bootloader_message(std::string* err);
235
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200236// Writes the reboot-bootloader reboot reason to the bootloader_message.
Alex Deymo27d15612019-05-14 21:05:26 +0200237bool write_reboot_bootloader(std::string* err);
238
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200239// Read the wipe package from BCB (from offset WIPE_PACKAGE_OFFSET_IN_MISC).
Alex Deymo27d15612019-05-14 21:05:26 +0200240bool read_wipe_package(std::string* package_data, size_t size, std::string* err);
241
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200242// Write the wipe package into BCB (to offset WIPE_PACKAGE_OFFSET_IN_MISC).
Alex Deymo27d15612019-05-14 21:05:26 +0200243bool write_wipe_package(const std::string& package_data, std::string* err);
244
245#else
246
247#include <stdbool.h>
248
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200249// C Interface.
Alex Deymo27d15612019-05-14 21:05:26 +0200250bool write_bootloader_message(const char* options);
251bool write_reboot_bootloader(void);
252
Eugeniu Rosca0381b712019-05-23 17:32:21 +0200253#endif // ifdef __cplusplus
Alex Deymo27d15612019-05-14 21:05:26 +0200254#endif /* __UBOOT__ */
255
256#endif /* __ANDROID_BOOTLOADER_MESSAGE_H */