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