blob: ddb474ffdc80999cc60a733580be83d59bba4ed9 [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>
Tao Bao0940fe12015-08-27 16:41:21 -070037#include <vector>
Tao Baoe6aa3322015-08-05 15:20:27 -070038
Tao Baob15fd222015-09-24 11:10:51 -070039#include <base/parseint.h>
Tao Baoe6aa3322015-08-05 15:20:27 -070040#include <base/strings.h>
41
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070042#include "applypatch/applypatch.h"
43#include "edify/expr.h"
44#include "mincrypt/sha.h"
Sami Tolvanen90221202014-12-09 16:39:47 +000045#include "minzip/Hash.h"
Tao Baoe6aa3322015-08-05 15:20:27 -070046#include "print_sha1.h"
Tao Bao0940fe12015-08-27 16:41:21 -070047#include "unique_fd.h"
48#include "updater.h"
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070049
50#define BLOCKSIZE 4096
51
Sami Tolvanene82fa182015-06-10 15:58:12 +000052// Set this to 0 to interpret 'erase' transfers to mean do a
53// BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret
54// erase to mean fill the region with zeroes.
55#define DEBUG_ERASE 0
56
Sami Tolvanen90221202014-12-09 16:39:47 +000057#define STASH_DIRECTORY_BASE "/cache/recovery"
58#define STASH_DIRECTORY_MODE 0700
59#define STASH_FILE_MODE 0600
60
Tao Bao0940fe12015-08-27 16:41:21 -070061struct RangeSet {
62 size_t count; // Limit is INT_MAX.
Shrinivas Sahukara6153df2015-08-19 13:01:45 +053063 size_t size;
Tao Bao0940fe12015-08-27 16:41:21 -070064 std::vector<size_t> pos; // Actual limit is INT_MAX.
65};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070066
Tao Bao0940fe12015-08-27 16:41:21 -070067static void parse_range(const char* range_text, RangeSet& rs) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010068
Tao Bao0940fe12015-08-27 16:41:21 -070069 if (range_text == nullptr) {
70 fprintf(stderr, "failed to parse range: null range\n");
71 exit(1);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070072 }
Sami Tolvanenf2bac042015-05-12 12:48:46 +010073
Tao Bao0940fe12015-08-27 16:41:21 -070074 std::vector<std::string> pieces = android::base::Split(std::string(range_text), ",");
75 long int val;
Sami Tolvanenf2bac042015-05-12 12:48:46 +010076
Tao Bao0940fe12015-08-27 16:41:21 -070077 if (pieces.size() < 3) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010078 goto err;
79 }
80
Tao Baob15fd222015-09-24 11:10:51 -070081 size_t num;
82 if (!android::base::ParseUint(pieces[0].c_str(), &num, static_cast<size_t>(INT_MAX))) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010083 goto err;
84 }
85
Tao Baob15fd222015-09-24 11:10:51 -070086 if (num == 0 || num % 2) {
87 goto err; // must be even
88 } else if (num != pieces.size() - 1) {
89 goto err;
90 }
Sami Tolvanenf2bac042015-05-12 12:48:46 +010091
Tao Bao0940fe12015-08-27 16:41:21 -070092 rs.pos.resize(num);
93 rs.count = num / 2;
94 rs.size = 0;
Sami Tolvanenf2bac042015-05-12 12:48:46 +010095
Tao Bao0940fe12015-08-27 16:41:21 -070096 for (size_t i = 0; i < num; i += 2) {
Tao Baob15fd222015-09-24 11:10:51 -070097 if (!android::base::ParseUint(pieces[i+1].c_str(), &rs.pos[i],
98 static_cast<size_t>(INT_MAX))) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010099 goto err;
100 }
101
Tao Baob15fd222015-09-24 11:10:51 -0700102 if (!android::base::ParseUint(pieces[i+2].c_str(), &rs.pos[i+1],
103 static_cast<size_t>(INT_MAX))) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530104 goto err;
105 }
106
Tao Bao0940fe12015-08-27 16:41:21 -0700107 if (rs.pos[i] >= rs.pos[i+1]) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530108 goto err; // empty or negative range
109 }
110
Tao Bao0940fe12015-08-27 16:41:21 -0700111 size_t sz = rs.pos[i+1] - rs.pos[i];
112 if (rs.size > SIZE_MAX - sz) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530113 goto err; // overflow
114 }
115
Tao Bao0940fe12015-08-27 16:41:21 -0700116 rs.size += sz;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700117 }
118
Tao Bao0940fe12015-08-27 16:41:21 -0700119 return;
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100120
121err:
Tao Bao0940fe12015-08-27 16:41:21 -0700122 fprintf(stderr, "failed to parse range '%s'\n", range_text);
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100123 exit(1);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700124}
125
Tao Baoe6aa3322015-08-05 15:20:27 -0700126static bool range_overlaps(const RangeSet& r1, const RangeSet& r2) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530127 for (size_t i = 0; i < r1.count; ++i) {
128 size_t r1_0 = r1.pos[i * 2];
129 size_t r1_1 = r1.pos[i * 2 + 1];
Sami Tolvanen90221202014-12-09 16:39:47 +0000130
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530131 for (size_t j = 0; j < r2.count; ++j) {
132 size_t r2_0 = r2.pos[j * 2];
133 size_t r2_1 = r2.pos[j * 2 + 1];
Sami Tolvanen90221202014-12-09 16:39:47 +0000134
Tao Baoc0f56ad2015-06-25 14:00:31 -0700135 if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700136 return true;
Sami Tolvanen90221202014-12-09 16:39:47 +0000137 }
138 }
139 }
140
Tao Baoe6aa3322015-08-05 15:20:27 -0700141 return false;
Sami Tolvanen90221202014-12-09 16:39:47 +0000142}
143
144static int read_all(int fd, uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700145 size_t so_far = 0;
146 while (so_far < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700147 ssize_t r = TEMP_FAILURE_RETRY(read(fd, data+so_far, size-so_far));
148 if (r == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700149 fprintf(stderr, "read failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000150 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700151 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700152 so_far += r;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700153 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000154 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700155}
156
Tao Bao612336d2015-08-27 16:41:21 -0700157static int read_all(int fd, std::vector<uint8_t>& buffer, size_t size) {
158 return read_all(fd, buffer.data(), size);
159}
160
Sami Tolvanen90221202014-12-09 16:39:47 +0000161static int write_all(int fd, const uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700162 size_t written = 0;
163 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700164 ssize_t w = TEMP_FAILURE_RETRY(write(fd, data+written, size-written));
165 if (w == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700166 fprintf(stderr, "write failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000167 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700168 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700169 written += w;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700170 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000171
Sami Tolvanen90221202014-12-09 16:39:47 +0000172 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700173}
174
Tao Bao612336d2015-08-27 16:41:21 -0700175static int write_all(int fd, const std::vector<uint8_t>& buffer, size_t size) {
176 return write_all(fd, buffer.data(), size);
177}
178
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700179static bool check_lseek(int fd, off64_t offset, int whence) {
180 off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
181 if (rc == -1) {
182 fprintf(stderr, "lseek64 failed: %s\n", strerror(errno));
183 return false;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700184 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700185 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700186}
187
Tao Bao612336d2015-08-27 16:41:21 -0700188static void allocate(size_t size, std::vector<uint8_t>& buffer) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700189 // if the buffer's big enough, reuse it.
Tao Bao612336d2015-08-27 16:41:21 -0700190 if (size <= buffer.size()) return;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700191
Tao Bao612336d2015-08-27 16:41:21 -0700192 buffer.resize(size);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700193}
194
Tao Bao0940fe12015-08-27 16:41:21 -0700195struct RangeSinkState {
196 RangeSinkState(RangeSet& rs) : tgt(rs) { };
197
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700198 int fd;
Tao Bao0940fe12015-08-27 16:41:21 -0700199 const RangeSet& tgt;
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530200 size_t p_block;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700201 size_t p_remain;
Tao Bao0940fe12015-08-27 16:41:21 -0700202};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700203
204static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) {
Tao Bao0940fe12015-08-27 16:41:21 -0700205 RangeSinkState* rss = reinterpret_cast<RangeSinkState*>(token);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700206
Tao Bao0940fe12015-08-27 16:41:21 -0700207 if (rss->p_remain == 0) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700208 fprintf(stderr, "range sink write overrun");
Sami Tolvanen90221202014-12-09 16:39:47 +0000209 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700210 }
211
212 ssize_t written = 0;
213 while (size > 0) {
214 size_t write_now = size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000215
216 if (rss->p_remain < write_now) {
217 write_now = rss->p_remain;
218 }
219
220 if (write_all(rss->fd, data, write_now) == -1) {
221 break;
222 }
223
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700224 data += write_now;
225 size -= write_now;
226
227 rss->p_remain -= write_now;
228 written += write_now;
229
230 if (rss->p_remain == 0) {
231 // move to the next block
232 ++rss->p_block;
Tao Bao0940fe12015-08-27 16:41:21 -0700233 if (rss->p_block < rss->tgt.count) {
234 rss->p_remain = (rss->tgt.pos[rss->p_block * 2 + 1] -
235 rss->tgt.pos[rss->p_block * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000236
Tao Bao0940fe12015-08-27 16:41:21 -0700237 if (!check_lseek(rss->fd, (off64_t)rss->tgt.pos[rss->p_block*2] * BLOCKSIZE,
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700238 SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000239 break;
240 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700241 } else {
242 // we can't write any more; return how many bytes have
243 // been written so far.
Sami Tolvanen90221202014-12-09 16:39:47 +0000244 break;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700245 }
246 }
247 }
248
249 return written;
250}
251
252// All of the data for all the 'new' transfers is contained in one
253// file in the update package, concatenated together in the order in
254// which transfers.list will need it. We want to stream it out of the
255// archive (it's compressed) without writing it to a temp file, but we
256// can't write each section until it's that transfer's turn to go.
257//
258// To achieve this, we expand the new data from the archive in a
259// background thread, and block that threads 'receive uncompressed
260// data' function until the main thread has reached a point where we
261// want some new data to be written. We signal the background thread
262// with the destination for the data and block the main thread,
263// waiting for the background thread to complete writing that section.
264// Then it signals the main thread to wake up and goes back to
265// blocking waiting for a transfer.
266//
267// NewThreadInfo is the struct used to pass information back and forth
268// between the two threads. When the main thread wants some data
269// written, it sets rss to the destination location and signals the
270// condition. When the background thread is done writing, it clears
271// rss and signals the condition again.
272
Tao Bao0940fe12015-08-27 16:41:21 -0700273struct NewThreadInfo {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700274 ZipArchive* za;
275 const ZipEntry* entry;
276
277 RangeSinkState* rss;
278
279 pthread_mutex_t mu;
280 pthread_cond_t cv;
Tao Bao0940fe12015-08-27 16:41:21 -0700281};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700282
283static bool receive_new_data(const unsigned char* data, int size, void* cookie) {
Tao Bao0940fe12015-08-27 16:41:21 -0700284 NewThreadInfo* nti = reinterpret_cast<NewThreadInfo*>(cookie);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700285
286 while (size > 0) {
Tao Bao0940fe12015-08-27 16:41:21 -0700287 // Wait for nti->rss to be non-null, indicating some of this
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700288 // data is wanted.
289 pthread_mutex_lock(&nti->mu);
Tao Bao0940fe12015-08-27 16:41:21 -0700290 while (nti->rss == nullptr) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700291 pthread_cond_wait(&nti->cv, &nti->mu);
292 }
293 pthread_mutex_unlock(&nti->mu);
294
295 // At this point nti->rss is set, and we own it. The main
296 // thread is waiting for it to disappear from nti.
297 ssize_t written = RangeSinkWrite(data, size, nti->rss);
298 data += written;
299 size -= written;
300
Tao Bao0940fe12015-08-27 16:41:21 -0700301 if (nti->rss->p_block == nti->rss->tgt.count) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700302 // we have written all the bytes desired by this rss.
303
304 pthread_mutex_lock(&nti->mu);
Tao Bao0940fe12015-08-27 16:41:21 -0700305 nti->rss = nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700306 pthread_cond_broadcast(&nti->cv);
307 pthread_mutex_unlock(&nti->mu);
308 }
309 }
310
311 return true;
312}
313
314static void* unzip_new_data(void* cookie) {
315 NewThreadInfo* nti = (NewThreadInfo*) cookie;
316 mzProcessZipEntryContents(nti->za, nti->entry, receive_new_data, nti);
Tao Bao0940fe12015-08-27 16:41:21 -0700317 return nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700318}
319
Tao Bao612336d2015-08-27 16:41:21 -0700320static int ReadBlocks(const RangeSet& src, std::vector<uint8_t>& buffer, int fd) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000321 size_t p = 0;
Tao Bao612336d2015-08-27 16:41:21 -0700322 uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000323
Tao Bao0940fe12015-08-27 16:41:21 -0700324 for (size_t i = 0; i < src.count; ++i) {
325 if (!check_lseek(fd, (off64_t) src.pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000326 return -1;
327 }
328
Tao Bao0940fe12015-08-27 16:41:21 -0700329 size_t size = (src.pos[i * 2 + 1] - src.pos[i * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000330
Tao Bao612336d2015-08-27 16:41:21 -0700331 if (read_all(fd, data + p, size) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000332 return -1;
333 }
334
335 p += size;
336 }
337
338 return 0;
339}
340
Tao Bao612336d2015-08-27 16:41:21 -0700341static int WriteBlocks(const RangeSet& tgt, const std::vector<uint8_t>& buffer, int fd) {
342 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000343
Tao Bao0940fe12015-08-27 16:41:21 -0700344 size_t p = 0;
345 for (size_t i = 0; i < tgt.count; ++i) {
346 if (!check_lseek(fd, (off64_t) tgt.pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000347 return -1;
348 }
349
Tao Bao0940fe12015-08-27 16:41:21 -0700350 size_t size = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000351
Tao Bao612336d2015-08-27 16:41:21 -0700352 if (write_all(fd, data + p, size) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000353 return -1;
354 }
355
356 p += size;
357 }
358
359 return 0;
360}
361
Doug Zongker52ae67d2014-09-08 12:22:09 -0700362// Do a source/target load for move/bsdiff/imgdiff in version 1.
363// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
364// to parse the remainder of the string as:
365//
366// <src_range> <tgt_range>
367//
368// The source range is loaded into the provided buffer, reallocating
Tao Bao34847b22015-09-08 11:05:49 -0700369// it to make it larger if necessary.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700370
Tao Bao34847b22015-09-08 11:05:49 -0700371static int LoadSrcTgtVersion1(char** wordsave, RangeSet& tgt, size_t& src_blocks,
Tao Bao612336d2015-08-27 16:41:21 -0700372 std::vector<uint8_t>& buffer, int fd) {
373 // <src_range>
Tao Bao0940fe12015-08-27 16:41:21 -0700374 char* word = strtok_r(nullptr, " ", wordsave);
375 RangeSet src;
376 parse_range(word, src);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700377
Tao Bao612336d2015-08-27 16:41:21 -0700378 // <tgt_range>
Tao Bao34847b22015-09-08 11:05:49 -0700379 word = strtok_r(nullptr, " ", wordsave);
380 parse_range(word, tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700381
Tao Bao612336d2015-08-27 16:41:21 -0700382 allocate(src.size * BLOCKSIZE, buffer);
383 int rc = ReadBlocks(src, buffer, fd);
Tao Bao0940fe12015-08-27 16:41:21 -0700384 src_blocks = src.size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000385
Sami Tolvanen90221202014-12-09 16:39:47 +0000386 return rc;
387}
388
Tao Bao612336d2015-08-27 16:41:21 -0700389static int VerifyBlocks(const std::string& expected, const std::vector<uint8_t>& buffer,
Tao Bao0940fe12015-08-27 16:41:21 -0700390 const size_t blocks, bool printerror) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000391 uint8_t digest[SHA_DIGEST_SIZE];
Tao Bao612336d2015-08-27 16:41:21 -0700392 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000393
Tao Bao612336d2015-08-27 16:41:21 -0700394 SHA_hash(data, blocks * BLOCKSIZE, digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000395
Tao Baoe6aa3322015-08-05 15:20:27 -0700396 std::string hexdigest = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000397
Tao Bao0940fe12015-08-27 16:41:21 -0700398 if (hexdigest != expected) {
399 if (printerror) {
400 fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
401 expected.c_str(), hexdigest.c_str());
402 }
403 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000404 }
405
Tao Bao0940fe12015-08-27 16:41:21 -0700406 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000407}
408
Tao Bao0940fe12015-08-27 16:41:21 -0700409static std::string GetStashFileName(const std::string& base, const std::string& id,
410 const std::string& postfix) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700411 if (base.empty()) {
412 return "";
Sami Tolvanen90221202014-12-09 16:39:47 +0000413 }
414
Tao Baoe6aa3322015-08-05 15:20:27 -0700415 std::string fn(STASH_DIRECTORY_BASE);
416 fn += "/" + base + "/" + id + postfix;
Sami Tolvanen90221202014-12-09 16:39:47 +0000417
418 return fn;
419}
420
Tao Baoe6aa3322015-08-05 15:20:27 -0700421typedef void (*StashCallback)(const std::string&, void*);
Sami Tolvanen90221202014-12-09 16:39:47 +0000422
423// Does a best effort enumeration of stash files. Ignores possible non-file
424// items in the stash directory and continues despite of errors. Calls the
425// 'callback' function for each file and passes 'data' to the function as a
426// parameter.
427
Tao Baoe6aa3322015-08-05 15:20:27 -0700428static void EnumerateStash(const std::string& dirname, StashCallback callback, void* data) {
Tao Bao0940fe12015-08-27 16:41:21 -0700429 if (dirname.empty() || callback == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000430 return;
431 }
432
Tao Baoe6aa3322015-08-05 15:20:27 -0700433 std::unique_ptr<DIR, int(*)(DIR*)> directory(opendir(dirname.c_str()), closedir);
Sami Tolvanen90221202014-12-09 16:39:47 +0000434
Tao Bao0940fe12015-08-27 16:41:21 -0700435 if (directory == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000436 if (errno != ENOENT) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700437 fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000438 }
439 return;
440 }
441
Tao Baoe6aa3322015-08-05 15:20:27 -0700442 struct dirent* item;
Tao Bao0940fe12015-08-27 16:41:21 -0700443 while ((item = readdir(directory.get())) != nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000444 if (item->d_type != DT_REG) {
445 continue;
446 }
447
Tao Baoe6aa3322015-08-05 15:20:27 -0700448 std::string fn = dirname + "/" + std::string(item->d_name);
Sami Tolvanen90221202014-12-09 16:39:47 +0000449 callback(fn, data);
Sami Tolvanen90221202014-12-09 16:39:47 +0000450 }
451}
452
Tao Baoe6aa3322015-08-05 15:20:27 -0700453static void UpdateFileSize(const std::string& fn, void* data) {
454 if (fn.empty() || !data) {
455 return;
456 }
457
Tao Bao0940fe12015-08-27 16:41:21 -0700458 struct stat sb;
459 if (stat(fn.c_str(), &sb) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700460 fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000461 return;
462 }
463
Tao Baoe6aa3322015-08-05 15:20:27 -0700464 int* size = reinterpret_cast<int*>(data);
Tao Bao0940fe12015-08-27 16:41:21 -0700465 *size += sb.st_size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000466}
467
468// Deletes the stash directory and all files in it. Assumes that it only
469// contains files. There is nothing we can do about unlikely, but possible
470// errors, so they are merely logged.
471
Tao Bao0940fe12015-08-27 16:41:21 -0700472static void DeleteFile(const std::string& fn, void* /* data */) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700473 if (!fn.empty()) {
474 fprintf(stderr, "deleting %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000475
Tao Baoe6aa3322015-08-05 15:20:27 -0700476 if (unlink(fn.c_str()) == -1 && errno != ENOENT) {
477 fprintf(stderr, "unlink \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000478 }
479 }
480}
481
Tao Baoe6aa3322015-08-05 15:20:27 -0700482static void DeletePartial(const std::string& fn, void* data) {
483 if (android::base::EndsWith(fn, ".partial")) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000484 DeleteFile(fn, data);
485 }
486}
487
Tao Baoe6aa3322015-08-05 15:20:27 -0700488static void DeleteStash(const std::string& base) {
489 if (base.empty()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000490 return;
491 }
492
Tao Baoe6aa3322015-08-05 15:20:27 -0700493 fprintf(stderr, "deleting stash %s\n", base.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000494
Tao Baoe6aa3322015-08-05 15:20:27 -0700495 std::string dirname = GetStashFileName(base, "", "");
Tao Bao0940fe12015-08-27 16:41:21 -0700496 EnumerateStash(dirname, DeleteFile, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000497
Tao Baoe6aa3322015-08-05 15:20:27 -0700498 if (rmdir(dirname.c_str()) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000499 if (errno != ENOENT && errno != ENOTDIR) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700500 fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000501 }
502 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000503}
504
Tao Bao0940fe12015-08-27 16:41:21 -0700505static int LoadStash(const std::string& base, const std::string& id, bool verify, size_t* blocks,
Tao Bao612336d2015-08-27 16:41:21 -0700506 std::vector<uint8_t>& buffer, bool printnoent) {
507 if (base.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -0700508 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000509 }
510
Tao Bao0940fe12015-08-27 16:41:21 -0700511 size_t blockcount = 0;
512
Sami Tolvanen90221202014-12-09 16:39:47 +0000513 if (!blocks) {
514 blocks = &blockcount;
515 }
516
Tao Bao0940fe12015-08-27 16:41:21 -0700517 std::string fn = GetStashFileName(base, id, "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000518
Tao Bao0940fe12015-08-27 16:41:21 -0700519 struct stat sb;
520 int res = stat(fn.c_str(), &sb);
Sami Tolvanen90221202014-12-09 16:39:47 +0000521
522 if (res == -1) {
523 if (errno != ENOENT || printnoent) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700524 fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000525 }
Tao Bao0940fe12015-08-27 16:41:21 -0700526 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000527 }
528
Tao Baoe6aa3322015-08-05 15:20:27 -0700529 fprintf(stderr, " loading %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000530
Tao Bao0940fe12015-08-27 16:41:21 -0700531 if ((sb.st_size % BLOCKSIZE) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700532 fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d",
Tao Bao0940fe12015-08-27 16:41:21 -0700533 fn.c_str(), static_cast<int64_t>(sb.st_size), BLOCKSIZE);
534 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000535 }
536
Tao Bao0940fe12015-08-27 16:41:21 -0700537 int fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY));
538 unique_fd fd_holder(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000539
540 if (fd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700541 fprintf(stderr, "open \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700542 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000543 }
544
Tao Bao612336d2015-08-27 16:41:21 -0700545 allocate(sb.st_size, buffer);
Sami Tolvanen90221202014-12-09 16:39:47 +0000546
Tao Bao612336d2015-08-27 16:41:21 -0700547 if (read_all(fd, buffer, sb.st_size) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700548 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000549 }
550
Tao Bao0940fe12015-08-27 16:41:21 -0700551 *blocks = sb.st_size / BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000552
Tao Bao612336d2015-08-27 16:41:21 -0700553 if (verify && VerifyBlocks(id, buffer, *blocks, true) != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700554 fprintf(stderr, "unexpected contents in %s\n", fn.c_str());
Tao Bao0940fe12015-08-27 16:41:21 -0700555 DeleteFile(fn, nullptr);
556 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000557 }
558
Tao Bao0940fe12015-08-27 16:41:21 -0700559 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000560}
561
Tao Bao612336d2015-08-27 16:41:21 -0700562static int WriteStash(const std::string& base, const std::string& id, int blocks,
563 std::vector<uint8_t>& buffer, bool checkspace, bool *exists) {
564 if (base.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -0700565 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000566 }
567
568 if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) {
569 fprintf(stderr, "not enough space to write stash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700570 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000571 }
572
Tao Bao0940fe12015-08-27 16:41:21 -0700573 std::string fn = GetStashFileName(base, id, ".partial");
574 std::string cn = GetStashFileName(base, id, "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000575
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100576 if (exists) {
Tao Bao0940fe12015-08-27 16:41:21 -0700577 struct stat sb;
578 int res = stat(cn.c_str(), &sb);
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100579
580 if (res == 0) {
581 // The file already exists and since the name is the hash of the contents,
582 // it's safe to assume the contents are identical (accidental hash collisions
583 // are unlikely)
Tao Baoe6aa3322015-08-05 15:20:27 -0700584 fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn.c_str());
Tao Bao0940fe12015-08-27 16:41:21 -0700585 *exists = true;
586 return 0;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100587 }
588
Tao Bao0940fe12015-08-27 16:41:21 -0700589 *exists = false;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100590 }
591
Tao Baoe6aa3322015-08-05 15:20:27 -0700592 fprintf(stderr, " writing %d blocks to %s\n", blocks, cn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000593
Tao Bao0940fe12015-08-27 16:41:21 -0700594 int fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE));
595 unique_fd fd_holder(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000596
597 if (fd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700598 fprintf(stderr, "failed to create \"%s\": %s\n", fn.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700599 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000600 }
601
602 if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700603 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000604 }
605
606 if (fsync(fd) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700607 fprintf(stderr, "fsync \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700608 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000609 }
610
Tao Baoe6aa3322015-08-05 15:20:27 -0700611 if (rename(fn.c_str(), cn.c_str()) == -1) {
612 fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn.c_str(), cn.c_str(),
613 strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700614 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000615 }
616
Tao Bao0940fe12015-08-27 16:41:21 -0700617 std::string dname = GetStashFileName(base, "", "");
618 int dfd = TEMP_FAILURE_RETRY(open(dname.c_str(), O_RDONLY | O_DIRECTORY));
619 unique_fd dfd_holder(dfd);
Tao Baodc392262015-07-31 15:56:44 -0700620
621 if (dfd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700622 fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700623 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700624 }
625
626 if (fsync(dfd) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700627 fprintf(stderr, "fsync \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700628 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700629 }
630
Tao Bao0940fe12015-08-27 16:41:21 -0700631 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000632}
633
634// Creates a directory for storing stash files and checks if the /cache partition
635// hash enough space for the expected amount of blocks we need to store. Returns
636// >0 if we created the directory, zero if it existed already, and <0 of failure.
637
Tao Bao0940fe12015-08-27 16:41:21 -0700638static int CreateStash(State* state, int maxblocks, const char* blockdev, std::string& base) {
639 if (blockdev == nullptr) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700640 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000641 }
642
643 // Stash directory should be different for each partition to avoid conflicts
644 // when updating multiple partitions at the same time, so we use the hash of
645 // the block device name as the base directory
Tao Baoe6aa3322015-08-05 15:20:27 -0700646 SHA_CTX ctx;
Sami Tolvanen90221202014-12-09 16:39:47 +0000647 SHA_init(&ctx);
648 SHA_update(&ctx, blockdev, strlen(blockdev));
Tao Baoe6aa3322015-08-05 15:20:27 -0700649 const uint8_t* digest = SHA_final(&ctx);
650 base = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000651
Tao Baoe6aa3322015-08-05 15:20:27 -0700652 std::string dirname = GetStashFileName(base, "", "");
Tao Bao0940fe12015-08-27 16:41:21 -0700653 struct stat sb;
654 int res = stat(dirname.c_str(), &sb);
Sami Tolvanen90221202014-12-09 16:39:47 +0000655
656 if (res == -1 && errno != ENOENT) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700657 ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
658 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000659 } else if (res != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700660 fprintf(stderr, "creating stash %s\n", dirname.c_str());
661 res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000662
663 if (res != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700664 ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
665 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000666 }
667
668 if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) {
669 ErrorAbort(state, "not enough space for stash\n");
Tao Baoe6aa3322015-08-05 15:20:27 -0700670 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000671 }
672
Tao Baoe6aa3322015-08-05 15:20:27 -0700673 return 1; // Created directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000674 }
675
Tao Baoe6aa3322015-08-05 15:20:27 -0700676 fprintf(stderr, "using existing stash %s\n", dirname.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000677
678 // If the directory already exists, calculate the space already allocated to
679 // stash files and check if there's enough for all required blocks. Delete any
680 // partially completed stash files first.
681
Tao Bao0940fe12015-08-27 16:41:21 -0700682 EnumerateStash(dirname, DeletePartial, nullptr);
Tao Baoe6aa3322015-08-05 15:20:27 -0700683 int size = 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000684 EnumerateStash(dirname, UpdateFileSize, &size);
685
Tao Bao0940fe12015-08-27 16:41:21 -0700686 size = maxblocks * BLOCKSIZE - size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000687
688 if (size > 0 && CacheSizeCheck(size) != 0) {
689 ErrorAbort(state, "not enough space for stash (%d more needed)\n", size);
Tao Baoe6aa3322015-08-05 15:20:27 -0700690 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000691 }
692
Tao Baoe6aa3322015-08-05 15:20:27 -0700693 return 0; // Using existing directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000694}
695
Tao Bao612336d2015-08-27 16:41:21 -0700696static int SaveStash(const std::string& base, char** wordsave, std::vector<uint8_t>& buffer,
697 int fd, bool usehash) {
698 if (!wordsave) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000699 return -1;
700 }
701
Tao Bao0940fe12015-08-27 16:41:21 -0700702 char *id_tok = strtok_r(nullptr, " ", wordsave);
703 if (id_tok == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000704 fprintf(stderr, "missing id field in stash command\n");
705 return -1;
706 }
Tao Bao0940fe12015-08-27 16:41:21 -0700707 std::string id(id_tok);
Sami Tolvanen90221202014-12-09 16:39:47 +0000708
Tao Bao0940fe12015-08-27 16:41:21 -0700709 size_t blocks = 0;
Tao Bao612336d2015-08-27 16:41:21 -0700710 if (usehash && LoadStash(base, id, true, &blocks, buffer, false) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000711 // Stash file already exists and has expected contents. Do not
712 // read from source again, as the source may have been already
713 // overwritten during a previous attempt.
714 return 0;
715 }
716
Tao Bao34847b22015-09-08 11:05:49 -0700717 char* word = strtok_r(nullptr, " ", wordsave);
718 RangeSet src;
719 parse_range(word, src);
720
Tao Bao612336d2015-08-27 16:41:21 -0700721 allocate(src.size * BLOCKSIZE, buffer);
722 if (ReadBlocks(src, buffer, fd) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000723 return -1;
724 }
Tao Bao34847b22015-09-08 11:05:49 -0700725 blocks = src.size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000726
Tao Bao612336d2015-08-27 16:41:21 -0700727 if (usehash && VerifyBlocks(id, buffer, blocks, true) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000728 // Source blocks have unexpected contents. If we actually need this
729 // data later, this is an unrecoverable error. However, the command
730 // that uses the data may have already completed previously, so the
731 // possible failure will occur during source block verification.
Tao Bao0940fe12015-08-27 16:41:21 -0700732 fprintf(stderr, "failed to load source blocks for stash %s\n", id.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000733 return 0;
734 }
735
Tao Bao0940fe12015-08-27 16:41:21 -0700736 fprintf(stderr, "stashing %zu blocks to %s\n", blocks, id.c_str());
Tao Bao612336d2015-08-27 16:41:21 -0700737 return WriteStash(base, id, blocks, buffer, false, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000738}
739
Tao Baoe6aa3322015-08-05 15:20:27 -0700740static int FreeStash(const std::string& base, const char* id) {
Tao Bao0940fe12015-08-27 16:41:21 -0700741 if (base.empty() || id == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000742 return -1;
743 }
744
Tao Baoe6aa3322015-08-05 15:20:27 -0700745 std::string fn = GetStashFileName(base, std::string(id), "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000746
Tao Bao0940fe12015-08-27 16:41:21 -0700747 DeleteFile(fn, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000748
749 return 0;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700750}
751
Tao Bao612336d2015-08-27 16:41:21 -0700752static void MoveRange(std::vector<uint8_t>& dest, const RangeSet& locs,
753 const std::vector<uint8_t>& source) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700754 // source contains packed data, which we want to move to the
Tao Bao612336d2015-08-27 16:41:21 -0700755 // locations given in locs in the dest buffer. source and dest
Doug Zongker52ae67d2014-09-08 12:22:09 -0700756 // may be the same buffer.
757
Tao Bao612336d2015-08-27 16:41:21 -0700758 const uint8_t* from = source.data();
759 uint8_t* to = dest.data();
Tao Bao0940fe12015-08-27 16:41:21 -0700760 size_t start = locs.size;
761 for (int i = locs.count-1; i >= 0; --i) {
762 size_t blocks = locs.pos[i*2+1] - locs.pos[i*2];
Doug Zongker52ae67d2014-09-08 12:22:09 -0700763 start -= blocks;
Tao Bao612336d2015-08-27 16:41:21 -0700764 memmove(to + (locs.pos[i*2] * BLOCKSIZE), from + (start * BLOCKSIZE),
Doug Zongker52ae67d2014-09-08 12:22:09 -0700765 blocks * BLOCKSIZE);
766 }
767}
768
769// Do a source/target load for move/bsdiff/imgdiff in version 2.
770// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
771// to parse the remainder of the string as one of:
772//
773// <tgt_range> <src_block_count> <src_range>
774// (loads data from source image only)
775//
776// <tgt_range> <src_block_count> - <[stash_id:stash_range] ...>
777// (loads data from stashes only)
778//
779// <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...>
780// (loads data from both source image and stashes)
781//
782// On return, buffer is filled with the loaded source data (rearranged
783// and combined with stashed data as necessary). buffer may be
784// reallocated if needed to accommodate the source data. *tgt is the
Sami Tolvanen90221202014-12-09 16:39:47 +0000785// target RangeSet. Any stashes required are loaded using LoadStash.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700786
Tao Bao612336d2015-08-27 16:41:21 -0700787static int LoadSrcTgtVersion2(char** wordsave, RangeSet& tgt, size_t& src_blocks,
788 std::vector<uint8_t>& buffer, int fd, const std::string& stashbase, bool* overlap) {
789 // <tgt_range>
790 char* word = strtok_r(nullptr, " ", wordsave);
Tao Bao34847b22015-09-08 11:05:49 -0700791 parse_range(word, tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700792
Tao Bao612336d2015-08-27 16:41:21 -0700793 // <src_block_count>
Tao Bao0940fe12015-08-27 16:41:21 -0700794 word = strtok_r(nullptr, " ", wordsave);
Tao Baob15fd222015-09-24 11:10:51 -0700795 android::base::ParseUint(word, &src_blocks);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700796
Tao Bao612336d2015-08-27 16:41:21 -0700797 allocate(src_blocks * BLOCKSIZE, buffer);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700798
Tao Bao612336d2015-08-27 16:41:21 -0700799 // "-" or <src_range> [<src_loc>]
Tao Bao0940fe12015-08-27 16:41:21 -0700800 word = strtok_r(nullptr, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700801 if (word[0] == '-' && word[1] == '\0') {
802 // no source ranges, only stashes
803 } else {
Tao Bao0940fe12015-08-27 16:41:21 -0700804 RangeSet src;
805 parse_range(word, src);
Tao Bao612336d2015-08-27 16:41:21 -0700806 int res = ReadBlocks(src, buffer, fd);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700807
Tao Bao34847b22015-09-08 11:05:49 -0700808 if (overlap) {
809 *overlap = range_overlaps(src, tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700810 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000811
Sami Tolvanen90221202014-12-09 16:39:47 +0000812 if (res == -1) {
813 return -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700814 }
815
Tao Bao0940fe12015-08-27 16:41:21 -0700816 word = strtok_r(nullptr, " ", wordsave);
817 if (word == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000818 // no stashes, only source range
819 return 0;
820 }
821
Tao Bao612336d2015-08-27 16:41:21 -0700822 RangeSet locs;
Tao Bao0940fe12015-08-27 16:41:21 -0700823 parse_range(word, locs);
Tao Bao612336d2015-08-27 16:41:21 -0700824 MoveRange(buffer, locs, buffer);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700825 }
826
Tao Bao612336d2015-08-27 16:41:21 -0700827 // <[stash_id:stash-range]>
828 char* colonsave;
Tao Bao0940fe12015-08-27 16:41:21 -0700829 while ((word = strtok_r(nullptr, " ", wordsave)) != nullptr) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700830 // Each word is a an index into the stash table, a colon, and
831 // then a rangeset describing where in the source block that
832 // stashed data should go.
Tao Bao0940fe12015-08-27 16:41:21 -0700833 colonsave = nullptr;
Tao Bao612336d2015-08-27 16:41:21 -0700834 char* colon = strtok_r(word, ":", &colonsave);
Sami Tolvanen90221202014-12-09 16:39:47 +0000835
Tao Bao612336d2015-08-27 16:41:21 -0700836 std::vector<uint8_t> stash;
837 int res = LoadStash(stashbase, std::string(colon), false, nullptr, stash, true);
Sami Tolvanen90221202014-12-09 16:39:47 +0000838
839 if (res == -1) {
840 // These source blocks will fail verification if used later, but we
841 // will let the caller decide if this is a fatal failure
842 fprintf(stderr, "failed to load stash %s\n", colon);
843 continue;
844 }
845
Tao Bao0940fe12015-08-27 16:41:21 -0700846 colon = strtok_r(nullptr, ":", &colonsave);
Tao Bao612336d2015-08-27 16:41:21 -0700847 RangeSet locs;
Tao Bao0940fe12015-08-27 16:41:21 -0700848 parse_range(colon, locs);
Sami Tolvanen90221202014-12-09 16:39:47 +0000849
Tao Bao612336d2015-08-27 16:41:21 -0700850 MoveRange(buffer, locs, stash);
Sami Tolvanen90221202014-12-09 16:39:47 +0000851 }
852
853 return 0;
854}
855
856// Parameters for transfer list command functions
Tao Bao0940fe12015-08-27 16:41:21 -0700857struct CommandParameters {
Sami Tolvanen90221202014-12-09 16:39:47 +0000858 char* cmdname;
859 char* cpos;
860 char* freestash;
Tao Baoe6aa3322015-08-05 15:20:27 -0700861 std::string stashbase;
862 bool canwrite;
Sami Tolvanen90221202014-12-09 16:39:47 +0000863 int createdstash;
864 int fd;
Tao Bao0940fe12015-08-27 16:41:21 -0700865 bool foundwrites;
Tao Baoe6aa3322015-08-05 15:20:27 -0700866 bool isunresumable;
Sami Tolvanen90221202014-12-09 16:39:47 +0000867 int version;
Tao Bao0940fe12015-08-27 16:41:21 -0700868 size_t written;
Sami Tolvanen90221202014-12-09 16:39:47 +0000869 NewThreadInfo nti;
870 pthread_t thread;
Tao Bao612336d2015-08-27 16:41:21 -0700871 std::vector<uint8_t> buffer;
Sami Tolvanen90221202014-12-09 16:39:47 +0000872 uint8_t* patch_start;
Tao Bao0940fe12015-08-27 16:41:21 -0700873};
Sami Tolvanen90221202014-12-09 16:39:47 +0000874
875// Do a source/target load for move/bsdiff/imgdiff in version 3.
876//
877// Parameters are the same as for LoadSrcTgtVersion2, except for 'onehash', which
878// tells the function whether to expect separate source and targe block hashes, or
879// if they are both the same and only one hash should be expected, and
880// 'isunresumable', which receives a non-zero value if block verification fails in
881// a way that the update cannot be resumed anymore.
882//
883// If the function is unable to load the necessary blocks or their contents don't
884// match the hashes, the return value is -1 and the command should be aborted.
885//
886// If the return value is 1, the command has already been completed according to
887// the contents of the target blocks, and should not be performed again.
888//
889// If the return value is 0, source blocks have expected content and the command
890// can be performed.
891
Tao Bao34847b22015-09-08 11:05:49 -0700892static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t& src_blocks,
Tao Bao0940fe12015-08-27 16:41:21 -0700893 bool onehash, bool& overlap) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000894
Tao Bao0940fe12015-08-27 16:41:21 -0700895 char* srchash = strtok_r(nullptr, " ", &params.cpos);
896 if (srchash == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000897 fprintf(stderr, "missing source hash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700898 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000899 }
900
Tao Bao0940fe12015-08-27 16:41:21 -0700901 char* tgthash = nullptr;
Sami Tolvanen90221202014-12-09 16:39:47 +0000902 if (onehash) {
903 tgthash = srchash;
904 } else {
Tao Bao0940fe12015-08-27 16:41:21 -0700905 tgthash = strtok_r(nullptr, " ", &params.cpos);
Sami Tolvanen90221202014-12-09 16:39:47 +0000906
Tao Bao0940fe12015-08-27 16:41:21 -0700907 if (tgthash == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000908 fprintf(stderr, "missing target hash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700909 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000910 }
911 }
912
Tao Bao612336d2015-08-27 16:41:21 -0700913 if (LoadSrcTgtVersion2(&params.cpos, tgt, src_blocks, params.buffer, params.fd,
914 params.stashbase, &overlap) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700915 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000916 }
917
Tao Bao34847b22015-09-08 11:05:49 -0700918 std::vector<uint8_t> tgtbuffer(tgt.size * BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000919
Tao Bao612336d2015-08-27 16:41:21 -0700920 if (ReadBlocks(tgt, tgtbuffer, params.fd) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700921 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000922 }
923
Tao Bao612336d2015-08-27 16:41:21 -0700924 if (VerifyBlocks(tgthash, tgtbuffer, tgt.size, false) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000925 // Target blocks already have expected content, command should be skipped
Tao Bao0940fe12015-08-27 16:41:21 -0700926 return 1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000927 }
928
Tao Bao0940fe12015-08-27 16:41:21 -0700929 if (VerifyBlocks(srchash, params.buffer, src_blocks, true) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000930 // If source and target blocks overlap, stash the source blocks so we can
931 // resume from possible write errors
Tao Bao0940fe12015-08-27 16:41:21 -0700932 if (overlap) {
933 fprintf(stderr, "stashing %zu overlapping blocks to %s\n", src_blocks, srchash);
Sami Tolvanen90221202014-12-09 16:39:47 +0000934
Tao Bao0940fe12015-08-27 16:41:21 -0700935 bool stash_exists = false;
Tao Bao612336d2015-08-27 16:41:21 -0700936 if (WriteStash(params.stashbase, srchash, src_blocks, params.buffer, true,
Tao Bao0940fe12015-08-27 16:41:21 -0700937 &stash_exists) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000938 fprintf(stderr, "failed to stash overlapping source blocks\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700939 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000940 }
941
942 // Can be deleted when the write has completed
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100943 if (!stash_exists) {
Tao Bao0940fe12015-08-27 16:41:21 -0700944 params.freestash = srchash;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100945 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000946 }
947
948 // Source blocks have expected content, command can proceed
Tao Bao0940fe12015-08-27 16:41:21 -0700949 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000950 }
951
Tao Bao612336d2015-08-27 16:41:21 -0700952 if (overlap && LoadStash(params.stashbase, srchash, true, nullptr, params.buffer, true) == 0) {
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100953 // Overlapping source blocks were previously stashed, command can proceed.
954 // We are recovering from an interrupted command, so we don't know if the
955 // stash can safely be deleted after this command.
Tao Bao0940fe12015-08-27 16:41:21 -0700956 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000957 }
958
959 // Valid source data not available, update cannot be resumed
960 fprintf(stderr, "partition has unexpected contents\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700961 params.isunresumable = true;
Sami Tolvanen90221202014-12-09 16:39:47 +0000962
Tao Bao0940fe12015-08-27 16:41:21 -0700963 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000964}
965
Tao Bao0940fe12015-08-27 16:41:21 -0700966static int PerformCommandMove(CommandParameters& params) {
967 size_t blocks = 0;
Tao Baoe6aa3322015-08-05 15:20:27 -0700968 bool overlap = false;
Sami Tolvanen90221202014-12-09 16:39:47 +0000969 int status = 0;
Tao Bao0940fe12015-08-27 16:41:21 -0700970 RangeSet tgt;
Sami Tolvanen90221202014-12-09 16:39:47 +0000971
Tao Bao0940fe12015-08-27 16:41:21 -0700972 if (params.version == 1) {
Tao Bao612336d2015-08-27 16:41:21 -0700973 status = LoadSrcTgtVersion1(&params.cpos, tgt, blocks, params.buffer, params.fd);
Tao Bao0940fe12015-08-27 16:41:21 -0700974 } else if (params.version == 2) {
Tao Bao612336d2015-08-27 16:41:21 -0700975 status = LoadSrcTgtVersion2(&params.cpos, tgt, blocks, params.buffer, params.fd,
976 params.stashbase, nullptr);
Tao Bao0940fe12015-08-27 16:41:21 -0700977 } else if (params.version >= 3) {
Tao Bao34847b22015-09-08 11:05:49 -0700978 status = LoadSrcTgtVersion3(params, tgt, blocks, true, overlap);
Sami Tolvanen90221202014-12-09 16:39:47 +0000979 }
980
981 if (status == -1) {
982 fprintf(stderr, "failed to read blocks for move\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700983 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000984 }
985
986 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -0700987 params.foundwrites = true;
988 } else if (params.foundwrites) {
989 fprintf(stderr, "warning: commands executed out of order [%s]\n", params.cmdname);
Sami Tolvanen90221202014-12-09 16:39:47 +0000990 }
991
Tao Bao0940fe12015-08-27 16:41:21 -0700992 if (params.canwrite) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000993 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -0700994 fprintf(stderr, " moving %zu blocks\n", blocks);
Sami Tolvanen90221202014-12-09 16:39:47 +0000995
Tao Bao0940fe12015-08-27 16:41:21 -0700996 if (WriteBlocks(tgt, params.buffer, params.fd) == -1) {
997 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000998 }
999 } else {
Tao Bao0940fe12015-08-27 16:41:21 -07001000 fprintf(stderr, "skipping %zu already moved blocks\n", blocks);
Sami Tolvanen90221202014-12-09 16:39:47 +00001001 }
1002
1003 }
1004
Tao Bao0940fe12015-08-27 16:41:21 -07001005 if (params.freestash) {
1006 FreeStash(params.stashbase, params.freestash);
1007 params.freestash = nullptr;
Sami Tolvanen90221202014-12-09 16:39:47 +00001008 }
1009
Tao Bao0940fe12015-08-27 16:41:21 -07001010 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001011
Tao Bao0940fe12015-08-27 16:41:21 -07001012 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001013}
1014
Tao Bao0940fe12015-08-27 16:41:21 -07001015static int PerformCommandStash(CommandParameters& params) {
Tao Bao612336d2015-08-27 16:41:21 -07001016 return SaveStash(params.stashbase, &params.cpos, params.buffer, params.fd,
1017 (params.version >= 3));
Sami Tolvanen90221202014-12-09 16:39:47 +00001018}
1019
Tao Bao0940fe12015-08-27 16:41:21 -07001020static int PerformCommandFree(CommandParameters& params) {
1021 if (params.createdstash || params.canwrite) {
1022 return FreeStash(params.stashbase, params.cpos);
Sami Tolvanen90221202014-12-09 16:39:47 +00001023 }
1024
1025 return 0;
1026}
1027
Tao Bao0940fe12015-08-27 16:41:21 -07001028static int PerformCommandZero(CommandParameters& params) {
1029 char* range = strtok_r(nullptr, " ", &params.cpos);
Sami Tolvanen90221202014-12-09 16:39:47 +00001030
Tao Bao0940fe12015-08-27 16:41:21 -07001031 if (range == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001032 fprintf(stderr, "missing target blocks for zero\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001033 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001034 }
1035
Tao Bao0940fe12015-08-27 16:41:21 -07001036 RangeSet tgt;
1037 parse_range(range, tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001038
Tao Bao0940fe12015-08-27 16:41:21 -07001039 fprintf(stderr, " zeroing %zu blocks\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001040
Tao Bao612336d2015-08-27 16:41:21 -07001041 allocate(BLOCKSIZE, params.buffer);
1042 memset(params.buffer.data(), 0, BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +00001043
Tao Bao0940fe12015-08-27 16:41:21 -07001044 if (params.canwrite) {
1045 for (size_t i = 0; i < tgt.count; ++i) {
1046 if (!check_lseek(params.fd, (off64_t) tgt.pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
1047 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001048 }
1049
Tao Bao0940fe12015-08-27 16:41:21 -07001050 for (size_t j = tgt.pos[i * 2]; j < tgt.pos[i * 2 + 1]; ++j) {
1051 if (write_all(params.fd, params.buffer, BLOCKSIZE) == -1) {
1052 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001053 }
1054 }
1055 }
1056 }
1057
Tao Bao0940fe12015-08-27 16:41:21 -07001058 if (params.cmdname[0] == 'z') {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001059 // Update only for the zero command, as the erase command will call
1060 // this if DEBUG_ERASE is defined.
Tao Bao0940fe12015-08-27 16:41:21 -07001061 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001062 }
1063
Tao Bao0940fe12015-08-27 16:41:21 -07001064 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001065}
1066
Tao Bao0940fe12015-08-27 16:41:21 -07001067static int PerformCommandNew(CommandParameters& params) {
1068 char* range = strtok_r(nullptr, " ", &params.cpos);
Sami Tolvanen90221202014-12-09 16:39:47 +00001069
Tao Bao0940fe12015-08-27 16:41:21 -07001070 if (range == nullptr) {
1071 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001072 }
1073
Tao Bao0940fe12015-08-27 16:41:21 -07001074 RangeSet tgt;
1075 parse_range(range, tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001076
Tao Bao0940fe12015-08-27 16:41:21 -07001077 if (params.canwrite) {
1078 fprintf(stderr, " writing %zu blocks of new data\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001079
Tao Bao0940fe12015-08-27 16:41:21 -07001080 RangeSinkState rss(tgt);
1081 rss.fd = params.fd;
Sami Tolvanen90221202014-12-09 16:39:47 +00001082 rss.p_block = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001083 rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001084
Tao Bao0940fe12015-08-27 16:41:21 -07001085 if (!check_lseek(params.fd, (off64_t) tgt.pos[0] * BLOCKSIZE, SEEK_SET)) {
1086 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001087 }
1088
Tao Bao0940fe12015-08-27 16:41:21 -07001089 pthread_mutex_lock(&params.nti.mu);
1090 params.nti.rss = &rss;
1091 pthread_cond_broadcast(&params.nti.cv);
Sami Tolvanen90221202014-12-09 16:39:47 +00001092
Tao Bao0940fe12015-08-27 16:41:21 -07001093 while (params.nti.rss) {
1094 pthread_cond_wait(&params.nti.cv, &params.nti.mu);
Sami Tolvanen90221202014-12-09 16:39:47 +00001095 }
1096
Tao Bao0940fe12015-08-27 16:41:21 -07001097 pthread_mutex_unlock(&params.nti.mu);
Sami Tolvanen90221202014-12-09 16:39:47 +00001098 }
1099
Tao Bao0940fe12015-08-27 16:41:21 -07001100 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001101
Tao Bao0940fe12015-08-27 16:41:21 -07001102 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001103}
1104
Tao Bao0940fe12015-08-27 16:41:21 -07001105static int PerformCommandDiff(CommandParameters& params) {
Tao Bao0940fe12015-08-27 16:41:21 -07001106
1107 const std::string logparams(params.cpos);
Tao Bao612336d2015-08-27 16:41:21 -07001108 char* value = strtok_r(nullptr, " ", &params.cpos);
Tao Bao0940fe12015-08-27 16:41:21 -07001109
1110 if (value == nullptr) {
1111 fprintf(stderr, "missing patch offset for %s\n", params.cmdname);
1112 return -1;
1113 }
1114
1115 size_t offset = strtoul(value, nullptr, 0);
1116
1117 value = strtok_r(nullptr, " ", &params.cpos);
1118
1119 if (value == nullptr) {
1120 fprintf(stderr, "missing patch length for %s\n", params.cmdname);
1121 return -1;
1122 }
1123
1124 size_t len = strtoul(value, nullptr, 0);
1125
Tao Bao612336d2015-08-27 16:41:21 -07001126 RangeSet tgt;
Tao Bao0940fe12015-08-27 16:41:21 -07001127 size_t blocks = 0;
Tao Bao612336d2015-08-27 16:41:21 -07001128 bool overlap = false;
Sami Tolvanen90221202014-12-09 16:39:47 +00001129 int status = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001130 if (params.version == 1) {
Tao Bao612336d2015-08-27 16:41:21 -07001131 status = LoadSrcTgtVersion1(&params.cpos, tgt, blocks, params.buffer, params.fd);
Tao Bao0940fe12015-08-27 16:41:21 -07001132 } else if (params.version == 2) {
Tao Bao612336d2015-08-27 16:41:21 -07001133 status = LoadSrcTgtVersion2(&params.cpos, tgt, blocks, params.buffer, params.fd,
1134 params.stashbase, nullptr);
Tao Bao0940fe12015-08-27 16:41:21 -07001135 } else if (params.version >= 3) {
Tao Bao34847b22015-09-08 11:05:49 -07001136 status = LoadSrcTgtVersion3(params, tgt, blocks, false, overlap);
Sami Tolvanen90221202014-12-09 16:39:47 +00001137 }
1138
1139 if (status == -1) {
1140 fprintf(stderr, "failed to read blocks for diff\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001141 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001142 }
1143
1144 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001145 params.foundwrites = true;
1146 } else if (params.foundwrites) {
1147 fprintf(stderr, "warning: commands executed out of order [%s]\n", params.cmdname);
Sami Tolvanen90221202014-12-09 16:39:47 +00001148 }
1149
Tao Bao0940fe12015-08-27 16:41:21 -07001150 if (params.canwrite) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001151 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001152 fprintf(stderr, "patching %zu blocks to %zu\n", blocks, tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001153
Tao Bao0940fe12015-08-27 16:41:21 -07001154 Value patch_value;
Sami Tolvanen90221202014-12-09 16:39:47 +00001155 patch_value.type = VAL_BLOB;
1156 patch_value.size = len;
Tao Bao0940fe12015-08-27 16:41:21 -07001157 patch_value.data = (char*) (params.patch_start + offset);
Sami Tolvanen90221202014-12-09 16:39:47 +00001158
Tao Bao0940fe12015-08-27 16:41:21 -07001159 RangeSinkState rss(tgt);
1160 rss.fd = params.fd;
Sami Tolvanen90221202014-12-09 16:39:47 +00001161 rss.p_block = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001162 rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001163
Tao Bao0940fe12015-08-27 16:41:21 -07001164 if (!check_lseek(params.fd, (off64_t) tgt.pos[0] * BLOCKSIZE, SEEK_SET)) {
1165 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001166 }
1167
Tao Bao0940fe12015-08-27 16:41:21 -07001168 if (params.cmdname[0] == 'i') { // imgdiff
Tao Bao612336d2015-08-27 16:41:21 -07001169 ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value,
Tao Bao0940fe12015-08-27 16:41:21 -07001170 &RangeSinkWrite, &rss, nullptr, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +00001171 } else {
Tao Bao612336d2015-08-27 16:41:21 -07001172 ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value, 0,
1173 &RangeSinkWrite, &rss, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +00001174 }
1175
1176 // We expect the output of the patcher to fill the tgt ranges exactly.
Tao Bao0940fe12015-08-27 16:41:21 -07001177 if (rss.p_block != tgt.count || rss.p_remain != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001178 fprintf(stderr, "range sink underrun?\n");
1179 }
1180 } else {
Tao Bao0940fe12015-08-27 16:41:21 -07001181 fprintf(stderr, "skipping %zu blocks already patched to %zu [%s]\n",
1182 blocks, tgt.size, logparams.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +00001183 }
1184 }
1185
Tao Bao0940fe12015-08-27 16:41:21 -07001186 if (params.freestash) {
1187 FreeStash(params.stashbase, params.freestash);
1188 params.freestash = nullptr;
Sami Tolvanen90221202014-12-09 16:39:47 +00001189 }
1190
Tao Bao0940fe12015-08-27 16:41:21 -07001191 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001192
Tao Bao0940fe12015-08-27 16:41:21 -07001193 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001194}
1195
Tao Bao0940fe12015-08-27 16:41:21 -07001196static int PerformCommandErase(CommandParameters& params) {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001197 if (DEBUG_ERASE) {
1198 return PerformCommandZero(params);
Sami Tolvanen90221202014-12-09 16:39:47 +00001199 }
1200
Tao Bao0940fe12015-08-27 16:41:21 -07001201 struct stat sb;
1202 if (fstat(params.fd, &sb) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001203 fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -07001204 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001205 }
1206
Tao Bao0940fe12015-08-27 16:41:21 -07001207 if (!S_ISBLK(sb.st_mode)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001208 fprintf(stderr, "not a block device; skipping erase\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001209 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001210 }
1211
Tao Bao0940fe12015-08-27 16:41:21 -07001212 char* range = strtok_r(nullptr, " ", &params.cpos);
Sami Tolvanen90221202014-12-09 16:39:47 +00001213
Tao Bao0940fe12015-08-27 16:41:21 -07001214 if (range == nullptr) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001215 fprintf(stderr, "missing target blocks for erase\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001216 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001217 }
1218
Tao Bao0940fe12015-08-27 16:41:21 -07001219 RangeSet tgt;
1220 parse_range(range, tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001221
Tao Bao0940fe12015-08-27 16:41:21 -07001222 if (params.canwrite) {
1223 fprintf(stderr, " erasing %zu blocks\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001224
Tao Bao0940fe12015-08-27 16:41:21 -07001225 for (size_t i = 0; i < tgt.count; ++i) {
1226 uint64_t blocks[2];
Sami Tolvanen90221202014-12-09 16:39:47 +00001227 // offset in bytes
Tao Bao0940fe12015-08-27 16:41:21 -07001228 blocks[0] = tgt.pos[i * 2] * (uint64_t) BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001229 // length in bytes
Tao Bao0940fe12015-08-27 16:41:21 -07001230 blocks[1] = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * (uint64_t) BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001231
Tao Bao0940fe12015-08-27 16:41:21 -07001232 if (ioctl(params.fd, BLKDISCARD, &blocks) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001233 fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -07001234 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001235 }
1236 }
1237 }
1238
Tao Bao0940fe12015-08-27 16:41:21 -07001239 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001240}
1241
1242// Definitions for transfer list command functions
Tao Bao0940fe12015-08-27 16:41:21 -07001243typedef int (*CommandFunction)(CommandParameters&);
Sami Tolvanen90221202014-12-09 16:39:47 +00001244
Tao Bao612336d2015-08-27 16:41:21 -07001245struct Command {
Sami Tolvanen90221202014-12-09 16:39:47 +00001246 const char* name;
1247 CommandFunction f;
Tao Bao612336d2015-08-27 16:41:21 -07001248};
Sami Tolvanen90221202014-12-09 16:39:47 +00001249
1250// CompareCommands and CompareCommandNames are for the hash table
1251
1252static int CompareCommands(const void* c1, const void* c2) {
1253 return strcmp(((const Command*) c1)->name, ((const Command*) c2)->name);
1254}
1255
1256static int CompareCommandNames(const void* c1, const void* c2) {
1257 return strcmp(((const Command*) c1)->name, (const char*) c2);
1258}
1259
1260// HashString is used to hash command names for the hash table
1261
1262static unsigned int HashString(const char *s) {
1263 unsigned int hash = 0;
1264 if (s) {
1265 while (*s) {
1266 hash = hash * 33 + *s++;
1267 }
1268 }
1269 return hash;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001270}
1271
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001272// args:
1273// - block device (or file) to modify in-place
1274// - transfer list (blob)
1275// - new data stream (filename within package.zip)
1276// - patch stream (filename within package.zip, must be uncompressed)
1277
Tao Bao0940fe12015-08-27 16:41:21 -07001278static Value* PerformBlockImageUpdate(const char* name, State* state, int /* argc */, Expr* argv[],
1279 const Command* commands, size_t cmdcount, bool dryrun) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001280
Sami Tolvanen90221202014-12-09 16:39:47 +00001281 CommandParameters params;
Sami Tolvanen90221202014-12-09 16:39:47 +00001282 memset(&params, 0, sizeof(params));
1283 params.canwrite = !dryrun;
1284
1285 fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001286
Tao Bao612336d2015-08-27 16:41:21 -07001287 Value* blockdev_filename = nullptr;
1288 Value* transfer_list_value = nullptr;
1289 Value* new_data_fn = nullptr;
1290 Value* patch_data_fn = nullptr;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001291 if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value,
Sami Tolvanen90221202014-12-09 16:39:47 +00001292 &new_data_fn, &patch_data_fn) < 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001293 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001294 }
Tao Bao612336d2015-08-27 16:41:21 -07001295 std::unique_ptr<Value, decltype(&FreeValue)> blockdev_filename_holder(blockdev_filename,
1296 FreeValue);
1297 std::unique_ptr<Value, decltype(&FreeValue)> transfer_list_value_holder(transfer_list_value,
1298 FreeValue);
1299 std::unique_ptr<Value, decltype(&FreeValue)> new_data_fn_holder(new_data_fn, FreeValue);
1300 std::unique_ptr<Value, decltype(&FreeValue)> patch_data_fn_holder(patch_data_fn, FreeValue);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001301
1302 if (blockdev_filename->type != VAL_STRING) {
1303 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001304 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001305 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001306 if (transfer_list_value->type != VAL_BLOB) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001307 ErrorAbort(state, "transfer_list argument to %s must be blob", name);
Tao Bao612336d2015-08-27 16:41:21 -07001308 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001309 }
1310 if (new_data_fn->type != VAL_STRING) {
1311 ErrorAbort(state, "new_data_fn argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001312 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001313 }
1314 if (patch_data_fn->type != VAL_STRING) {
1315 ErrorAbort(state, "patch_data_fn argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001316 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001317 }
1318
Tao Bao612336d2015-08-27 16:41:21 -07001319 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001320
Tao Bao0940fe12015-08-27 16:41:21 -07001321 if (ui == nullptr) {
Tao Bao612336d2015-08-27 16:41:21 -07001322 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001323 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001324
Tao Bao612336d2015-08-27 16:41:21 -07001325 FILE* cmd_pipe = ui->cmd_pipe;
1326 ZipArchive* za = ui->package_zip;
Sami Tolvanen90221202014-12-09 16:39:47 +00001327
Tao Bao0940fe12015-08-27 16:41:21 -07001328 if (cmd_pipe == nullptr || za == nullptr) {
Tao Bao612336d2015-08-27 16:41:21 -07001329 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001330 }
1331
Tao Bao612336d2015-08-27 16:41:21 -07001332 const ZipEntry* patch_entry = mzFindZipEntry(za, patch_data_fn->data);
Tao Bao0940fe12015-08-27 16:41:21 -07001333 if (patch_entry == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001334 fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data);
Tao Bao612336d2015-08-27 16:41:21 -07001335 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001336 }
1337
Sami Tolvanen90221202014-12-09 16:39:47 +00001338 params.patch_start = ui->package_zip_addr + mzGetZipEntryOffset(patch_entry);
Tao Bao612336d2015-08-27 16:41:21 -07001339 const ZipEntry* new_entry = mzFindZipEntry(za, new_data_fn->data);
Tao Bao0940fe12015-08-27 16:41:21 -07001340 if (new_entry == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001341 fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data);
Tao Bao612336d2015-08-27 16:41:21 -07001342 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001343 }
1344
Sami Tolvanen90221202014-12-09 16:39:47 +00001345 params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR));
Tao Bao612336d2015-08-27 16:41:21 -07001346 unique_fd fd_holder(params.fd);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001347
Sami Tolvanen90221202014-12-09 16:39:47 +00001348 if (params.fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001349 fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data, strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001350 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001351 }
1352
Sami Tolvanen90221202014-12-09 16:39:47 +00001353 if (params.canwrite) {
1354 params.nti.za = za;
1355 params.nti.entry = new_entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001356
Tao Bao0940fe12015-08-27 16:41:21 -07001357 pthread_mutex_init(&params.nti.mu, nullptr);
1358 pthread_cond_init(&params.nti.cv, nullptr);
Tao Bao612336d2015-08-27 16:41:21 -07001359 pthread_attr_t attr;
Sami Tolvanen90221202014-12-09 16:39:47 +00001360 pthread_attr_init(&attr);
1361 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1362
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001363 int error = pthread_create(&params.thread, &attr, unzip_new_data, &params.nti);
1364 if (error != 0) {
1365 fprintf(stderr, "pthread_create failed: %s\n", strerror(error));
Tao Bao612336d2015-08-27 16:41:21 -07001366 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001367 }
1368 }
1369
1370 // The data in transfer_list_value is not necessarily null-terminated, so we need
1371 // to copy it to a new buffer and add the null that strtok_r will need.
Tao Bao612336d2015-08-27 16:41:21 -07001372 const std::string transfer_list(transfer_list_value->data, transfer_list_value->size);
1373 std::vector<std::string> lines = android::base::Split(transfer_list, "\n");
Doug Zongker1d5d6092014-08-21 10:47:24 -07001374
Sami Tolvanen90221202014-12-09 16:39:47 +00001375 // First line in transfer list is the version number
Tao Baob15fd222015-09-24 11:10:51 -07001376 if (!android::base::ParseInt(lines[0].c_str(), &params.version, 1, 3)) {
Tao Bao612336d2015-08-27 16:41:21 -07001377 fprintf(stderr, "unexpected transfer list version [%s]\n", lines[0].c_str());
1378 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001379 }
1380
Sami Tolvanen90221202014-12-09 16:39:47 +00001381 fprintf(stderr, "blockimg version is %d\n", params.version);
1382
1383 // Second line in transfer list is the total number of blocks we expect to write
Tao Baob15fd222015-09-24 11:10:51 -07001384 int total_blocks;
1385 if (!android::base::ParseInt(lines[1].c_str(), &total_blocks, 0)) {
Tao Bao612336d2015-08-27 16:41:21 -07001386 ErrorAbort(state, "unexpected block count [%s]\n", lines[1].c_str());
1387 return StringValue(strdup(""));
Tao Baob15fd222015-09-24 11:10:51 -07001388 }
1389
1390 if (total_blocks == 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001391 return StringValue(strdup("t"));
Sami Tolvanen90221202014-12-09 16:39:47 +00001392 }
1393
Tao Bao612336d2015-08-27 16:41:21 -07001394 size_t start = 2;
Sami Tolvanen90221202014-12-09 16:39:47 +00001395 if (params.version >= 2) {
1396 // Third line is how many stash entries are needed simultaneously
Tao Bao612336d2015-08-27 16:41:21 -07001397 fprintf(stderr, "maximum stash entries %s\n", lines[2].c_str());
Doug Zongker52ae67d2014-09-08 12:22:09 -07001398
Sami Tolvanen90221202014-12-09 16:39:47 +00001399 // Fourth line is the maximum number of blocks that will be stashed simultaneously
Tao Baob15fd222015-09-24 11:10:51 -07001400 int stash_max_blocks;
1401 if (!android::base::ParseInt(lines[3].c_str(), &stash_max_blocks, 0)) {
Tao Bao612336d2015-08-27 16:41:21 -07001402 ErrorAbort(state, "unexpected maximum stash blocks [%s]\n", lines[3].c_str());
1403 return StringValue(strdup(""));
Doug Zongker52ae67d2014-09-08 12:22:09 -07001404 }
1405
Tao Baob15fd222015-09-24 11:10:51 -07001406 int res = CreateStash(state, stash_max_blocks, blockdev_filename->data, params.stashbase);
Sami Tolvanen90221202014-12-09 16:39:47 +00001407
Tao Baob15fd222015-09-24 11:10:51 -07001408 if (res == -1) {
1409 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001410 }
Tao Bao612336d2015-08-27 16:41:21 -07001411
Tao Baob15fd222015-09-24 11:10:51 -07001412 params.createdstash = res;
1413
Tao Bao612336d2015-08-27 16:41:21 -07001414 start += 2;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001415 }
1416
Sami Tolvanen90221202014-12-09 16:39:47 +00001417 // Build a hash table of the available commands
Tao Bao612336d2015-08-27 16:41:21 -07001418 HashTable* cmdht = mzHashTableCreate(cmdcount, nullptr);
1419 std::unique_ptr<HashTable, decltype(&mzHashTableFree)> cmdht_holder(cmdht, mzHashTableFree);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001420
Tao Bao0940fe12015-08-27 16:41:21 -07001421 for (size_t i = 0; i < cmdcount; ++i) {
1422 unsigned int cmdhash = HashString(commands[i].name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001423 mzHashTableLookup(cmdht, cmdhash, (void*) &commands[i], CompareCommands, true);
1424 }
1425
Tao Bao612336d2015-08-27 16:41:21 -07001426 int rc = -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001427
Tao Bao612336d2015-08-27 16:41:21 -07001428 // Subsequent lines are all individual transfer commands
1429 for (auto it = lines.cbegin() + start; it != lines.cend(); it++) {
1430 const std::string& line_str(*it);
1431 char* line = strdup(line_str.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +00001432 params.cmdname = strtok_r(line, " ", &params.cpos);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001433
Tao Bao0940fe12015-08-27 16:41:21 -07001434 if (params.cmdname == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001435 fprintf(stderr, "missing command [%s]\n", line);
1436 goto pbiudone;
1437 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001438
Tao Bao0940fe12015-08-27 16:41:21 -07001439 unsigned int cmdhash = HashString(params.cmdname);
Tao Bao612336d2015-08-27 16:41:21 -07001440 const Command* cmd = reinterpret_cast<const Command*>(mzHashTableLookup(cmdht, cmdhash,
1441 params.cmdname, CompareCommandNames, false));
Doug Zongker52ae67d2014-09-08 12:22:09 -07001442
Tao Bao0940fe12015-08-27 16:41:21 -07001443 if (cmd == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001444 fprintf(stderr, "unexpected command [%s]\n", params.cmdname);
1445 goto pbiudone;
1446 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001447
Tao Bao0940fe12015-08-27 16:41:21 -07001448 if (cmd->f != nullptr && cmd->f(params) == -1) {
Tao Bao612336d2015-08-27 16:41:21 -07001449 fprintf(stderr, "failed to execute command [%s]\n", line_str.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +00001450 goto pbiudone;
1451 }
1452
Sami Tolvanen90221202014-12-09 16:39:47 +00001453 if (params.canwrite) {
Tao Bao187efff2015-07-27 14:07:08 -07001454 if (fsync(params.fd) == -1) {
1455 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
1456 goto pbiudone;
1457 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001458 fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001459 fflush(cmd_pipe);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001460 }
1461 }
1462
Sami Tolvanen90221202014-12-09 16:39:47 +00001463 if (params.canwrite) {
Tao Bao0940fe12015-08-27 16:41:21 -07001464 pthread_join(params.thread, nullptr);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001465
Tao Bao0940fe12015-08-27 16:41:21 -07001466 fprintf(stderr, "wrote %zu blocks; expected %d\n", params.written, total_blocks);
Tao Bao612336d2015-08-27 16:41:21 -07001467 fprintf(stderr, "max alloc needed was %zu\n", params.buffer.size());
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001468
Sami Tolvanen90221202014-12-09 16:39:47 +00001469 // Delete stash only after successfully completing the update, as it
1470 // may contain blocks needed to complete the update later.
1471 DeleteStash(params.stashbase);
1472 } else {
1473 fprintf(stderr, "verified partition contents; update may be resumed\n");
1474 }
1475
1476 rc = 0;
1477
1478pbiudone:
1479 if (params.fd != -1) {
1480 if (fsync(params.fd) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001481 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001482 }
Elliott Hughesb47afed2015-05-15 16:19:20 -07001483 close(params.fd);
Sami Tolvanen90221202014-12-09 16:39:47 +00001484 }
1485
Sami Tolvanen90221202014-12-09 16:39:47 +00001486 // Only delete the stash if the update cannot be resumed, or it's
1487 // a verification run and we created the stash.
1488 if (params.isunresumable || (!params.canwrite && params.createdstash)) {
1489 DeleteStash(params.stashbase);
1490 }
1491
Sami Tolvanen90221202014-12-09 16:39:47 +00001492 return StringValue(rc == 0 ? strdup("t") : strdup(""));
1493}
1494
1495// The transfer list is a text file containing commands to
1496// transfer data from one place to another on the target
1497// partition. We parse it and execute the commands in order:
1498//
1499// zero [rangeset]
1500// - fill the indicated blocks with zeros
1501//
1502// new [rangeset]
1503// - fill the blocks with data read from the new_data file
1504//
1505// erase [rangeset]
1506// - mark the given blocks as empty
1507//
1508// move <...>
1509// bsdiff <patchstart> <patchlen> <...>
1510// imgdiff <patchstart> <patchlen> <...>
1511// - read the source blocks, apply a patch (or not in the
1512// case of move), write result to target blocks. bsdiff or
1513// imgdiff specifies the type of patch; move means no patch
1514// at all.
1515//
1516// The format of <...> differs between versions 1 and 2;
1517// see the LoadSrcTgtVersion{1,2}() functions for a
1518// description of what's expected.
1519//
1520// stash <stash_id> <src_range>
1521// - (version 2+ only) load the given source range and stash
1522// the data in the given slot of the stash table.
1523//
1524// The creator of the transfer list will guarantee that no block
1525// is read (ie, used as the source for a patch or move) after it
1526// has been written.
1527//
1528// In version 2, the creator will guarantee that a given stash is
1529// loaded (with a stash command) before it's used in a
1530// move/bsdiff/imgdiff command.
1531//
1532// Within one command the source and target ranges may overlap so
1533// in general we need to read the entire source into memory before
1534// writing anything to the target blocks.
1535//
1536// All the patch data is concatenated into one patch_data file in
1537// the update package. It must be stored uncompressed because we
1538// memory-map it in directly from the archive. (Since patches are
1539// already compressed, we lose very little by not compressing
1540// their concatenation.)
1541//
1542// In version 3, commands that read data from the partition (i.e.
1543// move/bsdiff/imgdiff/stash) have one or more additional hashes
1544// before the range parameters, which are used to check if the
1545// command has already been completed and verify the integrity of
1546// the source data.
1547
1548Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao0940fe12015-08-27 16:41:21 -07001549 // Commands which are not tested are set to nullptr to skip them completely
Sami Tolvanen90221202014-12-09 16:39:47 +00001550 const Command commands[] = {
1551 { "bsdiff", PerformCommandDiff },
Tao Bao0940fe12015-08-27 16:41:21 -07001552 { "erase", nullptr },
Sami Tolvanen90221202014-12-09 16:39:47 +00001553 { "free", PerformCommandFree },
1554 { "imgdiff", PerformCommandDiff },
1555 { "move", PerformCommandMove },
Tao Bao0940fe12015-08-27 16:41:21 -07001556 { "new", nullptr },
Sami Tolvanen90221202014-12-09 16:39:47 +00001557 { "stash", PerformCommandStash },
Tao Bao0940fe12015-08-27 16:41:21 -07001558 { "zero", nullptr }
Sami Tolvanen90221202014-12-09 16:39:47 +00001559 };
1560
1561 // Perform a dry run without writing to test if an update can proceed
1562 return PerformBlockImageUpdate(name, state, argc, argv, commands,
Tao Baoe6aa3322015-08-05 15:20:27 -07001563 sizeof(commands) / sizeof(commands[0]), true);
Sami Tolvanen90221202014-12-09 16:39:47 +00001564}
1565
1566Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) {
1567 const Command commands[] = {
1568 { "bsdiff", PerformCommandDiff },
1569 { "erase", PerformCommandErase },
1570 { "free", PerformCommandFree },
1571 { "imgdiff", PerformCommandDiff },
1572 { "move", PerformCommandMove },
1573 { "new", PerformCommandNew },
1574 { "stash", PerformCommandStash },
1575 { "zero", PerformCommandZero }
1576 };
1577
1578 return PerformBlockImageUpdate(name, state, argc, argv, commands,
Tao Baoe6aa3322015-08-05 15:20:27 -07001579 sizeof(commands) / sizeof(commands[0]), false);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001580}
1581
Tao Bao0940fe12015-08-27 16:41:21 -07001582Value* RangeSha1Fn(const char* name, State* state, int /* argc */, Expr* argv[]) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001583 Value* blockdev_filename;
1584 Value* ranges;
Tao Bao0940fe12015-08-27 16:41:21 -07001585
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001586 if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001587 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001588 }
Tao Bao612336d2015-08-27 16:41:21 -07001589 std::unique_ptr<Value, decltype(&FreeValue)> ranges_holder(ranges, FreeValue);
1590 std::unique_ptr<Value, decltype(&FreeValue)> blockdev_filename_holder(blockdev_filename,
1591 FreeValue);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001592
1593 if (blockdev_filename->type != VAL_STRING) {
1594 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001595 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001596 }
1597 if (ranges->type != VAL_STRING) {
1598 ErrorAbort(state, "ranges argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001599 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001600 }
1601
Tao Bao612336d2015-08-27 16:41:21 -07001602 int fd = open(blockdev_filename->data, O_RDWR);
1603 unique_fd fd_holder(fd);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001604 if (fd < 0) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001605 ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001606 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001607 }
1608
Tao Bao612336d2015-08-27 16:41:21 -07001609 RangeSet rs;
Tao Bao0940fe12015-08-27 16:41:21 -07001610 parse_range(ranges->data, rs);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001611
1612 SHA_CTX ctx;
1613 SHA_init(&ctx);
1614
Tao Bao612336d2015-08-27 16:41:21 -07001615 std::vector<uint8_t> buffer(BLOCKSIZE);
Tao Bao0940fe12015-08-27 16:41:21 -07001616 for (size_t i = 0; i < rs.count; ++i) {
1617 if (!check_lseek(fd, (off64_t)rs.pos[i*2] * BLOCKSIZE, SEEK_SET)) {
Tao Bao612336d2015-08-27 16:41:21 -07001618 ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data, strerror(errno));
1619 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001620 }
1621
Tao Bao0940fe12015-08-27 16:41:21 -07001622 for (size_t j = rs.pos[i*2]; j < rs.pos[i*2+1]; ++j) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001623 if (read_all(fd, buffer, BLOCKSIZE) == -1) {
1624 ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data,
Tao Bao0940fe12015-08-27 16:41:21 -07001625 strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001626 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001627 }
1628
Tao Bao612336d2015-08-27 16:41:21 -07001629 SHA_update(&ctx, buffer.data(), BLOCKSIZE);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001630 }
1631 }
Tao Bao612336d2015-08-27 16:41:21 -07001632 const uint8_t* digest = SHA_final(&ctx);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001633
Tao Bao612336d2015-08-27 16:41:21 -07001634 return StringValue(strdup(print_sha1(digest).c_str()));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001635}
1636
1637void RegisterBlockImageFunctions() {
Sami Tolvanen90221202014-12-09 16:39:47 +00001638 RegisterFunction("block_image_verify", BlockImageVerifyFn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001639 RegisterFunction("block_image_update", BlockImageUpdateFn);
1640 RegisterFunction("range_sha1", RangeSha1Fn);
1641}