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