Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #include <bootloader_message/bootloader_message.h> |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <string> |
Tao Bao | 35e0f6d | 2019-05-16 14:42:42 -0700 | [diff] [blame] | 24 | #include <string_view> |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
| 27 | #include <android-base/file.h> |
| 28 | #include <android-base/properties.h> |
| 29 | #include <android-base/stringprintf.h> |
| 30 | #include <android-base/unique_fd.h> |
Tom Cherry | 772c93c | 2018-12-04 09:37:39 -0800 | [diff] [blame] | 31 | #include <fstab/fstab.h> |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 32 | |
Tom Cherry | 72a114a | 2019-01-30 15:59:53 -0800 | [diff] [blame] | 33 | using android::fs_mgr::Fstab; |
| 34 | using android::fs_mgr::ReadDefaultFstab; |
| 35 | |
Tao Bao | 35e0f6d | 2019-05-16 14:42:42 -0700 | [diff] [blame] | 36 | static std::string g_misc_device_for_test; |
| 37 | |
| 38 | // Exposed for test purpose. |
| 39 | void SetMiscBlockDeviceForTest(std::string_view misc_device) { |
| 40 | g_misc_device_for_test = misc_device; |
| 41 | } |
| 42 | |
Alessandro Astone | 6752e19 | 2019-01-11 18:46:44 +0100 | [diff] [blame] | 43 | // Spaces used by misc partition are as below: |
| 44 | // 0 - 2K For bootloader_message |
| 45 | // 2K - 16K Used by Vendor's bootloader (the 2K - 4K range may be optionally used |
| 46 | // as bootloader_message_ab struct) |
| 47 | // 16K - 64K Used by uncrypt and recovery to store wipe_package for A/B devices |
| 48 | // Note that these offsets are admitted by bootloader,recovery and uncrypt, so they |
| 49 | // are not configurable without changing all of them. |
| 50 | constexpr size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = BOARD_RECOVERY_BLDRMSG_OFFSET; |
| 51 | constexpr size_t VENDOR_SPACE_OFFSET_IN_MISC = 2 * 1024 + BOOTLOADER_MESSAGE_OFFSET_IN_MISC; |
| 52 | constexpr size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024 + BOOTLOADER_MESSAGE_OFFSET_IN_MISC; |
| 53 | |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 54 | static std::string get_misc_blk_device(std::string* err) { |
Tao Bao | 35e0f6d | 2019-05-16 14:42:42 -0700 | [diff] [blame] | 55 | if (!g_misc_device_for_test.empty()) { |
| 56 | return g_misc_device_for_test; |
| 57 | } |
Tom Cherry | 772c93c | 2018-12-04 09:37:39 -0800 | [diff] [blame] | 58 | Fstab fstab; |
| 59 | if (!ReadDefaultFstab(&fstab)) { |
Bowgo Tsai | d13b6cf | 2017-03-10 16:00:40 +0800 | [diff] [blame] | 60 | *err = "failed to read default fstab"; |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 61 | return ""; |
| 62 | } |
Tom Cherry | 772c93c | 2018-12-04 09:37:39 -0800 | [diff] [blame] | 63 | for (const auto& entry : fstab) { |
| 64 | if (entry.mount_point == "/misc") { |
| 65 | return entry.blk_device; |
| 66 | } |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 67 | } |
Tom Cherry | 772c93c | 2018-12-04 09:37:39 -0800 | [diff] [blame] | 68 | |
| 69 | *err = "failed to find /misc partition"; |
| 70 | return ""; |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // In recovery mode, recovery can get started and try to access the misc |
| 74 | // device before the kernel has actually created it. |
| 75 | static bool wait_for_device(const std::string& blk_device, std::string* err) { |
| 76 | int tries = 0; |
| 77 | int ret; |
| 78 | err->clear(); |
| 79 | do { |
| 80 | ++tries; |
| 81 | struct stat buf; |
| 82 | ret = stat(blk_device.c_str(), &buf); |
| 83 | if (ret == -1) { |
| 84 | *err += android::base::StringPrintf("failed to stat %s try %d: %s\n", |
| 85 | blk_device.c_str(), tries, strerror(errno)); |
| 86 | sleep(1); |
| 87 | } |
| 88 | } while (ret && tries < 10); |
| 89 | |
| 90 | if (ret) { |
| 91 | *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str()); |
| 92 | } |
| 93 | return ret == 0; |
| 94 | } |
| 95 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 96 | static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device, |
| 97 | size_t offset, std::string* err) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 98 | if (!wait_for_device(misc_blk_device, err)) { |
| 99 | return false; |
| 100 | } |
Yabin Cui | 0d5b859 | 2016-07-06 11:47:23 -0700 | [diff] [blame] | 101 | android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY)); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 102 | if (fd == -1) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 103 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
| 104 | strerror(errno)); |
| 105 | return false; |
| 106 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 107 | if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 108 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
| 109 | strerror(errno)); |
| 110 | return false; |
| 111 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 112 | if (!android::base::ReadFully(fd, p, size)) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 113 | *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(), |
| 114 | strerror(errno)); |
| 115 | return false; |
| 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 120 | static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device, |
| 121 | size_t offset, std::string* err) { |
| 122 | android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY)); |
| 123 | if (fd == -1) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 124 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
| 125 | strerror(errno)); |
| 126 | return false; |
| 127 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 128 | if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 129 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
| 130 | strerror(errno)); |
| 131 | return false; |
| 132 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 133 | if (!android::base::WriteFully(fd, p, size)) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 134 | *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(), |
| 135 | strerror(errno)); |
| 136 | return false; |
| 137 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 138 | if (fsync(fd) == -1) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 139 | *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(), |
| 140 | strerror(errno)); |
| 141 | return false; |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
Alex Deymo | fb00d82 | 2016-11-08 15:46:07 -0800 | [diff] [blame] | 146 | std::string get_bootloader_message_blk_device(std::string* err) { |
| 147 | std::string misc_blk_device = get_misc_blk_device(err); |
| 148 | if (misc_blk_device.empty()) return ""; |
| 149 | if (!wait_for_device(misc_blk_device, err)) return ""; |
| 150 | return misc_blk_device; |
| 151 | } |
| 152 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 153 | bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device, |
| 154 | std::string* err) { |
| 155 | return read_misc_partition(boot, sizeof(*boot), misc_blk_device, |
| 156 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
| 157 | } |
| 158 | |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 159 | bool read_bootloader_message(bootloader_message* boot, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 160 | std::string misc_blk_device = get_misc_blk_device(err); |
| 161 | if (misc_blk_device.empty()) { |
| 162 | return false; |
| 163 | } |
| 164 | return read_bootloader_message_from(boot, misc_blk_device, err); |
| 165 | } |
| 166 | |
| 167 | bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device, |
| 168 | std::string* err) { |
| 169 | return write_misc_partition(&boot, sizeof(boot), misc_blk_device, |
| 170 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | bool write_bootloader_message(const bootloader_message& boot, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 174 | std::string misc_blk_device = get_misc_blk_device(err); |
| 175 | if (misc_blk_device.empty()) { |
| 176 | return false; |
| 177 | } |
| 178 | return write_bootloader_message_to(boot, misc_blk_device, err); |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | bool clear_bootloader_message(std::string* err) { |
| 182 | bootloader_message boot = {}; |
Alessandro Astone | 5087d01 | 2019-05-04 20:06:48 +0200 | [diff] [blame] | 183 | if (BOOTLOADER_MESSAGE_OFFSET_IN_MISC < sizeof(bootloader_message)) { |
| 184 | std::string misc_blk_device = get_misc_blk_device(err); |
| 185 | if (misc_blk_device.empty()) return false; |
| 186 | return write_misc_partition(&boot, sizeof(boot), misc_blk_device, 0 /* offset */, err); |
| 187 | } |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 188 | return write_bootloader_message(boot, err); |
| 189 | } |
| 190 | |
| 191 | bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) { |
Tao Bao | 26d5ae7 | 2016-12-13 01:06:24 +0000 | [diff] [blame] | 192 | bootloader_message boot = {}; |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 193 | update_bootloader_message_in_struct(&boot, options); |
| 194 | |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 195 | return write_bootloader_message(boot, err); |
| 196 | } |
| 197 | |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 198 | bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) { |
| 199 | bootloader_message boot; |
| 200 | if (!read_bootloader_message(&boot, err)) { |
| 201 | return false; |
| 202 | } |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 203 | update_bootloader_message_in_struct(&boot, options); |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 204 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 205 | return write_bootloader_message(boot, err); |
| 206 | } |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 207 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 208 | bool update_bootloader_message_in_struct(bootloader_message* boot, |
| 209 | const std::vector<std::string>& options) { |
| 210 | if (!boot) return false; |
| 211 | // Replace the command & recovery fields. |
| 212 | memset(boot->command, 0, sizeof(boot->command)); |
| 213 | memset(boot->recovery, 0, sizeof(boot->recovery)); |
| 214 | |
| 215 | strlcpy(boot->command, "boot-recovery", sizeof(boot->command)); |
| 216 | strlcpy(boot->recovery, "recovery\n", sizeof(boot->recovery)); |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 217 | for (const auto& s : options) { |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 218 | strlcat(boot->recovery, s.c_str(), sizeof(boot->recovery)); |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 219 | if (s.back() != '\n') { |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 220 | strlcat(boot->recovery, "\n", sizeof(boot->recovery)); |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 221 | } |
| 222 | } |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 223 | return true; |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 226 | bool write_reboot_bootloader(std::string* err) { |
| 227 | bootloader_message boot; |
| 228 | if (!read_bootloader_message(&boot, err)) { |
| 229 | return false; |
| 230 | } |
| 231 | if (boot.command[0] != '\0') { |
| 232 | *err = "Bootloader command pending."; |
| 233 | return false; |
| 234 | } |
| 235 | strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command)); |
| 236 | return write_bootloader_message(boot, err); |
| 237 | } |
| 238 | |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 239 | 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] | 240 | std::string misc_blk_device = get_misc_blk_device(err); |
| 241 | if (misc_blk_device.empty()) { |
| 242 | return false; |
| 243 | } |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 244 | package_data->resize(size); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 245 | return read_misc_partition(&(*package_data)[0], size, misc_blk_device, |
| 246 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | bool write_wipe_package(const std::string& package_data, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 250 | std::string misc_blk_device = get_misc_blk_device(err); |
| 251 | if (misc_blk_device.empty()) { |
| 252 | return false; |
| 253 | } |
| 254 | return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device, |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 255 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
| 256 | } |
| 257 | |
Tao Bao | 35e0f6d | 2019-05-16 14:42:42 -0700 | [diff] [blame] | 258 | static bool OffsetAndSizeInVendorSpace(size_t offset, size_t size) { |
| 259 | auto total_size = WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC; |
| 260 | return size <= total_size && offset <= total_size - size; |
| 261 | } |
| 262 | |
| 263 | bool ReadMiscPartitionVendorSpace(void* data, size_t size, size_t offset, std::string* err) { |
| 264 | if (!OffsetAndSizeInVendorSpace(offset, size)) { |
| 265 | *err = android::base::StringPrintf("Out of bound read (offset %zu size %zu)", offset, size); |
| 266 | return false; |
| 267 | } |
| 268 | auto misc_blk_device = get_misc_blk_device(err); |
| 269 | if (misc_blk_device.empty()) { |
| 270 | return false; |
| 271 | } |
| 272 | return read_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset, |
| 273 | err); |
| 274 | } |
| 275 | |
| 276 | bool WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset, std::string* err) { |
| 277 | if (!OffsetAndSizeInVendorSpace(offset, size)) { |
| 278 | *err = android::base::StringPrintf("Out of bound write (offset %zu size %zu)", offset, size); |
| 279 | return false; |
| 280 | } |
| 281 | auto misc_blk_device = get_misc_blk_device(err); |
| 282 | if (misc_blk_device.empty()) { |
| 283 | return false; |
| 284 | } |
| 285 | return write_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset, |
| 286 | err); |
| 287 | } |
| 288 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 289 | extern "C" bool write_reboot_bootloader(void) { |
| 290 | std::string err; |
| 291 | return write_reboot_bootloader(&err); |
| 292 | } |
| 293 | |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 294 | extern "C" bool write_bootloader_message(const char* options) { |
| 295 | std::string err; |
| 296 | return write_bootloader_message({options}, &err); |
| 297 | } |