Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 <ctype.h> |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 18 | #include <dirent.h> |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Tao Bao | 0bbc764 | 2017-03-29 23:57:47 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 22 | #include <linux/fs.h> |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 23 | #include <pthread.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 29 | #include <sys/stat.h> |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <sys/wait.h> |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 32 | #include <time.h> |
| 33 | #include <unistd.h> |
| 34 | |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 35 | #include <functional> |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 36 | #include <limits> |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 37 | #include <memory> |
| 38 | #include <string> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 39 | #include <unordered_map> |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 40 | #include <vector> |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 41 | |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 42 | #include <android-base/file.h> |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 43 | #include <android-base/logging.h> |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 44 | #include <android-base/parseint.h> |
| 45 | #include <android-base/strings.h> |
Elliott Hughes | bcabd09 | 2016-03-22 20:19:22 -0700 | [diff] [blame] | 46 | #include <android-base/unique_fd.h> |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 47 | #include <applypatch/applypatch.h> |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 48 | #include <brotli/decode.h> |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 49 | #include <fec/io.h> |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 50 | #include <openssl/sha.h> |
Tianjie Xu | a946b9e | 2017-03-21 16:24:57 -0700 | [diff] [blame] | 51 | #include <private/android_filesystem_config.h> |
Tianjie Xu | 69ffa15 | 2018-08-01 16:40:00 -0700 | [diff] [blame] | 52 | #include <verity/hash_tree_builder.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 53 | #include <ziparchive/zip_archive.h> |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 54 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 55 | #include "edify/expr.h" |
Yifan Hong | 63f5260 | 2019-01-11 13:52:33 -0800 | [diff] [blame] | 56 | #include "otautil/dirutil.h" |
Tao Bao | 1fc5bf3 | 2017-10-06 07:43:41 -0700 | [diff] [blame] | 57 | #include "otautil/error_code.h" |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 58 | #include "otautil/paths.h" |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 59 | #include "otautil/print_sha1.h" |
| 60 | #include "otautil/rangeset.h" |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 61 | #include "private/commands.h" |
Tao Bao | 8f23757 | 2017-03-26 13:36:49 -0700 | [diff] [blame] | 62 | #include "updater/install.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 63 | #include "updater/updater.h" |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 64 | |
Sami Tolvanen | e82fa18 | 2015-06-10 15:58:12 +0000 | [diff] [blame] | 65 | // Set this to 0 to interpret 'erase' transfers to mean do a |
| 66 | // BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret |
| 67 | // erase to mean fill the region with zeroes. |
| 68 | #define DEBUG_ERASE 0 |
| 69 | |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 70 | static constexpr size_t BLOCKSIZE = 4096; |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 71 | static constexpr mode_t STASH_DIRECTORY_MODE = 0700; |
| 72 | static constexpr mode_t STASH_FILE_MODE = 0600; |
Yifan Hong | 8ff84d7 | 2018-12-19 16:21:55 -0800 | [diff] [blame] | 73 | static constexpr mode_t MARKER_DIRECTORY_MODE = 0700; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 74 | |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 75 | static CauseCode failure_type = kNoCause; |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 76 | static bool is_retry = false; |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 77 | static std::unordered_map<std::string, RangeSet> stash_map; |
Tianjie Xu | 7eca97e | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 78 | |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 79 | static void DeleteLastCommandFile() { |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 80 | const std::string& last_command_file = Paths::Get().last_command_file(); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 81 | if (unlink(last_command_file.c_str()) == -1 && errno != ENOENT) { |
| 82 | PLOG(ERROR) << "Failed to unlink: " << last_command_file; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Parse the last command index of the last update and save the result to |last_command_index|. |
| 87 | // Return true if we successfully read the index. |
Tao Bao | 26efb0a | 2018-05-21 14:59:55 -0700 | [diff] [blame] | 88 | static bool ParseLastCommandFile(size_t* last_command_index) { |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 89 | const std::string& last_command_file = Paths::Get().last_command_file(); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 90 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(last_command_file.c_str(), O_RDONLY))); |
| 91 | if (fd == -1) { |
| 92 | if (errno != ENOENT) { |
| 93 | PLOG(ERROR) << "Failed to open " << last_command_file; |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | LOG(INFO) << last_command_file << " doesn't exist."; |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | // Now that the last_command file exists, parse the last command index of previous update. |
| 102 | std::string content; |
| 103 | if (!android::base::ReadFdToString(fd.get(), &content)) { |
| 104 | LOG(ERROR) << "Failed to read: " << last_command_file; |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | std::vector<std::string> lines = android::base::Split(android::base::Trim(content), "\n"); |
| 109 | if (lines.size() != 2) { |
| 110 | LOG(ERROR) << "Unexpected line counts in last command file: " << content; |
| 111 | return false; |
| 112 | } |
| 113 | |
Tom Cherry | 04e4afb | 2018-10-05 14:37:13 -0700 | [diff] [blame] | 114 | if (!android::base::ParseUint(lines[0], last_command_index)) { |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 115 | LOG(ERROR) << "Failed to parse integer in: " << lines[0]; |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 122 | static bool FsyncDir(const std::string& dirname) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 123 | android::base::unique_fd dfd(TEMP_FAILURE_RETRY(open(dirname.c_str(), O_RDONLY | O_DIRECTORY))); |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 124 | if (dfd == -1) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 125 | failure_type = errno == EIO ? kEioFailure : kFileOpenFailure; |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 126 | PLOG(ERROR) << "Failed to open " << dirname; |
| 127 | return false; |
| 128 | } |
| 129 | if (fsync(dfd) == -1) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 130 | failure_type = errno == EIO ? kEioFailure : kFsyncFailure; |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 131 | PLOG(ERROR) << "Failed to fsync " << dirname; |
| 132 | return false; |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
Tianjie Xu | c2b2bb5 | 2018-05-15 15:09:59 -0700 | [diff] [blame] | 137 | // Update the last executed command index in the last_command_file. |
Tao Bao | 26efb0a | 2018-05-21 14:59:55 -0700 | [diff] [blame] | 138 | static bool UpdateLastCommandIndex(size_t command_index, const std::string& command_string) { |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 139 | const std::string& last_command_file = Paths::Get().last_command_file(); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 140 | std::string last_command_tmp = last_command_file + ".tmp"; |
| 141 | std::string content = std::to_string(command_index) + "\n" + command_string; |
| 142 | android::base::unique_fd wfd( |
| 143 | TEMP_FAILURE_RETRY(open(last_command_tmp.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0660))); |
| 144 | if (wfd == -1 || !android::base::WriteStringToFd(content, wfd)) { |
| 145 | PLOG(ERROR) << "Failed to update last command"; |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | if (fsync(wfd) == -1) { |
| 150 | PLOG(ERROR) << "Failed to fsync " << last_command_tmp; |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | if (chown(last_command_tmp.c_str(), AID_SYSTEM, AID_SYSTEM) == -1) { |
| 155 | PLOG(ERROR) << "Failed to change owner for " << last_command_tmp; |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | if (rename(last_command_tmp.c_str(), last_command_file.c_str()) == -1) { |
| 160 | PLOG(ERROR) << "Failed to rename" << last_command_tmp; |
| 161 | return false; |
| 162 | } |
| 163 | |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 164 | if (!FsyncDir(android::base::Dirname(last_command_file))) { |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 165 | return false; |
| 166 | } |
| 167 | |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | |
Yifan Hong | 8ff84d7 | 2018-12-19 16:21:55 -0800 | [diff] [blame] | 171 | bool SetUpdatedMarker(const std::string& marker) { |
| 172 | auto dirname = android::base::Dirname(marker); |
| 173 | auto res = mkdir(dirname.c_str(), MARKER_DIRECTORY_MODE); |
| 174 | if (res == -1 && errno != EEXIST) { |
| 175 | PLOG(ERROR) << "Failed to create directory for marker: " << dirname; |
| 176 | return false; |
| 177 | } |
| 178 | |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 179 | if (!android::base::WriteStringToFile("", marker)) { |
| 180 | PLOG(ERROR) << "Failed to write to marker file " << marker; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 181 | return false; |
| 182 | } |
Yifan Hong | 8ff84d7 | 2018-12-19 16:21:55 -0800 | [diff] [blame] | 183 | if (!FsyncDir(dirname)) { |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 184 | return false; |
| 185 | } |
Yifan Hong | 8ff84d7 | 2018-12-19 16:21:55 -0800 | [diff] [blame] | 186 | LOG(INFO) << "Wrote updated marker to " << marker; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | |
Yifan Hong | 363d624 | 2019-01-04 11:14:19 -0800 | [diff] [blame] | 190 | static bool discard_blocks(int fd, off64_t offset, uint64_t size, bool force = false) { |
| 191 | // Don't discard blocks unless the update is a retry run or force == true |
| 192 | if (!is_retry && !force) { |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 193 | return true; |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | uint64_t args[2] = { static_cast<uint64_t>(offset), size }; |
| 197 | if (ioctl(fd, BLKDISCARD, &args) == -1) { |
Yifan Hong | 363d624 | 2019-01-04 11:14:19 -0800 | [diff] [blame] | 198 | // On devices that does not support BLKDISCARD, ignore the error. |
| 199 | if (errno == EOPNOTSUPP) { |
| 200 | return true; |
| 201 | } |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 202 | PLOG(ERROR) << "BLKDISCARD ioctl failed"; |
| 203 | return false; |
| 204 | } |
| 205 | return true; |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 208 | static bool check_lseek(int fd, off64_t offset, int whence) { |
| 209 | off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence)); |
| 210 | if (rc == -1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 211 | failure_type = kLseekFailure; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 212 | PLOG(ERROR) << "lseek64 failed"; |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 213 | return false; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 214 | } |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 215 | return true; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 218 | static void allocate(size_t size, std::vector<uint8_t>* buffer) { |
| 219 | // If the buffer's big enough, reuse it. |
| 220 | if (size <= buffer->size()) return; |
| 221 | buffer->resize(size); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 224 | /** |
| 225 | * RangeSinkWriter reads data from the given FD, and writes them to the destination specified by the |
| 226 | * given RangeSet. |
| 227 | */ |
| 228 | class RangeSinkWriter { |
| 229 | public: |
| 230 | RangeSinkWriter(int fd, const RangeSet& tgt) |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 231 | : fd_(fd), |
| 232 | tgt_(tgt), |
| 233 | next_range_(0), |
| 234 | current_range_left_(0), |
| 235 | bytes_written_(0) { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 236 | CHECK_NE(tgt.size(), static_cast<size_t>(0)); |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 237 | }; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 238 | |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 239 | bool Finished() const { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 240 | return next_range_ == tgt_.size() && current_range_left_ == 0; |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 243 | size_t AvailableSpace() const { |
| 244 | return tgt_.blocks() * BLOCKSIZE - bytes_written_; |
| 245 | } |
| 246 | |
| 247 | // Return number of bytes written; and 0 indicates a writing failure. |
| 248 | size_t Write(const uint8_t* data, size_t size) { |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 249 | if (Finished()) { |
| 250 | LOG(ERROR) << "range sink write overrun; can't write " << size << " bytes"; |
| 251 | return 0; |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 254 | size_t written = 0; |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 255 | while (size > 0) { |
| 256 | // Move to the next range as needed. |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 257 | if (!SeekToOutputRange()) { |
| 258 | break; |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 259 | } |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 260 | |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 261 | size_t write_now = size; |
| 262 | if (current_range_left_ < write_now) { |
| 263 | write_now = current_range_left_; |
| 264 | } |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 265 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 266 | if (!android::base::WriteFully(fd_, data, write_now)) { |
| 267 | failure_type = errno == EIO ? kEioFailure : kFwriteFailure; |
| 268 | PLOG(ERROR) << "Failed to write " << write_now << " bytes of data"; |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 269 | break; |
| 270 | } |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 271 | |
| 272 | data += write_now; |
| 273 | size -= write_now; |
| 274 | |
| 275 | current_range_left_ -= write_now; |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 276 | written += write_now; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 277 | } |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 278 | |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 279 | bytes_written_ += written; |
| 280 | return written; |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 281 | } |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 282 | |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 283 | size_t BytesWritten() const { |
| 284 | return bytes_written_; |
| 285 | } |
| 286 | |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 287 | private: |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 288 | // Set up the output cursor, move to next range if needed. |
| 289 | bool SeekToOutputRange() { |
| 290 | // We haven't finished the current range yet. |
| 291 | if (current_range_left_ != 0) { |
| 292 | return true; |
| 293 | } |
| 294 | // We can't write any more; let the write function return how many bytes have been written |
| 295 | // so far. |
| 296 | if (next_range_ >= tgt_.size()) { |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | const Range& range = tgt_[next_range_]; |
| 301 | off64_t offset = static_cast<off64_t>(range.first) * BLOCKSIZE; |
| 302 | current_range_left_ = (range.second - range.first) * BLOCKSIZE; |
| 303 | next_range_++; |
| 304 | |
| 305 | if (!discard_blocks(fd_, offset, current_range_left_)) { |
| 306 | return false; |
| 307 | } |
| 308 | if (!check_lseek(fd_, offset, SEEK_SET)) { |
| 309 | return false; |
| 310 | } |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | // The output file descriptor. |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 315 | int fd_; |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 316 | // The destination ranges for the data. |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 317 | const RangeSet& tgt_; |
| 318 | // The next range that we should write to. |
| 319 | size_t next_range_; |
| 320 | // The number of bytes to write before moving to the next range. |
| 321 | size_t current_range_left_; |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 322 | // Total bytes written by the writer. |
| 323 | size_t bytes_written_; |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 324 | }; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 325 | |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 326 | /** |
| 327 | * All of the data for all the 'new' transfers is contained in one file in the update package, |
| 328 | * concatenated together in the order in which transfers.list will need it. We want to stream it out |
| 329 | * of the archive (it's compressed) without writing it to a temp file, but we can't write each |
| 330 | * section until it's that transfer's turn to go. |
| 331 | * |
| 332 | * To achieve this, we expand the new data from the archive in a background thread, and block that |
| 333 | * threads 'receive uncompressed data' function until the main thread has reached a point where we |
| 334 | * want some new data to be written. We signal the background thread with the destination for the |
| 335 | * data and block the main thread, waiting for the background thread to complete writing that |
| 336 | * section. Then it signals the main thread to wake up and goes back to blocking waiting for a |
| 337 | * transfer. |
| 338 | * |
| 339 | * NewThreadInfo is the struct used to pass information back and forth between the two threads. When |
| 340 | * the main thread wants some data written, it sets writer to the destination location and signals |
| 341 | * the condition. When the background thread is done writing, it clears writer and signals the |
| 342 | * condition again. |
| 343 | */ |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 344 | struct NewThreadInfo { |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 345 | ZipArchiveHandle za; |
| 346 | ZipEntry entry; |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 347 | bool brotli_compressed; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 348 | |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 349 | std::unique_ptr<RangeSinkWriter> writer; |
| 350 | BrotliDecoderState* brotli_decoder_state; |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 351 | bool receiver_available; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 352 | |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 353 | pthread_mutex_t mu; |
| 354 | pthread_cond_t cv; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 355 | }; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 356 | |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 357 | static bool receive_new_data(const uint8_t* data, size_t size, void* cookie) { |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 358 | NewThreadInfo* nti = static_cast<NewThreadInfo*>(cookie); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 359 | |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 360 | while (size > 0) { |
| 361 | // Wait for nti->writer to be non-null, indicating some of this data is wanted. |
| 362 | pthread_mutex_lock(&nti->mu); |
| 363 | while (nti->writer == nullptr) { |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 364 | // End the new data receiver if we encounter an error when performing block image update. |
| 365 | if (!nti->receiver_available) { |
| 366 | pthread_mutex_unlock(&nti->mu); |
| 367 | return false; |
| 368 | } |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 369 | pthread_cond_wait(&nti->cv, &nti->mu); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 370 | } |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 371 | pthread_mutex_unlock(&nti->mu); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 372 | |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 373 | // At this point nti->writer is set, and we own it. The main thread is waiting for it to |
| 374 | // disappear from nti. |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 375 | size_t write_now = std::min(size, nti->writer->AvailableSpace()); |
| 376 | if (nti->writer->Write(data, write_now) != write_now) { |
| 377 | LOG(ERROR) << "Failed to write " << write_now << " bytes."; |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 378 | return false; |
| 379 | } |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 380 | |
| 381 | data += write_now; |
| 382 | size -= write_now; |
| 383 | |
| 384 | if (nti->writer->Finished()) { |
| 385 | // We have written all the bytes desired by this writer. |
| 386 | |
| 387 | pthread_mutex_lock(&nti->mu); |
| 388 | nti->writer = nullptr; |
| 389 | pthread_cond_broadcast(&nti->cv); |
| 390 | pthread_mutex_unlock(&nti->mu); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | static bool receive_brotli_new_data(const uint8_t* data, size_t size, void* cookie) { |
| 398 | NewThreadInfo* nti = static_cast<NewThreadInfo*>(cookie); |
| 399 | |
| 400 | while (size > 0 || BrotliDecoderHasMoreOutput(nti->brotli_decoder_state)) { |
| 401 | // Wait for nti->writer to be non-null, indicating some of this data is wanted. |
| 402 | pthread_mutex_lock(&nti->mu); |
| 403 | while (nti->writer == nullptr) { |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 404 | // End the receiver if we encounter an error when performing block image update. |
| 405 | if (!nti->receiver_available) { |
| 406 | pthread_mutex_unlock(&nti->mu); |
| 407 | return false; |
| 408 | } |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 409 | pthread_cond_wait(&nti->cv, &nti->mu); |
| 410 | } |
| 411 | pthread_mutex_unlock(&nti->mu); |
| 412 | |
| 413 | // At this point nti->writer is set, and we own it. The main thread is waiting for it to |
| 414 | // disappear from nti. |
| 415 | |
| 416 | size_t buffer_size = std::min<size_t>(32768, nti->writer->AvailableSpace()); |
| 417 | if (buffer_size == 0) { |
| 418 | LOG(ERROR) << "No space left in output range"; |
| 419 | return false; |
| 420 | } |
| 421 | uint8_t buffer[buffer_size]; |
| 422 | size_t available_in = size; |
| 423 | size_t available_out = buffer_size; |
| 424 | uint8_t* next_out = buffer; |
| 425 | |
| 426 | // The brotli decoder will update |data|, |available_in|, |next_out| and |available_out|. |
| 427 | BrotliDecoderResult result = BrotliDecoderDecompressStream( |
| 428 | nti->brotli_decoder_state, &available_in, &data, &available_out, &next_out, nullptr); |
| 429 | |
| 430 | if (result == BROTLI_DECODER_RESULT_ERROR) { |
| 431 | LOG(ERROR) << "Decompression failed with " |
| 432 | << BrotliDecoderErrorString(BrotliDecoderGetErrorCode(nti->brotli_decoder_state)); |
| 433 | return false; |
| 434 | } |
| 435 | |
| 436 | LOG(DEBUG) << "bytes to write: " << buffer_size - available_out << ", bytes consumed " |
| 437 | << size - available_in << ", decoder status " << result; |
| 438 | |
| 439 | size_t write_now = buffer_size - available_out; |
| 440 | if (nti->writer->Write(buffer, write_now) != write_now) { |
| 441 | LOG(ERROR) << "Failed to write " << write_now << " bytes."; |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | // Update the remaining size. The input data ptr is already updated by brotli decoder function. |
| 446 | size = available_in; |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 447 | |
| 448 | if (nti->writer->Finished()) { |
| 449 | // We have written all the bytes desired by this writer. |
| 450 | |
| 451 | pthread_mutex_lock(&nti->mu); |
| 452 | nti->writer = nullptr; |
| 453 | pthread_cond_broadcast(&nti->cv); |
| 454 | pthread_mutex_unlock(&nti->mu); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | return true; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static void* unzip_new_data(void* cookie) { |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 462 | NewThreadInfo* nti = static_cast<NewThreadInfo*>(cookie); |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 463 | if (nti->brotli_compressed) { |
| 464 | ProcessZipEntryContents(nti->za, &nti->entry, receive_brotli_new_data, nti); |
| 465 | } else { |
| 466 | ProcessZipEntryContents(nti->za, &nti->entry, receive_new_data, nti); |
| 467 | } |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 468 | pthread_mutex_lock(&nti->mu); |
| 469 | nti->receiver_available = false; |
| 470 | if (nti->writer != nullptr) { |
| 471 | pthread_cond_broadcast(&nti->cv); |
| 472 | } |
| 473 | pthread_mutex_unlock(&nti->mu); |
| 474 | return nullptr; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 477 | static int ReadBlocks(const RangeSet& src, std::vector<uint8_t>* buffer, int fd) { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 478 | size_t p = 0; |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 479 | for (const auto& [begin, end] : src) { |
| 480 | if (!check_lseek(fd, static_cast<off64_t>(begin) * BLOCKSIZE, SEEK_SET)) { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 481 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 484 | size_t size = (end - begin) * BLOCKSIZE; |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 485 | if (!android::base::ReadFully(fd, buffer->data() + p, size)) { |
| 486 | failure_type = errno == EIO ? kEioFailure : kFreadFailure; |
| 487 | PLOG(ERROR) << "Failed to read " << size << " bytes of data"; |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 488 | return -1; |
| 489 | } |
| 490 | |
| 491 | p += size; |
| 492 | } |
| 493 | |
| 494 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 497 | static int WriteBlocks(const RangeSet& tgt, const std::vector<uint8_t>& buffer, int fd) { |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 498 | size_t written = 0; |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 499 | for (const auto& [begin, end] : tgt) { |
| 500 | off64_t offset = static_cast<off64_t>(begin) * BLOCKSIZE; |
| 501 | size_t size = (end - begin) * BLOCKSIZE; |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 502 | if (!discard_blocks(fd, offset, size)) { |
| 503 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 506 | if (!check_lseek(fd, offset, SEEK_SET)) { |
| 507 | return -1; |
| 508 | } |
| 509 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 510 | if (!android::base::WriteFully(fd, buffer.data() + written, size)) { |
| 511 | failure_type = errno == EIO ? kEioFailure : kFwriteFailure; |
| 512 | PLOG(ERROR) << "Failed to write " << size << " bytes of data"; |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 513 | return -1; |
| 514 | } |
| 515 | |
| 516 | written += size; |
| 517 | } |
| 518 | |
| 519 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 522 | // Parameters for transfer list command functions |
| 523 | struct CommandParameters { |
| 524 | std::vector<std::string> tokens; |
| 525 | size_t cpos; |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 526 | std::string cmdname; |
| 527 | std::string cmdline; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 528 | std::string freestash; |
| 529 | std::string stashbase; |
| 530 | bool canwrite; |
| 531 | int createdstash; |
Elliott Hughes | bcabd09 | 2016-03-22 20:19:22 -0700 | [diff] [blame] | 532 | android::base::unique_fd fd; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 533 | bool foundwrites; |
| 534 | bool isunresumable; |
| 535 | int version; |
| 536 | size_t written; |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 537 | size_t stashed; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 538 | NewThreadInfo nti; |
| 539 | pthread_t thread; |
| 540 | std::vector<uint8_t> buffer; |
| 541 | uint8_t* patch_start; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 542 | bool target_verified; // The target blocks have expected contents already. |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 543 | }; |
| 544 | |
Tianjie Xu | 2cd36ba | 2017-03-15 23:52:46 +0000 | [diff] [blame] | 545 | // Print the hash in hex for corrupted source blocks (excluding the stashed blocks which is |
| 546 | // handled separately). |
| 547 | static void PrintHashForCorruptedSourceBlocks(const CommandParameters& params, |
| 548 | const std::vector<uint8_t>& buffer) { |
| 549 | LOG(INFO) << "unexpected contents of source blocks in cmd:\n" << params.cmdline; |
Tianjie Xu | 2cd36ba | 2017-03-15 23:52:46 +0000 | [diff] [blame] | 550 | CHECK(params.tokens[0] == "move" || params.tokens[0] == "bsdiff" || |
| 551 | params.tokens[0] == "imgdiff"); |
| 552 | |
| 553 | size_t pos = 0; |
| 554 | // Command example: |
| 555 | // move <onehash> <tgt_range> <src_blk_count> <src_range> [<loc_range> <stashed_blocks>] |
| 556 | // bsdiff <offset> <len> <src_hash> <tgt_hash> <tgt_range> <src_blk_count> <src_range> |
| 557 | // [<loc_range> <stashed_blocks>] |
| 558 | if (params.tokens[0] == "move") { |
| 559 | // src_range for move starts at the 4th position. |
| 560 | if (params.tokens.size() < 5) { |
| 561 | LOG(ERROR) << "failed to parse source range in cmd:\n" << params.cmdline; |
| 562 | return; |
| 563 | } |
| 564 | pos = 4; |
| 565 | } else { |
| 566 | // src_range for diff starts at the 7th position. |
| 567 | if (params.tokens.size() < 8) { |
| 568 | LOG(ERROR) << "failed to parse source range in cmd:\n" << params.cmdline; |
| 569 | return; |
| 570 | } |
| 571 | pos = 7; |
| 572 | } |
| 573 | |
| 574 | // Source blocks in stash only, no work to do. |
| 575 | if (params.tokens[pos] == "-") { |
| 576 | return; |
| 577 | } |
| 578 | |
Tao Bao | 8f23757 | 2017-03-26 13:36:49 -0700 | [diff] [blame] | 579 | RangeSet src = RangeSet::Parse(params.tokens[pos++]); |
Tao Bao | 6798315 | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 580 | if (!src) { |
| 581 | LOG(ERROR) << "Failed to parse range in " << params.cmdline; |
| 582 | return; |
| 583 | } |
Tianjie Xu | 2cd36ba | 2017-03-15 23:52:46 +0000 | [diff] [blame] | 584 | |
| 585 | RangeSet locs; |
| 586 | // If there's no stashed blocks, content in the buffer is consecutive and has the same |
| 587 | // order as the source blocks. |
| 588 | if (pos == params.tokens.size()) { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 589 | locs = RangeSet(std::vector<Range>{ Range{ 0, src.blocks() } }); |
Tianjie Xu | 2cd36ba | 2017-03-15 23:52:46 +0000 | [diff] [blame] | 590 | } else { |
| 591 | // Otherwise, the next token is the offset of the source blocks in the target range. |
| 592 | // Example: for the tokens <4,63946,63947,63948,63979> <4,6,7,8,39> <stashed_blocks>; |
| 593 | // We want to print SHA-1 for the data in buffer[6], buffer[8], buffer[9] ... buffer[38]; |
| 594 | // this corresponds to the 32 src blocks #63946, #63948, #63949 ... #63978. |
Tao Bao | 8f23757 | 2017-03-26 13:36:49 -0700 | [diff] [blame] | 595 | locs = RangeSet::Parse(params.tokens[pos++]); |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 596 | CHECK_EQ(src.blocks(), locs.blocks()); |
Tianjie Xu | 2cd36ba | 2017-03-15 23:52:46 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 599 | LOG(INFO) << "printing hash in hex for " << src.blocks() << " source blocks"; |
| 600 | for (size_t i = 0; i < src.blocks(); i++) { |
Tao Bao | 8f23757 | 2017-03-26 13:36:49 -0700 | [diff] [blame] | 601 | size_t block_num = src.GetBlockNumber(i); |
| 602 | size_t buffer_index = locs.GetBlockNumber(i); |
Tianjie Xu | 2cd36ba | 2017-03-15 23:52:46 +0000 | [diff] [blame] | 603 | CHECK_LE((buffer_index + 1) * BLOCKSIZE, buffer.size()); |
| 604 | |
| 605 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 606 | SHA1(buffer.data() + buffer_index * BLOCKSIZE, BLOCKSIZE, digest); |
| 607 | std::string hexdigest = print_sha1(digest); |
| 608 | LOG(INFO) << " block number: " << block_num << ", SHA-1: " << hexdigest; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | // If the calculated hash for the whole stash doesn't match the stash id, print the SHA-1 |
| 613 | // in hex for each block. |
| 614 | static void PrintHashForCorruptedStashedBlocks(const std::string& id, |
| 615 | const std::vector<uint8_t>& buffer, |
| 616 | const RangeSet& src) { |
| 617 | LOG(INFO) << "printing hash in hex for stash_id: " << id; |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 618 | CHECK_EQ(src.blocks() * BLOCKSIZE, buffer.size()); |
Tianjie Xu | 2cd36ba | 2017-03-15 23:52:46 +0000 | [diff] [blame] | 619 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 620 | for (size_t i = 0; i < src.blocks(); i++) { |
Tao Bao | 8f23757 | 2017-03-26 13:36:49 -0700 | [diff] [blame] | 621 | size_t block_num = src.GetBlockNumber(i); |
Tianjie Xu | 2cd36ba | 2017-03-15 23:52:46 +0000 | [diff] [blame] | 622 | |
| 623 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 624 | SHA1(buffer.data() + i * BLOCKSIZE, BLOCKSIZE, digest); |
| 625 | std::string hexdigest = print_sha1(digest); |
| 626 | LOG(INFO) << " block number: " << block_num << ", SHA-1: " << hexdigest; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | // If the stash file doesn't exist, read the source blocks this stash contains and print the |
| 631 | // SHA-1 for these blocks. |
| 632 | static void PrintHashForMissingStashedBlocks(const std::string& id, int fd) { |
| 633 | if (stash_map.find(id) == stash_map.end()) { |
| 634 | LOG(ERROR) << "No stash saved for id: " << id; |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | LOG(INFO) << "print hash in hex for source blocks in missing stash: " << id; |
| 639 | const RangeSet& src = stash_map[id]; |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 640 | std::vector<uint8_t> buffer(src.blocks() * BLOCKSIZE); |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 641 | if (ReadBlocks(src, &buffer, fd) == -1) { |
| 642 | LOG(ERROR) << "failed to read source blocks for stash: " << id; |
| 643 | return; |
Tianjie Xu | 2cd36ba | 2017-03-15 23:52:46 +0000 | [diff] [blame] | 644 | } |
| 645 | PrintHashForCorruptedStashedBlocks(id, buffer, src); |
| 646 | } |
| 647 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 648 | static int VerifyBlocks(const std::string& expected, const std::vector<uint8_t>& buffer, |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 649 | const size_t blocks, bool printerror) { |
| 650 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 651 | const uint8_t* data = buffer.data(); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 652 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 653 | SHA1(data, blocks * BLOCKSIZE, digest); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 654 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 655 | std::string hexdigest = print_sha1(digest); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 656 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 657 | if (hexdigest != expected) { |
| 658 | if (printerror) { |
| 659 | LOG(ERROR) << "failed to verify blocks (expected " << expected << ", read " << hexdigest |
| 660 | << ")"; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 661 | } |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 662 | return -1; |
| 663 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 664 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 665 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 668 | static std::string GetStashFileName(const std::string& base, const std::string& id, |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 669 | const std::string& postfix) { |
| 670 | if (base.empty()) { |
| 671 | return ""; |
| 672 | } |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 673 | std::string filename = Paths::Get().stash_directory_base() + "/" + base; |
| 674 | if (id.empty() && postfix.empty()) { |
| 675 | return filename; |
| 676 | } |
| 677 | return filename + "/" + id + postfix; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 680 | // Does a best effort enumeration of stash files. Ignores possible non-file items in the stash |
| 681 | // directory and continues despite of errors. Calls the 'callback' function for each file. |
| 682 | static void EnumerateStash(const std::string& dirname, |
| 683 | const std::function<void(const std::string&)>& callback) { |
| 684 | if (dirname.empty()) return; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 685 | |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 686 | std::unique_ptr<DIR, decltype(&closedir)> directory(opendir(dirname.c_str()), closedir); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 687 | |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 688 | if (directory == nullptr) { |
| 689 | if (errno != ENOENT) { |
| 690 | PLOG(ERROR) << "opendir \"" << dirname << "\" failed"; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 691 | } |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 692 | return; |
| 693 | } |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 694 | |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 695 | dirent* item; |
| 696 | while ((item = readdir(directory.get())) != nullptr) { |
| 697 | if (item->d_type != DT_REG) continue; |
| 698 | callback(dirname + "/" + item->d_name); |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 699 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | // Deletes the stash directory and all files in it. Assumes that it only |
| 703 | // contains files. There is nothing we can do about unlikely, but possible |
| 704 | // errors, so they are merely logged. |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 705 | static void DeleteFile(const std::string& fn) { |
| 706 | if (fn.empty()) return; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 707 | |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 708 | LOG(INFO) << "deleting " << fn; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 709 | |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 710 | if (unlink(fn.c_str()) == -1 && errno != ENOENT) { |
| 711 | PLOG(ERROR) << "unlink \"" << fn << "\" failed"; |
| 712 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 715 | static void DeleteStash(const std::string& base) { |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 716 | if (base.empty()) return; |
| 717 | |
| 718 | LOG(INFO) << "deleting stash " << base; |
| 719 | |
| 720 | std::string dirname = GetStashFileName(base, "", ""); |
| 721 | EnumerateStash(dirname, DeleteFile); |
| 722 | |
| 723 | if (rmdir(dirname.c_str()) == -1) { |
| 724 | if (errno != ENOENT && errno != ENOTDIR) { |
| 725 | PLOG(ERROR) << "rmdir \"" << dirname << "\" failed"; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 726 | } |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 727 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 730 | static int LoadStash(const CommandParameters& params, const std::string& id, bool verify, |
| 731 | std::vector<uint8_t>* buffer, bool printnoent) { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 732 | // In verify mode, if source range_set was saved for the given hash, check contents in the source |
| 733 | // blocks first. If the check fails, search for the stashed files on /cache as usual. |
| 734 | if (!params.canwrite) { |
| 735 | if (stash_map.find(id) != stash_map.end()) { |
| 736 | const RangeSet& src = stash_map[id]; |
| 737 | allocate(src.blocks() * BLOCKSIZE, buffer); |
Tianjie Xu | 7eca97e | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 738 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 739 | if (ReadBlocks(src, buffer, params.fd) == -1) { |
| 740 | LOG(ERROR) << "failed to read source blocks in stash map."; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 741 | return -1; |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 742 | } |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 743 | if (VerifyBlocks(id, *buffer, src.blocks(), true) != 0) { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 744 | LOG(ERROR) << "failed to verify loaded source blocks in stash map."; |
Tianjie Xu | 3c5958f | 2018-03-09 14:10:25 -0800 | [diff] [blame] | 745 | if (!is_retry) { |
| 746 | PrintHashForCorruptedStashedBlocks(id, *buffer, src); |
| 747 | } |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 748 | return -1; |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 749 | } |
| 750 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 751 | } |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 752 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 753 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 754 | std::string fn = GetStashFileName(params.stashbase, id, ""); |
| 755 | |
| 756 | struct stat sb; |
| 757 | if (stat(fn.c_str(), &sb) == -1) { |
| 758 | if (errno != ENOENT || printnoent) { |
| 759 | PLOG(ERROR) << "stat \"" << fn << "\" failed"; |
| 760 | PrintHashForMissingStashedBlocks(id, params.fd); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 761 | } |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 762 | return -1; |
| 763 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 764 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 765 | LOG(INFO) << " loading " << fn; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 766 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 767 | if ((sb.st_size % BLOCKSIZE) != 0) { |
| 768 | LOG(ERROR) << fn << " size " << sb.st_size << " not multiple of block size " << BLOCKSIZE; |
| 769 | return -1; |
| 770 | } |
| 771 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 772 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY))); |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 773 | if (fd == -1) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 774 | failure_type = errno == EIO ? kEioFailure : kFileOpenFailure; |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 775 | PLOG(ERROR) << "open \"" << fn << "\" failed"; |
| 776 | return -1; |
| 777 | } |
| 778 | |
| 779 | allocate(sb.st_size, buffer); |
| 780 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 781 | if (!android::base::ReadFully(fd, buffer->data(), sb.st_size)) { |
| 782 | failure_type = errno == EIO ? kEioFailure : kFreadFailure; |
| 783 | PLOG(ERROR) << "Failed to read " << sb.st_size << " bytes of data"; |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 784 | return -1; |
| 785 | } |
| 786 | |
Tao Bao | 64957ce | 2018-05-30 16:21:39 -0700 | [diff] [blame] | 787 | size_t blocks = sb.st_size / BLOCKSIZE; |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 788 | if (verify && VerifyBlocks(id, *buffer, blocks, true) != 0) { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 789 | LOG(ERROR) << "unexpected contents in " << fn; |
| 790 | if (stash_map.find(id) == stash_map.end()) { |
| 791 | LOG(ERROR) << "failed to find source blocks number for stash " << id |
| 792 | << " when executing command: " << params.cmdname; |
| 793 | } else { |
| 794 | const RangeSet& src = stash_map[id]; |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 795 | PrintHashForCorruptedStashedBlocks(id, *buffer, src); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 796 | } |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 797 | DeleteFile(fn); |
| 798 | return -1; |
| 799 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 800 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 801 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 804 | static int WriteStash(const std::string& base, const std::string& id, int blocks, |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 805 | const std::vector<uint8_t>& buffer, bool checkspace, bool* exists) { |
| 806 | if (base.empty()) { |
| 807 | return -1; |
| 808 | } |
| 809 | |
Tao Bao | 5ee2566 | 2018-07-11 15:55:32 -0700 | [diff] [blame] | 810 | if (checkspace && !CheckAndFreeSpaceOnCache(blocks * BLOCKSIZE)) { |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 811 | LOG(ERROR) << "not enough space to write stash"; |
| 812 | return -1; |
| 813 | } |
| 814 | |
| 815 | std::string fn = GetStashFileName(base, id, ".partial"); |
| 816 | std::string cn = GetStashFileName(base, id, ""); |
| 817 | |
| 818 | if (exists) { |
| 819 | struct stat sb; |
| 820 | int res = stat(cn.c_str(), &sb); |
| 821 | |
| 822 | if (res == 0) { |
| 823 | // The file already exists and since the name is the hash of the contents, |
| 824 | // it's safe to assume the contents are identical (accidental hash collisions |
| 825 | // are unlikely) |
| 826 | LOG(INFO) << " skipping " << blocks << " existing blocks in " << cn; |
| 827 | *exists = true; |
| 828 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 831 | *exists = false; |
| 832 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 833 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 834 | LOG(INFO) << " writing " << blocks << " blocks to " << cn; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 835 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 836 | android::base::unique_fd fd( |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 837 | TEMP_FAILURE_RETRY(open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE))); |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 838 | if (fd == -1) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 839 | failure_type = errno == EIO ? kEioFailure : kFileOpenFailure; |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 840 | PLOG(ERROR) << "failed to create \"" << fn << "\""; |
| 841 | return -1; |
| 842 | } |
Sami Tolvanen | 43b748f | 2015-04-17 12:50:31 +0100 | [diff] [blame] | 843 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 844 | if (fchown(fd, AID_SYSTEM, AID_SYSTEM) != 0) { // system user |
| 845 | PLOG(ERROR) << "failed to chown \"" << fn << "\""; |
| 846 | return -1; |
| 847 | } |
Sami Tolvanen | 43b748f | 2015-04-17 12:50:31 +0100 | [diff] [blame] | 848 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 849 | if (!android::base::WriteFully(fd, buffer.data(), blocks * BLOCKSIZE)) { |
| 850 | failure_type = errno == EIO ? kEioFailure : kFwriteFailure; |
| 851 | PLOG(ERROR) << "Failed to write " << blocks * BLOCKSIZE << " bytes of data"; |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 852 | return -1; |
| 853 | } |
Sami Tolvanen | 43b748f | 2015-04-17 12:50:31 +0100 | [diff] [blame] | 854 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 855 | if (fsync(fd) == -1) { |
| 856 | failure_type = errno == EIO ? kEioFailure : kFsyncFailure; |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 857 | PLOG(ERROR) << "fsync \"" << fn << "\" failed"; |
| 858 | return -1; |
| 859 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 860 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 861 | if (rename(fn.c_str(), cn.c_str()) == -1) { |
| 862 | PLOG(ERROR) << "rename(\"" << fn << "\", \"" << cn << "\") failed"; |
| 863 | return -1; |
| 864 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 865 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 866 | std::string dname = GetStashFileName(base, "", ""); |
| 867 | if (!FsyncDir(dname)) { |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 868 | return -1; |
| 869 | } |
Tianjie Xu | a946b9e | 2017-03-21 16:24:57 -0700 | [diff] [blame] | 870 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 871 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | // Creates a directory for storing stash files and checks if the /cache partition |
| 875 | // hash enough space for the expected amount of blocks we need to store. Returns |
| 876 | // >0 if we created the directory, zero if it existed already, and <0 of failure. |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 877 | static int CreateStash(State* state, size_t maxblocks, const std::string& base) { |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 878 | std::string dirname = GetStashFileName(base, "", ""); |
| 879 | struct stat sb; |
| 880 | int res = stat(dirname.c_str(), &sb); |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 881 | if (res == -1 && errno != ENOENT) { |
Tianjie Xu | 5ad8028 | 2018-01-28 15:37:48 -0800 | [diff] [blame] | 882 | ErrorAbort(state, kStashCreationFailure, "stat \"%s\" failed: %s", dirname.c_str(), |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 883 | strerror(errno)); |
| 884 | return -1; |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | size_t max_stash_size = maxblocks * BLOCKSIZE; |
| 888 | if (res == -1) { |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 889 | LOG(INFO) << "creating stash " << dirname; |
Yifan Hong | 63f5260 | 2019-01-11 13:52:33 -0800 | [diff] [blame] | 890 | res = mkdir_recursively(dirname, STASH_DIRECTORY_MODE, false, nullptr); |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 891 | |
| 892 | if (res != 0) { |
Tianjie Xu | 5ad8028 | 2018-01-28 15:37:48 -0800 | [diff] [blame] | 893 | ErrorAbort(state, kStashCreationFailure, "mkdir \"%s\" failed: %s", dirname.c_str(), |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 894 | strerror(errno)); |
| 895 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Tianjie Xu | a946b9e | 2017-03-21 16:24:57 -0700 | [diff] [blame] | 898 | if (chown(dirname.c_str(), AID_SYSTEM, AID_SYSTEM) != 0) { // system user |
Tianjie Xu | 5ad8028 | 2018-01-28 15:37:48 -0800 | [diff] [blame] | 899 | ErrorAbort(state, kStashCreationFailure, "chown \"%s\" failed: %s", dirname.c_str(), |
Tianjie Xu | a946b9e | 2017-03-21 16:24:57 -0700 | [diff] [blame] | 900 | strerror(errno)); |
| 901 | return -1; |
| 902 | } |
| 903 | |
Tao Bao | 5ee2566 | 2018-07-11 15:55:32 -0700 | [diff] [blame] | 904 | if (!CheckAndFreeSpaceOnCache(max_stash_size)) { |
Tianjie Xu | 5ad8028 | 2018-01-28 15:37:48 -0800 | [diff] [blame] | 905 | ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu needed)", |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 906 | max_stash_size); |
| 907 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 910 | return 1; // Created directory |
| 911 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 912 | |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 913 | LOG(INFO) << "using existing stash " << dirname; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 914 | |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 915 | // If the directory already exists, calculate the space already allocated to stash files and check |
| 916 | // if there's enough for all required blocks. Delete any partially completed stash files first. |
| 917 | EnumerateStash(dirname, [](const std::string& fn) { |
| 918 | if (android::base::EndsWith(fn, ".partial")) { |
| 919 | DeleteFile(fn); |
| 920 | } |
| 921 | }); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 922 | |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 923 | size_t existing = 0; |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 924 | EnumerateStash(dirname, [&existing](const std::string& fn) { |
| 925 | if (fn.empty()) return; |
| 926 | struct stat sb; |
| 927 | if (stat(fn.c_str(), &sb) == -1) { |
| 928 | PLOG(ERROR) << "stat \"" << fn << "\" failed"; |
| 929 | return; |
| 930 | } |
| 931 | existing += static_cast<size_t>(sb.st_size); |
| 932 | }); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 933 | |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 934 | if (max_stash_size > existing) { |
| 935 | size_t needed = max_stash_size - existing; |
Tao Bao | 5ee2566 | 2018-07-11 15:55:32 -0700 | [diff] [blame] | 936 | if (!CheckAndFreeSpaceOnCache(needed)) { |
Tianjie Xu | 5ad8028 | 2018-01-28 15:37:48 -0800 | [diff] [blame] | 937 | ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu more needed)", |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 938 | needed); |
| 939 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 940 | } |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 941 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 942 | |
Tao Bao | 5141221 | 2016-12-28 14:44:05 -0800 | [diff] [blame] | 943 | return 0; // Using existing directory |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 946 | static int FreeStash(const std::string& base, const std::string& id) { |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 947 | if (base.empty() || id.empty()) { |
| 948 | return -1; |
| 949 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 950 | |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 951 | DeleteFile(GetStashFileName(base, id, "")); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 952 | |
Tao Bao | ec8272f | 2017-03-15 17:39:01 -0700 | [diff] [blame] | 953 | return 0; |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 956 | // Source contains packed data, which we want to move to the locations given in locs in the dest |
| 957 | // buffer. source and dest may be the same buffer. |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 958 | static void MoveRange(std::vector<uint8_t>& dest, const RangeSet& locs, |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 959 | const std::vector<uint8_t>& source) { |
| 960 | const uint8_t* from = source.data(); |
| 961 | uint8_t* to = dest.data(); |
| 962 | size_t start = locs.blocks(); |
| 963 | // Must do the movement backward. |
| 964 | for (auto it = locs.crbegin(); it != locs.crend(); it++) { |
| 965 | size_t blocks = it->second - it->first; |
| 966 | start -= blocks; |
| 967 | memmove(to + (it->first * BLOCKSIZE), from + (start * BLOCKSIZE), blocks * BLOCKSIZE); |
| 968 | } |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 971 | /** |
| 972 | * We expect to parse the remainder of the parameter tokens as one of: |
| 973 | * |
| 974 | * <src_block_count> <src_range> |
| 975 | * (loads data from source image only) |
| 976 | * |
| 977 | * <src_block_count> - <[stash_id:stash_range] ...> |
| 978 | * (loads data from stashes only) |
| 979 | * |
| 980 | * <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...> |
| 981 | * (loads data from both source image and stashes) |
| 982 | * |
| 983 | * On return, params.buffer is filled with the loaded source data (rearranged and combined with |
| 984 | * stashed data as necessary). buffer may be reallocated if needed to accommodate the source data. |
| 985 | * tgt is the target RangeSet for detecting overlaps. Any stashes required are loaded using |
| 986 | * LoadStash. |
| 987 | */ |
| 988 | static int LoadSourceBlocks(CommandParameters& params, const RangeSet& tgt, size_t* src_blocks, |
| 989 | bool* overlap) { |
| 990 | CHECK(src_blocks != nullptr); |
| 991 | CHECK(overlap != nullptr); |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 992 | |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 993 | // <src_block_count> |
| 994 | const std::string& token = params.tokens[params.cpos++]; |
| 995 | if (!android::base::ParseUint(token, src_blocks)) { |
| 996 | LOG(ERROR) << "invalid src_block_count \"" << token << "\""; |
| 997 | return -1; |
| 998 | } |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 999 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 1000 | allocate(*src_blocks * BLOCKSIZE, ¶ms.buffer); |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1001 | |
| 1002 | // "-" or <src_range> [<src_loc>] |
| 1003 | if (params.tokens[params.cpos] == "-") { |
| 1004 | // no source ranges, only stashes |
| 1005 | params.cpos++; |
| 1006 | } else { |
Tao Bao | 8f23757 | 2017-03-26 13:36:49 -0700 | [diff] [blame] | 1007 | RangeSet src = RangeSet::Parse(params.tokens[params.cpos++]); |
Tao Bao | 6798315 | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 1008 | CHECK(static_cast<bool>(src)); |
Tao Bao | 8f23757 | 2017-03-26 13:36:49 -0700 | [diff] [blame] | 1009 | *overlap = src.Overlaps(tgt); |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1010 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 1011 | if (ReadBlocks(src, ¶ms.buffer, params.fd) == -1) { |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1012 | return -1; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1015 | if (params.cpos >= params.tokens.size()) { |
| 1016 | // no stashes, only source range |
| 1017 | return 0; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1018 | } |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 1019 | |
Tao Bao | 8f23757 | 2017-03-26 13:36:49 -0700 | [diff] [blame] | 1020 | RangeSet locs = RangeSet::Parse(params.tokens[params.cpos++]); |
Tao Bao | 6798315 | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 1021 | CHECK(static_cast<bool>(locs)); |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1022 | MoveRange(params.buffer, locs, params.buffer); |
| 1023 | } |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 1024 | |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1025 | // <[stash_id:stash_range]> |
| 1026 | while (params.cpos < params.tokens.size()) { |
| 1027 | // Each word is a an index into the stash table, a colon, and then a RangeSet describing where |
| 1028 | // in the source block that stashed data should go. |
| 1029 | std::vector<std::string> tokens = android::base::Split(params.tokens[params.cpos++], ":"); |
| 1030 | if (tokens.size() != 2) { |
| 1031 | LOG(ERROR) << "invalid parameter"; |
| 1032 | return -1; |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1035 | std::vector<uint8_t> stash; |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 1036 | if (LoadStash(params, tokens[0], false, &stash, true) == -1) { |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1037 | // These source blocks will fail verification if used later, but we |
| 1038 | // will let the caller decide if this is a fatal failure |
| 1039 | LOG(ERROR) << "failed to load stash " << tokens[0]; |
| 1040 | continue; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Tao Bao | 8f23757 | 2017-03-26 13:36:49 -0700 | [diff] [blame] | 1043 | RangeSet locs = RangeSet::Parse(tokens[1]); |
Tao Bao | 6798315 | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 1044 | CHECK(static_cast<bool>(locs)); |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1045 | MoveRange(params.buffer, locs, stash); |
| 1046 | } |
| 1047 | |
| 1048 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1051 | /** |
| 1052 | * Do a source/target load for move/bsdiff/imgdiff in version 3. |
| 1053 | * |
| 1054 | * We expect to parse the remainder of the parameter tokens as one of: |
| 1055 | * |
| 1056 | * <tgt_range> <src_block_count> <src_range> |
| 1057 | * (loads data from source image only) |
| 1058 | * |
| 1059 | * <tgt_range> <src_block_count> - <[stash_id:stash_range] ...> |
| 1060 | * (loads data from stashes only) |
| 1061 | * |
| 1062 | * <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...> |
| 1063 | * (loads data from both source image and stashes) |
| 1064 | * |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1065 | * 'onehash' tells whether to expect separate source and targe block hashes, or if they are both the |
| 1066 | * same and only one hash should be expected. params.isunresumable will be set to true if block |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1067 | * verification fails in a way that the update cannot be resumed anymore. |
| 1068 | * |
| 1069 | * If the function is unable to load the necessary blocks or their contents don't match the hashes, |
| 1070 | * the return value is -1 and the command should be aborted. |
| 1071 | * |
| 1072 | * If the return value is 1, the command has already been completed according to the contents of the |
| 1073 | * target blocks, and should not be performed again. |
| 1074 | * |
| 1075 | * If the return value is 0, source blocks have expected content and the command can be performed. |
| 1076 | */ |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 1077 | static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet* tgt, size_t* src_blocks, |
Tao Bao | 4a13508 | 2018-06-07 22:27:44 -0700 | [diff] [blame] | 1078 | bool onehash) { |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1079 | CHECK(src_blocks != nullptr); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1080 | |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1081 | if (params.cpos >= params.tokens.size()) { |
| 1082 | LOG(ERROR) << "missing source hash"; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1083 | return -1; |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | std::string srchash = params.tokens[params.cpos++]; |
| 1087 | std::string tgthash; |
| 1088 | |
| 1089 | if (onehash) { |
| 1090 | tgthash = srchash; |
| 1091 | } else { |
| 1092 | if (params.cpos >= params.tokens.size()) { |
| 1093 | LOG(ERROR) << "missing target hash"; |
| 1094 | return -1; |
| 1095 | } |
| 1096 | tgthash = params.tokens[params.cpos++]; |
| 1097 | } |
| 1098 | |
| 1099 | // At least it needs to provide three parameters: <tgt_range>, <src_block_count> and |
| 1100 | // "-"/<src_range>. |
| 1101 | if (params.cpos + 2 >= params.tokens.size()) { |
| 1102 | LOG(ERROR) << "invalid parameters"; |
| 1103 | return -1; |
| 1104 | } |
| 1105 | |
| 1106 | // <tgt_range> |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 1107 | *tgt = RangeSet::Parse(params.tokens[params.cpos++]); |
| 1108 | CHECK(static_cast<bool>(*tgt)); |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1109 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 1110 | std::vector<uint8_t> tgtbuffer(tgt->blocks() * BLOCKSIZE); |
| 1111 | if (ReadBlocks(*tgt, &tgtbuffer, params.fd) == -1) { |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1112 | return -1; |
| 1113 | } |
| 1114 | |
| 1115 | // Return now if target blocks already have expected content. |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 1116 | if (VerifyBlocks(tgthash, tgtbuffer, tgt->blocks(), false) == 0) { |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1117 | return 1; |
| 1118 | } |
| 1119 | |
| 1120 | // Load source blocks. |
Tao Bao | 4a13508 | 2018-06-07 22:27:44 -0700 | [diff] [blame] | 1121 | bool overlap = false; |
| 1122 | if (LoadSourceBlocks(params, *tgt, src_blocks, &overlap) == -1) { |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1123 | return -1; |
| 1124 | } |
| 1125 | |
| 1126 | if (VerifyBlocks(srchash, params.buffer, *src_blocks, true) == 0) { |
Tao Bao | 4a13508 | 2018-06-07 22:27:44 -0700 | [diff] [blame] | 1127 | // If source and target blocks overlap, stash the source blocks so we can resume from possible |
| 1128 | // write errors. In verify mode, we can skip stashing because the source blocks won't be |
| 1129 | // overwritten. |
| 1130 | if (overlap && params.canwrite) { |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1131 | LOG(INFO) << "stashing " << *src_blocks << " overlapping blocks to " << srchash; |
| 1132 | |
| 1133 | bool stash_exists = false; |
| 1134 | if (WriteStash(params.stashbase, srchash, *src_blocks, params.buffer, true, |
| 1135 | &stash_exists) != 0) { |
| 1136 | LOG(ERROR) << "failed to stash overlapping source blocks"; |
| 1137 | return -1; |
| 1138 | } |
| 1139 | |
| 1140 | params.stashed += *src_blocks; |
| 1141 | // Can be deleted when the write has completed. |
| 1142 | if (!stash_exists) { |
| 1143 | params.freestash = srchash; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | // Source blocks have expected content, command can proceed. |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
Tao Bao | 4a13508 | 2018-06-07 22:27:44 -0700 | [diff] [blame] | 1151 | if (overlap && LoadStash(params, srchash, true, ¶ms.buffer, true) == 0) { |
Tao Bao | d2aecd4 | 2017-03-23 14:43:44 -0700 | [diff] [blame] | 1152 | // Overlapping source blocks were previously stashed, command can proceed. We are recovering |
| 1153 | // from an interrupted command, so we don't know if the stash can safely be deleted after this |
| 1154 | // command. |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | // Valid source data not available, update cannot be resumed. |
| 1159 | LOG(ERROR) << "partition has unexpected contents"; |
| 1160 | PrintHashForCorruptedSourceBlocks(params, params.buffer); |
| 1161 | |
| 1162 | params.isunresumable = true; |
| 1163 | |
| 1164 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1167 | static int PerformCommandMove(CommandParameters& params) { |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1168 | size_t blocks = 0; |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1169 | RangeSet tgt; |
Tao Bao | 4a13508 | 2018-06-07 22:27:44 -0700 | [diff] [blame] | 1170 | int status = LoadSrcTgtVersion3(params, &tgt, &blocks, true); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1171 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1172 | if (status == -1) { |
| 1173 | LOG(ERROR) << "failed to read blocks for move"; |
| 1174 | return -1; |
| 1175 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1176 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1177 | if (status == 0) { |
| 1178 | params.foundwrites = true; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1179 | } else { |
| 1180 | params.target_verified = true; |
| 1181 | if (params.foundwrites) { |
| 1182 | LOG(WARNING) << "warning: commands executed out of order [" << params.cmdname << "]"; |
| 1183 | } |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1184 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1185 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1186 | if (params.canwrite) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1187 | if (status == 0) { |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1188 | LOG(INFO) << " moving " << blocks << " blocks"; |
| 1189 | |
| 1190 | if (WriteBlocks(tgt, params.buffer, params.fd) == -1) { |
| 1191 | return -1; |
| 1192 | } |
| 1193 | } else { |
| 1194 | LOG(INFO) << "skipping " << blocks << " already moved blocks"; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1195 | } |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1196 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1197 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1198 | if (!params.freestash.empty()) { |
| 1199 | FreeStash(params.stashbase, params.freestash); |
| 1200 | params.freestash.clear(); |
| 1201 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1202 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1203 | params.written += tgt.blocks(); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1204 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1205 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1208 | static int PerformCommandStash(CommandParameters& params) { |
Tao Bao | bcf4649 | 2017-03-23 15:28:20 -0700 | [diff] [blame] | 1209 | // <stash_id> <src_range> |
| 1210 | if (params.cpos + 1 >= params.tokens.size()) { |
| 1211 | LOG(ERROR) << "missing id and/or src range fields in stash command"; |
| 1212 | return -1; |
| 1213 | } |
| 1214 | |
| 1215 | const std::string& id = params.tokens[params.cpos++]; |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 1216 | if (LoadStash(params, id, true, ¶ms.buffer, false) == 0) { |
Tao Bao | bcf4649 | 2017-03-23 15:28:20 -0700 | [diff] [blame] | 1217 | // Stash file already exists and has expected contents. Do not read from source again, as the |
| 1218 | // source may have been already overwritten during a previous attempt. |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
Tao Bao | 8f23757 | 2017-03-26 13:36:49 -0700 | [diff] [blame] | 1222 | RangeSet src = RangeSet::Parse(params.tokens[params.cpos++]); |
Tao Bao | 6798315 | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 1223 | CHECK(static_cast<bool>(src)); |
Tao Bao | bcf4649 | 2017-03-23 15:28:20 -0700 | [diff] [blame] | 1224 | |
Tao Bao | 64957ce | 2018-05-30 16:21:39 -0700 | [diff] [blame] | 1225 | size_t blocks = src.blocks(); |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 1226 | allocate(blocks * BLOCKSIZE, ¶ms.buffer); |
| 1227 | if (ReadBlocks(src, ¶ms.buffer, params.fd) == -1) { |
Tao Bao | bcf4649 | 2017-03-23 15:28:20 -0700 | [diff] [blame] | 1228 | return -1; |
| 1229 | } |
Tao Bao | bcf4649 | 2017-03-23 15:28:20 -0700 | [diff] [blame] | 1230 | stash_map[id] = src; |
| 1231 | |
| 1232 | if (VerifyBlocks(id, params.buffer, blocks, true) != 0) { |
| 1233 | // Source blocks have unexpected contents. If we actually need this data later, this is an |
| 1234 | // unrecoverable error. However, the command that uses the data may have already completed |
| 1235 | // previously, so the possible failure will occur during source block verification. |
| 1236 | LOG(ERROR) << "failed to load source blocks for stash " << id; |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | // In verify mode, we don't need to stash any blocks. |
| 1241 | if (!params.canwrite) { |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | LOG(INFO) << "stashing " << blocks << " blocks to " << id; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1246 | int result = WriteStash(params.stashbase, id, blocks, params.buffer, false, nullptr); |
| 1247 | if (result == 0) { |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1248 | params.stashed += blocks; |
| 1249 | } |
| 1250 | return result; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1253 | static int PerformCommandFree(CommandParameters& params) { |
Tao Bao | bcf4649 | 2017-03-23 15:28:20 -0700 | [diff] [blame] | 1254 | // <stash_id> |
| 1255 | if (params.cpos >= params.tokens.size()) { |
| 1256 | LOG(ERROR) << "missing stash id in free command"; |
| 1257 | return -1; |
| 1258 | } |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1259 | |
Tao Bao | bcf4649 | 2017-03-23 15:28:20 -0700 | [diff] [blame] | 1260 | const std::string& id = params.tokens[params.cpos++]; |
| 1261 | stash_map.erase(id); |
Tianjie Xu | 7eca97e | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 1262 | |
Tao Bao | bcf4649 | 2017-03-23 15:28:20 -0700 | [diff] [blame] | 1263 | if (params.createdstash || params.canwrite) { |
| 1264 | return FreeStash(params.stashbase, id); |
| 1265 | } |
Tianjie Xu | 7eca97e | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 1266 | |
Tao Bao | bcf4649 | 2017-03-23 15:28:20 -0700 | [diff] [blame] | 1267 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1270 | static int PerformCommandZero(CommandParameters& params) { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1271 | if (params.cpos >= params.tokens.size()) { |
| 1272 | LOG(ERROR) << "missing target blocks for zero"; |
| 1273 | return -1; |
| 1274 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1275 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1276 | RangeSet tgt = RangeSet::Parse(params.tokens[params.cpos++]); |
Tao Bao | 6798315 | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 1277 | CHECK(static_cast<bool>(tgt)); |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1278 | |
| 1279 | LOG(INFO) << " zeroing " << tgt.blocks() << " blocks"; |
| 1280 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 1281 | allocate(BLOCKSIZE, ¶ms.buffer); |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1282 | memset(params.buffer.data(), 0, BLOCKSIZE); |
| 1283 | |
| 1284 | if (params.canwrite) { |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 1285 | for (const auto& [begin, end] : tgt) { |
| 1286 | off64_t offset = static_cast<off64_t>(begin) * BLOCKSIZE; |
| 1287 | size_t size = (end - begin) * BLOCKSIZE; |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1288 | if (!discard_blocks(params.fd, offset, size)) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1289 | return -1; |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1290 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1291 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1292 | if (!check_lseek(params.fd, offset, SEEK_SET)) { |
| 1293 | return -1; |
| 1294 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1295 | |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 1296 | for (size_t j = begin; j < end; ++j) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 1297 | if (!android::base::WriteFully(params.fd, params.buffer.data(), BLOCKSIZE)) { |
| 1298 | failure_type = errno == EIO ? kEioFailure : kFwriteFailure; |
| 1299 | PLOG(ERROR) << "Failed to write " << BLOCKSIZE << " bytes of data"; |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1300 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1301 | } |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1302 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1303 | } |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1304 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1305 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1306 | if (params.cmdname[0] == 'z') { |
| 1307 | // Update only for the zero command, as the erase command will call |
| 1308 | // this if DEBUG_ERASE is defined. |
| 1309 | params.written += tgt.blocks(); |
| 1310 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1311 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1312 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1315 | static int PerformCommandNew(CommandParameters& params) { |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 1316 | if (params.cpos >= params.tokens.size()) { |
| 1317 | LOG(ERROR) << "missing target blocks for new"; |
| 1318 | return -1; |
| 1319 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1320 | |
Tao Bao | 8f23757 | 2017-03-26 13:36:49 -0700 | [diff] [blame] | 1321 | RangeSet tgt = RangeSet::Parse(params.tokens[params.cpos++]); |
Tao Bao | 6798315 | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 1322 | CHECK(static_cast<bool>(tgt)); |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 1323 | |
| 1324 | if (params.canwrite) { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1325 | LOG(INFO) << " writing " << tgt.blocks() << " blocks of new data"; |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 1326 | |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 1327 | pthread_mutex_lock(¶ms.nti.mu); |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 1328 | params.nti.writer = std::make_unique<RangeSinkWriter>(params.fd, tgt); |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 1329 | pthread_cond_broadcast(¶ms.nti.cv); |
| 1330 | |
| 1331 | while (params.nti.writer != nullptr) { |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 1332 | if (!params.nti.receiver_available) { |
| 1333 | LOG(ERROR) << "missing " << (tgt.blocks() * BLOCKSIZE - params.nti.writer->BytesWritten()) |
| 1334 | << " bytes of new data"; |
| 1335 | pthread_mutex_unlock(¶ms.nti.mu); |
| 1336 | return -1; |
| 1337 | } |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 1338 | pthread_cond_wait(¶ms.nti.cv, ¶ms.nti.mu); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 1341 | pthread_mutex_unlock(¶ms.nti.mu); |
| 1342 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1343 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1344 | params.written += tgt.blocks(); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1345 | |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 1346 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1349 | static int PerformCommandDiff(CommandParameters& params) { |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1350 | // <offset> <length> |
| 1351 | if (params.cpos + 1 >= params.tokens.size()) { |
| 1352 | LOG(ERROR) << "missing patch offset or length for " << params.cmdname; |
| 1353 | return -1; |
| 1354 | } |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1355 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1356 | size_t offset; |
| 1357 | if (!android::base::ParseUint(params.tokens[params.cpos++], &offset)) { |
| 1358 | LOG(ERROR) << "invalid patch offset"; |
| 1359 | return -1; |
| 1360 | } |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1361 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1362 | size_t len; |
| 1363 | if (!android::base::ParseUint(params.tokens[params.cpos++], &len)) { |
| 1364 | LOG(ERROR) << "invalid patch len"; |
| 1365 | return -1; |
| 1366 | } |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1367 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1368 | RangeSet tgt; |
| 1369 | size_t blocks = 0; |
Tao Bao | 4a13508 | 2018-06-07 22:27:44 -0700 | [diff] [blame] | 1370 | int status = LoadSrcTgtVersion3(params, &tgt, &blocks, false); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1371 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1372 | if (status == -1) { |
| 1373 | LOG(ERROR) << "failed to read blocks for diff"; |
| 1374 | return -1; |
| 1375 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1376 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1377 | if (status == 0) { |
| 1378 | params.foundwrites = true; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1379 | } else { |
| 1380 | params.target_verified = true; |
| 1381 | if (params.foundwrites) { |
| 1382 | LOG(WARNING) << "warning: commands executed out of order [" << params.cmdname << "]"; |
| 1383 | } |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1384 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1385 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1386 | if (params.canwrite) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1387 | if (status == 0) { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1388 | LOG(INFO) << "patching " << blocks << " blocks to " << tgt.blocks(); |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1389 | Value patch_value( |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 1390 | Value::Type::BLOB, |
| 1391 | std::string(reinterpret_cast<const char*>(params.patch_start + offset), len)); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1392 | |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 1393 | RangeSinkWriter writer(params.fd, tgt); |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1394 | if (params.cmdname[0] == 'i') { // imgdiff |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 1395 | if (ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, patch_value, |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 1396 | std::bind(&RangeSinkWriter::Write, &writer, std::placeholders::_1, |
| 1397 | std::placeholders::_2), |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 1398 | nullptr) != 0) { |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1399 | LOG(ERROR) << "Failed to apply image patch."; |
Tianjie Xu | 6957555 | 2017-05-16 15:51:46 -0700 | [diff] [blame] | 1400 | failure_type = kPatchApplicationFailure; |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1401 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1402 | } |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1403 | } else { |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 1404 | if (ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, patch_value, 0, |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 1405 | std::bind(&RangeSinkWriter::Write, &writer, std::placeholders::_1, |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 1406 | std::placeholders::_2)) != 0) { |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1407 | LOG(ERROR) << "Failed to apply bsdiff patch."; |
Tianjie Xu | 6957555 | 2017-05-16 15:51:46 -0700 | [diff] [blame] | 1408 | failure_type = kPatchApplicationFailure; |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1409 | return -1; |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | // We expect the output of the patcher to fill the tgt ranges exactly. |
Tao Bao | 60a70af | 2017-03-26 14:03:52 -0700 | [diff] [blame] | 1414 | if (!writer.Finished()) { |
Tao Bao | a2cff95 | 2018-11-02 15:44:07 -0700 | [diff] [blame] | 1415 | LOG(ERROR) << "Failed to fully write target blocks (range sink underrun): Missing " |
| 1416 | << writer.AvailableSpace() << " bytes"; |
| 1417 | failure_type = kPatchApplicationFailure; |
| 1418 | return -1; |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1419 | } |
| 1420 | } else { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1421 | LOG(INFO) << "skipping " << blocks << " blocks already patched to " << tgt.blocks() << " [" |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1422 | << params.cmdline << "]"; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1423 | } |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1424 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1425 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1426 | if (!params.freestash.empty()) { |
| 1427 | FreeStash(params.stashbase, params.freestash); |
| 1428 | params.freestash.clear(); |
| 1429 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1430 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1431 | params.written += tgt.blocks(); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1432 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 1433 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1436 | static int PerformCommandErase(CommandParameters& params) { |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1437 | if (DEBUG_ERASE) { |
| 1438 | return PerformCommandZero(params); |
| 1439 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1440 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1441 | struct stat sb; |
| 1442 | if (fstat(params.fd, &sb) == -1) { |
| 1443 | PLOG(ERROR) << "failed to fstat device to erase"; |
| 1444 | return -1; |
| 1445 | } |
| 1446 | |
| 1447 | if (!S_ISBLK(sb.st_mode)) { |
| 1448 | LOG(ERROR) << "not a block device; skipping erase"; |
| 1449 | return -1; |
| 1450 | } |
| 1451 | |
| 1452 | if (params.cpos >= params.tokens.size()) { |
| 1453 | LOG(ERROR) << "missing target blocks for erase"; |
| 1454 | return -1; |
| 1455 | } |
| 1456 | |
| 1457 | RangeSet tgt = RangeSet::Parse(params.tokens[params.cpos++]); |
Tao Bao | 6798315 | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 1458 | CHECK(static_cast<bool>(tgt)); |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1459 | |
| 1460 | if (params.canwrite) { |
| 1461 | LOG(INFO) << " erasing " << tgt.blocks() << " blocks"; |
| 1462 | |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 1463 | for (const auto& [begin, end] : tgt) { |
Yifan Hong | 363d624 | 2019-01-04 11:14:19 -0800 | [diff] [blame] | 1464 | off64_t offset = static_cast<off64_t>(begin) * BLOCKSIZE; |
| 1465 | size_t size = (end - begin) * BLOCKSIZE; |
| 1466 | if (!discard_blocks(params.fd, offset, size, true /* force */)) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1467 | return -1; |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1468 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1469 | } |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1470 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1471 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 1472 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Tao Bao | 91a649a | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1475 | static int PerformCommandAbort(CommandParameters&) { |
| 1476 | LOG(INFO) << "Aborting as instructed"; |
| 1477 | return -1; |
| 1478 | } |
| 1479 | |
Tianjie Xu | 69ffa15 | 2018-08-01 16:40:00 -0700 | [diff] [blame] | 1480 | // Computes the hash_tree bytes based on the parameters, checks if the root hash of the tree |
| 1481 | // matches the expected hash and writes the result to the specified range on the block_device. |
| 1482 | // Hash_tree computation arguments: |
| 1483 | // hash_tree_ranges |
| 1484 | // source_ranges |
| 1485 | // hash_algorithm |
| 1486 | // salt_hex |
| 1487 | // root_hash |
| 1488 | static int PerformCommandComputeHashTree(CommandParameters& params) { |
| 1489 | if (params.cpos + 5 != params.tokens.size()) { |
| 1490 | LOG(ERROR) << "Invaild arguments count in hash computation " << params.cmdline; |
| 1491 | return -1; |
| 1492 | } |
| 1493 | |
| 1494 | // Expects the hash_tree data to be contiguous. |
| 1495 | RangeSet hash_tree_ranges = RangeSet::Parse(params.tokens[params.cpos++]); |
| 1496 | if (!hash_tree_ranges || hash_tree_ranges.size() != 1) { |
| 1497 | LOG(ERROR) << "Invalid hash tree ranges in " << params.cmdline; |
| 1498 | return -1; |
| 1499 | } |
| 1500 | |
| 1501 | RangeSet source_ranges = RangeSet::Parse(params.tokens[params.cpos++]); |
| 1502 | if (!source_ranges) { |
| 1503 | LOG(ERROR) << "Invalid source ranges in " << params.cmdline; |
| 1504 | return -1; |
| 1505 | } |
| 1506 | |
| 1507 | auto hash_function = HashTreeBuilder::HashFunction(params.tokens[params.cpos++]); |
| 1508 | if (hash_function == nullptr) { |
| 1509 | LOG(ERROR) << "Invalid hash algorithm in " << params.cmdline; |
| 1510 | return -1; |
| 1511 | } |
| 1512 | |
| 1513 | std::vector<unsigned char> salt; |
| 1514 | std::string salt_hex = params.tokens[params.cpos++]; |
| 1515 | if (salt_hex.empty() || !HashTreeBuilder::ParseBytesArrayFromString(salt_hex, &salt)) { |
| 1516 | LOG(ERROR) << "Failed to parse salt in " << params.cmdline; |
| 1517 | return -1; |
| 1518 | } |
| 1519 | |
| 1520 | std::string expected_root_hash = params.tokens[params.cpos++]; |
| 1521 | if (expected_root_hash.empty()) { |
| 1522 | LOG(ERROR) << "Invalid root hash in " << params.cmdline; |
| 1523 | return -1; |
| 1524 | } |
| 1525 | |
| 1526 | // Starts the hash_tree computation. |
| 1527 | HashTreeBuilder builder(BLOCKSIZE, hash_function); |
| 1528 | if (!builder.Initialize(source_ranges.blocks() * BLOCKSIZE, salt)) { |
| 1529 | LOG(ERROR) << "Failed to initialize hash tree computation, source " << source_ranges.ToString() |
| 1530 | << ", salt " << salt_hex; |
| 1531 | return -1; |
| 1532 | } |
| 1533 | |
| 1534 | // Iterates through every block in the source_ranges and updates the hash tree structure |
| 1535 | // accordingly. |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 1536 | for (const auto& [begin, end] : source_ranges) { |
Tianjie Xu | 69ffa15 | 2018-08-01 16:40:00 -0700 | [diff] [blame] | 1537 | uint8_t buffer[BLOCKSIZE]; |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 1538 | if (!check_lseek(params.fd, static_cast<off64_t>(begin) * BLOCKSIZE, SEEK_SET)) { |
| 1539 | PLOG(ERROR) << "Failed to seek to block: " << begin; |
Tianjie Xu | 69ffa15 | 2018-08-01 16:40:00 -0700 | [diff] [blame] | 1540 | return -1; |
| 1541 | } |
| 1542 | |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 1543 | for (size_t i = begin; i < end; i++) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 1544 | if (!android::base::ReadFully(params.fd, buffer, BLOCKSIZE)) { |
| 1545 | failure_type = errno == EIO ? kEioFailure : kFreadFailure; |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 1546 | LOG(ERROR) << "Failed to read data in " << begin << ":" << end; |
Tianjie Xu | 69ffa15 | 2018-08-01 16:40:00 -0700 | [diff] [blame] | 1547 | return -1; |
| 1548 | } |
| 1549 | |
| 1550 | if (!builder.Update(reinterpret_cast<unsigned char*>(buffer), BLOCKSIZE)) { |
| 1551 | LOG(ERROR) << "Failed to update hash tree builder"; |
| 1552 | return -1; |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | if (!builder.BuildHashTree()) { |
| 1558 | LOG(ERROR) << "Failed to build hash tree"; |
| 1559 | return -1; |
| 1560 | } |
| 1561 | |
| 1562 | std::string root_hash_hex = HashTreeBuilder::BytesArrayToString(builder.root_hash()); |
| 1563 | if (root_hash_hex != expected_root_hash) { |
| 1564 | LOG(ERROR) << "Root hash of the verity hash tree doesn't match the expected value. Expected: " |
| 1565 | << expected_root_hash << ", actual: " << root_hash_hex; |
| 1566 | return -1; |
| 1567 | } |
| 1568 | |
| 1569 | uint64_t write_offset = static_cast<uint64_t>(hash_tree_ranges.GetBlockNumber(0)) * BLOCKSIZE; |
| 1570 | if (params.canwrite && !builder.WriteHashTreeToFd(params.fd, write_offset)) { |
| 1571 | LOG(ERROR) << "Failed to write hash tree to output"; |
| 1572 | return -1; |
| 1573 | } |
| 1574 | |
| 1575 | // TODO(xunchang) validates the written bytes |
| 1576 | |
| 1577 | return 0; |
| 1578 | } |
| 1579 | |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1580 | using CommandFunction = std::function<int(CommandParameters&)>; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1581 | |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1582 | using CommandMap = std::unordered_map<Command::Type, CommandFunction>; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1583 | |
Yifan Hong | 8ff84d7 | 2018-12-19 16:21:55 -0800 | [diff] [blame] | 1584 | static bool Sha1DevicePath(const std::string& path, uint8_t digest[SHA_DIGEST_LENGTH]) { |
| 1585 | auto device_name = android::base::Basename(path); |
| 1586 | auto dm_target_name_path = "/sys/block/" + device_name + "/dm/name"; |
| 1587 | |
| 1588 | struct stat sb; |
| 1589 | if (stat(dm_target_name_path.c_str(), &sb) == 0) { |
| 1590 | // This is a device mapper target. Use partition name as part of the hash instead. Do not |
| 1591 | // include extents as part of the hash, because the size of a partition may be shrunk after |
| 1592 | // the patches are applied. |
| 1593 | std::string dm_target_name; |
| 1594 | if (!android::base::ReadFileToString(dm_target_name_path, &dm_target_name)) { |
| 1595 | PLOG(ERROR) << "Cannot read " << dm_target_name_path; |
| 1596 | return false; |
| 1597 | } |
| 1598 | SHA1(reinterpret_cast<const uint8_t*>(dm_target_name.data()), dm_target_name.size(), digest); |
| 1599 | return true; |
| 1600 | } |
| 1601 | |
| 1602 | if (errno != ENOENT) { |
| 1603 | // This is a device mapper target, but its name cannot be retrieved. |
| 1604 | PLOG(ERROR) << "Cannot get dm target name for " << path; |
| 1605 | return false; |
| 1606 | } |
| 1607 | |
| 1608 | // This doesn't appear to be a device mapper target, but if its name starts with dm-, something |
| 1609 | // else might have gone wrong. |
| 1610 | if (android::base::StartsWith(device_name, "dm-")) { |
| 1611 | LOG(WARNING) << "Device " << path << " starts with dm- but is not mapped by device-mapper."; |
| 1612 | } |
| 1613 | |
| 1614 | // Stash directory should be different for each partition to avoid conflicts when updating |
| 1615 | // multiple partitions at the same time, so we use the hash of the block device name as the base |
| 1616 | // directory. |
| 1617 | SHA1(reinterpret_cast<const uint8_t*>(path.data()), path.size(), digest); |
| 1618 | return true; |
| 1619 | } |
| 1620 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 1621 | static Value* PerformBlockImageUpdate(const char* name, State* state, |
| 1622 | const std::vector<std::unique_ptr<Expr>>& argv, |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1623 | const CommandMap& command_map, bool dryrun) { |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1624 | CommandParameters params = {}; |
Tao Bao | c0299ed | 2018-05-24 00:16:35 -0700 | [diff] [blame] | 1625 | stash_map.clear(); |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1626 | params.canwrite = !dryrun; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1627 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1628 | LOG(INFO) << "performing " << (dryrun ? "verification" : "update"); |
| 1629 | if (state->is_retry) { |
| 1630 | is_retry = true; |
| 1631 | LOG(INFO) << "This update is a retry."; |
| 1632 | } |
| 1633 | if (argv.size() != 4) { |
| 1634 | ErrorAbort(state, kArgsParsingFailure, "block_image_update expects 4 arguments, got %zu", |
| 1635 | argv.size()); |
| 1636 | return StringValue(""); |
| 1637 | } |
| 1638 | |
| 1639 | std::vector<std::unique_ptr<Value>> args; |
| 1640 | if (!ReadValueArgs(state, argv, &args)) { |
| 1641 | return nullptr; |
| 1642 | } |
| 1643 | |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1644 | // args: |
| 1645 | // - block device (or file) to modify in-place |
| 1646 | // - transfer list (blob) |
| 1647 | // - new data stream (filename within package.zip) |
| 1648 | // - patch stream (filename within package.zip, must be uncompressed) |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 1649 | const std::unique_ptr<Value>& blockdev_filename = args[0]; |
| 1650 | const std::unique_ptr<Value>& transfer_list_value = args[1]; |
| 1651 | const std::unique_ptr<Value>& new_data_fn = args[2]; |
| 1652 | const std::unique_ptr<Value>& patch_data_fn = args[3]; |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1653 | |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 1654 | if (blockdev_filename->type != Value::Type::STRING) { |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1655 | ErrorAbort(state, kArgsParsingFailure, "blockdev_filename argument to %s must be string", name); |
| 1656 | return StringValue(""); |
| 1657 | } |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 1658 | if (transfer_list_value->type != Value::Type::BLOB) { |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1659 | ErrorAbort(state, kArgsParsingFailure, "transfer_list argument to %s must be blob", name); |
| 1660 | return StringValue(""); |
| 1661 | } |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 1662 | if (new_data_fn->type != Value::Type::STRING) { |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1663 | ErrorAbort(state, kArgsParsingFailure, "new_data_fn argument to %s must be string", name); |
| 1664 | return StringValue(""); |
| 1665 | } |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 1666 | if (patch_data_fn->type != Value::Type::STRING) { |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1667 | ErrorAbort(state, kArgsParsingFailure, "patch_data_fn argument to %s must be string", name); |
| 1668 | return StringValue(""); |
| 1669 | } |
| 1670 | |
| 1671 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 1672 | if (ui == nullptr) { |
| 1673 | return StringValue(""); |
| 1674 | } |
| 1675 | |
| 1676 | FILE* cmd_pipe = ui->cmd_pipe; |
| 1677 | ZipArchiveHandle za = ui->package_zip; |
| 1678 | |
| 1679 | if (cmd_pipe == nullptr || za == nullptr) { |
| 1680 | return StringValue(""); |
| 1681 | } |
| 1682 | |
| 1683 | ZipString path_data(patch_data_fn->data.c_str()); |
| 1684 | ZipEntry patch_entry; |
| 1685 | if (FindEntry(za, path_data, &patch_entry) != 0) { |
| 1686 | LOG(ERROR) << name << "(): no file \"" << patch_data_fn->data << "\" in package"; |
| 1687 | return StringValue(""); |
| 1688 | } |
| 1689 | |
| 1690 | params.patch_start = ui->package_zip_addr + patch_entry.offset; |
| 1691 | ZipString new_data(new_data_fn->data.c_str()); |
| 1692 | ZipEntry new_entry; |
| 1693 | if (FindEntry(za, new_data, &new_entry) != 0) { |
| 1694 | LOG(ERROR) << name << "(): no file \"" << new_data_fn->data << "\" in package"; |
| 1695 | return StringValue(""); |
| 1696 | } |
| 1697 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 1698 | params.fd.reset(TEMP_FAILURE_RETRY(open(blockdev_filename->data.c_str(), O_RDWR))); |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1699 | if (params.fd == -1) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 1700 | failure_type = errno == EIO ? kEioFailure : kFileOpenFailure; |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1701 | PLOG(ERROR) << "open \"" << blockdev_filename->data << "\" failed"; |
| 1702 | return StringValue(""); |
| 1703 | } |
| 1704 | |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 1705 | uint8_t digest[SHA_DIGEST_LENGTH]; |
Yifan Hong | 8ff84d7 | 2018-12-19 16:21:55 -0800 | [diff] [blame] | 1706 | if (!Sha1DevicePath(blockdev_filename->data, digest)) { |
| 1707 | return StringValue(""); |
| 1708 | } |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 1709 | params.stashbase = print_sha1(digest); |
| 1710 | |
| 1711 | // Possibly do return early on retry, by checking the marker. If the update on this partition has |
| 1712 | // been finished (but interrupted at a later point), there could be leftover on /cache that would |
| 1713 | // fail the no-op retry. |
| 1714 | std::string updated_marker = GetStashFileName(params.stashbase + ".UPDATED", "", ""); |
| 1715 | if (is_retry) { |
| 1716 | struct stat sb; |
| 1717 | int result = stat(updated_marker.c_str(), &sb); |
| 1718 | if (result == 0) { |
| 1719 | LOG(INFO) << "Skipping already updated partition " << blockdev_filename->data |
| 1720 | << " based on marker"; |
| 1721 | return StringValue("t"); |
| 1722 | } |
| 1723 | } else { |
| 1724 | // Delete the obsolete marker if any. |
| 1725 | std::string err; |
| 1726 | if (!android::base::RemoveFileIfExists(updated_marker, &err)) { |
| 1727 | LOG(ERROR) << "Failed to remove partition updated marker " << updated_marker << ": " << err; |
| 1728 | return StringValue(""); |
| 1729 | } |
| 1730 | } |
| 1731 | |
Tao Bao | ffede3e | 2018-06-07 09:56:19 -0700 | [diff] [blame] | 1732 | static constexpr size_t kTransferListHeaderLines = 4; |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1733 | std::vector<std::string> lines = android::base::Split(transfer_list_value->data, "\n"); |
Tao Bao | ffede3e | 2018-06-07 09:56:19 -0700 | [diff] [blame] | 1734 | if (lines.size() < kTransferListHeaderLines) { |
| 1735 | ErrorAbort(state, kArgsParsingFailure, "too few lines in the transfer list [%zu]", |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1736 | lines.size()); |
| 1737 | return StringValue(""); |
| 1738 | } |
| 1739 | |
| 1740 | // First line in transfer list is the version number. |
| 1741 | if (!android::base::ParseInt(lines[0], ¶ms.version, 3, 4)) { |
| 1742 | LOG(ERROR) << "unexpected transfer list version [" << lines[0] << "]"; |
| 1743 | return StringValue(""); |
| 1744 | } |
| 1745 | |
| 1746 | LOG(INFO) << "blockimg version is " << params.version; |
| 1747 | |
| 1748 | // Second line in transfer list is the total number of blocks we expect to write. |
| 1749 | size_t total_blocks; |
| 1750 | if (!android::base::ParseUint(lines[1], &total_blocks)) { |
Tianjie Xu | 5ad8028 | 2018-01-28 15:37:48 -0800 | [diff] [blame] | 1751 | ErrorAbort(state, kArgsParsingFailure, "unexpected block count [%s]", lines[1].c_str()); |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1752 | return StringValue(""); |
| 1753 | } |
| 1754 | |
| 1755 | if (total_blocks == 0) { |
| 1756 | return StringValue("t"); |
| 1757 | } |
| 1758 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1759 | // Third line is how many stash entries are needed simultaneously. |
| 1760 | LOG(INFO) << "maximum stash entries " << lines[2]; |
| 1761 | |
| 1762 | // Fourth line is the maximum number of blocks that will be stashed simultaneously |
| 1763 | size_t stash_max_blocks; |
| 1764 | if (!android::base::ParseUint(lines[3], &stash_max_blocks)) { |
Tianjie Xu | 5ad8028 | 2018-01-28 15:37:48 -0800 | [diff] [blame] | 1765 | ErrorAbort(state, kArgsParsingFailure, "unexpected maximum stash blocks [%s]", |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1766 | lines[3].c_str()); |
| 1767 | return StringValue(""); |
| 1768 | } |
| 1769 | |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 1770 | int res = CreateStash(state, stash_max_blocks, params.stashbase); |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1771 | if (res == -1) { |
| 1772 | return StringValue(""); |
| 1773 | } |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1774 | params.createdstash = res; |
| 1775 | |
Tao Bao | 0a883c1 | 2018-06-18 12:49:06 -0700 | [diff] [blame] | 1776 | // Set up the new data writer. |
| 1777 | if (params.canwrite) { |
| 1778 | params.nti.za = za; |
| 1779 | params.nti.entry = new_entry; |
| 1780 | params.nti.brotli_compressed = android::base::EndsWith(new_data_fn->data, ".br"); |
| 1781 | if (params.nti.brotli_compressed) { |
| 1782 | // Initialize brotli decoder state. |
| 1783 | params.nti.brotli_decoder_state = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr); |
| 1784 | } |
| 1785 | params.nti.receiver_available = true; |
| 1786 | |
| 1787 | pthread_mutex_init(¶ms.nti.mu, nullptr); |
| 1788 | pthread_cond_init(¶ms.nti.cv, nullptr); |
| 1789 | pthread_attr_t attr; |
| 1790 | pthread_attr_init(&attr); |
| 1791 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| 1792 | |
| 1793 | int error = pthread_create(¶ms.thread, &attr, unzip_new_data, ¶ms.nti); |
| 1794 | if (error != 0) { |
| 1795 | LOG(ERROR) << "pthread_create failed: " << strerror(error); |
| 1796 | return StringValue(""); |
| 1797 | } |
| 1798 | } |
| 1799 | |
Tao Bao | 26efb0a | 2018-05-21 14:59:55 -0700 | [diff] [blame] | 1800 | // When performing an update, save the index and cmdline of the current command into the |
| 1801 | // last_command_file. |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1802 | // Upon resuming an update, read the saved index first; then |
| 1803 | // 1. In verification mode, check if the 'move' or 'diff' commands before the saved index has |
| 1804 | // the expected target blocks already. If not, these commands cannot be skipped and we need |
| 1805 | // to attempt to execute them again. Therefore, we will delete the last_command_file so that |
| 1806 | // the update will resume from the start of the transfer list. |
| 1807 | // 2. In update mode, skip all commands before the saved index. Therefore, we can avoid deleting |
| 1808 | // stashes with duplicate id unintentionally (b/69858743); and also speed up the update. |
| 1809 | // If an update succeeds or is unresumable, delete the last_command_file. |
Tao Bao | 26efb0a | 2018-05-21 14:59:55 -0700 | [diff] [blame] | 1810 | bool skip_executed_command = true; |
| 1811 | size_t saved_last_command_index; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1812 | if (!ParseLastCommandFile(&saved_last_command_index)) { |
| 1813 | DeleteLastCommandFile(); |
Tao Bao | 26efb0a | 2018-05-21 14:59:55 -0700 | [diff] [blame] | 1814 | // We failed to parse the last command. Disallow skipping executed commands. |
| 1815 | skip_executed_command = false; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1816 | } |
| 1817 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1818 | int rc = -1; |
| 1819 | |
| 1820 | // Subsequent lines are all individual transfer commands |
Tao Bao | ab20706 | 2018-05-21 14:48:49 -0700 | [diff] [blame] | 1821 | for (size_t i = kTransferListHeaderLines; i < lines.size(); i++) { |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1822 | const std::string& line = lines[i]; |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1823 | if (line.empty()) continue; |
| 1824 | |
Tao Bao | 26efb0a | 2018-05-21 14:59:55 -0700 | [diff] [blame] | 1825 | size_t cmdindex = i - kTransferListHeaderLines; |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1826 | params.tokens = android::base::Split(line, " "); |
| 1827 | params.cpos = 0; |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1828 | params.cmdname = params.tokens[params.cpos++]; |
| 1829 | params.cmdline = line; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1830 | params.target_verified = false; |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1831 | |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1832 | Command::Type cmd_type = Command::ParseType(params.cmdname); |
| 1833 | if (cmd_type == Command::Type::LAST) { |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1834 | LOG(ERROR) << "unexpected command [" << params.cmdname << "]"; |
| 1835 | goto pbiudone; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 1836 | } |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1837 | |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1838 | const CommandFunction& performer = command_map.at(cmd_type); |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1839 | |
Tianjie Xu | c242084 | 2018-02-27 17:05:39 -0800 | [diff] [blame] | 1840 | // Skip the command if we explicitly set the corresponding function pointer to nullptr, e.g. |
| 1841 | // "erase" during block_image_verify. |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1842 | if (performer == nullptr) { |
Tianjie Xu | c242084 | 2018-02-27 17:05:39 -0800 | [diff] [blame] | 1843 | LOG(DEBUG) << "skip executing command [" << line << "]"; |
| 1844 | continue; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1845 | } |
| 1846 | |
Tao Bao | 98f875e | 2018-05-07 15:03:30 -0700 | [diff] [blame] | 1847 | // Skip all commands before the saved last command index when resuming an update, except for |
| 1848 | // "new" command. Because new commands read in the data sequentially. |
Tao Bao | 26efb0a | 2018-05-21 14:59:55 -0700 | [diff] [blame] | 1849 | if (params.canwrite && skip_executed_command && cmdindex <= saved_last_command_index && |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1850 | cmd_type != Command::Type::NEW) { |
Tao Bao | 26efb0a | 2018-05-21 14:59:55 -0700 | [diff] [blame] | 1851 | LOG(INFO) << "Skipping already executed command: " << cmdindex |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1852 | << ", last executed command for previous update: " << saved_last_command_index; |
| 1853 | continue; |
| 1854 | } |
| 1855 | |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1856 | if (performer(params) == -1) { |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1857 | LOG(ERROR) << "failed to execute command [" << line << "]"; |
Tianjie Xu | 69ffa15 | 2018-08-01 16:40:00 -0700 | [diff] [blame] | 1858 | if (cmd_type == Command::Type::COMPUTE_HASH_TREE && failure_type == kNoCause) { |
| 1859 | failure_type = kHashTreeComputationFailure; |
| 1860 | } |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1861 | goto pbiudone; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
Tao Bao | 26efb0a | 2018-05-21 14:59:55 -0700 | [diff] [blame] | 1864 | // In verify mode, check if the commands before the saved last_command_index have been executed |
| 1865 | // correctly. If some target blocks have unexpected contents, delete the last command file so |
| 1866 | // that we will resume the update from the first command in the transfer list. |
| 1867 | if (!params.canwrite && skip_executed_command && cmdindex <= saved_last_command_index) { |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1868 | // TODO(xunchang) check that the cmdline of the saved index is correct. |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1869 | if ((cmd_type == Command::Type::MOVE || cmd_type == Command::Type::BSDIFF || |
| 1870 | cmd_type == Command::Type::IMGDIFF) && |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1871 | !params.target_verified) { |
| 1872 | LOG(WARNING) << "Previously executed command " << saved_last_command_index << ": " |
| 1873 | << params.cmdline << " doesn't produce expected target blocks."; |
Tao Bao | 26efb0a | 2018-05-21 14:59:55 -0700 | [diff] [blame] | 1874 | skip_executed_command = false; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1875 | DeleteLastCommandFile(); |
| 1876 | } |
| 1877 | } |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1878 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1879 | if (params.canwrite) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 1880 | if (fsync(params.fd) == -1) { |
| 1881 | failure_type = errno == EIO ? kEioFailure : kFsyncFailure; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1882 | PLOG(ERROR) << "fsync failed"; |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1883 | goto pbiudone; |
| 1884 | } |
Tianjie Xu | c2b2bb5 | 2018-05-15 15:09:59 -0700 | [diff] [blame] | 1885 | |
Tao Bao | 26efb0a | 2018-05-21 14:59:55 -0700 | [diff] [blame] | 1886 | if (!UpdateLastCommandIndex(cmdindex, params.cmdline)) { |
Tianjie Xu | c2b2bb5 | 2018-05-15 15:09:59 -0700 | [diff] [blame] | 1887 | LOG(WARNING) << "Failed to update the last command file."; |
| 1888 | } |
| 1889 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1890 | fprintf(cmd_pipe, "set_progress %.4f\n", static_cast<double>(params.written) / total_blocks); |
| 1891 | fflush(cmd_pipe); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1892 | } |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1893 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1894 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1895 | rc = 0; |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 1896 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1897 | pbiudone: |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 1898 | if (params.canwrite) { |
| 1899 | pthread_mutex_lock(¶ms.nti.mu); |
| 1900 | if (params.nti.receiver_available) { |
| 1901 | LOG(WARNING) << "new data receiver is still available after executing all commands."; |
| 1902 | } |
| 1903 | params.nti.receiver_available = false; |
| 1904 | pthread_cond_broadcast(¶ms.nti.cv); |
| 1905 | pthread_mutex_unlock(¶ms.nti.mu); |
| 1906 | int ret = pthread_join(params.thread, nullptr); |
| 1907 | if (ret != 0) { |
| 1908 | LOG(WARNING) << "pthread join returned with " << strerror(ret); |
| 1909 | } |
| 1910 | |
| 1911 | if (rc == 0) { |
| 1912 | LOG(INFO) << "wrote " << params.written << " blocks; expected " << total_blocks; |
| 1913 | LOG(INFO) << "stashed " << params.stashed << " blocks"; |
| 1914 | LOG(INFO) << "max alloc needed was " << params.buffer.size(); |
| 1915 | |
| 1916 | const char* partition = strrchr(blockdev_filename->data.c_str(), '/'); |
| 1917 | if (partition != nullptr && *(partition + 1) != 0) { |
| 1918 | fprintf(cmd_pipe, "log bytes_written_%s: %zu\n", partition + 1, params.written * BLOCKSIZE); |
| 1919 | fprintf(cmd_pipe, "log bytes_stashed_%s: %zu\n", partition + 1, params.stashed * BLOCKSIZE); |
| 1920 | fflush(cmd_pipe); |
| 1921 | } |
| 1922 | // Delete stash only after successfully completing the update, as it may contain blocks needed |
| 1923 | // to complete the update later. |
| 1924 | DeleteStash(params.stashbase); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1925 | DeleteLastCommandFile(); |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 1926 | |
| 1927 | // Create a marker on /cache partition, which allows skipping the update on this partition on |
| 1928 | // retry. The marker will be removed once booting into normal boot, or before starting next |
| 1929 | // fresh install. |
Yifan Hong | 8ff84d7 | 2018-12-19 16:21:55 -0800 | [diff] [blame] | 1930 | if (!SetUpdatedMarker(updated_marker)) { |
Tao Bao | 864c668 | 2018-05-07 11:38:25 -0700 | [diff] [blame] | 1931 | LOG(WARNING) << "Failed to set updated marker; continuing"; |
| 1932 | } |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | pthread_mutex_destroy(¶ms.nti.mu); |
| 1936 | pthread_cond_destroy(¶ms.nti.cv); |
| 1937 | } else if (rc == 0) { |
| 1938 | LOG(INFO) << "verified partition contents; update may be resumed"; |
| 1939 | } |
| 1940 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 1941 | if (fsync(params.fd) == -1) { |
| 1942 | failure_type = errno == EIO ? kEioFailure : kFsyncFailure; |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1943 | PLOG(ERROR) << "fsync failed"; |
| 1944 | } |
| 1945 | // params.fd will be automatically closed because it's a unique_fd. |
| 1946 | |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 1947 | if (params.nti.brotli_decoder_state != nullptr) { |
| 1948 | BrotliDecoderDestroyInstance(params.nti.brotli_decoder_state); |
| 1949 | } |
| 1950 | |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 1951 | // Delete the last command file if the update cannot be resumed. |
| 1952 | if (params.isunresumable) { |
| 1953 | DeleteLastCommandFile(); |
| 1954 | } |
| 1955 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1956 | // Only delete the stash if the update cannot be resumed, or it's a verification run and we |
| 1957 | // created the stash. |
| 1958 | if (params.isunresumable || (!params.canwrite && params.createdstash)) { |
| 1959 | DeleteStash(params.stashbase); |
| 1960 | } |
| 1961 | |
| 1962 | if (failure_type != kNoCause && state->cause_code == kNoCause) { |
| 1963 | state->cause_code = failure_type; |
| 1964 | } |
| 1965 | |
| 1966 | return StringValue(rc == 0 ? "t" : ""); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
Tao Bao | 3356777 | 2017-03-13 14:57:34 -0700 | [diff] [blame] | 1969 | /** |
| 1970 | * The transfer list is a text file containing commands to transfer data from one place to another |
| 1971 | * on the target partition. We parse it and execute the commands in order: |
| 1972 | * |
| 1973 | * zero [rangeset] |
| 1974 | * - Fill the indicated blocks with zeros. |
| 1975 | * |
| 1976 | * new [rangeset] |
| 1977 | * - Fill the blocks with data read from the new_data file. |
| 1978 | * |
| 1979 | * erase [rangeset] |
| 1980 | * - Mark the given blocks as empty. |
| 1981 | * |
| 1982 | * move <...> |
| 1983 | * bsdiff <patchstart> <patchlen> <...> |
| 1984 | * imgdiff <patchstart> <patchlen> <...> |
| 1985 | * - Read the source blocks, apply a patch (or not in the case of move), write result to target |
| 1986 | * blocks. bsdiff or imgdiff specifies the type of patch; move means no patch at all. |
| 1987 | * |
| 1988 | * See the comments in LoadSrcTgtVersion3() for a description of the <...> format. |
| 1989 | * |
| 1990 | * stash <stash_id> <src_range> |
| 1991 | * - Load the given source range and stash the data in the given slot of the stash table. |
| 1992 | * |
| 1993 | * free <stash_id> |
| 1994 | * - Free the given stash data. |
| 1995 | * |
| 1996 | * The creator of the transfer list will guarantee that no block is read (ie, used as the source for |
| 1997 | * a patch or move) after it has been written. |
| 1998 | * |
| 1999 | * The creator will guarantee that a given stash is loaded (with a stash command) before it's used |
| 2000 | * in a move/bsdiff/imgdiff command. |
| 2001 | * |
| 2002 | * Within one command the source and target ranges may overlap so in general we need to read the |
| 2003 | * entire source into memory before writing anything to the target blocks. |
| 2004 | * |
| 2005 | * All the patch data is concatenated into one patch_data file in the update package. It must be |
| 2006 | * stored uncompressed because we memory-map it in directly from the archive. (Since patches are |
| 2007 | * already compressed, we lose very little by not compressing their concatenation.) |
| 2008 | * |
| 2009 | * Commands that read data from the partition (i.e. move/bsdiff/imgdiff/stash) have one or more |
| 2010 | * additional hashes before the range parameters, which are used to check if the command has already |
| 2011 | * been completed and verify the integrity of the source data. |
| 2012 | */ |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 2013 | Value* BlockImageVerifyFn(const char* name, State* state, |
| 2014 | const std::vector<std::unique_ptr<Expr>>& argv) { |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 2015 | // Commands which are not allowed are set to nullptr to skip them completely. |
| 2016 | const CommandMap command_map{ |
| 2017 | // clang-format off |
Tianjie Xu | 69ffa15 | 2018-08-01 16:40:00 -0700 | [diff] [blame] | 2018 | { Command::Type::ABORT, PerformCommandAbort }, |
| 2019 | { Command::Type::BSDIFF, PerformCommandDiff }, |
| 2020 | { Command::Type::COMPUTE_HASH_TREE, PerformCommandComputeHashTree }, |
| 2021 | { Command::Type::ERASE, nullptr }, |
| 2022 | { Command::Type::FREE, PerformCommandFree }, |
| 2023 | { Command::Type::IMGDIFF, PerformCommandDiff }, |
| 2024 | { Command::Type::MOVE, PerformCommandMove }, |
| 2025 | { Command::Type::NEW, nullptr }, |
| 2026 | { Command::Type::STASH, PerformCommandStash }, |
| 2027 | { Command::Type::ZERO, nullptr }, |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 2028 | // clang-format on |
| 2029 | }; |
| 2030 | CHECK_EQ(static_cast<size_t>(Command::Type::LAST), command_map.size()); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 2031 | |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 2032 | // Perform a dry run without writing to test if an update can proceed. |
| 2033 | return PerformBlockImageUpdate(name, state, argv, command_map, true); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 2036 | Value* BlockImageUpdateFn(const char* name, State* state, |
| 2037 | const std::vector<std::unique_ptr<Expr>>& argv) { |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 2038 | const CommandMap command_map{ |
| 2039 | // clang-format off |
Tianjie Xu | 69ffa15 | 2018-08-01 16:40:00 -0700 | [diff] [blame] | 2040 | { Command::Type::ABORT, PerformCommandAbort }, |
| 2041 | { Command::Type::BSDIFF, PerformCommandDiff }, |
| 2042 | { Command::Type::COMPUTE_HASH_TREE, PerformCommandComputeHashTree }, |
| 2043 | { Command::Type::ERASE, PerformCommandErase }, |
| 2044 | { Command::Type::FREE, PerformCommandFree }, |
| 2045 | { Command::Type::IMGDIFF, PerformCommandDiff }, |
| 2046 | { Command::Type::MOVE, PerformCommandMove }, |
| 2047 | { Command::Type::NEW, PerformCommandNew }, |
| 2048 | { Command::Type::STASH, PerformCommandStash }, |
| 2049 | { Command::Type::ZERO, PerformCommandZero }, |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 2050 | // clang-format on |
| 2051 | }; |
| 2052 | CHECK_EQ(static_cast<size_t>(Command::Type::LAST), command_map.size()); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 2053 | |
Tao Bao | c390123 | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 2054 | return PerformBlockImageUpdate(name, state, argv, command_map, false); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 2055 | } |
| 2056 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 2057 | Value* RangeSha1Fn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2058 | if (argv.size() != 2) { |
| 2059 | ErrorAbort(state, kArgsParsingFailure, "range_sha1 expects 2 arguments, got %zu", argv.size()); |
| 2060 | return StringValue(""); |
| 2061 | } |
| 2062 | |
| 2063 | std::vector<std::unique_ptr<Value>> args; |
| 2064 | if (!ReadValueArgs(state, argv, &args)) { |
| 2065 | return nullptr; |
| 2066 | } |
| 2067 | |
| 2068 | const std::unique_ptr<Value>& blockdev_filename = args[0]; |
| 2069 | const std::unique_ptr<Value>& ranges = args[1]; |
| 2070 | |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 2071 | if (blockdev_filename->type != Value::Type::STRING) { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2072 | ErrorAbort(state, kArgsParsingFailure, "blockdev_filename argument to %s must be string", name); |
| 2073 | return StringValue(""); |
| 2074 | } |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 2075 | if (ranges->type != Value::Type::STRING) { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2076 | ErrorAbort(state, kArgsParsingFailure, "ranges argument to %s must be string", name); |
| 2077 | return StringValue(""); |
| 2078 | } |
| 2079 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 2080 | android::base::unique_fd fd(open(blockdev_filename->data.c_str(), O_RDWR)); |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2081 | if (fd == -1) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 2082 | CauseCode cause_code = errno == EIO ? kEioFailure : kFileOpenFailure; |
| 2083 | ErrorAbort(state, cause_code, "open \"%s\" failed: %s", blockdev_filename->data.c_str(), |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2084 | strerror(errno)); |
| 2085 | return StringValue(""); |
| 2086 | } |
| 2087 | |
| 2088 | RangeSet rs = RangeSet::Parse(ranges->data); |
Tao Bao | 6798315 | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 2089 | CHECK(static_cast<bool>(rs)); |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2090 | |
| 2091 | SHA_CTX ctx; |
| 2092 | SHA1_Init(&ctx); |
| 2093 | |
| 2094 | std::vector<uint8_t> buffer(BLOCKSIZE); |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 2095 | for (const auto& [begin, end] : rs) { |
| 2096 | if (!check_lseek(fd, static_cast<off64_t>(begin) * BLOCKSIZE, SEEK_SET)) { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2097 | ErrorAbort(state, kLseekFailure, "failed to seek %s: %s", blockdev_filename->data.c_str(), |
| 2098 | strerror(errno)); |
| 2099 | return StringValue(""); |
| 2100 | } |
| 2101 | |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 2102 | for (size_t j = begin; j < end; ++j) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 2103 | if (!android::base::ReadFully(fd, buffer.data(), BLOCKSIZE)) { |
| 2104 | CauseCode cause_code = errno == EIO ? kEioFailure : kFreadFailure; |
| 2105 | ErrorAbort(state, cause_code, "failed to read %s: %s", blockdev_filename->data.c_str(), |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2106 | strerror(errno)); |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 2107 | return StringValue(""); |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | SHA1_Update(&ctx, buffer.data(), BLOCKSIZE); |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 2111 | } |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2112 | } |
| 2113 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 2114 | SHA1_Final(digest, &ctx); |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 2115 | |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2116 | return StringValue(print_sha1(digest)); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
Tianjie Xu | 57bed6d | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 2119 | // This function checks if a device has been remounted R/W prior to an incremental |
| 2120 | // OTA update. This is an common cause of update abortion. The function reads the |
| 2121 | // 1st block of each partition and check for mounting time/count. It return string "t" |
| 2122 | // if executes successfully and an empty string otherwise. |
| 2123 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 2124 | Value* CheckFirstBlockFn(const char* name, State* state, |
| 2125 | const std::vector<std::unique_ptr<Expr>>& argv) { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2126 | if (argv.size() != 1) { |
| 2127 | ErrorAbort(state, kArgsParsingFailure, "check_first_block expects 1 argument, got %zu", |
| 2128 | argv.size()); |
| 2129 | return StringValue(""); |
| 2130 | } |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 2131 | |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2132 | std::vector<std::unique_ptr<Value>> args; |
| 2133 | if (!ReadValueArgs(state, argv, &args)) { |
| 2134 | return nullptr; |
| 2135 | } |
Tianjie Xu | 57bed6d | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 2136 | |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2137 | const std::unique_ptr<Value>& arg_filename = args[0]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 2138 | |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 2139 | if (arg_filename->type != Value::Type::STRING) { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2140 | ErrorAbort(state, kArgsParsingFailure, "filename argument to %s must be string", name); |
| 2141 | return StringValue(""); |
| 2142 | } |
Tianjie Xu | 57bed6d | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 2143 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 2144 | android::base::unique_fd fd(open(arg_filename->data.c_str(), O_RDONLY)); |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2145 | if (fd == -1) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 2146 | CauseCode cause_code = errno == EIO ? kEioFailure : kFileOpenFailure; |
| 2147 | ErrorAbort(state, cause_code, "open \"%s\" failed: %s", arg_filename->data.c_str(), |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2148 | strerror(errno)); |
| 2149 | return StringValue(""); |
| 2150 | } |
Tianjie Xu | 57bed6d | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 2151 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 2152 | RangeSet blk0(std::vector<Range>{ Range{ 0, 1 } }); |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2153 | std::vector<uint8_t> block0_buffer(BLOCKSIZE); |
Tianjie Xu | 57bed6d | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 2154 | |
Tao Bao | de3bbb8 | 2018-05-30 16:14:14 -0700 | [diff] [blame] | 2155 | if (ReadBlocks(blk0, &block0_buffer, fd) == -1) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 2156 | CauseCode cause_code = errno == EIO ? kEioFailure : kFreadFailure; |
| 2157 | ErrorAbort(state, cause_code, "failed to read %s: %s", arg_filename->data.c_str(), |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2158 | strerror(errno)); |
| 2159 | return StringValue(""); |
| 2160 | } |
Tianjie Xu | 57bed6d | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 2161 | |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2162 | // https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout |
| 2163 | // Super block starts from block 0, offset 0x400 |
| 2164 | // 0x2C: len32 Mount time |
| 2165 | // 0x30: len32 Write time |
| 2166 | // 0x34: len16 Number of mounts since the last fsck |
| 2167 | // 0x38: len16 Magic signature 0xEF53 |
Tianjie Xu | 57bed6d | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 2168 | |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2169 | time_t mount_time = *reinterpret_cast<uint32_t*>(&block0_buffer[0x400 + 0x2C]); |
| 2170 | uint16_t mount_count = *reinterpret_cast<uint16_t*>(&block0_buffer[0x400 + 0x34]); |
Tianjie Xu | 57bed6d | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 2171 | |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2172 | if (mount_count > 0) { |
| 2173 | uiPrintf(state, "Device was remounted R/W %" PRIu16 " times", mount_count); |
| 2174 | uiPrintf(state, "Last remount happened on %s", ctime(&mount_time)); |
| 2175 | } |
Tianjie Xu | 57bed6d | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 2176 | |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2177 | return StringValue("t"); |
Tianjie Xu | 57bed6d | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 2178 | } |
| 2179 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 2180 | Value* BlockImageRecoverFn(const char* name, State* state, |
| 2181 | const std::vector<std::unique_ptr<Expr>>& argv) { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2182 | if (argv.size() != 2) { |
| 2183 | ErrorAbort(state, kArgsParsingFailure, "block_image_recover expects 2 arguments, got %zu", |
| 2184 | argv.size()); |
| 2185 | return StringValue(""); |
| 2186 | } |
| 2187 | |
| 2188 | std::vector<std::unique_ptr<Value>> args; |
| 2189 | if (!ReadValueArgs(state, argv, &args)) { |
| 2190 | return nullptr; |
| 2191 | } |
| 2192 | |
| 2193 | const std::unique_ptr<Value>& filename = args[0]; |
| 2194 | const std::unique_ptr<Value>& ranges = args[1]; |
| 2195 | |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 2196 | if (filename->type != Value::Type::STRING) { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2197 | ErrorAbort(state, kArgsParsingFailure, "filename argument to %s must be string", name); |
| 2198 | return StringValue(""); |
| 2199 | } |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 2200 | if (ranges->type != Value::Type::STRING) { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2201 | ErrorAbort(state, kArgsParsingFailure, "ranges argument to %s must be string", name); |
| 2202 | return StringValue(""); |
| 2203 | } |
Tao Bao | 6798315 | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 2204 | RangeSet rs = RangeSet::Parse(ranges->data); |
| 2205 | if (!rs) { |
| 2206 | ErrorAbort(state, kArgsParsingFailure, "failed to parse ranges: %s", ranges->data.c_str()); |
| 2207 | return StringValue(""); |
| 2208 | } |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2209 | |
| 2210 | // Output notice to log when recover is attempted |
| 2211 | LOG(INFO) << filename->data << " image corrupted, attempting to recover..."; |
| 2212 | |
| 2213 | // When opened with O_RDWR, libfec rewrites corrupted blocks when they are read |
| 2214 | fec::io fh(filename->data, O_RDWR); |
| 2215 | |
| 2216 | if (!fh) { |
| 2217 | ErrorAbort(state, kLibfecFailure, "fec_open \"%s\" failed: %s", filename->data.c_str(), |
| 2218 | strerror(errno)); |
| 2219 | return StringValue(""); |
| 2220 | } |
| 2221 | |
| 2222 | if (!fh.has_ecc() || !fh.has_verity()) { |
| 2223 | ErrorAbort(state, kLibfecFailure, "unable to use metadata to correct errors"); |
| 2224 | return StringValue(""); |
| 2225 | } |
| 2226 | |
| 2227 | fec_status status; |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2228 | if (!fh.get_status(status)) { |
| 2229 | ErrorAbort(state, kLibfecFailure, "failed to read FEC status"); |
| 2230 | return StringValue(""); |
| 2231 | } |
| 2232 | |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2233 | uint8_t buffer[BLOCKSIZE]; |
Tao Bao | 43bfa6e | 2018-08-28 10:09:13 -0700 | [diff] [blame] | 2234 | for (const auto& [begin, end] : rs) { |
| 2235 | for (size_t j = begin; j < end; ++j) { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2236 | // Stay within the data area, libfec validates and corrects metadata |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 2237 | if (status.data_size <= static_cast<uint64_t>(j) * BLOCKSIZE) { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2238 | continue; |
| 2239 | } |
| 2240 | |
Tao Bao | bf5b77d | 2017-03-30 16:57:29 -0700 | [diff] [blame] | 2241 | if (fh.pread(buffer, BLOCKSIZE, static_cast<off64_t>(j) * BLOCKSIZE) != BLOCKSIZE) { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2242 | ErrorAbort(state, kLibfecFailure, "failed to recover %s (block %zu): %s", |
| 2243 | filename->data.c_str(), j, strerror(errno)); |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 2244 | return StringValue(""); |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | // If we want to be able to recover from a situation where rewriting a corrected |
| 2248 | // block doesn't guarantee the same data will be returned when re-read later, we |
| 2249 | // can save a copy of corrected blocks to /cache. Note: |
| 2250 | // |
| 2251 | // 1. Maximum space required from /cache is the same as the maximum number of |
| 2252 | // corrupted blocks we can correct. For RS(255, 253) and a 2 GiB partition, |
| 2253 | // this would be ~16 MiB, for example. |
| 2254 | // |
| 2255 | // 2. To find out if this block was corrupted, call fec_get_status after each |
| 2256 | // read and check if the errors field value has increased. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 2257 | } |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2258 | } |
| 2259 | LOG(INFO) << "..." << filename->data << " image recovered successfully."; |
| 2260 | return StringValue("t"); |
Sami Tolvanen | 0a7b473 | 2015-06-25 10:25:36 +0100 | [diff] [blame] | 2261 | } |
| 2262 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 2263 | void RegisterBlockImageFunctions() { |
Tao Bao | c97edcb | 2017-03-31 01:18:13 -0700 | [diff] [blame] | 2264 | RegisterFunction("block_image_verify", BlockImageVerifyFn); |
| 2265 | RegisterFunction("block_image_update", BlockImageUpdateFn); |
| 2266 | RegisterFunction("block_image_recover", BlockImageRecoverFn); |
| 2267 | RegisterFunction("check_first_block", CheckFirstBlockFn); |
| 2268 | RegisterFunction("range_sha1", RangeSha1Fn); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 2269 | } |