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