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