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