blob: 3a07da404599cb234c2f5d606284be57e16138f6 [file] [log] [blame]
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ctype.h>
18#include <errno.h>
Sami Tolvanen90221202014-12-09 16:39:47 +000019#include <dirent.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070020#include <fcntl.h>
21#include <inttypes.h>
Tao Baoba9a42a2015-06-23 23:23:33 -070022#include <linux/fs.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070023#include <pthread.h>
24#include <stdarg.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Sami Tolvanen90221202014-12-09 16:39:47 +000028#include <sys/stat.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070029#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/ioctl.h>
32#include <time.h>
33#include <unistd.h>
34
Tao Baoe6aa3322015-08-05 15:20:27 -070035#include <memory>
36#include <string>
37
38#include <base/strings.h>
39
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070040#include "applypatch/applypatch.h"
41#include "edify/expr.h"
42#include "mincrypt/sha.h"
Sami Tolvanen90221202014-12-09 16:39:47 +000043#include "minzip/Hash.h"
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070044#include "updater.h"
Tao Baoe6aa3322015-08-05 15:20:27 -070045#include "print_sha1.h"
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070046
47#define BLOCKSIZE 4096
48
Sami Tolvanene82fa182015-06-10 15:58:12 +000049// Set this to 0 to interpret 'erase' transfers to mean do a
50// BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret
51// erase to mean fill the region with zeroes.
52#define DEBUG_ERASE 0
53
Sami Tolvanen90221202014-12-09 16:39:47 +000054#define STASH_DIRECTORY_BASE "/cache/recovery"
55#define STASH_DIRECTORY_MODE 0700
56#define STASH_FILE_MODE 0600
57
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070058typedef struct {
59 int count;
60 int size;
61 int pos[0];
62} RangeSet;
63
Sami Tolvanenf2bac042015-05-12 12:48:46 +010064#define RANGESET_MAX_POINTS \
65 ((int)((INT_MAX / sizeof(int)) - sizeof(RangeSet)))
66
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070067static RangeSet* parse_range(char* text) {
68 char* save;
Sami Tolvanenf2bac042015-05-12 12:48:46 +010069 char* token;
Tao Baoba9a42a2015-06-23 23:23:33 -070070 int num;
Sami Tolvanenf2bac042015-05-12 12:48:46 +010071 long int val;
72 RangeSet* out = NULL;
73 size_t bufsize;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070074
Sami Tolvanenf2bac042015-05-12 12:48:46 +010075 if (!text) {
76 goto err;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070077 }
Sami Tolvanenf2bac042015-05-12 12:48:46 +010078
79 token = strtok_r(text, ",", &save);
80
81 if (!token) {
82 goto err;
83 }
84
85 val = strtol(token, NULL, 0);
86
87 if (val < 2 || val > RANGESET_MAX_POINTS) {
88 goto err;
89 } else if (val % 2) {
90 goto err; // must be even
91 }
92
93 num = (int) val;
94 bufsize = sizeof(RangeSet) + num * sizeof(int);
95
Tao Baoba9a42a2015-06-23 23:23:33 -070096 out = reinterpret_cast<RangeSet*>(malloc(bufsize));
Sami Tolvanenf2bac042015-05-12 12:48:46 +010097
98 if (!out) {
99 fprintf(stderr, "failed to allocate range of %zu bytes\n", bufsize);
100 goto err;
101 }
102
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700103 out->count = num / 2;
104 out->size = 0;
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100105
Tao Baoba9a42a2015-06-23 23:23:33 -0700106 for (int i = 0; i < num; ++i) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100107 token = strtok_r(NULL, ",", &save);
108
109 if (!token) {
110 goto err;
111 }
112
113 val = strtol(token, NULL, 0);
114
115 if (val < 0 || val > INT_MAX) {
116 goto err;
117 }
118
119 out->pos[i] = (int) val;
120
121 if (i % 2) {
122 if (out->pos[i - 1] >= out->pos[i]) {
123 goto err; // empty or negative range
124 }
125
126 if (out->size > INT_MAX - out->pos[i]) {
127 goto err; // overflow
128 }
129
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700130 out->size += out->pos[i];
131 } else {
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100132 if (out->size < 0) {
133 goto err;
134 }
135
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700136 out->size -= out->pos[i];
137 }
138 }
139
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100140 if (out->size <= 0) {
141 goto err;
142 }
143
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700144 return out;
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100145
146err:
147 fprintf(stderr, "failed to parse range '%s'\n", text ? text : "NULL");
148 exit(1);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700149}
150
Tao Baoe6aa3322015-08-05 15:20:27 -0700151static bool range_overlaps(const RangeSet& r1, const RangeSet& r2) {
152 for (int i = 0; i < r1.count; ++i) {
153 int r1_0 = r1.pos[i * 2];
154 int r1_1 = r1.pos[i * 2 + 1];
Sami Tolvanen90221202014-12-09 16:39:47 +0000155
Tao Baoe6aa3322015-08-05 15:20:27 -0700156 for (int j = 0; j < r2.count; ++j) {
157 int r2_0 = r2.pos[j * 2];
158 int r2_1 = r2.pos[j * 2 + 1];
Sami Tolvanen90221202014-12-09 16:39:47 +0000159
Tao Baoc0f56ad2015-06-25 14:00:31 -0700160 if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700161 return true;
Sami Tolvanen90221202014-12-09 16:39:47 +0000162 }
163 }
164 }
165
Tao Baoe6aa3322015-08-05 15:20:27 -0700166 return false;
Sami Tolvanen90221202014-12-09 16:39:47 +0000167}
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
Sami Tolvanen90221202014-12-09 16:39:47 +0000193 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700194}
195
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700196static bool check_lseek(int fd, off64_t offset, int whence) {
197 off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
198 if (rc == -1) {
199 fprintf(stderr, "lseek64 failed: %s\n", strerror(errno));
200 return false;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700201 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700202 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700203}
204
205static void allocate(size_t size, uint8_t** buffer, size_t* buffer_alloc) {
206 // if the buffer's big enough, reuse it.
207 if (size <= *buffer_alloc) return;
208
209 free(*buffer);
210
211 *buffer = (uint8_t*) malloc(size);
212 if (*buffer == NULL) {
Doug Zongker1d5d6092014-08-21 10:47:24 -0700213 fprintf(stderr, "failed to allocate %zu bytes\n", size);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700214 exit(1);
215 }
216 *buffer_alloc = size;
217}
218
219typedef struct {
220 int fd;
221 RangeSet* tgt;
222 int p_block;
223 size_t p_remain;
224} RangeSinkState;
225
226static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) {
227 RangeSinkState* rss = (RangeSinkState*) token;
228
229 if (rss->p_remain <= 0) {
230 fprintf(stderr, "range sink write overrun");
Sami Tolvanen90221202014-12-09 16:39:47 +0000231 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700232 }
233
234 ssize_t written = 0;
235 while (size > 0) {
236 size_t write_now = size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000237
238 if (rss->p_remain < write_now) {
239 write_now = rss->p_remain;
240 }
241
242 if (write_all(rss->fd, data, write_now) == -1) {
243 break;
244 }
245
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700246 data += write_now;
247 size -= write_now;
248
249 rss->p_remain -= write_now;
250 written += write_now;
251
252 if (rss->p_remain == 0) {
253 // move to the next block
254 ++rss->p_block;
255 if (rss->p_block < rss->tgt->count) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000256 rss->p_remain = (rss->tgt->pos[rss->p_block * 2 + 1] -
257 rss->tgt->pos[rss->p_block * 2]) * BLOCKSIZE;
258
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700259 if (!check_lseek(rss->fd, (off64_t)rss->tgt->pos[rss->p_block*2] * BLOCKSIZE,
260 SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000261 break;
262 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700263 } else {
264 // we can't write any more; return how many bytes have
265 // been written so far.
Sami Tolvanen90221202014-12-09 16:39:47 +0000266 break;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700267 }
268 }
269 }
270
271 return written;
272}
273
274// All of the data for all the 'new' transfers is contained in one
275// file in the update package, concatenated together in the order in
276// which transfers.list will need it. We want to stream it out of the
277// archive (it's compressed) without writing it to a temp file, but we
278// can't write each section until it's that transfer's turn to go.
279//
280// To achieve this, we expand the new data from the archive in a
281// background thread, and block that threads 'receive uncompressed
282// data' function until the main thread has reached a point where we
283// want some new data to be written. We signal the background thread
284// with the destination for the data and block the main thread,
285// waiting for the background thread to complete writing that section.
286// Then it signals the main thread to wake up and goes back to
287// blocking waiting for a transfer.
288//
289// NewThreadInfo is the struct used to pass information back and forth
290// between the two threads. When the main thread wants some data
291// written, it sets rss to the destination location and signals the
292// condition. When the background thread is done writing, it clears
293// rss and signals the condition again.
294
295typedef struct {
296 ZipArchive* za;
297 const ZipEntry* entry;
298
299 RangeSinkState* rss;
300
301 pthread_mutex_t mu;
302 pthread_cond_t cv;
303} NewThreadInfo;
304
305static bool receive_new_data(const unsigned char* data, int size, void* cookie) {
306 NewThreadInfo* nti = (NewThreadInfo*) cookie;
307
308 while (size > 0) {
309 // Wait for nti->rss to be non-NULL, indicating some of this
310 // data is wanted.
311 pthread_mutex_lock(&nti->mu);
312 while (nti->rss == NULL) {
313 pthread_cond_wait(&nti->cv, &nti->mu);
314 }
315 pthread_mutex_unlock(&nti->mu);
316
317 // At this point nti->rss is set, and we own it. The main
318 // thread is waiting for it to disappear from nti.
319 ssize_t written = RangeSinkWrite(data, size, nti->rss);
320 data += written;
321 size -= written;
322
323 if (nti->rss->p_block == nti->rss->tgt->count) {
324 // we have written all the bytes desired by this rss.
325
326 pthread_mutex_lock(&nti->mu);
327 nti->rss = NULL;
328 pthread_cond_broadcast(&nti->cv);
329 pthread_mutex_unlock(&nti->mu);
330 }
331 }
332
333 return true;
334}
335
336static void* unzip_new_data(void* cookie) {
337 NewThreadInfo* nti = (NewThreadInfo*) cookie;
338 mzProcessZipEntryContents(nti->za, nti->entry, receive_new_data, nti);
339 return NULL;
340}
341
Sami Tolvanen90221202014-12-09 16:39:47 +0000342static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) {
343 int i;
344 size_t p = 0;
345 size_t size;
346
347 if (!src || !buffer) {
348 return -1;
349 }
350
351 for (i = 0; i < src->count; ++i) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700352 if (!check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000353 return -1;
354 }
355
356 size = (src->pos[i * 2 + 1] - src->pos[i * 2]) * BLOCKSIZE;
357
358 if (read_all(fd, buffer + p, size) == -1) {
359 return -1;
360 }
361
362 p += size;
363 }
364
365 return 0;
366}
367
368static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) {
369 int i;
370 size_t p = 0;
371 size_t size;
372
373 if (!tgt || !buffer) {
374 return -1;
375 }
376
377 for (i = 0; i < tgt->count; ++i) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700378 if (!check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000379 return -1;
380 }
381
382 size = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * BLOCKSIZE;
383
384 if (write_all(fd, buffer + p, size) == -1) {
385 return -1;
386 }
387
388 p += size;
389 }
390
391 return 0;
392}
393
Doug Zongker52ae67d2014-09-08 12:22:09 -0700394// Do a source/target load for move/bsdiff/imgdiff in version 1.
395// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
396// to parse the remainder of the string as:
397//
398// <src_range> <tgt_range>
399//
400// The source range is loaded into the provided buffer, reallocating
401// it to make it larger if necessary. The target ranges are returned
402// in *tgt, if tgt is non-NULL.
403
Sami Tolvanen90221202014-12-09 16:39:47 +0000404static int LoadSrcTgtVersion1(char** wordsave, RangeSet** tgt, int* src_blocks,
Tao Baoe6aa3322015-08-05 15:20:27 -0700405 uint8_t** buffer, size_t* buffer_alloc, int fd) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700406 char* word;
Sami Tolvanen90221202014-12-09 16:39:47 +0000407 int rc;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700408
Sami Tolvanen90221202014-12-09 16:39:47 +0000409 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700410 RangeSet* src = parse_range(word);
411
412 if (tgt != NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000413 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700414 *tgt = parse_range(word);
415 }
416
417 allocate(src->size * BLOCKSIZE, buffer, buffer_alloc);
Sami Tolvanen90221202014-12-09 16:39:47 +0000418 rc = ReadBlocks(src, *buffer, fd);
419 *src_blocks = src->size;
420
421 free(src);
422 return rc;
423}
424
425static int VerifyBlocks(const char *expected, const uint8_t *buffer,
Tao Baoe6aa3322015-08-05 15:20:27 -0700426 size_t blocks, bool printerror) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000427 int rc = -1;
428 uint8_t digest[SHA_DIGEST_SIZE];
429
430 if (!expected || !buffer) {
431 return rc;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700432 }
433
Sami Tolvanen90221202014-12-09 16:39:47 +0000434 SHA_hash(buffer, blocks * BLOCKSIZE, digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000435
Tao Baoe6aa3322015-08-05 15:20:27 -0700436 std::string hexdigest = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000437
Tao Baoe6aa3322015-08-05 15:20:27 -0700438 rc = hexdigest != std::string(expected);
Sami Tolvanen90221202014-12-09 16:39:47 +0000439
Tao Baoe6aa3322015-08-05 15:20:27 -0700440 if (rc != 0 && printerror) {
441 fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
442 expected, hexdigest.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000443 }
444
445 return rc;
446}
447
Tao Baoe6aa3322015-08-05 15:20:27 -0700448static std::string GetStashFileName(const std::string& base, const std::string id,
449 const std::string postfix) {
450 if (base.empty()) {
451 return "";
Sami Tolvanen90221202014-12-09 16:39:47 +0000452 }
453
Tao Baoe6aa3322015-08-05 15:20:27 -0700454 std::string fn(STASH_DIRECTORY_BASE);
455 fn += "/" + base + "/" + id + postfix;
Sami Tolvanen90221202014-12-09 16:39:47 +0000456
457 return fn;
458}
459
Tao Baoe6aa3322015-08-05 15:20:27 -0700460typedef void (*StashCallback)(const std::string&, void*);
Sami Tolvanen90221202014-12-09 16:39:47 +0000461
462// Does a best effort enumeration of stash files. Ignores possible non-file
463// items in the stash directory and continues despite of errors. Calls the
464// 'callback' function for each file and passes 'data' to the function as a
465// parameter.
466
Tao Baoe6aa3322015-08-05 15:20:27 -0700467static void EnumerateStash(const std::string& dirname, StashCallback callback, void* data) {
468 if (dirname.empty() || callback == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000469 return;
470 }
471
Tao Baoe6aa3322015-08-05 15:20:27 -0700472 std::unique_ptr<DIR, int(*)(DIR*)> directory(opendir(dirname.c_str()), closedir);
Sami Tolvanen90221202014-12-09 16:39:47 +0000473
474 if (directory == NULL) {
475 if (errno != ENOENT) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700476 fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000477 }
478 return;
479 }
480
Tao Baoe6aa3322015-08-05 15:20:27 -0700481 struct dirent* item;
482 while ((item = readdir(directory.get())) != NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000483 if (item->d_type != DT_REG) {
484 continue;
485 }
486
Tao Baoe6aa3322015-08-05 15:20:27 -0700487 std::string fn = dirname + "/" + std::string(item->d_name);
Sami Tolvanen90221202014-12-09 16:39:47 +0000488 callback(fn, data);
Sami Tolvanen90221202014-12-09 16:39:47 +0000489 }
490}
491
Tao Baoe6aa3322015-08-05 15:20:27 -0700492static void UpdateFileSize(const std::string& fn, void* data) {
493 if (fn.empty() || !data) {
494 return;
495 }
496
Sami Tolvanen90221202014-12-09 16:39:47 +0000497 struct stat st;
Tao Baoe6aa3322015-08-05 15:20:27 -0700498 if (stat(fn.c_str(), &st) == -1) {
499 fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000500 return;
501 }
502
Tao Baoe6aa3322015-08-05 15:20:27 -0700503 int* size = reinterpret_cast<int*>(data);
Sami Tolvanen90221202014-12-09 16:39:47 +0000504 *size += st.st_size;
505}
506
507// Deletes the stash directory and all files in it. Assumes that it only
508// contains files. There is nothing we can do about unlikely, but possible
509// errors, so they are merely logged.
510
Tao Baoe6aa3322015-08-05 15:20:27 -0700511static void DeleteFile(const std::string& fn, void* data) {
512 if (!fn.empty()) {
513 fprintf(stderr, "deleting %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000514
Tao Baoe6aa3322015-08-05 15:20:27 -0700515 if (unlink(fn.c_str()) == -1 && errno != ENOENT) {
516 fprintf(stderr, "unlink \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000517 }
518 }
519}
520
Tao Baoe6aa3322015-08-05 15:20:27 -0700521static void DeletePartial(const std::string& fn, void* data) {
522 if (android::base::EndsWith(fn, ".partial")) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000523 DeleteFile(fn, data);
524 }
525}
526
Tao Baoe6aa3322015-08-05 15:20:27 -0700527static void DeleteStash(const std::string& base) {
528 if (base.empty()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000529 return;
530 }
531
Tao Baoe6aa3322015-08-05 15:20:27 -0700532 fprintf(stderr, "deleting stash %s\n", base.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000533
Tao Baoe6aa3322015-08-05 15:20:27 -0700534 std::string dirname = GetStashFileName(base, "", "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000535 EnumerateStash(dirname, DeleteFile, NULL);
536
Tao Baoe6aa3322015-08-05 15:20:27 -0700537 if (rmdir(dirname.c_str()) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000538 if (errno != ENOENT && errno != ENOTDIR) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700539 fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000540 }
541 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000542}
543
Tao Baoe6aa3322015-08-05 15:20:27 -0700544static int LoadStash(const std::string& base, const char* id, int verify, int* blocks,
545 uint8_t** buffer, size_t* buffer_alloc, bool printnoent) {
546 std::string fn;
Sami Tolvanen90221202014-12-09 16:39:47 +0000547 int blockcount = 0;
548 int fd = -1;
549 int rc = -1;
550 int res;
551 struct stat st;
552
Tao Baoe6aa3322015-08-05 15:20:27 -0700553 if (base.empty() || !id || !buffer || !buffer_alloc) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000554 goto lsout;
555 }
556
557 if (!blocks) {
558 blocks = &blockcount;
559 }
560
Tao Baoe6aa3322015-08-05 15:20:27 -0700561 fn = GetStashFileName(base, std::string(id), "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000562
Tao Baoe6aa3322015-08-05 15:20:27 -0700563 res = stat(fn.c_str(), &st);
Sami Tolvanen90221202014-12-09 16:39:47 +0000564
565 if (res == -1) {
566 if (errno != ENOENT || printnoent) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700567 fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000568 }
569 goto lsout;
570 }
571
Tao Baoe6aa3322015-08-05 15:20:27 -0700572 fprintf(stderr, " loading %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000573
574 if ((st.st_size % BLOCKSIZE) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700575 fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d",
Tao Baoe6aa3322015-08-05 15:20:27 -0700576 fn.c_str(), static_cast<int64_t>(st.st_size), BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000577 goto lsout;
578 }
579
Tao Baoe6aa3322015-08-05 15:20:27 -0700580 fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY));
Sami Tolvanen90221202014-12-09 16:39:47 +0000581
582 if (fd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700583 fprintf(stderr, "open \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000584 goto lsout;
585 }
586
587 allocate(st.st_size, buffer, buffer_alloc);
588
589 if (read_all(fd, *buffer, st.st_size) == -1) {
590 goto lsout;
591 }
592
593 *blocks = st.st_size / BLOCKSIZE;
594
Tao Baoe6aa3322015-08-05 15:20:27 -0700595 if (verify && VerifyBlocks(id, *buffer, *blocks, true) != 0) {
596 fprintf(stderr, "unexpected contents in %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000597 DeleteFile(fn, NULL);
598 goto lsout;
599 }
600
601 rc = 0;
602
603lsout:
604 if (fd != -1) {
Elliott Hughesb3ac6762015-05-28 23:06:17 -0700605 close(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000606 }
607
Sami Tolvanen90221202014-12-09 16:39:47 +0000608 return rc;
609}
610
Tao Baoe6aa3322015-08-05 15:20:27 -0700611static int WriteStash(const std::string& base, const char* id, int blocks,
612 uint8_t* buffer, bool checkspace, int *exists) {
613 std::string fn;
614 std::string cn;
615 std::string dname;
Sami Tolvanen90221202014-12-09 16:39:47 +0000616 int fd = -1;
617 int rc = -1;
Tao Baodc392262015-07-31 15:56:44 -0700618 int dfd = -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000619 int res;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100620 struct stat st;
Sami Tolvanen90221202014-12-09 16:39:47 +0000621
Tao Baoe6aa3322015-08-05 15:20:27 -0700622 if (base.empty() || buffer == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000623 goto wsout;
624 }
625
626 if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) {
627 fprintf(stderr, "not enough space to write stash\n");
628 goto wsout;
629 }
630
Tao Baoe6aa3322015-08-05 15:20:27 -0700631 fn = GetStashFileName(base, std::string(id), ".partial");
632 cn = GetStashFileName(base, std::string(id), "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000633
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100634 if (exists) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700635 res = stat(cn.c_str(), &st);
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100636
637 if (res == 0) {
638 // The file already exists and since the name is the hash of the contents,
639 // it's safe to assume the contents are identical (accidental hash collisions
640 // are unlikely)
Tao Baoe6aa3322015-08-05 15:20:27 -0700641 fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn.c_str());
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100642 *exists = 1;
643 rc = 0;
644 goto wsout;
645 }
646
647 *exists = 0;
648 }
649
Tao Baoe6aa3322015-08-05 15:20:27 -0700650 fprintf(stderr, " writing %d blocks to %s\n", blocks, cn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000651
Tao Baoe6aa3322015-08-05 15:20:27 -0700652 fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE));
Sami Tolvanen90221202014-12-09 16:39:47 +0000653
654 if (fd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700655 fprintf(stderr, "failed to create \"%s\": %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000656 goto wsout;
657 }
658
659 if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) {
660 goto wsout;
661 }
662
663 if (fsync(fd) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700664 fprintf(stderr, "fsync \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000665 goto wsout;
666 }
667
Tao Baoe6aa3322015-08-05 15:20:27 -0700668 if (rename(fn.c_str(), cn.c_str()) == -1) {
669 fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn.c_str(), cn.c_str(),
670 strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000671 goto wsout;
672 }
673
Tao Baoe6aa3322015-08-05 15:20:27 -0700674 dname = GetStashFileName(base, "", "");
675 dfd = TEMP_FAILURE_RETRY(open(dname.c_str(), O_RDONLY | O_DIRECTORY));
Tao Baodc392262015-07-31 15:56:44 -0700676
677 if (dfd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700678 fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
Tao Baodc392262015-07-31 15:56:44 -0700679 goto wsout;
680 }
681
682 if (fsync(dfd) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700683 fprintf(stderr, "fsync \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
Tao Baodc392262015-07-31 15:56:44 -0700684 goto wsout;
685 }
686
Sami Tolvanen90221202014-12-09 16:39:47 +0000687 rc = 0;
688
689wsout:
690 if (fd != -1) {
Elliott Hughesb47afed2015-05-15 16:19:20 -0700691 close(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000692 }
693
Tao Baodc392262015-07-31 15:56:44 -0700694 if (dfd != -1) {
695 close(dfd);
696 }
697
Sami Tolvanen90221202014-12-09 16:39:47 +0000698 return rc;
699}
700
701// Creates a directory for storing stash files and checks if the /cache partition
702// hash enough space for the expected amount of blocks we need to store. Returns
703// >0 if we created the directory, zero if it existed already, and <0 of failure.
704
Tao Baoe6aa3322015-08-05 15:20:27 -0700705static int CreateStash(State* state, int maxblocks, const char* blockdev,
706 std::string& base) {
707 if (blockdev == NULL) {
708 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000709 }
710
711 // Stash directory should be different for each partition to avoid conflicts
712 // when updating multiple partitions at the same time, so we use the hash of
713 // the block device name as the base directory
Tao Baoe6aa3322015-08-05 15:20:27 -0700714 SHA_CTX ctx;
Sami Tolvanen90221202014-12-09 16:39:47 +0000715 SHA_init(&ctx);
716 SHA_update(&ctx, blockdev, strlen(blockdev));
Tao Baoe6aa3322015-08-05 15:20:27 -0700717 const uint8_t* digest = SHA_final(&ctx);
718 base = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000719
Tao Baoe6aa3322015-08-05 15:20:27 -0700720 std::string dirname = GetStashFileName(base, "", "");
721 struct stat st;
722 int res = stat(dirname.c_str(), &st);
Sami Tolvanen90221202014-12-09 16:39:47 +0000723
724 if (res == -1 && errno != ENOENT) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700725 ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
726 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000727 } else if (res != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700728 fprintf(stderr, "creating stash %s\n", dirname.c_str());
729 res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000730
731 if (res != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700732 ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
733 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000734 }
735
736 if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) {
737 ErrorAbort(state, "not enough space for stash\n");
Tao Baoe6aa3322015-08-05 15:20:27 -0700738 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000739 }
740
Tao Baoe6aa3322015-08-05 15:20:27 -0700741 return 1; // Created directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000742 }
743
Tao Baoe6aa3322015-08-05 15:20:27 -0700744 fprintf(stderr, "using existing stash %s\n", dirname.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000745
746 // If the directory already exists, calculate the space already allocated to
747 // stash files and check if there's enough for all required blocks. Delete any
748 // partially completed stash files first.
749
750 EnumerateStash(dirname, DeletePartial, NULL);
Tao Baoe6aa3322015-08-05 15:20:27 -0700751 int size = 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000752 EnumerateStash(dirname, UpdateFileSize, &size);
753
754 size = (maxblocks * BLOCKSIZE) - size;
755
756 if (size > 0 && CacheSizeCheck(size) != 0) {
757 ErrorAbort(state, "not enough space for stash (%d more needed)\n", size);
Tao Baoe6aa3322015-08-05 15:20:27 -0700758 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000759 }
760
Tao Baoe6aa3322015-08-05 15:20:27 -0700761 return 0; // Using existing directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000762}
763
Tao Baoe6aa3322015-08-05 15:20:27 -0700764static int SaveStash(const std::string& base, char** wordsave, uint8_t** buffer,
765 size_t* buffer_alloc, int fd, int usehash, bool* isunresumable) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000766 if (!wordsave || !buffer || !buffer_alloc || !isunresumable) {
767 return -1;
768 }
769
Tao Baoe6aa3322015-08-05 15:20:27 -0700770 char *id = strtok_r(NULL, " ", wordsave);
Sami Tolvanen90221202014-12-09 16:39:47 +0000771 if (id == NULL) {
772 fprintf(stderr, "missing id field in stash command\n");
773 return -1;
774 }
775
Tao Baoe6aa3322015-08-05 15:20:27 -0700776 int blocks = 0;
777 if (usehash && LoadStash(base, id, 1, &blocks, buffer, buffer_alloc, false) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000778 // Stash file already exists and has expected contents. Do not
779 // read from source again, as the source may have been already
780 // overwritten during a previous attempt.
781 return 0;
782 }
783
784 if (LoadSrcTgtVersion1(wordsave, NULL, &blocks, buffer, buffer_alloc, fd) == -1) {
785 return -1;
786 }
787
Tao Baoe6aa3322015-08-05 15:20:27 -0700788 if (usehash && VerifyBlocks(id, *buffer, blocks, true) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000789 // Source blocks have unexpected contents. If we actually need this
790 // data later, this is an unrecoverable error. However, the command
791 // that uses the data may have already completed previously, so the
792 // possible failure will occur during source block verification.
793 fprintf(stderr, "failed to load source blocks for stash %s\n", id);
794 return 0;
795 }
796
797 fprintf(stderr, "stashing %d blocks to %s\n", blocks, id);
Tao Baoe6aa3322015-08-05 15:20:27 -0700798 return WriteStash(base, id, blocks, *buffer, false, NULL);
Sami Tolvanen90221202014-12-09 16:39:47 +0000799}
800
Tao Baoe6aa3322015-08-05 15:20:27 -0700801static int FreeStash(const std::string& base, const char* id) {
802 if (base.empty() || id == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000803 return -1;
804 }
805
Tao Baoe6aa3322015-08-05 15:20:27 -0700806 std::string fn = GetStashFileName(base, std::string(id), "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000807
808 DeleteFile(fn, NULL);
Sami Tolvanen90221202014-12-09 16:39:47 +0000809
810 return 0;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700811}
812
813static void MoveRange(uint8_t* dest, RangeSet* locs, const uint8_t* source) {
814 // source contains packed data, which we want to move to the
815 // locations given in *locs in the dest buffer. source and dest
816 // may be the same buffer.
817
818 int start = locs->size;
819 int i;
820 for (i = locs->count-1; i >= 0; --i) {
821 int blocks = locs->pos[i*2+1] - locs->pos[i*2];
822 start -= blocks;
823 memmove(dest + (locs->pos[i*2] * BLOCKSIZE), source + (start * BLOCKSIZE),
824 blocks * BLOCKSIZE);
825 }
826}
827
828// Do a source/target load for move/bsdiff/imgdiff in version 2.
829// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
830// to parse the remainder of the string as one of:
831//
832// <tgt_range> <src_block_count> <src_range>
833// (loads data from source image only)
834//
835// <tgt_range> <src_block_count> - <[stash_id:stash_range] ...>
836// (loads data from stashes only)
837//
838// <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...>
839// (loads data from both source image and stashes)
840//
841// On return, buffer is filled with the loaded source data (rearranged
842// and combined with stashed data as necessary). buffer may be
843// reallocated if needed to accommodate the source data. *tgt is the
Sami Tolvanen90221202014-12-09 16:39:47 +0000844// target RangeSet. Any stashes required are loaded using LoadStash.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700845
Sami Tolvanen90221202014-12-09 16:39:47 +0000846static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks,
Tao Baoe6aa3322015-08-05 15:20:27 -0700847 uint8_t** buffer, size_t* buffer_alloc, int fd,
848 const std::string& stashbase, bool* overlap) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700849 char* word;
Sami Tolvanen90221202014-12-09 16:39:47 +0000850 char* colonsave;
851 char* colon;
Sami Tolvanen90221202014-12-09 16:39:47 +0000852 int res;
853 RangeSet* locs;
854 size_t stashalloc = 0;
855 uint8_t* stash = NULL;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700856
857 if (tgt != NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000858 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700859 *tgt = parse_range(word);
860 }
861
Sami Tolvanen90221202014-12-09 16:39:47 +0000862 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700863 *src_blocks = strtol(word, NULL, 0);
864
865 allocate(*src_blocks * BLOCKSIZE, buffer, buffer_alloc);
866
Sami Tolvanen90221202014-12-09 16:39:47 +0000867 word = strtok_r(NULL, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700868 if (word[0] == '-' && word[1] == '\0') {
869 // no source ranges, only stashes
870 } else {
871 RangeSet* src = parse_range(word);
Sami Tolvanen90221202014-12-09 16:39:47 +0000872 res = ReadBlocks(src, *buffer, fd);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700873
Sami Tolvanen90221202014-12-09 16:39:47 +0000874 if (overlap && tgt) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700875 *overlap = range_overlaps(*src, **tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700876 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000877
Doug Zongker52ae67d2014-09-08 12:22:09 -0700878 free(src);
879
Sami Tolvanen90221202014-12-09 16:39:47 +0000880 if (res == -1) {
881 return -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700882 }
883
Sami Tolvanen90221202014-12-09 16:39:47 +0000884 word = strtok_r(NULL, " ", wordsave);
885 if (word == NULL) {
886 // no stashes, only source range
887 return 0;
888 }
889
890 locs = parse_range(word);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700891 MoveRange(*buffer, locs, *buffer);
Sami Tolvanen90221202014-12-09 16:39:47 +0000892 free(locs);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700893 }
894
Sami Tolvanen90221202014-12-09 16:39:47 +0000895 while ((word = strtok_r(NULL, " ", wordsave)) != NULL) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700896 // Each word is a an index into the stash table, a colon, and
897 // then a rangeset describing where in the source block that
898 // stashed data should go.
Sami Tolvanen90221202014-12-09 16:39:47 +0000899 colonsave = NULL;
900 colon = strtok_r(word, ":", &colonsave);
901
Tao Baoe6aa3322015-08-05 15:20:27 -0700902 res = LoadStash(stashbase, colon, 0, NULL, &stash, &stashalloc, true);
Sami Tolvanen90221202014-12-09 16:39:47 +0000903
904 if (res == -1) {
905 // These source blocks will fail verification if used later, but we
906 // will let the caller decide if this is a fatal failure
907 fprintf(stderr, "failed to load stash %s\n", colon);
908 continue;
909 }
910
Doug Zongker52ae67d2014-09-08 12:22:09 -0700911 colon = strtok_r(NULL, ":", &colonsave);
Sami Tolvanen90221202014-12-09 16:39:47 +0000912 locs = parse_range(colon);
913
914 MoveRange(*buffer, locs, stash);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700915 free(locs);
916 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000917
918 if (stash) {
919 free(stash);
920 }
921
922 return 0;
923}
924
925// Parameters for transfer list command functions
926typedef struct {
927 char* cmdname;
928 char* cpos;
929 char* freestash;
Tao Baoe6aa3322015-08-05 15:20:27 -0700930 std::string stashbase;
931 bool canwrite;
Sami Tolvanen90221202014-12-09 16:39:47 +0000932 int createdstash;
933 int fd;
934 int foundwrites;
Tao Baoe6aa3322015-08-05 15:20:27 -0700935 bool isunresumable;
Sami Tolvanen90221202014-12-09 16:39:47 +0000936 int version;
937 int written;
938 NewThreadInfo nti;
939 pthread_t thread;
940 size_t bufsize;
941 uint8_t* buffer;
942 uint8_t* patch_start;
943} CommandParameters;
944
945// Do a source/target load for move/bsdiff/imgdiff in version 3.
946//
947// Parameters are the same as for LoadSrcTgtVersion2, except for 'onehash', which
948// tells the function whether to expect separate source and targe block hashes, or
949// if they are both the same and only one hash should be expected, and
950// 'isunresumable', which receives a non-zero value if block verification fails in
951// a way that the update cannot be resumed anymore.
952//
953// If the function is unable to load the necessary blocks or their contents don't
954// match the hashes, the return value is -1 and the command should be aborted.
955//
956// If the return value is 1, the command has already been completed according to
957// the contents of the target blocks, and should not be performed again.
958//
959// If the return value is 0, source blocks have expected content and the command
960// can be performed.
961
962static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* src_blocks,
Tao Baoe6aa3322015-08-05 15:20:27 -0700963 int onehash, bool* overlap) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000964 char* srchash = NULL;
965 char* tgthash = NULL;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100966 int stash_exists = 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000967 int rc = -1;
968 uint8_t* tgtbuffer = NULL;
969
970 if (!params|| !tgt || !src_blocks || !overlap) {
971 goto v3out;
972 }
973
974 srchash = strtok_r(NULL, " ", &params->cpos);
975
976 if (srchash == NULL) {
977 fprintf(stderr, "missing source hash\n");
978 goto v3out;
979 }
980
981 if (onehash) {
982 tgthash = srchash;
983 } else {
984 tgthash = strtok_r(NULL, " ", &params->cpos);
985
986 if (tgthash == NULL) {
987 fprintf(stderr, "missing target hash\n");
988 goto v3out;
989 }
990 }
991
992 if (LoadSrcTgtVersion2(&params->cpos, tgt, src_blocks, &params->buffer, &params->bufsize,
993 params->fd, params->stashbase, overlap) == -1) {
994 goto v3out;
995 }
996
997 tgtbuffer = (uint8_t*) malloc((*tgt)->size * BLOCKSIZE);
998
999 if (tgtbuffer == NULL) {
1000 fprintf(stderr, "failed to allocate %d bytes\n", (*tgt)->size * BLOCKSIZE);
1001 goto v3out;
1002 }
1003
1004 if (ReadBlocks(*tgt, tgtbuffer, params->fd) == -1) {
1005 goto v3out;
1006 }
1007
Tao Baoe6aa3322015-08-05 15:20:27 -07001008 if (VerifyBlocks(tgthash, tgtbuffer, (*tgt)->size, false) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001009 // Target blocks already have expected content, command should be skipped
1010 rc = 1;
1011 goto v3out;
1012 }
1013
Tao Baoe6aa3322015-08-05 15:20:27 -07001014 if (VerifyBlocks(srchash, params->buffer, *src_blocks, true) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001015 // If source and target blocks overlap, stash the source blocks so we can
1016 // resume from possible write errors
1017 if (*overlap) {
1018 fprintf(stderr, "stashing %d overlapping blocks to %s\n", *src_blocks,
1019 srchash);
1020
Tao Baoe6aa3322015-08-05 15:20:27 -07001021 if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, true,
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001022 &stash_exists) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001023 fprintf(stderr, "failed to stash overlapping source blocks\n");
1024 goto v3out;
1025 }
1026
1027 // Can be deleted when the write has completed
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001028 if (!stash_exists) {
1029 params->freestash = srchash;
1030 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001031 }
1032
1033 // Source blocks have expected content, command can proceed
1034 rc = 0;
1035 goto v3out;
1036 }
1037
1038 if (*overlap && LoadStash(params->stashbase, srchash, 1, NULL, &params->buffer,
Tao Baoe6aa3322015-08-05 15:20:27 -07001039 &params->bufsize, true) == 0) {
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001040 // Overlapping source blocks were previously stashed, command can proceed.
1041 // We are recovering from an interrupted command, so we don't know if the
1042 // stash can safely be deleted after this command.
Sami Tolvanen90221202014-12-09 16:39:47 +00001043 rc = 0;
1044 goto v3out;
1045 }
1046
1047 // Valid source data not available, update cannot be resumed
1048 fprintf(stderr, "partition has unexpected contents\n");
Tao Baoe6aa3322015-08-05 15:20:27 -07001049 params->isunresumable = true;
Sami Tolvanen90221202014-12-09 16:39:47 +00001050
1051v3out:
1052 if (tgtbuffer) {
1053 free(tgtbuffer);
1054 }
1055
1056 return rc;
1057}
1058
1059static int PerformCommandMove(CommandParameters* params) {
1060 int blocks = 0;
Tao Baoe6aa3322015-08-05 15:20:27 -07001061 bool overlap = false;
Sami Tolvanen90221202014-12-09 16:39:47 +00001062 int rc = -1;
1063 int status = 0;
1064 RangeSet* tgt = NULL;
1065
1066 if (!params) {
1067 goto pcmout;
1068 }
1069
1070 if (params->version == 1) {
1071 status = LoadSrcTgtVersion1(&params->cpos, &tgt, &blocks, &params->buffer,
1072 &params->bufsize, params->fd);
1073 } else if (params->version == 2) {
1074 status = LoadSrcTgtVersion2(&params->cpos, &tgt, &blocks, &params->buffer,
1075 &params->bufsize, params->fd, params->stashbase, NULL);
1076 } else if (params->version >= 3) {
1077 status = LoadSrcTgtVersion3(params, &tgt, &blocks, 1, &overlap);
1078 }
1079
1080 if (status == -1) {
1081 fprintf(stderr, "failed to read blocks for move\n");
1082 goto pcmout;
1083 }
1084
1085 if (status == 0) {
1086 params->foundwrites = 1;
1087 } else if (params->foundwrites) {
1088 fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname);
1089 }
1090
1091 if (params->canwrite) {
1092 if (status == 0) {
1093 fprintf(stderr, " moving %d blocks\n", blocks);
1094
1095 if (WriteBlocks(tgt, params->buffer, params->fd) == -1) {
1096 goto pcmout;
1097 }
1098 } else {
1099 fprintf(stderr, "skipping %d already moved blocks\n", blocks);
1100 }
1101
1102 }
1103
1104 if (params->freestash) {
1105 FreeStash(params->stashbase, params->freestash);
1106 params->freestash = NULL;
1107 }
1108
1109 params->written += tgt->size;
1110 rc = 0;
1111
1112pcmout:
1113 if (tgt) {
1114 free(tgt);
1115 }
1116
1117 return rc;
1118}
1119
1120static int PerformCommandStash(CommandParameters* params) {
1121 if (!params) {
1122 return -1;
1123 }
1124
1125 return SaveStash(params->stashbase, &params->cpos, &params->buffer, &params->bufsize,
1126 params->fd, (params->version >= 3), &params->isunresumable);
1127}
1128
1129static int PerformCommandFree(CommandParameters* params) {
1130 if (!params) {
1131 return -1;
1132 }
1133
1134 if (params->createdstash || params->canwrite) {
1135 return FreeStash(params->stashbase, params->cpos);
1136 }
1137
1138 return 0;
1139}
1140
1141static int PerformCommandZero(CommandParameters* params) {
1142 char* range = NULL;
1143 int i;
1144 int j;
1145 int rc = -1;
1146 RangeSet* tgt = NULL;
1147
1148 if (!params) {
1149 goto pczout;
1150 }
1151
1152 range = strtok_r(NULL, " ", &params->cpos);
1153
1154 if (range == NULL) {
1155 fprintf(stderr, "missing target blocks for zero\n");
1156 goto pczout;
1157 }
1158
1159 tgt = parse_range(range);
1160
1161 fprintf(stderr, " zeroing %d blocks\n", tgt->size);
1162
1163 allocate(BLOCKSIZE, &params->buffer, &params->bufsize);
1164 memset(params->buffer, 0, BLOCKSIZE);
1165
1166 if (params->canwrite) {
1167 for (i = 0; i < tgt->count; ++i) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -07001168 if (!check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001169 goto pczout;
1170 }
1171
1172 for (j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) {
1173 if (write_all(params->fd, params->buffer, BLOCKSIZE) == -1) {
1174 goto pczout;
1175 }
1176 }
1177 }
1178 }
1179
1180 if (params->cmdname[0] == 'z') {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001181 // Update only for the zero command, as the erase command will call
1182 // this if DEBUG_ERASE is defined.
Sami Tolvanen90221202014-12-09 16:39:47 +00001183 params->written += tgt->size;
1184 }
1185
1186 rc = 0;
1187
1188pczout:
1189 if (tgt) {
1190 free(tgt);
1191 }
1192
1193 return rc;
1194}
1195
1196static int PerformCommandNew(CommandParameters* params) {
1197 char* range = NULL;
1198 int rc = -1;
1199 RangeSet* tgt = NULL;
1200 RangeSinkState rss;
1201
1202 if (!params) {
1203 goto pcnout;
1204 }
1205
1206 range = strtok_r(NULL, " ", &params->cpos);
1207
1208 if (range == NULL) {
1209 goto pcnout;
1210 }
1211
1212 tgt = parse_range(range);
1213
1214 if (params->canwrite) {
1215 fprintf(stderr, " writing %d blocks of new data\n", tgt->size);
1216
1217 rss.fd = params->fd;
1218 rss.tgt = tgt;
1219 rss.p_block = 0;
1220 rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
1221
Elliott Hughes7bad7c42015-04-28 17:24:24 -07001222 if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001223 goto pcnout;
1224 }
1225
1226 pthread_mutex_lock(&params->nti.mu);
1227 params->nti.rss = &rss;
1228 pthread_cond_broadcast(&params->nti.cv);
1229
1230 while (params->nti.rss) {
1231 pthread_cond_wait(&params->nti.cv, &params->nti.mu);
1232 }
1233
1234 pthread_mutex_unlock(&params->nti.mu);
1235 }
1236
1237 params->written += tgt->size;
1238 rc = 0;
1239
1240pcnout:
1241 if (tgt) {
1242 free(tgt);
1243 }
1244
1245 return rc;
1246}
1247
1248static int PerformCommandDiff(CommandParameters* params) {
1249 char* logparams = NULL;
1250 char* value = NULL;
1251 int blocks = 0;
Tao Baoe6aa3322015-08-05 15:20:27 -07001252 bool overlap = false;
Sami Tolvanen90221202014-12-09 16:39:47 +00001253 int rc = -1;
1254 int status = 0;
1255 RangeSet* tgt = NULL;
1256 RangeSinkState rss;
1257 size_t len = 0;
1258 size_t offset = 0;
1259 Value patch_value;
1260
1261 if (!params) {
1262 goto pcdout;
1263 }
1264
1265 logparams = strdup(params->cpos);
1266 value = strtok_r(NULL, " ", &params->cpos);
1267
1268 if (value == NULL) {
1269 fprintf(stderr, "missing patch offset for %s\n", params->cmdname);
1270 goto pcdout;
1271 }
1272
1273 offset = strtoul(value, NULL, 0);
1274
1275 value = strtok_r(NULL, " ", &params->cpos);
1276
1277 if (value == NULL) {
1278 fprintf(stderr, "missing patch length for %s\n", params->cmdname);
1279 goto pcdout;
1280 }
1281
1282 len = strtoul(value, NULL, 0);
1283
1284 if (params->version == 1) {
1285 status = LoadSrcTgtVersion1(&params->cpos, &tgt, &blocks, &params->buffer,
1286 &params->bufsize, params->fd);
1287 } else if (params->version == 2) {
1288 status = LoadSrcTgtVersion2(&params->cpos, &tgt, &blocks, &params->buffer,
1289 &params->bufsize, params->fd, params->stashbase, NULL);
1290 } else if (params->version >= 3) {
1291 status = LoadSrcTgtVersion3(params, &tgt, &blocks, 0, &overlap);
1292 }
1293
1294 if (status == -1) {
1295 fprintf(stderr, "failed to read blocks for diff\n");
1296 goto pcdout;
1297 }
1298
1299 if (status == 0) {
1300 params->foundwrites = 1;
1301 } else if (params->foundwrites) {
1302 fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname);
1303 }
1304
1305 if (params->canwrite) {
1306 if (status == 0) {
1307 fprintf(stderr, "patching %d blocks to %d\n", blocks, tgt->size);
1308
1309 patch_value.type = VAL_BLOB;
1310 patch_value.size = len;
1311 patch_value.data = (char*) (params->patch_start + offset);
1312
1313 rss.fd = params->fd;
1314 rss.tgt = tgt;
1315 rss.p_block = 0;
1316 rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
1317
Elliott Hughes7bad7c42015-04-28 17:24:24 -07001318 if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001319 goto pcdout;
1320 }
1321
1322 if (params->cmdname[0] == 'i') { // imgdiff
1323 ApplyImagePatch(params->buffer, blocks * BLOCKSIZE, &patch_value,
1324 &RangeSinkWrite, &rss, NULL, NULL);
1325 } else {
1326 ApplyBSDiffPatch(params->buffer, blocks * BLOCKSIZE, &patch_value,
1327 0, &RangeSinkWrite, &rss, NULL);
1328 }
1329
1330 // We expect the output of the patcher to fill the tgt ranges exactly.
1331 if (rss.p_block != tgt->count || rss.p_remain != 0) {
1332 fprintf(stderr, "range sink underrun?\n");
1333 }
1334 } else {
1335 fprintf(stderr, "skipping %d blocks already patched to %d [%s]\n",
1336 blocks, tgt->size, logparams);
1337 }
1338 }
1339
1340 if (params->freestash) {
1341 FreeStash(params->stashbase, params->freestash);
1342 params->freestash = NULL;
1343 }
1344
1345 params->written += tgt->size;
1346 rc = 0;
1347
1348pcdout:
1349 if (logparams) {
1350 free(logparams);
1351 }
1352
1353 if (tgt) {
1354 free(tgt);
1355 }
1356
1357 return rc;
1358}
1359
1360static int PerformCommandErase(CommandParameters* params) {
1361 char* range = NULL;
1362 int i;
1363 int rc = -1;
1364 RangeSet* tgt = NULL;
1365 struct stat st;
1366 uint64_t blocks[2];
1367
Sami Tolvanene82fa182015-06-10 15:58:12 +00001368 if (DEBUG_ERASE) {
1369 return PerformCommandZero(params);
Sami Tolvanen90221202014-12-09 16:39:47 +00001370 }
1371
1372 if (!params) {
1373 goto pceout;
1374 }
1375
1376 if (fstat(params->fd, &st) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001377 fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001378 goto pceout;
1379 }
1380
1381 if (!S_ISBLK(st.st_mode)) {
1382 fprintf(stderr, "not a block device; skipping erase\n");
Sami Tolvanen90221202014-12-09 16:39:47 +00001383 goto pceout;
1384 }
1385
1386 range = strtok_r(NULL, " ", &params->cpos);
1387
1388 if (range == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001389 fprintf(stderr, "missing target blocks for erase\n");
Sami Tolvanen90221202014-12-09 16:39:47 +00001390 goto pceout;
1391 }
1392
1393 tgt = parse_range(range);
1394
1395 if (params->canwrite) {
1396 fprintf(stderr, " erasing %d blocks\n", tgt->size);
1397
1398 for (i = 0; i < tgt->count; ++i) {
1399 // offset in bytes
1400 blocks[0] = tgt->pos[i * 2] * (uint64_t) BLOCKSIZE;
1401 // length in bytes
1402 blocks[1] = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * (uint64_t) BLOCKSIZE;
1403
1404 if (ioctl(params->fd, BLKDISCARD, &blocks) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001405 fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno));
Sami Tolvanencc2428c2015-04-27 11:24:29 +01001406 goto pceout;
Sami Tolvanen90221202014-12-09 16:39:47 +00001407 }
1408 }
1409 }
1410
1411 rc = 0;
1412
1413pceout:
1414 if (tgt) {
1415 free(tgt);
1416 }
1417
1418 return rc;
1419}
1420
1421// Definitions for transfer list command functions
1422typedef int (*CommandFunction)(CommandParameters*);
1423
1424typedef struct {
1425 const char* name;
1426 CommandFunction f;
1427} Command;
1428
1429// CompareCommands and CompareCommandNames are for the hash table
1430
1431static int CompareCommands(const void* c1, const void* c2) {
1432 return strcmp(((const Command*) c1)->name, ((const Command*) c2)->name);
1433}
1434
1435static int CompareCommandNames(const void* c1, const void* c2) {
1436 return strcmp(((const Command*) c1)->name, (const char*) c2);
1437}
1438
1439// HashString is used to hash command names for the hash table
1440
1441static unsigned int HashString(const char *s) {
1442 unsigned int hash = 0;
1443 if (s) {
1444 while (*s) {
1445 hash = hash * 33 + *s++;
1446 }
1447 }
1448 return hash;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001449}
1450
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001451// args:
1452// - block device (or file) to modify in-place
1453// - transfer list (blob)
1454// - new data stream (filename within package.zip)
1455// - patch stream (filename within package.zip, must be uncompressed)
1456
Sami Tolvanen90221202014-12-09 16:39:47 +00001457static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, Expr* argv[],
Tao Baoe6aa3322015-08-05 15:20:27 -07001458 const Command* commands, int cmdcount, bool dryrun) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001459
1460 char* line = NULL;
1461 char* linesave = NULL;
1462 char* logcmd = NULL;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001463 char* transfer_list = NULL;
Sami Tolvanen90221202014-12-09 16:39:47 +00001464 CommandParameters params;
1465 const Command* cmd = NULL;
1466 const ZipEntry* new_entry = NULL;
1467 const ZipEntry* patch_entry = NULL;
1468 FILE* cmd_pipe = NULL;
1469 HashTable* cmdht = NULL;
1470 int i;
1471 int res;
1472 int rc = -1;
1473 int stash_max_blocks = 0;
1474 int total_blocks = 0;
1475 pthread_attr_t attr;
1476 unsigned int cmdhash;
1477 UpdaterInfo* ui = NULL;
1478 Value* blockdev_filename = NULL;
1479 Value* new_data_fn = NULL;
1480 Value* patch_data_fn = NULL;
1481 Value* transfer_list_value = NULL;
1482 ZipArchive* za = NULL;
1483
1484 memset(&params, 0, sizeof(params));
1485 params.canwrite = !dryrun;
1486
1487 fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001488
Doug Zongker1d5d6092014-08-21 10:47:24 -07001489 if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value,
Sami Tolvanen90221202014-12-09 16:39:47 +00001490 &new_data_fn, &patch_data_fn) < 0) {
1491 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001492 }
1493
1494 if (blockdev_filename->type != VAL_STRING) {
1495 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001496 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001497 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001498 if (transfer_list_value->type != VAL_BLOB) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001499 ErrorAbort(state, "transfer_list argument to %s must be blob", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001500 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001501 }
1502 if (new_data_fn->type != VAL_STRING) {
1503 ErrorAbort(state, "new_data_fn argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001504 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001505 }
1506 if (patch_data_fn->type != VAL_STRING) {
1507 ErrorAbort(state, "patch_data_fn argument to %s must be string", name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001508 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001509 }
1510
Sami Tolvanen90221202014-12-09 16:39:47 +00001511 ui = (UpdaterInfo*) state->cookie;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001512
Sami Tolvanen90221202014-12-09 16:39:47 +00001513 if (ui == NULL) {
1514 goto pbiudone;
1515 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001516
Sami Tolvanen90221202014-12-09 16:39:47 +00001517 cmd_pipe = ui->cmd_pipe;
1518 za = ui->package_zip;
1519
1520 if (cmd_pipe == NULL || za == NULL) {
1521 goto pbiudone;
1522 }
1523
1524 patch_entry = mzFindZipEntry(za, patch_data_fn->data);
1525
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001526 if (patch_entry == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001527 fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data);
1528 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001529 }
1530
Sami Tolvanen90221202014-12-09 16:39:47 +00001531 params.patch_start = ui->package_zip_addr + mzGetZipEntryOffset(patch_entry);
1532 new_entry = mzFindZipEntry(za, new_data_fn->data);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001533
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001534 if (new_entry == NULL) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001535 fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data);
1536 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001537 }
1538
Sami Tolvanen90221202014-12-09 16:39:47 +00001539 params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001540
Sami Tolvanen90221202014-12-09 16:39:47 +00001541 if (params.fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001542 fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data, strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001543 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001544 }
1545
Sami Tolvanen90221202014-12-09 16:39:47 +00001546 if (params.canwrite) {
1547 params.nti.za = za;
1548 params.nti.entry = new_entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001549
Sami Tolvanen90221202014-12-09 16:39:47 +00001550 pthread_mutex_init(&params.nti.mu, NULL);
1551 pthread_cond_init(&params.nti.cv, NULL);
1552 pthread_attr_init(&attr);
1553 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1554
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001555 int error = pthread_create(&params.thread, &attr, unzip_new_data, &params.nti);
1556 if (error != 0) {
1557 fprintf(stderr, "pthread_create failed: %s\n", strerror(error));
Sami Tolvanen90221202014-12-09 16:39:47 +00001558 goto pbiudone;
1559 }
1560 }
1561
1562 // The data in transfer_list_value is not necessarily null-terminated, so we need
1563 // to copy it to a new buffer and add the null that strtok_r will need.
Tao Baoba9a42a2015-06-23 23:23:33 -07001564 transfer_list = reinterpret_cast<char*>(malloc(transfer_list_value->size + 1));
Sami Tolvanen90221202014-12-09 16:39:47 +00001565
Doug Zongker1d5d6092014-08-21 10:47:24 -07001566 if (transfer_list == NULL) {
1567 fprintf(stderr, "failed to allocate %zd bytes for transfer list\n",
Sami Tolvanen90221202014-12-09 16:39:47 +00001568 transfer_list_value->size + 1);
1569 goto pbiudone;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001570 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001571
Doug Zongker1d5d6092014-08-21 10:47:24 -07001572 memcpy(transfer_list, transfer_list_value->data, transfer_list_value->size);
1573 transfer_list[transfer_list_value->size] = '\0';
1574
Sami Tolvanen90221202014-12-09 16:39:47 +00001575 // First line in transfer list is the version number
Doug Zongker1d5d6092014-08-21 10:47:24 -07001576 line = strtok_r(transfer_list, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001577 params.version = strtol(line, NULL, 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001578
Sami Tolvanen90221202014-12-09 16:39:47 +00001579 if (params.version < 1 || params.version > 3) {
1580 fprintf(stderr, "unexpected transfer list version [%s]\n", line);
1581 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001582 }
1583
Sami Tolvanen90221202014-12-09 16:39:47 +00001584 fprintf(stderr, "blockimg version is %d\n", params.version);
1585
1586 // Second line in transfer list is the total number of blocks we expect to write
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001587 line = strtok_r(NULL, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001588 total_blocks = strtol(line, NULL, 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001589
Sami Tolvanen90221202014-12-09 16:39:47 +00001590 if (total_blocks < 0) {
1591 ErrorAbort(state, "unexpected block count [%s]\n", line);
1592 goto pbiudone;
1593 } else if (total_blocks == 0) {
1594 rc = 0;
1595 goto pbiudone;
1596 }
1597
1598 if (params.version >= 2) {
1599 // Third line is how many stash entries are needed simultaneously
Doug Zongker52ae67d2014-09-08 12:22:09 -07001600 line = strtok_r(NULL, "\n", &linesave);
Sami Tolvanen90221202014-12-09 16:39:47 +00001601 fprintf(stderr, "maximum stash entries %s\n", line);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001602
Sami Tolvanen90221202014-12-09 16:39:47 +00001603 // Fourth line is the maximum number of blocks that will be stashed simultaneously
1604 line = strtok_r(NULL, "\n", &linesave);
1605 stash_max_blocks = strtol(line, NULL, 0);
1606
1607 if (stash_max_blocks < 0) {
1608 ErrorAbort(state, "unexpected maximum stash blocks [%s]\n", line);
1609 goto pbiudone;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001610 }
1611
Jesse Zhao1df64d32015-02-17 17:09:23 -08001612 if (stash_max_blocks >= 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -07001613 res = CreateStash(state, stash_max_blocks, blockdev_filename->data, params.stashbase);
Sami Tolvanen90221202014-12-09 16:39:47 +00001614
1615 if (res == -1) {
1616 goto pbiudone;
1617 }
1618
1619 params.createdstash = res;
1620 }
Doug Zongker52ae67d2014-09-08 12:22:09 -07001621 }
1622
Sami Tolvanen90221202014-12-09 16:39:47 +00001623 // Build a hash table of the available commands
1624 cmdht = mzHashTableCreate(cmdcount, NULL);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001625
Sami Tolvanen90221202014-12-09 16:39:47 +00001626 for (i = 0; i < cmdcount; ++i) {
1627 cmdhash = HashString(commands[i].name);
1628 mzHashTableLookup(cmdht, cmdhash, (void*) &commands[i], CompareCommands, true);
1629 }
1630
1631 // Subsequent lines are all individual transfer commands
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001632 for (line = strtok_r(NULL, "\n", &linesave); line;
1633 line = strtok_r(NULL, "\n", &linesave)) {
Doug Zongker52ae67d2014-09-08 12:22:09 -07001634
Sami Tolvanen90221202014-12-09 16:39:47 +00001635 logcmd = strdup(line);
1636 params.cmdname = strtok_r(line, " ", &params.cpos);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001637
Sami Tolvanen90221202014-12-09 16:39:47 +00001638 if (params.cmdname == NULL) {
1639 fprintf(stderr, "missing command [%s]\n", line);
1640 goto pbiudone;
1641 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001642
Sami Tolvanen90221202014-12-09 16:39:47 +00001643 cmdhash = HashString(params.cmdname);
1644 cmd = (const Command*) mzHashTableLookup(cmdht, cmdhash, params.cmdname,
1645 CompareCommandNames, false);
Doug Zongker52ae67d2014-09-08 12:22:09 -07001646
Sami Tolvanen90221202014-12-09 16:39:47 +00001647 if (cmd == NULL) {
1648 fprintf(stderr, "unexpected command [%s]\n", params.cmdname);
1649 goto pbiudone;
1650 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001651
Sami Tolvanen90221202014-12-09 16:39:47 +00001652 if (cmd->f != NULL && cmd->f(&params) == -1) {
1653 fprintf(stderr, "failed to execute command [%s]\n",
1654 logcmd ? logcmd : params.cmdname);
1655 goto pbiudone;
1656 }
1657
1658 if (logcmd) {
1659 free(logcmd);
1660 logcmd = NULL;
1661 }
1662
1663 if (params.canwrite) {
Tao Bao187efff2015-07-27 14:07:08 -07001664 if (fsync(params.fd) == -1) {
1665 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
1666 goto pbiudone;
1667 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001668 fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001669 fflush(cmd_pipe);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001670 }
1671 }
1672
Sami Tolvanen90221202014-12-09 16:39:47 +00001673 if (params.canwrite) {
1674 pthread_join(params.thread, NULL);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001675
Sami Tolvanen90221202014-12-09 16:39:47 +00001676 fprintf(stderr, "wrote %d blocks; expected %d\n", params.written, total_blocks);
1677 fprintf(stderr, "max alloc needed was %zu\n", params.bufsize);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001678
Sami Tolvanen90221202014-12-09 16:39:47 +00001679 // Delete stash only after successfully completing the update, as it
1680 // may contain blocks needed to complete the update later.
1681 DeleteStash(params.stashbase);
1682 } else {
1683 fprintf(stderr, "verified partition contents; update may be resumed\n");
1684 }
1685
1686 rc = 0;
1687
1688pbiudone:
1689 if (params.fd != -1) {
1690 if (fsync(params.fd) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001691 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001692 }
Elliott Hughesb47afed2015-05-15 16:19:20 -07001693 close(params.fd);
Sami Tolvanen90221202014-12-09 16:39:47 +00001694 }
1695
1696 if (logcmd) {
1697 free(logcmd);
1698 }
1699
1700 if (cmdht) {
1701 mzHashTableFree(cmdht);
1702 }
1703
1704 if (params.buffer) {
1705 free(params.buffer);
1706 }
1707
1708 if (transfer_list) {
1709 free(transfer_list);
1710 }
1711
1712 if (blockdev_filename) {
1713 FreeValue(blockdev_filename);
1714 }
1715
1716 if (transfer_list_value) {
1717 FreeValue(transfer_list_value);
1718 }
1719
1720 if (new_data_fn) {
1721 FreeValue(new_data_fn);
1722 }
1723
1724 if (patch_data_fn) {
1725 FreeValue(patch_data_fn);
1726 }
1727
1728 // Only delete the stash if the update cannot be resumed, or it's
1729 // a verification run and we created the stash.
1730 if (params.isunresumable || (!params.canwrite && params.createdstash)) {
1731 DeleteStash(params.stashbase);
1732 }
1733
Sami Tolvanen90221202014-12-09 16:39:47 +00001734 return StringValue(rc == 0 ? strdup("t") : strdup(""));
1735}
1736
1737// The transfer list is a text file containing commands to
1738// transfer data from one place to another on the target
1739// partition. We parse it and execute the commands in order:
1740//
1741// zero [rangeset]
1742// - fill the indicated blocks with zeros
1743//
1744// new [rangeset]
1745// - fill the blocks with data read from the new_data file
1746//
1747// erase [rangeset]
1748// - mark the given blocks as empty
1749//
1750// move <...>
1751// bsdiff <patchstart> <patchlen> <...>
1752// imgdiff <patchstart> <patchlen> <...>
1753// - read the source blocks, apply a patch (or not in the
1754// case of move), write result to target blocks. bsdiff or
1755// imgdiff specifies the type of patch; move means no patch
1756// at all.
1757//
1758// The format of <...> differs between versions 1 and 2;
1759// see the LoadSrcTgtVersion{1,2}() functions for a
1760// description of what's expected.
1761//
1762// stash <stash_id> <src_range>
1763// - (version 2+ only) load the given source range and stash
1764// the data in the given slot of the stash table.
1765//
1766// The creator of the transfer list will guarantee that no block
1767// is read (ie, used as the source for a patch or move) after it
1768// has been written.
1769//
1770// In version 2, the creator will guarantee that a given stash is
1771// loaded (with a stash command) before it's used in a
1772// move/bsdiff/imgdiff command.
1773//
1774// Within one command the source and target ranges may overlap so
1775// in general we need to read the entire source into memory before
1776// writing anything to the target blocks.
1777//
1778// All the patch data is concatenated into one patch_data file in
1779// the update package. It must be stored uncompressed because we
1780// memory-map it in directly from the archive. (Since patches are
1781// already compressed, we lose very little by not compressing
1782// their concatenation.)
1783//
1784// In version 3, commands that read data from the partition (i.e.
1785// move/bsdiff/imgdiff/stash) have one or more additional hashes
1786// before the range parameters, which are used to check if the
1787// command has already been completed and verify the integrity of
1788// the source data.
1789
1790Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]) {
1791 // Commands which are not tested are set to NULL to skip them completely
1792 const Command commands[] = {
1793 { "bsdiff", PerformCommandDiff },
1794 { "erase", NULL },
1795 { "free", PerformCommandFree },
1796 { "imgdiff", PerformCommandDiff },
1797 { "move", PerformCommandMove },
1798 { "new", NULL },
1799 { "stash", PerformCommandStash },
1800 { "zero", NULL }
1801 };
1802
1803 // Perform a dry run without writing to test if an update can proceed
1804 return PerformBlockImageUpdate(name, state, argc, argv, commands,
Tao Baoe6aa3322015-08-05 15:20:27 -07001805 sizeof(commands) / sizeof(commands[0]), true);
Sami Tolvanen90221202014-12-09 16:39:47 +00001806}
1807
1808Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) {
1809 const Command commands[] = {
1810 { "bsdiff", PerformCommandDiff },
1811 { "erase", PerformCommandErase },
1812 { "free", PerformCommandFree },
1813 { "imgdiff", PerformCommandDiff },
1814 { "move", PerformCommandMove },
1815 { "new", PerformCommandNew },
1816 { "stash", PerformCommandStash },
1817 { "zero", PerformCommandZero }
1818 };
1819
1820 return PerformBlockImageUpdate(name, state, argc, argv, commands,
Tao Baoe6aa3322015-08-05 15:20:27 -07001821 sizeof(commands) / sizeof(commands[0]), false);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001822}
1823
1824Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) {
1825 Value* blockdev_filename;
1826 Value* ranges;
1827 const uint8_t* digest = NULL;
1828 if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) {
1829 return NULL;
1830 }
1831
1832 if (blockdev_filename->type != VAL_STRING) {
1833 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
1834 goto done;
1835 }
1836 if (ranges->type != VAL_STRING) {
1837 ErrorAbort(state, "ranges argument to %s must be string", name);
1838 goto done;
1839 }
1840
Tao Baoba9a42a2015-06-23 23:23:33 -07001841 int fd;
1842 fd = open(blockdev_filename->data, O_RDWR);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001843 if (fd < 0) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001844 ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001845 goto done;
1846 }
1847
Tao Baoba9a42a2015-06-23 23:23:33 -07001848 RangeSet* rs;
1849 rs = parse_range(ranges->data);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001850 uint8_t buffer[BLOCKSIZE];
1851
1852 SHA_CTX ctx;
1853 SHA_init(&ctx);
1854
1855 int i, j;
1856 for (i = 0; i < rs->count; ++i) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -07001857 if (!check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001858 ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data,
1859 strerror(errno));
1860 goto done;
1861 }
1862
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001863 for (j = rs->pos[i*2]; j < rs->pos[i*2+1]; ++j) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001864 if (read_all(fd, buffer, BLOCKSIZE) == -1) {
1865 ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data,
1866 strerror(errno));
1867 goto done;
1868 }
1869
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001870 SHA_update(&ctx, buffer, BLOCKSIZE);
1871 }
1872 }
1873 digest = SHA_final(&ctx);
1874 close(fd);
1875
1876 done:
1877 FreeValue(blockdev_filename);
1878 FreeValue(ranges);
1879 if (digest == NULL) {
1880 return StringValue(strdup(""));
1881 } else {
Tao Baoe6aa3322015-08-05 15:20:27 -07001882 return StringValue(strdup(print_sha1(digest).c_str()));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001883 }
1884}
1885
1886void RegisterBlockImageFunctions() {
Sami Tolvanen90221202014-12-09 16:39:47 +00001887 RegisterFunction("block_image_verify", BlockImageVerifyFn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001888 RegisterFunction("block_image_update", BlockImageUpdateFn);
1889 RegisterFunction("range_sha1", RangeSha1Fn);
1890}