The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 _RECOVERY_BOOTLOADER_H |
| 18 | #define _RECOVERY_BOOTLOADER_H |
| 19 | |
Tao Bao | 9fb2b59 | 2016-06-09 12:10:54 -0700 | [diff] [blame] | 20 | #include <assert.h> |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 21 | #include <stddef.h> |
| 22 | |
| 23 | // Spaces used by misc partition are as below: |
Yabin Cui | bf049bf | 2016-06-21 11:00:44 -0700 | [diff] [blame] | 24 | // 0 - 2K For bootloader_message |
| 25 | // 2K - 16K Used by Vendor's bootloader (the 2K - 4K range may be optionally used |
| 26 | // as bootloader_message_ab struct) |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 27 | // 16K - 64K Used by uncrypt and recovery to store wipe_package for A/B devices |
| 28 | // Note that these offsets are admitted by bootloader,recovery and uncrypt, so they |
| 29 | // are not configurable without changing all of them. |
| 30 | static const size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0; |
| 31 | static const size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024; |
Tao Bao | 9fb2b59 | 2016-06-09 12:10:54 -0700 | [diff] [blame] | 32 | |
| 33 | /* Bootloader Message (2-KiB) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | * |
| 35 | * This structure describes the content of a block in flash |
| 36 | * that is used for recovery and the bootloader to talk to |
| 37 | * each other. |
| 38 | * |
| 39 | * The command field is updated by linux when it wants to |
| 40 | * reboot into recovery or to update radio or bootloader firmware. |
| 41 | * It is also updated by the bootloader when firmware update |
| 42 | * is complete (to boot into recovery for any final cleanup) |
| 43 | * |
| 44 | * The status field is written by the bootloader after the |
| 45 | * completion of an "update-radio" or "update-hboot" command. |
| 46 | * |
| 47 | * The recovery field is only written by linux and used |
| 48 | * for the system to send a message to recovery or the |
| 49 | * other way around. |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 50 | * |
| 51 | * The stage field is written by packages which restart themselves |
| 52 | * multiple times, so that the UI can reflect which invocation of the |
| 53 | * package it is. If the value is of the format "#/#" (eg, "1/3"), |
| 54 | * the UI will add a simple indicator of that status. |
David Zeuthen | d85ae79 | 2015-09-02 15:49:58 -0400 | [diff] [blame] | 55 | * |
Tao Bao | 9fb2b59 | 2016-06-09 12:10:54 -0700 | [diff] [blame] | 56 | * We used to have slot_suffix field for A/B boot control metadata in |
| 57 | * this struct, which gets unintentionally cleared by recovery or |
| 58 | * uncrypt. Move it into struct bootloader_message_ab to avoid the |
| 59 | * issue. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 60 | */ |
| 61 | struct bootloader_message { |
| 62 | char command[32]; |
| 63 | char status[32]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 64 | char recovery[768]; |
| 65 | |
| 66 | // The 'recovery' field used to be 1024 bytes. It has only ever |
| 67 | // been used to store the recovery command line, so 768 bytes |
| 68 | // should be plenty. We carve off the last 256 bytes to store the |
| 69 | // stage string (for multistage packages) and possible future |
| 70 | // expansion. |
| 71 | char stage[32]; |
Tao Bao | 9fb2b59 | 2016-06-09 12:10:54 -0700 | [diff] [blame] | 72 | |
| 73 | // The 'reserved' field used to be 224 bytes when it was initially |
| 74 | // carved off from the 1024-byte recovery field. Bump it up to |
| 75 | // 1184-byte so that the entire bootloader_message struct rounds up |
| 76 | // to 2048-byte. |
| 77 | char reserved[1184]; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
Tao Bao | 9fb2b59 | 2016-06-09 12:10:54 -0700 | [diff] [blame] | 80 | /** |
| 81 | * We must be cautious when changing the bootloader_message struct size, |
| 82 | * because A/B-specific fields may end up with different offsets. |
| 83 | */ |
| 84 | #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) |
| 85 | static_assert(sizeof(struct bootloader_message) == 2048, |
| 86 | "struct bootloader_message size changes, which may break A/B devices"); |
| 87 | #endif |
| 88 | |
| 89 | /** |
| 90 | * The A/B-specific bootloader message structure (4-KiB). |
| 91 | * |
| 92 | * We separate A/B boot control metadata from the regular bootloader |
| 93 | * message struct and keep it here. Everything that's A/B-specific |
| 94 | * stays after struct bootloader_message, which should be managed by |
| 95 | * the A/B-bootloader or boot control HAL. |
| 96 | * |
| 97 | * The slot_suffix field is used for A/B implementations where the |
| 98 | * bootloader does not set the androidboot.ro.boot.slot_suffix kernel |
| 99 | * commandline parameter. This is used by fs_mgr to mount /system and |
| 100 | * other partitions with the slotselect flag set in fstab. A/B |
| 101 | * implementations are free to use all 32 bytes and may store private |
| 102 | * data past the first NUL-byte in this field. It is encouraged, but |
| 103 | * not mandatory, to use 'struct bootloader_control' described below. |
| 104 | */ |
| 105 | struct bootloader_message_ab { |
| 106 | struct bootloader_message message; |
| 107 | char slot_suffix[32]; |
| 108 | |
| 109 | // Round up the entire struct to 4096-byte. |
| 110 | char reserved[2016]; |
| 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * Be cautious about the struct size change, in case we put anything post |
| 115 | * bootloader_message_ab struct (b/29159185). |
| 116 | */ |
| 117 | #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) |
| 118 | static_assert(sizeof(struct bootloader_message_ab) == 4096, |
| 119 | "struct bootloader_message_ab size changes"); |
| 120 | #endif |
| 121 | |
Jeremy Compostella | b3bf958 | 2016-04-01 17:16:13 +0200 | [diff] [blame] | 122 | #define BOOT_CTRL_MAGIC 0x42414342 /* Bootloader Control AB */ |
| 123 | #define BOOT_CTRL_VERSION 1 |
| 124 | |
| 125 | struct slot_metadata { |
| 126 | // Slot priority with 15 meaning highest priority, 1 lowest |
| 127 | // priority and 0 the slot is unbootable. |
| 128 | uint8_t priority : 4; |
| 129 | // Number of times left attempting to boot this slot. |
| 130 | uint8_t tries_remaining : 3; |
| 131 | // 1 if this slot has booted successfully, 0 otherwise. |
| 132 | uint8_t successful_boot : 1; |
Jeremy Compostella | e77a68f | 2016-05-23 13:10:23 +0200 | [diff] [blame] | 133 | // 1 if this slot is corrupted from a dm-verity corruption, 0 |
| 134 | // otherwise. |
| 135 | uint8_t verity_corrupted : 1; |
Jeremy Compostella | b3bf958 | 2016-04-01 17:16:13 +0200 | [diff] [blame] | 136 | // Reserved for further use. |
Jeremy Compostella | e77a68f | 2016-05-23 13:10:23 +0200 | [diff] [blame] | 137 | uint8_t reserved : 7; |
Jeremy Compostella | b3bf958 | 2016-04-01 17:16:13 +0200 | [diff] [blame] | 138 | } __attribute__((packed)); |
| 139 | |
| 140 | /* Bootloader Control AB |
| 141 | * |
| 142 | * This struct can be used to manage A/B metadata. It is designed to |
| 143 | * be put in the 'slot_suffix' field of the 'bootloader_message' |
| 144 | * structure described above. It is encouraged to use the |
| 145 | * 'bootloader_control' structure to store the A/B metadata, but not |
| 146 | * mandatory. |
| 147 | */ |
| 148 | struct bootloader_control { |
| 149 | // NUL terminated active slot suffix. |
| 150 | char slot_suffix[4]; |
| 151 | // Bootloader Control AB magic number (see BOOT_CTRL_MAGIC). |
| 152 | uint32_t magic; |
| 153 | // Version of struct being used (see BOOT_CTRL_VERSION). |
| 154 | uint8_t version; |
| 155 | // Number of slots being managed. |
| 156 | uint8_t nb_slot : 3; |
| 157 | // Number of times left attempting to boot recovery. |
| 158 | uint8_t recovery_tries_remaining : 3; |
| 159 | // Ensure 4-bytes alignment for slot_info field. |
| 160 | uint8_t reserved0[2]; |
| 161 | // Per-slot information. Up to 4 slots. |
| 162 | struct slot_metadata slot_info[4]; |
| 163 | // Reserved for further use. |
| 164 | uint8_t reserved1[8]; |
| 165 | // CRC32 of all 28 bytes preceding this field (little endian |
| 166 | // format). |
| 167 | uint32_t crc32_le; |
| 168 | } __attribute__((packed)); |
| 169 | |
Tao Bao | 9fb2b59 | 2016-06-09 12:10:54 -0700 | [diff] [blame] | 170 | #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) |
| 171 | static_assert(sizeof(struct bootloader_control) == |
| 172 | sizeof(((struct bootloader_message_ab *)0)->slot_suffix), |
| 173 | "struct bootloader_control has wrong size"); |
Jeremy Compostella | b3bf958 | 2016-04-01 17:16:13 +0200 | [diff] [blame] | 174 | #endif |
| 175 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 176 | /* Read and write the bootloader command from the "misc" partition. |
| 177 | * These return zero on success. |
| 178 | */ |
| 179 | int get_bootloader_message(struct bootloader_message *out); |
| 180 | int set_bootloader_message(const struct bootloader_message *in); |
| 181 | |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 182 | #ifdef __cplusplus |
| 183 | |
| 184 | #include <string> |
| 185 | |
| 186 | bool read_wipe_package(size_t size, std::string* out); |
| 187 | #endif |
| 188 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 189 | #endif |