Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [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 _BOOTLOADER_MESSAGE_H |
| 18 | #define _BOOTLOADER_MESSAGE_H |
| 19 | |
Yabin Cui | 9b15ba8 | 2016-06-30 14:36:59 -0700 | [diff] [blame] | 20 | #include <assert.h> |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 21 | #include <stddef.h> |
Yabin Cui | 9b15ba8 | 2016-06-30 14:36:59 -0700 | [diff] [blame] | 22 | #include <stdint.h> |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 23 | |
| 24 | // Spaces used by misc partition are as below: |
Yabin Cui | 9b15ba8 | 2016-06-30 14:36:59 -0700 | [diff] [blame] | 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) |
David Anderson | 4ff4cbd | 2019-11-04 13:09:38 -0800 | [diff] [blame] | 28 | // 16K - 32K Used by uncrypt and recovery to store wipe_package for A/B devices |
| 29 | // 32K - 64K System space, used for miscellanious AOSP features. See below. |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 30 | // Note that these offsets are admitted by bootloader,recovery and uncrypt, so they |
| 31 | // are not configurable without changing all of them. |
Tao Bao | 7ae0169 | 2019-05-16 14:42:42 -0700 | [diff] [blame] | 32 | constexpr size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0; |
| 33 | constexpr size_t VENDOR_SPACE_OFFSET_IN_MISC = 2 * 1024; |
| 34 | constexpr size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024; |
David Anderson | 4ff4cbd | 2019-11-04 13:09:38 -0800 | [diff] [blame] | 35 | constexpr size_t SYSTEM_SPACE_OFFSET_IN_MISC = 32 * 1024; |
| 36 | constexpr size_t SYSTEM_SPACE_SIZE_IN_MISC = 32 * 1024; |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 37 | |
Yabin Cui | 9b15ba8 | 2016-06-30 14:36:59 -0700 | [diff] [blame] | 38 | /* Bootloader Message (2-KiB) |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 39 | * |
| 40 | * This structure describes the content of a block in flash |
| 41 | * that is used for recovery and the bootloader to talk to |
| 42 | * each other. |
| 43 | * |
| 44 | * The command field is updated by linux when it wants to |
| 45 | * reboot into recovery or to update radio or bootloader firmware. |
| 46 | * It is also updated by the bootloader when firmware update |
| 47 | * is complete (to boot into recovery for any final cleanup) |
| 48 | * |
Tao Bao | c033639 | 2016-12-13 22:29:49 -0800 | [diff] [blame] | 49 | * The status field was used by the bootloader after the completion |
| 50 | * of an "update-radio" or "update-hboot" command, which has been |
| 51 | * deprecated since Froyo. |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 52 | * |
| 53 | * The recovery field is only written by linux and used |
| 54 | * for the system to send a message to recovery or the |
| 55 | * other way around. |
| 56 | * |
| 57 | * The stage field is written by packages which restart themselves |
| 58 | * multiple times, so that the UI can reflect which invocation of the |
| 59 | * package it is. If the value is of the format "#/#" (eg, "1/3"), |
| 60 | * the UI will add a simple indicator of that status. |
| 61 | * |
Yabin Cui | 9b15ba8 | 2016-06-30 14:36:59 -0700 | [diff] [blame] | 62 | * We used to have slot_suffix field for A/B boot control metadata in |
| 63 | * this struct, which gets unintentionally cleared by recovery or |
| 64 | * uncrypt. Move it into struct bootloader_message_ab to avoid the |
| 65 | * issue. |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 66 | */ |
| 67 | struct bootloader_message { |
| 68 | char command[32]; |
| 69 | char status[32]; |
| 70 | char recovery[768]; |
| 71 | |
| 72 | // The 'recovery' field used to be 1024 bytes. It has only ever |
| 73 | // been used to store the recovery command line, so 768 bytes |
| 74 | // should be plenty. We carve off the last 256 bytes to store the |
| 75 | // stage string (for multistage packages) and possible future |
| 76 | // expansion. |
| 77 | char stage[32]; |
Yabin Cui | 9b15ba8 | 2016-06-30 14:36:59 -0700 | [diff] [blame] | 78 | |
| 79 | // The 'reserved' field used to be 224 bytes when it was initially |
| 80 | // carved off from the 1024-byte recovery field. Bump it up to |
| 81 | // 1184-byte so that the entire bootloader_message struct rounds up |
| 82 | // to 2048-byte. |
| 83 | char reserved[1184]; |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
David Anderson | cf8427a | 2019-11-04 14:08:11 -0800 | [diff] [blame] | 86 | // Holds Virtual A/B merge status information. Current version is 1. New fields |
| 87 | // must be added to the end. |
| 88 | struct misc_virtual_ab_message { |
| 89 | uint8_t version; |
David Anderson | 682d2a5 | 2019-11-20 15:47:07 -0800 | [diff] [blame] | 90 | uint32_t magic; |
David Anderson | cf8427a | 2019-11-04 14:08:11 -0800 | [diff] [blame] | 91 | uint8_t merge_status; // IBootControl 1.1, MergeStatus enum. |
| 92 | uint8_t source_slot; // Slot number when merge_status was written. |
David Anderson | 682d2a5 | 2019-11-20 15:47:07 -0800 | [diff] [blame] | 93 | uint8_t reserved[57]; |
David Anderson | cf8427a | 2019-11-04 14:08:11 -0800 | [diff] [blame] | 94 | } __attribute__((packed)); |
| 95 | |
David Anderson | 682d2a5 | 2019-11-20 15:47:07 -0800 | [diff] [blame] | 96 | #define MISC_VIRTUAL_AB_MESSAGE_VERSION 2 |
| 97 | #define MISC_VIRTUAL_AB_MAGIC_HEADER 0x56740AB0 |
David Anderson | cf8427a | 2019-11-04 14:08:11 -0800 | [diff] [blame] | 98 | |
| 99 | #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) |
| 100 | static_assert(sizeof(struct misc_virtual_ab_message) == 64, |
| 101 | "struct misc_virtual_ab_message has wrong size"); |
| 102 | #endif |
| 103 | |
| 104 | // This struct is not meant to be used directly, rather, it is to make |
| 105 | // computation of offsets easier. New fields must be added to the end. |
| 106 | struct misc_system_space_layout { |
| 107 | misc_virtual_ab_message virtual_ab_message; |
| 108 | } __attribute__((packed)); |
| 109 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 110 | #ifdef __cplusplus |
| 111 | |
| 112 | #include <string> |
| 113 | #include <vector> |
| 114 | |
Tianjie Xu | 3d57c84 | 2019-11-09 22:07:20 -0800 | [diff] [blame] | 115 | // Gets the block device name of /misc partition. |
| 116 | std::string get_misc_blk_device(std::string* err); |
Alex Deymo | fb00d82 | 2016-11-08 15:46:07 -0800 | [diff] [blame] | 117 | // Return the block device name for the bootloader message partition and waits |
| 118 | // for the device for up to 10 seconds. In case of error returns the empty |
| 119 | // string. |
| 120 | std::string get_bootloader_message_blk_device(std::string* err); |
| 121 | |
Tianjie Xu | 3d57c84 | 2019-11-09 22:07:20 -0800 | [diff] [blame] | 122 | // Writes |size| bytes of data from buffer |p| to |misc_blk_device| at |offset|. If the write fails, |
| 123 | // sets the error message in |err|. |
| 124 | bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device, |
| 125 | size_t offset, std::string* err); |
| 126 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 127 | // Read bootloader message into boot. Error message will be set in err. |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 128 | bool read_bootloader_message(bootloader_message* boot, std::string* err); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 129 | |
| 130 | // Read bootloader message from the specified misc device into boot. |
| 131 | bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device, |
| 132 | std::string* err); |
| 133 | |
| 134 | // Write bootloader message to BCB. |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 135 | bool write_bootloader_message(const bootloader_message& boot, std::string* err); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 136 | |
| 137 | // Write bootloader message to the specified BCB device. |
| 138 | bool write_bootloader_message_to(const bootloader_message& boot, |
| 139 | const std::string& misc_blk_device, std::string* err); |
| 140 | |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 141 | // Write bootloader message (boots into recovery with the options) to BCB. Will |
| 142 | // set the command and recovery fields, and reset the rest. |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 143 | bool write_bootloader_message(const std::vector<std::string>& options, std::string* err); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 144 | |
Yifan Hong | c784ce5 | 2019-05-01 13:13:58 -0700 | [diff] [blame] | 145 | // Write bootloader message (boots into recovery with the options) to the specific BCB device. Will |
| 146 | // set the command and recovery fields, and reset the rest. |
| 147 | bool write_bootloader_message_to(const std::vector<std::string>& options, |
| 148 | const std::string& misc_blk_device, std::string* err); |
| 149 | |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 150 | // Update bootloader message (boots into recovery with the options) to BCB. Will |
| 151 | // only update the command and recovery fields. |
| 152 | bool update_bootloader_message(const std::vector<std::string>& options, std::string* err); |
| 153 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 154 | // Update bootloader message (boots into recovery with the |options|) in |boot|. Will only update |
| 155 | // the command and recovery fields. |
| 156 | bool update_bootloader_message_in_struct(bootloader_message* boot, |
| 157 | const std::vector<std::string>& options); |
| 158 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 159 | // Clear BCB. |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 160 | bool clear_bootloader_message(std::string* err); |
| 161 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 162 | // Writes the reboot-bootloader reboot reason to the bootloader_message. |
| 163 | bool write_reboot_bootloader(std::string* err); |
| 164 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 165 | // Read the wipe package from BCB (from offset WIPE_PACKAGE_OFFSET_IN_MISC). |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 166 | bool read_wipe_package(std::string* package_data, size_t size, std::string* err); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 167 | |
| 168 | // Write the wipe package into BCB (to offset WIPE_PACKAGE_OFFSET_IN_MISC). |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 169 | bool write_wipe_package(const std::string& package_data, std::string* err); |
| 170 | |
David Anderson | cf8427a | 2019-11-04 14:08:11 -0800 | [diff] [blame] | 171 | // Read or write the Virtual A/B message from system space in /misc. |
| 172 | bool ReadMiscVirtualAbMessage(misc_virtual_ab_message* message, std::string* err); |
| 173 | bool WriteMiscVirtualAbMessage(const misc_virtual_ab_message& message, std::string* err); |
| 174 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 175 | #else |
| 176 | |
| 177 | #include <stdbool.h> |
| 178 | |
| 179 | // C Interface. |
| 180 | bool write_bootloader_message(const char* options); |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 181 | bool write_reboot_bootloader(void); |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 182 | |
| 183 | #endif // ifdef __cplusplus |
| 184 | |
| 185 | #endif // _BOOTLOADER_MESSAGE_H |