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