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