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