blob: 77117db05987e0511a603b23afff1eeed727340f [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 Baoba9a42a2015-06-23 23:23:33 -070022#include <linux/fs.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 Tolvanene82fa182015-06-10 15:58:12 +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
Sami Tolvanen90221202014-12-09 16:39:47 +000048#define STASH_DIRECTORY_BASE "/cache/recovery"
49#define STASH_DIRECTORY_MODE 0700
50#define STASH_FILE_MODE 0600
51
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070052char* PrintSha1(const uint8_t* digest);
53
54typedef struct {
55 int count;
56 int size;
57 int pos[0];
58} RangeSet;
59
Sami Tolvanenf2bac042015-05-12 12:48:46 +010060#define RANGESET_MAX_POINTS \
61 ((int)((INT_MAX / sizeof(int)) - sizeof(RangeSet)))
62
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070063static RangeSet* parse_range(char* text) {
64 char* save;
Sami Tolvanenf2bac042015-05-12 12:48:46 +010065 char* token;
Tao Baoba9a42a2015-06-23 23:23:33 -070066 int num;
Sami Tolvanenf2bac042015-05-12 12:48:46 +010067 long int val;
68 RangeSet* out = NULL;
69 size_t bufsize;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070070
Sami Tolvanenf2bac042015-05-12 12:48:46 +010071 if (!text) {
72 goto err;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070073 }
Sami Tolvanenf2bac042015-05-12 12:48:46 +010074
75 token = strtok_r(text, ",", &save);
76
77 if (!token) {
78 goto err;
79 }
80
81 val = strtol(token, NULL, 0);
82
83 if (val < 2 || val > RANGESET_MAX_POINTS) {
84 goto err;
85 } else if (val % 2) {
86 goto err; // must be even
87 }
88
89 num = (int) val;
90 bufsize = sizeof(RangeSet) + num * sizeof(int);
91
Tao Baoba9a42a2015-06-23 23:23:33 -070092 out = reinterpret_cast<RangeSet*>(malloc(bufsize));
Sami Tolvanenf2bac042015-05-12 12:48:46 +010093
94 if (!out) {
95 fprintf(stderr, "failed to allocate range of %zu bytes\n", bufsize);
96 goto err;
97 }
98
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070099 out->count = num / 2;
100 out->size = 0;
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100101
Tao Baoba9a42a2015-06-23 23:23:33 -0700102 for (int i = 0; i < num; ++i) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100103 token = strtok_r(NULL, ",", &save);
104
105 if (!token) {
106 goto err;
107 }
108
109 val = strtol(token, NULL, 0);
110
111 if (val < 0 || val > INT_MAX) {
112 goto err;
113 }
114
115 out->pos[i] = (int) val;
116
117 if (i % 2) {
118 if (out->pos[i - 1] >= out->pos[i]) {
119 goto err; // empty or negative range
120 }
121
122 if (out->size > INT_MAX - out->pos[i]) {
123 goto err; // overflow
124 }
125
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700126 out->size += out->pos[i];
127 } else {
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100128 if (out->size < 0) {
129 goto err;
130 }
131
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700132 out->size -= out->pos[i];
133 }
134 }
135
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100136 if (out->size <= 0) {
137 goto err;
138 }
139
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700140 return out;
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100141
142err:
143 fprintf(stderr, "failed to parse range '%s'\n", text ? text : "NULL");
144 exit(1);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700145}
146
Sami Tolvanen90221202014-12-09 16:39:47 +0000147static int range_overlaps(RangeSet* r1, RangeSet* r2) {
148 int i, j, r1_0, r1_1, r2_0, r2_1;
149
150 if (!r1 || !r2) {
151 return 0;
152 }
153
154 for (i = 0; i < r1->count; ++i) {
155 r1_0 = r1->pos[i * 2];
156 r1_1 = r1->pos[i * 2 + 1];
157
158 for (j = 0; j < r2->count; ++j) {
159 r2_0 = r2->pos[j * 2];
160 r2_1 = r2->pos[j * 2 + 1];
161
Tao Baoc0f56ad2015-06-25 14:00:31 -0700162 if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000163 return 1;
164 }
165 }
166 }
167
168 return 0;
169}
170
171static int read_all(int fd, uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700172 size_t so_far = 0;
173 while (so_far < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700174 ssize_t r = TEMP_FAILURE_RETRY(read(fd, data+so_far, size-so_far));
175 if (r == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700176 fprintf(stderr, "read failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000177 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700178 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700179 so_far += r;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700180 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000181 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700182}
183
Sami Tolvanen90221202014-12-09 16:39:47 +0000184static int write_all(int fd, const uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700185 size_t written = 0;
186 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700187 ssize_t w = TEMP_FAILURE_RETRY(write(fd, data+written, size-written));
188 if (w == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700189 fprintf(stderr, "write failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000190 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700191 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700192 written += w;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700193 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000194
Sami Tolvanen90221202014-12-09 16:39:47 +0000195 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700196}
197
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700198static bool check_lseek(int fd, off64_t offset, int whence) {
199 off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
200 if (rc == -1) {
201 fprintf(stderr, "lseek64 failed: %s\n", strerror(errno));
202 return false;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700203 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700204 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700205}
206
207static void allocate(size_t size, uint8_t** buffer, size_t* buffer_alloc) {
208 // if the buffer's big enough, reuse it.
209 if (size <= *buffer_alloc) return;
210
211 free(*buffer);
212
213 *buffer = (uint8_t*) malloc(size);
214 if (*buffer == NULL) {
Doug Zongker1d5d6092014-08-21 10:47:24 -0700215 fprintf(stderr, "failed to allocate %zu bytes\n", size);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700216 exit(1);
217 }
218 *buffer_alloc = size;
219}
220
221typedef struct {
222 int fd;
223 RangeSet* tgt;
224 int p_block;
225 size_t p_remain;
226} RangeSinkState;
227
228static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) {
229 RangeSinkState* rss = (RangeSinkState*) token;
230
231 if (rss->p_remain <= 0) {
232 fprintf(stderr, "range sink write overrun");
Sami Tolvanen90221202014-12-09 16:39:47 +0000233 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700234 }
235
236 ssize_t written = 0;
237 while (size > 0) {
238 size_t write_now = size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000239
240 if (rss->p_remain < write_now) {
241 write_now = rss->p_remain;
242 }
243
244 if (write_all(rss->fd, data, write_now) == -1) {
245 break;
246 }
247
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700248 data += write_now;
249 size -= write_now;
250
251 rss->p_remain -= write_now;
252 written += write_now;
253
254 if (rss->p_remain == 0) {
255 // move to the next block
256 ++rss->p_block;
257 if (rss->p_block < rss->tgt->count) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000258 rss->p_remain = (rss->tgt->pos[rss->p_block * 2 + 1] -
259 rss->tgt->pos[rss->p_block * 2]) * BLOCKSIZE;
260
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700261 if (!check_lseek(rss->fd, (off64_t)rss->tgt->pos[rss->p_block*2] * BLOCKSIZE,
262 SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000263 break;
264 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700265 } else {
266 // we can't write any more; return how many bytes have
267 // been written so far.
Sami Tolvanen90221202014-12-09 16:39:47 +0000268 break;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700269 }
270 }
271 }
272
273 return written;
274}
275
276// All of the data for all the 'new' transfers is contained in one
277// file in the update package, concatenated together in the order in
278// which transfers.list will need it. We want to stream it out of the
279// archive (it's compressed) without writing it to a temp file, but we
280// can't write each section until it's that transfer's turn to go.
281//
282// To achieve this, we expand the new data from the archive in a
283// background thread, and block that threads 'receive uncompressed
284// data' function until the main thread has reached a point where we
285// want some new data to be written. We signal the background thread
286// with the destination for the data and block the main thread,
287// waiting for the background thread to complete writing that section.
288// Then it signals the main thread to wake up and goes back to
289// blocking waiting for a transfer.
290//
291// NewThreadInfo is the struct used to pass information back and forth
292// between the two threads. When the main thread wants some data
293// written, it sets rss to the destination location and signals the
294// condition. When the background thread is done writing, it clears
295// rss and signals the condition again.
296
297typedef struct {
298 ZipArchive* za;
299 const ZipEntry* entry;
300
301 RangeSinkState* rss;
302
303 pthread_mutex_t mu;
304 pthread_cond_t cv;
305} NewThreadInfo;
306
307static bool receive_new_data(const unsigned char* data, int size, void* cookie) {
308 NewThreadInfo* nti = (NewThreadInfo*) cookie;
309
310 while (size > 0) {
311 // Wait for nti->rss to be non-NULL, indicating some of this
312 // data is wanted.
313 pthread_mutex_lock(&nti->mu);
314 while (nti->rss == NULL) {
315 pthread_cond_wait(&nti->cv, &nti->mu);
316 }
317 pthread_mutex_unlock(&nti->mu);
318
319 // At this point nti->rss is set, and we own it. The main
320 // thread is waiting for it to disappear from nti.
321 ssize_t written = RangeSinkWrite(data, size, nti->rss);
322 data += written;
323 size -= written;
324
325 if (nti->rss->p_block == nti->rss->tgt->count) {
326 // we have written all the bytes desired by this rss.
327
328 pthread_mutex_lock(&nti->mu);
329 nti->rss = NULL;
330 pthread_cond_broadcast(&nti->cv);
331 pthread_mutex_unlock(&nti->mu);
332 }
333 }
334
335 return true;
336}
337
338static void* unzip_new_data(void* cookie) {
339 NewThreadInfo* nti = (NewThreadInfo*) cookie;
340 mzProcessZipEntryContents(nti->za, nti->entry, receive_new_data, nti);
341 return NULL;
342}
343
Sami Tolvanen90221202014-12-09 16:39:47 +0000344static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) {
345 int i;
346 size_t p = 0;
347 size_t size;
348
349 if (!src || !buffer) {
350 return -1;
351 }
352
353 for (i = 0; i < src->count; ++i) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700354 if (!check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000355 return -1;
356 }
357
358 size = (src->pos[i * 2 + 1] - src->pos[i * 2]) * BLOCKSIZE;
359
360 if (read_all(fd, buffer + p, size) == -1) {
361 return -1;
362 }
363
364 p += size;
365 }
366
367 return 0;
368}
369
370static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) {
371 int i;
372 size_t p = 0;
373 size_t size;
374
375 if (!tgt || !buffer) {
376 return -1;
377 }
378
379 for (i = 0; i < tgt->count; ++i) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700380 if (!check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000381 return -1;
382 }
383
384 size = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * BLOCKSIZE;
385
386 if (write_all(fd, buffer + p, size) == -1) {
387 return -1;
388 }
389
390 p += size;
391 }
392
393 return 0;
394}
395
Doug Zongker52ae67d2014-09-08 12:22:09 -0700396// Do a source/target load for move/bsdiff/imgdiff in version 1.
397// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
398// to parse the remainder of the string as:
399//
400// <src_range> <tgt_range>
401//
402// The source range is loaded into the provided buffer, reallocating
403// it to make it larger if necessary. The target ranges are returned
404// in *tgt, if tgt is non-NULL.
405
Sami Tolvanen90221202014-12-09 16:39:47 +0000406static int LoadSrcTgtVersion1(char** wordsave, RangeSet** tgt, int* src_blocks,
Doug Zongker52ae67d2014-09-08 12:22:09 -0700407 uint8_t** buffer, size_t* buffer_alloc, int fd) {
408 char* word;
Sami Tolvanen90221202014-12-09 16:39:47 +0000409 int rc;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700410
Sami Tolvanen90221202014-12-09 16:39:47 +0000411 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700412 RangeSet* src = parse_range(word);
413
414 if (tgt != NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000415 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700416 *tgt = parse_range(word);
417 }
418
419 allocate(src->size * BLOCKSIZE, buffer, buffer_alloc);
Sami Tolvanen90221202014-12-09 16:39:47 +0000420 rc = ReadBlocks(src, *buffer, fd);
421 *src_blocks = src->size;
422
423 free(src);
424 return rc;
425}
426
427static int VerifyBlocks(const char *expected, const uint8_t *buffer,
428 size_t blocks, int printerror) {
429 char* hexdigest = NULL;
430 int rc = -1;
431 uint8_t digest[SHA_DIGEST_SIZE];
432
433 if (!expected || !buffer) {
434 return rc;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700435 }
436
Sami Tolvanen90221202014-12-09 16:39:47 +0000437 SHA_hash(buffer, blocks * BLOCKSIZE, digest);
438 hexdigest = PrintSha1(digest);
439
440 if (hexdigest != NULL) {
441 rc = strcmp(expected, hexdigest);
442
443 if (rc != 0 && printerror) {
444 fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
445 expected, hexdigest);
446 }
447
448 free(hexdigest);
449 }
450
451 return rc;
452}
453
454static char* GetStashFileName(const char* base, const char* id, const char* postfix) {
455 char* fn;
456 int len;
457 int res;
458
459 if (base == NULL) {
460 return NULL;
461 }
462
463 if (id == NULL) {
464 id = "";
465 }
466
467 if (postfix == NULL) {
468 postfix = "";
469 }
470
471 len = strlen(STASH_DIRECTORY_BASE) + 1 + strlen(base) + 1 + strlen(id) + strlen(postfix) + 1;
Tao Baoba9a42a2015-06-23 23:23:33 -0700472 fn = reinterpret_cast<char*>(malloc(len));
Sami Tolvanen90221202014-12-09 16:39:47 +0000473
474 if (fn == NULL) {
475 fprintf(stderr, "failed to malloc %d bytes for fn\n", len);
476 return NULL;
477 }
478
479 res = snprintf(fn, len, STASH_DIRECTORY_BASE "/%s/%s%s", base, id, postfix);
480
481 if (res < 0 || res >= len) {
482 fprintf(stderr, "failed to format file name (return value %d)\n", res);
483 free(fn);
484 return NULL;
485 }
486
487 return fn;
488}
489
490typedef void (*StashCallback)(const char*, void*);
491
492// Does a best effort enumeration of stash files. Ignores possible non-file
493// items in the stash directory and continues despite of errors. Calls the
494// 'callback' function for each file and passes 'data' to the function as a
495// parameter.
496
497static void EnumerateStash(const char* dirname, StashCallback callback, void* data) {
498 char* fn;
499 DIR* directory;
500 int len;
501 int res;
502 struct dirent* item;
503
504 if (dirname == NULL || callback == NULL) {
505 return;
506 }
507
508 directory = opendir(dirname);
509
510 if (directory == NULL) {
511 if (errno != ENOENT) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700512 fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000513 }
514 return;
515 }
516
517 while ((item = readdir(directory)) != NULL) {
518 if (item->d_type != DT_REG) {
519 continue;
520 }
521
522 len = strlen(dirname) + 1 + strlen(item->d_name) + 1;
Tao Baoba9a42a2015-06-23 23:23:33 -0700523 fn = reinterpret_cast<char*>(malloc(len));
Sami Tolvanen90221202014-12-09 16:39:47 +0000524
525 if (fn == NULL) {
526 fprintf(stderr, "failed to malloc %d bytes for fn\n", len);
527 continue;
528 }
529
530 res = snprintf(fn, len, "%s/%s", dirname, item->d_name);
531
532 if (res < 0 || res >= len) {
533 fprintf(stderr, "failed to format file name (return value %d)\n", res);
534 free(fn);
535 continue;
536 }
537
538 callback(fn, data);
539 free(fn);
540 }
541
542 if (closedir(directory) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700543 fprintf(stderr, "closedir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000544 }
545}
546
547static void UpdateFileSize(const char* fn, void* data) {
548 int* size = (int*) data;
549 struct stat st;
550
551 if (!fn || !data) {
552 return;
553 }
554
555 if (stat(fn, &st) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700556 fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000557 return;
558 }
559
560 *size += st.st_size;
561}
562
563// Deletes the stash directory and all files in it. Assumes that it only
564// contains files. There is nothing we can do about unlikely, but possible
565// errors, so they are merely logged.
566
567static void DeleteFile(const char* fn, void* data) {
568 if (fn) {
569 fprintf(stderr, "deleting %s\n", fn);
570
571 if (unlink(fn) == -1 && errno != ENOENT) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700572 fprintf(stderr, "unlink \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000573 }
574 }
575}
576
577static void DeletePartial(const char* fn, void* data) {
578 if (fn && strstr(fn, ".partial") != NULL) {
579 DeleteFile(fn, data);
580 }
581}
582
583static void DeleteStash(const char* base) {
584 char* dirname;
585
586 if (base == NULL) {
587 return;
588 }
589
590 dirname = GetStashFileName(base, NULL, NULL);
591
592 if (dirname == NULL) {
593 return;
594 }
595
596 fprintf(stderr, "deleting stash %s\n", base);
597 EnumerateStash(dirname, DeleteFile, NULL);
598
599 if (rmdir(dirname) == -1) {
600 if (errno != ENOENT && errno != ENOTDIR) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700601 fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000602 }
603 }
604
605 free(dirname);
606}
607
608static int LoadStash(const char* base, const char* id, int verify, int* blocks, uint8_t** buffer,
609 size_t* buffer_alloc, int printnoent) {
610 char *fn = NULL;
611 int blockcount = 0;
612 int fd = -1;
613 int rc = -1;
614 int res;
615 struct stat st;
616
617 if (!base || !id || !buffer || !buffer_alloc) {
618 goto lsout;
619 }
620
621 if (!blocks) {
622 blocks = &blockcount;
623 }
624
625 fn = GetStashFileName(base, id, NULL);
626
627 if (fn == NULL) {
628 goto lsout;
629 }
630
631 res = stat(fn, &st);
632
633 if (res == -1) {
634 if (errno != ENOENT || printnoent) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700635 fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000636 }
637 goto lsout;
638 }
639
640 fprintf(stderr, " loading %s\n", fn);
641
642 if ((st.st_size % BLOCKSIZE) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700643 fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d",
644 fn, static_cast<int64_t>(st.st_size), BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000645 goto lsout;
646 }
647
648 fd = TEMP_FAILURE_RETRY(open(fn, O_RDONLY));
649
650 if (fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700651 fprintf(stderr, "open \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000652 goto lsout;
653 }
654
655 allocate(st.st_size, buffer, buffer_alloc);
656
657 if (read_all(fd, *buffer, st.st_size) == -1) {
658 goto lsout;
659 }
660
661 *blocks = st.st_size / BLOCKSIZE;
662
663 if (verify && VerifyBlocks(id, *buffer, *blocks, 1) != 0) {
664 fprintf(stderr, "unexpected contents in %s\n", fn);
665 DeleteFile(fn, NULL);
666 goto lsout;
667 }
668
669 rc = 0;
670
671lsout:
672 if (fd != -1) {
Elliott Hughesb3ac6762015-05-28 23:06:17 -0700673 close(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000674 }
675
676 if (fn) {
677 free(fn);
678 }
679
680 return rc;
681}
682
683static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buffer,
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100684 int checkspace, int *exists) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000685 char *fn = NULL;
686 char *cn = NULL;
687 int fd = -1;
688 int rc = -1;
689 int res;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100690 struct stat st;
Sami Tolvanen90221202014-12-09 16:39:47 +0000691
692 if (base == NULL || buffer == NULL) {
693 goto wsout;
694 }
695
696 if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) {
697 fprintf(stderr, "not enough space to write stash\n");
698 goto wsout;
699 }
700
701 fn = GetStashFileName(base, id, ".partial");
702 cn = GetStashFileName(base, id, NULL);
703
704 if (fn == NULL || cn == NULL) {
705 goto wsout;
706 }
707
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100708 if (exists) {
709 res = stat(cn, &st);
710
711 if (res == 0) {
712 // The file already exists and since the name is the hash of the contents,
713 // it's safe to assume the contents are identical (accidental hash collisions
714 // are unlikely)
715 fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn);
716 *exists = 1;
717 rc = 0;
718 goto wsout;
719 }
720
721 *exists = 0;
722 }
723
Sami Tolvanen90221202014-12-09 16:39:47 +0000724 fprintf(stderr, " writing %d blocks to %s\n", blocks, cn);
725
Tao Bao187efff2015-07-27 14:07:08 -0700726 fd = TEMP_FAILURE_RETRY(open(fn, O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE));
Sami Tolvanen90221202014-12-09 16:39:47 +0000727
728 if (fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700729 fprintf(stderr, "failed to create \"%s\": %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000730 goto wsout;
731 }
732
733 if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) {
734 goto wsout;
735 }
736
737 if (fsync(fd) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700738 fprintf(stderr, "fsync \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000739 goto wsout;
740 }
741
742 if (rename(fn, cn) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700743 fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn, cn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000744 goto wsout;
745 }
746
747 rc = 0;
748
749wsout:
750 if (fd != -1) {
Elliott Hughesb47afed2015-05-15 16:19:20 -0700751 close(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000752 }
753
754 if (fn) {
755 free(fn);
756 }
757
758 if (cn) {
759 free(cn);
760 }
761
762 return rc;
763}
764
765// Creates a directory for storing stash files and checks if the /cache partition
766// hash enough space for the expected amount of blocks we need to store. Returns
767// >0 if we created the directory, zero if it existed already, and <0 of failure.
768
769static int CreateStash(State* state, int maxblocks, const char* blockdev, char** base) {
770 char* dirname = NULL;
771 const uint8_t* digest;
772 int rc = -1;
773 int res;
774 int size = 0;
775 SHA_CTX ctx;
776 struct stat st;
777
778 if (blockdev == NULL || base == NULL) {
779 goto csout;
780 }
781
782 // Stash directory should be different for each partition to avoid conflicts
783 // when updating multiple partitions at the same time, so we use the hash of
784 // the block device name as the base directory
785 SHA_init(&ctx);
786 SHA_update(&ctx, blockdev, strlen(blockdev));
787 digest = SHA_final(&ctx);
788 *base = PrintSha1(digest);
789
790 if (*base == NULL) {
791 goto csout;
792 }
793
794 dirname = GetStashFileName(*base, NULL, NULL);
795
796 if (dirname == NULL) {
797 goto csout;
798 }
799
800 res = stat(dirname, &st);
801
802 if (res == -1 && errno != ENOENT) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700803 ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000804 goto csout;
805 } else if (res != 0) {
806 fprintf(stderr, "creating stash %s\n", dirname);
807 res = mkdir(dirname, STASH_DIRECTORY_MODE);
808
809 if (res != 0) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700810 ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000811 goto csout;
812 }
813
814 if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) {
815 ErrorAbort(state, "not enough space for stash\n");
816 goto csout;
817 }
818
819 rc = 1; // Created directory
820 goto csout;
821 }
822
823 fprintf(stderr, "using existing stash %s\n", dirname);
824
825 // If the directory already exists, calculate the space already allocated to
826 // stash files and check if there's enough for all required blocks. Delete any
827 // partially completed stash files first.
828
829 EnumerateStash(dirname, DeletePartial, NULL);
830 EnumerateStash(dirname, UpdateFileSize, &size);
831
832 size = (maxblocks * BLOCKSIZE) - size;
833
834 if (size > 0 && CacheSizeCheck(size) != 0) {
835 ErrorAbort(state, "not enough space for stash (%d more needed)\n", size);
836 goto csout;
837 }
838
839 rc = 0; // Using existing directory
840
841csout:
842 if (dirname) {
843 free(dirname);
844 }
845
846 return rc;
847}
848
849static int SaveStash(const char* base, char** wordsave, uint8_t** buffer, size_t* buffer_alloc,
850 int fd, int usehash, int* isunresumable) {
851 char *id = NULL;
Sami Tolvanen90221202014-12-09 16:39:47 +0000852 int blocks = 0;
853
854 if (!wordsave || !buffer || !buffer_alloc || !isunresumable) {
855 return -1;
856 }
857
858 id = strtok_r(NULL, " ", wordsave);
859
860 if (id == NULL) {
861 fprintf(stderr, "missing id field in stash command\n");
862 return -1;
863 }
864
865 if (usehash && LoadStash(base, id, 1, &blocks, buffer, buffer_alloc, 0) == 0) {
866 // Stash file already exists and has expected contents. Do not
867 // read from source again, as the source may have been already
868 // overwritten during a previous attempt.
869 return 0;
870 }
871
872 if (LoadSrcTgtVersion1(wordsave, NULL, &blocks, buffer, buffer_alloc, fd) == -1) {
873 return -1;
874 }
875
876 if (usehash && VerifyBlocks(id, *buffer, blocks, 1) != 0) {
877 // Source blocks have unexpected contents. If we actually need this
878 // data later, this is an unrecoverable error. However, the command
879 // that uses the data may have already completed previously, so the
880 // possible failure will occur during source block verification.
881 fprintf(stderr, "failed to load source blocks for stash %s\n", id);
882 return 0;
883 }
884
885 fprintf(stderr, "stashing %d blocks to %s\n", blocks, id);
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100886 return WriteStash(base, id, blocks, *buffer, 0, NULL);
Sami Tolvanen90221202014-12-09 16:39:47 +0000887}
888
889static int FreeStash(const char* base, const char* id) {
890 char *fn = NULL;
891
892 if (base == NULL || id == NULL) {
893 return -1;
894 }
895
896 fn = GetStashFileName(base, id, NULL);
897
898 if (fn == NULL) {
899 return -1;
900 }
901
902 DeleteFile(fn, NULL);
903 free(fn);
904
905 return 0;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700906}
907
908static void MoveRange(uint8_t* dest, RangeSet* locs, const uint8_t* source) {
909 // source contains packed data, which we want to move to the
910 // locations given in *locs in the dest buffer. source and dest
911 // may be the same buffer.
912
913 int start = locs->size;
914 int i;
915 for (i = locs->count-1; i >= 0; --i) {
916 int blocks = locs->pos[i*2+1] - locs->pos[i*2];
917 start -= blocks;
918 memmove(dest + (locs->pos[i*2] * BLOCKSIZE), source + (start * BLOCKSIZE),
919 blocks * BLOCKSIZE);
920 }
921}
922
923// Do a source/target load for move/bsdiff/imgdiff in version 2.
924// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
925// to parse the remainder of the string as one of:
926//
927// <tgt_range> <src_block_count> <src_range>
928// (loads data from source image only)
929//
930// <tgt_range> <src_block_count> - <[stash_id:stash_range] ...>
931// (loads data from stashes only)
932//
933// <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...>
934// (loads data from both source image and stashes)
935//
936// On return, buffer is filled with the loaded source data (rearranged
937// and combined with stashed data as necessary). buffer may be
938// reallocated if needed to accommodate the source data. *tgt is the
Sami Tolvanen90221202014-12-09 16:39:47 +0000939// target RangeSet. Any stashes required are loaded using LoadStash.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700940
Sami Tolvanen90221202014-12-09 16:39:47 +0000941static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks,
Doug Zongker52ae67d2014-09-08 12:22:09 -0700942 uint8_t** buffer, size_t* buffer_alloc, int fd,
Sami Tolvanen90221202014-12-09 16:39:47 +0000943 const char* stashbase, int* overlap) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700944 char* word;
Sami Tolvanen90221202014-12-09 16:39:47 +0000945 char* colonsave;
946 char* colon;
Sami Tolvanen90221202014-12-09 16:39:47 +0000947 int res;
948 RangeSet* locs;
949 size_t stashalloc = 0;
950 uint8_t* stash = NULL;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700951
952 if (tgt != NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000953 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700954 *tgt = parse_range(word);
955 }
956
Sami Tolvanen90221202014-12-09 16:39:47 +0000957 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700958 *src_blocks = strtol(word, NULL, 0);
959
960 allocate(*src_blocks * BLOCKSIZE, buffer, buffer_alloc);
961
Sami Tolvanen90221202014-12-09 16:39:47 +0000962 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700963 if (word[0] == '-' && word[1] == '\0') {
964 // no source ranges, only stashes
965 } else {
966 RangeSet* src = parse_range(word);
Sami Tolvanen90221202014-12-09 16:39:47 +0000967 res = ReadBlocks(src, *buffer, fd);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700968
Sami Tolvanen90221202014-12-09 16:39:47 +0000969 if (overlap && tgt) {
970 *overlap = range_overlaps(src, *tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700971 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000972
Doug Zongker52ae67d2014-09-08 12:22:09 -0700973 free(src);
974
Sami Tolvanen90221202014-12-09 16:39:47 +0000975 if (res == -1) {
976 return -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700977 }
978
Sami Tolvanen90221202014-12-09 16:39:47 +0000979 word = strtok_r(NULL, " ", wordsave);
980 if (word == NULL) {
981 // no stashes, only source range
982 return 0;
983 }
984
985 locs = parse_range(word);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700986 MoveRange(*buffer, locs, *buffer);
Sami Tolvanen90221202014-12-09 16:39:47 +0000987 free(locs);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700988 }
989
Sami Tolvanen90221202014-12-09 16:39:47 +0000990 while ((word = strtok_r(NULL, " ", wordsave)) != NULL) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700991 // Each word is a an index into the stash table, a colon, and
992 // then a rangeset describing where in the source block that
993 // stashed data should go.
Sami Tolvanen90221202014-12-09 16:39:47 +0000994 colonsave = NULL;
995 colon = strtok_r(word, ":", &colonsave);
996
997 res = LoadStash(stashbase, colon, 0, NULL, &stash, &stashalloc, 1);
998
999 if (res == -1) {
1000 // These source blocks will fail verification if used later, but we
1001 // will let the caller decide if this is a fatal failure
1002 fprintf(stderr, "failed to load stash %s\n", colon);
1003 continue;
1004 }
1005
Doug Zongker52ae67d2014-09-08 12:22:09 -07001006 colon = strtok_r(NULL, ":", &colonsave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001007 locs = parse_range(colon);
1008
1009 MoveRange(*buffer, locs, stash);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001010 free(locs);
1011 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001012
1013 if (stash) {
1014 free(stash);
1015 }
1016
1017 return 0;
1018}
1019
1020// Parameters for transfer list command functions
1021typedef struct {
1022 char* cmdname;
1023 char* cpos;
1024 char* freestash;
1025 char* stashbase;
1026 int canwrite;
1027 int createdstash;
1028 int fd;
1029 int foundwrites;
1030 int isunresumable;
1031 int version;
1032 int written;
1033 NewThreadInfo nti;
1034 pthread_t thread;
1035 size_t bufsize;
1036 uint8_t* buffer;
1037 uint8_t* patch_start;
1038} CommandParameters;
1039
1040// Do a source/target load for move/bsdiff/imgdiff in version 3.
1041//
1042// Parameters are the same as for LoadSrcTgtVersion2, except for 'onehash', which
1043// tells the function whether to expect separate source and targe block hashes, or
1044// if they are both the same and only one hash should be expected, and
1045// 'isunresumable', which receives a non-zero value if block verification fails in
1046// a way that the update cannot be resumed anymore.
1047//
1048// If the function is unable to load the necessary blocks or their contents don't
1049// match the hashes, the return value is -1 and the command should be aborted.
1050//
1051// If the return value is 1, the command has already been completed according to
1052// the contents of the target blocks, and should not be performed again.
1053//
1054// If the return value is 0, source blocks have expected content and the command
1055// can be performed.
1056
1057static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* src_blocks,
1058 int onehash, int* overlap) {
1059 char* srchash = NULL;
1060 char* tgthash = NULL;
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001061 int stash_exists = 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001062 int rc = -1;
1063 uint8_t* tgtbuffer = NULL;
1064
1065 if (!params|| !tgt || !src_blocks || !overlap) {
1066 goto v3out;
1067 }
1068
1069 srchash = strtok_r(NULL, " ", &params->cpos);
1070
1071 if (srchash == NULL) {
1072 fprintf(stderr, "missing source hash\n");
1073 goto v3out;
1074 }
1075
1076 if (onehash) {
1077 tgthash = srchash;
1078 } else {
1079 tgthash = strtok_r(NULL, " ", &params->cpos);
1080
1081 if (tgthash == NULL) {
1082 fprintf(stderr, "missing target hash\n");
1083 goto v3out;
1084 }
1085 }
1086
1087 if (LoadSrcTgtVersion2(&params->cpos, tgt, src_blocks, &params->buffer, &params->bufsize,
1088 params->fd, params->stashbase, overlap) == -1) {
1089 goto v3out;
1090 }
1091
1092 tgtbuffer = (uint8_t*) malloc((*tgt)->size * BLOCKSIZE);
1093
1094 if (tgtbuffer == NULL) {
1095 fprintf(stderr, "failed to allocate %d bytes\n", (*tgt)->size * BLOCKSIZE);
1096 goto v3out;
1097 }
1098
1099 if (ReadBlocks(*tgt, tgtbuffer, params->fd) == -1) {
1100 goto v3out;
1101 }
1102
1103 if (VerifyBlocks(tgthash, tgtbuffer, (*tgt)->size, 0) == 0) {
1104 // Target blocks already have expected content, command should be skipped
1105 rc = 1;
1106 goto v3out;
1107 }
1108
1109 if (VerifyBlocks(srchash, params->buffer, *src_blocks, 1) == 0) {
1110 // If source and target blocks overlap, stash the source blocks so we can
1111 // resume from possible write errors
1112 if (*overlap) {
1113 fprintf(stderr, "stashing %d overlapping blocks to %s\n", *src_blocks,
1114 srchash);
1115
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001116 if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, 1,
1117 &stash_exists) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001118 fprintf(stderr, "failed to stash overlapping source blocks\n");
1119 goto v3out;
1120 }
1121
1122 // Can be deleted when the write has completed
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001123 if (!stash_exists) {
1124 params->freestash = srchash;
1125 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001126 }
1127
1128 // Source blocks have expected content, command can proceed
1129 rc = 0;
1130 goto v3out;
1131 }
1132
1133 if (*overlap && LoadStash(params->stashbase, srchash, 1, NULL, &params->buffer,
1134 &params->bufsize, 1) == 0) {
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001135 // Overlapping source blocks were previously stashed, command can proceed.
1136 // We are recovering from an interrupted command, so we don't know if the
1137 // stash can safely be deleted after this command.
Sami Tolvanen90221202014-12-09 16:39:47 +00001138 rc = 0;
1139 goto v3out;
1140 }
1141
1142 // Valid source data not available, update cannot be resumed
1143 fprintf(stderr, "partition has unexpected contents\n");
1144 params->isunresumable = 1;
1145
1146v3out:
1147 if (tgtbuffer) {
1148 free(tgtbuffer);
1149 }
1150
1151 return rc;
1152}
1153
1154static int PerformCommandMove(CommandParameters* params) {
1155 int blocks = 0;
1156 int overlap = 0;
1157 int rc = -1;
1158 int status = 0;
1159 RangeSet* tgt = NULL;
1160
1161 if (!params) {
1162 goto pcmout;
1163 }
1164
1165 if (params->version == 1) {
1166 status = LoadSrcTgtVersion1(&params->cpos, &tgt, &blocks, &params->buffer,
1167 &params->bufsize, params->fd);
1168 } else if (params->version == 2) {
1169 status = LoadSrcTgtVersion2(&params->cpos, &tgt, &blocks, &params->buffer,
1170 &params->bufsize, params->fd, params->stashbase, NULL);
1171 } else if (params->version >= 3) {
1172 status = LoadSrcTgtVersion3(params, &tgt, &blocks, 1, &overlap);
1173 }
1174
1175 if (status == -1) {
1176 fprintf(stderr, "failed to read blocks for move\n");
1177 goto pcmout;
1178 }
1179
1180 if (status == 0) {
1181 params->foundwrites = 1;
1182 } else if (params->foundwrites) {
1183 fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname);
1184 }
1185
1186 if (params->canwrite) {
1187 if (status == 0) {
1188 fprintf(stderr, " moving %d blocks\n", blocks);
1189
1190 if (WriteBlocks(tgt, params->buffer, params->fd) == -1) {
1191 goto pcmout;
1192 }
1193 } else {
1194 fprintf(stderr, "skipping %d already moved blocks\n", blocks);
1195 }
1196
1197 }
1198
1199 if (params->freestash) {
1200 FreeStash(params->stashbase, params->freestash);
1201 params->freestash = NULL;
1202 }
1203
1204 params->written += tgt->size;
1205 rc = 0;
1206
1207pcmout:
1208 if (tgt) {
1209 free(tgt);
1210 }
1211
1212 return rc;
1213}
1214
1215static int PerformCommandStash(CommandParameters* params) {
1216 if (!params) {
1217 return -1;
1218 }
1219
1220 return SaveStash(params->stashbase, &params->cpos, &params->buffer, &params->bufsize,
1221 params->fd, (params->version >= 3), &params->isunresumable);
1222}
1223
1224static int PerformCommandFree(CommandParameters* params) {
1225 if (!params) {
1226 return -1;
1227 }
1228
1229 if (params->createdstash || params->canwrite) {
1230 return FreeStash(params->stashbase, params->cpos);
1231 }
1232
1233 return 0;
1234}
1235
1236static int PerformCommandZero(CommandParameters* params) {
1237 char* range = NULL;
1238 int i;
1239 int j;
1240 int rc = -1;
1241 RangeSet* tgt = NULL;
1242
1243 if (!params) {
1244 goto pczout;
1245 }
1246
1247 range = strtok_r(NULL, " ", &params->cpos);
1248
1249 if (range == NULL) {
1250 fprintf(stderr, "missing target blocks for zero\n");
1251 goto pczout;
1252 }
1253
1254 tgt = parse_range(range);
1255
1256 fprintf(stderr, " zeroing %d blocks\n", tgt->size);
1257
1258 allocate(BLOCKSIZE, &params->buffer, &params->bufsize);
1259 memset(params->buffer, 0, BLOCKSIZE);
1260
1261 if (params->canwrite) {
1262 for (i = 0; i < tgt->count; ++i) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -07001263 if (!check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001264 goto pczout;
1265 }
1266
1267 for (j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) {
1268 if (write_all(params->fd, params->buffer, BLOCKSIZE) == -1) {
1269 goto pczout;
1270 }
1271 }
1272 }
1273 }
1274
1275 if (params->cmdname[0] == 'z') {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001276 // Update only for the zero command, as the erase command will call
1277 // this if DEBUG_ERASE is defined.
Sami Tolvanen90221202014-12-09 16:39:47 +00001278 params->written += tgt->size;
1279 }
1280
1281 rc = 0;
1282
1283pczout:
1284 if (tgt) {
1285 free(tgt);
1286 }
1287
1288 return rc;
1289}
1290
1291static int PerformCommandNew(CommandParameters* params) {
1292 char* range = NULL;
1293 int rc = -1;
1294 RangeSet* tgt = NULL;
1295 RangeSinkState rss;
1296
1297 if (!params) {
1298 goto pcnout;
1299 }
1300
1301 range = strtok_r(NULL, " ", &params->cpos);
1302
1303 if (range == NULL) {
1304 goto pcnout;
1305 }
1306
1307 tgt = parse_range(range);
1308
1309 if (params->canwrite) {
1310 fprintf(stderr, " writing %d blocks of new data\n", tgt->size);
1311
1312 rss.fd = params->fd;
1313 rss.tgt = tgt;
1314 rss.p_block = 0;
1315 rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
1316
Elliott Hughes7bad7c42015-04-28 17:24:24 -07001317 if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001318 goto pcnout;
1319 }
1320
1321 pthread_mutex_lock(&params->nti.mu);
1322 params->nti.rss = &rss;
1323 pthread_cond_broadcast(&params->nti.cv);
1324
1325 while (params->nti.rss) {
1326 pthread_cond_wait(&params->nti.cv, &params->nti.mu);
1327 }
1328
1329 pthread_mutex_unlock(&params->nti.mu);
1330 }
1331
1332 params->written += tgt->size;
1333 rc = 0;
1334
1335pcnout:
1336 if (tgt) {
1337 free(tgt);
1338 }
1339
1340 return rc;
1341}
1342
1343static int PerformCommandDiff(CommandParameters* params) {
1344 char* logparams = NULL;
1345 char* value = NULL;
1346 int blocks = 0;
1347 int overlap = 0;
1348 int rc = -1;
1349 int status = 0;
1350 RangeSet* tgt = NULL;
1351 RangeSinkState rss;
1352 size_t len = 0;
1353 size_t offset = 0;
1354 Value patch_value;
1355
1356 if (!params) {
1357 goto pcdout;
1358 }
1359
1360 logparams = strdup(params->cpos);
1361 value = strtok_r(NULL, " ", &params->cpos);
1362
1363 if (value == NULL) {
1364 fprintf(stderr, "missing patch offset for %s\n", params->cmdname);
1365 goto pcdout;
1366 }
1367
1368 offset = strtoul(value, NULL, 0);
1369
1370 value = strtok_r(NULL, " ", &params->cpos);
1371
1372 if (value == NULL) {
1373 fprintf(stderr, "missing patch length for %s\n", params->cmdname);
1374 goto pcdout;
1375 }
1376
1377 len = strtoul(value, NULL, 0);
1378
1379 if (params->version == 1) {
1380 status = LoadSrcTgtVersion1(&params->cpos, &tgt, &blocks, &params->buffer,
1381 &params->bufsize, params->fd);
1382 } else if (params->version == 2) {
1383 status = LoadSrcTgtVersion2(&params->cpos, &tgt, &blocks, &params->buffer,
1384 &params->bufsize, params->fd, params->stashbase, NULL);
1385 } else if (params->version >= 3) {
1386 status = LoadSrcTgtVersion3(params, &tgt, &blocks, 0, &overlap);
1387 }
1388
1389 if (status == -1) {
1390 fprintf(stderr, "failed to read blocks for diff\n");
1391 goto pcdout;
1392 }
1393
1394 if (status == 0) {
1395 params->foundwrites = 1;
1396 } else if (params->foundwrites) {
1397 fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname);
1398 }
1399
1400 if (params->canwrite) {
1401 if (status == 0) {
1402 fprintf(stderr, "patching %d blocks to %d\n", blocks, tgt->size);
1403
1404 patch_value.type = VAL_BLOB;
1405 patch_value.size = len;
1406 patch_value.data = (char*) (params->patch_start + offset);
1407
1408 rss.fd = params->fd;
1409 rss.tgt = tgt;
1410 rss.p_block = 0;
1411 rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
1412
Elliott Hughes7bad7c42015-04-28 17:24:24 -07001413 if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001414 goto pcdout;
1415 }
1416
1417 if (params->cmdname[0] == 'i') { // imgdiff
1418 ApplyImagePatch(params->buffer, blocks * BLOCKSIZE, &patch_value,
1419 &RangeSinkWrite, &rss, NULL, NULL);
1420 } else {
1421 ApplyBSDiffPatch(params->buffer, blocks * BLOCKSIZE, &patch_value,
1422 0, &RangeSinkWrite, &rss, NULL);
1423 }
1424
1425 // We expect the output of the patcher to fill the tgt ranges exactly.
1426 if (rss.p_block != tgt->count || rss.p_remain != 0) {
1427 fprintf(stderr, "range sink underrun?\n");
1428 }
1429 } else {
1430 fprintf(stderr, "skipping %d blocks already patched to %d [%s]\n",
1431 blocks, tgt->size, logparams);
1432 }
1433 }
1434
1435 if (params->freestash) {
1436 FreeStash(params->stashbase, params->freestash);
1437 params->freestash = NULL;
1438 }
1439
1440 params->written += tgt->size;
1441 rc = 0;
1442
1443pcdout:
1444 if (logparams) {
1445 free(logparams);
1446 }
1447
1448 if (tgt) {
1449 free(tgt);
1450 }
1451
1452 return rc;
1453}
1454
1455static int PerformCommandErase(CommandParameters* params) {
1456 char* range = NULL;
1457 int i;
1458 int rc = -1;
1459 RangeSet* tgt = NULL;
1460 struct stat st;
1461 uint64_t blocks[2];
1462
Sami Tolvanene82fa182015-06-10 15:58:12 +00001463 if (DEBUG_ERASE) {
1464 return PerformCommandZero(params);
Sami Tolvanen90221202014-12-09 16:39:47 +00001465 }
1466
1467 if (!params) {
1468 goto pceout;
1469 }
1470
1471 if (fstat(params->fd, &st) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001472 fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001473 goto pceout;
1474 }
1475
1476 if (!S_ISBLK(st.st_mode)) {
1477 fprintf(stderr, "not a block device; skipping erase\n");
Sami Tolvanen90221202014-12-09 16:39:47 +00001478 goto pceout;
1479 }
1480
1481 range = strtok_r(NULL, " ", &params->cpos);
1482
1483 if (range == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001484 fprintf(stderr, "missing target blocks for erase\n");
Sami Tolvanen90221202014-12-09 16:39:47 +00001485 goto pceout;
1486 }
1487
1488 tgt = parse_range(range);
1489
1490 if (params->canwrite) {
1491 fprintf(stderr, " erasing %d blocks\n", tgt->size);
1492
1493 for (i = 0; i < tgt->count; ++i) {
1494 // offset in bytes
1495 blocks[0] = tgt->pos[i * 2] * (uint64_t) BLOCKSIZE;
1496 // length in bytes
1497 blocks[1] = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * (uint64_t) BLOCKSIZE;
1498
1499 if (ioctl(params->fd, BLKDISCARD, &blocks) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001500 fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno));
Sami Tolvanencc2428c2015-04-27 11:24:29 +01001501 goto pceout;
Sami Tolvanen90221202014-12-09 16:39:47 +00001502 }
1503 }
1504 }
1505
1506 rc = 0;
1507
1508pceout:
1509 if (tgt) {
1510 free(tgt);
1511 }
1512
1513 return rc;
1514}
1515
1516// Definitions for transfer list command functions
1517typedef int (*CommandFunction)(CommandParameters*);
1518
1519typedef struct {
1520 const char* name;
1521 CommandFunction f;
1522} Command;
1523
1524// CompareCommands and CompareCommandNames are for the hash table
1525
1526static int CompareCommands(const void* c1, const void* c2) {
1527 return strcmp(((const Command*) c1)->name, ((const Command*) c2)->name);
1528}
1529
1530static int CompareCommandNames(const void* c1, const void* c2) {
1531 return strcmp(((const Command*) c1)->name, (const char*) c2);
1532}
1533
1534// HashString is used to hash command names for the hash table
1535
1536static unsigned int HashString(const char *s) {
1537 unsigned int hash = 0;
1538 if (s) {
1539 while (*s) {
1540 hash = hash * 33 + *s++;
1541 }
1542 }
1543 return hash;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001544}
1545
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001546// args:
1547// - block device (or file) to modify in-place
1548// - transfer list (blob)
1549// - new data stream (filename within package.zip)
1550// - patch stream (filename within package.zip, must be uncompressed)
1551
Sami Tolvanen90221202014-12-09 16:39:47 +00001552static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, Expr* argv[],
1553 const Command* commands, int cmdcount, int dryrun) {
1554
1555 char* line = NULL;
1556 char* linesave = NULL;
1557 char* logcmd = NULL;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001558 char* transfer_list = NULL;
Sami Tolvanen90221202014-12-09 16:39:47 +00001559 CommandParameters params;
1560 const Command* cmd = NULL;
1561 const ZipEntry* new_entry = NULL;
1562 const ZipEntry* patch_entry = NULL;
1563 FILE* cmd_pipe = NULL;
1564 HashTable* cmdht = NULL;
1565 int i;
1566 int res;
1567 int rc = -1;
1568 int stash_max_blocks = 0;
1569 int total_blocks = 0;
1570 pthread_attr_t attr;
1571 unsigned int cmdhash;
1572 UpdaterInfo* ui = NULL;
1573 Value* blockdev_filename = NULL;
1574 Value* new_data_fn = NULL;
1575 Value* patch_data_fn = NULL;
1576 Value* transfer_list_value = NULL;
1577 ZipArchive* za = NULL;
1578
1579 memset(&params, 0, sizeof(params));
1580 params.canwrite = !dryrun;
1581
1582 fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001583
Doug Zongker1d5d6092014-08-21 10:47:24 -07001584 if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value,
Sami Tolvanen90221202014-12-09 16:39:47 +00001585 &new_data_fn, &patch_data_fn) < 0) {
1586 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001587 }
1588
1589 if (blockdev_filename->type != VAL_STRING) {
1590 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001591 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001592 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001593 if (transfer_list_value->type != VAL_BLOB) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001594 ErrorAbort(state, "transfer_list argument to %s must be blob", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001595 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001596 }
1597 if (new_data_fn->type != VAL_STRING) {
1598 ErrorAbort(state, "new_data_fn argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001599 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001600 }
1601 if (patch_data_fn->type != VAL_STRING) {
1602 ErrorAbort(state, "patch_data_fn argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001603 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001604 }
1605
Sami Tolvanen90221202014-12-09 16:39:47 +00001606 ui = (UpdaterInfo*) state->cookie;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001607
Sami Tolvanen90221202014-12-09 16:39:47 +00001608 if (ui == NULL) {
1609 goto pbiudone;
1610 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001611
Sami Tolvanen90221202014-12-09 16:39:47 +00001612 cmd_pipe = ui->cmd_pipe;
1613 za = ui->package_zip;
1614
1615 if (cmd_pipe == NULL || za == NULL) {
1616 goto pbiudone;
1617 }
1618
1619 patch_entry = mzFindZipEntry(za, patch_data_fn->data);
1620
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001621 if (patch_entry == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001622 fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data);
1623 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001624 }
1625
Sami Tolvanen90221202014-12-09 16:39:47 +00001626 params.patch_start = ui->package_zip_addr + mzGetZipEntryOffset(patch_entry);
1627 new_entry = mzFindZipEntry(za, new_data_fn->data);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001628
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001629 if (new_entry == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001630 fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data);
1631 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001632 }
1633
Sami Tolvanen90221202014-12-09 16:39:47 +00001634 params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001635
Sami Tolvanen90221202014-12-09 16:39:47 +00001636 if (params.fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001637 fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001638 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001639 }
1640
Sami Tolvanen90221202014-12-09 16:39:47 +00001641 if (params.canwrite) {
1642 params.nti.za = za;
1643 params.nti.entry = new_entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001644
Sami Tolvanen90221202014-12-09 16:39:47 +00001645 pthread_mutex_init(&params.nti.mu, NULL);
1646 pthread_cond_init(&params.nti.cv, NULL);
1647 pthread_attr_init(&attr);
1648 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1649
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001650 int error = pthread_create(&params.thread, &attr, unzip_new_data, &params.nti);
1651 if (error != 0) {
1652 fprintf(stderr, "pthread_create failed: %s\n", strerror(error));
Sami Tolvanen90221202014-12-09 16:39:47 +00001653 goto pbiudone;
1654 }
1655 }
1656
1657 // The data in transfer_list_value is not necessarily null-terminated, so we need
1658 // to copy it to a new buffer and add the null that strtok_r will need.
Tao Baoba9a42a2015-06-23 23:23:33 -07001659 transfer_list = reinterpret_cast<char*>(malloc(transfer_list_value->size + 1));
Sami Tolvanen90221202014-12-09 16:39:47 +00001660
Doug Zongker1d5d6092014-08-21 10:47:24 -07001661 if (transfer_list == NULL) {
1662 fprintf(stderr, "failed to allocate %zd bytes for transfer list\n",
Sami Tolvanen90221202014-12-09 16:39:47 +00001663 transfer_list_value->size + 1);
1664 goto pbiudone;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001665 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001666
Doug Zongker1d5d6092014-08-21 10:47:24 -07001667 memcpy(transfer_list, transfer_list_value->data, transfer_list_value->size);
1668 transfer_list[transfer_list_value->size] = '\0';
1669
Sami Tolvanen90221202014-12-09 16:39:47 +00001670 // First line in transfer list is the version number
Doug Zongker1d5d6092014-08-21 10:47:24 -07001671 line = strtok_r(transfer_list, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001672 params.version = strtol(line, NULL, 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001673
Sami Tolvanen90221202014-12-09 16:39:47 +00001674 if (params.version < 1 || params.version > 3) {
1675 fprintf(stderr, "unexpected transfer list version [%s]\n", line);
1676 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001677 }
1678
Sami Tolvanen90221202014-12-09 16:39:47 +00001679 fprintf(stderr, "blockimg version is %d\n", params.version);
1680
1681 // Second line in transfer list is the total number of blocks we expect to write
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001682 line = strtok_r(NULL, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001683 total_blocks = strtol(line, NULL, 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001684
Sami Tolvanen90221202014-12-09 16:39:47 +00001685 if (total_blocks < 0) {
1686 ErrorAbort(state, "unexpected block count [%s]\n", line);
1687 goto pbiudone;
1688 } else if (total_blocks == 0) {
1689 rc = 0;
1690 goto pbiudone;
1691 }
1692
1693 if (params.version >= 2) {
1694 // Third line is how many stash entries are needed simultaneously
Doug Zongker52ae67d2014-09-08 12:22:09 -07001695 line = strtok_r(NULL, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001696 fprintf(stderr, "maximum stash entries %s\n", line);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001697
Sami Tolvanen90221202014-12-09 16:39:47 +00001698 // Fourth line is the maximum number of blocks that will be stashed simultaneously
1699 line = strtok_r(NULL, "\n", &linesave);
1700 stash_max_blocks = strtol(line, NULL, 0);
1701
1702 if (stash_max_blocks < 0) {
1703 ErrorAbort(state, "unexpected maximum stash blocks [%s]\n", line);
1704 goto pbiudone;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001705 }
1706
Jesse Zhao1df64d32015-02-17 17:09:23 -08001707 if (stash_max_blocks >= 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001708 res = CreateStash(state, stash_max_blocks, blockdev_filename->data,
1709 &params.stashbase);
1710
1711 if (res == -1) {
1712 goto pbiudone;
1713 }
1714
1715 params.createdstash = res;
1716 }
Doug Zongker52ae67d2014-09-08 12:22:09 -07001717 }
1718
Sami Tolvanen90221202014-12-09 16:39:47 +00001719 // Build a hash table of the available commands
1720 cmdht = mzHashTableCreate(cmdcount, NULL);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001721
Sami Tolvanen90221202014-12-09 16:39:47 +00001722 for (i = 0; i < cmdcount; ++i) {
1723 cmdhash = HashString(commands[i].name);
1724 mzHashTableLookup(cmdht, cmdhash, (void*) &commands[i], CompareCommands, true);
1725 }
1726
1727 // Subsequent lines are all individual transfer commands
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001728 for (line = strtok_r(NULL, "\n", &linesave); line;
1729 line = strtok_r(NULL, "\n", &linesave)) {
Doug Zongker52ae67d2014-09-08 12:22:09 -07001730
Sami Tolvanen90221202014-12-09 16:39:47 +00001731 logcmd = strdup(line);
1732 params.cmdname = strtok_r(line, " ", &params.cpos);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001733
Sami Tolvanen90221202014-12-09 16:39:47 +00001734 if (params.cmdname == NULL) {
1735 fprintf(stderr, "missing command [%s]\n", line);
1736 goto pbiudone;
1737 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001738
Sami Tolvanen90221202014-12-09 16:39:47 +00001739 cmdhash = HashString(params.cmdname);
1740 cmd = (const Command*) mzHashTableLookup(cmdht, cmdhash, params.cmdname,
1741 CompareCommandNames, false);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001742
Sami Tolvanen90221202014-12-09 16:39:47 +00001743 if (cmd == NULL) {
1744 fprintf(stderr, "unexpected command [%s]\n", params.cmdname);
1745 goto pbiudone;
1746 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001747
Sami Tolvanen90221202014-12-09 16:39:47 +00001748 if (cmd->f != NULL && cmd->f(&params) == -1) {
1749 fprintf(stderr, "failed to execute command [%s]\n",
1750 logcmd ? logcmd : params.cmdname);
1751 goto pbiudone;
1752 }
1753
1754 if (logcmd) {
1755 free(logcmd);
1756 logcmd = NULL;
1757 }
1758
1759 if (params.canwrite) {
Tao Bao187efff2015-07-27 14:07:08 -07001760 if (fsync(params.fd) == -1) {
1761 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
1762 goto pbiudone;
1763 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001764 fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001765 fflush(cmd_pipe);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001766 }
1767 }
1768
Sami Tolvanen90221202014-12-09 16:39:47 +00001769 if (params.canwrite) {
1770 pthread_join(params.thread, NULL);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001771
Sami Tolvanen90221202014-12-09 16:39:47 +00001772 fprintf(stderr, "wrote %d blocks; expected %d\n", params.written, total_blocks);
1773 fprintf(stderr, "max alloc needed was %zu\n", params.bufsize);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001774
Sami Tolvanen90221202014-12-09 16:39:47 +00001775 // Delete stash only after successfully completing the update, as it
1776 // may contain blocks needed to complete the update later.
1777 DeleteStash(params.stashbase);
1778 } else {
1779 fprintf(stderr, "verified partition contents; update may be resumed\n");
1780 }
1781
1782 rc = 0;
1783
1784pbiudone:
1785 if (params.fd != -1) {
1786 if (fsync(params.fd) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001787 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001788 }
Elliott Hughesb47afed2015-05-15 16:19:20 -07001789 close(params.fd);
Sami Tolvanen90221202014-12-09 16:39:47 +00001790 }
1791
1792 if (logcmd) {
1793 free(logcmd);
1794 }
1795
1796 if (cmdht) {
1797 mzHashTableFree(cmdht);
1798 }
1799
1800 if (params.buffer) {
1801 free(params.buffer);
1802 }
1803
1804 if (transfer_list) {
1805 free(transfer_list);
1806 }
1807
1808 if (blockdev_filename) {
1809 FreeValue(blockdev_filename);
1810 }
1811
1812 if (transfer_list_value) {
1813 FreeValue(transfer_list_value);
1814 }
1815
1816 if (new_data_fn) {
1817 FreeValue(new_data_fn);
1818 }
1819
1820 if (patch_data_fn) {
1821 FreeValue(patch_data_fn);
1822 }
1823
1824 // Only delete the stash if the update cannot be resumed, or it's
1825 // a verification run and we created the stash.
1826 if (params.isunresumable || (!params.canwrite && params.createdstash)) {
1827 DeleteStash(params.stashbase);
1828 }
1829
1830 if (params.stashbase) {
1831 free(params.stashbase);
1832 }
1833
1834 return StringValue(rc == 0 ? strdup("t") : strdup(""));
1835}
1836
1837// The transfer list is a text file containing commands to
1838// transfer data from one place to another on the target
1839// partition. We parse it and execute the commands in order:
1840//
1841// zero [rangeset]
1842// - fill the indicated blocks with zeros
1843//
1844// new [rangeset]
1845// - fill the blocks with data read from the new_data file
1846//
1847// erase [rangeset]
1848// - mark the given blocks as empty
1849//
1850// move <...>
1851// bsdiff <patchstart> <patchlen> <...>
1852// imgdiff <patchstart> <patchlen> <...>
1853// - read the source blocks, apply a patch (or not in the
1854// case of move), write result to target blocks. bsdiff or
1855// imgdiff specifies the type of patch; move means no patch
1856// at all.
1857//
1858// The format of <...> differs between versions 1 and 2;
1859// see the LoadSrcTgtVersion{1,2}() functions for a
1860// description of what's expected.
1861//
1862// stash <stash_id> <src_range>
1863// - (version 2+ only) load the given source range and stash
1864// the data in the given slot of the stash table.
1865//
1866// The creator of the transfer list will guarantee that no block
1867// is read (ie, used as the source for a patch or move) after it
1868// has been written.
1869//
1870// In version 2, the creator will guarantee that a given stash is
1871// loaded (with a stash command) before it's used in a
1872// move/bsdiff/imgdiff command.
1873//
1874// Within one command the source and target ranges may overlap so
1875// in general we need to read the entire source into memory before
1876// writing anything to the target blocks.
1877//
1878// All the patch data is concatenated into one patch_data file in
1879// the update package. It must be stored uncompressed because we
1880// memory-map it in directly from the archive. (Since patches are
1881// already compressed, we lose very little by not compressing
1882// their concatenation.)
1883//
1884// In version 3, commands that read data from the partition (i.e.
1885// move/bsdiff/imgdiff/stash) have one or more additional hashes
1886// before the range parameters, which are used to check if the
1887// command has already been completed and verify the integrity of
1888// the source data.
1889
1890Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]) {
1891 // Commands which are not tested are set to NULL to skip them completely
1892 const Command commands[] = {
1893 { "bsdiff", PerformCommandDiff },
1894 { "erase", NULL },
1895 { "free", PerformCommandFree },
1896 { "imgdiff", PerformCommandDiff },
1897 { "move", PerformCommandMove },
1898 { "new", NULL },
1899 { "stash", PerformCommandStash },
1900 { "zero", NULL }
1901 };
1902
1903 // Perform a dry run without writing to test if an update can proceed
1904 return PerformBlockImageUpdate(name, state, argc, argv, commands,
1905 sizeof(commands) / sizeof(commands[0]), 1);
1906}
1907
1908Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) {
1909 const Command commands[] = {
1910 { "bsdiff", PerformCommandDiff },
1911 { "erase", PerformCommandErase },
1912 { "free", PerformCommandFree },
1913 { "imgdiff", PerformCommandDiff },
1914 { "move", PerformCommandMove },
1915 { "new", PerformCommandNew },
1916 { "stash", PerformCommandStash },
1917 { "zero", PerformCommandZero }
1918 };
1919
1920 return PerformBlockImageUpdate(name, state, argc, argv, commands,
1921 sizeof(commands) / sizeof(commands[0]), 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001922}
1923
1924Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) {
1925 Value* blockdev_filename;
1926 Value* ranges;
1927 const uint8_t* digest = NULL;
1928 if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) {
1929 return NULL;
1930 }
1931
1932 if (blockdev_filename->type != VAL_STRING) {
1933 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
1934 goto done;
1935 }
1936 if (ranges->type != VAL_STRING) {
1937 ErrorAbort(state, "ranges argument to %s must be string", name);
1938 goto done;
1939 }
1940
Tao Baoba9a42a2015-06-23 23:23:33 -07001941 int fd;
1942 fd = open(blockdev_filename->data, O_RDWR);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001943 if (fd < 0) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001944 ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001945 goto done;
1946 }
1947
Tao Baoba9a42a2015-06-23 23:23:33 -07001948 RangeSet* rs;
1949 rs = parse_range(ranges->data);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001950 uint8_t buffer[BLOCKSIZE];
1951
1952 SHA_CTX ctx;
1953 SHA_init(&ctx);
1954
1955 int i, j;
1956 for (i = 0; i < rs->count; ++i) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -07001957 if (!check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001958 ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data,
1959 strerror(errno));
1960 goto done;
1961 }
1962
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001963 for (j = rs->pos[i*2]; j < rs->pos[i*2+1]; ++j) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001964 if (read_all(fd, buffer, BLOCKSIZE) == -1) {
1965 ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data,
1966 strerror(errno));
1967 goto done;
1968 }
1969
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001970 SHA_update(&ctx, buffer, BLOCKSIZE);
1971 }
1972 }
1973 digest = SHA_final(&ctx);
1974 close(fd);
1975
1976 done:
1977 FreeValue(blockdev_filename);
1978 FreeValue(ranges);
1979 if (digest == NULL) {
1980 return StringValue(strdup(""));
1981 } else {
1982 return StringValue(PrintSha1(digest));
1983 }
1984}
1985
1986void RegisterBlockImageFunctions() {
Sami Tolvanen90221202014-12-09 16:39:47 +00001987 RegisterFunction("block_image_verify", BlockImageVerifyFn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001988 RegisterFunction("block_image_update", BlockImageUpdateFn);
1989 RegisterFunction("range_sha1", RangeSha1Fn);
1990}