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