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