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