blob: f08ca5b0c4890b32cf3340c09c8aaf560d74e4cf [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>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070038#include <unordered_map>
Tao Bao0940fe12015-08-27 16:41:21 -070039#include <vector>
Tao Baoe6aa3322015-08-05 15:20:27 -070040
Elliott Hughes4b166f02015-12-04 15:30:20 -080041#include <android-base/parseint.h>
42#include <android-base/strings.h>
Elliott Hughesbcabd092016-03-22 20:19:22 -070043#include <android-base/unique_fd.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070044#include <ziparchive/zip_archive.h>
Tao Baoe6aa3322015-08-05 15:20:27 -070045
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070046#include "applypatch/applypatch.h"
47#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070048#include "error_code.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070049#include "updater/install.h"
Sen Jiangc48cb5e2016-02-04 16:23:21 +080050#include "openssl/sha.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080051#include "ota_io.h"
Tao Baoe6aa3322015-08-05 15:20:27 -070052#include "print_sha1.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070053#include "updater/updater.h"
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070054
55#define BLOCKSIZE 4096
56
Sami Tolvanene82fa182015-06-10 15:58:12 +000057// Set this to 0 to interpret 'erase' transfers to mean do a
58// BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret
59// erase to mean fill the region with zeroes.
60#define DEBUG_ERASE 0
61
Sami Tolvanen90221202014-12-09 16:39:47 +000062#define STASH_DIRECTORY_BASE "/cache/recovery"
63#define STASH_DIRECTORY_MODE 0700
64#define STASH_FILE_MODE 0600
65
Tao Bao0940fe12015-08-27 16:41:21 -070066struct RangeSet {
67 size_t count; // Limit is INT_MAX.
Shrinivas Sahukara6153df2015-08-19 13:01:45 +053068 size_t size;
Tao Bao0940fe12015-08-27 16:41:21 -070069 std::vector<size_t> pos; // Actual limit is INT_MAX.
70};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070071
Tianjie Xu16255832016-04-30 11:49:59 -070072static CauseCode failure_type = kNoCause;
Tianjie Xu7ce287d2016-05-31 09:29:49 -070073static bool is_retry = false;
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070074static std::unordered_map<std::string, RangeSet> stash_map;
Tianjie Xu7eca97e2016-03-22 18:08:12 -070075
Tao Baobaad2d42015-12-06 16:56:27 -080076static void parse_range(const std::string& range_text, RangeSet& rs) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010077
Tao Baobaad2d42015-12-06 16:56:27 -080078 std::vector<std::string> pieces = android::base::Split(range_text, ",");
Tao Bao0940fe12015-08-27 16:41:21 -070079 if (pieces.size() < 3) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010080 goto err;
81 }
82
Tao Baob15fd222015-09-24 11:10:51 -070083 size_t num;
84 if (!android::base::ParseUint(pieces[0].c_str(), &num, static_cast<size_t>(INT_MAX))) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010085 goto err;
86 }
87
Tao Baob15fd222015-09-24 11:10:51 -070088 if (num == 0 || num % 2) {
89 goto err; // must be even
90 } else if (num != pieces.size() - 1) {
91 goto err;
92 }
Sami Tolvanenf2bac042015-05-12 12:48:46 +010093
Tao Bao0940fe12015-08-27 16:41:21 -070094 rs.pos.resize(num);
95 rs.count = num / 2;
96 rs.size = 0;
Sami Tolvanenf2bac042015-05-12 12:48:46 +010097
Tao Bao0940fe12015-08-27 16:41:21 -070098 for (size_t i = 0; i < num; i += 2) {
Tao Baob15fd222015-09-24 11:10:51 -070099 if (!android::base::ParseUint(pieces[i+1].c_str(), &rs.pos[i],
100 static_cast<size_t>(INT_MAX))) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100101 goto err;
102 }
103
Tao Baob15fd222015-09-24 11:10:51 -0700104 if (!android::base::ParseUint(pieces[i+2].c_str(), &rs.pos[i+1],
105 static_cast<size_t>(INT_MAX))) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530106 goto err;
107 }
108
Tao Bao0940fe12015-08-27 16:41:21 -0700109 if (rs.pos[i] >= rs.pos[i+1]) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530110 goto err; // empty or negative range
111 }
112
Tao Bao0940fe12015-08-27 16:41:21 -0700113 size_t sz = rs.pos[i+1] - rs.pos[i];
114 if (rs.size > SIZE_MAX - sz) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530115 goto err; // overflow
116 }
117
Tao Bao0940fe12015-08-27 16:41:21 -0700118 rs.size += sz;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700119 }
120
Tao Bao0940fe12015-08-27 16:41:21 -0700121 return;
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100122
123err:
Tao Baobaad2d42015-12-06 16:56:27 -0800124 fprintf(stderr, "failed to parse range '%s'\n", range_text.c_str());
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100125 exit(1);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700126}
127
Tao Baoe6aa3322015-08-05 15:20:27 -0700128static bool range_overlaps(const RangeSet& r1, const RangeSet& r2) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530129 for (size_t i = 0; i < r1.count; ++i) {
130 size_t r1_0 = r1.pos[i * 2];
131 size_t r1_1 = r1.pos[i * 2 + 1];
Sami Tolvanen90221202014-12-09 16:39:47 +0000132
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530133 for (size_t j = 0; j < r2.count; ++j) {
134 size_t r2_0 = r2.pos[j * 2];
135 size_t r2_1 = r2.pos[j * 2 + 1];
Sami Tolvanen90221202014-12-09 16:39:47 +0000136
Tao Baoc0f56ad2015-06-25 14:00:31 -0700137 if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700138 return true;
Sami Tolvanen90221202014-12-09 16:39:47 +0000139 }
140 }
141 }
142
Tao Baoe6aa3322015-08-05 15:20:27 -0700143 return false;
Sami Tolvanen90221202014-12-09 16:39:47 +0000144}
145
146static int read_all(int fd, uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700147 size_t so_far = 0;
148 while (so_far < size) {
Jed Estepa7b9a462015-12-15 16:04:53 -0800149 ssize_t r = TEMP_FAILURE_RETRY(ota_read(fd, data+so_far, size-so_far));
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700150 if (r == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700151 failure_type = kFreadFailure;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700152 fprintf(stderr, "read failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000153 return -1;
Tianjie Xu71e182b2016-08-31 18:06:33 -0700154 } else if (r == 0) {
155 failure_type = kFreadFailure;
156 fprintf(stderr, "read reached unexpected EOF.\n");
157 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700158 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700159 so_far += r;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700160 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000161 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700162}
163
Tao Bao612336d2015-08-27 16:41:21 -0700164static int read_all(int fd, std::vector<uint8_t>& buffer, size_t size) {
165 return read_all(fd, buffer.data(), size);
166}
167
Sami Tolvanen90221202014-12-09 16:39:47 +0000168static int write_all(int fd, const uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700169 size_t written = 0;
170 while (written < size) {
Jed Estepa7b9a462015-12-15 16:04:53 -0800171 ssize_t w = TEMP_FAILURE_RETRY(ota_write(fd, data+written, size-written));
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700172 if (w == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700173 failure_type = kFwriteFailure;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700174 fprintf(stderr, "write failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000175 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700176 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700177 written += w;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700178 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000179
Sami Tolvanen90221202014-12-09 16:39:47 +0000180 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700181}
182
Tao Bao612336d2015-08-27 16:41:21 -0700183static int write_all(int fd, const std::vector<uint8_t>& buffer, size_t size) {
184 return write_all(fd, buffer.data(), size);
185}
186
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700187static bool discard_blocks(int fd, off64_t offset, uint64_t size) {
188 // Don't discard blocks unless the update is a retry run.
189 if (!is_retry) {
190 return true;
191 }
192
193 uint64_t args[2] = {static_cast<uint64_t>(offset), size};
194 int status = ioctl(fd, BLKDISCARD, &args);
195 if (status == -1) {
196 fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno));
197 return false;
198 }
199 return true;
200}
201
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700202static bool check_lseek(int fd, off64_t offset, int whence) {
203 off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
204 if (rc == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700205 failure_type = kLseekFailure;
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700206 fprintf(stderr, "lseek64 failed: %s\n", strerror(errno));
207 return false;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700208 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700209 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700210}
211
Tao Bao612336d2015-08-27 16:41:21 -0700212static void allocate(size_t size, std::vector<uint8_t>& buffer) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700213 // if the buffer's big enough, reuse it.
Tao Bao612336d2015-08-27 16:41:21 -0700214 if (size <= buffer.size()) return;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700215
Tao Bao612336d2015-08-27 16:41:21 -0700216 buffer.resize(size);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700217}
218
Tao Bao0940fe12015-08-27 16:41:21 -0700219struct RangeSinkState {
Chih-Hung Hsieh49c5c792016-04-29 14:16:35 -0700220 explicit RangeSinkState(RangeSet& rs) : tgt(rs) { };
Tao Bao0940fe12015-08-27 16:41:21 -0700221
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700222 int fd;
Tao Bao0940fe12015-08-27 16:41:21 -0700223 const RangeSet& tgt;
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530224 size_t p_block;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700225 size_t p_remain;
Tao Bao0940fe12015-08-27 16:41:21 -0700226};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700227
228static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) {
Tao Bao0940fe12015-08-27 16:41:21 -0700229 RangeSinkState* rss = reinterpret_cast<RangeSinkState*>(token);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700230
Tao Bao0940fe12015-08-27 16:41:21 -0700231 if (rss->p_remain == 0) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700232 fprintf(stderr, "range sink write overrun");
Sami Tolvanen90221202014-12-09 16:39:47 +0000233 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700234 }
235
236 ssize_t written = 0;
237 while (size > 0) {
238 size_t write_now = size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000239
240 if (rss->p_remain < write_now) {
241 write_now = rss->p_remain;
242 }
243
244 if (write_all(rss->fd, data, write_now) == -1) {
245 break;
246 }
247
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700248 data += write_now;
249 size -= write_now;
250
251 rss->p_remain -= write_now;
252 written += write_now;
253
254 if (rss->p_remain == 0) {
255 // move to the next block
256 ++rss->p_block;
Tao Bao0940fe12015-08-27 16:41:21 -0700257 if (rss->p_block < rss->tgt.count) {
258 rss->p_remain = (rss->tgt.pos[rss->p_block * 2 + 1] -
259 rss->tgt.pos[rss->p_block * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000260
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700261 off64_t offset = static_cast<off64_t>(rss->tgt.pos[rss->p_block*2]) * BLOCKSIZE;
262 if (!discard_blocks(rss->fd, offset, rss->p_remain)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000263 break;
264 }
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700265
266 if (!check_lseek(rss->fd, offset, SEEK_SET)) {
267 break;
268 }
269
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700270 } else {
271 // we can't write any more; return how many bytes have
272 // been written so far.
Sami Tolvanen90221202014-12-09 16:39:47 +0000273 break;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700274 }
275 }
276 }
277
278 return written;
279}
280
281// All of the data for all the 'new' transfers is contained in one
282// file in the update package, concatenated together in the order in
283// which transfers.list will need it. We want to stream it out of the
284// archive (it's compressed) without writing it to a temp file, but we
285// can't write each section until it's that transfer's turn to go.
286//
287// To achieve this, we expand the new data from the archive in a
288// background thread, and block that threads 'receive uncompressed
289// data' function until the main thread has reached a point where we
290// want some new data to be written. We signal the background thread
291// with the destination for the data and block the main thread,
292// waiting for the background thread to complete writing that section.
293// Then it signals the main thread to wake up and goes back to
294// blocking waiting for a transfer.
295//
296// NewThreadInfo is the struct used to pass information back and forth
297// between the two threads. When the main thread wants some data
298// written, it sets rss to the destination location and signals the
299// condition. When the background thread is done writing, it clears
300// rss and signals the condition again.
301
Tao Bao0940fe12015-08-27 16:41:21 -0700302struct NewThreadInfo {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700303 ZipArchiveHandle za;
304 ZipEntry entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700305
306 RangeSinkState* rss;
307
308 pthread_mutex_t mu;
309 pthread_cond_t cv;
Tao Bao0940fe12015-08-27 16:41:21 -0700310};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700311
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700312static bool receive_new_data(const uint8_t* data, size_t size, void* cookie) {
Tao Bao0940fe12015-08-27 16:41:21 -0700313 NewThreadInfo* nti = reinterpret_cast<NewThreadInfo*>(cookie);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700314
315 while (size > 0) {
Tao Bao0940fe12015-08-27 16:41:21 -0700316 // Wait for nti->rss to be non-null, indicating some of this
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700317 // data is wanted.
318 pthread_mutex_lock(&nti->mu);
Tao Bao0940fe12015-08-27 16:41:21 -0700319 while (nti->rss == nullptr) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700320 pthread_cond_wait(&nti->cv, &nti->mu);
321 }
322 pthread_mutex_unlock(&nti->mu);
323
324 // At this point nti->rss is set, and we own it. The main
325 // thread is waiting for it to disappear from nti.
326 ssize_t written = RangeSinkWrite(data, size, nti->rss);
327 data += written;
328 size -= written;
329
Tao Bao0940fe12015-08-27 16:41:21 -0700330 if (nti->rss->p_block == nti->rss->tgt.count) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700331 // we have written all the bytes desired by this rss.
332
333 pthread_mutex_lock(&nti->mu);
Tao Bao0940fe12015-08-27 16:41:21 -0700334 nti->rss = nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700335 pthread_cond_broadcast(&nti->cv);
336 pthread_mutex_unlock(&nti->mu);
337 }
338 }
339
340 return true;
341}
342
343static void* unzip_new_data(void* cookie) {
344 NewThreadInfo* nti = (NewThreadInfo*) cookie;
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700345 ProcessZipEntryContents(nti->za, &nti->entry, receive_new_data, nti);
Tao Bao0940fe12015-08-27 16:41:21 -0700346 return nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700347}
348
Tao Bao612336d2015-08-27 16:41:21 -0700349static int ReadBlocks(const RangeSet& src, std::vector<uint8_t>& buffer, int fd) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000350 size_t p = 0;
Tao Bao612336d2015-08-27 16:41:21 -0700351 uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000352
Tao Bao0940fe12015-08-27 16:41:21 -0700353 for (size_t i = 0; i < src.count; ++i) {
354 if (!check_lseek(fd, (off64_t) src.pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000355 return -1;
356 }
357
Tao Bao0940fe12015-08-27 16:41:21 -0700358 size_t size = (src.pos[i * 2 + 1] - src.pos[i * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000359
Tao Bao612336d2015-08-27 16:41:21 -0700360 if (read_all(fd, data + p, size) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000361 return -1;
362 }
363
364 p += size;
365 }
366
367 return 0;
368}
369
Tao Bao612336d2015-08-27 16:41:21 -0700370static int WriteBlocks(const RangeSet& tgt, const std::vector<uint8_t>& buffer, int fd) {
371 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000372
Tao Bao0940fe12015-08-27 16:41:21 -0700373 size_t p = 0;
374 for (size_t i = 0; i < tgt.count; ++i) {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700375 off64_t offset = static_cast<off64_t>(tgt.pos[i * 2]) * BLOCKSIZE;
376 size_t size = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * BLOCKSIZE;
377 if (!discard_blocks(fd, offset, size)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000378 return -1;
379 }
380
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700381 if (!check_lseek(fd, offset, SEEK_SET)) {
382 return -1;
383 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000384
Tao Bao612336d2015-08-27 16:41:21 -0700385 if (write_all(fd, data + p, size) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000386 return -1;
387 }
388
389 p += size;
390 }
391
392 return 0;
393}
394
Tao Baobaad2d42015-12-06 16:56:27 -0800395// Parameters for transfer list command functions
396struct CommandParameters {
397 std::vector<std::string> tokens;
398 size_t cpos;
399 const char* cmdname;
400 const char* cmdline;
401 std::string freestash;
402 std::string stashbase;
403 bool canwrite;
404 int createdstash;
Elliott Hughesbcabd092016-03-22 20:19:22 -0700405 android::base::unique_fd fd;
Tao Baobaad2d42015-12-06 16:56:27 -0800406 bool foundwrites;
407 bool isunresumable;
408 int version;
409 size_t written;
Tianjie Xudd874b12016-05-13 12:13:15 -0700410 size_t stashed;
Tao Baobaad2d42015-12-06 16:56:27 -0800411 NewThreadInfo nti;
412 pthread_t thread;
413 std::vector<uint8_t> buffer;
414 uint8_t* patch_start;
415};
416
Doug Zongker52ae67d2014-09-08 12:22:09 -0700417// Do a source/target load for move/bsdiff/imgdiff in version 1.
Tao Baobaad2d42015-12-06 16:56:27 -0800418// We expect to parse the remainder of the parameter tokens as:
Doug Zongker52ae67d2014-09-08 12:22:09 -0700419//
420// <src_range> <tgt_range>
421//
422// The source range is loaded into the provided buffer, reallocating
Tao Bao34847b22015-09-08 11:05:49 -0700423// it to make it larger if necessary.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700424
Tao Baobaad2d42015-12-06 16:56:27 -0800425static int LoadSrcTgtVersion1(CommandParameters& params, RangeSet& tgt, size_t& src_blocks,
Tao Bao612336d2015-08-27 16:41:21 -0700426 std::vector<uint8_t>& buffer, int fd) {
Tao Baobaad2d42015-12-06 16:56:27 -0800427
428 if (params.cpos + 1 >= params.tokens.size()) {
429 fprintf(stderr, "invalid parameters\n");
430 return -1;
431 }
432
Tao Bao612336d2015-08-27 16:41:21 -0700433 // <src_range>
Tao Bao0940fe12015-08-27 16:41:21 -0700434 RangeSet src;
Tao Baobaad2d42015-12-06 16:56:27 -0800435 parse_range(params.tokens[params.cpos++], src);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700436
Tao Bao612336d2015-08-27 16:41:21 -0700437 // <tgt_range>
Tao Baobaad2d42015-12-06 16:56:27 -0800438 parse_range(params.tokens[params.cpos++], tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700439
Tao Bao612336d2015-08-27 16:41:21 -0700440 allocate(src.size * BLOCKSIZE, buffer);
441 int rc = ReadBlocks(src, buffer, fd);
Tao Bao0940fe12015-08-27 16:41:21 -0700442 src_blocks = src.size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000443
Sami Tolvanen90221202014-12-09 16:39:47 +0000444 return rc;
445}
446
Tao Bao612336d2015-08-27 16:41:21 -0700447static int VerifyBlocks(const std::string& expected, const std::vector<uint8_t>& buffer,
Tao Bao0940fe12015-08-27 16:41:21 -0700448 const size_t blocks, bool printerror) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800449 uint8_t digest[SHA_DIGEST_LENGTH];
Tao Bao612336d2015-08-27 16:41:21 -0700450 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000451
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800452 SHA1(data, blocks * BLOCKSIZE, digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000453
Tao Baoe6aa3322015-08-05 15:20:27 -0700454 std::string hexdigest = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000455
Tao Bao0940fe12015-08-27 16:41:21 -0700456 if (hexdigest != expected) {
457 if (printerror) {
458 fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
459 expected.c_str(), hexdigest.c_str());
460 }
461 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000462 }
463
Tao Bao0940fe12015-08-27 16:41:21 -0700464 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000465}
466
Tao Bao0940fe12015-08-27 16:41:21 -0700467static std::string GetStashFileName(const std::string& base, const std::string& id,
468 const std::string& postfix) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700469 if (base.empty()) {
470 return "";
Sami Tolvanen90221202014-12-09 16:39:47 +0000471 }
472
Tao Baoe6aa3322015-08-05 15:20:27 -0700473 std::string fn(STASH_DIRECTORY_BASE);
474 fn += "/" + base + "/" + id + postfix;
Sami Tolvanen90221202014-12-09 16:39:47 +0000475
476 return fn;
477}
478
Tao Baoe6aa3322015-08-05 15:20:27 -0700479typedef void (*StashCallback)(const std::string&, void*);
Sami Tolvanen90221202014-12-09 16:39:47 +0000480
481// Does a best effort enumeration of stash files. Ignores possible non-file
482// items in the stash directory and continues despite of errors. Calls the
483// 'callback' function for each file and passes 'data' to the function as a
484// parameter.
485
Tao Baoe6aa3322015-08-05 15:20:27 -0700486static void EnumerateStash(const std::string& dirname, StashCallback callback, void* data) {
Tao Bao0940fe12015-08-27 16:41:21 -0700487 if (dirname.empty() || callback == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000488 return;
489 }
490
Tao Baoe6aa3322015-08-05 15:20:27 -0700491 std::unique_ptr<DIR, int(*)(DIR*)> directory(opendir(dirname.c_str()), closedir);
Sami Tolvanen90221202014-12-09 16:39:47 +0000492
Tao Bao0940fe12015-08-27 16:41:21 -0700493 if (directory == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000494 if (errno != ENOENT) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700495 fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000496 }
497 return;
498 }
499
Tao Baoe6aa3322015-08-05 15:20:27 -0700500 struct dirent* item;
Tao Bao0940fe12015-08-27 16:41:21 -0700501 while ((item = readdir(directory.get())) != nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000502 if (item->d_type != DT_REG) {
503 continue;
504 }
505
Tao Baoe6aa3322015-08-05 15:20:27 -0700506 std::string fn = dirname + "/" + std::string(item->d_name);
Sami Tolvanen90221202014-12-09 16:39:47 +0000507 callback(fn, data);
Sami Tolvanen90221202014-12-09 16:39:47 +0000508 }
509}
510
Tao Baoe6aa3322015-08-05 15:20:27 -0700511static void UpdateFileSize(const std::string& fn, void* data) {
512 if (fn.empty() || !data) {
513 return;
514 }
515
Tao Bao0940fe12015-08-27 16:41:21 -0700516 struct stat sb;
517 if (stat(fn.c_str(), &sb) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700518 fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000519 return;
520 }
521
Tao Baoe6aa3322015-08-05 15:20:27 -0700522 int* size = reinterpret_cast<int*>(data);
Tao Bao0940fe12015-08-27 16:41:21 -0700523 *size += sb.st_size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000524}
525
526// Deletes the stash directory and all files in it. Assumes that it only
527// contains files. There is nothing we can do about unlikely, but possible
528// errors, so they are merely logged.
529
Tao Bao0940fe12015-08-27 16:41:21 -0700530static void DeleteFile(const std::string& fn, void* /* data */) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700531 if (!fn.empty()) {
532 fprintf(stderr, "deleting %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000533
Tao Baoe6aa3322015-08-05 15:20:27 -0700534 if (unlink(fn.c_str()) == -1 && errno != ENOENT) {
535 fprintf(stderr, "unlink \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000536 }
537 }
538}
539
Tao Baoe6aa3322015-08-05 15:20:27 -0700540static void DeletePartial(const std::string& fn, void* data) {
541 if (android::base::EndsWith(fn, ".partial")) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000542 DeleteFile(fn, data);
543 }
544}
545
Tao Baoe6aa3322015-08-05 15:20:27 -0700546static void DeleteStash(const std::string& base) {
547 if (base.empty()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000548 return;
549 }
550
Tao Baoe6aa3322015-08-05 15:20:27 -0700551 fprintf(stderr, "deleting stash %s\n", base.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000552
Tao Baoe6aa3322015-08-05 15:20:27 -0700553 std::string dirname = GetStashFileName(base, "", "");
Tao Bao0940fe12015-08-27 16:41:21 -0700554 EnumerateStash(dirname, DeleteFile, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000555
Tao Baoe6aa3322015-08-05 15:20:27 -0700556 if (rmdir(dirname.c_str()) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000557 if (errno != ENOENT && errno != ENOTDIR) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700558 fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000559 }
560 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000561}
562
Tianjie Xu7eca97e2016-03-22 18:08:12 -0700563static int LoadStash(CommandParameters& params, const std::string& base, const std::string& id,
564 bool verify, size_t* blocks, std::vector<uint8_t>& buffer, bool printnoent) {
565 // In verify mode, if source range_set was saved for the given hash,
566 // check contents in the source blocks first. If the check fails,
567 // search for the stashed files on /cache as usual.
568 if (!params.canwrite) {
569 if (stash_map.find(id) != stash_map.end()) {
570 const RangeSet& src = stash_map[id];
571 allocate(src.size * BLOCKSIZE, buffer);
572
573 if (ReadBlocks(src, buffer, params.fd) == -1) {
574 fprintf(stderr, "failed to read source blocks in stash map.\n");
575 return -1;
576 }
577 if (VerifyBlocks(id, buffer, src.size, true) != 0) {
578 fprintf(stderr, "failed to verify loaded source blocks in stash map.\n");
579 return -1;
580 }
581 return 0;
582 }
583 }
584
Tao Bao612336d2015-08-27 16:41:21 -0700585 if (base.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -0700586 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000587 }
588
Tao Bao0940fe12015-08-27 16:41:21 -0700589 size_t blockcount = 0;
590
Sami Tolvanen90221202014-12-09 16:39:47 +0000591 if (!blocks) {
592 blocks = &blockcount;
593 }
594
Tao Bao0940fe12015-08-27 16:41:21 -0700595 std::string fn = GetStashFileName(base, id, "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000596
Tao Bao0940fe12015-08-27 16:41:21 -0700597 struct stat sb;
598 int res = stat(fn.c_str(), &sb);
Sami Tolvanen90221202014-12-09 16:39:47 +0000599
600 if (res == -1) {
601 if (errno != ENOENT || printnoent) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700602 fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000603 }
Tao Bao0940fe12015-08-27 16:41:21 -0700604 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000605 }
606
Tao Baoe6aa3322015-08-05 15:20:27 -0700607 fprintf(stderr, " loading %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000608
Tao Bao0940fe12015-08-27 16:41:21 -0700609 if ((sb.st_size % BLOCKSIZE) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700610 fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d",
Tao Bao0940fe12015-08-27 16:41:21 -0700611 fn.c_str(), static_cast<int64_t>(sb.st_size), BLOCKSIZE);
612 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000613 }
614
Elliott Hughesbcabd092016-03-22 20:19:22 -0700615 android::base::unique_fd fd(TEMP_FAILURE_RETRY(ota_open(fn.c_str(), O_RDONLY)));
Sami Tolvanen90221202014-12-09 16:39:47 +0000616 if (fd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700617 fprintf(stderr, "open \"%s\" failed: %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
Tao Bao612336d2015-08-27 16:41:21 -0700621 allocate(sb.st_size, buffer);
Sami Tolvanen90221202014-12-09 16:39:47 +0000622
Tao Bao612336d2015-08-27 16:41:21 -0700623 if (read_all(fd, buffer, sb.st_size) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700624 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000625 }
626
Tao Bao0940fe12015-08-27 16:41:21 -0700627 *blocks = sb.st_size / BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000628
Tao Bao612336d2015-08-27 16:41:21 -0700629 if (verify && VerifyBlocks(id, buffer, *blocks, true) != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700630 fprintf(stderr, "unexpected contents in %s\n", fn.c_str());
Tao Bao0940fe12015-08-27 16:41:21 -0700631 DeleteFile(fn, nullptr);
632 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000633 }
634
Tao Bao0940fe12015-08-27 16:41:21 -0700635 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000636}
637
Tao Bao612336d2015-08-27 16:41:21 -0700638static int WriteStash(const std::string& base, const std::string& id, int blocks,
639 std::vector<uint8_t>& buffer, bool checkspace, bool *exists) {
640 if (base.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -0700641 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000642 }
643
644 if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) {
645 fprintf(stderr, "not enough space to write stash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700646 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000647 }
648
Tao Bao0940fe12015-08-27 16:41:21 -0700649 std::string fn = GetStashFileName(base, id, ".partial");
650 std::string cn = GetStashFileName(base, id, "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000651
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100652 if (exists) {
Tao Bao0940fe12015-08-27 16:41:21 -0700653 struct stat sb;
654 int res = stat(cn.c_str(), &sb);
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100655
656 if (res == 0) {
657 // The file already exists and since the name is the hash of the contents,
658 // it's safe to assume the contents are identical (accidental hash collisions
659 // are unlikely)
Tao Baoe6aa3322015-08-05 15:20:27 -0700660 fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn.c_str());
Tao Bao0940fe12015-08-27 16:41:21 -0700661 *exists = true;
662 return 0;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100663 }
664
Tao Bao0940fe12015-08-27 16:41:21 -0700665 *exists = false;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100666 }
667
Tao Baoe6aa3322015-08-05 15:20:27 -0700668 fprintf(stderr, " writing %d blocks to %s\n", blocks, cn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000669
Elliott Hughesbcabd092016-03-22 20:19:22 -0700670 android::base::unique_fd fd(TEMP_FAILURE_RETRY(ota_open(fn.c_str(),
671 O_WRONLY | O_CREAT | O_TRUNC,
672 STASH_FILE_MODE)));
Sami Tolvanen90221202014-12-09 16:39:47 +0000673 if (fd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700674 fprintf(stderr, "failed to create \"%s\": %s\n", fn.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700675 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000676 }
677
678 if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700679 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000680 }
681
Jed Estepa7b9a462015-12-15 16:04:53 -0800682 if (ota_fsync(fd) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700683 failure_type = kFsyncFailure;
Tao Baoe6aa3322015-08-05 15:20:27 -0700684 fprintf(stderr, "fsync \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700685 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000686 }
687
Tao Baoe6aa3322015-08-05 15:20:27 -0700688 if (rename(fn.c_str(), cn.c_str()) == -1) {
689 fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn.c_str(), cn.c_str(),
690 strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700691 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000692 }
693
Tao Bao0940fe12015-08-27 16:41:21 -0700694 std::string dname = GetStashFileName(base, "", "");
Elliott Hughesbcabd092016-03-22 20:19:22 -0700695 android::base::unique_fd dfd(TEMP_FAILURE_RETRY(ota_open(dname.c_str(),
696 O_RDONLY | O_DIRECTORY)));
Tao Baodc392262015-07-31 15:56:44 -0700697 if (dfd == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700698 failure_type = kFileOpenFailure;
Tao Baoe6aa3322015-08-05 15:20:27 -0700699 fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700700 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700701 }
702
Jed Estepa7b9a462015-12-15 16:04:53 -0800703 if (ota_fsync(dfd) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700704 failure_type = kFsyncFailure;
Tao Baoe6aa3322015-08-05 15:20:27 -0700705 fprintf(stderr, "fsync \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700706 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700707 }
708
Tao Bao0940fe12015-08-27 16:41:21 -0700709 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000710}
711
712// Creates a directory for storing stash files and checks if the /cache partition
713// hash enough space for the expected amount of blocks we need to store. Returns
714// >0 if we created the directory, zero if it existed already, and <0 of failure.
715
Tao Bao0940fe12015-08-27 16:41:21 -0700716static int CreateStash(State* state, int maxblocks, const char* blockdev, std::string& base) {
717 if (blockdev == nullptr) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700718 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000719 }
720
721 // Stash directory should be different for each partition to avoid conflicts
722 // when updating multiple partitions at the same time, so we use the hash of
723 // the block device name as the base directory
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800724 uint8_t digest[SHA_DIGEST_LENGTH];
725 SHA1(reinterpret_cast<const uint8_t*>(blockdev), strlen(blockdev), digest);
Tao Baoe6aa3322015-08-05 15:20:27 -0700726 base = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000727
Tao Baoe6aa3322015-08-05 15:20:27 -0700728 std::string dirname = GetStashFileName(base, "", "");
Tao Bao0940fe12015-08-27 16:41:21 -0700729 struct stat sb;
730 int res = stat(dirname.c_str(), &sb);
Sami Tolvanen90221202014-12-09 16:39:47 +0000731
732 if (res == -1 && errno != ENOENT) {
Tianjie Xu16255832016-04-30 11:49:59 -0700733 ErrorAbort(state, kStashCreationFailure, "stat \"%s\" failed: %s\n",
734 dirname.c_str(), strerror(errno));
Tao Baoe6aa3322015-08-05 15:20:27 -0700735 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000736 } else if (res != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700737 fprintf(stderr, "creating stash %s\n", dirname.c_str());
738 res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000739
740 if (res != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700741 ErrorAbort(state, kStashCreationFailure, "mkdir \"%s\" failed: %s\n",
742 dirname.c_str(), strerror(errno));
Tao Baoe6aa3322015-08-05 15:20:27 -0700743 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000744 }
745
746 if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700747 ErrorAbort(state, kStashCreationFailure, "not enough space for stash\n");
Tao Baoe6aa3322015-08-05 15:20:27 -0700748 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000749 }
750
Tao Baoe6aa3322015-08-05 15:20:27 -0700751 return 1; // Created directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000752 }
753
Tao Baoe6aa3322015-08-05 15:20:27 -0700754 fprintf(stderr, "using existing stash %s\n", dirname.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000755
756 // If the directory already exists, calculate the space already allocated to
757 // stash files and check if there's enough for all required blocks. Delete any
758 // partially completed stash files first.
759
Tao Bao0940fe12015-08-27 16:41:21 -0700760 EnumerateStash(dirname, DeletePartial, nullptr);
Tao Baoe6aa3322015-08-05 15:20:27 -0700761 int size = 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000762 EnumerateStash(dirname, UpdateFileSize, &size);
763
Tao Bao0940fe12015-08-27 16:41:21 -0700764 size = maxblocks * BLOCKSIZE - size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000765
766 if (size > 0 && CacheSizeCheck(size) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700767 ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%d more needed)\n",
768 size);
Tao Baoe6aa3322015-08-05 15:20:27 -0700769 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000770 }
771
Tao Baoe6aa3322015-08-05 15:20:27 -0700772 return 0; // Using existing directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000773}
774
Tao Baobaad2d42015-12-06 16:56:27 -0800775static int SaveStash(CommandParameters& params, const std::string& base,
776 std::vector<uint8_t>& buffer, int fd, bool usehash) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000777
Tao Baobaad2d42015-12-06 16:56:27 -0800778 // <stash_id> <src_range>
779 if (params.cpos + 1 >= params.tokens.size()) {
780 fprintf(stderr, "missing id and/or src range fields in stash command\n");
Sami Tolvanen90221202014-12-09 16:39:47 +0000781 return -1;
782 }
Tao Baobaad2d42015-12-06 16:56:27 -0800783 const std::string& id = params.tokens[params.cpos++];
Sami Tolvanen90221202014-12-09 16:39:47 +0000784
Tao Bao0940fe12015-08-27 16:41:21 -0700785 size_t blocks = 0;
Tianjie Xu7eca97e2016-03-22 18:08:12 -0700786 if (usehash && LoadStash(params, base, id, true, &blocks, buffer, false) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000787 // Stash file already exists and has expected contents. Do not
788 // read from source again, as the source may have been already
789 // overwritten during a previous attempt.
790 return 0;
791 }
792
Tao Bao34847b22015-09-08 11:05:49 -0700793 RangeSet src;
Tao Baobaad2d42015-12-06 16:56:27 -0800794 parse_range(params.tokens[params.cpos++], src);
Tao Bao34847b22015-09-08 11:05:49 -0700795
Tao Bao612336d2015-08-27 16:41:21 -0700796 allocate(src.size * BLOCKSIZE, buffer);
797 if (ReadBlocks(src, buffer, fd) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000798 return -1;
799 }
Tao Bao34847b22015-09-08 11:05:49 -0700800 blocks = src.size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000801
Tao Bao612336d2015-08-27 16:41:21 -0700802 if (usehash && VerifyBlocks(id, buffer, blocks, true) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000803 // Source blocks have unexpected contents. If we actually need this
804 // data later, this is an unrecoverable error. However, the command
805 // that uses the data may have already completed previously, so the
806 // possible failure will occur during source block verification.
Tao Bao0940fe12015-08-27 16:41:21 -0700807 fprintf(stderr, "failed to load source blocks for stash %s\n", id.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000808 return 0;
809 }
810
Tianjie Xu7eca97e2016-03-22 18:08:12 -0700811 // In verify mode, save source range_set instead of stashing blocks.
812 if (!params.canwrite && usehash) {
813 stash_map[id] = src;
814 return 0;
815 }
816
Tao Bao0940fe12015-08-27 16:41:21 -0700817 fprintf(stderr, "stashing %zu blocks to %s\n", blocks, id.c_str());
Tianjie Xudd874b12016-05-13 12:13:15 -0700818 params.stashed += blocks;
Tao Bao612336d2015-08-27 16:41:21 -0700819 return WriteStash(base, id, blocks, buffer, false, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000820}
821
Tao Baobaad2d42015-12-06 16:56:27 -0800822static int FreeStash(const std::string& base, const std::string& id) {
823 if (base.empty() || id.empty()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000824 return -1;
825 }
826
Tao Baobaad2d42015-12-06 16:56:27 -0800827 std::string fn = GetStashFileName(base, id, "");
Tao Bao0940fe12015-08-27 16:41:21 -0700828 DeleteFile(fn, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000829
830 return 0;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700831}
832
Tao Bao612336d2015-08-27 16:41:21 -0700833static void MoveRange(std::vector<uint8_t>& dest, const RangeSet& locs,
834 const std::vector<uint8_t>& source) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700835 // source contains packed data, which we want to move to the
Tao Bao612336d2015-08-27 16:41:21 -0700836 // locations given in locs in the dest buffer. source and dest
Doug Zongker52ae67d2014-09-08 12:22:09 -0700837 // may be the same buffer.
838
Tao Bao612336d2015-08-27 16:41:21 -0700839 const uint8_t* from = source.data();
840 uint8_t* to = dest.data();
Tao Bao0940fe12015-08-27 16:41:21 -0700841 size_t start = locs.size;
842 for (int i = locs.count-1; i >= 0; --i) {
843 size_t blocks = locs.pos[i*2+1] - locs.pos[i*2];
Doug Zongker52ae67d2014-09-08 12:22:09 -0700844 start -= blocks;
Tao Bao612336d2015-08-27 16:41:21 -0700845 memmove(to + (locs.pos[i*2] * BLOCKSIZE), from + (start * BLOCKSIZE),
Doug Zongker52ae67d2014-09-08 12:22:09 -0700846 blocks * BLOCKSIZE);
847 }
848}
849
850// Do a source/target load for move/bsdiff/imgdiff in version 2.
Tao Baobaad2d42015-12-06 16:56:27 -0800851// We expect to parse the remainder of the parameter tokens as one of:
Doug Zongker52ae67d2014-09-08 12:22:09 -0700852//
853// <tgt_range> <src_block_count> <src_range>
854// (loads data from source image only)
855//
856// <tgt_range> <src_block_count> - <[stash_id:stash_range] ...>
857// (loads data from stashes only)
858//
859// <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...>
860// (loads data from both source image and stashes)
861//
862// On return, buffer is filled with the loaded source data (rearranged
863// and combined with stashed data as necessary). buffer may be
864// reallocated if needed to accommodate the source data. *tgt is the
Sami Tolvanen90221202014-12-09 16:39:47 +0000865// target RangeSet. Any stashes required are loaded using LoadStash.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700866
Tao Baobaad2d42015-12-06 16:56:27 -0800867static int LoadSrcTgtVersion2(CommandParameters& params, RangeSet& tgt, size_t& src_blocks,
Tao Bao612336d2015-08-27 16:41:21 -0700868 std::vector<uint8_t>& buffer, int fd, const std::string& stashbase, bool* overlap) {
Tao Baobaad2d42015-12-06 16:56:27 -0800869
870 // At least it needs to provide three parameters: <tgt_range>,
871 // <src_block_count> and "-"/<src_range>.
872 if (params.cpos + 2 >= params.tokens.size()) {
873 fprintf(stderr, "invalid parameters\n");
874 return -1;
875 }
876
Tao Bao612336d2015-08-27 16:41:21 -0700877 // <tgt_range>
Tao Baobaad2d42015-12-06 16:56:27 -0800878 parse_range(params.tokens[params.cpos++], tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700879
Tao Bao612336d2015-08-27 16:41:21 -0700880 // <src_block_count>
Tao Baobaad2d42015-12-06 16:56:27 -0800881 const std::string& token = params.tokens[params.cpos++];
882 if (!android::base::ParseUint(token.c_str(), &src_blocks)) {
883 fprintf(stderr, "invalid src_block_count \"%s\"\n", token.c_str());
884 return -1;
885 }
Doug Zongker52ae67d2014-09-08 12:22:09 -0700886
Tao Bao612336d2015-08-27 16:41:21 -0700887 allocate(src_blocks * BLOCKSIZE, buffer);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700888
Tao Bao612336d2015-08-27 16:41:21 -0700889 // "-" or <src_range> [<src_loc>]
Tao Baobaad2d42015-12-06 16:56:27 -0800890 if (params.tokens[params.cpos] == "-") {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700891 // no source ranges, only stashes
Tao Baobaad2d42015-12-06 16:56:27 -0800892 params.cpos++;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700893 } else {
Tao Bao0940fe12015-08-27 16:41:21 -0700894 RangeSet src;
Tao Baobaad2d42015-12-06 16:56:27 -0800895 parse_range(params.tokens[params.cpos++], src);
Tao Bao612336d2015-08-27 16:41:21 -0700896 int res = ReadBlocks(src, buffer, fd);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700897
Tao Bao34847b22015-09-08 11:05:49 -0700898 if (overlap) {
899 *overlap = range_overlaps(src, tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700900 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000901
Sami Tolvanen90221202014-12-09 16:39:47 +0000902 if (res == -1) {
903 return -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700904 }
905
Tao Baobaad2d42015-12-06 16:56:27 -0800906 if (params.cpos >= params.tokens.size()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000907 // no stashes, only source range
908 return 0;
909 }
910
Tao Bao612336d2015-08-27 16:41:21 -0700911 RangeSet locs;
Tao Baobaad2d42015-12-06 16:56:27 -0800912 parse_range(params.tokens[params.cpos++], locs);
Tao Bao612336d2015-08-27 16:41:21 -0700913 MoveRange(buffer, locs, buffer);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700914 }
915
Tao Baobaad2d42015-12-06 16:56:27 -0800916 // <[stash_id:stash_range]>
917 while (params.cpos < params.tokens.size()) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700918 // Each word is a an index into the stash table, a colon, and
919 // then a rangeset describing where in the source block that
920 // stashed data should go.
Tao Baobaad2d42015-12-06 16:56:27 -0800921 std::vector<std::string> tokens = android::base::Split(params.tokens[params.cpos++], ":");
922 if (tokens.size() != 2) {
923 fprintf(stderr, "invalid parameter\n");
924 return -1;
925 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000926
Tao Bao612336d2015-08-27 16:41:21 -0700927 std::vector<uint8_t> stash;
Tianjie Xu7eca97e2016-03-22 18:08:12 -0700928 int res = LoadStash(params, stashbase, tokens[0], false, nullptr, stash, true);
Sami Tolvanen90221202014-12-09 16:39:47 +0000929
930 if (res == -1) {
931 // These source blocks will fail verification if used later, but we
932 // will let the caller decide if this is a fatal failure
Tao Baobaad2d42015-12-06 16:56:27 -0800933 fprintf(stderr, "failed to load stash %s\n", tokens[0].c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000934 continue;
935 }
936
Tao Bao612336d2015-08-27 16:41:21 -0700937 RangeSet locs;
Tao Baobaad2d42015-12-06 16:56:27 -0800938 parse_range(tokens[1], locs);
Sami Tolvanen90221202014-12-09 16:39:47 +0000939
Tao Bao612336d2015-08-27 16:41:21 -0700940 MoveRange(buffer, locs, stash);
Sami Tolvanen90221202014-12-09 16:39:47 +0000941 }
942
943 return 0;
944}
945
Sami Tolvanen90221202014-12-09 16:39:47 +0000946// Do a source/target load for move/bsdiff/imgdiff in version 3.
947//
948// Parameters are the same as for LoadSrcTgtVersion2, except for 'onehash', which
949// tells the function whether to expect separate source and targe block hashes, or
950// if they are both the same and only one hash should be expected, and
951// 'isunresumable', which receives a non-zero value if block verification fails in
952// a way that the update cannot be resumed anymore.
953//
954// If the function is unable to load the necessary blocks or their contents don't
955// match the hashes, the return value is -1 and the command should be aborted.
956//
957// If the return value is 1, the command has already been completed according to
958// the contents of the target blocks, and should not be performed again.
959//
960// If the return value is 0, source blocks have expected content and the command
961// can be performed.
962
Tao Bao34847b22015-09-08 11:05:49 -0700963static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t& src_blocks,
Tao Bao0940fe12015-08-27 16:41:21 -0700964 bool onehash, bool& overlap) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000965
Tao Baobaad2d42015-12-06 16:56:27 -0800966 if (params.cpos >= params.tokens.size()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000967 fprintf(stderr, "missing source hash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700968 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000969 }
970
Tao Baobaad2d42015-12-06 16:56:27 -0800971 std::string srchash = params.tokens[params.cpos++];
972 std::string tgthash;
973
Sami Tolvanen90221202014-12-09 16:39:47 +0000974 if (onehash) {
975 tgthash = srchash;
976 } else {
Tao Baobaad2d42015-12-06 16:56:27 -0800977 if (params.cpos >= params.tokens.size()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000978 fprintf(stderr, "missing target hash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700979 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000980 }
Tao Baobaad2d42015-12-06 16:56:27 -0800981 tgthash = params.tokens[params.cpos++];
Sami Tolvanen90221202014-12-09 16:39:47 +0000982 }
983
Elliott Hughesbcabd092016-03-22 20:19:22 -0700984 if (LoadSrcTgtVersion2(params, tgt, src_blocks, params.buffer, params.fd,
985 params.stashbase, &overlap) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700986 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000987 }
988
Tao Bao34847b22015-09-08 11:05:49 -0700989 std::vector<uint8_t> tgtbuffer(tgt.size * BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000990
Tao Bao612336d2015-08-27 16:41:21 -0700991 if (ReadBlocks(tgt, tgtbuffer, params.fd) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700992 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000993 }
994
Tao Bao612336d2015-08-27 16:41:21 -0700995 if (VerifyBlocks(tgthash, tgtbuffer, tgt.size, false) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000996 // Target blocks already have expected content, command should be skipped
Tao Bao0940fe12015-08-27 16:41:21 -0700997 return 1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000998 }
999
Tao Bao0940fe12015-08-27 16:41:21 -07001000 if (VerifyBlocks(srchash, params.buffer, src_blocks, true) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001001 // If source and target blocks overlap, stash the source blocks so we can
Tianjie Xu7eca97e2016-03-22 18:08:12 -07001002 // resume from possible write errors. In verify mode, we can skip stashing
1003 // because the source blocks won't be overwritten.
1004 if (overlap && params.canwrite) {
Tao Baobaad2d42015-12-06 16:56:27 -08001005 fprintf(stderr, "stashing %zu overlapping blocks to %s\n", src_blocks,
1006 srchash.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +00001007
Tao Bao0940fe12015-08-27 16:41:21 -07001008 bool stash_exists = false;
Tao Bao612336d2015-08-27 16:41:21 -07001009 if (WriteStash(params.stashbase, srchash, src_blocks, params.buffer, true,
Tao Bao0940fe12015-08-27 16:41:21 -07001010 &stash_exists) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001011 fprintf(stderr, "failed to stash overlapping source blocks\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001012 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001013 }
1014
Tianjie Xudd874b12016-05-13 12:13:15 -07001015 params.stashed += src_blocks;
Sami Tolvanen90221202014-12-09 16:39:47 +00001016 // Can be deleted when the write has completed
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001017 if (!stash_exists) {
Tao Bao0940fe12015-08-27 16:41:21 -07001018 params.freestash = srchash;
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001019 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001020 }
1021
1022 // Source blocks have expected content, command can proceed
Tao Bao0940fe12015-08-27 16:41:21 -07001023 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001024 }
1025
Tianjie Xu7eca97e2016-03-22 18:08:12 -07001026 if (overlap && LoadStash(params, params.stashbase, srchash, true, nullptr, params.buffer,
1027 true) == 0) {
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001028 // Overlapping source blocks were previously stashed, command can proceed.
1029 // We are recovering from an interrupted command, so we don't know if the
1030 // stash can safely be deleted after this command.
Tao Bao0940fe12015-08-27 16:41:21 -07001031 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001032 }
1033
1034 // Valid source data not available, update cannot be resumed
1035 fprintf(stderr, "partition has unexpected contents\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001036 params.isunresumable = true;
Sami Tolvanen90221202014-12-09 16:39:47 +00001037
Tao Bao0940fe12015-08-27 16:41:21 -07001038 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001039}
1040
Tao Bao0940fe12015-08-27 16:41:21 -07001041static int PerformCommandMove(CommandParameters& params) {
1042 size_t blocks = 0;
Tao Baoe6aa3322015-08-05 15:20:27 -07001043 bool overlap = false;
Sami Tolvanen90221202014-12-09 16:39:47 +00001044 int status = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001045 RangeSet tgt;
Sami Tolvanen90221202014-12-09 16:39:47 +00001046
Tao Bao0940fe12015-08-27 16:41:21 -07001047 if (params.version == 1) {
Tao Baobaad2d42015-12-06 16:56:27 -08001048 status = LoadSrcTgtVersion1(params, tgt, blocks, params.buffer, params.fd);
Tao Bao0940fe12015-08-27 16:41:21 -07001049 } else if (params.version == 2) {
Tao Baobaad2d42015-12-06 16:56:27 -08001050 status = LoadSrcTgtVersion2(params, tgt, blocks, params.buffer, params.fd,
Tao Bao612336d2015-08-27 16:41:21 -07001051 params.stashbase, nullptr);
Tao Bao0940fe12015-08-27 16:41:21 -07001052 } else if (params.version >= 3) {
Tao Bao34847b22015-09-08 11:05:49 -07001053 status = LoadSrcTgtVersion3(params, tgt, blocks, true, overlap);
Sami Tolvanen90221202014-12-09 16:39:47 +00001054 }
1055
1056 if (status == -1) {
1057 fprintf(stderr, "failed to read blocks for move\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001058 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001059 }
1060
1061 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001062 params.foundwrites = true;
1063 } else if (params.foundwrites) {
1064 fprintf(stderr, "warning: commands executed out of order [%s]\n", params.cmdname);
Sami Tolvanen90221202014-12-09 16:39:47 +00001065 }
1066
Tao Bao0940fe12015-08-27 16:41:21 -07001067 if (params.canwrite) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001068 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001069 fprintf(stderr, " moving %zu blocks\n", blocks);
Sami Tolvanen90221202014-12-09 16:39:47 +00001070
Tao Bao0940fe12015-08-27 16:41:21 -07001071 if (WriteBlocks(tgt, params.buffer, params.fd) == -1) {
1072 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001073 }
1074 } else {
Tao Bao0940fe12015-08-27 16:41:21 -07001075 fprintf(stderr, "skipping %zu already moved blocks\n", blocks);
Sami Tolvanen90221202014-12-09 16:39:47 +00001076 }
1077
1078 }
1079
Tao Baobaad2d42015-12-06 16:56:27 -08001080 if (!params.freestash.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -07001081 FreeStash(params.stashbase, params.freestash);
Tao Baobaad2d42015-12-06 16:56:27 -08001082 params.freestash.clear();
Sami Tolvanen90221202014-12-09 16:39:47 +00001083 }
1084
Tao Bao0940fe12015-08-27 16:41:21 -07001085 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001086
Tao Bao0940fe12015-08-27 16:41:21 -07001087 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001088}
1089
Tao Bao0940fe12015-08-27 16:41:21 -07001090static int PerformCommandStash(CommandParameters& params) {
Tao Baobaad2d42015-12-06 16:56:27 -08001091 return SaveStash(params, params.stashbase, params.buffer, params.fd,
Tao Bao612336d2015-08-27 16:41:21 -07001092 (params.version >= 3));
Sami Tolvanen90221202014-12-09 16:39:47 +00001093}
1094
Tao Bao0940fe12015-08-27 16:41:21 -07001095static int PerformCommandFree(CommandParameters& params) {
Tao Baobaad2d42015-12-06 16:56:27 -08001096 // <stash_id>
1097 if (params.cpos >= params.tokens.size()) {
1098 fprintf(stderr, "missing stash id in free command\n");
1099 return -1;
1100 }
1101
Tianjie Xu7eca97e2016-03-22 18:08:12 -07001102 const std::string& id = params.tokens[params.cpos++];
1103
1104 if (!params.canwrite && stash_map.find(id) != stash_map.end()) {
1105 stash_map.erase(id);
1106 return 0;
1107 }
1108
Tao Bao0940fe12015-08-27 16:41:21 -07001109 if (params.createdstash || params.canwrite) {
Tianjie Xu7eca97e2016-03-22 18:08:12 -07001110 return FreeStash(params.stashbase, id);
Sami Tolvanen90221202014-12-09 16:39:47 +00001111 }
1112
1113 return 0;
1114}
1115
Tao Bao0940fe12015-08-27 16:41:21 -07001116static int PerformCommandZero(CommandParameters& params) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001117
Tao Baobaad2d42015-12-06 16:56:27 -08001118 if (params.cpos >= params.tokens.size()) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001119 fprintf(stderr, "missing target blocks for zero\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001120 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001121 }
1122
Tao Bao0940fe12015-08-27 16:41:21 -07001123 RangeSet tgt;
Tao Baobaad2d42015-12-06 16:56:27 -08001124 parse_range(params.tokens[params.cpos++], tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001125
Tao Bao0940fe12015-08-27 16:41:21 -07001126 fprintf(stderr, " zeroing %zu blocks\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001127
Tao Bao612336d2015-08-27 16:41:21 -07001128 allocate(BLOCKSIZE, params.buffer);
1129 memset(params.buffer.data(), 0, BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +00001130
Tao Bao0940fe12015-08-27 16:41:21 -07001131 if (params.canwrite) {
1132 for (size_t i = 0; i < tgt.count; ++i) {
Tianjie Xu7ce287d2016-05-31 09:29:49 -07001133 off64_t offset = static_cast<off64_t>(tgt.pos[i * 2]) * BLOCKSIZE;
1134 size_t size = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * BLOCKSIZE;
1135 if (!discard_blocks(params.fd, offset, size)) {
1136 return -1;
1137 }
1138
1139 if (!check_lseek(params.fd, offset, SEEK_SET)) {
Tao Bao0940fe12015-08-27 16:41:21 -07001140 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001141 }
1142
Tao Bao0940fe12015-08-27 16:41:21 -07001143 for (size_t j = tgt.pos[i * 2]; j < tgt.pos[i * 2 + 1]; ++j) {
1144 if (write_all(params.fd, params.buffer, BLOCKSIZE) == -1) {
1145 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001146 }
1147 }
1148 }
1149 }
1150
Tao Bao0940fe12015-08-27 16:41:21 -07001151 if (params.cmdname[0] == 'z') {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001152 // Update only for the zero command, as the erase command will call
1153 // this if DEBUG_ERASE is defined.
Tao Bao0940fe12015-08-27 16:41:21 -07001154 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001155 }
1156
Tao Bao0940fe12015-08-27 16:41:21 -07001157 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001158}
1159
Tao Bao0940fe12015-08-27 16:41:21 -07001160static int PerformCommandNew(CommandParameters& params) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001161
Tao Baobaad2d42015-12-06 16:56:27 -08001162 if (params.cpos >= params.tokens.size()) {
1163 fprintf(stderr, "missing target blocks for new\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001164 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001165 }
1166
Tao Bao0940fe12015-08-27 16:41:21 -07001167 RangeSet tgt;
Tao Baobaad2d42015-12-06 16:56:27 -08001168 parse_range(params.tokens[params.cpos++], tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001169
Tao Bao0940fe12015-08-27 16:41:21 -07001170 if (params.canwrite) {
1171 fprintf(stderr, " writing %zu blocks of new data\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001172
Tao Bao0940fe12015-08-27 16:41:21 -07001173 RangeSinkState rss(tgt);
1174 rss.fd = params.fd;
Sami Tolvanen90221202014-12-09 16:39:47 +00001175 rss.p_block = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001176 rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001177
Tianjie Xu7ce287d2016-05-31 09:29:49 -07001178 off64_t offset = static_cast<off64_t>(tgt.pos[0]) * BLOCKSIZE;
1179 if (!discard_blocks(params.fd, offset, tgt.size * BLOCKSIZE)) {
1180 return -1;
1181 }
1182
1183 if (!check_lseek(params.fd, offset, SEEK_SET)) {
Tao Bao0940fe12015-08-27 16:41:21 -07001184 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001185 }
1186
Tao Bao0940fe12015-08-27 16:41:21 -07001187 pthread_mutex_lock(&params.nti.mu);
1188 params.nti.rss = &rss;
1189 pthread_cond_broadcast(&params.nti.cv);
Sami Tolvanen90221202014-12-09 16:39:47 +00001190
Tao Bao0940fe12015-08-27 16:41:21 -07001191 while (params.nti.rss) {
1192 pthread_cond_wait(&params.nti.cv, &params.nti.mu);
Sami Tolvanen90221202014-12-09 16:39:47 +00001193 }
1194
Tao Bao0940fe12015-08-27 16:41:21 -07001195 pthread_mutex_unlock(&params.nti.mu);
Sami Tolvanen90221202014-12-09 16:39:47 +00001196 }
1197
Tao Bao0940fe12015-08-27 16:41:21 -07001198 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001199
Tao Bao0940fe12015-08-27 16:41:21 -07001200 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001201}
1202
Tao Bao0940fe12015-08-27 16:41:21 -07001203static int PerformCommandDiff(CommandParameters& params) {
Tao Bao0940fe12015-08-27 16:41:21 -07001204
Tao Baobaad2d42015-12-06 16:56:27 -08001205 // <offset> <length>
1206 if (params.cpos + 1 >= params.tokens.size()) {
1207 fprintf(stderr, "missing patch offset or length for %s\n", params.cmdname);
Tao Bao0940fe12015-08-27 16:41:21 -07001208 return -1;
1209 }
1210
Tao Baobaad2d42015-12-06 16:56:27 -08001211 size_t offset;
1212 if (!android::base::ParseUint(params.tokens[params.cpos++].c_str(), &offset)) {
1213 fprintf(stderr, "invalid patch offset\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001214 return -1;
1215 }
1216
Tao Baobaad2d42015-12-06 16:56:27 -08001217 size_t len;
1218 if (!android::base::ParseUint(params.tokens[params.cpos++].c_str(), &len)) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001219 fprintf(stderr, "invalid patch len\n");
Tao Baobaad2d42015-12-06 16:56:27 -08001220 return -1;
1221 }
Tao Bao0940fe12015-08-27 16:41:21 -07001222
Tao Bao612336d2015-08-27 16:41:21 -07001223 RangeSet tgt;
Tao Bao0940fe12015-08-27 16:41:21 -07001224 size_t blocks = 0;
Tao Bao612336d2015-08-27 16:41:21 -07001225 bool overlap = false;
Sami Tolvanen90221202014-12-09 16:39:47 +00001226 int status = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001227 if (params.version == 1) {
Tao Baobaad2d42015-12-06 16:56:27 -08001228 status = LoadSrcTgtVersion1(params, tgt, blocks, params.buffer, params.fd);
Tao Bao0940fe12015-08-27 16:41:21 -07001229 } else if (params.version == 2) {
Tao Baobaad2d42015-12-06 16:56:27 -08001230 status = LoadSrcTgtVersion2(params, tgt, blocks, params.buffer, params.fd,
Tao Bao612336d2015-08-27 16:41:21 -07001231 params.stashbase, nullptr);
Tao Bao0940fe12015-08-27 16:41:21 -07001232 } else if (params.version >= 3) {
Tao Bao34847b22015-09-08 11:05:49 -07001233 status = LoadSrcTgtVersion3(params, tgt, blocks, false, overlap);
Sami Tolvanen90221202014-12-09 16:39:47 +00001234 }
1235
1236 if (status == -1) {
1237 fprintf(stderr, "failed to read blocks for diff\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001238 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001239 }
1240
1241 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001242 params.foundwrites = true;
1243 } else if (params.foundwrites) {
1244 fprintf(stderr, "warning: commands executed out of order [%s]\n", params.cmdname);
Sami Tolvanen90221202014-12-09 16:39:47 +00001245 }
1246
Tao Bao0940fe12015-08-27 16:41:21 -07001247 if (params.canwrite) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001248 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001249 fprintf(stderr, "patching %zu blocks to %zu\n", blocks, tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001250
Tianjie Xuaced5d92016-10-12 10:55:04 -07001251 Value patch_value(VAL_BLOB,
1252 std::string(reinterpret_cast<const char*>(params.patch_start + offset), len));
Sami Tolvanen90221202014-12-09 16:39:47 +00001253
Tao Bao0940fe12015-08-27 16:41:21 -07001254 RangeSinkState rss(tgt);
1255 rss.fd = params.fd;
Sami Tolvanen90221202014-12-09 16:39:47 +00001256 rss.p_block = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001257 rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001258
Tianjie Xu7ce287d2016-05-31 09:29:49 -07001259 off64_t offset = static_cast<off64_t>(tgt.pos[0]) * BLOCKSIZE;
1260 if (!discard_blocks(params.fd, offset, rss.p_remain)) {
1261 return -1;
1262 }
1263
1264 if (!check_lseek(params.fd, offset, SEEK_SET)) {
Tao Bao0940fe12015-08-27 16:41:21 -07001265 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001266 }
1267
Tao Bao0940fe12015-08-27 16:41:21 -07001268 if (params.cmdname[0] == 'i') { // imgdiff
Tianjie Xu31f8cc82016-06-15 11:56:42 -07001269 if (ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value,
1270 &RangeSinkWrite, &rss, nullptr, nullptr) != 0) {
1271 fprintf(stderr, "Failed to apply image patch.\n");
1272 return -1;
1273 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001274 } else {
Tianjie Xu31f8cc82016-06-15 11:56:42 -07001275 if (ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value,
1276 0, &RangeSinkWrite, &rss, nullptr) != 0) {
1277 fprintf(stderr, "Failed to apply bsdiff patch.\n");
1278 return -1;
1279 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001280 }
1281
1282 // We expect the output of the patcher to fill the tgt ranges exactly.
Tao Bao0940fe12015-08-27 16:41:21 -07001283 if (rss.p_block != tgt.count || rss.p_remain != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001284 fprintf(stderr, "range sink underrun?\n");
1285 }
1286 } else {
Tao Bao0940fe12015-08-27 16:41:21 -07001287 fprintf(stderr, "skipping %zu blocks already patched to %zu [%s]\n",
Tao Baobaad2d42015-12-06 16:56:27 -08001288 blocks, tgt.size, params.cmdline);
Sami Tolvanen90221202014-12-09 16:39:47 +00001289 }
1290 }
1291
Tao Baobaad2d42015-12-06 16:56:27 -08001292 if (!params.freestash.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -07001293 FreeStash(params.stashbase, params.freestash);
Tao Baobaad2d42015-12-06 16:56:27 -08001294 params.freestash.clear();
Sami Tolvanen90221202014-12-09 16:39:47 +00001295 }
1296
Tao Bao0940fe12015-08-27 16:41:21 -07001297 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001298
Tao Bao0940fe12015-08-27 16:41:21 -07001299 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001300}
1301
Tao Bao0940fe12015-08-27 16:41:21 -07001302static int PerformCommandErase(CommandParameters& params) {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001303 if (DEBUG_ERASE) {
1304 return PerformCommandZero(params);
Sami Tolvanen90221202014-12-09 16:39:47 +00001305 }
1306
Tao Bao0940fe12015-08-27 16:41:21 -07001307 struct stat sb;
1308 if (fstat(params.fd, &sb) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001309 fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -07001310 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001311 }
1312
Tao Bao0940fe12015-08-27 16:41:21 -07001313 if (!S_ISBLK(sb.st_mode)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001314 fprintf(stderr, "not a block device; skipping erase\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001315 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001316 }
1317
Tao Baobaad2d42015-12-06 16:56:27 -08001318 if (params.cpos >= params.tokens.size()) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001319 fprintf(stderr, "missing target blocks for erase\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001320 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001321 }
1322
Tao Bao0940fe12015-08-27 16:41:21 -07001323 RangeSet tgt;
Tao Baobaad2d42015-12-06 16:56:27 -08001324 parse_range(params.tokens[params.cpos++], tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001325
Tao Bao0940fe12015-08-27 16:41:21 -07001326 if (params.canwrite) {
1327 fprintf(stderr, " erasing %zu blocks\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001328
Tao Bao0940fe12015-08-27 16:41:21 -07001329 for (size_t i = 0; i < tgt.count; ++i) {
1330 uint64_t blocks[2];
Sami Tolvanen90221202014-12-09 16:39:47 +00001331 // offset in bytes
Tao Bao0940fe12015-08-27 16:41:21 -07001332 blocks[0] = tgt.pos[i * 2] * (uint64_t) BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001333 // length in bytes
Tao Bao0940fe12015-08-27 16:41:21 -07001334 blocks[1] = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * (uint64_t) BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001335
Tao Bao0940fe12015-08-27 16:41:21 -07001336 if (ioctl(params.fd, BLKDISCARD, &blocks) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001337 fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -07001338 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001339 }
1340 }
1341 }
1342
Tao Bao0940fe12015-08-27 16:41:21 -07001343 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001344}
1345
1346// Definitions for transfer list command functions
Tao Bao0940fe12015-08-27 16:41:21 -07001347typedef int (*CommandFunction)(CommandParameters&);
Sami Tolvanen90221202014-12-09 16:39:47 +00001348
Tao Bao612336d2015-08-27 16:41:21 -07001349struct Command {
Sami Tolvanen90221202014-12-09 16:39:47 +00001350 const char* name;
1351 CommandFunction f;
Tao Bao612336d2015-08-27 16:41:21 -07001352};
Sami Tolvanen90221202014-12-09 16:39:47 +00001353
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001354// args:
1355// - block device (or file) to modify in-place
1356// - transfer list (blob)
1357// - new data stream (filename within package.zip)
1358// - patch stream (filename within package.zip, must be uncompressed)
1359
Tao Bao0940fe12015-08-27 16:41:21 -07001360static Value* PerformBlockImageUpdate(const char* name, State* state, int /* argc */, Expr* argv[],
1361 const Command* commands, size_t cmdcount, bool dryrun) {
Tao Bao73064612016-04-26 17:14:32 -07001362 CommandParameters params = {};
Sami Tolvanen90221202014-12-09 16:39:47 +00001363 params.canwrite = !dryrun;
1364
1365 fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update");
Tianjie Xu7ce287d2016-05-31 09:29:49 -07001366 if (state->is_retry) {
1367 is_retry = true;
1368 fprintf(stderr, "This update is a retry.\n");
1369 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001370
Tao Bao612336d2015-08-27 16:41:21 -07001371 Value* blockdev_filename = nullptr;
1372 Value* transfer_list_value = nullptr;
1373 Value* new_data_fn = nullptr;
1374 Value* patch_data_fn = nullptr;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001375 if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value,
Sami Tolvanen90221202014-12-09 16:39:47 +00001376 &new_data_fn, &patch_data_fn) < 0) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001377 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001378 }
Tianjie Xuaced5d92016-10-12 10:55:04 -07001379 std::unique_ptr<Value> blockdev_filename_holder(blockdev_filename);
1380 std::unique_ptr<Value> transfer_list_value_holder(transfer_list_value);
1381 std::unique_ptr<Value> new_data_fn_holder(new_data_fn);
1382 std::unique_ptr<Value> patch_data_fn_holder(patch_data_fn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001383
1384 if (blockdev_filename->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001385 ErrorAbort(state, kArgsParsingFailure, "blockdev_filename argument to %s must be string",
1386 name);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001387 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001388 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001389 if (transfer_list_value->type != VAL_BLOB) {
Tianjie Xu16255832016-04-30 11:49:59 -07001390 ErrorAbort(state, kArgsParsingFailure, "transfer_list argument to %s must be blob", name);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001391 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001392 }
1393 if (new_data_fn->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001394 ErrorAbort(state, kArgsParsingFailure, "new_data_fn argument to %s must be string", name);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001395 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001396 }
1397 if (patch_data_fn->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001398 ErrorAbort(state, kArgsParsingFailure, "patch_data_fn argument to %s must be string",
1399 name);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001400 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001401 }
1402
Tao Bao612336d2015-08-27 16:41:21 -07001403 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001404
Tao Bao0940fe12015-08-27 16:41:21 -07001405 if (ui == nullptr) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001406 return StringValue("");
Sami Tolvanen90221202014-12-09 16:39:47 +00001407 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001408
Tao Bao612336d2015-08-27 16:41:21 -07001409 FILE* cmd_pipe = ui->cmd_pipe;
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -07001410 ZipArchiveHandle za = ui->package_zip;
Sami Tolvanen90221202014-12-09 16:39:47 +00001411
Tao Bao0940fe12015-08-27 16:41:21 -07001412 if (cmd_pipe == nullptr || za == nullptr) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001413 return StringValue("");
Sami Tolvanen90221202014-12-09 16:39:47 +00001414 }
1415
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -07001416 ZipString path_data(patch_data_fn->data.c_str());
1417 ZipEntry patch_entry;
1418 if (FindEntry(za, path_data, &patch_entry) != 0) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001419 fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data.c_str());
1420 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001421 }
1422
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -07001423 params.patch_start = ui->package_zip_addr + patch_entry.offset;
1424 ZipString new_data(new_data_fn->data.c_str());
1425 ZipEntry new_entry;
1426 if (FindEntry(za, new_data, &new_entry) != 0) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001427 fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data.c_str());
1428 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001429 }
1430
Tianjie Xuaced5d92016-10-12 10:55:04 -07001431 params.fd.reset(TEMP_FAILURE_RETRY(ota_open(blockdev_filename->data.c_str(), O_RDWR)));
Sami Tolvanen90221202014-12-09 16:39:47 +00001432 if (params.fd == -1) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001433 fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data.c_str(), strerror(errno));
1434 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001435 }
1436
Sami Tolvanen90221202014-12-09 16:39:47 +00001437 if (params.canwrite) {
1438 params.nti.za = za;
1439 params.nti.entry = new_entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001440
Tao Bao0940fe12015-08-27 16:41:21 -07001441 pthread_mutex_init(&params.nti.mu, nullptr);
1442 pthread_cond_init(&params.nti.cv, nullptr);
Tao Bao612336d2015-08-27 16:41:21 -07001443 pthread_attr_t attr;
Sami Tolvanen90221202014-12-09 16:39:47 +00001444 pthread_attr_init(&attr);
1445 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1446
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001447 int error = pthread_create(&params.thread, &attr, unzip_new_data, &params.nti);
1448 if (error != 0) {
1449 fprintf(stderr, "pthread_create failed: %s\n", strerror(error));
Tianjie Xuaced5d92016-10-12 10:55:04 -07001450 return StringValue("");
Sami Tolvanen90221202014-12-09 16:39:47 +00001451 }
1452 }
1453
Tianjie Xuaced5d92016-10-12 10:55:04 -07001454 std::vector<std::string> lines = android::base::Split(transfer_list_value->data, "\n");
Tao Baobaad2d42015-12-06 16:56:27 -08001455 if (lines.size() < 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001456 ErrorAbort(state, kArgsParsingFailure, "too few lines in the transfer list [%zd]\n",
1457 lines.size());
Tianjie Xuaced5d92016-10-12 10:55:04 -07001458 return StringValue("");
Tao Baobaad2d42015-12-06 16:56:27 -08001459 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001460
Sami Tolvanen90221202014-12-09 16:39:47 +00001461 // First line in transfer list is the version number
Tao Bao1fdec862015-10-21 14:57:44 -07001462 if (!android::base::ParseInt(lines[0].c_str(), &params.version, 1, 4)) {
Tao Bao612336d2015-08-27 16:41:21 -07001463 fprintf(stderr, "unexpected transfer list version [%s]\n", lines[0].c_str());
Tianjie Xuaced5d92016-10-12 10:55:04 -07001464 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001465 }
1466
Sami Tolvanen90221202014-12-09 16:39:47 +00001467 fprintf(stderr, "blockimg version is %d\n", params.version);
1468
1469 // Second line in transfer list is the total number of blocks we expect to write
Tao Baob15fd222015-09-24 11:10:51 -07001470 int total_blocks;
1471 if (!android::base::ParseInt(lines[1].c_str(), &total_blocks, 0)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001472 ErrorAbort(state, kArgsParsingFailure, "unexpected block count [%s]\n", lines[1].c_str());
Tianjie Xuaced5d92016-10-12 10:55:04 -07001473 return StringValue("");
Tao Baob15fd222015-09-24 11:10:51 -07001474 }
1475
1476 if (total_blocks == 0) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001477 return StringValue("t");
Sami Tolvanen90221202014-12-09 16:39:47 +00001478 }
1479
Tao Bao612336d2015-08-27 16:41:21 -07001480 size_t start = 2;
Sami Tolvanen90221202014-12-09 16:39:47 +00001481 if (params.version >= 2) {
Tao Baobaad2d42015-12-06 16:56:27 -08001482 if (lines.size() < 4) {
Tianjie Xu16255832016-04-30 11:49:59 -07001483 ErrorAbort(state, kArgsParsingFailure, "too few lines in the transfer list [%zu]\n",
1484 lines.size());
Tianjie Xuaced5d92016-10-12 10:55:04 -07001485 return StringValue("");
Tao Baobaad2d42015-12-06 16:56:27 -08001486 }
1487
Sami Tolvanen90221202014-12-09 16:39:47 +00001488 // Third line is how many stash entries are needed simultaneously
Tao Bao612336d2015-08-27 16:41:21 -07001489 fprintf(stderr, "maximum stash entries %s\n", lines[2].c_str());
Doug Zongker52ae67d2014-09-08 12:22:09 -07001490
Sami Tolvanen90221202014-12-09 16:39:47 +00001491 // Fourth line is the maximum number of blocks that will be stashed simultaneously
Tao Baob15fd222015-09-24 11:10:51 -07001492 int stash_max_blocks;
1493 if (!android::base::ParseInt(lines[3].c_str(), &stash_max_blocks, 0)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001494 ErrorAbort(state, kArgsParsingFailure, "unexpected maximum stash blocks [%s]\n",
1495 lines[3].c_str());
Tianjie Xuaced5d92016-10-12 10:55:04 -07001496 return StringValue("");
Doug Zongker52ae67d2014-09-08 12:22:09 -0700