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