blob: a2786c8320fdfd277cad798303425488364e58c9 [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
58static RangeSet* parse_range(char* text) {
59 char* save;
60 int num;
61 num = strtol(strtok_r(text, ",", &save), NULL, 0);
62
63 RangeSet* out = malloc(sizeof(RangeSet) + num * sizeof(int));
64 if (out == NULL) {
Doug Zongker52ae67d2014-09-08 12:22:09 -070065 fprintf(stderr, "failed to allocate range of %zu bytes\n",
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070066 sizeof(RangeSet) + num * sizeof(int));
67 exit(1);
68 }
69 out->count = num / 2;
70 out->size = 0;
71 int i;
72 for (i = 0; i < num; ++i) {
73 out->pos[i] = strtol(strtok_r(NULL, ",", &save), NULL, 0);
74 if (i%2) {
75 out->size += out->pos[i];
76 } else {
77 out->size -= out->pos[i];
78 }
79 }
80
81 return out;
82}
83
Sami Tolvanen90221202014-12-09 16:39:47 +000084static int range_overlaps(RangeSet* r1, RangeSet* r2) {
85 int i, j, r1_0, r1_1, r2_0, r2_1;
86
87 if (!r1 || !r2) {
88 return 0;
89 }
90
91 for (i = 0; i < r1->count; ++i) {
92 r1_0 = r1->pos[i * 2];
93 r1_1 = r1->pos[i * 2 + 1];
94
95 for (j = 0; j < r2->count; ++j) {
96 r2_0 = r2->pos[j * 2];
97 r2_1 = r2->pos[j * 2 + 1];
98
99 if (!(r2_0 > r1_1 || r1_0 > r2_1)) {
100 return 1;
101 }
102 }
103 }
104
105 return 0;
106}
107
108static int read_all(int fd, uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700109 size_t so_far = 0;
110 while (so_far < size) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700111 ssize_t r = TEMP_FAILURE_RETRY(read(fd, data+so_far, size-so_far));
112 if (r == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700113 fprintf(stderr, "read failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000114 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700115 }
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700116 so_far += r;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700117 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000118 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700119}
120
Sami Tolvanen90221202014-12-09 16:39:47 +0000121static int write_all(int fd, const uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700122 size_t written = 0;
123 while (written < size) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700124 ssize_t w = TEMP_FAILURE_RETRY(write(fd, data+written, size-written));
125 if (w == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700126 fprintf(stderr, "write failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000127 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700128 }
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700129 written += w;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700130 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000131
132 if (fsync(fd) == -1) {
133 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
134 return -1;
135 }
136
137 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700138}
139
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700140static bool check_lseek(int fd, off64_t offset, int whence) {
141 off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
142 if (rc == -1) {
143 fprintf(stderr, "lseek64 failed: %s\n", strerror(errno));
144 return false;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700145 }
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700146 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700147}
148
149static void allocate(size_t size, uint8_t** buffer, size_t* buffer_alloc) {
150 // if the buffer's big enough, reuse it.
151 if (size <= *buffer_alloc) return;
152
153 free(*buffer);
154
155 *buffer = (uint8_t*) malloc(size);
156 if (*buffer == NULL) {
Doug Zongker1d5d6092014-08-21 10:47:24 -0700157 fprintf(stderr, "failed to allocate %zu bytes\n", size);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700158 exit(1);
159 }
160 *buffer_alloc = size;
161}
162
163typedef struct {
164 int fd;
165 RangeSet* tgt;
166 int p_block;
167 size_t p_remain;
168} RangeSinkState;
169
170static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) {
171 RangeSinkState* rss = (RangeSinkState*) token;
172
173 if (rss->p_remain <= 0) {
174 fprintf(stderr, "range sink write overrun");
Sami Tolvanen90221202014-12-09 16:39:47 +0000175 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700176 }
177
178 ssize_t written = 0;
179 while (size > 0) {
180 size_t write_now = size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000181
182 if (rss->p_remain < write_now) {
183 write_now = rss->p_remain;
184 }
185
186 if (write_all(rss->fd, data, write_now) == -1) {
187 break;
188 }
189
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700190 data += write_now;
191 size -= write_now;
192
193 rss->p_remain -= write_now;
194 written += write_now;
195
196 if (rss->p_remain == 0) {
197 // move to the next block
198 ++rss->p_block;
199 if (rss->p_block < rss->tgt->count) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000200 rss->p_remain = (rss->tgt->pos[rss->p_block * 2 + 1] -
201 rss->tgt->pos[rss->p_block * 2]) * BLOCKSIZE;
202
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700203 if (!check_lseek(rss->fd, (off64_t)rss->tgt->pos[rss->p_block*2] * BLOCKSIZE,
204 SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000205 break;
206 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700207 } else {
208 // we can't write any more; return how many bytes have
209 // been written so far.
Sami Tolvanen90221202014-12-09 16:39:47 +0000210 break;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700211 }
212 }
213 }
214
215 return written;
216}
217
218// All of the data for all the 'new' transfers is contained in one
219// file in the update package, concatenated together in the order in
220// which transfers.list will need it. We want to stream it out of the
221// archive (it's compressed) without writing it to a temp file, but we
222// can't write each section until it's that transfer's turn to go.
223//
224// To achieve this, we expand the new data from the archive in a
225// background thread, and block that threads 'receive uncompressed
226// data' function until the main thread has reached a point where we
227// want some new data to be written. We signal the background thread
228// with the destination for the data and block the main thread,
229// waiting for the background thread to complete writing that section.
230// Then it signals the main thread to wake up and goes back to
231// blocking waiting for a transfer.
232//
233// NewThreadInfo is the struct used to pass information back and forth
234// between the two threads. When the main thread wants some data
235// written, it sets rss to the destination location and signals the
236// condition. When the background thread is done writing, it clears
237// rss and signals the condition again.
238
239typedef struct {
240 ZipArchive* za;
241 const ZipEntry* entry;
242
243 RangeSinkState* rss;
244
245 pthread_mutex_t mu;
246 pthread_cond_t cv;
247} NewThreadInfo;
248
249static bool receive_new_data(const unsigned char* data, int size, void* cookie) {
250 NewThreadInfo* nti = (NewThreadInfo*) cookie;
251
252 while (size > 0) {
253 // Wait for nti->rss to be non-NULL, indicating some of this
254 // data is wanted.
255 pthread_mutex_lock(&nti->mu);
256 while (nti->rss == NULL) {
257 pthread_cond_wait(&nti->cv, &nti->mu);
258 }
259 pthread_mutex_unlock(&nti->mu);
260
261 // At this point nti->rss is set, and we own it. The main
262 // thread is waiting for it to disappear from nti.
263 ssize_t written = RangeSinkWrite(data, size, nti->rss);
264 data += written;
265 size -= written;
266
267 if (nti->rss->p_block == nti->rss->tgt->count) {
268 // we have written all the bytes desired by this rss.
269
270 pthread_mutex_lock(&nti->mu);
271 nti->rss = NULL;
272 pthread_cond_broadcast(&nti->cv);
273 pthread_mutex_unlock(&nti->mu);
274 }
275 }
276
277 return true;
278}
279
280static void* unzip_new_data(void* cookie) {
281 NewThreadInfo* nti = (NewThreadInfo*) cookie;
282 mzProcessZipEntryContents(nti->za, nti->entry, receive_new_data, nti);
283 return NULL;
284}
285
Sami Tolvanen90221202014-12-09 16:39:47 +0000286static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) {
287 int i;
288 size_t p = 0;
289 size_t size;
290
291 if (!src || !buffer) {
292 return -1;
293 }
294
295 for (i = 0; i < src->count; ++i) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700296 if (!check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000297 return -1;
298 }
299
300 size = (src->pos[i * 2 + 1] - src->pos[i * 2]) * BLOCKSIZE;
301
302 if (read_all(fd, buffer + p, size) == -1) {
303 return -1;
304 }
305
306 p += size;
307 }
308
309 return 0;
310}
311
312static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) {
313 int i;
314 size_t p = 0;
315 size_t size;
316
317 if (!tgt || !buffer) {
318 return -1;
319 }
320
321 for (i = 0; i < tgt->count; ++i) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700322 if (!check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000323 return -1;
324 }
325
326 size = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * BLOCKSIZE;
327
328 if (write_all(fd, buffer + p, size) == -1) {
329 return -1;
330 }
331
332 p += size;
333 }
334
335 return 0;
336}
337
Doug Zongker52ae67d2014-09-08 12:22:09 -0700338// Do a source/target load for move/bsdiff/imgdiff in version 1.
339// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
340// to parse the remainder of the string as:
341//
342// <src_range> <tgt_range>
343//
344// The source range is loaded into the provided buffer, reallocating
345// it to make it larger if necessary. The target ranges are returned
346// in *tgt, if tgt is non-NULL.
347
Sami Tolvanen90221202014-12-09 16:39:47 +0000348static int LoadSrcTgtVersion1(char** wordsave, RangeSet** tgt, int* src_blocks,
Doug Zongker52ae67d2014-09-08 12:22:09 -0700349 uint8_t** buffer, size_t* buffer_alloc, int fd) {
350 char* word;
Sami Tolvanen90221202014-12-09 16:39:47 +0000351 int rc;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700352
Sami Tolvanen90221202014-12-09 16:39:47 +0000353 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700354 RangeSet* src = parse_range(word);
355
356 if (tgt != NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000357 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700358 *tgt = parse_range(word);
359 }
360
361 allocate(src->size * BLOCKSIZE, buffer, buffer_alloc);
Sami Tolvanen90221202014-12-09 16:39:47 +0000362 rc = ReadBlocks(src, *buffer, fd);
363 *src_blocks = src->size;
364
365 free(src);
366 return rc;
367}
368
369static int VerifyBlocks(const char *expected, const uint8_t *buffer,
370 size_t blocks, int printerror) {
371 char* hexdigest = NULL;
372 int rc = -1;
373 uint8_t digest[SHA_DIGEST_SIZE];
374
375 if (!expected || !buffer) {
376 return rc;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700377 }
378
Sami Tolvanen90221202014-12-09 16:39:47 +0000379 SHA_hash(buffer, blocks * BLOCKSIZE, digest);
380 hexdigest = PrintSha1(digest);
381
382 if (hexdigest != NULL) {
383 rc = strcmp(expected, hexdigest);
384
385 if (rc != 0 && printerror) {
386 fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
387 expected, hexdigest);
388 }
389
390 free(hexdigest);
391 }
392
393 return rc;
394}
395
396static char* GetStashFileName(const char* base, const char* id, const char* postfix) {
397 char* fn;
398 int len;
399 int res;
400
401 if (base == NULL) {
402 return NULL;
403 }
404
405 if (id == NULL) {
406 id = "";
407 }
408
409 if (postfix == NULL) {
410 postfix = "";
411 }
412
413 len = strlen(STASH_DIRECTORY_BASE) + 1 + strlen(base) + 1 + strlen(id) + strlen(postfix) + 1;
414 fn = malloc(len);
415
416 if (fn == NULL) {
417 fprintf(stderr, "failed to malloc %d bytes for fn\n", len);
418 return NULL;
419 }
420
421 res = snprintf(fn, len, STASH_DIRECTORY_BASE "/%s/%s%s", base, id, postfix);
422
423 if (res < 0 || res >= len) {
424 fprintf(stderr, "failed to format file name (return value %d)\n", res);
425 free(fn);
426 return NULL;
427 }
428
429 return fn;
430}
431
432typedef void (*StashCallback)(const char*, void*);
433
434// Does a best effort enumeration of stash files. Ignores possible non-file
435// items in the stash directory and continues despite of errors. Calls the
436// 'callback' function for each file and passes 'data' to the function as a
437// parameter.
438
439static void EnumerateStash(const char* dirname, StashCallback callback, void* data) {
440 char* fn;
441 DIR* directory;
442 int len;
443 int res;
444 struct dirent* item;
445
446 if (dirname == NULL || callback == NULL) {
447 return;
448 }
449
450 directory = opendir(dirname);
451
452 if (directory == NULL) {
453 if (errno != ENOENT) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700454 fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000455 }
456 return;
457 }
458
459 while ((item = readdir(directory)) != NULL) {
460 if (item->d_type != DT_REG) {
461 continue;
462 }
463
464 len = strlen(dirname) + 1 + strlen(item->d_name) + 1;
465 fn = malloc(len);
466
467 if (fn == NULL) {
468 fprintf(stderr, "failed to malloc %d bytes for fn\n", len);
469 continue;
470 }
471
472 res = snprintf(fn, len, "%s/%s", dirname, item->d_name);
473
474 if (res < 0 || res >= len) {
475 fprintf(stderr, "failed to format file name (return value %d)\n", res);
476 free(fn);
477 continue;
478 }
479
480 callback(fn, data);
481 free(fn);
482 }
483
484 if (closedir(directory) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700485 fprintf(stderr, "closedir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000486 }
487}
488
489static void UpdateFileSize(const char* fn, void* data) {
490 int* size = (int*) data;
491 struct stat st;
492
493 if (!fn || !data) {
494 return;
495 }
496
497 if (stat(fn, &st) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700498 fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000499 return;
500 }
501
502 *size += st.st_size;
503}
504
505// Deletes the stash directory and all files in it. Assumes that it only
506// contains files. There is nothing we can do about unlikely, but possible
507// errors, so they are merely logged.
508
509static void DeleteFile(const char* fn, void* data) {
510 if (fn) {
511 fprintf(stderr, "deleting %s\n", fn);
512
513 if (unlink(fn) == -1 && errno != ENOENT) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700514 fprintf(stderr, "unlink \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000515 }
516 }
517}
518
519static void DeletePartial(const char* fn, void* data) {
520 if (fn && strstr(fn, ".partial") != NULL) {
521 DeleteFile(fn, data);
522 }
523}
524
525static void DeleteStash(const char* base) {
526 char* dirname;
527
528 if (base == NULL) {
529 return;
530 }
531
532 dirname = GetStashFileName(base, NULL, NULL);
533
534 if (dirname == NULL) {
535 return;
536 }
537
538 fprintf(stderr, "deleting stash %s\n", base);
539 EnumerateStash(dirname, DeleteFile, NULL);
540
541 if (rmdir(dirname) == -1) {
542 if (errno != ENOENT && errno != ENOTDIR) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700543 fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000544 }
545 }
546
547 free(dirname);
548}
549
550static int LoadStash(const char* base, const char* id, int verify, int* blocks, uint8_t** buffer,
551 size_t* buffer_alloc, int printnoent) {
552 char *fn = NULL;
553 int blockcount = 0;
554 int fd = -1;
555 int rc = -1;
556 int res;
557 struct stat st;
558
559 if (!base || !id || !buffer || !buffer_alloc) {
560 goto lsout;
561 }
562
563 if (!blocks) {
564 blocks = &blockcount;
565 }
566
567 fn = GetStashFileName(base, id, NULL);
568
569 if (fn == NULL) {
570 goto lsout;
571 }
572
573 res = stat(fn, &st);
574
575 if (res == -1) {
576 if (errno != ENOENT || printnoent) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700577 fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000578 }
579 goto lsout;
580 }
581
582 fprintf(stderr, " loading %s\n", fn);
583
584 if ((st.st_size % BLOCKSIZE) != 0) {
585 fprintf(stderr, "%s size %zd not multiple of block size %d", fn, st.st_size, BLOCKSIZE);
586 goto lsout;
587 }
588
589 fd = TEMP_FAILURE_RETRY(open(fn, O_RDONLY));
590
591 if (fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700592 fprintf(stderr, "open \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000593 goto lsout;
594 }
595
596 allocate(st.st_size, buffer, buffer_alloc);
597
598 if (read_all(fd, *buffer, st.st_size) == -1) {
599 goto lsout;
600 }
601
602 *blocks = st.st_size / BLOCKSIZE;
603
604 if (verify && VerifyBlocks(id, *buffer, *blocks, 1) != 0) {
605 fprintf(stderr, "unexpected contents in %s\n", fn);
606 DeleteFile(fn, NULL);
607 goto lsout;
608 }
609
610 rc = 0;
611
612lsout:
613 if (fd != -1) {
Elliott Hughesb5dabd22015-05-28 23:06:17 -0700614 close(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000615 }
616
617 if (fn) {
618 free(fn);
619 }
620
621 return rc;
622}
623
624static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buffer,
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100625 int checkspace, int *exists) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000626 char *fn = NULL;
627 char *cn = NULL;
628 int fd = -1;
629 int rc = -1;
630 int res;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100631 struct stat st;
Sami Tolvanen90221202014-12-09 16:39:47 +0000632
633 if (base == NULL || buffer == NULL) {
634 goto wsout;
635 }
636
637 if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) {
638 fprintf(stderr, "not enough space to write stash\n");
639 goto wsout;
640 }
641
642 fn = GetStashFileName(base, id, ".partial");
643 cn = GetStashFileName(base, id, NULL);
644
645 if (fn == NULL || cn == NULL) {
646 goto wsout;
647 }
648
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100649 if (exists) {
650 res = stat(cn, &st);
651
652 if (res == 0) {
653 // The file already exists and since the name is the hash of the contents,
654 // it's safe to assume the contents are identical (accidental hash collisions
655 // are unlikely)
656 fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn);
657 *exists = 1;
658 rc = 0;
659 goto wsout;
660 }
661
662 *exists = 0;
663 }
664
Sami Tolvanen90221202014-12-09 16:39:47 +0000665 fprintf(stderr, " writing %d blocks to %s\n", blocks, cn);
666
667 fd = TEMP_FAILURE_RETRY(open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, STASH_FILE_MODE));
668
669 if (fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700670 fprintf(stderr, "failed to create \"%s\": %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000671 goto wsout;
672 }
673
674 if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) {
675 goto wsout;
676 }
677
678 if (fsync(fd) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700679 fprintf(stderr, "fsync \"%s\" failed: %s\n", fn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000680 goto wsout;
681 }
682
683 if (rename(fn, cn) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700684 fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn, cn, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000685 goto wsout;
686 }
687
688 rc = 0;
689
690wsout:
691 if (fd != -1) {
Elliott Hughes1857a7f2015-05-15 16:19:20 -0700692 close(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000693 }
694
695 if (fn) {
696 free(fn);
697 }
698
699 if (cn) {
700 free(cn);
701 }
702
703 return rc;
704}
705
706// Creates a directory for storing stash files and checks if the /cache partition
707// hash enough space for the expected amount of blocks we need to store. Returns
708// >0 if we created the directory, zero if it existed already, and <0 of failure.
709
710static int CreateStash(State* state, int maxblocks, const char* blockdev, char** base) {
711 char* dirname = NULL;
712 const uint8_t* digest;
713 int rc = -1;
714 int res;
715 int size = 0;
716 SHA_CTX ctx;
717 struct stat st;
718
719 if (blockdev == NULL || base == NULL) {
720 goto csout;
721 }
722
723 // Stash directory should be different for each partition to avoid conflicts
724 // when updating multiple partitions at the same time, so we use the hash of
725 // the block device name as the base directory
726 SHA_init(&ctx);
727 SHA_update(&ctx, blockdev, strlen(blockdev));
728 digest = SHA_final(&ctx);
729 *base = PrintSha1(digest);
730
731 if (*base == NULL) {
732 goto csout;
733 }
734
735 dirname = GetStashFileName(*base, NULL, NULL);
736
737 if (dirname == NULL) {
738 goto csout;
739 }
740
741 res = stat(dirname, &st);
742
743 if (res == -1 && errno != ENOENT) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700744 ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000745 goto csout;
746 } else if (res != 0) {
747 fprintf(stderr, "creating stash %s\n", dirname);
748 res = mkdir(dirname, STASH_DIRECTORY_MODE);
749
750 if (res != 0) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -0700751 ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000752 goto csout;
753 }
754
755 if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) {
756 ErrorAbort(state, "not enough space for stash\n");
757 goto csout;
758 }
759
760 rc = 1; // Created directory
761 goto csout;
762 }
763
764 fprintf(stderr, "using existing stash %s\n", dirname);
765
766 // If the directory already exists, calculate the space already allocated to
767 // stash files and check if there's enough for all required blocks. Delete any
768 // partially completed stash files first.
769
770 EnumerateStash(dirname, DeletePartial, NULL);
771 EnumerateStash(dirname, UpdateFileSize, &size);
772
773 size = (maxblocks * BLOCKSIZE) - size;
774
775 if (size > 0 && CacheSizeCheck(size) != 0) {
776 ErrorAbort(state, "not enough space for stash (%d more needed)\n", size);
777 goto csout;
778 }
779
780 rc = 0; // Using existing directory
781
782csout:
783 if (dirname) {
784 free(dirname);
785 }
786
787 return rc;
788}
789
790static int SaveStash(const char* base, char** wordsave, uint8_t** buffer, size_t* buffer_alloc,
791 int fd, int usehash, int* isunresumable) {
792 char *id = NULL;
793 int res = -1;
794 int blocks = 0;
795
796 if (!wordsave || !buffer || !buffer_alloc || !isunresumable) {
797 return -1;
798 }
799
800 id = strtok_r(NULL, " ", wordsave);
801
802 if (id == NULL) {
803 fprintf(stderr, "missing id field in stash command\n");
804 return -1;
805 }
806
807 if (usehash && LoadStash(base, id, 1, &blocks, buffer, buffer_alloc, 0) == 0) {
808 // Stash file already exists and has expected contents. Do not
809 // read from source again, as the source may have been already
810 // overwritten during a previous attempt.
811 return 0;
812 }
813
814 if (LoadSrcTgtVersion1(wordsave, NULL, &blocks, buffer, buffer_alloc, fd) == -1) {
815 return -1;
816 }
817
818 if (usehash && VerifyBlocks(id, *buffer, blocks, 1) != 0) {
819 // Source blocks have unexpected contents. If we actually need this
820 // data later, this is an unrecoverable error. However, the command
821 // that uses the data may have already completed previously, so the
822 // possible failure will occur during source block verification.
823 fprintf(stderr, "failed to load source blocks for stash %s\n", id);
824 return 0;
825 }
826
827 fprintf(stderr, "stashing %d blocks to %s\n", blocks, id);
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100828 return WriteStash(base, id, blocks, *buffer, 0, NULL);
Sami Tolvanen90221202014-12-09 16:39:47 +0000829}
830
831static int FreeStash(const char* base, const char* id) {
832 char *fn = NULL;
833
834 if (base == NULL || id == NULL) {
835 return -1;
836 }
837
838 fn = GetStashFileName(base, id, NULL);
839
840 if (fn == NULL) {
841 return -1;
842 }
843
844 DeleteFile(fn, NULL);
845 free(fn);
846
847 return 0;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700848}
849
850static void MoveRange(uint8_t* dest, RangeSet* locs, const uint8_t* source) {
851 // source contains packed data, which we want to move to the
852 // locations given in *locs in the dest buffer. source and dest
853 // may be the same buffer.
854
855 int start = locs->size;
856 int i;
857 for (i = locs->count-1; i >= 0; --i) {
858 int blocks = locs->pos[i*2+1] - locs->pos[i*2];
859 start -= blocks;
860 memmove(dest + (locs->pos[i*2] * BLOCKSIZE), source + (start * BLOCKSIZE),
861 blocks * BLOCKSIZE);
862 }
863}
864
865// Do a source/target load for move/bsdiff/imgdiff in version 2.
866// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
867// to parse the remainder of the string as one of:
868//
869// <tgt_range> <src_block_count> <src_range>
870// (loads data from source image only)
871//
872// <tgt_range> <src_block_count> - <[stash_id:stash_range] ...>
873// (loads data from stashes only)
874//
875// <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...>
876// (loads data from both source image and stashes)
877//
878// On return, buffer is filled with the loaded source data (rearranged
879// and combined with stashed data as necessary). buffer may be
880// reallocated if needed to accommodate the source data. *tgt is the
Sami Tolvanen90221202014-12-09 16:39:47 +0000881// target RangeSet. Any stashes required are loaded using LoadStash.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700882
Sami Tolvanen90221202014-12-09 16:39:47 +0000883static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks,
Doug Zongker52ae67d2014-09-08 12:22:09 -0700884 uint8_t** buffer, size_t* buffer_alloc, int fd,
Sami Tolvanen90221202014-12-09 16:39:47 +0000885 const char* stashbase, int* overlap) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700886 char* word;
Sami Tolvanen90221202014-12-09 16:39:47 +0000887 char* colonsave;
888 char* colon;
889 int id;
890 int res;
891 RangeSet* locs;
892 size_t stashalloc = 0;
893 uint8_t* stash = NULL;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700894
895 if (tgt != NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000896 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700897 *tgt = parse_range(word);
898 }
899
Sami Tolvanen90221202014-12-09 16:39:47 +0000900 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700901 *src_blocks = strtol(word, NULL, 0);
902
903 allocate(*src_blocks * BLOCKSIZE, buffer, buffer_alloc);
904
Sami Tolvanen90221202014-12-09 16:39:47 +0000905 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700906 if (word[0] == '-' && word[1] == '\0') {
907 // no source ranges, only stashes
908 } else {
909 RangeSet* src = parse_range(word);
Sami Tolvanen90221202014-12-09 16:39:47 +0000910 res = ReadBlocks(src, *buffer, fd);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700911
Sami Tolvanen90221202014-12-09 16:39:47 +0000912 if (overlap && tgt) {
913 *overlap = range_overlaps(src, *tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700914 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000915
Doug Zongker52ae67d2014-09-08 12:22:09 -0700916 free(src);
917
Sami Tolvanen90221202014-12-09 16:39:47 +0000918 if (res == -1) {
919 return -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700920 }
921
Sami Tolvanen90221202014-12-09 16:39:47 +0000922 word = strtok_r(NULL, " ", wordsave);
923 if (word == NULL) {
924 // no stashes, only source range
925 return 0;
926 }
927
928 locs = parse_range(word);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700929 MoveRange(*buffer, locs, *buffer);
Sami Tolvanen90221202014-12-09 16:39:47 +0000930 free(locs);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700931 }
932
Sami Tolvanen90221202014-12-09 16:39:47 +0000933 while ((word = strtok_r(NULL, " ", wordsave)) != NULL) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700934 // Each word is a an index into the stash table, a colon, and
935 // then a rangeset describing where in the source block that
936 // stashed data should go.
Sami Tolvanen90221202014-12-09 16:39:47 +0000937 colonsave = NULL;
938 colon = strtok_r(word, ":", &colonsave);
939
940 res = LoadStash(stashbase, colon, 0, NULL, &stash, &stashalloc, 1);
941
942 if (res == -1) {
943 // These source blocks will fail verification if used later, but we
944 // will let the caller decide if this is a fatal failure
945 fprintf(stderr, "failed to load stash %s\n", colon);
946 continue;
947 }
948
Doug Zongker52ae67d2014-09-08 12:22:09 -0700949 colon = strtok_r(NULL, ":", &colonsave);
Sami Tolvanen90221202014-12-09 16:39:47 +0000950 locs = parse_range(colon);
951
952 MoveRange(*buffer, locs, stash);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700953 free(locs);
954 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000955
956 if (stash) {
957 free(stash);
958 }
959
960 return 0;
961}
962
963// Parameters for transfer list command functions
964typedef struct {
965 char* cmdname;
966 char* cpos;
967 char* freestash;
968 char* stashbase;
969 int canwrite;
970 int createdstash;
971 int fd;
972 int foundwrites;
973 int isunresumable;
974 int version;
975 int written;
976 NewThreadInfo nti;
977 pthread_t thread;
978 size_t bufsize;
979 uint8_t* buffer;
980 uint8_t* patch_start;
981} CommandParameters;
982
983// Do a source/target load for move/bsdiff/imgdiff in version 3.
984//
985// Parameters are the same as for LoadSrcTgtVersion2, except for 'onehash', which
986// tells the function whether to expect separate source and targe block hashes, or
987// if they are both the same and only one hash should be expected, and
988// 'isunresumable', which receives a non-zero value if block verification fails in
989// a way that the update cannot be resumed anymore.
990//
991// If the function is unable to load the necessary blocks or their contents don't
992// match the hashes, the return value is -1 and the command should be aborted.
993//
994// If the return value is 1, the command has already been completed according to
995// the contents of the target blocks, and should not be performed again.
996//
997// If the return value is 0, source blocks have expected content and the command
998// can be performed.
999
1000static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* src_blocks,
1001 int onehash, int* overlap) {
1002 char* srchash = NULL;
1003 char* tgthash = NULL;
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001004 int stash_exists = 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001005 int overlap_blocks = 0;
1006 int rc = -1;
1007 uint8_t* tgtbuffer = NULL;
1008
1009 if (!params|| !tgt || !src_blocks || !overlap) {
1010 goto v3out;
1011 }
1012
1013 srchash = strtok_r(NULL, " ", &params->cpos);
1014
1015 if (srchash == NULL) {
1016 fprintf(stderr, "missing source hash\n");
1017 goto v3out;
1018 }
1019
1020 if (onehash) {
1021 tgthash = srchash;
1022 } else {
1023 tgthash = strtok_r(NULL, " ", &params->cpos);
1024
1025 if (tgthash == NULL) {
1026 fprintf(stderr, "missing target hash\n");
1027 goto v3out;
1028 }
1029 }
1030
1031 if (LoadSrcTgtVersion2(&params->cpos, tgt, src_blocks, &params->buffer, &params->bufsize,
1032 params->fd, params->stashbase, overlap) == -1) {
1033 goto v3out;
1034 }
1035
1036 tgtbuffer = (uint8_t*) malloc((*tgt)->size * BLOCKSIZE);
1037
1038 if (tgtbuffer == NULL) {
1039 fprintf(stderr, "failed to allocate %d bytes\n", (*tgt)->size * BLOCKSIZE);
1040 goto v3out;
1041 }
1042
1043 if (ReadBlocks(*tgt, tgtbuffer, params->fd) == -1) {
1044 goto v3out;
1045 }
1046
1047 if (VerifyBlocks(tgthash, tgtbuffer, (*tgt)->size, 0) == 0) {
1048 // Target blocks already have expected content, command should be skipped
1049 rc = 1;
1050 goto v3out;
1051 }
1052
1053 if (VerifyBlocks(srchash, params->buffer, *src_blocks, 1) == 0) {
1054 // If source and target blocks overlap, stash the source blocks so we can
1055 // resume from possible write errors
1056 if (*overlap) {
1057 fprintf(stderr, "stashing %d overlapping blocks to %s\n", *src_blocks,
1058 srchash);
1059
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001060 if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, 1,
1061 &stash_exists) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001062 fprintf(stderr, "failed to stash overlapping source blocks\n");
1063 goto v3out;
1064 }
1065
1066 // Can be deleted when the write has completed
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001067 if (!stash_exists) {
1068 params->freestash = srchash;
1069 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001070 }
1071
1072 // Source blocks have expected content, command can proceed
1073 rc = 0;
1074 goto v3out;
1075 }
1076
1077 if (*overlap && LoadStash(params->stashbase, srchash, 1, NULL, &params->buffer,
1078 &params->bufsize, 1) == 0) {
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001079 // Overlapping source blocks were previously stashed, command can proceed.
1080 // We are recovering from an interrupted command, so we don't know if the
1081 // stash can safely be deleted after this command.
Sami Tolvanen90221202014-12-09 16:39:47 +00001082 rc = 0;
1083 goto v3out;
1084 }
1085
1086 // Valid source data not available, update cannot be resumed
1087 fprintf(stderr, "partition has unexpected contents\n");
1088 params->isunresumable = 1;
1089
1090v3out:
1091 if (tgtbuffer) {
1092 free(tgtbuffer);
1093 }
1094
1095 return rc;
1096}
1097
1098static int PerformCommandMove(CommandParameters* params) {
1099 int blocks = 0;
1100 int overlap = 0;
1101 int rc = -1;
1102 int status = 0;
1103 RangeSet* tgt = NULL;
1104
1105 if (!params) {
1106 goto pcmout;
1107 }
1108
1109 if (params->version == 1) {
1110 status = LoadSrcTgtVersion1(&params->cpos, &tgt, &blocks, &params->buffer,
1111 &params->bufsize, params->fd);
1112 } else if (params->version == 2) {
1113 status = LoadSrcTgtVersion2(&params->cpos, &tgt, &blocks, &params->buffer,
1114 &params->bufsize, params->fd, params->stashbase, NULL);
1115 } else if (params->version >= 3) {
1116 status = LoadSrcTgtVersion3(params, &tgt, &blocks, 1, &overlap);
1117 }
1118
1119 if (status == -1) {
1120 fprintf(stderr, "failed to read blocks for move\n");
1121 goto pcmout;
1122 }
1123
1124 if (status == 0) {
1125 params->foundwrites = 1;
1126 } else if (params->foundwrites) {
1127 fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname);
1128 }
1129
1130 if (params->canwrite) {
1131 if (status == 0) {
1132 fprintf(stderr, " moving %d blocks\n", blocks);
1133
1134 if (WriteBlocks(tgt, params->buffer, params->fd) == -1) {
1135 goto pcmout;
1136 }
1137 } else {
1138 fprintf(stderr, "skipping %d already moved blocks\n", blocks);
1139 }
1140
1141 }
1142
1143 if (params->freestash) {
1144 FreeStash(params->stashbase, params->freestash);
1145 params->freestash = NULL;
1146 }
1147
1148 params->written += tgt->size;
1149 rc = 0;
1150
1151pcmout:
1152 if (tgt) {
1153 free(tgt);
1154 }
1155
1156 return rc;
1157}
1158
1159static int PerformCommandStash(CommandParameters* params) {
1160 if (!params) {
1161 return -1;
1162 }
1163
1164 return SaveStash(params->stashbase, &params->cpos, &params->buffer, &params->bufsize,
1165 params->fd, (params->version >= 3), &params->isunresumable);
1166}
1167
1168static int PerformCommandFree(CommandParameters* params) {
1169 if (!params) {
1170 return -1;
1171 }
1172
1173 if (params->createdstash || params->canwrite) {
1174 return FreeStash(params->stashbase, params->cpos);
1175 }
1176
1177 return 0;
1178}
1179
1180static int PerformCommandZero(CommandParameters* params) {
1181 char* range = NULL;
1182 int i;
1183 int j;
1184 int rc = -1;
1185 RangeSet* tgt = NULL;
1186
1187 if (!params) {
1188 goto pczout;
1189 }
1190
1191 range = strtok_r(NULL, " ", &params->cpos);
1192
1193 if (range == NULL) {
1194 fprintf(stderr, "missing target blocks for zero\n");
1195 goto pczout;
1196 }
1197
1198 tgt = parse_range(range);
1199
1200 fprintf(stderr, " zeroing %d blocks\n", tgt->size);
1201
1202 allocate(BLOCKSIZE, &params->buffer, &params->bufsize);
1203 memset(params->buffer, 0, BLOCKSIZE);
1204
1205 if (params->canwrite) {
1206 for (i = 0; i < tgt->count; ++i) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -07001207 if (!check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001208 goto pczout;
1209 }
1210
1211 for (j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) {
1212 if (write_all(params->fd, params->buffer, BLOCKSIZE) == -1) {
1213 goto pczout;
1214 }
1215 }
1216 }
1217 }
1218
1219 if (params->cmdname[0] == 'z') {
Sami Tolvanen604c5832015-06-09 21:42:30 +01001220 // Update only for the zero command, as the erase command will call this
Sami Tolvanen90221202014-12-09 16:39:47 +00001221 params->written += tgt->size;
1222 }
1223
1224 rc = 0;
1225
1226pczout:
1227 if (tgt) {
1228 free(tgt);
1229 }
1230
1231 return rc;
1232}
1233
1234static int PerformCommandNew(CommandParameters* params) {
1235 char* range = NULL;
1236 int rc = -1;
1237 RangeSet* tgt = NULL;
1238 RangeSinkState rss;
1239
1240 if (!params) {
1241 goto pcnout;
1242 }
1243
1244 range = strtok_r(NULL, " ", &params->cpos);
1245
1246 if (range == NULL) {
1247 goto pcnout;
1248 }
1249
1250 tgt = parse_range(range);
1251
1252 if (params->canwrite) {
1253 fprintf(stderr, " writing %d blocks of new data\n", tgt->size);
1254
1255 rss.fd = params->fd;
1256 rss.tgt = tgt;
1257 rss.p_block = 0;
1258 rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
1259
Elliott Hughes2f5feed2015-04-28 17:24:24 -07001260 if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001261 goto pcnout;
1262 }
1263
1264 pthread_mutex_lock(&params->nti.mu);
1265 params->nti.rss = &rss;
1266 pthread_cond_broadcast(&params->nti.cv);
1267
1268 while (params->nti.rss) {
1269 pthread_cond_wait(&params->nti.cv, &params->nti.mu);
1270 }
1271
1272 pthread_mutex_unlock(&params->nti.mu);
1273 }
1274
1275 params->written += tgt->size;
1276 rc = 0;
1277
1278pcnout:
1279 if (tgt) {
1280 free(tgt);
1281 }
1282
1283 return rc;
1284}
1285
1286static int PerformCommandDiff(CommandParameters* params) {
1287 char* logparams = NULL;
1288 char* value = NULL;
1289 int blocks = 0;
1290 int overlap = 0;
1291 int rc = -1;
1292 int status = 0;
1293 RangeSet* tgt = NULL;
1294 RangeSinkState rss;
1295 size_t len = 0;
1296 size_t offset = 0;
1297 Value patch_value;
1298
1299 if (!params) {
1300 goto pcdout;
1301 }
1302
1303 logparams = strdup(params->cpos);
1304 value = strtok_r(NULL, " ", &params->cpos);
1305
1306 if (value == NULL) {
1307 fprintf(stderr, "missing patch offset for %s\n", params->cmdname);
1308 goto pcdout;
1309 }
1310
1311 offset = strtoul(value, NULL, 0);
1312
1313 value = strtok_r(NULL, " ", &params->cpos);
1314
1315 if (value == NULL) {
1316 fprintf(stderr, "missing patch length for %s\n", params->cmdname);
1317 goto pcdout;
1318 }
1319
1320 len = strtoul(value, NULL, 0);
1321
1322 if (params->version == 1) {
1323 status = LoadSrcTgtVersion1(&params->cpos, &tgt, &blocks, &params->buffer,
1324 &params->bufsize, params->fd);
1325 } else if (params->version == 2) {
1326 status = LoadSrcTgtVersion2(&params->cpos, &tgt, &blocks, &params->buffer,
1327 &params->bufsize, params->fd, params->stashbase, NULL);
1328 } else if (params->version >= 3) {
1329 status = LoadSrcTgtVersion3(params, &tgt, &blocks, 0, &overlap);
1330 }
1331
1332 if (status == -1) {
1333 fprintf(stderr, "failed to read blocks for diff\n");
1334 goto pcdout;
1335 }
1336
1337 if (status == 0) {
1338 params->foundwrites = 1;
1339 } else if (params->foundwrites) {
1340 fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname);
1341 }
1342
1343 if (params->canwrite) {
1344 if (status == 0) {
1345 fprintf(stderr, "patching %d blocks to %d\n", blocks, tgt->size);
1346
1347 patch_value.type = VAL_BLOB;
1348 patch_value.size = len;
1349 patch_value.data = (char*) (params->patch_start + offset);
1350
1351 rss.fd = params->fd;
1352 rss.tgt = tgt;
1353 rss.p_block = 0;
1354 rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
1355
Elliott Hughes2f5feed2015-04-28 17:24:24 -07001356 if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001357 goto pcdout;
1358 }
1359
1360 if (params->cmdname[0] == 'i') { // imgdiff
1361 ApplyImagePatch(params->buffer, blocks * BLOCKSIZE, &patch_value,
1362 &RangeSinkWrite, &rss, NULL, NULL);
1363 } else {
1364 ApplyBSDiffPatch(params->buffer, blocks * BLOCKSIZE, &patch_value,
1365 0, &RangeSinkWrite, &rss, NULL);
1366 }
1367
1368 // We expect the output of the patcher to fill the tgt ranges exactly.
1369 if (rss.p_block != tgt->count || rss.p_remain != 0) {
1370 fprintf(stderr, "range sink underrun?\n");
1371 }
1372 } else {
1373 fprintf(stderr, "skipping %d blocks already patched to %d [%s]\n",
1374 blocks, tgt->size, logparams);
1375 }
1376 }
1377
1378 if (params->freestash) {
1379 FreeStash(params->stashbase, params->freestash);
1380 params->freestash = NULL;
1381 }
1382
1383 params->written += tgt->size;
1384 rc = 0;
1385
1386pcdout:
1387 if (logparams) {
1388 free(logparams);
1389 }
1390
1391 if (tgt) {
1392 free(tgt);
1393 }
1394
1395 return rc;
1396}
1397
1398static int PerformCommandErase(CommandParameters* params) {
1399 char* range = NULL;
1400 int i;
1401 int rc = -1;
1402 RangeSet* tgt = NULL;
1403 struct stat st;
1404 uint64_t blocks[2];
1405
Sami Tolvanen604c5832015-06-09 21:42:30 +01001406 // Always zero the blocks first to work around possibly flaky BLKDISCARD
1407 // Bug: 20881595
1408 if (PerformCommandZero(params) != 0) {
1409 goto pceout;
Sami Tolvanen90221202014-12-09 16:39:47 +00001410 }
1411
1412 if (!params) {
1413 goto pceout;
1414 }
1415
1416 if (fstat(params->fd, &st) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001417 fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001418 goto pceout;
1419 }
1420
1421 if (!S_ISBLK(st.st_mode)) {
1422 fprintf(stderr, "not a block device; skipping erase\n");
Sami Tolvanen90221202014-12-09 16:39:47 +00001423 goto pceout;
1424 }
1425
1426 range = strtok_r(NULL, " ", &params->cpos);
1427
1428 if (range == NULL) {
1429 fprintf(stderr, "missing target blocks for zero\n");
1430 goto pceout;
1431 }
1432
1433 tgt = parse_range(range);
1434
1435 if (params->canwrite) {
1436 fprintf(stderr, " erasing %d blocks\n", tgt->size);
1437
1438 for (i = 0; i < tgt->count; ++i) {
1439 // offset in bytes
1440 blocks[0] = tgt->pos[i * 2] * (uint64_t) BLOCKSIZE;
1441 // length in bytes
1442 blocks[1] = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * (uint64_t) BLOCKSIZE;
1443
1444 if (ioctl(params->fd, BLKDISCARD, &blocks) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001445 fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno));
Sami Tolvanen92eea1b2015-04-27 11:24:29 +01001446 goto pceout;
Sami Tolvanen90221202014-12-09 16:39:47 +00001447 }
1448 }
1449 }
1450
1451 rc = 0;
1452
1453pceout:
1454 if (tgt) {
1455 free(tgt);
1456 }
1457
1458 return rc;
1459}
1460
1461// Definitions for transfer list command functions
1462typedef int (*CommandFunction)(CommandParameters*);
1463
1464typedef struct {
1465 const char* name;
1466 CommandFunction f;
1467} Command;
1468
1469// CompareCommands and CompareCommandNames are for the hash table
1470
1471static int CompareCommands(const void* c1, const void* c2) {
1472 return strcmp(((const Command*) c1)->name, ((const Command*) c2)->name);
1473}
1474
1475static int CompareCommandNames(const void* c1, const void* c2) {
1476 return strcmp(((const Command*) c1)->name, (const char*) c2);
1477}
1478
1479// HashString is used to hash command names for the hash table
1480
1481static unsigned int HashString(const char *s) {
1482 unsigned int hash = 0;
1483 if (s) {
1484 while (*s) {
1485 hash = hash * 33 + *s++;
1486 }
1487 }
1488 return hash;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001489}
1490
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001491// args:
1492// - block device (or file) to modify in-place
1493// - transfer list (blob)
1494// - new data stream (filename within package.zip)
1495// - patch stream (filename within package.zip, must be uncompressed)
1496
Sami Tolvanen90221202014-12-09 16:39:47 +00001497static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, Expr* argv[],
1498 const Command* commands, int cmdcount, int dryrun) {
1499
1500 char* line = NULL;
1501 char* linesave = NULL;
1502 char* logcmd = NULL;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001503 char* transfer_list = NULL;
Sami Tolvanen90221202014-12-09 16:39:47 +00001504 CommandParameters params;
1505 const Command* cmd = NULL;
1506 const ZipEntry* new_entry = NULL;
1507 const ZipEntry* patch_entry = NULL;
1508 FILE* cmd_pipe = NULL;
1509 HashTable* cmdht = NULL;
1510 int i;
1511 int res;
1512 int rc = -1;
1513 int stash_max_blocks = 0;
1514 int total_blocks = 0;
1515 pthread_attr_t attr;
1516 unsigned int cmdhash;
1517 UpdaterInfo* ui = NULL;
1518 Value* blockdev_filename = NULL;
1519 Value* new_data_fn = NULL;
1520 Value* patch_data_fn = NULL;
1521 Value* transfer_list_value = NULL;
1522 ZipArchive* za = NULL;
1523
1524 memset(&params, 0, sizeof(params));
1525 params.canwrite = !dryrun;
1526
1527 fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001528
Doug Zongker1d5d6092014-08-21 10:47:24 -07001529 if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value,
Sami Tolvanen90221202014-12-09 16:39:47 +00001530 &new_data_fn, &patch_data_fn) < 0) {
1531 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001532 }
1533
1534 if (blockdev_filename->type != VAL_STRING) {
1535 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001536 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001537 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001538 if (transfer_list_value->type != VAL_BLOB) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001539 ErrorAbort(state, "transfer_list argument to %s must be blob", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001540 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001541 }
1542 if (new_data_fn->type != VAL_STRING) {
1543 ErrorAbort(state, "new_data_fn argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001544 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001545 }
1546 if (patch_data_fn->type != VAL_STRING) {
1547 ErrorAbort(state, "patch_data_fn argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001548 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001549 }
1550
Sami Tolvanen90221202014-12-09 16:39:47 +00001551 ui = (UpdaterInfo*) state->cookie;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001552
Sami Tolvanen90221202014-12-09 16:39:47 +00001553 if (ui == NULL) {
1554 goto pbiudone;
1555 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001556
Sami Tolvanen90221202014-12-09 16:39:47 +00001557 cmd_pipe = ui->cmd_pipe;
1558 za = ui->package_zip;
1559
1560 if (cmd_pipe == NULL || za == NULL) {
1561 goto pbiudone;
1562 }
1563
1564 patch_entry = mzFindZipEntry(za, patch_data_fn->data);
1565
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001566 if (patch_entry == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001567 fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data);
1568 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001569 }
1570
Sami Tolvanen90221202014-12-09 16:39:47 +00001571 params.patch_start = ui->package_zip_addr + mzGetZipEntryOffset(patch_entry);
1572 new_entry = mzFindZipEntry(za, new_data_fn->data);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001573
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001574 if (new_entry == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001575 fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data);
1576 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001577 }
1578
Sami Tolvanen90221202014-12-09 16:39:47 +00001579 params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001580
Sami Tolvanen90221202014-12-09 16:39:47 +00001581 if (params.fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001582 fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001583 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001584 }
1585
Sami Tolvanen90221202014-12-09 16:39:47 +00001586 if (params.canwrite) {
1587 params.nti.za = za;
1588 params.nti.entry = new_entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001589
Sami Tolvanen90221202014-12-09 16:39:47 +00001590 pthread_mutex_init(&params.nti.mu, NULL);
1591 pthread_cond_init(&params.nti.cv, NULL);
1592 pthread_attr_init(&attr);
1593 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1594
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001595 int error = pthread_create(&params.thread, &attr, unzip_new_data, &params.nti);
1596 if (error != 0) {
1597 fprintf(stderr, "pthread_create failed: %s\n", strerror(error));
Sami Tolvanen90221202014-12-09 16:39:47 +00001598 goto pbiudone;
1599 }
1600 }
1601
1602 // The data in transfer_list_value is not necessarily null-terminated, so we need
1603 // to copy it to a new buffer and add the null that strtok_r will need.
1604 transfer_list = malloc(transfer_list_value->size + 1);
1605
Doug Zongker1d5d6092014-08-21 10:47:24 -07001606 if (transfer_list == NULL) {
1607 fprintf(stderr, "failed to allocate %zd bytes for transfer list\n",
Sami Tolvanen90221202014-12-09 16:39:47 +00001608 transfer_list_value->size + 1);
1609 goto pbiudone;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001610 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001611
Doug Zongker1d5d6092014-08-21 10:47:24 -07001612 memcpy(transfer_list, transfer_list_value->data, transfer_list_value->size);
1613 transfer_list[transfer_list_value->size] = '\0';
1614
Sami Tolvanen90221202014-12-09 16:39:47 +00001615 // First line in transfer list is the version number
Doug Zongker1d5d6092014-08-21 10:47:24 -07001616 line = strtok_r(transfer_list, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001617 params.version = strtol(line, NULL, 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001618
Sami Tolvanen90221202014-12-09 16:39:47 +00001619 if (params.version < 1 || params.version > 3) {
1620 fprintf(stderr, "unexpected transfer list version [%s]\n", line);
1621 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001622 }
1623
Sami Tolvanen90221202014-12-09 16:39:47 +00001624 fprintf(stderr, "blockimg version is %d\n", params.version);
1625
1626 // Second line in transfer list is the total number of blocks we expect to write
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001627 line = strtok_r(NULL, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001628 total_blocks = strtol(line, NULL, 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001629
Sami Tolvanen90221202014-12-09 16:39:47 +00001630 if (total_blocks < 0) {
1631 ErrorAbort(state, "unexpected block count [%s]\n", line);
1632 goto pbiudone;
1633 } else if (total_blocks == 0) {
1634 rc = 0;
1635 goto pbiudone;
1636 }
1637
1638 if (params.version >= 2) {
1639 // Third line is how many stash entries are needed simultaneously
Doug Zongker52ae67d2014-09-08 12:22:09 -07001640 line = strtok_r(NULL, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001641 fprintf(stderr, "maximum stash entries %s\n", line);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001642
Sami Tolvanen90221202014-12-09 16:39:47 +00001643 // Fourth line is the maximum number of blocks that will be stashed simultaneously
1644 line = strtok_r(NULL, "\n", &linesave);
1645 stash_max_blocks = strtol(line, NULL, 0);
1646
1647 if (stash_max_blocks < 0) {
1648 ErrorAbort(state, "unexpected maximum stash blocks [%s]\n", line);
1649 goto pbiudone;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001650 }
1651
Jesse Zhao1df64d32015-02-17 17:09:23 -08001652 if (stash_max_blocks >= 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001653 res = CreateStash(state, stash_max_blocks, blockdev_filename->data,
1654 &params.stashbase);
1655
1656 if (res == -1) {
1657 goto pbiudone;
1658 }
1659
1660 params.createdstash = res;
1661 }
Doug Zongker52ae67d2014-09-08 12:22:09 -07001662 }
1663
Sami Tolvanen90221202014-12-09 16:39:47 +00001664 // Build a hash table of the available commands
1665 cmdht = mzHashTableCreate(cmdcount, NULL);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001666
Sami Tolvanen90221202014-12-09 16:39:47 +00001667 for (i = 0; i < cmdcount; ++i) {
1668 cmdhash = HashString(commands[i].name);
1669 mzHashTableLookup(cmdht, cmdhash, (void*) &commands[i], CompareCommands, true);
1670 }
1671
1672 // Subsequent lines are all individual transfer commands
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001673 for (line = strtok_r(NULL, "\n", &linesave); line;
1674 line = strtok_r(NULL, "\n", &linesave)) {
Doug Zongker52ae67d2014-09-08 12:22:09 -07001675
Sami Tolvanen90221202014-12-09 16:39:47 +00001676 logcmd = strdup(line);
1677 params.cmdname = strtok_r(line, " ", &params.cpos);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001678
Sami Tolvanen90221202014-12-09 16:39:47 +00001679 if (params.cmdname == NULL) {
1680 fprintf(stderr, "missing command [%s]\n", line);
1681 goto pbiudone;
1682 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001683
Sami Tolvanen90221202014-12-09 16:39:47 +00001684 cmdhash = HashString(params.cmdname);
1685 cmd = (const Command*) mzHashTableLookup(cmdht, cmdhash, params.cmdname,
1686 CompareCommandNames, false);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001687
Sami Tolvanen90221202014-12-09 16:39:47 +00001688 if (cmd == NULL) {
1689 fprintf(stderr, "unexpected command [%s]\n", params.cmdname);
1690 goto pbiudone;
1691 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001692
Sami Tolvanen90221202014-12-09 16:39:47 +00001693 if (cmd->f != NULL && cmd->f(&params) == -1) {
1694 fprintf(stderr, "failed to execute command [%s]\n",
1695 logcmd ? logcmd : params.cmdname);
1696 goto pbiudone;
1697 }
1698
1699 if (logcmd) {
1700 free(logcmd);
1701 logcmd = NULL;
1702 }
1703
1704 if (params.canwrite) {
1705 fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001706 fflush(cmd_pipe);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001707 }
1708 }
1709
Sami Tolvanen90221202014-12-09 16:39:47 +00001710 if (params.canwrite) {
1711 pthread_join(params.thread, NULL);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001712
Sami Tolvanen90221202014-12-09 16:39:47 +00001713 fprintf(stderr, "wrote %d blocks; expected %d\n", params.written, total_blocks);
1714 fprintf(stderr, "max alloc needed was %zu\n", params.bufsize);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001715
Sami Tolvanen90221202014-12-09 16:39:47 +00001716 // Delete stash only after successfully completing the update, as it
1717 // may contain blocks needed to complete the update later.
1718 DeleteStash(params.stashbase);
1719 } else {
1720 fprintf(stderr, "verified partition contents; update may be resumed\n");
1721 }
1722
1723 rc = 0;
1724
1725pbiudone:
1726 if (params.fd != -1) {
1727 if (fsync(params.fd) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001728 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001729 }
Elliott Hughes1857a7f2015-05-15 16:19:20 -07001730 close(params.fd);
Sami Tolvanen90221202014-12-09 16:39:47 +00001731 }
1732
1733 if (logcmd) {
1734 free(logcmd);
1735 }
1736
1737 if (cmdht) {
1738 mzHashTableFree(cmdht);
1739 }
1740
1741 if (params.buffer) {
1742 free(params.buffer);
1743 }
1744
1745 if (transfer_list) {
1746 free(transfer_list);
1747 }
1748
1749 if (blockdev_filename) {
1750 FreeValue(blockdev_filename);
1751 }
1752
1753 if (transfer_list_value) {
1754 FreeValue(transfer_list_value);
1755 }
1756
1757 if (new_data_fn) {
1758 FreeValue(new_data_fn);
1759 }
1760
1761 if (patch_data_fn) {
1762 FreeValue(patch_data_fn);
1763 }
1764
1765 // Only delete the stash if the update cannot be resumed, or it's
1766 // a verification run and we created the stash.
1767 if (params.isunresumable || (!params.canwrite && params.createdstash)) {
1768 DeleteStash(params.stashbase);
1769 }
1770
1771 if (params.stashbase) {
1772 free(params.stashbase);
1773 }
1774
1775 return StringValue(rc == 0 ? strdup("t") : strdup(""));
1776}
1777
1778// The transfer list is a text file containing commands to
1779// transfer data from one place to another on the target
1780// partition. We parse it and execute the commands in order:
1781//
1782// zero [rangeset]
1783// - fill the indicated blocks with zeros
1784//
1785// new [rangeset]
1786// - fill the blocks with data read from the new_data file
1787//
1788// erase [rangeset]
1789// - mark the given blocks as empty
1790//
1791// move <...>
1792// bsdiff <patchstart> <patchlen> <...>
1793// imgdiff <patchstart> <patchlen> <...>
1794// - read the source blocks, apply a patch (or not in the
1795// case of move), write result to target blocks. bsdiff or
1796// imgdiff specifies the type of patch; move means no patch
1797// at all.
1798//
1799// The format of <...> differs between versions 1 and 2;
1800// see the LoadSrcTgtVersion{1,2}() functions for a
1801// description of what's expected.
1802//
1803// stash <stash_id> <src_range>
1804// - (version 2+ only) load the given source range and stash
1805// the data in the given slot of the stash table.
1806//
1807// The creator of the transfer list will guarantee that no block
1808// is read (ie, used as the source for a patch or move) after it
1809// has been written.
1810//
1811// In version 2, the creator will guarantee that a given stash is
1812// loaded (with a stash command) before it's used in a
1813// move/bsdiff/imgdiff command.
1814//
1815// Within one command the source and target ranges may overlap so
1816// in general we need to read the entire source into memory before
1817// writing anything to the target blocks.
1818//
1819// All the patch data is concatenated into one patch_data file in
1820// the update package. It must be stored uncompressed because we
1821// memory-map it in directly from the archive. (Since patches are
1822// already compressed, we lose very little by not compressing
1823// their concatenation.)
1824//
1825// In version 3, commands that read data from the partition (i.e.
1826// move/bsdiff/imgdiff/stash) have one or more additional hashes
1827// before the range parameters, which are used to check if the
1828// command has already been completed and verify the integrity of
1829// the source data.
1830
1831Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]) {
1832 // Commands which are not tested are set to NULL to skip them completely
1833 const Command commands[] = {
1834 { "bsdiff", PerformCommandDiff },
1835 { "erase", NULL },
1836 { "free", PerformCommandFree },
1837 { "imgdiff", PerformCommandDiff },
1838 { "move", PerformCommandMove },
1839 { "new", NULL },
1840 { "stash", PerformCommandStash },
1841 { "zero", NULL }
1842 };
1843
1844 // Perform a dry run without writing to test if an update can proceed
1845 return PerformBlockImageUpdate(name, state, argc, argv, commands,
1846 sizeof(commands) / sizeof(commands[0]), 1);
1847}
1848
1849Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) {
1850 const Command commands[] = {
1851 { "bsdiff", PerformCommandDiff },
1852 { "erase", PerformCommandErase },
1853 { "free", PerformCommandFree },
1854 { "imgdiff", PerformCommandDiff },
1855 { "move", PerformCommandMove },
1856 { "new", PerformCommandNew },
1857 { "stash", PerformCommandStash },
1858 { "zero", PerformCommandZero }
1859 };
1860
1861 return PerformBlockImageUpdate(name, state, argc, argv, commands,
1862 sizeof(commands) / sizeof(commands[0]), 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001863}
1864
1865Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) {
1866 Value* blockdev_filename;
1867 Value* ranges;
1868 const uint8_t* digest = NULL;
1869 if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) {
1870 return NULL;
1871 }
1872
1873 if (blockdev_filename->type != VAL_STRING) {
1874 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
1875 goto done;
1876 }
1877 if (ranges->type != VAL_STRING) {
1878 ErrorAbort(state, "ranges argument to %s must be string", name);
1879 goto done;
1880 }
1881
1882 int fd = open(blockdev_filename->data, O_RDWR);
1883 if (fd < 0) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001884 ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001885 goto done;
1886 }
1887
1888 RangeSet* rs = parse_range(ranges->data);
1889 uint8_t buffer[BLOCKSIZE];
1890
1891 SHA_CTX ctx;
1892 SHA_init(&ctx);
1893
1894 int i, j;
1895 for (i = 0; i < rs->count; ++i) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -07001896 if (!check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001897 ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data,
1898 strerror(errno));
1899 goto done;
1900 }
1901
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001902 for (j = rs->pos[i*2]; j < rs->pos[i*2+1]; ++j) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001903 if (read_all(fd, buffer, BLOCKSIZE) == -1) {
1904 ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data,
1905 strerror(errno));
1906 goto done;
1907 }
1908
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001909 SHA_update(&ctx, buffer, BLOCKSIZE);
1910 }
1911 }
1912 digest = SHA_final(&ctx);
1913 close(fd);
1914
1915 done:
1916 FreeValue(blockdev_filename);
1917 FreeValue(ranges);
1918 if (digest == NULL) {
1919 return StringValue(strdup(""));
1920 } else {
1921 return StringValue(PrintSha1(digest));
1922 }
1923}
1924
1925void RegisterBlockImageFunctions() {
Sami Tolvanen90221202014-12-09 16:39:47 +00001926 RegisterFunction("block_image_verify", BlockImageVerifyFn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001927 RegisterFunction("block_image_update", BlockImageUpdateFn);
1928 RegisterFunction("range_sha1", RangeSha1Fn);
1929}