Alex Deymo | 27d1561 | 2019-05-14 21:05:26 +0200 | [diff] [blame] | 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: c784ce50e8c10eaf70e1f97e24e8324aef45faf5 |
| 6 | * |
| 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 | |
| 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 For 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 | static const size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0; |
| 28 | static const size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024; |
| 29 | |
| 30 | /* Bootloader Message (2-KiB) |
| 31 | * |
| 32 | * This structure describes the content of a block in flash |
| 33 | * that is used for recovery and the bootloader to talk to |
| 34 | * each other. |
| 35 | * |
| 36 | * The command field is updated by linux when it wants to |
| 37 | * reboot into recovery or to update radio or bootloader firmware. |
| 38 | * It is also updated by the bootloader when firmware update |
| 39 | * is complete (to boot into recovery for any final cleanup) |
| 40 | * |
| 41 | * The status field was used by the bootloader after the completion |
| 42 | * of an "update-radio" or "update-hboot" command, which has been |
| 43 | * deprecated since Froyo. |
| 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 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 | * We must be cautious when changing the bootloader_message struct size, |
| 80 | * because A/B-specific fields may end up with different offsets. |
| 81 | */ |
| 82 | #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) |
| 83 | static_assert(sizeof(struct bootloader_message) == 2048, |
| 84 | "struct bootloader_message size changes, which may break A/B devices"); |
| 85 | #endif |
| 86 | |
| 87 | /** |
| 88 | * The A/B-specific bootloader message structure (4-KiB). |
| 89 | * |
| 90 | * We separate A/B boot control metadata from the regular bootloader |
| 91 | * message struct and keep it here. Everything that's A/B-specific |
| 92 | * stays after struct bootloader_message, which should be managed by |
| 93 | * the A/B-bootloader or boot control HAL. |
| 94 | * |
| 95 | * The slot_suffix field is used for A/B implementations where the |
| 96 | * bootloader does not set the androidboot.ro.boot.slot_suffix kernel |
| 97 | * commandline parameter. This is used by fs_mgr to mount /system and |
| 98 | * other partitions with the slotselect flag set in fstab. A/B |
| 99 | * implementations are free to use all 32 bytes and may store private |
| 100 | * data past the first NUL-byte in this field. It is encouraged, but |
| 101 | * not mandatory, to use 'struct bootloader_control' described below. |
| 102 | * |
| 103 | * The update_channel field is used to store the Omaha update channel |
| 104 | * if update_engine is compiled with Omaha support. |
| 105 | */ |
| 106 | struct bootloader_message_ab { |
| 107 | struct bootloader_message message; |
| 108 | char slot_suffix[32]; |
| 109 | char update_channel[128]; |
| 110 | |
| 111 | /* Round up the entire struct to 4096-byte. */ |
| 112 | char reserved[1888]; |
| 113 | }; |
| 114 | |
| 115 | /** |
| 116 | * Be cautious about the struct size change, in case we put anything post |
| 117 | * bootloader_message_ab struct (b/29159185). |
| 118 | */ |
| 119 | #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) |
| 120 | static_assert(sizeof(struct bootloader_message_ab) == 4096, |
| 121 | "struct bootloader_message_ab size changes"); |
| 122 | #endif |
| 123 | |
| 124 | #define BOOT_CTRL_MAGIC 0x42414342 /* Bootloader Control AB */ |
| 125 | #define BOOT_CTRL_VERSION 1 |
| 126 | |
| 127 | struct slot_metadata { |
| 128 | /* Slot priority with 15 meaning highest priority, 1 lowest |
| 129 | * priority and 0 the slot is unbootable. */ |
| 130 | uint8_t priority : 4; |
| 131 | /* Number of times left attempting to boot this slot. */ |
| 132 | uint8_t tries_remaining : 3; |
| 133 | /* 1 if this slot has booted successfully, 0 otherwise. */ |
| 134 | uint8_t successful_boot : 1; |
| 135 | /* 1 if this slot is corrupted from a dm-verity corruption, 0 |
| 136 | * otherwise. */ |
| 137 | uint8_t verity_corrupted : 1; |
| 138 | /* Reserved for further use. */ |
| 139 | uint8_t reserved : 7; |
| 140 | } __attribute__((packed)); |
| 141 | |
| 142 | /* Bootloader Control AB |
| 143 | * |
| 144 | * This struct can be used to manage A/B metadata. It is designed to |
| 145 | * be put in the 'slot_suffix' field of the 'bootloader_message' |
| 146 | * structure described above. It is encouraged to use the |
| 147 | * 'bootloader_control' structure to store the A/B metadata, but not |
| 148 | * mandatory. |
| 149 | */ |
| 150 | struct bootloader_control { |
| 151 | /* NUL terminated active slot suffix. */ |
| 152 | char slot_suffix[4]; |
| 153 | /* Bootloader Control AB magic number (see BOOT_CTRL_MAGIC). */ |
| 154 | uint32_t magic; |
| 155 | /* Version of struct being used (see BOOT_CTRL_VERSION). */ |
| 156 | uint8_t version; |
| 157 | /* Number of slots being managed. */ |
| 158 | uint8_t nb_slot : 3; |
| 159 | /* Number of times left attempting to boot recovery. */ |
| 160 | uint8_t recovery_tries_remaining : 3; |
| 161 | /* Ensure 4-bytes alignment for slot_info field. */ |
| 162 | uint8_t reserved0[2]; |
| 163 | /* Per-slot information. Up to 4 slots. */ |
| 164 | struct slot_metadata slot_info[4]; |
| 165 | /* Reserved for further use. */ |
| 166 | uint8_t reserved1[8]; |
| 167 | /* CRC32 of all 28 bytes preceding this field (little endian |
| 168 | * format). */ |
| 169 | uint32_t crc32_le; |
| 170 | } __attribute__((packed)); |
| 171 | |
| 172 | #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) |
| 173 | static_assert(sizeof(struct bootloader_control) == |
| 174 | sizeof(((struct bootloader_message_ab *)0)->slot_suffix), |
| 175 | "struct bootloader_control has wrong size"); |
| 176 | #endif |
| 177 | |
| 178 | #ifndef __UBOOT__ |
| 179 | |
| 180 | #ifdef __cplusplus |
| 181 | |
| 182 | #include <string> |
| 183 | #include <vector> |
| 184 | |
| 185 | /* Return the block device name for the bootloader message partition and waits |
| 186 | * for the device for up to 10 seconds. In case of error returns the empty |
| 187 | * string. */ |
| 188 | std::string get_bootloader_message_blk_device(std::string* err); |
| 189 | |
| 190 | /* Read bootloader message into boot. Error message will be set in err. */ |
| 191 | bool read_bootloader_message(bootloader_message* boot, std::string* err); |
| 192 | |
| 193 | /* Read bootloader message from the specified misc device into boot. */ |
| 194 | bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device, |
| 195 | std::string* err); |
| 196 | |
| 197 | /* Write bootloader message to BCB. */ |
| 198 | bool write_bootloader_message(const bootloader_message& boot, std::string* err); |
| 199 | |
| 200 | /* Write bootloader message to the specified BCB device. */ |
| 201 | bool write_bootloader_message_to(const bootloader_message& boot, |
| 202 | const std::string& misc_blk_device, std::string* err); |
| 203 | |
| 204 | /* Write bootloader message (boots into recovery with the options) to BCB. Will |
| 205 | * set the command and recovery fields, and reset the rest. */ |
| 206 | bool write_bootloader_message(const std::vector<std::string>& options, std::string* err); |
| 207 | |
| 208 | /* Write bootloader message (boots into recovery with the options) to the specific BCB device. Will |
| 209 | * set the command and recovery fields, and reset the rest. */ |
| 210 | bool write_bootloader_message_to(const std::vector<std::string>& options, |
| 211 | const std::string& misc_blk_device, std::string* err); |
| 212 | |
| 213 | /* Update bootloader message (boots into recovery with the options) to BCB. Will |
| 214 | * only update the command and recovery fields. */ |
| 215 | bool update_bootloader_message(const std::vector<std::string>& options, std::string* err); |
| 216 | |
| 217 | /* Update bootloader message (boots into recovery with the |options|) in |boot|. Will only update |
| 218 | * the command and recovery fields. */ |
| 219 | bool update_bootloader_message_in_struct(bootloader_message* boot, |
| 220 | const std::vector<std::string>& options); |
| 221 | |
| 222 | /* Clear BCB. */ |
| 223 | bool clear_bootloader_message(std::string* err); |
| 224 | |
| 225 | /* Writes the reboot-bootloader reboot reason to the bootloader_message. */ |
| 226 | bool write_reboot_bootloader(std::string* err); |
| 227 | |
| 228 | /* Read the wipe package from BCB (from offset WIPE_PACKAGE_OFFSET_IN_MISC). */ |
| 229 | bool read_wipe_package(std::string* package_data, size_t size, std::string* err); |
| 230 | |
| 231 | /* Write the wipe package into BCB (to offset WIPE_PACKAGE_OFFSET_IN_MISC). */ |
| 232 | bool write_wipe_package(const std::string& package_data, std::string* err); |
| 233 | |
| 234 | #else |
| 235 | |
| 236 | #include <stdbool.h> |
| 237 | |
| 238 | /* C Interface. */ |
| 239 | bool write_bootloader_message(const char* options); |
| 240 | bool write_reboot_bootloader(void); |
| 241 | |
| 242 | #endif /* ifdef __cplusplus */ |
| 243 | |
| 244 | #endif /* __UBOOT__ */ |
| 245 | |
| 246 | #endif /* __ANDROID_BOOTLOADER_MESSAGE_H */ |