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