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