blob: e0be03917bd04d06cb556fd0cec495c987e7d8cd [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 Baobe19dce2015-07-31 15:56:44 -070022#include <libgen.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070023#include <pthread.h>
24#include <stdarg.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Sami Tolvanen90221202014-12-09 16:39:47 +000028#include <sys/stat.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070029#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/ioctl.h>
32#include <time.h>
33#include <unistd.h>
34
35#include "applypatch/applypatch.h"
36#include "edify/expr.h"
37#include "mincrypt/sha.h"
Sami Tolvanen90221202014-12-09 16:39:47 +000038#include "minzip/Hash.h"
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070039#include "updater.h"
40
41#define BLOCKSIZE 4096
42
Sami Tolvanen6abd52f2015-06-10 15:52:04 +000043// Set this to 0 to interpret 'erase' transfers to mean do a
44// BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret
45// erase to mean fill the region with zeroes.
46#define DEBUG_ERASE 0
47
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070048#ifndef BLKDISCARD
49#define BLKDISCARD _IO(0x12,119)
50#endif
51
Sami Tolvanen90221202014-12-09 16:39:47 +000052#define STASH_DIRECTORY_BASE "/cache/recovery"
53#define STASH_DIRECTORY_MODE 0700
54#define STASH_FILE_MODE 0600
55
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070056char* PrintSha1(const uint8_t* digest);
57
58typedef struct {
59 int count;
60 int size;
61 int pos[0];
62} RangeSet;
63
Sami Tolvanen806f72f2015-05-12 12:48:46 +010064#define RANGESET_MAX_POINTS \
65 ((int)((INT_MAX / sizeof(int)) - sizeof(RangeSet)))
66
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070067static RangeSet* parse_range(char* text) {
68 char* save;
Sami Tolvanen806f72f2015-05-12 12:48:46 +010069 char* token;
70 int i, num;
71 long int val;
72 RangeSet* out = NULL;
73 size_t bufsize;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070074
Sami Tolvanen806f72f2015-05-12 12:48:46 +010075 if (!text) {
76 goto err;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070077 }
Sami Tolvanen806f72f2015-05-12 12:48:46 +010078
79 token = strtok_r(text, ",", &save);
80
81 if (!token) {
82 goto err;
83 }
84
85 val = strtol(token, NULL, 0);
86
87 if (val < 2 || val > RANGESET_MAX_POINTS) {
88 goto err;
89 } else if (val % 2) {
90 goto err; // must be even
91 }
92
93 num = (int) val;
94 bufsize = sizeof(RangeSet) + num * sizeof(int);
95
96 out = malloc(bufsize);
97
98 if (!out) {
99 fprintf(stderr, "failed to allocate range of %zu bytes\n", bufsize);
100 goto err;
101 }
102
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700103 out->count = num / 2;
104 out->size = 0;
Sami Tolvanen806f72f2015-05-12 12:48:46 +0100105
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700106 for (i = 0; i < num; ++i) {
Sami Tolvanen806f72f2015-05-12 12:48:46 +0100107 token = strtok_r(NULL, ",", &save);
108
109 if (!token) {
110 goto err;
111 }
112
113 val = strtol(token, NULL, 0);
114
115 if (val < 0 || val > INT_MAX) {
116 goto err;
117 }
118
119 out->pos[i] = (int) val;
120
121 if (i % 2) {
122 if (out->pos[i - 1] >= out->pos[i]) {
123 goto err; // empty or negative range
124 }
125
126 if (out->size > INT_MAX - out->pos[i]) {
127 goto err; // overflow
128 }
129
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700130 out->size += out->pos[i];
131 } else {
Sami Tolvanen806f72f2015-05-12 12:48:46 +0100132 if (out->size < 0) {
133 goto err;
134 }
135
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700136 out->size -= out->pos[i];
137 }
138 }
139
Sami Tolvanen806f72f2015-05-12 12:48:46 +0100140 if (out->size <= 0) {
141 goto err;
142 }
143
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700144 return out;
Sami Tolvanen806f72f2015-05-12 12:48:46 +0100145
146err:
147 fprintf(stderr, "failed to parse range '%s'\n", text ? text : "NULL");
148 exit(1);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700149}
150
Sami Tolvanen90221202014-12-09 16:39:47 +0000151static int range_overlaps(RangeSet* r1, RangeSet* r2) {
152 int i, j, r1_0, r1_1, r2_0, r2_1;
153
154 if (!r1 || !r2) {
155 return 0;
156 }
157
158 for (i = 0; i < r1->count; ++i) {
159 r1_0 = r1->pos[i * 2];
160 r1_1 = r1->pos[i * 2 + 1];
161
162 for (j = 0; j < r2->count; ++j) {
163 r2_0 = r2->pos[j * 2];
164 r2_1 = r2->pos[j * 2 + 1];
165
Tao Baoc3dddce2015-06-25 14:00:31 -0700166 if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000167 return 1;
168 }
169 }
170 }
171
172 return 0;
173}
174
175static int read_all(int fd, uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700176 size_t so_far = 0;
177 while (so_far < size) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700178 ssize_t r = TEMP_FAILURE_RETRY(read(fd, data+so_far, size-so_far));
179 if (r == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700180 fprintf(stderr, "read failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000181 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700182 }
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700183 so_far += r;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700184 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000185 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700186}
187
Sami Tolvanen90221202014-12-09 16:39:47 +0000188static int write_all(int fd, const uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700189 size_t written = 0;
190 while (written < size) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700191 ssize_t w = TEMP_FAILURE_RETRY(write(fd, data+written, size-written));
192 if (w == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700193 fprintf(stderr, "write failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000194 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700195 }
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700196 written += w;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700197 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000198
199 if (fsync(fd) == -1) {
200 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
201 return -1;
202 }
203
204 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700205}
206
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700207static bool check_lseek(int fd, off64_t offset, int whence) {
208 off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
209 if (rc == -1) {
210 fprintf(stderr, "lseek64 failed: %s\n", strerror(errno));
211 return false;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700212 }
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700213 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700214}
215
216static void allocate(size_t size, uint8_t** buffer, size_t* buffer_alloc) {
217 // if the buffer's big enough, reuse it.
218 if (size <= *buffer_alloc) return;
219
220 free(*buffer);
221
222 *buffer = (uint8_t*) malloc(size);
223 if (*buffer == NULL) {
Doug Zongker1d5d6092014-08-21 10:47:24 -0700224 fprintf(stderr, "failed to allocate %zu bytes\n", size);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700225 exit(1);
226 }
227 *buffer_alloc = size;
228}
229
230typedef struct {
231 int fd;
232 RangeSet* tgt;
233 int p_block;
234 size_t p_remain;
235} RangeSinkState;
236
237static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) {
238 RangeSinkState* rss = (RangeSinkState*) token;
239
240 if (rss->p_remain <= 0) {
241 fprintf(stderr, "range sink write overrun");
Sami Tolvanen90221202014-12-09 16:39:47 +0000242 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700243 }
244
245 ssize_t written = 0;
246 while (size > 0) {
247 size_t write_now = size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000248
249 if (rss->p_remain < write_now) {
250 write_now = rss->p_remain;
251 }
252
253 if (write_all(rss->fd, data, write_now) == -1) {
254 break;
255 }
256
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700257 data += write_now;
258 size -= write_now;
259
260 rss->p_remain -= write_now;
261 written += write_now;
262
263 if (rss->p_remain == 0) {
264 // move to the next block
265 ++rss->p_block;
266 if (rss->p_block < rss->tgt->count) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000267 rss->p_remain = (rss->tgt->pos[rss->p_block * 2 + 1] -
268 rss->tgt->pos[rss->p_block * 2]) * BLOCKSIZE;
269
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700270 if (!check_lseek(rss->fd, (off64_t)rss->tgt->pos[rss->p_block*2] * BLOCKSIZE,
271 SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000272 break;
273 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700274 } else {
275 // we can't write any more; return how many bytes have
276 // been written so far.
Sami Tolvanen90221202014-12-09 16:39:47 +0000277 break;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700278 }
279 }
280 }
281
282 return written;
283}
284
285// All of the data for all the 'new' transfers is contained in one
286// file in the update package, concatenated together in the order in
287// which transfers.list will need it. We want to stream it out of the
288// archive (it's compressed) without writing it to a temp file, but we
289// can't write each section until it's that transfer's turn to go.
290//
291// To achieve this, we expand the new data from the archive in a
292// background thread, and block that threads 'receive uncompressed
293// data' function until the main thread has reached a point where we
294// want some new data to be written. We signal the background thread
295// with the destination for the data and block the main thread,
296// waiting for the background thread to complete writing that section.
297// Then it signals the main thread to wake up and goes back to
298// blocking waiting for a transfer.
299//
300// NewThreadInfo is the struct used to pass information back and forth
301// between the two threads. When the main thread wants some data
302// written, it sets rss to the destination location and signals the
303// condition. When the background thread is done writing, it clears
304// rss and signals the condition again.
305
306typedef struct {
307 ZipArchive* za;
308 const ZipEntry* entry;
309
310 RangeSinkState* rss;
311
312 pthread_mutex_t mu;
313 pthread_cond_t cv;
314} NewThreadInfo;
315
316static bool receive_new_data(const unsigned char* data, int size, void* cookie) {
317 NewThreadInfo* nti = (NewThreadInfo*) cookie;
318
319 while (size > 0) {
320 // Wait for nti->rss to be non-NULL, indicating some of this
321 // data is wanted.
322 pthread_mutex_lock(&nti->mu);
323 while (nti->rss == NULL) {
324 pthread_cond_wait(&nti->cv, &nti->mu);
325 }
326 pthread_mutex_unlock(&nti->mu);
327
328 // At this point nti->rss is set, and we own it. The main
329 // thread is waiting for it to disappear from nti.
330 ssize_t written = RangeSinkWrite(data, size, nti->rss);
331 data += written;
332 size -= written;
333
334 if (nti->rss->p_block == nti->rss->tgt->count) {
335 // we have written all the bytes desired by this rss.
336
337 pthread_mutex_lock(&nti->mu);
338 nti->rss = NULL;
339 pthread_cond_broadcast(&nti->cv);
340 pthread_mutex_unlock(&nti->mu);
341 }
342 }
343
344 return true;
345}
346
347static void* unzip_new_data(void* cookie) {
348 NewThreadInfo* nti = (NewThreadInfo*) cookie;
349 mzProcessZipEntryContents(nti->za, nti->entry, receive_new_data, nti);
350 return NULL;
351}
352
Sami Tolvanen90221202014-12-09 16:39:47 +0000353static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) {
354 int i;
355 size_t p = 0;
356 size_t size;
357
358 if (!src || !buffer) {
359 return -1;
360 }
361
362 for (i = 0; i < src->count; ++i) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700363 if (!check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000364 return -1;
365 }
366
367 size = (src->pos[i * 2 + 1] - src->pos[i * 2]) * BLOCKSIZE;
368
369 if (read_all(fd, buffer + p, size) == -1) {
370 return -1;
371 }
372
373 p += size;
374 }
375
376 return 0;
377}
378
379static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) {
380 int i;
381 size_t p = 0;
382 size_t size;
383
384 if (!tgt || !buffer) {
385 return -1;
386 }
387
388 for (i = 0; i < tgt->count; ++i) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700389 if (!check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000390 return -1;
391 }
392
393 size = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * BLOCKSIZE;
394
395 if (write_all(fd, buffer + p, size) == -1) {
396 return -1;
397 }
398
399 p += size;
400 }
401
402 return 0;
403}
404
Doug Zongker52ae67d2014-09-08 12:22:09 -0700405// Do a source/target load for move/bsdiff/imgdiff in version 1.
406// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
407// to parse the remainder of the string as:
408//
409// <src_range> <tgt_range>
410//
411// The source range is loaded into the provided buffer, reallocating
412// it to make it larger if necessary. The target ranges are returned
413// in *tgt, if tgt is non-NULL.
414
Sami Tolvanen90221202014-12-09 16:39:47 +0000415static int LoadSrcTgtVersion1(char** wordsave, RangeSet** tgt, int* src_blocks,
Doug Zongker52ae67d2014-09-08 12:22:09 -0700416 uint8_t** buffer, size_t* buffer_alloc, int fd) {
417 char* word;
Sami Tolvanen90221202014-12-09 16:39:47 +0000418 int rc;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700419
Sami Tolvanen90221202014-12-09 16:39:47 +0000420 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700421 RangeSet* src = parse_range(word);
422
423 if (tgt != NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000424 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700425 *tgt = parse_range(word);
426 }
427
428 allocate(src->size * BLOCKSIZE, buffer, buffer_alloc);
Sami Tolvanen90221202014-12-09 16:39:47 +0000429 rc = ReadBlocks(src, *buffer, fd);
430 *src_blocks = src->size;
431
432 free(src);
433 return rc;
434}
435
436static int VerifyBlocks(const char *expected, const uint8_t *buffer,
437 size_t blocks, int printerror) {
438 char* hexdigest = NULL;
439 int rc = -1;
440 uint8_t digest[SHA_DIGEST_SIZE];
441
442 if (!expected || !buffer) {
443 return rc;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700444 }
445
Sami Tolvanen90221202014-12-09 16:39:47 +0000446 SHA_hash(buffer, blocks * BLOCKSIZE, digest);
447 hexdigest = PrintSha1(digest);
448
449 if (hexdigest != NULL) {
450 rc = strcmp(expected, hexdigest);
451
452 if (rc != 0 && printerror) {
453 fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
454 expected, hexdigest);
455 }
456
457 free(hexdigest);
458 }
459
460 return rc;
461}
462
463static char* GetStashFileName(const char* base, const char* id, const char* postfix) {
464 char* fn;
465 int len;
466 int res;
467
468 if (base == NULL) {
469 return NULL;
470 }
471
472 if (id == NULL) {
473 id = "";
474 }
475
476 if (postfix == NULL) {
477 postfix = "";
478 }
479
480 len = strlen(STASH_DIRECTORY_BASE) + 1 + strlen(base) + 1 + strlen(id) + strlen(postfix) + 1;
481 fn = malloc(len);
482
483 if (fn == NULL) {
484 fprintf(stderr, "failed to malloc %d bytes for fn\n", len);
485 return NULL;
486 }
487
488 res = snprintf(fn, len, STASH_DIRECTORY_BASE "/%s/%s%s", base, id, postfix);
489
490 if (res < 0 || res >= len) {
491 fprintf(stderr, "failed to format file name (return value %d)\n", res);
492 free(fn);
493 return NULL;
494 }
495
496 return fn;
497}
498
499typedef void (*StashCallback)(const char*, void*);
500
501// Does a best effort enumeration of stash files. Ignores possible non-file
502// items in the stash directory and continues despite of errors. Calls the
503// 'callback' function for each file and passes 'data' to the function as a
504// parameter.
505
506static void EnumerateStash(const char* dirname, StashCallback callback, void* data) {
507 char* fn;
508 DIR* directory;
509 int len;
510 int res;
511 struct dirent* item;
512
513 if (dirname == NULL || callback == NULL) {
514 return;
515 }
516
517 directory = opendir(dirname);
518
519 if (directory == NULL) {
520 if (errno != ENOENT) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700521 fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000522 }
523 return;
524 }
525
526 while ((item = readdir(directory)) != NULL) {
527 if (item->d_type != DT_REG) {
528 continue;
529 }
530
531 len = strlen(dirname) + 1 + strlen(item->d_name) + 1;
532 fn = malloc(len);
533
534 if (fn == NULL) {
535 fprintf(stderr, "failed to malloc %d bytes for fn\n", len);
536 continue;
537 }
538
539 res = snprintf(fn, len, "%s/%s", dirname, item->d_name);
540
541 if (res < 0 || res >= len) {
542 fprintf(stderr, "failed to format file name (return value %d)\n", res);
543 free(fn);
544 continue;
545 }
546
547 callback(fn, data);
548 free(fn);
549 }
550
551 if (closedir(directory) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700552 fprintf(stderr, "closedir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000553 }
554}
555
556static void UpdateFileSize(const char* fn, void* data) {
557 int* size = (int*) data;
558 struct stat st;
559
560 if (!fn || !data) {
561 return;
562 }
563
564 if (stat(fn, &st) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700565 fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000566 return;
567 }
568
569 *size += st.st_size;
570}
571
572// Deletes the stash directory and all files in it. Assumes that it only
573// contains files. There is nothing we can do about unlikely, but possible
574// errors, so they are merely logged.
575
576static void DeleteFile(const char* fn, void* data) {
577 if (fn) {
578 fprintf(stderr, "deleting %s\n", fn);
579
580 if (unlink(fn) == -1 && errno != ENOENT) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700581 fprintf(stderr, "unlink \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000582 }
583 }
584}
585
586static void DeletePartial(const char* fn, void* data) {
587 if (fn && strstr(fn, ".partial") != NULL) {
588 DeleteFile(fn, data);
589 }
590}
591
592static void DeleteStash(const char* base) {
593 char* dirname;
594
595 if (base == NULL) {
596 return;
597 }
598
599 dirname = GetStashFileName(base, NULL, NULL);
600
601 if (dirname == NULL) {
602 return;
603 }
604
605 fprintf(stderr, "deleting stash %s\n", base);
606 EnumerateStash(dirname, DeleteFile, NULL);
607
608 if (rmdir(dirname) == -1) {
609 if (errno != ENOENT && errno != ENOTDIR) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700610 fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000611 }
612 }
613
614 free(dirname);
615}
616
617static int LoadStash(const char* base, const char* id, int verify, int* blocks, uint8_t** buffer,
618 size_t* buffer_alloc, int printnoent) {
619 char *fn = NULL;
620 int blockcount = 0;
621 int fd = -1;
622 int rc = -1;
623 int res;
624 struct stat st;
625
626 if (!base || !id || !buffer || !buffer_alloc) {
627 goto lsout;
628 }
629
630 if (!blocks) {
631 blocks = &blockcount;
632 }
633
634 fn = GetStashFileName(base, id, NULL);
635
636 if (fn == NULL) {
637 goto lsout;
638 }
639
640 res = stat(fn, &st);
641
642 if (res == -1) {
643 if (errno != ENOENT || printnoent) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700644 fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000645 }
646 goto lsout;
647 }
648
649 fprintf(stderr, " loading %s\n", fn);
650
651 if ((st.st_size % BLOCKSIZE) != 0) {
652 fprintf(stderr, "%s size %zd not multiple of block size %d", fn, st.st_size, BLOCKSIZE);
653 goto lsout;
654 }
655
656 fd = TEMP_FAILURE_RETRY(open(fn, O_RDONLY));
657
658 if (fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700659 fprintf(stderr, "open \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000660 goto lsout;
661 }
662
663 allocate(st.st_size, buffer, buffer_alloc);
664
665 if (read_all(fd, *buffer, st.st_size) == -1) {
666 goto lsout;
667 }
668
669 *blocks = st.st_size / BLOCKSIZE;
670
671 if (verify && VerifyBlocks(id, *buffer, *blocks, 1) != 0) {
672 fprintf(stderr, "unexpected contents in %s\n", fn);
673 DeleteFile(fn, NULL);
674 goto lsout;
675 }
676
677 rc = 0;
678
679lsout:
680 if (fd != -1) {
Elliott Hughesb5dabd22015-05-28 23:06:17 -0700681 close(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000682 }
683
684 if (fn) {
685 free(fn);
686 }
687
688 return rc;
689}
690
691static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buffer,
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100692 int checkspace, int *exists) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000693 char *fn = NULL;
694 char *cn = NULL;
695 int fd = -1;
696 int rc = -1;
Tao Baobe19dce2015-07-31 15:56:44 -0700697 int dfd = -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000698 int res;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100699 struct stat st;
Sami Tolvanen90221202014-12-09 16:39:47 +0000700
701 if (base == NULL || buffer == NULL) {
702 goto wsout;
703 }
704
705 if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) {
706 fprintf(stderr, "not enough space to write stash\n");
707 goto wsout;
708 }
709
710 fn = GetStashFileName(base, id, ".partial");
711 cn = GetStashFileName(base, id, NULL);
712
713 if (fn == NULL || cn == NULL) {
714 goto wsout;
715 }
716
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100717 if (exists) {
718 res = stat(cn, &st);
719
720 if (res == 0) {
721 // The file already exists and since the name is the hash of the contents,
722 // it's safe to assume the contents are identical (accidental hash collisions
723 // are unlikely)
724 fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn);
725 *exists = 1;
726 rc = 0;
727 goto wsout;
728 }
729
730 *exists = 0;
731 }
732
Sami Tolvanen90221202014-12-09 16:39:47 +0000733 fprintf(stderr, " writing %d blocks to %s\n", blocks, cn);
734
735 fd = TEMP_FAILURE_RETRY(open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, STASH_FILE_MODE));
736
737 if (fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700738 fprintf(stderr, "failed to create \"%s\": %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000739 goto wsout;
740 }
741
742 if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) {
743 goto wsout;
744 }
745
746 if (fsync(fd) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700747 fprintf(stderr, "fsync \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000748 goto wsout;
749 }
750
751 if (rename(fn, cn) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700752 fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn, cn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000753 goto wsout;
754 }
755
Tao Baobe19dce2015-07-31 15:56:44 -0700756 const char* dname;
757 dname = dirname(cn);
758 dfd = TEMP_FAILURE_RETRY(open(dname, O_RDONLY | O_DIRECTORY));
759
760 if (dfd == -1) {
761 fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname, strerror(errno));
762 goto wsout;
763 }
764
765 if (fsync(dfd) == -1) {
766 fprintf(stderr, "fsync \"%s\" failed: %s\n", dname, strerror(errno));
767 goto wsout;
768 }
769
Sami Tolvanen90221202014-12-09 16:39:47 +0000770 rc = 0;
771
772wsout:
773 if (fd != -1) {
Elliott Hughes1857a7f2015-05-15 16:19:20 -0700774 close(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000775 }
776
Tao Baobe19dce2015-07-31 15:56:44 -0700777 if (dfd != -1) {
778 close(dfd);
779 }
780
Sami Tolvanen90221202014-12-09 16:39:47 +0000781 if (fn) {
782 free(fn);
783 }
784
785 if (cn) {
786 free(cn);
787 }
788
789 return rc;
790}
791
792// Creates a directory for storing stash files and checks if the /cache partition
793// hash enough space for the expected amount of blocks we need to store. Returns
794// >0 if we created the directory, zero if it existed already, and <0 of failure.
795
796static int CreateStash(State* state, int maxblocks, const char* blockdev, char** base) {
797 char* dirname = NULL;
798 const uint8_t* digest;
799 int rc = -1;
800 int res;
801 int size = 0;
802 SHA_CTX ctx;
803 struct stat st;
804
805 if (blockdev == NULL || base == NULL) {
806 goto csout;
807 }
808
809 // Stash directory should be different for each partition to avoid conflicts
810 // when updating multiple partitions at the same time, so we use the hash of
811 // the block device name as the base directory
812 SHA_init(&ctx);
813 SHA_update(&ctx, blockdev, strlen(blockdev));
814 digest = SHA_final(&ctx);
815 *base = PrintSha1(digest);
816
817 if (*base == NULL) {
818 goto csout;
819 }
820
821 dirname = GetStashFileName(*base, NULL, NULL);
822
823 if (dirname == NULL) {
824 goto csout;
825 }
826
827 res = stat(dirname, &st);
828
829 if (res == -1 && errno != ENOENT) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700830 ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000831 goto csout;
832 } else if (res != 0) {
833 fprintf(stderr, "creating stash %s\n", dirname);
834 res = mkdir(dirname, STASH_DIRECTORY_MODE);
835
836 if (res != 0) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700837 ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000838 goto csout;
839 }
840
841 if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) {
842 ErrorAbort(state, "not enough space for stash\n");
843 goto csout;
844 }
845
846 rc = 1; // Created directory
847 goto csout;
848 }
849
850 fprintf(stderr, "using existing stash %s\n", dirname);
851
852 // If the directory already exists, calculate the space already allocated to
853 // stash files and check if there's enough for all required blocks. Delete any
854 // partially completed stash files first.
855
856 EnumerateStash(dirname, DeletePartial, NULL);
857 EnumerateStash(dirname, UpdateFileSize, &size);
858
859 size = (maxblocks * BLOCKSIZE) - size;
860
861 if (size > 0 && CacheSizeCheck(size) != 0) {
862 ErrorAbort(state, "not enough space for stash (%d more needed)\n", size);
863 goto csout;
864 }
865
866 rc = 0; // Using existing directory
867
868csout:
869 if (dirname) {
870 free(dirname);
871 }
872
873 return rc;
874}
875
876static int SaveStash(const char* base, char** wordsave, uint8_t** buffer, size_t* buffer_alloc,
877 int fd, int usehash, int* isunresumable) {
878 char *id = NULL;
879 int res = -1;
880 int blocks = 0;
881
882 if (!wordsave || !buffer || !buffer_alloc || !isunresumable) {
883 return -1;
884 }
885
886 id = strtok_r(NULL, " ", wordsave);
887
888 if (id == NULL) {
889 fprintf(stderr, "missing id field in stash command\n");
890 return -1;
891 }
892
893 if (usehash && LoadStash(base, id, 1, &blocks, buffer, buffer_alloc, 0) == 0) {
894 // Stash file already exists and has expected contents. Do not
895 // read from source again, as the source may have been already
896 // overwritten during a previous attempt.
897 return 0;
898 }
899
900 if (LoadSrcTgtVersion1(wordsave, NULL, &blocks, buffer, buffer_alloc, fd) == -1) {
901 return -1;
902 }
903
904 if (usehash && VerifyBlocks(id, *buffer, blocks, 1) != 0) {
905 // Source blocks have unexpected contents. If we actually need this
906 // data later, this is an unrecoverable error. However, the command
907 // that uses the data may have already completed previously, so the
908 // possible failure will occur during source block verification.
909 fprintf(stderr, "failed to load source blocks for stash %s\n", id);
910 return 0;
911 }
912
913 fprintf(stderr, "stashing %d blocks to %s\n", blocks, id);
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100914 return WriteStash(base, id, blocks, *buffer, 0, NULL);
Sami Tolvanen90221202014-12-09 16:39:47 +0000915}
916
917static int FreeStash(const char* base, const char* id) {
918 char *fn = NULL;
919
920 if (base == NULL || id == NULL) {
921 return -1;
922 }
923
924 fn = GetStashFileName(base, id, NULL);
925
926 if (fn == NULL) {
927 return -1;
928 }
929
930 DeleteFile(fn, NULL);
931 free(fn);
932
933 return 0;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700934}
935
936static void MoveRange(uint8_t* dest, RangeSet* locs, const uint8_t* source) {
937 // source contains packed data, which we want to move to the
938 // locations given in *locs in the dest buffer. source and dest
939 // may be the same buffer.
940
941 int start = locs->size;
942 int i;
943 for (i = locs->count-1; i >= 0; --i) {
944 int blocks = locs->pos[i*2+1] - locs->pos[i*2];
945 start -= blocks;
946 memmove(dest + (locs->pos[i*2] * BLOCKSIZE), source + (start * BLOCKSIZE),
947 blocks * BLOCKSIZE);
948 }
949}
950
951// Do a source/target load for move/bsdiff/imgdiff in version 2.
952// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
953// to parse the remainder of the string as one of:
954//
955// <tgt_range> <src_block_count> <src_range>
956// (loads data from source image only)
957//
958// <tgt_range> <src_block_count> - <[stash_id:stash_range] ...>
959// (loads data from stashes only)
960//
961// <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...>
962// (loads data from both source image and stashes)
963//
964// On return, buffer is filled with the loaded source data (rearranged
965// and combined with stashed data as necessary). buffer may be
966// reallocated if needed to accommodate the source data. *tgt is the
Sami Tolvanen90221202014-12-09 16:39:47 +0000967// target RangeSet. Any stashes required are loaded using LoadStash.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700968
Sami Tolvanen90221202014-12-09 16:39:47 +0000969static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks,
Doug Zongker52ae67d2014-09-08 12:22:09 -0700970 uint8_t** buffer, size_t* buffer_alloc, int fd,
Sami Tolvanen90221202014-12-09 16:39:47 +0000971 const char* stashbase, int* overlap) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700972 char* word;
Sami Tolvanen90221202014-12-09 16:39:47 +0000973 char* colonsave;
974 char* colon;
975 int id;
976 int res;
977 RangeSet* locs;
978 size_t stashalloc = 0;
979 uint8_t* stash = NULL;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700980
981 if (tgt != NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000982 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700983 *tgt = parse_range(word);
984 }
985
Sami Tolvanen90221202014-12-09 16:39:47 +0000986 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700987 *src_blocks = strtol(word, NULL, 0);
988
989 allocate(*src_blocks * BLOCKSIZE, buffer, buffer_alloc);
990
Sami Tolvanen90221202014-12-09 16:39:47 +0000991 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700992 if (word[0] == '-' && word[1] == '\0') {
993 // no source ranges, only stashes
994 } else {
995 RangeSet* src = parse_range(word);
Sami Tolvanen90221202014-12-09 16:39:47 +0000996 res = ReadBlocks(src, *buffer, fd);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700997
Sami Tolvanen90221202014-12-09 16:39:47 +0000998 if (overlap && tgt) {
999 *overlap = range_overlaps(src, *tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001000 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001001
Doug Zongker52ae67d2014-09-08 12:22:09 -07001002 free(src);
1003
Sami Tolvanen90221202014-12-09 16:39:47 +00001004 if (res == -1) {
1005 return -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001006 }
1007
Sami Tolvanen90221202014-12-09 16:39:47 +00001008 word = strtok_r(NULL, " ", wordsave);
1009 if (word == NULL) {
1010 // no stashes, only source range
1011 return 0;
1012 }
1013
1014 locs = parse_range(word);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001015 MoveRange(*buffer, locs, *buffer);
Sami Tolvanen90221202014-12-09 16:39:47 +00001016 free(locs);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001017 }
1018
Sami Tolvanen90221202014-12-09 16:39:47 +00001019 while ((word = strtok_r(NULL, " ", wordsave)) != NULL) {
Doug Zongker52ae67d2014-09-08 12:22:09 -07001020 // Each word is a an index into the stash table, a colon, and
1021 // then a rangeset describing where in the source block that
1022 // stashed data should go.
Sami Tolvanen90221202014-12-09 16:39:47 +00001023 colonsave = NULL;
1024 colon = strtok_r(word, ":", &colonsave);
1025
1026 res = LoadStash(stashbase, colon, 0, NULL, &stash, &stashalloc, 1);
1027
1028 if (res == -1) {
1029 // These source blocks will fail verification if used later, but we
1030 // will let the caller decide if this is a fatal failure
1031 fprintf(stderr, "failed to load stash %s\n", colon);
1032 continue;
1033 }
1034
Doug Zongker52ae67d2014-09-08 12:22:09 -07001035 colon = strtok_r(NULL, ":", &colonsave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001036 locs = parse_range(colon);
1037
1038 MoveRange(*buffer, locs, stash);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001039 free(locs);
1040 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001041
1042 if (stash) {
1043 free(stash);
1044 }
1045
1046 return 0;
1047}
1048
1049// Parameters for transfer list command functions
1050typedef struct {
1051 char* cmdname;
1052 char* cpos;
1053 char* freestash;
1054 char* stashbase;
1055 int canwrite;
1056 int createdstash;
1057 int fd;
1058 int foundwrites;
1059 int isunresumable;
1060 int version;
1061 int written;
1062 NewThreadInfo nti;
1063 pthread_t thread;
1064 size_t bufsize;
1065 uint8_t* buffer;
1066 uint8_t* patch_start;
1067} CommandParameters;
1068
1069// Do a source/target load for move/bsdiff/imgdiff in version 3.
1070//
1071// Parameters are the same as for LoadSrcTgtVersion2, except for 'onehash', which
1072// tells the function whether to expect separate source and targe block hashes, or
1073// if they are both the same and only one hash should be expected, and
1074// 'isunresumable', which receives a non-zero value if block verification fails in
1075// a way that the update cannot be resumed anymore.
1076//
1077// If the function is unable to load the necessary blocks or their contents don't
1078// match the hashes, the return value is -1 and the command should be aborted.
1079//
1080// If the return value is 1, the command has already been completed according to
1081// the contents of the target blocks, and should not be performed again.
1082//
1083// If the return value is 0, source blocks have expected content and the command
1084// can be performed.
1085
1086static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* src_blocks,
1087 int onehash, int* overlap) {
1088 char* srchash = NULL;
1089 char* tgthash = NULL;
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001090 int stash_exists = 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001091 int overlap_blocks = 0;
1092 int rc = -1;
1093 uint8_t* tgtbuffer = NULL;
1094
1095 if (!params|| !tgt || !src_blocks || !overlap) {
1096 goto v3out;
1097 }
1098
1099 srchash = strtok_r(NULL, " ", &params->cpos);
1100
1101 if (srchash == NULL) {
1102 fprintf(stderr, "missing source hash\n");
1103 goto v3out;
1104 }
1105
1106 if (onehash) {
1107 tgthash = srchash;
1108 } else {
1109 tgthash = strtok_r(NULL, " ", &params->cpos);
1110
1111 if (tgthash == NULL) {
1112 fprintf(stderr, "missing target hash\n");
1113 goto v3out;
1114 }
1115 }
1116
1117 if (LoadSrcTgtVersion2(&params->cpos, tgt, src_blocks, &params->buffer, &params->bufsize,
1118 params->fd, params->stashbase, overlap) == -1) {
1119 goto v3out;
1120 }
1121
1122 tgtbuffer = (uint8_t*) malloc((*tgt)->size * BLOCKSIZE);
1123
1124 if (tgtbuffer == NULL) {
1125 fprintf(stderr, "failed to allocate %d bytes\n", (*tgt)->size * BLOCKSIZE);
1126 goto v3out;
1127 }
1128
1129 if (ReadBlocks(*tgt, tgtbuffer, params->fd) == -1) {
1130 goto v3out;
1131 }
1132
1133 if (VerifyBlocks(tgthash, tgtbuffer, (*tgt)->size, 0) == 0) {
1134 // Target blocks already have expected content, command should be skipped
1135 rc = 1;
1136 goto v3out;
1137 }
1138
1139 if (VerifyBlocks(srchash, params->buffer, *src_blocks, 1) == 0) {
1140 // If source and target blocks overlap, stash the source blocks so we can
1141 // resume from possible write errors
1142 if (*overlap) {
1143 fprintf(stderr, "stashing %d overlapping blocks to %s\n", *src_blocks,
1144 srchash);
1145
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001146 if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, 1,
1147 &stash_exists) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001148 fprintf(stderr, "failed to stash overlapping source blocks\n");
1149 goto v3out;
1150 }
1151
1152 // Can be deleted when the write has completed
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001153 if (!stash_exists) {
1154 params->freestash = srchash;
1155 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001156 }
1157
1158 // Source blocks have expected content, command can proceed
1159 rc = 0;
1160 goto v3out;
1161 }
1162
1163 if (*overlap && LoadStash(params->stashbase, srchash, 1, NULL, &params->buffer,
1164 &params->bufsize, 1) == 0) {
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001165 // Overlapping source blocks were previously stashed, command can proceed.
1166 // We are recovering from an interrupted command, so we don't know if the
1167 // stash can safely be deleted after this command.
Sami Tolvanen90221202014-12-09 16:39:47 +00001168 rc = 0;
1169 goto v3out;
1170 }
1171
1172 // Valid source data not available, update cannot be resumed
1173 fprintf(stderr, "partition has unexpected contents\n");
1174 params->isunresumable = 1;
1175
1176v3out:
1177 if (tgtbuffer) {
1178 free(tgtbuffer);
1179 }
1180
1181 return rc;
1182}
1183
1184static int PerformCommandMove(CommandParameters* params) {
1185 int blocks = 0;
1186 int overlap = 0;
1187 int rc = -1;
1188 int status = 0;
1189 RangeSet* tgt = NULL;
1190
1191 if (!params) {
1192 goto pcmout;
1193 }
1194
1195 if (params->version == 1) {
1196 status = LoadSrcTgtVersion1(&params->cpos, &tgt, &blocks, &params->buffer,
1197 &params->bufsize, params->fd);
1198 } else if (params->version == 2) {
1199 status = LoadSrcTgtVersion2(&params->cpos, &tgt, &blocks, &params->buffer,
1200 &params->bufsize, params->fd, params->stashbase, NULL);
1201 } else if (params->version >= 3) {
1202 status = LoadSrcTgtVersion3(params, &tgt, &blocks, 1, &overlap);
1203 }
1204
1205 if (status == -1) {
1206 fprintf(stderr, "failed to read blocks for move\n");
1207 goto pcmout;
1208 }
1209
1210 if (status == 0) {
1211 params->foundwrites = 1;
1212 } else if (params->foundwrites) {
1213 fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname);
1214 }
1215
1216 if (params->canwrite) {
1217 if (status == 0) {
1218 fprintf(stderr, " moving %d blocks\n", blocks);
1219
1220 if (WriteBlocks(tgt, params->buffer, params->fd) == -1) {
1221 goto pcmout;
1222 }
1223 } else {
1224 fprintf(stderr, "skipping %d already moved blocks\n", blocks);
1225 }
1226
1227 }
1228
1229 if (params->freestash) {
1230 FreeStash(params->stashbase, params->freestash);
1231 params->freestash = NULL;
1232 }
1233
1234 params->written += tgt->size;
1235 rc = 0;
1236
1237pcmout:
1238 if (tgt) {
1239 free(tgt);
1240 }
1241
1242 return rc;
1243}
1244
1245static int PerformCommandStash(CommandParameters* params) {
1246 if (!params) {
1247 return -1;
1248 }
1249
1250 return SaveStash(params->stashbase, &params->cpos, &params->buffer, &params->bufsize,
1251 params->fd, (params->version >= 3), &params->isunresumable);
1252}
1253
1254static int PerformCommandFree(CommandParameters* params) {
1255 if (!params) {
1256 return -1;
1257 }
1258
1259 if (params->createdstash || params->canwrite) {
1260 return FreeStash(params->stashbase, params->cpos);
1261 }
1262
1263 return 0;
1264}
1265
1266static int PerformCommandZero(CommandParameters* params) {
1267 char* range = NULL;
1268 int i;
1269 int j;
1270 int rc = -1;
1271 RangeSet* tgt = NULL;
1272
1273 if (!params) {
1274 goto pczout;
1275 }
1276
1277 range = strtok_r(NULL, " ", &params->cpos);
1278
1279 if (range == NULL) {
1280 fprintf(stderr, "missing target blocks for zero\n");
1281 goto pczout;
1282 }
1283
1284 tgt = parse_range(range);
1285
1286 fprintf(stderr, " zeroing %d blocks\n", tgt->size);
1287
1288 allocate(BLOCKSIZE, &params->buffer, &params->bufsize);
1289 memset(params->buffer, 0, BLOCKSIZE);
1290
1291 if (params->canwrite) {
1292 for (i = 0; i < tgt->count; ++i) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -07001293 if (!check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001294 goto pczout;
1295 }
1296
1297 for (j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) {
1298 if (write_all(params->fd, params->buffer, BLOCKSIZE) == -1) {
1299 goto pczout;
1300 }
1301 }
1302 }
1303 }
1304
1305 if (params->cmdname[0] == 'z') {
Sami Tolvanen6abd52f2015-06-10 15:52:04 +00001306 // Update only for the zero command, as the erase command will call
1307 // this if DEBUG_ERASE is defined.
Sami Tolvanen90221202014-12-09 16:39:47 +00001308 params->written += tgt->size;
1309 }
1310
1311 rc = 0;
1312
1313pczout:
1314 if (tgt) {
1315 free(tgt);
1316 }
1317
1318 return rc;
1319}
1320
1321static int PerformCommandNew(CommandParameters* params) {
1322 char* range = NULL;
1323 int rc = -1;
1324 RangeSet* tgt = NULL;
1325 RangeSinkState rss;
1326
1327 if (!params) {
1328 goto pcnout;
1329 }
1330
1331 range = strtok_r(NULL, " ", &params->cpos);
1332
1333 if (range == NULL) {
1334 goto pcnout;
1335 }
1336
1337 tgt = parse_range(range);
1338
1339 if (params->canwrite) {
1340 fprintf(stderr, " writing %d blocks of new data\n", tgt->size);
1341
1342 rss.fd = params->fd;
1343 rss.tgt = tgt;
1344 rss.p_block = 0;
1345 rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
1346
Elliott Hughes2f5feed2015-04-28 17:24:24 -07001347 if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001348 goto pcnout;
1349 }
1350
1351 pthread_mutex_lock(&params->nti.mu);
1352 params->nti.rss = &rss;
1353 pthread_cond_broadcast(&params->nti.cv);
1354
1355 while (params->nti.rss) {
1356 pthread_cond_wait(&params->nti.cv, &params->nti.mu);
1357 }
1358
1359 pthread_mutex_unlock(&params->nti.mu);
1360 }
1361
1362 params->written += tgt->size;
1363 rc = 0;
1364
1365pcnout:
1366 if (tgt) {
1367 free(tgt);
1368 }
1369
1370 return rc;
1371}
1372
1373static int PerformCommandDiff(CommandParameters* params) {
1374 char* logparams = NULL;
1375 char* value = NULL;
1376 int blocks = 0;
1377 int overlap = 0;
1378 int rc = -1;
1379 int status = 0;
1380 RangeSet* tgt = NULL;
1381 RangeSinkState rss;
1382 size_t len = 0;
1383 size_t offset = 0;
1384 Value patch_value;
1385
1386 if (!params) {
1387 goto pcdout;
1388 }
1389
1390 logparams = strdup(params->cpos);
1391 value = strtok_r(NULL, " ", &params->cpos);
1392
1393 if (value == NULL) {
1394 fprintf(stderr, "missing patch offset for %s\n", params->cmdname);
1395 goto pcdout;
1396 }
1397
1398 offset = strtoul(value, NULL, 0);
1399
1400 value = strtok_r(NULL, " ", &params->cpos);
1401
1402 if (value == NULL) {
1403 fprintf(stderr, "missing patch length for %s\n", params->cmdname);
1404 goto pcdout;
1405 }
1406
1407 len = strtoul(value, NULL, 0);
1408
1409 if (params->version == 1) {
1410 status = LoadSrcTgtVersion1(&params->cpos, &tgt, &blocks, &params->buffer,
1411 &params->bufsize, params->fd);
1412 } else if (params->version == 2) {
1413 status = LoadSrcTgtVersion2(&params->cpos, &tgt, &blocks, &params->buffer,
1414 &params->bufsize, params->fd, params->stashbase, NULL);
1415 } else if (params->version >= 3) {
1416 status = LoadSrcTgtVersion3(params, &tgt, &blocks, 0, &overlap);
1417 }
1418
1419 if (status == -1) {
1420 fprintf(stderr, "failed to read blocks for diff\n");
1421 goto pcdout;
1422 }
1423
1424 if (status == 0) {
1425 params->foundwrites = 1;
1426 } else if (params->foundwrites) {
1427 fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname);
1428 }
1429
1430 if (params->canwrite) {
1431 if (status == 0) {
1432 fprintf(stderr, "patching %d blocks to %d\n", blocks, tgt->size);
1433
1434 patch_value.type = VAL_BLOB;
1435 patch_value.size = len;
1436 patch_value.data = (char*) (params->patch_start + offset);
1437
1438 rss.fd = params->fd;
1439 rss.tgt = tgt;
1440 rss.p_block = 0;
1441 rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
1442
Elliott Hughes2f5feed2015-04-28 17:24:24 -07001443 if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001444 goto pcdout;
1445 }
1446
1447 if (params->cmdname[0] == 'i') { // imgdiff
1448 ApplyImagePatch(params->buffer, blocks * BLOCKSIZE, &patch_value,
1449 &RangeSinkWrite, &rss, NULL, NULL);
1450 } else {
1451 ApplyBSDiffPatch(params->buffer, blocks * BLOCKSIZE, &patch_value,
1452 0, &RangeSinkWrite, &rss, NULL);
1453 }
1454
1455 // We expect the output of the patcher to fill the tgt ranges exactly.
1456 if (rss.p_block != tgt->count || rss.p_remain != 0) {
1457 fprintf(stderr, "range sink underrun?\n");
1458 }
1459 } else {
1460 fprintf(stderr, "skipping %d blocks already patched to %d [%s]\n",
1461 blocks, tgt->size, logparams);
1462 }
1463 }
1464
1465 if (params->freestash) {
1466 FreeStash(params->stashbase, params->freestash);
1467 params->freestash = NULL;
1468 }
1469
1470 params->written += tgt->size;
1471 rc = 0;
1472
1473pcdout:
1474 if (logparams) {
1475 free(logparams);
1476 }
1477
1478 if (tgt) {
1479 free(tgt);
1480 }
1481
1482 return rc;
1483}
1484
1485static int PerformCommandErase(CommandParameters* params) {
1486 char* range = NULL;
1487 int i;
1488 int rc = -1;
1489 RangeSet* tgt = NULL;
1490 struct stat st;
1491 uint64_t blocks[2];
1492
Sami Tolvanen6abd52f2015-06-10 15:52:04 +00001493 if (DEBUG_ERASE) {
1494 return PerformCommandZero(params);
Sami Tolvanen90221202014-12-09 16:39:47 +00001495 }
1496
1497 if (!params) {
1498 goto pceout;
1499 }
1500
1501 if (fstat(params->fd, &st) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001502 fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001503 goto pceout;
1504 }
1505
1506 if (!S_ISBLK(st.st_mode)) {
1507 fprintf(stderr, "not a block device; skipping erase\n");
Sami Tolvanen90221202014-12-09 16:39:47 +00001508 goto pceout;
1509 }
1510
1511 range = strtok_r(NULL, " ", &params->cpos);
1512
1513 if (range == NULL) {
Tao Bao7125f952015-06-30 23:09:12 -07001514 fprintf(stderr, "missing target blocks for zero\n");
Sami Tolvanen90221202014-12-09 16:39:47 +00001515 goto pceout;
1516 }
1517
1518 tgt = parse_range(range);
1519
1520 if (params->canwrite) {
1521 fprintf(stderr, " erasing %d blocks\n", tgt->size);
1522
1523 for (i = 0; i < tgt->count; ++i) {
1524 // offset in bytes
1525 blocks[0] = tgt->pos[i * 2] * (uint64_t) BLOCKSIZE;
1526 // length in bytes
1527 blocks[1] = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * (uint64_t) BLOCKSIZE;
1528
1529 if (ioctl(params->fd, BLKDISCARD, &blocks) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001530 fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno));
Sami Tolvanen92eea1b2015-04-27 11:24:29 +01001531 goto pceout;
Sami Tolvanen90221202014-12-09 16:39:47 +00001532 }
1533 }
1534 }
1535
1536 rc = 0;
1537
1538pceout:
1539 if (tgt) {
1540 free(tgt);
1541 }
1542
1543 return rc;
1544}
1545
1546// Definitions for transfer list command functions
1547typedef int (*CommandFunction)(CommandParameters*);
1548
1549typedef struct {
1550 const char* name;
1551 CommandFunction f;
1552} Command;
1553
1554// CompareCommands and CompareCommandNames are for the hash table
1555
1556static int CompareCommands(const void* c1, const void* c2) {
1557 return strcmp(((const Command*) c1)->name, ((const Command*) c2)->name);
1558}
1559
1560static int CompareCommandNames(const void* c1, const void* c2) {
1561 return strcmp(((const Command*) c1)->name, (const char*) c2);
1562}
1563
1564// HashString is used to hash command names for the hash table
1565
1566static unsigned int HashString(const char *s) {
1567 unsigned int hash = 0;
1568 if (s) {
1569 while (*s) {
1570 hash = hash * 33 + *s++;
1571 }
1572 }
1573 return hash;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001574}
1575
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001576// args:
1577// - block device (or file) to modify in-place
1578// - transfer list (blob)
1579// - new data stream (filename within package.zip)
1580// - patch stream (filename within package.zip, must be uncompressed)
1581
Sami Tolvanen90221202014-12-09 16:39:47 +00001582static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, Expr* argv[],
1583 const Command* commands, int cmdcount, int dryrun) {
1584
1585 char* line = NULL;
1586 char* linesave = NULL;
1587 char* logcmd = NULL;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001588 char* transfer_list = NULL;
Sami Tolvanen90221202014-12-09 16:39:47 +00001589 CommandParameters params;
1590 const Command* cmd = NULL;
1591 const ZipEntry* new_entry = NULL;
1592 const ZipEntry* patch_entry = NULL;
1593 FILE* cmd_pipe = NULL;
1594 HashTable* cmdht = NULL;
1595 int i;
1596 int res;
1597 int rc = -1;
1598 int stash_max_blocks = 0;
1599 int total_blocks = 0;
1600 pthread_attr_t attr;
1601 unsigned int cmdhash;
1602 UpdaterInfo* ui = NULL;
1603 Value* blockdev_filename = NULL;
1604 Value* new_data_fn = NULL;
1605 Value* patch_data_fn = NULL;
1606 Value* transfer_list_value = NULL;
1607 ZipArchive* za = NULL;
1608
1609 memset(&params, 0, sizeof(params));
1610 params.canwrite = !dryrun;
1611
1612 fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001613
Doug Zongker1d5d6092014-08-21 10:47:24 -07001614 if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value,
Sami Tolvanen90221202014-12-09 16:39:47 +00001615 &new_data_fn, &patch_data_fn) < 0) {
1616 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001617 }
1618
1619 if (blockdev_filename->type != VAL_STRING) {
1620 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001621 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001622 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001623 if (transfer_list_value->type != VAL_BLOB) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001624 ErrorAbort(state, "transfer_list argument to %s must be blob", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001625 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001626 }
1627 if (new_data_fn->type != VAL_STRING) {
1628 ErrorAbort(state, "new_data_fn argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001629 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001630 }
1631 if (patch_data_fn->type != VAL_STRING) {
1632 ErrorAbort(state, "patch_data_fn argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001633 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001634 }
1635
Sami Tolvanen90221202014-12-09 16:39:47 +00001636 ui = (UpdaterInfo*) state->cookie;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001637
Sami Tolvanen90221202014-12-09 16:39:47 +00001638 if (ui == NULL) {
1639 goto pbiudone;
1640 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001641
Sami Tolvanen90221202014-12-09 16:39:47 +00001642 cmd_pipe = ui->cmd_pipe;
1643 za = ui->package_zip;
1644
1645 if (cmd_pipe == NULL || za == NULL) {
1646 goto pbiudone;
1647 }
1648
1649 patch_entry = mzFindZipEntry(za, patch_data_fn->data);
1650
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001651 if (patch_entry == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001652 fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data);
1653 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001654 }
1655
Sami Tolvanen90221202014-12-09 16:39:47 +00001656 params.patch_start = ui->package_zip_addr + mzGetZipEntryOffset(patch_entry);
1657 new_entry = mzFindZipEntry(za, new_data_fn->data);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001658
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001659 if (new_entry == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001660 fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data);
1661 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001662 }
1663
Sami Tolvanen90221202014-12-09 16:39:47 +00001664 params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001665
Sami Tolvanen90221202014-12-09 16:39:47 +00001666 if (params.fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001667 fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001668 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001669 }
1670
Sami Tolvanen90221202014-12-09 16:39:47 +00001671 if (params.canwrite) {
1672 params.nti.za = za;
1673 params.nti.entry = new_entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001674
Sami Tolvanen90221202014-12-09 16:39:47 +00001675 pthread_mutex_init(&params.nti.mu, NULL);
1676 pthread_cond_init(&params.nti.cv, NULL);
1677 pthread_attr_init(&attr);
1678 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1679
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001680 int error = pthread_create(&params.thread, &attr, unzip_new_data, &params.nti);
1681 if (error != 0) {
1682 fprintf(stderr, "pthread_create failed: %s\n", strerror(error));
Sami Tolvanen90221202014-12-09 16:39:47 +00001683 goto pbiudone;
1684 }
1685 }
1686
1687 // The data in transfer_list_value is not necessarily null-terminated, so we need
1688 // to copy it to a new buffer and add the null that strtok_r will need.
1689 transfer_list = malloc(transfer_list_value->size + 1);
1690
Doug Zongker1d5d6092014-08-21 10:47:24 -07001691 if (transfer_list == NULL) {
1692 fprintf(stderr, "failed to allocate %zd bytes for transfer list\n",
Sami Tolvanen90221202014-12-09 16:39:47 +00001693 transfer_list_value->size + 1);
1694 goto pbiudone;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001695 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001696
Doug Zongker1d5d6092014-08-21 10:47:24 -07001697 memcpy(transfer_list, transfer_list_value->data, transfer_list_value->size);
1698 transfer_list[transfer_list_value->size] = '\0';
1699
Sami Tolvanen90221202014-12-09 16:39:47 +00001700 // First line in transfer list is the version number
Doug Zongker1d5d6092014-08-21 10:47:24 -07001701 line = strtok_r(transfer_list, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001702 params.version = strtol(line, NULL, 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001703
Sami Tolvanen90221202014-12-09 16:39:47 +00001704 if (params.version < 1 || params.version > 3) {
1705 fprintf(stderr, "unexpected transfer list version [%s]\n", line);
1706 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001707 }
1708
Sami Tolvanen90221202014-12-09 16:39:47 +00001709 fprintf(stderr, "blockimg version is %d\n", params.version);
1710
1711 // Second line in transfer list is the total number of blocks we expect to write
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001712 line = strtok_r(NULL, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001713 total_blocks = strtol(line, NULL, 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001714
Sami Tolvanen90221202014-12-09 16:39:47 +00001715 if (total_blocks < 0) {
1716 ErrorAbort(state, "unexpected block count [%s]\n", line);
1717 goto pbiudone;
1718 } else if (total_blocks == 0) {
1719 rc = 0;
1720 goto pbiudone;
1721 }
1722
1723 if (params.version >= 2) {
1724 // Third line is how many stash entries are needed simultaneously
Doug Zongker52ae67d2014-09-08 12:22:09 -07001725 line = strtok_r(NULL, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001726 fprintf(stderr, "maximum stash entries %s\n", line);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001727
Sami Tolvanen90221202014-12-09 16:39:47 +00001728 // Fourth line is the maximum number of blocks that will be stashed simultaneously
1729 line = strtok_r(NULL, "\n", &linesave);
1730 stash_max_blocks = strtol(line, NULL, 0);
1731
1732 if (stash_max_blocks < 0) {
1733 ErrorAbort(state, "unexpected maximum stash blocks [%s]\n", line);
1734 goto pbiudone;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001735 }
1736
Jesse Zhao1df64d32015-02-17 17:09:23 -08001737 if (stash_max_blocks >= 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001738 res = CreateStash(state, stash_max_blocks, blockdev_filename->data,
1739 &params.stashbase);
1740
1741 if (res == -1) {
1742 goto pbiudone;
1743 }
1744
1745 params.createdstash = res;
1746 }
Doug Zongker52ae67d2014-09-08 12:22:09 -07001747 }
1748
Sami Tolvanen90221202014-12-09 16:39:47 +00001749 // Build a hash table of the available commands
1750 cmdht = mzHashTableCreate(cmdcount, NULL);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001751
Sami Tolvanen90221202014-12-09 16:39:47 +00001752 for (i = 0; i < cmdcount; ++i) {
1753 cmdhash = HashString(commands[i].name);
1754 mzHashTableLookup(cmdht, cmdhash, (void*) &commands[i], CompareCommands, true);
1755 }
1756
1757 // Subsequent lines are all individual transfer commands
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001758 for (line = strtok_r(NULL, "\n", &linesave); line;
1759 line = strtok_r(NULL, "\n", &linesave)) {
Doug Zongker52ae67d2014-09-08 12:22:09 -07001760
Sami Tolvanen90221202014-12-09 16:39:47 +00001761 logcmd = strdup(line);
1762 params.cmdname = strtok_r(line, " ", &params.cpos);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001763
Sami Tolvanen90221202014-12-09 16:39:47 +00001764 if (params.cmdname == NULL) {
1765 fprintf(stderr, "missing command [%s]\n", line);
1766 goto pbiudone;
1767 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001768
Sami Tolvanen90221202014-12-09 16:39:47 +00001769 cmdhash = HashString(params.cmdname);
1770 cmd = (const Command*) mzHashTableLookup(cmdht, cmdhash, params.cmdname,
1771 CompareCommandNames, false);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001772
Sami Tolvanen90221202014-12-09 16:39:47 +00001773 if (cmd == NULL) {
1774 fprintf(stderr, "unexpected command [%s]\n", params.cmdname);
1775 goto pbiudone;
1776 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001777
Sami Tolvanen90221202014-12-09 16:39:47 +00001778 if (cmd->f != NULL && cmd->f(&params) == -1) {
1779 fprintf(stderr, "failed to execute command [%s]\n",
1780 logcmd ? logcmd : params.cmdname);
1781 goto pbiudone;
1782 }
1783
1784 if (logcmd) {
1785 free(logcmd);
1786 logcmd = NULL;
1787 }
1788
1789 if (params.canwrite) {
1790 fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001791 fflush(cmd_pipe);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001792 }
1793 }
1794
Sami Tolvanen90221202014-12-09 16:39:47 +00001795 if (params.canwrite) {
1796 pthread_join(params.thread, NULL);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001797
Sami Tolvanen90221202014-12-09 16:39:47 +00001798 fprintf(stderr, "wrote %d blocks; expected %d\n", params.written, total_blocks);
1799 fprintf(stderr, "max alloc needed was %zu\n", params.bufsize);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001800
Sami Tolvanen90221202014-12-09 16:39:47 +00001801 // Delete stash only after successfully completing the update, as it
1802 // may contain blocks needed to complete the update later.
1803 DeleteStash(params.stashbase);
1804 } else {
1805 fprintf(stderr, "verified partition contents; update may be resumed\n");
1806 }
1807
1808 rc = 0;
1809
1810pbiudone:
1811 if (params.fd != -1) {
1812 if (fsync(params.fd) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001813 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001814 }
Elliott Hughes1857a7f2015-05-15 16:19:20 -07001815 close(params.fd);
Sami Tolvanen90221202014-12-09 16:39:47 +00001816 }
1817
1818 if (logcmd) {
1819 free(logcmd);
1820 }
1821
1822 if (cmdht) {
1823 mzHashTableFree(cmdht);
1824 }
1825
1826 if (params.buffer) {
1827 free(params.buffer);
1828 }
1829
1830 if (transfer_list) {
1831 free(transfer_list);
1832 }
1833
1834 if (blockdev_filename) {
1835 FreeValue(blockdev_filename);
1836 }
1837
1838 if (transfer_list_value) {
1839 FreeValue(transfer_list_value);
1840 }
1841
1842 if (new_data_fn) {
1843 FreeValue(new_data_fn);
1844 }
1845
1846 if (patch_data_fn) {
1847 FreeValue(patch_data_fn);
1848 }
1849
1850 // Only delete the stash if the update cannot be resumed, or it's
1851 // a verification run and we created the stash.
1852 if (params.isunresumable || (!params.canwrite && params.createdstash)) {
1853 DeleteStash(params.stashbase);
1854 }
1855
1856 if (params.stashbase) {
1857 free(params.stashbase);
1858 }
1859
1860 return StringValue(rc == 0 ? strdup("t") : strdup(""));
1861}
1862
1863// The transfer list is a text file containing commands to
1864// transfer data from one place to another on the target
1865// partition. We parse it and execute the commands in order:
1866//
1867// zero [rangeset]
1868// - fill the indicated blocks with zeros
1869//
1870// new [rangeset]
1871// - fill the blocks with data read from the new_data file
1872//
1873// erase [rangeset]
1874// - mark the given blocks as empty
1875//
1876// move <...>
1877// bsdiff <patchstart> <patchlen> <...>
1878// imgdiff <patchstart> <patchlen> <...>
1879// - read the source blocks, apply a patch (or not in the
1880// case of move), write result to target blocks. bsdiff or
1881// imgdiff specifies the type of patch; move means no patch
1882// at all.
1883//
1884// The format of <...> differs between versions 1 and 2;
1885// see the LoadSrcTgtVersion{1,2}() functions for a
1886// description of what's expected.
1887//
1888// stash <stash_id> <src_range>
1889// - (version 2+ only) load the given source range and stash
1890// the data in the given slot of the stash table.
1891//
1892// The creator of the transfer list will guarantee that no block
1893// is read (ie, used as the source for a patch or move) after it
1894// has been written.
1895//
1896// In version 2, the creator will guarantee that a given stash is
1897// loaded (with a stash command) before it's used in a
1898// move/bsdiff/imgdiff command.
1899//
1900// Within one command the source and target ranges may overlap so
1901// in general we need to read the entire source into memory before
1902// writing anything to the target blocks.
1903//
1904// All the patch data is concatenated into one patch_data file in
1905// the update package. It must be stored uncompressed because we
1906// memory-map it in directly from the archive. (Since patches are
1907// already compressed, we lose very little by not compressing
1908// their concatenation.)
1909//
1910// In version 3, commands that read data from the partition (i.e.
1911// move/bsdiff/imgdiff/stash) have one or more additional hashes
1912// before the range parameters, which are used to check if the
1913// command has already been completed and verify the integrity of
1914// the source data.
1915
1916Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]) {
1917 // Commands which are not tested are set to NULL to skip them completely
1918 const Command commands[] = {
1919 { "bsdiff", PerformCommandDiff },
1920 { "erase", NULL },
1921 { "free", PerformCommandFree },
1922 { "imgdiff", PerformCommandDiff },
1923 { "move", PerformCommandMove },
1924 { "new", NULL },
1925 { "stash", PerformCommandStash },
1926 { "zero", NULL }
1927 };
1928
1929 // Perform a dry run without writing to test if an update can proceed
1930 return PerformBlockImageUpdate(name, state, argc, argv, commands,
1931 sizeof(commands) / sizeof(commands[0]), 1);
1932}
1933
1934Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) {
1935 const Command commands[] = {
1936 { "bsdiff", PerformCommandDiff },
1937 { "erase", PerformCommandErase },
1938 { "free", PerformCommandFree },
1939 { "imgdiff", PerformCommandDiff },
1940 { "move", PerformCommandMove },
1941 { "new", PerformCommandNew },
1942 { "stash", PerformCommandStash },
1943 { "zero", PerformCommandZero }
1944 };
1945
1946 return PerformBlockImageUpdate(name, state, argc, argv, commands,
1947 sizeof(commands) / sizeof(commands[0]), 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001948}
1949
1950Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) {
1951 Value* blockdev_filename;
1952 Value* ranges;
1953 const uint8_t* digest = NULL;
1954 if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) {
1955 return NULL;
1956 }
1957
1958 if (blockdev_filename->type != VAL_STRING) {
1959 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
1960 goto done;
1961 }
1962 if (ranges->type != VAL_STRING) {
1963 ErrorAbort(state, "ranges argument to %s must be string", name);
1964 goto done;
1965 }
1966
1967 int fd = open(blockdev_filename->data, O_RDWR);
1968 if (fd < 0) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001969 ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001970 goto done;
1971 }
1972
1973 RangeSet* rs = parse_range(ranges->data);
1974 uint8_t buffer[BLOCKSIZE];
1975
1976 SHA_CTX ctx;
1977 SHA_init(&ctx);
1978
1979 int i, j;
1980 for (i = 0; i < rs->count; ++i) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -07001981 if (!check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001982 ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data,
1983 strerror(errno));
1984 goto done;
1985 }
1986
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001987 for (j = rs->pos[i*2]; j < rs->pos[i*2+1]; ++j) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001988 if (read_all(fd, buffer, BLOCKSIZE) == -1) {
1989 ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data,
1990 strerror(errno));
1991 goto done;
1992 }
1993
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001994 SHA_update(&ctx, buffer, BLOCKSIZE);
1995 }
1996 }
1997 digest = SHA_final(&ctx);
1998 close(fd);
1999
2000 done:
2001 FreeValue(blockdev_filename);
2002 FreeValue(ranges);
2003 if (digest == NULL) {
2004 return StringValue(strdup(""));
2005 } else {
2006 return StringValue(PrintSha1(digest));
2007 }
2008}
2009
2010void RegisterBlockImageFunctions() {
Sami Tolvanen90221202014-12-09 16:39:47 +00002011 RegisterFunction("block_image_verify", BlockImageVerifyFn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07002012 RegisterFunction("block_image_update", BlockImageUpdateFn);
2013 RegisterFunction("range_sha1", RangeSha1Fn);
2014}