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