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