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