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