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