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