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