blob: a7821f36225f822509afe06f8bf43f6c584a82f4 [file] [log] [blame]
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001/*
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 Tolvanen90221202014-12-09 16:39:47 +000019#include <dirent.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070020#include <fcntl.h>
21#include <inttypes.h>
Tao Baodc392262015-07-31 15:56:44 -070022#include <libgen.h>
Tao Baoba9a42a2015-06-23 23:23:33 -070023#include <linux/fs.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070024#include <pthread.h>
25#include <stdarg.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Sami Tolvanen90221202014-12-09 16:39:47 +000029#include <sys/stat.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070030#include <sys/types.h>
31#include <sys/wait.h>
32#include <sys/ioctl.h>
33#include <time.h>
34#include <unistd.h>
35
36#include "applypatch/applypatch.h"
37#include "edify/expr.h"
38#include "mincrypt/sha.h"
Sami Tolvanen90221202014-12-09 16:39:47 +000039#include "minzip/Hash.h"
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070040#include "updater.h"
41
42#define BLOCKSIZE 4096
43
Sami Tolvanene82fa182015-06-10 15:58:12 +000044// Set this to 0 to interpret 'erase' transfers to mean do a
45// BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret
46// erase to mean fill the region with zeroes.
47#define DEBUG_ERASE 0
48
Sami Tolvanen90221202014-12-09 16:39:47 +000049#define STASH_DIRECTORY_BASE "/cache/recovery"
50#define STASH_DIRECTORY_MODE 0700
51#define STASH_FILE_MODE 0600
52
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070053char* PrintSha1(const uint8_t* digest);
54
55typedef struct {
56 int count;
57 int size;
58 int pos[0];
59} RangeSet;
60
Sami Tolvanenf2bac042015-05-12 12:48:46 +010061#define RANGESET_MAX_POINTS \
62 ((int)((INT_MAX / sizeof(int)) - sizeof(RangeSet)))
63
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070064static RangeSet* parse_range(char* text) {
65 char* save;
Sami Tolvanenf2bac042015-05-12 12:48:46 +010066 char* token;
Tao Baoba9a42a2015-06-23 23:23:33 -070067 int num;
Sami Tolvanenf2bac042015-05-12 12:48:46 +010068 long int val;
69 RangeSet* out = NULL;
70 size_t bufsize;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070071
Sami Tolvanenf2bac042015-05-12 12:48:46 +010072 if (!text) {
73 goto err;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070074 }
Sami Tolvanenf2bac042015-05-12 12:48:46 +010075
76 token = strtok_r(text, ",", &save);
77
78 if (!token) {
79 goto err;
80 }
81
82 val = strtol(token, NULL, 0);
83
84 if (val < 2 || val > RANGESET_MAX_POINTS) {
85 goto err;
86 } else if (val % 2) {
87 goto err; // must be even
88 }
89
90 num = (int) val;
91 bufsize = sizeof(RangeSet) + num * sizeof(int);
92
Tao Baoba9a42a2015-06-23 23:23:33 -070093 out = reinterpret_cast<RangeSet*>(malloc(bufsize));
Sami Tolvanenf2bac042015-05-12 12:48:46 +010094
95 if (!out) {
96 fprintf(stderr, "failed to allocate range of %zu bytes\n", bufsize);
97 goto err;
98 }
99
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700100 out->count = num / 2;
101 out->size = 0;
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100102
Tao Baoba9a42a2015-06-23 23:23:33 -0700103 for (int i = 0; i < num; ++i) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100104 token = strtok_r(NULL, ",", &save);
105
106 if (!token) {
107 goto err;
108 }
109
110 val = strtol(token, NULL, 0);
111
112 if (val < 0 || val > INT_MAX) {
113 goto err;
114 }
115
116 out->pos[i] = (int) val;
117
118 if (i % 2) {
119 if (out->pos[i - 1] >= out->pos[i]) {
120 goto err; // empty or negative range
121 }
122
123 if (out->size > INT_MAX - out->pos[i]) {
124 goto err; // overflow
125 }
126
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700127 out->size += out->pos[i];
128 } else {
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100129 if (out->size < 0) {
130 goto err;
131 }
132
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700133 out->size -= out->pos[i];
134 }
135 }
136
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100137 if (out->size <= 0) {
138 goto err;
139 }
140
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700141 return out;
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100142
143err:
144 fprintf(stderr, "failed to parse range '%s'\n", text ? text : "NULL");
145 exit(1);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700146}
147
Sami Tolvanen90221202014-12-09 16:39:47 +0000148static int range_overlaps(RangeSet* r1, RangeSet* r2) {
149 int i, j, r1_0, r1_1, r2_0, r2_1;
150
151 if (!r1 || !r2) {
152 return 0;
153 }
154
155 for (i = 0; i < r1->count; ++i) {
156 r1_0 = r1->pos[i * 2];
157 r1_1 = r1->pos[i * 2 + 1];
158
159 for (j = 0; j < r2->count; ++j) {
160 r2_0 = r2->pos[j * 2];
161 r2_1 = r2->pos[j * 2 + 1];
162
Tao Baoc0f56ad2015-06-25 14:00:31 -0700163 if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000164 return 1;
165 }
166 }
167 }
168
169 return 0;
170}
171
172static int read_all(int fd, uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700173 size_t so_far = 0;
174 while (so_far < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700175 ssize_t r = TEMP_FAILURE_RETRY(read(fd, data+so_far, size-so_far));
176 if (r == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700177 fprintf(stderr, "read failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000178 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700179 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700180 so_far += r;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700181 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000182 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700183}
184
Sami Tolvanen90221202014-12-09 16:39:47 +0000185static int write_all(int fd, const uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700186 size_t written = 0;
187 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700188 ssize_t w = TEMP_FAILURE_RETRY(write(fd, data+written, size-written));
189 if (w == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700190 fprintf(stderr, "write failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000191 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700192 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700193 written += w;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700194 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000195
Sami Tolvanen90221202014-12-09 16:39:47 +0000196 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700197}
198
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700199static bool check_lseek(int fd, off64_t offset, int whence) {
200 off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
201 if (rc == -1) {
202 fprintf(stderr, "lseek64 failed: %s\n", strerror(errno));
203 return false;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700204 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700205 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700206}
207
208static void allocate(size_t size, uint8_t** buffer, size_t* buffer_alloc) {
209 // if the buffer's big enough, reuse it.
210 if (size <= *buffer_alloc) return;
211
212 free(*buffer);
213
214 *buffer = (uint8_t*) malloc(size);
215 if (*buffer == NULL) {
Doug Zongker1d5d6092014-08-21 10:47:24 -0700216 fprintf(stderr, "failed to allocate %zu bytes\n", size);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700217 exit(1);
218 }
219 *buffer_alloc = size;
220}
221
222typedef struct {
223 int fd;
224 RangeSet* tgt;
225 int p_block;
226 size_t p_remain;
227} RangeSinkState;
228
229static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) {
230 RangeSinkState* rss = (RangeSinkState*) token;
231
232 if (rss->p_remain <= 0) {
233 fprintf(stderr, "range sink write overrun");
Sami Tolvanen90221202014-12-09 16:39:47 +0000234 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700235 }
236
237 ssize_t written = 0;
238 while (size > 0) {
239 size_t write_now = size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000240
241 if (rss->p_remain < write_now) {
242 write_now = rss->p_remain;
243 }
244
245 if (write_all(rss->fd, data, write_now) == -1) {
246 break;
247 }
248
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700249 data += write_now;
250 size -= write_now;
251
252 rss->p_remain -= write_now;
253 written += write_now;
254
255 if (rss->p_remain == 0) {
256 // move to the next block
257 ++rss->p_block;
258 if (rss->p_block < rss->tgt->count) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000259 rss->p_remain = (rss->tgt->pos[rss->p_block * 2 + 1] -
260 rss->tgt->pos[rss->p_block * 2]) * BLOCKSIZE;
261
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700262 if (!check_lseek(rss->fd, (off64_t)rss->tgt->pos[rss->p_block*2] * BLOCKSIZE,
263 SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000264 break;
265 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700266 } else {
267 // we can't write any more; return how many bytes have
268 // been written so far.
Sami Tolvanen90221202014-12-09 16:39:47 +0000269 break;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700270 }
271 }
272 }
273
274 return written;
275}
276
277// All of the data for all the 'new' transfers is contained in one
278// file in the update package, concatenated together in the order in
279// which transfers.list will need it. We want to stream it out of the
280// archive (it's compressed) without writing it to a temp file, but we
281// can't write each section until it's that transfer's turn to go.
282//
283// To achieve this, we expand the new data from the archive in a
284// background thread, and block that threads 'receive uncompressed
285// data' function until the main thread has reached a point where we
286// want some new data to be written. We signal the background thread
287// with the destination for the data and block the main thread,
288// waiting for the background thread to complete writing that section.
289// Then it signals the main thread to wake up and goes back to
290// blocking waiting for a transfer.
291//
292// NewThreadInfo is the struct used to pass information back and forth
293// between the two threads. When the main thread wants some data
294// written, it sets rss to the destination location and signals the
295// condition. When the background thread is done writing, it clears
296// rss and signals the condition again.
297
298typedef struct {
299 ZipArchive* za;
300 const ZipEntry* entry;
301
302 RangeSinkState* rss;
303
304 pthread_mutex_t mu;
305 pthread_cond_t cv;
306} NewThreadInfo;
307
308static bool receive_new_data(const unsigned char* data, int size, void* cookie) {
309 NewThreadInfo* nti = (NewThreadInfo*) cookie;
310
311 while (size > 0) {
312 // Wait for nti->rss to be non-NULL, indicating some of this
313 // data is wanted.
314 pthread_mutex_lock(&nti->mu);
315 while (nti->rss == NULL) {
316 pthread_cond_wait(&nti->cv, &nti->mu);
317 }
318 pthread_mutex_unlock(&nti->mu);
319
320 // At this point nti->rss is set, and we own it. The main
321 // thread is waiting for it to disappear from nti.
322 ssize_t written = RangeSinkWrite(data, size, nti->rss);
323 data += written;
324 size -= written;
325
326 if (nti->rss->p_block == nti->rss->tgt->count) {
327 // we have written all the bytes desired by this rss.
328
329 pthread_mutex_lock(&nti->mu);
330 nti->rss = NULL;
331 pthread_cond_broadcast(&nti->cv);
332 pthread_mutex_unlock(&nti->mu);
333 }
334 }
335
336 return true;
337}
338
339static void* unzip_new_data(void* cookie) {
340 NewThreadInfo* nti = (NewThreadInfo*) cookie;
341 mzProcessZipEntryContents(nti->za, nti->entry, receive_new_data, nti);
342 return NULL;
343}
344
Sami Tolvanen90221202014-12-09 16:39:47 +0000345static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) {
346 int i;
347 size_t p = 0;
348 size_t size;
349
350 if (!src || !buffer) {
351 return -1;
352 }
353
354 for (i = 0; i < src->count; ++i) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700355 if (!check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000356 return -1;
357 }
358
359 size = (src->pos[i * 2 + 1] - src->pos[i * 2]) * BLOCKSIZE;
360
361 if (read_all(fd, buffer + p, size) == -1) {
362 return -1;
363 }
364
365 p += size;
366 }
367
368 return 0;
369}
370
371static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) {
372 int i;
373 size_t p = 0;
374 size_t size;
375
376 if (!tgt || !buffer) {
377 return -1;
378 }
379
380 for (i = 0; i < tgt->count; ++i) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700381 if (!check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000382 return -1;
383 }
384
385 size = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * BLOCKSIZE;
386
387 if (write_all(fd, buffer + p, size) == -1) {
388 return -1;
389 }
390
391 p += size;
392 }
393
394 return 0;
395}
396
Doug Zongker52ae67d2014-09-08 12:22:09 -0700397// Do a source/target load for move/bsdiff/imgdiff in version 1.
398// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
399// to parse the remainder of the string as:
400//
401// <src_range> <tgt_range>
402//
403// The source range is loaded into the provided buffer, reallocating
404// it to make it larger if necessary. The target ranges are returned
405// in *tgt, if tgt is non-NULL.
406
Sami Tolvanen90221202014-12-09 16:39:47 +0000407static int LoadSrcTgtVersion1(char** wordsave, RangeSet** tgt, int* src_blocks,
Doug Zongker52ae67d2014-09-08 12:22:09 -0700408 uint8_t** buffer, size_t* buffer_alloc, int fd) {
409 char* word;
Sami Tolvanen90221202014-12-09 16:39:47 +0000410 int rc;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700411
Sami Tolvanen90221202014-12-09 16:39:47 +0000412 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700413 RangeSet* src = parse_range(word);
414
415 if (tgt != NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000416 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700417 *tgt = parse_range(word);
418 }
419
420 allocate(src->size * BLOCKSIZE, buffer, buffer_alloc);
Sami Tolvanen90221202014-12-09 16:39:47 +0000421 rc = ReadBlocks(src, *buffer, fd);
422 *src_blocks = src->size;
423
424 free(src);
425 return rc;
426}
427
428static int VerifyBlocks(const char *expected, const uint8_t *buffer,
429 size_t blocks, int printerror) {
430 char* hexdigest = NULL;
431 int rc = -1;
432 uint8_t digest[SHA_DIGEST_SIZE];
433
434 if (!expected || !buffer) {
435 return rc;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700436 }
437
Sami Tolvanen90221202014-12-09 16:39:47 +0000438 SHA_hash(buffer, blocks * BLOCKSIZE, digest);
439 hexdigest = PrintSha1(digest);
440
441 if (hexdigest != NULL) {
442 rc = strcmp(expected, hexdigest);
443
444 if (rc != 0 && printerror) {
445 fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
446 expected, hexdigest);
447 }
448
449 free(hexdigest);
450 }
451
452 return rc;
453}
454
455static char* GetStashFileName(const char* base, const char* id, const char* postfix) {
456 char* fn;
457 int len;
458 int res;
459
460 if (base == NULL) {
461 return NULL;
462 }
463
464 if (id == NULL) {
465 id = "";
466 }
467
468 if (postfix == NULL) {
469 postfix = "";
470 }
471
472 len = strlen(STASH_DIRECTORY_BASE) + 1 + strlen(base) + 1 + strlen(id) + strlen(postfix) + 1;
Tao Baoba9a42a2015-06-23 23:23:33 -0700473 fn = reinterpret_cast<char*>(malloc(len));
Sami Tolvanen90221202014-12-09 16:39:47 +0000474
475 if (fn == NULL) {
476 fprintf(stderr, "failed to malloc %d bytes for fn\n", len);
477 return NULL;
478 }
479
480 res = snprintf(fn, len, STASH_DIRECTORY_BASE "/%s/%s%s", base, id, postfix);
481
482 if (res < 0 || res >= len) {
483 fprintf(stderr, "failed to format file name (return value %d)\n", res);
484 free(fn);
485 return NULL;
486 }
487
488 return fn;
489}
490
491typedef void (*StashCallback)(const char*, void*);
492
493// Does a best effort enumeration of stash files. Ignores possible non-file
494// items in the stash directory and continues despite of errors. Calls the
495// 'callback' function for each file and passes 'data' to the function as a
496// parameter.
497
498static void EnumerateStash(const char* dirname, StashCallback callback, void* data) {
499 char* fn;
500 DIR* directory;
501 int len;
502 int res;
503 struct dirent* item;
504
505 if (dirname == NULL || callback == NULL) {
506 return;
507 }
508
509 directory = opendir(dirname);
510
511 if (directory == NULL) {
512 if (errno != ENOENT) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700513 fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000514 }
515 return;
516 }
517
518 while ((item = readdir(directory)) != NULL) {
519 if (item->d_type != DT_REG) {
520 continue;
521 }
522
523 len = strlen(dirname) + 1 + strlen(item->d_name) + 1;
Tao Baoba9a42a2015-06-23 23:23:33 -0700524 fn = reinterpret_cast<char*>(malloc(len));
Sami Tolvanen90221202014-12-09 16:39:47 +0000525
526 if (fn == NULL) {
527 fprintf(stderr, "failed to malloc %d bytes for fn\n", len);
528 continue;
529 }
530
531 res = snprintf(fn, len, "%s/%s", dirname, item->d_name);
532
533 if (res < 0 || res >= len) {
534 fprintf(stderr, "failed to format file name (return value %d)\n", res);
535 free(fn);
536 continue;
537 }
538
539 callback(fn, data);
540 free(fn);
541 }
542
543 if (closedir(directory) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700544 fprintf(stderr, "closedir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000545 }
546}
547
548static void UpdateFileSize(const char* fn, void* data) {
549 int* size = (int*) data;
550 struct stat st;
551
552 if (!fn || !data) {
553 return;
554 }
555
556 if (stat(fn, &st) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700557 fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000558 return;
559 }
560
561 *size += st.st_size;
562}
563
564// Deletes the stash directory and all files in it. Assumes that it only
565// contains files. There is nothing we can do about unlikely, but possible
566// errors, so they are merely logged.
567
568static void DeleteFile(const char* fn, void* data) {
569 if (fn) {
570 fprintf(stderr, "deleting %s\n", fn);
571
572 if (unlink(fn) == -1 && errno != ENOENT) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700573 fprintf(stderr, "unlink \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000574 }
575 }
576}
577
578static void DeletePartial(const char* fn, void* data) {
579 if (fn && strstr(fn, ".partial") != NULL) {
580 DeleteFile(fn, data);
581 }
582}
583
584static void DeleteStash(const char* base) {
585 char* dirname;
586
587 if (base == NULL) {
588 return;
589 }
590
591 dirname = GetStashFileName(base, NULL, NULL);
592
593 if (dirname == NULL) {
594 return;
595 }
596
597 fprintf(stderr, "deleting stash %s\n", base);
598 EnumerateStash(dirname, DeleteFile, NULL);
599
600 if (rmdir(dirname) == -1) {
601 if (errno != ENOENT && errno != ENOTDIR) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700602 fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000603 }
604 }
605
606 free(dirname);
607}
608
609static int LoadStash(const char* base, const char* id, int verify, int* blocks, uint8_t** buffer,
610 size_t* buffer_alloc, int printnoent) {
611 char *fn = NULL;
612 int blockcount = 0;
613 int fd = -1;
614 int rc = -1;
615 int res;
616 struct stat st;
617
618 if (!base || !id || !buffer || !buffer_alloc) {
619 goto lsout;
620 }
621
622 if (!blocks) {
623 blocks = &blockcount;
624 }
625
626 fn = GetStashFileName(base, id, NULL);
627
628 if (fn == NULL) {
629 goto lsout;
630 }
631
632 res = stat(fn, &st);
633
634 if (res == -1) {
635 if (errno != ENOENT || printnoent) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700636 fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000637 }
638 goto lsout;
639 }
640
641 fprintf(stderr, " loading %s\n", fn);
642
643 if ((st.st_size % BLOCKSIZE) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700644 fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d",
645 fn, static_cast<int64_t>(st.st_size), BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000646 goto lsout;
647 }
648
649 fd = TEMP_FAILURE_RETRY(open(fn, O_RDONLY));
650
651 if (fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700652 fprintf(stderr, "open \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000653 goto lsout;
654 }
655
656 allocate(st.st_size, buffer, buffer_alloc);
657
658 if (read_all(fd, *buffer, st.st_size) == -1) {
659 goto lsout;
660 }
661
662 *blocks = st.st_size / BLOCKSIZE;
663
664 if (verify && VerifyBlocks(id, *buffer, *blocks, 1) != 0) {
665 fprintf(stderr, "unexpected contents in %s\n", fn);
666 DeleteFile(fn, NULL);
667 goto lsout;
668 }
669
670 rc = 0;
671
672lsout:
673 if (fd != -1) {
Elliott Hughesb3ac6762015-05-28 23:06:17 -0700674 close(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000675 }
676
677 if (fn) {
678 free(fn);
679 }
680
681 return rc;
682}
683
684static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buffer,
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100685 int checkspace, int *exists) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000686 char *fn = NULL;
687 char *cn = NULL;
688 int fd = -1;
689 int rc = -1;
Tao Baodc392262015-07-31 15:56:44 -0700690 int dfd = -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000691 int res;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100692 struct stat st;
Sami Tolvanen90221202014-12-09 16:39:47 +0000693
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 Tolvanen43b748f2015-04-17 12:50:31 +0100710 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 Tolvanen90221202014-12-09 16:39:47 +0000726 fprintf(stderr, " writing %d blocks to %s\n", blocks, cn);
727
Tao Bao187efff2015-07-27 14:07:08 -0700728 fd = TEMP_FAILURE_RETRY(open(fn, O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE));
Sami Tolvanen90221202014-12-09 16:39:47 +0000729
730 if (fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700731 fprintf(stderr, "failed to create \"%s\": %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000732 goto wsout;
733 }
734
735 if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) {
736 goto wsout;
737 }
738
739 if (fsync(fd) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700740 fprintf(stderr, "fsync \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000741 goto wsout;
742 }
743
744 if (rename(fn, cn) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700745 fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn, cn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000746 goto wsout;
747 }
748
Tao Baodc392262015-07-31 15:56:44 -0700749 const char* dname;
750 dname = dirname(cn);
751 dfd = TEMP_FAILURE_RETRY(open(dname, O_RDONLY | O_DIRECTORY));
752
753 if (dfd == -1) {
754 fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname, strerror(errno));
755 goto wsout;
756 }
757
758 if (fsync(dfd) == -1) {
759 fprintf(stderr, "fsync \"%s\" failed: %s\n", dname, strerror(errno));
760 goto wsout;
761 }
762
Sami Tolvanen90221202014-12-09 16:39:47 +0000763 rc = 0;
764
765wsout:
766 if (fd != -1) {
Elliott Hughesb47afed2015-05-15 16:19:20 -0700767 close(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000768 }
769
Tao Baodc392262015-07-31 15:56:44 -0700770 if (dfd != -1) {
771 close(dfd);
772 }
773
Sami Tolvanen90221202014-12-09 16:39:47 +0000774 if (fn) {
775 free(fn);
776 }
777
778 if (cn) {
779 free(cn);
780 }
781
782 return rc;
783}
784
785// Creates a directory for storing stash files and checks if the /cache partition
786// hash enough space for the expected amount of blocks we need to store. Returns
787// >0 if we created the directory, zero if it existed already, and <0 of failure.
788
789static int CreateStash(State* state, int maxblocks, const char* blockdev, char** base) {
790 char* dirname = NULL;
791 const uint8_t* digest;
792 int rc = -1;
793 int res;
794 int size = 0;
795 SHA_CTX ctx;
796 struct stat st;
797
798 if (blockdev == NULL || base == NULL) {
799 goto csout;
800 }
801
802 // Stash directory should be different for each partition to avoid conflicts
803 // when updating multiple partitions at the same time, so we use the hash of
804 // the block device name as the base directory
805 SHA_init(&ctx);
806 SHA_update(&ctx, blockdev, strlen(blockdev));
807 digest = SHA_final(&ctx);
808 *base = PrintSha1(digest);
809
810 if (*base == NULL) {
811 goto csout;
812 }
813
814 dirname = GetStashFileName(*base, NULL, NULL);
815
816 if (dirname == NULL) {
817 goto csout;
818 }
819
820 res = stat(dirname, &st);
821
822 if (res == -1 && errno != ENOENT) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700823 ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000824 goto csout;
825 } else if (res != 0) {
826 fprintf(stderr, "creating stash %s\n", dirname);
827 res = mkdir(dirname, STASH_DIRECTORY_MODE);
828
829 if (res != 0) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700830 ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000831 goto csout;
832 }
833
834 if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) {
835 ErrorAbort(state, "not enough space for stash\n");
836 goto csout;
837 }
838
839 rc = 1; // Created directory
840 goto csout;
841 }
842
843 fprintf(stderr, "using existing stash %s\n", dirname);
844
845 // If the directory already exists, calculate the space already allocated to
846 // stash files and check if there's enough for all required blocks. Delete any
847 // partially completed stash files first.
848
849 EnumerateStash(dirname, DeletePartial, NULL);
850 EnumerateStash(dirname, UpdateFileSize, &size);
851
852 size = (maxblocks * BLOCKSIZE) - size;
853
854 if (size > 0 && CacheSizeCheck(size) != 0) {
855 ErrorAbort(state, "not enough space for stash (%d more needed)\n", size);
856 goto csout;
857 }
858
859 rc = 0; // Using existing directory
860
861csout:
862 if (dirname) {
863 free(dirname);
864 }
865
866 return rc;
867}
868
869static int SaveStash(const char* base, char** wordsave, uint8_t** buffer, size_t* buffer_alloc,
870 int fd, int usehash, int* isunresumable) {
871 char *id = NULL;
Sami Tolvanen90221202014-12-09 16:39:47 +0000872 int blocks = 0;
873
874 if (!wordsave || !buffer || !buffer_alloc || !isunresumable) {
875 return -1;
876 }
877
878 id = strtok_r(NULL, " ", wordsave);
879
880 if (id == NULL) {
881 fprintf(stderr, "missing id field in stash command\n");
882 return -1;
883 }
884
885 if (usehash && LoadStash(base, id, 1, &blocks, buffer, buffer_alloc, 0) == 0) {
886 // Stash file already exists and has expected contents. Do not
887 // read from source again, as the source may have been already
888 // overwritten during a previous attempt.
889 return 0;
890 }
891
892 if (LoadSrcTgtVersion1(wordsave, NULL, &blocks, buffer, buffer_alloc, fd) == -1) {
893 return -1;
894 }
895
896 if (usehash && VerifyBlocks(id, *buffer, blocks, 1) != 0) {
897 // Source blocks have unexpected contents. If we actually need this
898 // data later, this is an unrecoverable error. However, the command
899 // that uses the data may have already completed previously, so the
900 // possible failure will occur during source block verification.
901 fprintf(stderr, "failed to load source blocks for stash %s\n", id);
902 return 0;
903 }
904
905 fprintf(stderr, "stashing %d blocks to %s\n", blocks, id);
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100906 return WriteStash(base, id, blocks, *buffer, 0, NULL);
Sami Tolvanen90221202014-12-09 16:39:47 +0000907}
908
909static int FreeStash(const char* base, const char* id) {
910 char *fn = NULL;
911
912 if (base == NULL || id == NULL) {
913 return -1;
914 }
915
916 fn = GetStashFileName(base, id, NULL);
917
918 if (fn == NULL) {
919 return -1;
920 }
921
922 DeleteFile(fn, NULL);
923 free(fn);
924
925 return 0;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700926}
927
928static void MoveRange(uint8_t* dest, RangeSet* locs, const uint8_t* source) {
929 // source contains packed data, which we want to move to the
930 // locations given in *locs in the dest buffer. source and dest
931 // may be the same buffer.
932
933 int start = locs->size;
934 int i;
935 for (i = locs->count-1; i >= 0; --i) {
936 int blocks = locs->pos[i*2+1] - locs->pos[i*2];
937 start -= blocks;
938 memmove(dest + (locs->pos[i*2] * BLOCKSIZE), source + (start * BLOCKSIZE),
939 blocks * BLOCKSIZE);
940 }
941}
942
943// Do a source/target load for move/bsdiff/imgdiff in version 2.
944// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
945// to parse the remainder of the string as one of:
946//
947// <tgt_range> <src_block_count> <src_range>
948// (loads data from source image only)
949//
950// <tgt_range> <src_block_count> - <[stash_id:stash_range] ...>
951// (loads data from stashes only)
952//
953// <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...>
954// (loads data from both source image and stashes)
955//
956// On return, buffer is filled with the loaded source data (rearranged
957// and combined with stashed data as necessary). buffer may be
958// reallocated if needed to accommodate the source data. *tgt is the
Sami Tolvanen90221202014-12-09 16:39:47 +0000959// target RangeSet. Any stashes required are loaded using LoadStash.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700960
Sami Tolvanen90221202014-12-09 16:39:47 +0000961static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks,
Doug Zongker52ae67d2014-09-08 12:22:09 -0700962 uint8_t** buffer, size_t* buffer_alloc, int fd,
Sami Tolvanen90221202014-12-09 16:39:47 +0000963 const char* stashbase, int* overlap) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700964 char* word;
Sami Tolvanen90221202014-12-09 16:39:47 +0000965 char* colonsave;
966 char* colon;
Sami Tolvanen90221202014-12-09 16:39:47 +0000967 int res;
968 RangeSet* locs;
969 size_t stashalloc = 0;
970 uint8_t* stash = NULL;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700971
972 if (tgt != NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000973 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700974 *tgt = parse_range(word);
975 }
976
Sami Tolvanen90221202014-12-09 16:39:47 +0000977 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700978 *src_blocks = strtol(word, NULL, 0);
979
980 allocate(*src_blocks * BLOCKSIZE, buffer, buffer_alloc);
981
Sami Tolvanen90221202014-12-09 16:39:47 +0000982 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700983 if (word[0] == '-' && word[1] == '\0') {
984 // no source ranges, only stashes
985 } else {
986 RangeSet* src = parse_range(word);
Sami Tolvanen90221202014-12-09 16:39:47 +0000987 res = ReadBlocks(src, *buffer, fd);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700988
Sami Tolvanen90221202014-12-09 16:39:47 +0000989 if (overlap && tgt) {
990 *overlap = range_overlaps(src, *tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700991 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000992
Doug Zongker52ae67d2014-09-08 12:22:09 -0700993 free(src);
994
Sami Tolvanen90221202014-12-09 16:39:47 +0000995 if (res == -1) {
996 return -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700997 }
998
Sami Tolvanen90221202014-12-09 16:39:47 +0000999 word = strtok_r(NULL, " ", wordsave);
1000 if (word == NULL) {
1001 // no stashes, only source range
1002 return 0;
1003 }
1004
1005 locs = parse_range(word);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001006 MoveRange(*buffer, locs, *buffer);
Sami Tolvanen90221202014-12-09 16:39:47 +00001007 free(locs);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001008 }
1009
Sami Tolvanen90221202014-12-09 16:39:47 +00001010 while ((word = strtok_r(NULL, " ", wordsave)) != NULL) {
Doug Zongker52ae67d2014-09-08 12:22:09 -07001011 // Each word is a an index into the stash table, a colon, and
1012 // then a rangeset describing where in the source block that
1013 // stashed data should go.
Sami Tolvanen90221202014-12-09 16:39:47 +00001014 colonsave = NULL;
1015 colon = strtok_r(word, ":", &colonsave);
1016
1017 res = LoadStash(stashbase, colon, 0, NULL, &stash, &stashalloc, 1);
1018
1019 if (res == -1) {
1020 // These source blocks will fail verification if used later, but we
1021 // will let the caller decide if this is a fatal failure
1022 fprintf(stderr, "failed to load stash %s\n", colon);
1023 continue;
1024 }
1025
Doug Zongker52ae67d2014-09-08 12:22:09 -07001026 colon = strtok_r(NULL, ":", &colonsave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001027 locs = parse_range(colon);
1028
1029 MoveRange(*buffer, locs, stash);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001030 free(locs);
1031 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001032
1033 if (stash) {
1034 free(stash);
1035 }
1036
1037 return 0;
1038}
1039
1040// Parameters for transfer list command functions
1041typedef struct {
1042 char* cmdname;
1043 char* cpos;
1044 char* freestash;
1045 char* stashbase;
1046 int canwrite;
1047 int createdstash;
1048 int fd;
1049 int foundwrites;
1050 int isunresumable;
1051 int version;
1052 int written;
1053 NewThreadInfo nti;
1054 pthread_t thread;
1055 size_t bufsize;
1056 uint8_t* buffer;
1057 uint8_t* patch_start;
1058} CommandParameters;
1059
1060// Do a source/target load for move/bsdiff/imgdiff in version 3.
1061//
1062// Parameters are the same as for LoadSrcTgtVersion2, except for 'onehash', which
1063// tells the function whether to expect separate source and targe block hashes, or
1064// if they are both the same and only one hash should be expected, and
1065// 'isunresumable', which receives a non-zero value if block verification fails in
1066// a way that the update cannot be resumed anymore.
1067//
1068// If the function is unable to load the necessary blocks or their contents don't
1069// match the hashes, the return value is -1 and the command should be aborted.
1070//
1071// If the return value is 1, the command has already been completed according to
1072// the contents of the target blocks, and should not be performed again.
1073//
1074// If the return value is 0, source blocks have expected content and the command
1075// can be performed.
1076
1077static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* src_blocks,
1078 int onehash, int* overlap) {
1079 char* srchash = NULL;
1080 char* tgthash = NULL;
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001081 int stash_exists = 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001082 int rc = -1;
1083 uint8_t* tgtbuffer = NULL;
1084
1085 if (!params|| !tgt || !src_blocks || !overlap) {
1086 goto v3out;
1087 }
1088
1089 srchash = strtok_r(NULL, " ", &params->cpos);
1090
1091 if (srchash == NULL) {
1092 fprintf(stderr, "missing source hash\n");
1093 goto v3out;
1094 }
1095
1096 if (onehash) {
1097 tgthash = srchash;
1098 } else {
1099 tgthash = strtok_r(NULL, " ", &params->cpos);
1100
1101 if (tgthash == NULL) {
1102 fprintf(stderr, "missing target hash\n");
1103 goto v3out;
1104 }
1105 }
1106
1107 if (LoadSrcTgtVersion2(&params->cpos, tgt, src_blocks, &params->buffer, &params->bufsize,
1108 params->fd, params->stashbase, overlap) == -1) {
1109 goto v3out;
1110 }
1111
1112 tgtbuffer = (uint8_t*) malloc((*tgt)->size * BLOCKSIZE);
1113
1114 if (tgtbuffer == NULL) {
1115 fprintf(stderr, "failed to allocate %d bytes\n", (*tgt)->size * BLOCKSIZE);
1116 goto v3out;
1117 }
1118
1119 if (ReadBlocks(*tgt, tgtbuffer, params->fd) == -1) {
1120 goto v3out;
1121 }
1122
1123 if (VerifyBlocks(tgthash, tgtbuffer, (*tgt)->size, 0) == 0) {
1124 // Target blocks already have expected content, command should be skipped
1125 rc = 1;
1126 goto v3out;
1127 }
1128
1129 if (VerifyBlocks(srchash, params->buffer, *src_blocks, 1) == 0) {
1130 // If source and target blocks overlap, stash the source blocks so we can
1131 // resume from possible write errors
1132 if (*overlap) {
1133 fprintf(stderr, "stashing %d overlapping blocks to %s\n", *src_blocks,
1134 srchash);
1135
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001136 if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, 1,
1137 &stash_exists) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001138 fprintf(stderr, "failed to stash overlapping source blocks\n");
1139 goto v3out;
1140 }
1141
1142 // Can be deleted when the write has completed
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001143 if (!stash_exists) {
1144 params->freestash = srchash;
1145 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001146 }
1147
1148 // Source blocks have expected content, command can proceed
1149 rc = 0;
1150 goto v3out;
1151 }
1152
1153 if (*overlap && LoadStash(params->stashbase, srchash, 1, NULL, &params->buffer,
1154 &params->bufsize, 1) == 0) {
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001155 // Overlapping source blocks were previously stashed, command can proceed.
1156 // We are recovering from an interrupted command, so we don't know if the
1157 // stash can safely be deleted after this command.
Sami Tolvanen90221202014-12-09 16:39:47 +00001158 rc = 0;
1159 goto v3out;
1160 }
1161
1162 // Valid source data not available, update cannot be resumed
1163 fprintf(stderr, "partition has unexpected contents\n");
1164 params->isunresumable = 1;
1165
1166v3out:
1167 if (tgtbuffer) {
1168 free(tgtbuffer);
1169 }
1170
1171 return rc;
1172}
1173
1174static int PerformCommandMove(CommandParameters* params) {
1175 int blocks = 0;
1176 int overlap = 0;
1177 int rc = -1;
1178 int status = 0;
1179 RangeSet* tgt = NULL;
1180
1181 if (!params) {
1182 goto pcmout;
1183 }
1184
1185 if (params->version == 1) {
1186 status = LoadSrcTgtVersion1(&params->cpos, &tgt, &blocks, &params->buffer,
1187 &params->bufsize, params->fd);
1188 } else if (params->version == 2) {
1189 status = LoadSrcTgtVersion2(&params->cpos, &tgt, &blocks, &params->buffer,
1190 &params->bufsize, params->fd, params->stashbase, NULL);
1191 } else if (params->version >= 3) {
1192 status = LoadSrcTgtVersion3(params, &tgt, &blocks, 1, &overlap);
1193 }
1194
1195 if (status == -1) {
1196 fprintf(stderr, "failed to read blocks for move\n");
1197 goto pcmout;
1198 }
1199
1200 if (status == 0) {
1201 params->foundwrites = 1;
1202 } else if (params->foundwrites) {
1203 fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname);
1204 }
1205
1206 if (params->canwrite) {
1207 if (status == 0) {
1208 fprintf(stderr, " moving %d blocks\n", blocks);
1209
1210 if (WriteBlocks(tgt, params->buffer, params->fd) == -1) {
1211 goto pcmout;
1212 }
1213 } else {
1214 fprintf(stderr, "skipping %d already moved blocks\n", blocks);
1215 }
1216
1217 }
1218
1219 if (params->freestash) {
1220 FreeStash(params->stashbase, params->freestash);
1221 params->freestash = NULL;
1222 }
1223
1224 params->written += tgt->size;
1225 rc = 0;
1226
1227pcmout:
1228 if (tgt) {
1229 free(tgt);
1230 }
1231
1232 return rc;
1233}
1234
1235static int PerformCommandStash(CommandParameters* params) {
1236 if (!params) {
1237 return -1;
1238 }
1239
1240 return SaveStash(params->stashbase, &params->cpos, &params->buffer, &params->bufsize,
1241 params->fd, (params->version >= 3), &params->isunresumable);
1242}
1243
1244static int PerformCommandFree(CommandParameters* params) {
1245 if (!params) {
1246 return -1;
1247 }
1248
1249 if (params->createdstash || params->canwrite) {
1250 return FreeStash(params->stashbase, params->cpos);
1251 }
1252
1253 return 0;
1254}
1255
1256static int PerformCommandZero(CommandParameters* params) {
1257 char* range = NULL;
1258 int i;
1259 int j;
1260 int rc = -1;
1261 RangeSet* tgt = NULL;
1262
1263 if (!params) {
1264 goto pczout;
1265 }
1266
1267 range = strtok_r(NULL, " ", &params->cpos);
1268
1269 if (range == NULL) {
1270 fprintf(stderr, "missing target blocks for zero\n");
1271 goto pczout;
1272 }
1273
1274 tgt = parse_range(range);
1275
1276 fprintf(stderr, " zeroing %d blocks\n", tgt->size);
1277
1278 allocate(BLOCKSIZE, &params->buffer, &params->bufsize);
1279 memset(params->buffer, 0, BLOCKSIZE);
1280
1281 if (params->canwrite) {
1282 for (i = 0; i < tgt->count; ++i) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -07001283 if (!check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001284 goto pczout;
1285 }
1286
1287 for (j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) {
1288 if (write_all(params->fd, params->buffer, BLOCKSIZE) == -1) {
1289 goto pczout;
1290 }
1291 }
1292 }
1293 }
1294
1295 if (params->cmdname[0] == 'z') {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001296 // Update only for the zero command, as the erase command will call
1297 // this if DEBUG_ERASE is defined.
Sami Tolvanen90221202014-12-09 16:39:47 +00001298 params->written += tgt->size;
1299 }
1300
1301 rc = 0;
1302
1303pczout:
1304 if (tgt) {
1305 free(tgt);
1306 }
1307
1308 return rc;
1309}
1310
1311static int PerformCommandNew(CommandParameters* params) {
1312 char* range = NULL;
1313 int rc = -1;
1314 RangeSet* tgt = NULL;
1315 RangeSinkState rss;
1316
1317 if (!params) {
1318 goto pcnout;
1319 }
1320
1321 range = strtok_r(NULL, " ", &params->cpos);
1322
1323 if (range == NULL) {
1324 goto pcnout;
1325 }
1326
1327 tgt = parse_range(range);
1328
1329 if (params->canwrite) {
1330 fprintf(stderr, " writing %d blocks of new data\n", tgt->size);
1331
1332 rss.fd = params->fd;
1333 rss.tgt = tgt;
1334 rss.p_block = 0;
1335 rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
1336
Elliott Hughes7bad7c42015-04-28 17:24:24 -07001337 if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001338 goto pcnout;
1339 }
1340
1341 pthread_mutex_lock(&params->nti.mu);
1342 params->nti.rss = &rss;
1343 pthread_cond_broadcast(&params->nti.cv);
1344
1345 while (params->nti.rss) {
1346 pthread_cond_wait(&params->nti.cv, &params->nti.mu);
1347 }
1348
1349 pthread_mutex_unlock(&params->nti.mu);
1350 }
1351
1352 params->written += tgt->size;
1353 rc = 0;
1354
1355pcnout:
1356 if (tgt) {
1357 free(tgt);
1358 }
1359
1360 return rc;
1361}
1362
1363static int PerformCommandDiff(CommandParameters* params) {
1364 char* logparams = NULL;
1365 char* value = NULL;
1366 int blocks = 0;
1367 int overlap = 0;
1368 int rc = -1;
1369 int status = 0;
1370 RangeSet* tgt = NULL;
1371 RangeSinkState rss;
1372 size_t len = 0;
1373 size_t offset = 0;
1374 Value patch_value;
1375
1376 if (!params) {
1377 goto pcdout;
1378 }
1379
1380 logparams = strdup(params->cpos);
1381 value = strtok_r(NULL, " ", &params->cpos);
1382
1383 if (value == NULL) {
1384 fprintf(stderr, "missing patch offset for %s\n", params->cmdname);
1385 goto pcdout;
1386 }
1387
1388 offset = strtoul(value, NULL, 0);
1389
1390 value = strtok_r(NULL, " ", &params->cpos);
1391
1392 if (value == NULL) {
1393 fprintf(stderr, "missing patch length for %s\n", params->cmdname);
1394 goto pcdout;
1395 }
1396
1397 len = strtoul(value, NULL, 0);
1398
1399 if (params->version == 1) {
1400 status = LoadSrcTgtVersion1(&params->cpos, &tgt, &blocks, &params->buffer,
1401 &params->bufsize, params->fd);
1402 } else if (params->version == 2) {
1403 status = LoadSrcTgtVersion2(&params->cpos, &tgt, &blocks, &params->buffer,
1404 &params->bufsize, params->fd, params->stashbase, NULL);
1405 } else if (params->version >= 3) {
1406 status = LoadSrcTgtVersion3(params, &tgt, &blocks, 0, &overlap);
1407 }
1408
1409 if (status == -1) {
1410 fprintf(stderr, "failed to read blocks for diff\n");
1411 goto pcdout;
1412 }
1413
1414 if (status == 0) {
1415 params->foundwrites = 1;
1416 } else if (params->foundwrites) {
1417 fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname);
1418 }
1419
1420 if (params->canwrite) {
1421 if (status == 0) {
1422 fprintf(stderr, "patching %d blocks to %d\n", blocks, tgt->size);
1423
1424 patch_value.type = VAL_BLOB;
1425 patch_value.size = len;
1426 patch_value.data = (char*) (params->patch_start + offset);
1427
1428 rss.fd = params->fd;
1429 rss.tgt = tgt;
1430 rss.p_block = 0;
1431 rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
1432
Elliott Hughes7bad7c42015-04-28 17:24:24 -07001433 if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001434 goto pcdout;
1435 }
1436
1437 if (params->cmdname[0] == 'i') { // imgdiff
1438 ApplyImagePatch(params->buffer, blocks * BLOCKSIZE, &patch_value,
1439 &RangeSinkWrite, &rss, NULL, NULL);
1440 } else {
1441 ApplyBSDiffPatch(params->buffer, blocks * BLOCKSIZE, &patch_value,
1442 0, &RangeSinkWrite, &rss, NULL);
1443 }
1444
1445 // We expect the output of the patcher to fill the tgt ranges exactly.
1446 if (rss.p_block != tgt->count || rss.p_remain != 0) {
1447 fprintf(stderr, "range sink underrun?\n");
1448 }
1449 } else {
1450 fprintf(stderr, "skipping %d blocks already patched to %d [%s]\n",
1451 blocks, tgt->size, logparams);
1452 }
1453 }
1454
1455 if (params->freestash) {
1456 FreeStash(params->stashbase, params->freestash);
1457 params->freestash = NULL;
1458 }
1459
1460 params->written += tgt->size;
1461 rc = 0;
1462
1463pcdout:
1464 if (logparams) {
1465 free(logparams);
1466 }
1467
1468 if (tgt) {
1469 free(tgt);
1470 }
1471
1472 return rc;
1473}
1474
1475static int PerformCommandErase(CommandParameters* params) {
1476 char* range = NULL;
1477 int i;
1478 int rc = -1;
1479 RangeSet* tgt = NULL;
1480 struct stat st;
1481 uint64_t blocks[2];
1482
Sami Tolvanene82fa182015-06-10 15:58:12 +00001483 if (DEBUG_ERASE) {
1484 return PerformCommandZero(params);
Sami Tolvanen90221202014-12-09 16:39:47 +00001485 }
1486
1487 if (!params) {
1488 goto pceout;
1489 }
1490
1491 if (fstat(params->fd, &st) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001492 fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001493 goto pceout;
1494 }
1495
1496 if (!S_ISBLK(st.st_mode)) {
1497 fprintf(stderr, "not a block device; skipping erase\n");
Sami Tolvanen90221202014-12-09 16:39:47 +00001498 goto pceout;
1499 }
1500
1501 range = strtok_r(NULL, " ", &params->cpos);
1502
1503 if (range == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001504 fprintf(stderr, "missing target blocks for erase\n");
Sami Tolvanen90221202014-12-09 16:39:47 +00001505 goto pceout;
1506 }
1507
1508 tgt = parse_range(range);
1509
1510 if (params->canwrite) {
1511 fprintf(stderr, " erasing %d blocks\n", tgt->size);
1512
1513 for (i = 0; i < tgt->count; ++i) {
1514 // offset in bytes
1515 blocks[0] = tgt->pos[i * 2] * (uint64_t) BLOCKSIZE;
1516 // length in bytes
1517 blocks[1] = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * (uint64_t) BLOCKSIZE;
1518
1519 if (ioctl(params->fd, BLKDISCARD, &blocks) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001520 fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno));
Sami Tolvanencc2428c2015-04-27 11:24:29 +01001521 goto pceout;
Sami Tolvanen90221202014-12-09 16:39:47 +00001522 }
1523 }
1524 }
1525
1526 rc = 0;
1527
1528pceout:
1529 if (tgt) {
1530 free(tgt);
1531 }
1532
1533 return rc;
1534}
1535
1536// Definitions for transfer list command functions
1537typedef int (*CommandFunction)(CommandParameters*);
1538
1539typedef struct {
1540 const char* name;
1541 CommandFunction f;
1542} Command;
1543
1544// CompareCommands and CompareCommandNames are for the hash table
1545
1546static int CompareCommands(const void* c1, const void* c2) {
1547 return strcmp(((const Command*) c1)->name, ((const Command*) c2)->name);
1548}
1549
1550static int CompareCommandNames(const void* c1, const void* c2) {
1551 return strcmp(((const Command*) c1)->name, (const char*) c2);
1552}
1553
1554// HashString is used to hash command names for the hash table
1555
1556static unsigned int HashString(const char *s) {
1557 unsigned int hash = 0;
1558 if (s) {
1559 while (*s) {
1560 hash = hash * 33 + *s++;
1561 }
1562 }
1563 return hash;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001564}
1565
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001566// args:
1567// - block device (or file) to modify in-place
1568// - transfer list (blob)
1569// - new data stream (filename within package.zip)
1570// - patch stream (filename within package.zip, must be uncompressed)
1571
Sami Tolvanen90221202014-12-09 16:39:47 +00001572static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, Expr* argv[],
1573 const Command* commands, int cmdcount, int dryrun) {
1574
1575 char* line = NULL;
1576 char* linesave = NULL;
1577 char* logcmd = NULL;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001578 char* transfer_list = NULL;
Sami Tolvanen90221202014-12-09 16:39:47 +00001579 CommandParameters params;
1580 const Command* cmd = NULL;
1581 const ZipEntry* new_entry = NULL;
1582 const ZipEntry* patch_entry = NULL;
1583 FILE* cmd_pipe = NULL;
1584 HashTable* cmdht = NULL;
1585 int i;
1586 int res;
1587 int rc = -1;
1588 int stash_max_blocks = 0;
1589 int total_blocks = 0;
1590 pthread_attr_t attr;
1591 unsigned int cmdhash;
1592 UpdaterInfo* ui = NULL;
1593 Value* blockdev_filename = NULL;
1594 Value* new_data_fn = NULL;
1595 Value* patch_data_fn = NULL;
1596 Value* transfer_list_value = NULL;
1597 ZipArchive* za = NULL;
1598
1599 memset(&params, 0, sizeof(params));
1600 params.canwrite = !dryrun;
1601
1602 fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001603
Doug Zongker1d5d6092014-08-21 10:47:24 -07001604 if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value,
Sami Tolvanen90221202014-12-09 16:39:47 +00001605 &new_data_fn, &patch_data_fn) < 0) {
1606 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001607 }
1608
1609 if (blockdev_filename->type != VAL_STRING) {
1610 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001611 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001612 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001613 if (transfer_list_value->type != VAL_BLOB) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001614 ErrorAbort(state, "transfer_list argument to %s must be blob", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001615 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001616 }
1617 if (new_data_fn->type != VAL_STRING) {
1618 ErrorAbort(state, "new_data_fn argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001619 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001620 }
1621 if (patch_data_fn->type != VAL_STRING) {
1622 ErrorAbort(state, "patch_data_fn argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001623 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001624 }
1625
Sami Tolvanen90221202014-12-09 16:39:47 +00001626 ui = (UpdaterInfo*) state->cookie;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001627
Sami Tolvanen90221202014-12-09 16:39:47 +00001628 if (ui == NULL) {
1629 goto pbiudone;
1630 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001631
Sami Tolvanen90221202014-12-09 16:39:47 +00001632 cmd_pipe = ui->cmd_pipe;
1633 za = ui->package_zip;
1634
1635 if (cmd_pipe == NULL || za == NULL) {
1636 goto pbiudone;
1637 }
1638
1639 patch_entry = mzFindZipEntry(za, patch_data_fn->data);
1640
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001641 if (patch_entry == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001642 fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data);
1643 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001644 }
1645
Sami Tolvanen90221202014-12-09 16:39:47 +00001646 params.patch_start = ui->package_zip_addr + mzGetZipEntryOffset(patch_entry);
1647 new_entry = mzFindZipEntry(za, new_data_fn->data);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001648
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001649 if (new_entry == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001650 fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data);
1651 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001652 }
1653
Sami Tolvanen90221202014-12-09 16:39:47 +00001654 params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001655
Sami Tolvanen90221202014-12-09 16:39:47 +00001656 if (params.fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001657 fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001658 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001659 }
1660
Sami Tolvanen90221202014-12-09 16:39:47 +00001661 if (params.canwrite) {
1662 params.nti.za = za;
1663 params.nti.entry = new_entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001664
Sami Tolvanen90221202014-12-09 16:39:47 +00001665 pthread_mutex_init(&params.nti.mu, NULL);
1666 pthread_cond_init(&params.nti.cv, NULL);
1667 pthread_attr_init(&attr);
1668 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1669
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001670 int error = pthread_create(&params.thread, &attr, unzip_new_data, &params.nti);
1671 if (error != 0) {
1672 fprintf(stderr, "pthread_create failed: %s\n", strerror(error));
Sami Tolvanen90221202014-12-09 16:39:47 +00001673 goto pbiudone;
1674 }
1675 }
1676
1677 // The data in transfer_list_value is not necessarily null-terminated, so we need
1678 // to copy it to a new buffer and add the null that strtok_r will need.
Tao Baoba9a42a2015-06-23 23:23:33 -07001679 transfer_list = reinterpret_cast<char*>(malloc(transfer_list_value->size + 1));
Sami Tolvanen90221202014-12-09 16:39:47 +00001680
Doug Zongker1d5d6092014-08-21 10:47:24 -07001681 if (transfer_list == NULL) {
1682 fprintf(stderr, "failed to allocate %zd bytes for transfer list\n",
Sami Tolvanen90221202014-12-09 16:39:47 +00001683 transfer_list_value->size + 1);
1684 goto pbiudone;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001685 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001686
Doug Zongker1d5d6092014-08-21 10:47:24 -07001687 memcpy(transfer_list, transfer_list_value->data, transfer_list_value->size);
1688 transfer_list[transfer_list_value->size] = '\0';
1689
Sami Tolvanen90221202014-12-09 16:39:47 +00001690 // First line in transfer list is the version number
Doug Zongker1d5d6092014-08-21 10:47:24 -07001691 line = strtok_r(transfer_list, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001692 params.version = strtol(line, NULL, 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001693
Sami Tolvanen90221202014-12-09 16:39:47 +00001694 if (params.version < 1 || params.version > 3) {
1695 fprintf(stderr, "unexpected transfer list version [%s]\n", line);
1696 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001697 }
1698
Sami Tolvanen90221202014-12-09 16:39:47 +00001699 fprintf(stderr, "blockimg version is %d\n", params.version);
1700
1701 // Second line in transfer list is the total number of blocks we expect to write
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001702 line = strtok_r(NULL, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001703 total_blocks = strtol(line, NULL, 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001704
Sami Tolvanen90221202014-12-09 16:39:47 +00001705 if (total_blocks < 0) {
1706 ErrorAbort(state, "unexpected block count [%s]\n", line);
1707 goto pbiudone;
1708 } else if (total_blocks == 0) {
1709 rc = 0;
1710 goto pbiudone;
1711 }
1712
1713 if (params.version >= 2) {
1714 // Third line is how many stash entries are needed simultaneously
Doug Zongker52ae67d2014-09-08 12:22:09 -07001715 line = strtok_r(NULL, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001716 fprintf(stderr, "maximum stash entries %s\n", line);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001717
Sami Tolvanen90221202014-12-09 16:39:47 +00001718 // Fourth line is the maximum number of blocks that will be stashed simultaneously
1719 line = strtok_r(NULL, "\n", &linesave);
1720 stash_max_blocks = strtol(line, NULL, 0);
1721
1722 if (stash_max_blocks < 0) {
1723 ErrorAbort(state, "unexpected maximum stash blocks [%s]\n", line);
1724 goto pbiudone;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001725 }
1726
Jesse Zhao1df64d32015-02-17 17:09:23 -08001727 if (stash_max_blocks >= 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001728 res = CreateStash(state, stash_max_blocks, blockdev_filename->data,
1729 &params.stashbase);
1730
1731 if (res == -1) {
1732 goto pbiudone;
1733 }
1734
1735 params.createdstash = res;
1736 }
Doug Zongker52ae67d2014-09-08 12:22:09 -07001737 }
1738
Sami Tolvanen90221202014-12-09 16:39:47 +00001739 // Build a hash table of the available commands
1740 cmdht = mzHashTableCreate(cmdcount, NULL);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001741
Sami Tolvanen90221202014-12-09 16:39:47 +00001742 for (i = 0; i < cmdcount; ++i) {
1743 cmdhash = HashString(commands[i].name);
1744 mzHashTableLookup(cmdht, cmdhash, (void*) &commands[i], CompareCommands, true);
1745 }
1746
1747 // Subsequent lines are all individual transfer commands
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001748 for (line = strtok_r(NULL, "\n", &linesave); line;
1749 line = strtok_r(NULL, "\n", &linesave)) {
Doug Zongker52ae67d2014-09-08 12:22:09 -07001750
Sami Tolvanen90221202014-12-09 16:39:47 +00001751 logcmd = strdup(line);
1752 params.cmdname = strtok_r(line, " ", &params.cpos);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001753
Sami Tolvanen90221202014-12-09 16:39:47 +00001754 if (params.cmdname == NULL) {
1755 fprintf(stderr, "missing command [%s]\n", line);
1756 goto pbiudone;
1757 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001758
Sami Tolvanen90221202014-12-09 16:39:47 +00001759 cmdhash = HashString(params.cmdname);
1760 cmd = (const Command*) mzHashTableLookup(cmdht, cmdhash, params.cmdname,
1761 CompareCommandNames, false);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001762
Sami Tolvanen90221202014-12-09 16:39:47 +00001763 if (cmd == NULL) {
1764 fprintf(stderr, "unexpected command [%s]\n", params.cmdname);
1765 goto pbiudone;
1766 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001767
Sami Tolvanen90221202014-12-09 16:39:47 +00001768 if (cmd->f != NULL && cmd->f(&params) == -1) {
1769 fprintf(stderr, "failed to execute command [%s]\n",
1770 logcmd ? logcmd : params.cmdname);
1771 goto pbiudone;
1772 }
1773
1774 if (logcmd) {
1775 free(logcmd);
1776 logcmd = NULL;
1777 }
1778
1779 if (params.canwrite) {
Tao Bao187efff2015-07-27 14:07:08 -07001780 if (fsync(params.fd) == -1) {
1781 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
1782 goto pbiudone;
1783 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001784 fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001785 fflush(cmd_pipe);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001786 }
1787 }
1788
Sami Tolvanen90221202014-12-09 16:39:47 +00001789 if (params.canwrite) {
1790 pthread_join(params.thread, NULL);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001791
Sami Tolvanen90221202014-12-09 16:39:47 +00001792 fprintf(stderr, "wrote %d blocks; expected %d\n", params.written, total_blocks);
1793 fprintf(stderr, "max alloc needed was %zu\n", params.bufsize);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001794
Sami Tolvanen90221202014-12-09 16:39:47 +00001795 // Delete stash only after successfully completing the update, as it
1796 // may contain blocks needed to complete the update later.
1797 DeleteStash(params.stashbase);
1798 } else {
1799 fprintf(stderr, "verified partition contents; update may be resumed\n");
1800 }
1801
1802 rc = 0;
1803
1804pbiudone:
1805 if (params.fd != -1) {
1806 if (fsync(params.fd) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001807 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001808 }
Elliott Hughesb47afed2015-05-15 16:19:20 -07001809 close(params.fd);
Sami Tolvanen90221202014-12-09 16:39:47 +00001810 }
1811
1812 if (logcmd) {
1813 free(logcmd);
1814 }
1815
1816 if (cmdht) {
1817 mzHashTableFree(cmdht);
1818 }
1819
1820 if (params.buffer) {
1821 free(params.buffer);
1822 }
1823
1824 if (transfer_list) {
1825 free(transfer_list);
1826 }
1827
1828 if (blockdev_filename) {
1829 FreeValue(blockdev_filename);
1830 }
1831
1832 if (transfer_list_value) {
1833 FreeValue(transfer_list_value);
1834 }
1835
1836 if (new_data_fn) {
1837 FreeValue(new_data_fn);
1838 }
1839
1840 if (patch_data_fn) {
1841 FreeValue(patch_data_fn);
1842 }
1843
1844 // Only delete the stash if the update cannot be resumed, or it's
1845 // a verification run and we created the stash.
1846 if (params.isunresumable || (!params.canwrite && params.createdstash)) {
1847 DeleteStash(params.stashbase);
1848 }
1849
1850 if (params.stashbase) {
1851 free(params.stashbase);
1852 }
1853
1854 return StringValue(rc == 0 ? strdup("t") : strdup(""));
1855}
1856
1857// The transfer list is a text file containing commands to
1858// transfer data from one place to another on the target
1859// partition. We parse it and execute the commands in order:
1860//
1861// zero [rangeset]
1862// - fill the indicated blocks with zeros
1863//
1864// new [rangeset]
1865// - fill the blocks with data read from the new_data file
1866//
1867// erase [rangeset]
1868// - mark the given blocks as empty
1869//
1870// move <...>
1871// bsdiff <patchstart> <patchlen> <...>
1872// imgdiff <patchstart> <patchlen> <...>
1873// - read the source blocks, apply a patch (or not in the
1874// case of move), write result to target blocks. bsdiff or
1875// imgdiff specifies the type of patch; move means no patch
1876// at all.
1877//
1878// The format of <...> differs between versions 1 and 2;
1879// see the LoadSrcTgtVersion{1,2}() functions for a
1880// description of what's expected.
1881//
1882// stash <stash_id> <src_range>
1883// - (version 2+ only) load the given source range and stash
1884// the data in the given slot of the stash table.
1885//
1886// The creator of the transfer list will guarantee that no block
1887// is read (ie, used as the source for a patch or move) after it
1888// has been written.
1889//
1890// In version 2, the creator will guarantee that a given stash is
1891// loaded (with a stash command) before it's used in a
1892// move/bsdiff/imgdiff command.
1893//
1894// Within one command the source and target ranges may overlap so
1895// in general we need to read the entire source into memory before
1896// writing anything to the target blocks.
1897//
1898// All the patch data is concatenated into one patch_data file in
1899// the update package. It must be stored uncompressed because we
1900// memory-map it in directly from the archive. (Since patches are
1901// already compressed, we lose very little by not compressing
1902// their concatenation.)
1903//
1904// In version 3, commands that read data from the partition (i.e.
1905// move/bsdiff/imgdiff/stash) have one or more additional hashes
1906// before the range parameters, which are used to check if the
1907// command has already been completed and verify the integrity of
1908// the source data.
1909
1910Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]) {
1911 // Commands which are not tested are set to NULL to skip them completely
1912 const Command commands[] = {
1913 { "bsdiff", PerformCommandDiff },
1914 { "erase", NULL },
1915 { "free", PerformCommandFree },
1916 { "imgdiff", PerformCommandDiff },
1917 { "move", PerformCommandMove },
1918 { "new", NULL },
1919 { "stash", PerformCommandStash },
1920 { "zero", NULL }
1921 };
1922
1923 // Perform a dry run without writing to test if an update can proceed
1924 return PerformBlockImageUpdate(name, state, argc, argv, commands,
1925 sizeof(commands) / sizeof(commands[0]), 1);
1926}
1927
1928Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) {
1929 const Command commands[] = {
1930 { "bsdiff", PerformCommandDiff },
1931 { "erase", PerformCommandErase },
1932 { "free", PerformCommandFree },
1933 { "imgdiff", PerformCommandDiff },
1934 { "move", PerformCommandMove },
1935 { "new", PerformCommandNew },
1936 { "stash", PerformCommandStash },
1937 { "zero", PerformCommandZero }
1938 };
1939
1940 return PerformBlockImageUpdate(name, state, argc, argv, commands,
1941 sizeof(commands) / sizeof(commands[0]), 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001942}
1943
1944Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) {
1945 Value* blockdev_filename;
1946 Value* ranges;
1947 const uint8_t* digest = NULL;
1948 if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) {
1949 return NULL;
1950 }
1951
1952 if (blockdev_filename->type != VAL_STRING) {
1953 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
1954 goto done;
1955 }
1956 if (ranges->type != VAL_STRING) {
1957 ErrorAbort(state, "ranges argument to %s must be string", name);
1958 goto done;
1959 }
1960
Tao Baoba9a42a2015-06-23 23:23:33 -07001961 int fd;
1962 fd = open(blockdev_filename->data, O_RDWR);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001963 if (fd < 0) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001964 ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001965 goto done;
1966 }
1967
Tao Baoba9a42a2015-06-23 23:23:33 -07001968 RangeSet* rs;
1969 rs = parse_range(ranges->data);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001970 uint8_t buffer[BLOCKSIZE];
1971
1972 SHA_CTX ctx;
1973 SHA_init(&ctx);
1974
1975 int i, j;
1976 for (i = 0; i < rs->count; ++i) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -07001977 if (!check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001978 ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data,
1979 strerror(errno));
1980 goto done;
1981 }
1982
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001983 for (j = rs->pos[i*2]; j < rs->pos[i*2+1]; ++j) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001984 if (read_all(fd, buffer, BLOCKSIZE) == -1) {
1985 ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data,
1986 strerror(errno));
1987 goto done;
1988 }
1989
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001990 SHA_update(&ctx, buffer, BLOCKSIZE);
1991 }
1992 }
1993 digest = SHA_final(&ctx);
1994 close(fd);
1995
1996 done:
1997 FreeValue(blockdev_filename);
1998 FreeValue(ranges);
1999 if (digest == NULL) {
2000 return StringValue(strdup(""));
2001 } else {
2002 return StringValue(PrintSha1(digest));
2003 }
2004}
2005
2006void RegisterBlockImageFunctions() {
Sami Tolvanen90221202014-12-09 16:39:47 +00002007 RegisterFunction("block_image_verify", BlockImageVerifyFn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07002008 RegisterFunction("block_image_update", BlockImageUpdateFn);
2009 RegisterFunction("range_sha1", RangeSha1Fn);
2010}