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