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