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