blob: c6daf7db5be2bf5a4b6ec029ba2aa202e1457fcb [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>
Sami Tolvanen0a7b4732015-06-25 10:25:36 +010034#include <fec/io.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070035
Tao Baoe6aa3322015-08-05 15:20:27 -070036#include <memory>
37#include <string>
Tao Bao0940fe12015-08-27 16:41:21 -070038#include <vector>
Tao Baoe6aa3322015-08-05 15:20:27 -070039
Elliott Hughes4b166f02015-12-04 15:30:20 -080040#include <android-base/parseint.h>
41#include <android-base/strings.h>
Tao Baoe6aa3322015-08-05 15:20:27 -070042
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070043#include "applypatch/applypatch.h"
44#include "edify/expr.h"
Tianjie Xu57bed6d2015-12-15 11:47:30 -080045#include "install.h"
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070046#include "mincrypt/sha.h"
Sami Tolvanen90221202014-12-09 16:39:47 +000047#include "minzip/Hash.h"
Tao Baoe6aa3322015-08-05 15:20:27 -070048#include "print_sha1.h"
Tao Bao0940fe12015-08-27 16:41:21 -070049#include "unique_fd.h"
50#include "updater.h"
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070051
52#define BLOCKSIZE 4096
53
Sami Tolvanene82fa182015-06-10 15:58:12 +000054// Set this to 0 to interpret 'erase' transfers to mean do a
55// BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret
56// erase to mean fill the region with zeroes.
57#define DEBUG_ERASE 0
58
Sami Tolvanen90221202014-12-09 16:39:47 +000059#define STASH_DIRECTORY_BASE "/cache/recovery"
60#define STASH_DIRECTORY_MODE 0700
61#define STASH_FILE_MODE 0600
62
Tao Bao0940fe12015-08-27 16:41:21 -070063struct RangeSet {
64 size_t count; // Limit is INT_MAX.
Shrinivas Sahukara6153df2015-08-19 13:01:45 +053065 size_t size;
Tao Bao0940fe12015-08-27 16:41:21 -070066 std::vector<size_t> pos; // Actual limit is INT_MAX.
67};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070068
Tao Baobaad2d42015-12-06 16:56:27 -080069static void parse_range(const std::string& range_text, RangeSet& rs) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010070
Tao Baobaad2d42015-12-06 16:56:27 -080071 std::vector<std::string> pieces = android::base::Split(range_text, ",");
Tao Bao0940fe12015-08-27 16:41:21 -070072 if (pieces.size() < 3) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010073 goto err;
74 }
75
Tao Baob15fd222015-09-24 11:10:51 -070076 size_t num;
77 if (!android::base::ParseUint(pieces[0].c_str(), &num, static_cast<size_t>(INT_MAX))) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010078 goto err;
79 }
80
Tao Baob15fd222015-09-24 11:10:51 -070081 if (num == 0 || num % 2) {
82 goto err; // must be even
83 } else if (num != pieces.size() - 1) {
84 goto err;
85 }
Sami Tolvanenf2bac042015-05-12 12:48:46 +010086
Tao Bao0940fe12015-08-27 16:41:21 -070087 rs.pos.resize(num);
88 rs.count = num / 2;
89 rs.size = 0;
Sami Tolvanenf2bac042015-05-12 12:48:46 +010090
Tao Bao0940fe12015-08-27 16:41:21 -070091 for (size_t i = 0; i < num; i += 2) {
Tao Baob15fd222015-09-24 11:10:51 -070092 if (!android::base::ParseUint(pieces[i+1].c_str(), &rs.pos[i],
93 static_cast<size_t>(INT_MAX))) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010094 goto err;
95 }
96
Tao Baob15fd222015-09-24 11:10:51 -070097 if (!android::base::ParseUint(pieces[i+2].c_str(), &rs.pos[i+1],
98 static_cast<size_t>(INT_MAX))) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +053099 goto err;
100 }
101
Tao Bao0940fe12015-08-27 16:41:21 -0700102 if (rs.pos[i] >= rs.pos[i+1]) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530103 goto err; // empty or negative range
104 }
105
Tao Bao0940fe12015-08-27 16:41:21 -0700106 size_t sz = rs.pos[i+1] - rs.pos[i];
107 if (rs.size > SIZE_MAX - sz) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530108 goto err; // overflow
109 }
110
Tao Bao0940fe12015-08-27 16:41:21 -0700111 rs.size += sz;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700112 }
113
Tao Bao0940fe12015-08-27 16:41:21 -0700114 return;
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100115
116err:
Tao Baobaad2d42015-12-06 16:56:27 -0800117 fprintf(stderr, "failed to parse range '%s'\n", range_text.c_str());
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100118 exit(1);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700119}
120
Tao Baoe6aa3322015-08-05 15:20:27 -0700121static bool range_overlaps(const RangeSet& r1, const RangeSet& r2) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530122 for (size_t i = 0; i < r1.count; ++i) {
123 size_t r1_0 = r1.pos[i * 2];
124 size_t r1_1 = r1.pos[i * 2 + 1];
Sami Tolvanen90221202014-12-09 16:39:47 +0000125
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530126 for (size_t j = 0; j < r2.count; ++j) {
127 size_t r2_0 = r2.pos[j * 2];
128 size_t r2_1 = r2.pos[j * 2 + 1];
Sami Tolvanen90221202014-12-09 16:39:47 +0000129
Tao Baoc0f56ad2015-06-25 14:00:31 -0700130 if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700131 return true;
Sami Tolvanen90221202014-12-09 16:39:47 +0000132 }
133 }
134 }
135
Tao Baoe6aa3322015-08-05 15:20:27 -0700136 return false;
Sami Tolvanen90221202014-12-09 16:39:47 +0000137}
138
139static int read_all(int fd, uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700140 size_t so_far = 0;
141 while (so_far < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700142 ssize_t r = TEMP_FAILURE_RETRY(read(fd, data+so_far, size-so_far));
143 if (r == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700144 fprintf(stderr, "read failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000145 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700146 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700147 so_far += r;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700148 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000149 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700150}
151
Tao Bao612336d2015-08-27 16:41:21 -0700152static int read_all(int fd, std::vector<uint8_t>& buffer, size_t size) {
153 return read_all(fd, buffer.data(), size);
154}
155
Sami Tolvanen90221202014-12-09 16:39:47 +0000156static int write_all(int fd, const uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700157 size_t written = 0;
158 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700159 ssize_t w = TEMP_FAILURE_RETRY(write(fd, data+written, size-written));
160 if (w == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700161 fprintf(stderr, "write failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000162 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700163 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700164 written += w;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700165 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000166
Sami Tolvanen90221202014-12-09 16:39:47 +0000167 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700168}
169
Tao Bao612336d2015-08-27 16:41:21 -0700170static int write_all(int fd, const std::vector<uint8_t>& buffer, size_t size) {
171 return write_all(fd, buffer.data(), size);
172}
173
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700174static bool check_lseek(int fd, off64_t offset, int whence) {
175 off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
176 if (rc == -1) {
177 fprintf(stderr, "lseek64 failed: %s\n", strerror(errno));
178 return false;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700179 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700180 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700181}
182
Tao Bao612336d2015-08-27 16:41:21 -0700183static void allocate(size_t size, std::vector<uint8_t>& buffer) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700184 // if the buffer's big enough, reuse it.
Tao Bao612336d2015-08-27 16:41:21 -0700185 if (size <= buffer.size()) return;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700186
Tao Bao612336d2015-08-27 16:41:21 -0700187 buffer.resize(size);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700188}
189
Tao Bao0940fe12015-08-27 16:41:21 -0700190struct RangeSinkState {
191 RangeSinkState(RangeSet& rs) : tgt(rs) { };
192
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700193 int fd;
Tao Bao0940fe12015-08-27 16:41:21 -0700194 const RangeSet& tgt;
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530195 size_t p_block;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700196 size_t p_remain;
Tao Bao0940fe12015-08-27 16:41:21 -0700197};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700198
199static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) {
Tao Bao0940fe12015-08-27 16:41:21 -0700200 RangeSinkState* rss = reinterpret_cast<RangeSinkState*>(token);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700201
Tao Bao0940fe12015-08-27 16:41:21 -0700202 if (rss->p_remain == 0) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700203 fprintf(stderr, "range sink write overrun");
Sami Tolvanen90221202014-12-09 16:39:47 +0000204 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700205 }
206
207 ssize_t written = 0;
208 while (size > 0) {
209 size_t write_now = size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000210
211 if (rss->p_remain < write_now) {
212 write_now = rss->p_remain;
213 }
214
215 if (write_all(rss->fd, data, write_now) == -1) {
216 break;
217 }
218
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700219 data += write_now;
220 size -= write_now;
221
222 rss->p_remain -= write_now;
223 written += write_now;
224
225 if (rss->p_remain == 0) {
226 // move to the next block
227 ++rss->p_block;
Tao Bao0940fe12015-08-27 16:41:21 -0700228 if (rss->p_block < rss->tgt.count) {
229 rss->p_remain = (rss->tgt.pos[rss->p_block * 2 + 1] -
230 rss->tgt.pos[rss->p_block * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000231
Tao Bao0940fe12015-08-27 16:41:21 -0700232 if (!check_lseek(rss->fd, (off64_t)rss->tgt.pos[rss->p_block*2] * BLOCKSIZE,
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700233 SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000234 break;
235 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700236 } else {
237 // we can't write any more; return how many bytes have
238 // been written so far.
Sami Tolvanen90221202014-12-09 16:39:47 +0000239 break;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700240 }
241 }
242 }
243
244 return written;
245}
246
247// All of the data for all the 'new' transfers is contained in one
248// file in the update package, concatenated together in the order in
249// which transfers.list will need it. We want to stream it out of the
250// archive (it's compressed) without writing it to a temp file, but we
251// can't write each section until it's that transfer's turn to go.
252//
253// To achieve this, we expand the new data from the archive in a
254// background thread, and block that threads 'receive uncompressed
255// data' function until the main thread has reached a point where we
256// want some new data to be written. We signal the background thread
257// with the destination for the data and block the main thread,
258// waiting for the background thread to complete writing that section.
259// Then it signals the main thread to wake up and goes back to
260// blocking waiting for a transfer.
261//
262// NewThreadInfo is the struct used to pass information back and forth
263// between the two threads. When the main thread wants some data
264// written, it sets rss to the destination location and signals the
265// condition. When the background thread is done writing, it clears
266// rss and signals the condition again.
267
Tao Bao0940fe12015-08-27 16:41:21 -0700268struct NewThreadInfo {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700269 ZipArchive* za;
270 const ZipEntry* entry;
271
272 RangeSinkState* rss;
273
274 pthread_mutex_t mu;
275 pthread_cond_t cv;
Tao Bao0940fe12015-08-27 16:41:21 -0700276};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700277
278static bool receive_new_data(const unsigned char* data, int size, void* cookie) {
Tao Bao0940fe12015-08-27 16:41:21 -0700279 NewThreadInfo* nti = reinterpret_cast<NewThreadInfo*>(cookie);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700280
281 while (size > 0) {
Tao Bao0940fe12015-08-27 16:41:21 -0700282 // Wait for nti->rss to be non-null, indicating some of this
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700283 // data is wanted.
284 pthread_mutex_lock(&nti->mu);
Tao Bao0940fe12015-08-27 16:41:21 -0700285 while (nti->rss == nullptr) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700286 pthread_cond_wait(&nti->cv, &nti->mu);
287 }
288 pthread_mutex_unlock(&nti->mu);
289
290 // At this point nti->rss is set, and we own it. The main
291 // thread is waiting for it to disappear from nti.
292 ssize_t written = RangeSinkWrite(data, size, nti->rss);
293 data += written;
294 size -= written;
295
Tao Bao0940fe12015-08-27 16:41:21 -0700296 if (nti->rss->p_block == nti->rss->tgt.count) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700297 // we have written all the bytes desired by this rss.
298
299 pthread_mutex_lock(&nti->mu);
Tao Bao0940fe12015-08-27 16:41:21 -0700300 nti->rss = nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700301 pthread_cond_broadcast(&nti->cv);
302 pthread_mutex_unlock(&nti->mu);
303 }
304 }
305
306 return true;
307}
308
309static void* unzip_new_data(void* cookie) {
310 NewThreadInfo* nti = (NewThreadInfo*) cookie;
311 mzProcessZipEntryContents(nti->za, nti->entry, receive_new_data, nti);
Tao Bao0940fe12015-08-27 16:41:21 -0700312 return nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700313}
314
Tao Bao612336d2015-08-27 16:41:21 -0700315static int ReadBlocks(const RangeSet& src, std::vector<uint8_t>& buffer, int fd) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000316 size_t p = 0;
Tao Bao612336d2015-08-27 16:41:21 -0700317 uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000318
Tao Bao0940fe12015-08-27 16:41:21 -0700319 for (size_t i = 0; i < src.count; ++i) {
320 if (!check_lseek(fd, (off64_t) src.pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000321 return -1;
322 }
323
Tao Bao0940fe12015-08-27 16:41:21 -0700324 size_t size = (src.pos[i * 2 + 1] - src.pos[i * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000325
Tao Bao612336d2015-08-27 16:41:21 -0700326 if (read_all(fd, data + p, size) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000327 return -1;
328 }
329
330 p += size;
331 }
332
333 return 0;
334}
335
Tao Bao612336d2015-08-27 16:41:21 -0700336static int WriteBlocks(const RangeSet& tgt, const std::vector<uint8_t>& buffer, int fd) {
337 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000338
Tao Bao0940fe12015-08-27 16:41:21 -0700339 size_t p = 0;
340 for (size_t i = 0; i < tgt.count; ++i) {
341 if (!check_lseek(fd, (off64_t) tgt.pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000342 return -1;
343 }
344
Tao Bao0940fe12015-08-27 16:41:21 -0700345 size_t size = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000346
Tao Bao612336d2015-08-27 16:41:21 -0700347 if (write_all(fd, data + p, size) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000348 return -1;
349 }
350
351 p += size;
352 }
353
354 return 0;
355}
356
Tao Baobaad2d42015-12-06 16:56:27 -0800357// Parameters for transfer list command functions
358struct CommandParameters {
359 std::vector<std::string> tokens;
360 size_t cpos;
361 const char* cmdname;
362 const char* cmdline;
363 std::string freestash;
364 std::string stashbase;
365 bool canwrite;
366 int createdstash;
367 int fd;
368 bool foundwrites;
369 bool isunresumable;
370 int version;
371 size_t written;
372 NewThreadInfo nti;
373 pthread_t thread;
374 std::vector<uint8_t> buffer;
375 uint8_t* patch_start;
376};
377
Doug Zongker52ae67d2014-09-08 12:22:09 -0700378// Do a source/target load for move/bsdiff/imgdiff in version 1.
Tao Baobaad2d42015-12-06 16:56:27 -0800379// We expect to parse the remainder of the parameter tokens as:
Doug Zongker52ae67d2014-09-08 12:22:09 -0700380//
381// <src_range> <tgt_range>
382//
383// The source range is loaded into the provided buffer, reallocating
Tao Bao34847b22015-09-08 11:05:49 -0700384// it to make it larger if necessary.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700385
Tao Baobaad2d42015-12-06 16:56:27 -0800386static int LoadSrcTgtVersion1(CommandParameters& params, RangeSet& tgt, size_t& src_blocks,
Tao Bao612336d2015-08-27 16:41:21 -0700387 std::vector<uint8_t>& buffer, int fd) {
Tao Baobaad2d42015-12-06 16:56:27 -0800388
389 if (params.cpos + 1 >= params.tokens.size()) {
390 fprintf(stderr, "invalid parameters\n");
391 return -1;
392 }
393
Tao Bao612336d2015-08-27 16:41:21 -0700394 // <src_range>
Tao Bao0940fe12015-08-27 16:41:21 -0700395 RangeSet src;
Tao Baobaad2d42015-12-06 16:56:27 -0800396 parse_range(params.tokens[params.cpos++], src);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700397
Tao Bao612336d2015-08-27 16:41:21 -0700398 // <tgt_range>
Tao Baobaad2d42015-12-06 16:56:27 -0800399 parse_range(params.tokens[params.cpos++], tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700400
Tao Bao612336d2015-08-27 16:41:21 -0700401 allocate(src.size * BLOCKSIZE, buffer);
402 int rc = ReadBlocks(src, buffer, fd);
Tao Bao0940fe12015-08-27 16:41:21 -0700403 src_blocks = src.size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000404
Sami Tolvanen90221202014-12-09 16:39:47 +0000405 return rc;
406}
407
Tao Bao612336d2015-08-27 16:41:21 -0700408static int VerifyBlocks(const std::string& expected, const std::vector<uint8_t>& buffer,
Tao Bao0940fe12015-08-27 16:41:21 -0700409 const size_t blocks, bool printerror) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000410 uint8_t digest[SHA_DIGEST_SIZE];
Tao Bao612336d2015-08-27 16:41:21 -0700411 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000412
Tao Bao612336d2015-08-27 16:41:21 -0700413 SHA_hash(data, blocks * BLOCKSIZE, digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000414
Tao Baoe6aa3322015-08-05 15:20:27 -0700415 std::string hexdigest = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000416
Tao Bao0940fe12015-08-27 16:41:21 -0700417 if (hexdigest != expected) {
418 if (printerror) {
419 fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
420 expected.c_str(), hexdigest.c_str());
421 }
422 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000423 }
424
Tao Bao0940fe12015-08-27 16:41:21 -0700425 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000426}
427
Tao Bao0940fe12015-08-27 16:41:21 -0700428static std::string GetStashFileName(const std::string& base, const std::string& id,
429 const std::string& postfix) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700430 if (base.empty()) {
431 return "";
Sami Tolvanen90221202014-12-09 16:39:47 +0000432 }
433
Tao Baoe6aa3322015-08-05 15:20:27 -0700434 std::string fn(STASH_DIRECTORY_BASE);
435 fn += "/" + base + "/" + id + postfix;
Sami Tolvanen90221202014-12-09 16:39:47 +0000436
437 return fn;
438}
439
Tao Baoe6aa3322015-08-05 15:20:27 -0700440typedef void (*StashCallback)(const std::string&, void*);
Sami Tolvanen90221202014-12-09 16:39:47 +0000441
442// Does a best effort enumeration of stash files. Ignores possible non-file
443// items in the stash directory and continues despite of errors. Calls the
444// 'callback' function for each file and passes 'data' to the function as a
445// parameter.
446
Tao Baoe6aa3322015-08-05 15:20:27 -0700447static void EnumerateStash(const std::string& dirname, StashCallback callback, void* data) {
Tao Bao0940fe12015-08-27 16:41:21 -0700448 if (dirname.empty() || callback == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000449 return;
450 }
451
Tao Baoe6aa3322015-08-05 15:20:27 -0700452 std::unique_ptr<DIR, int(*)(DIR*)> directory(opendir(dirname.c_str()), closedir);
Sami Tolvanen90221202014-12-09 16:39:47 +0000453
Tao Bao0940fe12015-08-27 16:41:21 -0700454 if (directory == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000455 if (errno != ENOENT) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700456 fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000457 }
458 return;
459 }
460
Tao Baoe6aa3322015-08-05 15:20:27 -0700461 struct dirent* item;
Tao Bao0940fe12015-08-27 16:41:21 -0700462 while ((item = readdir(directory.get())) != nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000463 if (item->d_type != DT_REG) {
464 continue;
465 }
466
Tao Baoe6aa3322015-08-05 15:20:27 -0700467 std::string fn = dirname + "/" + std::string(item->d_name);
Sami Tolvanen90221202014-12-09 16:39:47 +0000468 callback(fn, data);
Sami Tolvanen90221202014-12-09 16:39:47 +0000469 }
470}
471
Tao Baoe6aa3322015-08-05 15:20:27 -0700472static void UpdateFileSize(const std::string& fn, void* data) {
473 if (fn.empty() || !data) {
474 return;
475 }
476
Tao Bao0940fe12015-08-27 16:41:21 -0700477 struct stat sb;
478 if (stat(fn.c_str(), &sb) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700479 fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000480 return;
481 }
482
Tao Baoe6aa3322015-08-05 15:20:27 -0700483 int* size = reinterpret_cast<int*>(data);
Tao Bao0940fe12015-08-27 16:41:21 -0700484 *size += sb.st_size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000485}
486
487// Deletes the stash directory and all files in it. Assumes that it only
488// contains files. There is nothing we can do about unlikely, but possible
489// errors, so they are merely logged.
490
Tao Bao0940fe12015-08-27 16:41:21 -0700491static void DeleteFile(const std::string& fn, void* /* data */) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700492 if (!fn.empty()) {
493 fprintf(stderr, "deleting %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000494
Tao Baoe6aa3322015-08-05 15:20:27 -0700495 if (unlink(fn.c_str()) == -1 && errno != ENOENT) {
496 fprintf(stderr, "unlink \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000497 }
498 }
499}
500
Tao Baoe6aa3322015-08-05 15:20:27 -0700501static void DeletePartial(const std::string& fn, void* data) {
502 if (android::base::EndsWith(fn, ".partial")) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000503 DeleteFile(fn, data);
504 }
505}
506
Tao Baoe6aa3322015-08-05 15:20:27 -0700507static void DeleteStash(const std::string& base) {
508 if (base.empty()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000509 return;
510 }
511
Tao Baoe6aa3322015-08-05 15:20:27 -0700512 fprintf(stderr, "deleting stash %s\n", base.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000513
Tao Baoe6aa3322015-08-05 15:20:27 -0700514 std::string dirname = GetStashFileName(base, "", "");
Tao Bao0940fe12015-08-27 16:41:21 -0700515 EnumerateStash(dirname, DeleteFile, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000516
Tao Baoe6aa3322015-08-05 15:20:27 -0700517 if (rmdir(dirname.c_str()) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000518 if (errno != ENOENT && errno != ENOTDIR) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700519 fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000520 }
521 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000522}
523
Tao Bao0940fe12015-08-27 16:41:21 -0700524static int LoadStash(const std::string& base, const std::string& id, bool verify, size_t* blocks,
Tao Bao612336d2015-08-27 16:41:21 -0700525 std::vector<uint8_t>& buffer, bool printnoent) {
526 if (base.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -0700527 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000528 }
529
Tao Bao0940fe12015-08-27 16:41:21 -0700530 size_t blockcount = 0;
531
Sami Tolvanen90221202014-12-09 16:39:47 +0000532 if (!blocks) {
533 blocks = &blockcount;
534 }
535
Tao Bao0940fe12015-08-27 16:41:21 -0700536 std::string fn = GetStashFileName(base, id, "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000537
Tao Bao0940fe12015-08-27 16:41:21 -0700538 struct stat sb;
539 int res = stat(fn.c_str(), &sb);
Sami Tolvanen90221202014-12-09 16:39:47 +0000540
541 if (res == -1) {
542 if (errno != ENOENT || printnoent) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700543 fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000544 }
Tao Bao0940fe12015-08-27 16:41:21 -0700545 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000546 }
547
Tao Baoe6aa3322015-08-05 15:20:27 -0700548 fprintf(stderr, " loading %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000549
Tao Bao0940fe12015-08-27 16:41:21 -0700550 if ((sb.st_size % BLOCKSIZE) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700551 fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d",
Tao Bao0940fe12015-08-27 16:41:21 -0700552 fn.c_str(), static_cast<int64_t>(sb.st_size), BLOCKSIZE);
553 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000554 }
555
Tao Bao0940fe12015-08-27 16:41:21 -0700556 int fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY));
557 unique_fd fd_holder(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000558
559 if (fd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700560 fprintf(stderr, "open \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700561 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000562 }
563
Tao Bao612336d2015-08-27 16:41:21 -0700564 allocate(sb.st_size, buffer);
Sami Tolvanen90221202014-12-09 16:39:47 +0000565
Tao Bao612336d2015-08-27 16:41:21 -0700566 if (read_all(fd, buffer, sb.st_size) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700567 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000568 }
569
Tao Bao0940fe12015-08-27 16:41:21 -0700570 *blocks = sb.st_size / BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000571
Tao Bao612336d2015-08-27 16:41:21 -0700572 if (verify && VerifyBlocks(id, buffer, *blocks, true) != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700573 fprintf(stderr, "unexpected contents in %s\n", fn.c_str());
Tao Bao0940fe12015-08-27 16:41:21 -0700574 DeleteFile(fn, nullptr);
575 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000576 }
577
Tao Bao0940fe12015-08-27 16:41:21 -0700578 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000579}
580
Tao Bao612336d2015-08-27 16:41:21 -0700581static int WriteStash(const std::string& base, const std::string& id, int blocks,
582 std::vector<uint8_t>& buffer, bool checkspace, bool *exists) {
583 if (base.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -0700584 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000585 }
586
587 if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) {
588 fprintf(stderr, "not enough space to write stash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700589 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000590 }
591
Tao Bao0940fe12015-08-27 16:41:21 -0700592 std::string fn = GetStashFileName(base, id, ".partial");
593 std::string cn = GetStashFileName(base, id, "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000594
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100595 if (exists) {
Tao Bao0940fe12015-08-27 16:41:21 -0700596 struct stat sb;
597 int res = stat(cn.c_str(), &sb);
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100598
599 if (res == 0) {
600 // The file already exists and since the name is the hash of the contents,
601 // it's safe to assume the contents are identical (accidental hash collisions
602 // are unlikely)
Tao Baoe6aa3322015-08-05 15:20:27 -0700603 fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn.c_str());
Tao Bao0940fe12015-08-27 16:41:21 -0700604 *exists = true;
605 return 0;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100606 }
607
Tao Bao0940fe12015-08-27 16:41:21 -0700608 *exists = false;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100609 }
610
Tao Baoe6aa3322015-08-05 15:20:27 -0700611 fprintf(stderr, " writing %d blocks to %s\n", blocks, cn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000612
Tao Bao0940fe12015-08-27 16:41:21 -0700613 int fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE));
614 unique_fd fd_holder(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000615
616 if (fd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700617 fprintf(stderr, "failed to create \"%s\": %s\n", fn.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700618 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000619 }
620
621 if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700622 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000623 }
624
625 if (fsync(fd) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700626 fprintf(stderr, "fsync \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700627 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000628 }
629
Tao Baoe6aa3322015-08-05 15:20:27 -0700630 if (rename(fn.c_str(), cn.c_str()) == -1) {
631 fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn.c_str(), cn.c_str(),
632 strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700633 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000634 }
635
Tao Bao0940fe12015-08-27 16:41:21 -0700636 std::string dname = GetStashFileName(base, "", "");
637 int dfd = TEMP_FAILURE_RETRY(open(dname.c_str(), O_RDONLY | O_DIRECTORY));
638 unique_fd dfd_holder(dfd);
Tao Baodc392262015-07-31 15:56:44 -0700639
640 if (dfd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700641 fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700642 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700643 }
644
645 if (fsync(dfd) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700646 fprintf(stderr, "fsync \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700647 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700648 }
649
Tao Bao0940fe12015-08-27 16:41:21 -0700650 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000651}
652
653// Creates a directory for storing stash files and checks if the /cache partition
654// hash enough space for the expected amount of blocks we need to store. Returns
655// >0 if we created the directory, zero if it existed already, and <0 of failure.
656
Tao Bao0940fe12015-08-27 16:41:21 -0700657static int CreateStash(State* state, int maxblocks, const char* blockdev, std::string& base) {
658 if (blockdev == nullptr) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700659 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000660 }
661
662 // Stash directory should be different for each partition to avoid conflicts
663 // when updating multiple partitions at the same time, so we use the hash of
664 // the block device name as the base directory
Tao Baoe6aa3322015-08-05 15:20:27 -0700665 SHA_CTX ctx;
Sami Tolvanen90221202014-12-09 16:39:47 +0000666 SHA_init(&ctx);
667 SHA_update(&ctx, blockdev, strlen(blockdev));
Tao Baoe6aa3322015-08-05 15:20:27 -0700668 const uint8_t* digest = SHA_final(&ctx);
669 base = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000670
Tao Baoe6aa3322015-08-05 15:20:27 -0700671 std::string dirname = GetStashFileName(base, "", "");
Tao Bao0940fe12015-08-27 16:41:21 -0700672 struct stat sb;
673 int res = stat(dirname.c_str(), &sb);
Sami Tolvanen90221202014-12-09 16:39:47 +0000674
675 if (res == -1 && errno != ENOENT) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700676 ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
677 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000678 } else if (res != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700679 fprintf(stderr, "creating stash %s\n", dirname.c_str());
680 res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000681
682 if (res != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700683 ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
684 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000685 }
686
687 if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) {
688 ErrorAbort(state, "not enough space for stash\n");
Tao Baoe6aa3322015-08-05 15:20:27 -0700689 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000690 }
691
Tao Baoe6aa3322015-08-05 15:20:27 -0700692 return 1; // Created directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000693 }
694
Tao Baoe6aa3322015-08-05 15:20:27 -0700695 fprintf(stderr, "using existing stash %s\n", dirname.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000696
697 // If the directory already exists, calculate the space already allocated to
698 // stash files and check if there's enough for all required blocks. Delete any
699 // partially completed stash files first.
700
Tao Bao0940fe12015-08-27 16:41:21 -0700701 EnumerateStash(dirname, DeletePartial, nullptr);
Tao Baoe6aa3322015-08-05 15:20:27 -0700702 int size = 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000703 EnumerateStash(dirname, UpdateFileSize, &size);
704
Tao Bao0940fe12015-08-27 16:41:21 -0700705 size = maxblocks * BLOCKSIZE - size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000706
707 if (size > 0 && CacheSizeCheck(size) != 0) {
708 ErrorAbort(state, "not enough space for stash (%d more needed)\n", size);
Tao Baoe6aa3322015-08-05 15:20:27 -0700709 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000710 }
711
Tao Baoe6aa3322015-08-05 15:20:27 -0700712 return 0; // Using existing directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000713}
714
Tao Baobaad2d42015-12-06 16:56:27 -0800715static int SaveStash(CommandParameters& params, const std::string& base,
716 std::vector<uint8_t>& buffer, int fd, bool usehash) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000717
Tao Baobaad2d42015-12-06 16:56:27 -0800718 // <stash_id> <src_range>
719 if (params.cpos + 1 >= params.tokens.size()) {
720 fprintf(stderr, "missing id and/or src range fields in stash command\n");
Sami Tolvanen90221202014-12-09 16:39:47 +0000721 return -1;
722 }
Tao Baobaad2d42015-12-06 16:56:27 -0800723 const std::string& id = params.tokens[params.cpos++];
Sami Tolvanen90221202014-12-09 16:39:47 +0000724
Tao Bao0940fe12015-08-27 16:41:21 -0700725 size_t blocks = 0;
Tao Bao612336d2015-08-27 16:41:21 -0700726 if (usehash && LoadStash(base, id, true, &blocks, buffer, false) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000727 // Stash file already exists and has expected contents. Do not
728 // read from source again, as the source may have been already
729 // overwritten during a previous attempt.
730 return 0;
731 }
732
Tao Bao34847b22015-09-08 11:05:49 -0700733 RangeSet src;
Tao Baobaad2d42015-12-06 16:56:27 -0800734 parse_range(params.tokens[params.cpos++], src);
Tao Bao34847b22015-09-08 11:05:49 -0700735
Tao Bao612336d2015-08-27 16:41:21 -0700736 allocate(src.size * BLOCKSIZE, buffer);
737 if (ReadBlocks(src, buffer, fd) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000738 return -1;
739 }
Tao Bao34847b22015-09-08 11:05:49 -0700740 blocks = src.size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000741
Tao Bao612336d2015-08-27 16:41:21 -0700742 if (usehash && VerifyBlocks(id, buffer, blocks, true) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000743 // Source blocks have unexpected contents. If we actually need this
744 // data later, this is an unrecoverable error. However, the command
745 // that uses the data may have already completed previously, so the
746 // possible failure will occur during source block verification.
Tao Bao0940fe12015-08-27 16:41:21 -0700747 fprintf(stderr, "failed to load source blocks for stash %s\n", id.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000748 return 0;
749 }
750
Tao Bao0940fe12015-08-27 16:41:21 -0700751 fprintf(stderr, "stashing %zu blocks to %s\n", blocks, id.c_str());
Tao Bao612336d2015-08-27 16:41:21 -0700752 return WriteStash(base, id, blocks, buffer, false, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000753}
754
Tao Baobaad2d42015-12-06 16:56:27 -0800755static int FreeStash(const std::string& base, const std::string& id) {
756 if (base.empty() || id.empty()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000757 return -1;
758 }
759
Tao Baobaad2d42015-12-06 16:56:27 -0800760 std::string fn = GetStashFileName(base, id, "");
Tao Bao0940fe12015-08-27 16:41:21 -0700761 DeleteFile(fn, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000762
763 return 0;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700764}
765
Tao Bao612336d2015-08-27 16:41:21 -0700766static void MoveRange(std::vector<uint8_t>& dest, const RangeSet& locs,
767 const std::vector<uint8_t>& source) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700768 // source contains packed data, which we want to move to the
Tao Bao612336d2015-08-27 16:41:21 -0700769 // locations given in locs in the dest buffer. source and dest
Doug Zongker52ae67d2014-09-08 12:22:09 -0700770 // may be the same buffer.
771
Tao Bao612336d2015-08-27 16:41:21 -0700772 const uint8_t* from = source.data();
773 uint8_t* to = dest.data();
Tao Bao0940fe12015-08-27 16:41:21 -0700774 size_t start = locs.size;
775 for (int i = locs.count-1; i >= 0; --i) {
776 size_t blocks = locs.pos[i*2+1] - locs.pos[i*2];
Doug Zongker52ae67d2014-09-08 12:22:09 -0700777 start -= blocks;
Tao Bao612336d2015-08-27 16:41:21 -0700778 memmove(to + (locs.pos[i*2] * BLOCKSIZE), from + (start * BLOCKSIZE),
Doug Zongker52ae67d2014-09-08 12:22:09 -0700779 blocks * BLOCKSIZE);
780 }
781}
782
783// Do a source/target load for move/bsdiff/imgdiff in version 2.
Tao Baobaad2d42015-12-06 16:56:27 -0800784// We expect to parse the remainder of the parameter tokens as one of:
Doug Zongker52ae67d2014-09-08 12:22:09 -0700785//
786// <tgt_range> <src_block_count> <src_range>
787// (loads data from source image only)
788//
789// <tgt_range> <src_block_count> - <[stash_id:stash_range] ...>
790// (loads data from stashes only)
791//
792// <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...>
793// (loads data from both source image and stashes)
794//
795// On return, buffer is filled with the loaded source data (rearranged
796// and combined with stashed data as necessary). buffer may be
797// reallocated if needed to accommodate the source data. *tgt is the
Sami Tolvanen90221202014-12-09 16:39:47 +0000798// target RangeSet. Any stashes required are loaded using LoadStash.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700799
Tao Baobaad2d42015-12-06 16:56:27 -0800800static int LoadSrcTgtVersion2(CommandParameters& params, RangeSet& tgt, size_t& src_blocks,
Tao Bao612336d2015-08-27 16:41:21 -0700801 std::vector<uint8_t>& buffer, int fd, const std::string& stashbase, bool* overlap) {
Tao Baobaad2d42015-12-06 16:56:27 -0800802
803 // At least it needs to provide three parameters: <tgt_range>,
804 // <src_block_count> and "-"/<src_range>.
805 if (params.cpos + 2 >= params.tokens.size()) {
806 fprintf(stderr, "invalid parameters\n");
807 return -1;
808 }
809
Tao Bao612336d2015-08-27 16:41:21 -0700810 // <tgt_range>
Tao Baobaad2d42015-12-06 16:56:27 -0800811 parse_range(params.tokens[params.cpos++], tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700812
Tao Bao612336d2015-08-27 16:41:21 -0700813 // <src_block_count>
Tao Baobaad2d42015-12-06 16:56:27 -0800814 const std::string& token = params.tokens[params.cpos++];
815 if (!android::base::ParseUint(token.c_str(), &src_blocks)) {
816 fprintf(stderr, "invalid src_block_count \"%s\"\n", token.c_str());
817 return -1;
818 }
Doug Zongker52ae67d2014-09-08 12:22:09 -0700819
Tao Bao612336d2015-08-27 16:41:21 -0700820 allocate(src_blocks * BLOCKSIZE, buffer);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700821
Tao Bao612336d2015-08-27 16:41:21 -0700822 // "-" or <src_range> [<src_loc>]
Tao Baobaad2d42015-12-06 16:56:27 -0800823 if (params.tokens[params.cpos] == "-") {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700824 // no source ranges, only stashes
Tao Baobaad2d42015-12-06 16:56:27 -0800825 params.cpos++;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700826 } else {
Tao Bao0940fe12015-08-27 16:41:21 -0700827 RangeSet src;
Tao Baobaad2d42015-12-06 16:56:27 -0800828 parse_range(params.tokens[params.cpos++], src);
Tao Bao612336d2015-08-27 16:41:21 -0700829 int res = ReadBlocks(src, buffer, fd);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700830
Tao Bao34847b22015-09-08 11:05:49 -0700831 if (overlap) {
832 *overlap = range_overlaps(src, tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700833 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000834
Sami Tolvanen90221202014-12-09 16:39:47 +0000835 if (res == -1) {
836 return -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700837 }
838
Tao Baobaad2d42015-12-06 16:56:27 -0800839 if (params.cpos >= params.tokens.size()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000840 // no stashes, only source range
841 return 0;
842 }
843
Tao Bao612336d2015-08-27 16:41:21 -0700844 RangeSet locs;
Tao Baobaad2d42015-12-06 16:56:27 -0800845 parse_range(params.tokens[params.cpos++], locs);
Tao Bao612336d2015-08-27 16:41:21 -0700846 MoveRange(buffer, locs, buffer);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700847 }
848
Tao Baobaad2d42015-12-06 16:56:27 -0800849 // <[stash_id:stash_range]>
850 while (params.cpos < params.tokens.size()) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700851 // Each word is a an index into the stash table, a colon, and
852 // then a rangeset describing where in the source block that
853 // stashed data should go.
Tao Baobaad2d42015-12-06 16:56:27 -0800854 std::vector<std::string> tokens = android::base::Split(params.tokens[params.cpos++], ":");
855 if (tokens.size() != 2) {
856 fprintf(stderr, "invalid parameter\n");
857 return -1;
858 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000859
Tao Bao612336d2015-08-27 16:41:21 -0700860 std::vector<uint8_t> stash;
Tao Baobaad2d42015-12-06 16:56:27 -0800861 int res = LoadStash(stashbase, tokens[0], false, nullptr, stash, true);
Sami Tolvanen90221202014-12-09 16:39:47 +0000862
863 if (res == -1) {
864 // These source blocks will fail verification if used later, but we
865 // will let the caller decide if this is a fatal failure
Tao Baobaad2d42015-12-06 16:56:27 -0800866 fprintf(stderr, "failed to load stash %s\n", tokens[0].c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000867 continue;
868 }
869
Tao Bao612336d2015-08-27 16:41:21 -0700870 RangeSet locs;
Tao Baobaad2d42015-12-06 16:56:27 -0800871 parse_range(tokens[1], locs);
Sami Tolvanen90221202014-12-09 16:39:47 +0000872
Tao Bao612336d2015-08-27 16:41:21 -0700873 MoveRange(buffer, locs, stash);
Sami Tolvanen90221202014-12-09 16:39:47 +0000874 }
875
876 return 0;
877}
878
Sami Tolvanen90221202014-12-09 16:39:47 +0000879// Do a source/target load for move/bsdiff/imgdiff in version 3.
880//
881// Parameters are the same as for LoadSrcTgtVersion2, except for 'onehash', which
882// tells the function whether to expect separate source and targe block hashes, or
883// if they are both the same and only one hash should be expected, and
884// 'isunresumable', which receives a non-zero value if block verification fails in
885// a way that the update cannot be resumed anymore.
886//
887// If the function is unable to load the necessary blocks or their contents don't
888// match the hashes, the return value is -1 and the command should be aborted.
889//
890// If the return value is 1, the command has already been completed according to
891// the contents of the target blocks, and should not be performed again.
892//
893// If the return value is 0, source blocks have expected content and the command
894// can be performed.
895
Tao Bao34847b22015-09-08 11:05:49 -0700896static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t& src_blocks,
Tao Bao0940fe12015-08-27 16:41:21 -0700897 bool onehash, bool& overlap) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000898
Tao Baobaad2d42015-12-06 16:56:27 -0800899 if (params.cpos >= params.tokens.size()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000900 fprintf(stderr, "missing source hash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700901 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000902 }
903
Tao Baobaad2d42015-12-06 16:56:27 -0800904 std::string srchash = params.tokens[params.cpos++];
905 std::string tgthash;
906
Sami Tolvanen90221202014-12-09 16:39:47 +0000907 if (onehash) {
908 tgthash = srchash;
909 } else {
Tao Baobaad2d42015-12-06 16:56:27 -0800910 if (params.cpos >= params.tokens.size()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000911 fprintf(stderr, "missing target hash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700912 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000913 }
Tao Baobaad2d42015-12-06 16:56:27 -0800914 tgthash = params.tokens[params.cpos++];
Sami Tolvanen90221202014-12-09 16:39:47 +0000915 }
916
Tao Baobaad2d42015-12-06 16:56:27 -0800917 if (LoadSrcTgtVersion2(params, tgt, src_blocks, params.buffer, params.fd, params.stashbase,
918 &overlap) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700919 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000920 }
921
Tao Bao34847b22015-09-08 11:05:49 -0700922 std::vector<uint8_t> tgtbuffer(tgt.size * BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000923
Tao Bao612336d2015-08-27 16:41:21 -0700924 if (ReadBlocks(tgt, tgtbuffer, params.fd) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700925 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000926 }
927
Tao Bao612336d2015-08-27 16:41:21 -0700928 if (VerifyBlocks(tgthash, tgtbuffer, tgt.size, false) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000929 // Target blocks already have expected content, command should be skipped
Tao Bao0940fe12015-08-27 16:41:21 -0700930 return 1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000931 }
932
Tao Bao0940fe12015-08-27 16:41:21 -0700933 if (VerifyBlocks(srchash, params.buffer, src_blocks, true) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000934 // If source and target blocks overlap, stash the source blocks so we can
935 // resume from possible write errors
Tao Bao0940fe12015-08-27 16:41:21 -0700936 if (overlap) {
Tao Baobaad2d42015-12-06 16:56:27 -0800937 fprintf(stderr, "stashing %zu overlapping blocks to %s\n", src_blocks,
938 srchash.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000939
Tao Bao0940fe12015-08-27 16:41:21 -0700940 bool stash_exists = false;
Tao Bao612336d2015-08-27 16:41:21 -0700941 if (WriteStash(params.stashbase, srchash, src_blocks, params.buffer, true,
Tao Bao0940fe12015-08-27 16:41:21 -0700942 &stash_exists) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000943 fprintf(stderr, "failed to stash overlapping source blocks\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700944 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000945 }
946
947 // Can be deleted when the write has completed
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100948 if (!stash_exists) {
Tao Bao0940fe12015-08-27 16:41:21 -0700949 params.freestash = srchash;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100950 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000951 }
952
953 // Source blocks have expected content, command can proceed
Tao Bao0940fe12015-08-27 16:41:21 -0700954 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000955 }
956
Tao Bao612336d2015-08-27 16:41:21 -0700957 if (overlap && LoadStash(params.stashbase, srchash, true, nullptr, params.buffer, true) == 0) {
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100958 // Overlapping source blocks were previously stashed, command can proceed.
959 // We are recovering from an interrupted command, so we don't know if the
960 // stash can safely be deleted after this command.
Tao Bao0940fe12015-08-27 16:41:21 -0700961 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000962 }
963
964 // Valid source data not available, update cannot be resumed
965 fprintf(stderr, "partition has unexpected contents\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700966 params.isunresumable = true;
Sami Tolvanen90221202014-12-09 16:39:47 +0000967
Tao Bao0940fe12015-08-27 16:41:21 -0700968 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000969}
970
Tao Bao0940fe12015-08-27 16:41:21 -0700971static int PerformCommandMove(CommandParameters& params) {
972 size_t blocks = 0;
Tao Baoe6aa3322015-08-05 15:20:27 -0700973 bool overlap = false;
Sami Tolvanen90221202014-12-09 16:39:47 +0000974 int status = 0;
Tao Bao0940fe12015-08-27 16:41:21 -0700975 RangeSet tgt;
Sami Tolvanen90221202014-12-09 16:39:47 +0000976
Tao Bao0940fe12015-08-27 16:41:21 -0700977 if (params.version == 1) {
Tao Baobaad2d42015-12-06 16:56:27 -0800978 status = LoadSrcTgtVersion1(params, tgt, blocks, params.buffer, params.fd);
Tao Bao0940fe12015-08-27 16:41:21 -0700979 } else if (params.version == 2) {
Tao Baobaad2d42015-12-06 16:56:27 -0800980 status = LoadSrcTgtVersion2(params, tgt, blocks, params.buffer, params.fd,
Tao Bao612336d2015-08-27 16:41:21 -0700981 params.stashbase, nullptr);
Tao Bao0940fe12015-08-27 16:41:21 -0700982 } else if (params.version >= 3) {
Tao Bao34847b22015-09-08 11:05:49 -0700983 status = LoadSrcTgtVersion3(params, tgt, blocks, true, overlap);
Sami Tolvanen90221202014-12-09 16:39:47 +0000984 }
985
986 if (status == -1) {
987 fprintf(stderr, "failed to read blocks for move\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700988 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000989 }
990
991 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -0700992 params.foundwrites = true;
993 } else if (params.foundwrites) {
994 fprintf(stderr, "warning: commands executed out of order [%s]\n", params.cmdname);
Sami Tolvanen90221202014-12-09 16:39:47 +0000995 }
996
Tao Bao0940fe12015-08-27 16:41:21 -0700997 if (params.canwrite) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000998 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -0700999 fprintf(stderr, " moving %zu blocks\n", blocks);
Sami Tolvanen90221202014-12-09 16:39:47 +00001000
Tao Bao0940fe12015-08-27 16:41:21 -07001001 if (WriteBlocks(tgt, params.buffer, params.fd) == -1) {
1002 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001003 }
1004 } else {
Tao Bao0940fe12015-08-27 16:41:21 -07001005 fprintf(stderr, "skipping %zu already moved blocks\n", blocks);
Sami Tolvanen90221202014-12-09 16:39:47 +00001006 }
1007
1008 }
1009
Tao Baobaad2d42015-12-06 16:56:27 -08001010 if (!params.freestash.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -07001011 FreeStash(params.stashbase, params.freestash);
Tao Baobaad2d42015-12-06 16:56:27 -08001012 params.freestash.clear();
Sami Tolvanen90221202014-12-09 16:39:47 +00001013 }
1014
Tao Bao0940fe12015-08-27 16:41:21 -07001015 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001016
Tao Bao0940fe12015-08-27 16:41:21 -07001017 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001018}
1019
Tao Bao0940fe12015-08-27 16:41:21 -07001020static int PerformCommandStash(CommandParameters& params) {
Tao Baobaad2d42015-12-06 16:56:27 -08001021 return SaveStash(params, params.stashbase, params.buffer, params.fd,
Tao Bao612336d2015-08-27 16:41:21 -07001022 (params.version >= 3));
Sami Tolvanen90221202014-12-09 16:39:47 +00001023}
1024
Tao Bao0940fe12015-08-27 16:41:21 -07001025static int PerformCommandFree(CommandParameters& params) {
Tao Baobaad2d42015-12-06 16:56:27 -08001026 // <stash_id>
1027 if (params.cpos >= params.tokens.size()) {
1028 fprintf(stderr, "missing stash id in free command\n");
1029 return -1;
1030 }
1031
Tao Bao0940fe12015-08-27 16:41:21 -07001032 if (params.createdstash || params.canwrite) {
Tao Baobaad2d42015-12-06 16:56:27 -08001033 return FreeStash(params.stashbase, params.tokens[params.cpos++]);
Sami Tolvanen90221202014-12-09 16:39:47 +00001034 }
1035
1036 return 0;
1037}
1038
Tao Bao0940fe12015-08-27 16:41:21 -07001039static int PerformCommandZero(CommandParameters& params) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001040
Tao Baobaad2d42015-12-06 16:56:27 -08001041 if (params.cpos >= params.tokens.size()) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001042 fprintf(stderr, "missing target blocks for zero\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001043 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001044 }
1045
Tao Bao0940fe12015-08-27 16:41:21 -07001046 RangeSet tgt;
Tao Baobaad2d42015-12-06 16:56:27 -08001047 parse_range(params.tokens[params.cpos++], tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001048
Tao Bao0940fe12015-08-27 16:41:21 -07001049 fprintf(stderr, " zeroing %zu blocks\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001050
Tao Bao612336d2015-08-27 16:41:21 -07001051 allocate(BLOCKSIZE, params.buffer);
1052 memset(params.buffer.data(), 0, BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +00001053
Tao Bao0940fe12015-08-27 16:41:21 -07001054 if (params.canwrite) {
1055 for (size_t i = 0; i < tgt.count; ++i) {
1056 if (!check_lseek(params.fd, (off64_t) tgt.pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
1057 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001058 }
1059
Tao Bao0940fe12015-08-27 16:41:21 -07001060 for (size_t j = tgt.pos[i * 2]; j < tgt.pos[i * 2 + 1]; ++j) {
1061 if (write_all(params.fd, params.buffer, BLOCKSIZE) == -1) {
1062 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001063 }
1064 }
1065 }
1066 }
1067
Tao Bao0940fe12015-08-27 16:41:21 -07001068 if (params.cmdname[0] == 'z') {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001069 // Update only for the zero command, as the erase command will call
1070 // this if DEBUG_ERASE is defined.
Tao Bao0940fe12015-08-27 16:41:21 -07001071 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001072 }
1073
Tao Bao0940fe12015-08-27 16:41:21 -07001074 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001075}
1076
Tao Bao0940fe12015-08-27 16:41:21 -07001077static int PerformCommandNew(CommandParameters& params) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001078
Tao Baobaad2d42015-12-06 16:56:27 -08001079 if (params.cpos >= params.tokens.size()) {
1080 fprintf(stderr, "missing target blocks for new\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001081 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001082 }
1083
Tao Bao0940fe12015-08-27 16:41:21 -07001084 RangeSet tgt;
Tao Baobaad2d42015-12-06 16:56:27 -08001085 parse_range(params.tokens[params.cpos++], tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001086
Tao Bao0940fe12015-08-27 16:41:21 -07001087 if (params.canwrite) {
1088 fprintf(stderr, " writing %zu blocks of new data\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001089
Tao Bao0940fe12015-08-27 16:41:21 -07001090 RangeSinkState rss(tgt);
1091 rss.fd = params.fd;
Sami Tolvanen90221202014-12-09 16:39:47 +00001092 rss.p_block = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001093 rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001094
Tao Bao0940fe12015-08-27 16:41:21 -07001095 if (!check_lseek(params.fd, (off64_t) tgt.pos[0] * BLOCKSIZE, SEEK_SET)) {
1096 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001097 }
1098
Tao Bao0940fe12015-08-27 16:41:21 -07001099 pthread_mutex_lock(&params.nti.mu);
1100 params.nti.rss = &rss;
1101 pthread_cond_broadcast(&params.nti.cv);
Sami Tolvanen90221202014-12-09 16:39:47 +00001102
Tao Bao0940fe12015-08-27 16:41:21 -07001103 while (params.nti.rss) {
1104 pthread_cond_wait(&params.nti.cv, &params.nti.mu);
Sami Tolvanen90221202014-12-09 16:39:47 +00001105 }
1106
Tao Bao0940fe12015-08-27 16:41:21 -07001107 pthread_mutex_unlock(&params.nti.mu);
Sami Tolvanen90221202014-12-09 16:39:47 +00001108 }
1109
Tao Bao0940fe12015-08-27 16:41:21 -07001110 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001111
Tao Bao0940fe12015-08-27 16:41:21 -07001112 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001113}
1114
Tao Bao0940fe12015-08-27 16:41:21 -07001115static int PerformCommandDiff(CommandParameters& params) {
Tao Bao0940fe12015-08-27 16:41:21 -07001116
Tao Baobaad2d42015-12-06 16:56:27 -08001117 // <offset> <length>
1118 if (params.cpos + 1 >= params.tokens.size()) {
1119 fprintf(stderr, "missing patch offset or length for %s\n", params.cmdname);
Tao Bao0940fe12015-08-27 16:41:21 -07001120 return -1;
1121 }
1122
Tao Baobaad2d42015-12-06 16:56:27 -08001123 size_t offset;
1124 if (!android::base::ParseUint(params.tokens[params.cpos++].c_str(), &offset)) {
1125 fprintf(stderr, "invalid patch offset\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001126 return -1;
1127 }
1128
Tao Baobaad2d42015-12-06 16:56:27 -08001129 size_t len;
1130 if (!android::base::ParseUint(params.tokens[params.cpos++].c_str(), &len)) {
1131 fprintf(stderr, "invalid patch offset\n");
1132 return -1;
1133 }
Tao Bao0940fe12015-08-27 16:41:21 -07001134
Tao Bao612336d2015-08-27 16:41:21 -07001135 RangeSet tgt;
Tao Bao0940fe12015-08-27 16:41:21 -07001136 size_t blocks = 0;
Tao Bao612336d2015-08-27 16:41:21 -07001137 bool overlap = false;
Sami Tolvanen90221202014-12-09 16:39:47 +00001138 int status = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001139 if (params.version == 1) {
Tao Baobaad2d42015-12-06 16:56:27 -08001140 status = LoadSrcTgtVersion1(params, tgt, blocks, params.buffer, params.fd);
Tao Bao0940fe12015-08-27 16:41:21 -07001141 } else if (params.version == 2) {
Tao Baobaad2d42015-12-06 16:56:27 -08001142 status = LoadSrcTgtVersion2(params, tgt, blocks, params.buffer, params.fd,
Tao Bao612336d2015-08-27 16:41:21 -07001143 params.stashbase, nullptr);
Tao Bao0940fe12015-08-27 16:41:21 -07001144 } else if (params.version >= 3) {
Tao Bao34847b22015-09-08 11:05:49 -07001145 status = LoadSrcTgtVersion3(params, tgt, blocks, false, overlap);
Sami Tolvanen90221202014-12-09 16:39:47 +00001146 }
1147
1148 if (status == -1) {
1149 fprintf(stderr, "failed to read blocks for diff\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001150 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001151 }
1152
1153 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001154 params.foundwrites = true;
1155 } else if (params.foundwrites) {
1156 fprintf(stderr, "warning: commands executed out of order [%s]\n", params.cmdname);
Sami Tolvanen90221202014-12-09 16:39:47 +00001157 }
1158
Tao Bao0940fe12015-08-27 16:41:21 -07001159 if (params.canwrite) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001160 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001161 fprintf(stderr, "patching %zu blocks to %zu\n", blocks, tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001162
Tao Bao0940fe12015-08-27 16:41:21 -07001163 Value patch_value;
Sami Tolvanen90221202014-12-09 16:39:47 +00001164 patch_value.type = VAL_BLOB;
1165 patch_value.size = len;
Tao Bao0940fe12015-08-27 16:41:21 -07001166 patch_value.data = (char*) (params.patch_start + offset);
Sami Tolvanen90221202014-12-09 16:39:47 +00001167
Tao Bao0940fe12015-08-27 16:41:21 -07001168 RangeSinkState rss(tgt);
1169 rss.fd = params.fd;
Sami Tolvanen90221202014-12-09 16:39:47 +00001170 rss.p_block = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001171 rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001172
Tao Bao0940fe12015-08-27 16:41:21 -07001173 if (!check_lseek(params.fd, (off64_t) tgt.pos[0] * BLOCKSIZE, SEEK_SET)) {
1174 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001175 }
1176
Tao Bao0940fe12015-08-27 16:41:21 -07001177 if (params.cmdname[0] == 'i') { // imgdiff
Tao Bao612336d2015-08-27 16:41:21 -07001178 ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value,
Tao Bao0940fe12015-08-27 16:41:21 -07001179 &RangeSinkWrite, &rss, nullptr, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +00001180 } else {
Tao Bao612336d2015-08-27 16:41:21 -07001181 ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value, 0,
1182 &RangeSinkWrite, &rss, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +00001183 }
1184
1185 // We expect the output of the patcher to fill the tgt ranges exactly.
Tao Bao0940fe12015-08-27 16:41:21 -07001186 if (rss.p_block != tgt.count || rss.p_remain != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001187 fprintf(stderr, "range sink underrun?\n");
1188 }
1189 } else {
Tao Bao0940fe12015-08-27 16:41:21 -07001190 fprintf(stderr, "skipping %zu blocks already patched to %zu [%s]\n",
Tao Baobaad2d42015-12-06 16:56:27 -08001191 blocks, tgt.size, params.cmdline);
Sami Tolvanen90221202014-12-09 16:39:47 +00001192 }
1193 }
1194
Tao Baobaad2d42015-12-06 16:56:27 -08001195 if (!params.freestash.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -07001196 FreeStash(params.stashbase, params.freestash);
Tao Baobaad2d42015-12-06 16:56:27 -08001197 params.freestash.clear();
Sami Tolvanen90221202014-12-09 16:39:47 +00001198 }
1199
Tao Bao0940fe12015-08-27 16:41:21 -07001200 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001201
Tao Bao0940fe12015-08-27 16:41:21 -07001202 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001203}
1204
Tao Bao0940fe12015-08-27 16:41:21 -07001205static int PerformCommandErase(CommandParameters& params) {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001206 if (DEBUG_ERASE) {
1207 return PerformCommandZero(params);
Sami Tolvanen90221202014-12-09 16:39:47 +00001208 }
1209
Tao Bao0940fe12015-08-27 16:41:21 -07001210 struct stat sb;
1211 if (fstat(params.fd, &sb) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001212 fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -07001213 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001214 }
1215
Tao Bao0940fe12015-08-27 16:41:21 -07001216 if (!S_ISBLK(sb.st_mode)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001217 fprintf(stderr, "not a block device; skipping erase\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001218 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001219 }
1220
Tao Baobaad2d42015-12-06 16:56:27 -08001221 if (params.cpos >= params.tokens.size()) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001222 fprintf(stderr, "missing target blocks for erase\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001223 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001224 }
1225
Tao Bao0940fe12015-08-27 16:41:21 -07001226 RangeSet tgt;
Tao Baobaad2d42015-12-06 16:56:27 -08001227 parse_range(params.tokens[params.cpos++], tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001228
Tao Bao0940fe12015-08-27 16:41:21 -07001229 if (params.canwrite) {
1230 fprintf(stderr, " erasing %zu blocks\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001231
Tao Bao0940fe12015-08-27 16:41:21 -07001232 for (size_t i = 0; i < tgt.count; ++i) {
1233 uint64_t blocks[2];
Sami Tolvanen90221202014-12-09 16:39:47 +00001234 // offset in bytes
Tao Bao0940fe12015-08-27 16:41:21 -07001235 blocks[0] = tgt.pos[i * 2] * (uint64_t) BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001236 // length in bytes
Tao Bao0940fe12015-08-27 16:41:21 -07001237 blocks[1] = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * (uint64_t) BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001238
Tao Bao0940fe12015-08-27 16:41:21 -07001239 if (ioctl(params.fd, BLKDISCARD, &blocks) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001240 fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -07001241 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001242 }
1243 }
1244 }
1245
Tao Bao0940fe12015-08-27 16:41:21 -07001246 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001247}
1248
1249// Definitions for transfer list command functions
Tao Bao0940fe12015-08-27 16:41:21 -07001250typedef int (*CommandFunction)(CommandParameters&);
Sami Tolvanen90221202014-12-09 16:39:47 +00001251
Tao Bao612336d2015-08-27 16:41:21 -07001252struct Command {
Sami Tolvanen90221202014-12-09 16:39:47 +00001253 const char* name;
1254 CommandFunction f;
Tao Bao612336d2015-08-27 16:41:21 -07001255};
Sami Tolvanen90221202014-12-09 16:39:47 +00001256
1257// CompareCommands and CompareCommandNames are for the hash table
1258
1259static int CompareCommands(const void* c1, const void* c2) {
1260 return strcmp(((const Command*) c1)->name, ((const Command*) c2)->name);
1261}
1262
1263static int CompareCommandNames(const void* c1, const void* c2) {
1264 return strcmp(((const Command*) c1)->name, (const char*) c2);
1265}
1266
1267// HashString is used to hash command names for the hash table
1268
1269static unsigned int HashString(const char *s) {
1270 unsigned int hash = 0;
1271 if (s) {
1272 while (*s) {
1273 hash = hash * 33 + *s++;
1274 }
1275 }
1276 return hash;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001277}
1278
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001279// args:
1280// - block device (or file) to modify in-place
1281// - transfer list (blob)
1282// - new data stream (filename within package.zip)
1283// - patch stream (filename within package.zip, must be uncompressed)
1284
Tao Bao0940fe12015-08-27 16:41:21 -07001285static Value* PerformBlockImageUpdate(const char* name, State* state, int /* argc */, Expr* argv[],
1286 const Command* commands, size_t cmdcount, bool dryrun) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001287 CommandParameters params;
Sami Tolvanen90221202014-12-09 16:39:47 +00001288 memset(&params, 0, sizeof(params));
1289 params.canwrite = !dryrun;
1290
1291 fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001292
Tao Bao612336d2015-08-27 16:41:21 -07001293 Value* blockdev_filename = nullptr;
1294 Value* transfer_list_value = nullptr;
1295 Value* new_data_fn = nullptr;
1296 Value* patch_data_fn = nullptr;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001297 if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value,
Sami Tolvanen90221202014-12-09 16:39:47 +00001298 &new_data_fn, &patch_data_fn) < 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001299 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001300 }
Tao Bao612336d2015-08-27 16:41:21 -07001301 std::unique_ptr<Value, decltype(&FreeValue)> blockdev_filename_holder(blockdev_filename,
1302 FreeValue);
1303 std::unique_ptr<Value, decltype(&FreeValue)> transfer_list_value_holder(transfer_list_value,
1304 FreeValue);
1305 std::unique_ptr<Value, decltype(&FreeValue)> new_data_fn_holder(new_data_fn, FreeValue);
1306 std::unique_ptr<Value, decltype(&FreeValue)> patch_data_fn_holder(patch_data_fn, FreeValue);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001307
1308 if (blockdev_filename->type != VAL_STRING) {
1309 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001310 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001311 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001312 if (transfer_list_value->type != VAL_BLOB) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001313 ErrorAbort(state, "transfer_list argument to %s must be blob", name);
Tao Bao612336d2015-08-27 16:41:21 -07001314 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001315 }
1316 if (new_data_fn->type != VAL_STRING) {
1317 ErrorAbort(state, "new_data_fn argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001318 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001319 }
1320 if (patch_data_fn->type != VAL_STRING) {
1321 ErrorAbort(state, "patch_data_fn argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001322 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001323 }
1324
Tao Bao612336d2015-08-27 16:41:21 -07001325 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001326
Tao Bao0940fe12015-08-27 16:41:21 -07001327 if (ui == nullptr) {
Tao Bao612336d2015-08-27 16:41:21 -07001328 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001329 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001330
Tao Bao612336d2015-08-27 16:41:21 -07001331 FILE* cmd_pipe = ui->cmd_pipe;
1332 ZipArchive* za = ui->package_zip;
Sami Tolvanen90221202014-12-09 16:39:47 +00001333
Tao Bao0940fe12015-08-27 16:41:21 -07001334 if (cmd_pipe == nullptr || za == nullptr) {
Tao Bao612336d2015-08-27 16:41:21 -07001335 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001336 }
1337
Tao Bao612336d2015-08-27 16:41:21 -07001338 const ZipEntry* patch_entry = mzFindZipEntry(za, patch_data_fn->data);
Tao Bao0940fe12015-08-27 16:41:21 -07001339 if (patch_entry == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001340 fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data);
Tao Bao612336d2015-08-27 16:41:21 -07001341 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001342 }
1343
Sami Tolvanen90221202014-12-09 16:39:47 +00001344 params.patch_start = ui->package_zip_addr + mzGetZipEntryOffset(patch_entry);
Tao Bao612336d2015-08-27 16:41:21 -07001345 const ZipEntry* new_entry = mzFindZipEntry(za, new_data_fn->data);
Tao Bao0940fe12015-08-27 16:41:21 -07001346 if (new_entry == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001347 fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data);
Tao Bao612336d2015-08-27 16:41:21 -07001348 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001349 }
1350
Sami Tolvanen90221202014-12-09 16:39:47 +00001351 params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR));
Tao Bao612336d2015-08-27 16:41:21 -07001352 unique_fd fd_holder(params.fd);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001353
Sami Tolvanen90221202014-12-09 16:39:47 +00001354 if (params.fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001355 fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data, strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001356 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001357 }
1358
Sami Tolvanen90221202014-12-09 16:39:47 +00001359 if (params.canwrite) {
1360 params.nti.za = za;
1361 params.nti.entry = new_entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001362
Tao Bao0940fe12015-08-27 16:41:21 -07001363 pthread_mutex_init(&params.nti.mu, nullptr);
1364 pthread_cond_init(&params.nti.cv, nullptr);
Tao Bao612336d2015-08-27 16:41:21 -07001365 pthread_attr_t attr;
Sami Tolvanen90221202014-12-09 16:39:47 +00001366 pthread_attr_init(&attr);
1367 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1368
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001369 int error = pthread_create(&params.thread, &attr, unzip_new_data, &params.nti);
1370 if (error != 0) {
1371 fprintf(stderr, "pthread_create failed: %s\n", strerror(error));
Tao Bao612336d2015-08-27 16:41:21 -07001372 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001373 }
1374 }
1375
Tao Baobaad2d42015-12-06 16:56:27 -08001376 // Copy all the lines in transfer_list_value into std::string for
1377 // processing.
Tao Bao612336d2015-08-27 16:41:21 -07001378 const std::string transfer_list(transfer_list_value->data, transfer_list_value->size);
1379 std::vector<std::string> lines = android::base::Split(transfer_list, "\n");
Tao Baobaad2d42015-12-06 16:56:27 -08001380 if (lines.size() < 2) {
1381 ErrorAbort(state, "too few lines in the transfer list [%zd]\n", lines.size());
1382 return StringValue(strdup(""));
1383 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001384
Sami Tolvanen90221202014-12-09 16:39:47 +00001385 // First line in transfer list is the version number
Tao Bao1fdec862015-10-21 14:57:44 -07001386 if (!android::base::ParseInt(lines[0].c_str(), &params.version, 1, 4)) {
Tao Bao612336d2015-08-27 16:41:21 -07001387 fprintf(stderr, "unexpected transfer list version [%s]\n", lines[0].c_str());
1388 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001389 }
1390
Sami Tolvanen90221202014-12-09 16:39:47 +00001391 fprintf(stderr, "blockimg version is %d\n", params.version);
1392
1393 // Second line in transfer list is the total number of blocks we expect to write
Tao Baob15fd222015-09-24 11:10:51 -07001394 int total_blocks;
1395 if (!android::base::ParseInt(lines[1].c_str(), &total_blocks, 0)) {
Tao Bao612336d2015-08-27 16:41:21 -07001396 ErrorAbort(state, "unexpected block count [%s]\n", lines[1].c_str());
1397 return StringValue(strdup(""));
Tao Baob15fd222015-09-24 11:10:51 -07001398 }
1399
1400 if (total_blocks == 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001401 return StringValue(strdup("t"));
Sami Tolvanen90221202014-12-09 16:39:47 +00001402 }
1403
Tao Bao612336d2015-08-27 16:41:21 -07001404 size_t start = 2;
Sami Tolvanen90221202014-12-09 16:39:47 +00001405 if (params.version >= 2) {
Tao Baobaad2d42015-12-06 16:56:27 -08001406 if (lines.size() < 4) {
1407 ErrorAbort(state, "too few lines in the transfer list [%zu]\n", lines.size());
1408 return StringValue(strdup(""));
1409 }
1410
Sami Tolvanen90221202014-12-09 16:39:47 +00001411 // Third line is how many stash entries are needed simultaneously
Tao Bao612336d2015-08-27 16:41:21 -07001412 fprintf(stderr, "maximum stash entries %s\n", lines[2].c_str());
Doug Zongker52ae67d2014-09-08 12:22:09 -07001413
Sami Tolvanen90221202014-12-09 16:39:47 +00001414 // Fourth line is the maximum number of blocks that will be stashed simultaneously
Tao Baob15fd222015-09-24 11:10:51 -07001415 int stash_max_blocks;
1416 if (!android::base::ParseInt(lines[3].c_str(), &stash_max_blocks, 0)) {
Tao Bao612336d2015-08-27 16:41:21 -07001417 ErrorAbort(state, "unexpected maximum stash blocks [%s]\n", lines[3].c_str());
1418 return StringValue(strdup(""));
Doug Zongker52ae67d2014-09-08 12:22:09 -07001419 }
1420
Tao Baob15fd222015-09-24 11:10:51 -07001421 int res = CreateStash(state, stash_max_blocks, blockdev_filename->data, params.stashbase);
Tao Baob15fd222015-09-24 11:10:51 -07001422 if (res == -1) {
1423 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001424 }
Tao Bao612336d2015-08-27 16:41:21 -07001425
Tao Baob15fd222015-09-24 11:10:51 -07001426 params.createdstash = res;
1427
Tao Bao612336d2015-08-27 16:41:21 -07001428 start += 2;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001429 }
1430
Sami Tolvanen90221202014-12-09 16:39:47 +00001431 // Build a hash table of the available commands
Tao Bao612336d2015-08-27 16:41:21 -07001432 HashTable* cmdht = mzHashTableCreate(cmdcount, nullptr);
1433 std::unique_ptr<HashTable, decltype(&mzHashTableFree)> cmdht_holder(cmdht, mzHashTableFree);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001434
Tao Bao0940fe12015-08-27 16:41:21 -07001435 for (size_t i = 0; i < cmdcount; ++i) {
1436 unsigned int cmdhash = HashString(commands[i].name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001437 mzHashTableLookup(cmdht, cmdhash, (void*) &commands[i], CompareCommands, true);
1438 }
1439
Tao Bao612336d2015-08-27 16:41:21 -07001440 int rc = -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001441
Tao Bao612336d2015-08-27 16:41:21 -07001442 // Subsequent lines are all individual transfer commands
1443 for (auto it = lines.cbegin() + start; it != lines.cend(); it++) {
1444 const std::string& line_str(*it);
Tao Bao6a47dff2015-09-25 17:12:28 -07001445 if (line_str.empty()) {
1446 continue;
1447 }
1448
Tao Baobaad2d42015-12-06 16:56:27 -08001449 params.tokens = android::base::Split(line_str, " ");
1450 params.cpos = 0;
1451 params.cmdname = params.tokens[params.cpos++].c_str();
1452 params.cmdline = line_str.c_str();
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001453
Tao Bao0940fe12015-08-27 16:41:21 -07001454 unsigned int cmdhash = HashString(params.cmdname);
Tao Bao612336d2015-08-27 16:41:21 -07001455 const Command* cmd = reinterpret_cast<const Command*>(mzHashTableLookup(cmdht, cmdhash,
Tao Baobaad2d42015-12-06 16:56:27 -08001456 const_cast<char*>(params.cmdname), CompareCommandNames,
1457 false));
Doug Zongker52ae67d2014-09-08 12:22:09 -07001458
Tao Bao0940fe12015-08-27 16:41:21 -07001459 if (cmd == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001460 fprintf(stderr, "unexpected command [%s]\n", params.cmdname);
1461 goto pbiudone;
1462 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001463
Tao Bao0940fe12015-08-27 16:41:21 -07001464 if (cmd->f != nullptr && cmd->f(params) == -1) {
Tao Bao612336d2015-08-27 16:41:21 -07001465 fprintf(stderr, "failed to execute command [%s]\n", line_str.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +00001466 goto pbiudone;
1467 }
1468
Sami Tolvanen90221202014-12-09 16:39:47 +00001469 if (params.canwrite) {
Tao Bao187efff2015-07-27 14:07:08 -07001470 if (fsync(params.fd) == -1) {
1471 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
1472 goto pbiudone;
1473 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001474 fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001475 fflush(cmd_pipe);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001476 }
1477 }
1478
Sami Tolvanen90221202014-12-09 16:39:47 +00001479 if (params.canwrite) {
Tao Bao0940fe12015-08-27 16:41:21 -07001480 pthread_join(params.thread, nullptr);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001481
Tao Bao0940fe12015-08-27 16:41:21 -07001482 fprintf(stderr, "wrote %zu blocks; expected %d\n", params.written, total_blocks);
Tao Bao612336d2015-08-27 16:41:21 -07001483 fprintf(stderr, "max alloc needed was %zu\n", params.buffer.size());
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001484
Sami Tolvanen90221202014-12-09 16:39:47 +00001485 // Delete stash only after successfully completing the update, as it
1486 // may contain blocks needed to complete the update later.
1487 DeleteStash(params.stashbase);
1488 } else {
1489 fprintf(stderr, "verified partition contents; update may be resumed\n");
1490 }
1491
1492 rc = 0;
1493
1494pbiudone:
Tao Baobaad2d42015-12-06 16:56:27 -08001495 if (fsync(params.fd) == -1) {
1496 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001497 }
Tao Baobaad2d42015-12-06 16:56:27 -08001498 // params.fd will be automatically closed because of the fd_holder above.
Sami Tolvanen90221202014-12-09 16:39:47 +00001499
Sami Tolvanen90221202014-12-09 16:39:47 +00001500 // Only delete the stash if the update cannot be resumed, or it's
1501 // a verification run and we created the stash.
1502 if (params.isunresumable || (!params.canwrite && params.createdstash)) {
1503 DeleteStash(params.stashbase);
1504 }
1505
Sami Tolvanen90221202014-12-09 16:39:47 +00001506 return StringValue(rc == 0 ? strdup("t") : strdup(""));
1507}
1508
1509// The transfer list is a text file containing commands to
1510// transfer data from one place to another on the target
1511// partition. We parse it and execute the commands in order:
1512//
1513// zero [rangeset]
1514// - fill the indicated blocks with zeros
1515//
1516// new [rangeset]
1517// - fill the blocks with data read from the new_data file
1518//
1519// erase [rangeset]
1520// - mark the given blocks as empty
1521//
1522// move <...>
1523// bsdiff <patchstart> <patchlen> <...>
1524// imgdiff <patchstart> <patchlen> <...>
1525// - read the source blocks, apply a patch (or not in the
1526// case of move), write result to target blocks. bsdiff or
1527// imgdiff specifies the type of patch; move means no patch
1528// at all.
1529//
1530// The format of <...> differs between versions 1 and 2;
1531// see the LoadSrcTgtVersion{1,2}() functions for a
1532// description of what's expected.
1533//
1534// stash <stash_id> <src_range>
1535// - (version 2+ only) load the given source range and stash
1536// the data in the given slot of the stash table.
1537//
Tao Baobaad2d42015-12-06 16:56:27 -08001538// free <stash_id>
1539// - (version 3+ only) free the given stash data.
1540//
Sami Tolvanen90221202014-12-09 16:39:47 +00001541// The creator of the transfer list will guarantee that no block
1542// is read (ie, used as the source for a patch or move) after it
1543// has been written.
1544//
1545// In version 2, the creator will guarantee that a given stash is
1546// loaded (with a stash command) before it's used in a
1547// move/bsdiff/imgdiff command.
1548//
1549// Within one command the source and target ranges may overlap so
1550// in general we need to read the entire source into memory before
1551// writing anything to the target blocks.
1552//
1553// All the patch data is concatenated into one patch_data file in
1554// the update package. It must be stored uncompressed because we
1555// memory-map it in directly from the archive. (Since patches are
1556// already compressed, we lose very little by not compressing
1557// their concatenation.)
1558//
1559// In version 3, commands that read data from the partition (i.e.
1560// move/bsdiff/imgdiff/stash) have one or more additional hashes
1561// before the range parameters, which are used to check if the
1562// command has already been completed and verify the integrity of
1563// the source data.
1564
1565Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao0940fe12015-08-27 16:41:21 -07001566 // Commands which are not tested are set to nullptr to skip them completely
Sami Tolvanen90221202014-12-09 16:39:47 +00001567 const Command commands[] = {
1568 { "bsdiff", PerformCommandDiff },
Tao Bao0940fe12015-08-27 16:41:21 -07001569 { "erase", nullptr },
Sami Tolvanen90221202014-12-09 16:39:47 +00001570 { "free", PerformCommandFree },
1571 { "imgdiff", PerformCommandDiff },
1572 { "move", PerformCommandMove },
Tao Bao0940fe12015-08-27 16:41:21 -07001573 { "new", nullptr },
Sami Tolvanen90221202014-12-09 16:39:47 +00001574 { "stash", PerformCommandStash },
Tao Bao0940fe12015-08-27 16:41:21 -07001575 { "zero", nullptr }
Sami Tolvanen90221202014-12-09 16:39:47 +00001576 };
1577
1578 // Perform a dry run without writing to test if an update can proceed
1579 return PerformBlockImageUpdate(name, state, argc, argv, commands,
Tao Baoe6aa3322015-08-05 15:20:27 -07001580 sizeof(commands) / sizeof(commands[0]), true);
Sami Tolvanen90221202014-12-09 16:39:47 +00001581}
1582
1583Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) {
1584 const Command commands[] = {
1585 { "bsdiff", PerformCommandDiff },
1586 { "erase", PerformCommandErase },
1587 { "free", PerformCommandFree },
1588 { "imgdiff", PerformCommandDiff },
1589 { "move", PerformCommandMove },
1590 { "new", PerformCommandNew },
1591 { "stash", PerformCommandStash },
1592 { "zero", PerformCommandZero }
1593 };
1594
1595 return PerformBlockImageUpdate(name, state, argc, argv, commands,
Tao Baoe6aa3322015-08-05 15:20:27 -07001596 sizeof(commands) / sizeof(commands[0]), false);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001597}
1598
Tao Bao0940fe12015-08-27 16:41:21 -07001599Value* RangeSha1Fn(const char* name, State* state, int /* argc */, Expr* argv[]) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001600 Value* blockdev_filename;
1601 Value* ranges;
Tao Bao0940fe12015-08-27 16:41:21 -07001602
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001603 if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001604 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001605 }
Tao Bao612336d2015-08-27 16:41:21 -07001606 std::unique_ptr<Value, decltype(&FreeValue)> ranges_holder(ranges, FreeValue);
1607 std::unique_ptr<Value, decltype(&FreeValue)> blockdev_filename_holder(blockdev_filename,
1608 FreeValue);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001609
1610 if (blockdev_filename->type != VAL_STRING) {
1611 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001612 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001613 }
1614 if (ranges->type != VAL_STRING) {
1615 ErrorAbort(state, "ranges argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001616 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001617 }
1618
Tao Bao612336d2015-08-27 16:41:21 -07001619 int fd = open(blockdev_filename->data, O_RDWR);
1620 unique_fd fd_holder(fd);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001621 if (fd < 0) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001622 ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001623 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001624 }
1625
Tao Bao612336d2015-08-27 16:41:21 -07001626 RangeSet rs;
Tao Bao0940fe12015-08-27 16:41:21 -07001627 parse_range(ranges->data, rs);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001628
1629 SHA_CTX ctx;
1630 SHA_init(&ctx);
1631
Tao Bao612336d2015-08-27 16:41:21 -07001632 std::vector<uint8_t> buffer(BLOCKSIZE);
Tao Bao0940fe12015-08-27 16:41:21 -07001633 for (size_t i = 0; i < rs.count; ++i) {
1634 if (!check_lseek(fd, (off64_t)rs.pos[i*2] * BLOCKSIZE, SEEK_SET)) {
Tao Bao612336d2015-08-27 16:41:21 -07001635 ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data, strerror(errno));
1636 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001637 }
1638
Tao Bao0940fe12015-08-27 16:41:21 -07001639 for (size_t j = rs.pos[i*2]; j < rs.pos[i*2+1]; ++j) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001640 if (read_all(fd, buffer, BLOCKSIZE) == -1) {
1641 ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data,
Tao Bao0940fe12015-08-27 16:41:21 -07001642 strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001643 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001644 }
1645
Tao Bao612336d2015-08-27 16:41:21 -07001646 SHA_update(&ctx, buffer.data(), BLOCKSIZE);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001647 }
1648 }
Tao Bao612336d2015-08-27 16:41:21 -07001649 const uint8_t* digest = SHA_final(&ctx);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001650
Tao Bao612336d2015-08-27 16:41:21 -07001651 return StringValue(strdup(print_sha1(digest).c_str()));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001652}
1653
Tianjie Xu57bed6d2015-12-15 11:47:30 -08001654// This function checks if a device has been remounted R/W prior to an incremental
1655// OTA update. This is an common cause of update abortion. The function reads the
1656// 1st block of each partition and check for mounting time/count. It return string "t"
1657// if executes successfully and an empty string otherwise.
1658
1659Value* CheckFirstBlockFn(const char* name, State* state, int argc, Expr* argv[]) {
1660 Value* arg_filename;
1661
1662 if (ReadValueArgs(state, argv, 1, &arg_filename) < 0) {
1663 return nullptr;
1664 }
1665 std::unique_ptr<Value, decltype(&FreeValue)> filename(arg_filename, FreeValue);
1666
1667 if (filename->type != VAL_STRING) {
1668 ErrorAbort(state, "filename argument to %s must be string", name);
1669 return StringValue(strdup(""));
1670 }
1671
1672 int fd = open(arg_filename->data, O_RDONLY);
1673 unique_fd fd_holder(fd);
1674 if (fd == -1) {
1675 ErrorAbort(state, "open \"%s\" failed: %s", arg_filename->data, strerror(errno));
1676 return StringValue(strdup(""));
1677 }
1678
1679 RangeSet blk0 {1 /*count*/, 1/*size*/, std::vector<size_t> {0, 1}/*position*/};
1680 std::vector<uint8_t> block0_buffer(BLOCKSIZE);
1681
1682 if (ReadBlocks(blk0, block0_buffer, fd) == -1) {
1683 ErrorAbort(state, "failed to read %s: %s", arg_filename->data,
1684 strerror(errno));
1685 return StringValue(strdup(""));
1686 }
1687
1688 // https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout
1689 // Super block starts from block 0, offset 0x400
1690 // 0x2C: len32 Mount time
1691 // 0x30: len32 Write time
1692 // 0x34: len16 Number of mounts since the last fsck
1693 // 0x38: len16 Magic signature 0xEF53
1694
1695 time_t mount_time = *reinterpret_cast<uint32_t*>(&block0_buffer[0x400+0x2C]);
1696 uint16_t mount_count = *reinterpret_cast<uint16_t*>(&block0_buffer[0x400+0x34]);
1697
1698 if (mount_count > 0) {
1699 uiPrintf(state, "Device was remounted R/W %d times\n", mount_count);
1700 uiPrintf(state, "Last remount happened on %s", ctime(&mount_time));
1701 }
1702
1703 return StringValue(strdup("t"));
1704}
1705
1706
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001707Value* BlockImageRecoverFn(const char* name, State* state, int argc, Expr* argv[]) {
1708 Value* arg_filename;
1709 Value* arg_ranges;
1710
1711 if (ReadValueArgs(state, argv, 2, &arg_filename, &arg_ranges) < 0) {
1712 return NULL;
1713 }
1714
1715 std::unique_ptr<Value, decltype(&FreeValue)> filename(arg_filename, FreeValue);
1716 std::unique_ptr<Value, decltype(&FreeValue)> ranges(arg_ranges, FreeValue);
1717
1718 if (filename->type != VAL_STRING) {
1719 ErrorAbort(state, "filename argument to %s must be string", name);
1720 return StringValue(strdup(""));
1721 }
1722 if (ranges->type != VAL_STRING) {
1723 ErrorAbort(state, "ranges argument to %s must be string", name);
1724 return StringValue(strdup(""));
1725 }
1726
Tianjie Xu3b010bc2015-12-09 15:29:45 -08001727 // Output notice to log when recover is attempted
1728 fprintf(stderr, "%s image corrupted, attempting to recover...\n", filename->data);
1729
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001730 // When opened with O_RDWR, libfec rewrites corrupted blocks when they are read
1731 fec::io fh(filename->data, O_RDWR);
1732
1733 if (!fh) {
1734 ErrorAbort(state, "fec_open \"%s\" failed: %s", filename->data, strerror(errno));
1735 return StringValue(strdup(""));
1736 }
1737
1738 if (!fh.has_ecc() || !fh.has_verity()) {
1739 ErrorAbort(state, "unable to use metadata to correct errors");
1740 return StringValue(strdup(""));
1741 }
1742
1743 fec_status status;
1744
1745 if (!fh.get_status(status)) {
1746 ErrorAbort(state, "failed to read FEC status");
1747 return StringValue(strdup(""));
1748 }
1749
1750 RangeSet rs;
1751 parse_range(ranges->data, rs);
1752
1753 uint8_t buffer[BLOCKSIZE];
1754
1755 for (size_t i = 0; i < rs.count; ++i) {
1756 for (size_t j = rs.pos[i * 2]; j < rs.pos[i * 2 + 1]; ++j) {
1757 // Stay within the data area, libfec validates and corrects metadata
1758 if (status.data_size <= (uint64_t)j * BLOCKSIZE) {
1759 continue;
1760 }
1761
1762 if (fh.pread(buffer, BLOCKSIZE, (off64_t)j * BLOCKSIZE) != BLOCKSIZE) {
Tao Baobaad2d42015-12-06 16:56:27 -08001763 ErrorAbort(state, "failed to recover %s (block %zu): %s", filename->data,
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001764 j, strerror(errno));
1765 return StringValue(strdup(""));
1766 }
1767
1768 // If we want to be able to recover from a situation where rewriting a corrected
1769 // block doesn't guarantee the same data will be returned when re-read later, we
1770 // can save a copy of corrected blocks to /cache. Note:
1771 //
1772 // 1. Maximum space required from /cache is the same as the maximum number of
1773 // corrupted blocks we can correct. For RS(255, 253) and a 2 GiB partition,
1774 // this would be ~16 MiB, for example.
1775 //
1776 // 2. To find out if this block was corrupted, call fec_get_status after each
1777 // read and check if the errors field value has increased.
1778 }
1779 }
Tianjie Xu3b010bc2015-12-09 15:29:45 -08001780 fprintf(stderr, "...%s image recovered successfully.\n", filename->data);
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001781 return StringValue(strdup("t"));
1782}
1783
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001784void RegisterBlockImageFunctions() {
Sami Tolvanen90221202014-12-09 16:39:47 +00001785 RegisterFunction("block_image_verify", BlockImageVerifyFn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001786 RegisterFunction("block_image_update", BlockImageUpdateFn);
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001787 RegisterFunction("block_image_recover", BlockImageRecoverFn);
Tianjie Xu57bed6d2015-12-15 11:47:30 -08001788 RegisterFunction("check_first_block", CheckFirstBlockFn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001789 RegisterFunction("range_sha1", RangeSha1Fn);
1790}