blob: 696c1bba421a0c7c316e750eca00139a2913a253 [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>
Tao Baoba9a42a2015-06-23 23:23:33 -070021#include <linux/fs.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070022#include <pthread.h>
23#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Sami Tolvanen90221202014-12-09 16:39:47 +000027#include <sys/stat.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070028#include <sys/types.h>
29#include <sys/wait.h>
30#include <sys/ioctl.h>
31#include <time.h>
32#include <unistd.h>
Sami Tolvanen0a7b4732015-06-25 10:25:36 +010033#include <fec/io.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070034
Tao Baoe6aa3322015-08-05 15:20:27 -070035#include <memory>
36#include <string>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070037#include <unordered_map>
Tao Bao0940fe12015-08-27 16:41:21 -070038#include <vector>
Tao Baoe6aa3322015-08-05 15:20:27 -070039
Tao Bao039f2da2016-11-22 16:29:50 -080040#include <android-base/logging.h>
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>
Tao Bao51412212016-12-28 14:44:05 -080044#include <applypatch/applypatch.h>
45#include <openssl/sha.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070046#include <ziparchive/zip_archive.h>
Tao Baoe6aa3322015-08-05 15:20:27 -070047
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070048#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070049#include "error_code.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070050#include "updater/install.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
Sami Tolvanene82fa182015-06-10 15:58:12 +000055// Set this to 0 to interpret 'erase' transfers to mean do a
56// BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret
57// erase to mean fill the region with zeroes.
58#define DEBUG_ERASE 0
59
Tao Bao51412212016-12-28 14:44:05 -080060static constexpr size_t BLOCKSIZE = 4096;
61static constexpr const char* STASH_DIRECTORY_BASE = "/cache/recovery";
62static constexpr mode_t STASH_DIRECTORY_MODE = 0700;
63static constexpr mode_t STASH_FILE_MODE = 0600;
Sami Tolvanen90221202014-12-09 16:39:47 +000064
Tao Bao0940fe12015-08-27 16:41:21 -070065struct RangeSet {
66 size_t count; // Limit is INT_MAX.
Shrinivas Sahukara6153df2015-08-19 13:01:45 +053067 size_t size;
Tao Bao0940fe12015-08-27 16:41:21 -070068 std::vector<size_t> pos; // Actual limit is INT_MAX.
69};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070070
Tianjie Xu16255832016-04-30 11:49:59 -070071static CauseCode failure_type = kNoCause;
Tianjie Xu7ce287d2016-05-31 09:29:49 -070072static bool is_retry = false;
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070073static std::unordered_map<std::string, RangeSet> stash_map;
Tianjie Xu7eca97e2016-03-22 18:08:12 -070074
Tao Baobaad2d42015-12-06 16:56:27 -080075static void parse_range(const std::string& range_text, RangeSet& rs) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010076
Tao Baobaad2d42015-12-06 16:56:27 -080077 std::vector<std::string> pieces = android::base::Split(range_text, ",");
Tao Bao0940fe12015-08-27 16:41:21 -070078 if (pieces.size() < 3) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010079 goto err;
80 }
81
Tao Baob15fd222015-09-24 11:10:51 -070082 size_t num;
83 if (!android::base::ParseUint(pieces[0].c_str(), &num, static_cast<size_t>(INT_MAX))) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010084 goto err;
85 }
86
Tao Baob15fd222015-09-24 11:10:51 -070087 if (num == 0 || num % 2) {
88 goto err; // must be even
89 } else if (num != pieces.size() - 1) {
90 goto err;
91 }
Sami Tolvanenf2bac042015-05-12 12:48:46 +010092
Tao Bao0940fe12015-08-27 16:41:21 -070093 rs.pos.resize(num);
94 rs.count = num / 2;
95 rs.size = 0;
Sami Tolvanenf2bac042015-05-12 12:48:46 +010096
Tao Bao0940fe12015-08-27 16:41:21 -070097 for (size_t i = 0; i < num; i += 2) {
Tao Baob15fd222015-09-24 11:10:51 -070098 if (!android::base::ParseUint(pieces[i+1].c_str(), &rs.pos[i],
99 static_cast<size_t>(INT_MAX))) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100100 goto err;
101 }
102
Tao Baob15fd222015-09-24 11:10:51 -0700103 if (!android::base::ParseUint(pieces[i+2].c_str(), &rs.pos[i+1],
104 static_cast<size_t>(INT_MAX))) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530105 goto err;
106 }
107
Tao Bao0940fe12015-08-27 16:41:21 -0700108 if (rs.pos[i] >= rs.pos[i+1]) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530109 goto err; // empty or negative range
110 }
111
Tao Bao0940fe12015-08-27 16:41:21 -0700112 size_t sz = rs.pos[i+1] - rs.pos[i];
113 if (rs.size > SIZE_MAX - sz) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530114 goto err; // overflow
115 }
116
Tao Bao0940fe12015-08-27 16:41:21 -0700117 rs.size += sz;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700118 }
119
Tao Bao0940fe12015-08-27 16:41:21 -0700120 return;
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100121
122err:
Tao Bao039f2da2016-11-22 16:29:50 -0800123 LOG(ERROR) << "failed to parse range '" << range_text << "'";
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100124 exit(1);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700125}
126
Tao Baoe6aa3322015-08-05 15:20:27 -0700127static bool range_overlaps(const RangeSet& r1, const RangeSet& r2) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530128 for (size_t i = 0; i < r1.count; ++i) {
129 size_t r1_0 = r1.pos[i * 2];
130 size_t r1_1 = r1.pos[i * 2 + 1];
Sami Tolvanen90221202014-12-09 16:39:47 +0000131
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530132 for (size_t j = 0; j < r2.count; ++j) {
133 size_t r2_0 = r2.pos[j * 2];
134 size_t r2_1 = r2.pos[j * 2 + 1];
Sami Tolvanen90221202014-12-09 16:39:47 +0000135
Tao Baoc0f56ad2015-06-25 14:00:31 -0700136 if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700137 return true;
Sami Tolvanen90221202014-12-09 16:39:47 +0000138 }
139 }
140 }
141
Tao Baoe6aa3322015-08-05 15:20:27 -0700142 return false;
Sami Tolvanen90221202014-12-09 16:39:47 +0000143}
144
145static int read_all(int fd, uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700146 size_t so_far = 0;
147 while (so_far < size) {
Jed Estepa7b9a462015-12-15 16:04:53 -0800148 ssize_t r = TEMP_FAILURE_RETRY(ota_read(fd, data+so_far, size-so_far));
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700149 if (r == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700150 failure_type = kFreadFailure;
Tao Bao039f2da2016-11-22 16:29:50 -0800151 PLOG(ERROR) << "read failed";
Sami Tolvanen90221202014-12-09 16:39:47 +0000152 return -1;
Tianjie Xu71e182b2016-08-31 18:06:33 -0700153 } else if (r == 0) {
154 failure_type = kFreadFailure;
Tao Bao039f2da2016-11-22 16:29:50 -0800155 LOG(ERROR) << "read reached unexpected EOF.";
Tianjie Xu71e182b2016-08-31 18:06:33 -0700156 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700157 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700158 so_far += r;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700159 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000160 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700161}
162
Tao Bao612336d2015-08-27 16:41:21 -0700163static int read_all(int fd, std::vector<uint8_t>& buffer, size_t size) {
164 return read_all(fd, buffer.data(), size);
165}
166
Sami Tolvanen90221202014-12-09 16:39:47 +0000167static int write_all(int fd, const uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700168 size_t written = 0;
169 while (written < size) {
Jed Estepa7b9a462015-12-15 16:04:53 -0800170 ssize_t w = TEMP_FAILURE_RETRY(ota_write(fd, data+written, size-written));
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700171 if (w == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700172 failure_type = kFwriteFailure;
Tao Bao039f2da2016-11-22 16:29:50 -0800173 PLOG(ERROR) << "write failed";
Sami Tolvanen90221202014-12-09 16:39:47 +0000174 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700175 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700176 written += w;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700177 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000178
Sami Tolvanen90221202014-12-09 16:39:47 +0000179 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700180}
181
Tao Bao612336d2015-08-27 16:41:21 -0700182static int write_all(int fd, const std::vector<uint8_t>& buffer, size_t size) {
183 return write_all(fd, buffer.data(), size);
184}
185
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700186static bool discard_blocks(int fd, off64_t offset, uint64_t size) {
187 // Don't discard blocks unless the update is a retry run.
188 if (!is_retry) {
189 return true;
190 }
191
192 uint64_t args[2] = {static_cast<uint64_t>(offset), size};
193 int status = ioctl(fd, BLKDISCARD, &args);
194 if (status == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800195 PLOG(ERROR) << "BLKDISCARD ioctl failed";
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700196 return false;
197 }
198 return true;
199}
200
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700201static bool check_lseek(int fd, off64_t offset, int whence) {
202 off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
203 if (rc == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700204 failure_type = kLseekFailure;
Tao Bao039f2da2016-11-22 16:29:50 -0800205 PLOG(ERROR) << "lseek64 failed";
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700206 return false;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700207 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700208 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700209}
210
Tao Bao612336d2015-08-27 16:41:21 -0700211static void allocate(size_t size, std::vector<uint8_t>& buffer) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700212 // if the buffer's big enough, reuse it.
Tao Bao612336d2015-08-27 16:41:21 -0700213 if (size <= buffer.size()) return;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700214
Tao Bao612336d2015-08-27 16:41:21 -0700215 buffer.resize(size);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700216}
217
Tao Bao0940fe12015-08-27 16:41:21 -0700218struct RangeSinkState {
Chih-Hung Hsieh49c5c792016-04-29 14:16:35 -0700219 explicit RangeSinkState(RangeSet& rs) : tgt(rs) { };
Tao Bao0940fe12015-08-27 16:41:21 -0700220
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700221 int fd;
Tao Bao0940fe12015-08-27 16:41:21 -0700222 const RangeSet& tgt;
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530223 size_t p_block;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700224 size_t p_remain;
Tao Bao0940fe12015-08-27 16:41:21 -0700225};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700226
227static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) {
Tao Bao0940fe12015-08-27 16:41:21 -0700228 RangeSinkState* rss = reinterpret_cast<RangeSinkState*>(token);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700229
Tao Bao0940fe12015-08-27 16:41:21 -0700230 if (rss->p_remain == 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800231 LOG(ERROR) << "range sink write overrun";
Sami Tolvanen90221202014-12-09 16:39:47 +0000232 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700233 }
234
235 ssize_t written = 0;
236 while (size > 0) {
237 size_t write_now = size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000238
239 if (rss->p_remain < write_now) {
240 write_now = rss->p_remain;
241 }
242
243 if (write_all(rss->fd, data, write_now) == -1) {
244 break;
245 }
246
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700247 data += write_now;
248 size -= write_now;
249
250 rss->p_remain -= write_now;
251 written += write_now;
252
253 if (rss->p_remain == 0) {
254 // move to the next block
255 ++rss->p_block;
Tao Bao0940fe12015-08-27 16:41:21 -0700256 if (rss->p_block < rss->tgt.count) {
257 rss->p_remain = (rss->tgt.pos[rss->p_block * 2 + 1] -
258 rss->tgt.pos[rss->p_block * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000259
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700260 off64_t offset = static_cast<off64_t>(rss->tgt.pos[rss->p_block*2]) * BLOCKSIZE;
261 if (!discard_blocks(rss->fd, offset, rss->p_remain)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000262 break;
263 }
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700264
265 if (!check_lseek(rss->fd, offset, SEEK_SET)) {
266 break;
267 }
268
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700269 } else {
270 // we can't write any more; return how many bytes have
271 // been written so far.
Sami Tolvanen90221202014-12-09 16:39:47 +0000272 break;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700273 }
274 }
275 }
276
277 return written;
278}
279
280// All of the data for all the 'new' transfers is contained in one
281// file in the update package, concatenated together in the order in
282// which transfers.list will need it. We want to stream it out of the
283// archive (it's compressed) without writing it to a temp file, but we
284// can't write each section until it's that transfer's turn to go.
285//
286// To achieve this, we expand the new data from the archive in a
287// background thread, and block that threads 'receive uncompressed
288// data' function until the main thread has reached a point where we
289// want some new data to be written. We signal the background thread
290// with the destination for the data and block the main thread,
291// waiting for the background thread to complete writing that section.
292// Then it signals the main thread to wake up and goes back to
293// blocking waiting for a transfer.
294//
295// NewThreadInfo is the struct used to pass information back and forth
296// between the two threads. When the main thread wants some data
297// written, it sets rss to the destination location and signals the
298// condition. When the background thread is done writing, it clears
299// rss and signals the condition again.
300
Tao Bao0940fe12015-08-27 16:41:21 -0700301struct NewThreadInfo {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700302 ZipArchiveHandle za;
303 ZipEntry entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700304
305 RangeSinkState* rss;
306
307 pthread_mutex_t mu;
308 pthread_cond_t cv;
Tao Bao0940fe12015-08-27 16:41:21 -0700309};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700310
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700311static bool receive_new_data(const uint8_t* data, size_t size, void* cookie) {
Tao Bao0940fe12015-08-27 16:41:21 -0700312 NewThreadInfo* nti = reinterpret_cast<NewThreadInfo*>(cookie);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700313
314 while (size > 0) {
Tao Bao0940fe12015-08-27 16:41:21 -0700315 // Wait for nti->rss to be non-null, indicating some of this
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700316 // data is wanted.
317 pthread_mutex_lock(&nti->mu);
Tao Bao0940fe12015-08-27 16:41:21 -0700318 while (nti->rss == nullptr) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700319 pthread_cond_wait(&nti->cv, &nti->mu);
320 }
321 pthread_mutex_unlock(&nti->mu);
322
323 // At this point nti->rss is set, and we own it. The main
324 // thread is waiting for it to disappear from nti.
325 ssize_t written = RangeSinkWrite(data, size, nti->rss);
326 data += written;
327 size -= written;
328
Tao Bao0940fe12015-08-27 16:41:21 -0700329 if (nti->rss->p_block == nti->rss->tgt.count) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700330 // we have written all the bytes desired by this rss.
331
332 pthread_mutex_lock(&nti->mu);
Tao Bao0940fe12015-08-27 16:41:21 -0700333 nti->rss = nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700334 pthread_cond_broadcast(&nti->cv);
335 pthread_mutex_unlock(&nti->mu);
336 }
337 }
338
339 return true;
340}
341
342static void* unzip_new_data(void* cookie) {
343 NewThreadInfo* nti = (NewThreadInfo*) cookie;
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700344 ProcessZipEntryContents(nti->za, &nti->entry, receive_new_data, nti);
Tao Bao0940fe12015-08-27 16:41:21 -0700345 return nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700346}
347
Tao Bao612336d2015-08-27 16:41:21 -0700348static int ReadBlocks(const RangeSet& src, std::vector<uint8_t>& buffer, int fd) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000349 size_t p = 0;
Tao Bao612336d2015-08-27 16:41:21 -0700350 uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000351
Tao Bao0940fe12015-08-27 16:41:21 -0700352 for (size_t i = 0; i < src.count; ++i) {
353 if (!check_lseek(fd, (off64_t) src.pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000354 return -1;
355 }
356
Tao Bao0940fe12015-08-27 16:41:21 -0700357 size_t size = (src.pos[i * 2 + 1] - src.pos[i * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000358
Tao Bao612336d2015-08-27 16:41:21 -0700359 if (read_all(fd, data + p, size) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000360 return -1;
361 }
362
363 p += size;
364 }
365
366 return 0;
367}
368
Tao Bao612336d2015-08-27 16:41:21 -0700369static int WriteBlocks(const RangeSet& tgt, const std::vector<uint8_t>& buffer, int fd) {
370 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000371
Tao Bao0940fe12015-08-27 16:41:21 -0700372 size_t p = 0;
373 for (size_t i = 0; i < tgt.count; ++i) {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700374 off64_t offset = static_cast<off64_t>(tgt.pos[i * 2]) * BLOCKSIZE;
375 size_t size = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * BLOCKSIZE;
376 if (!discard_blocks(fd, offset, size)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000377 return -1;
378 }
379
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700380 if (!check_lseek(fd, offset, SEEK_SET)) {
381 return -1;
382 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000383
Tao Bao612336d2015-08-27 16:41:21 -0700384 if (write_all(fd, data + p, size) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000385 return -1;
386 }
387
388 p += size;
389 }
390
391 return 0;
392}
393
Tao Baobaad2d42015-12-06 16:56:27 -0800394// Parameters for transfer list command functions
395struct CommandParameters {
396 std::vector<std::string> tokens;
397 size_t cpos;
398 const char* cmdname;
399 const char* cmdline;
400 std::string freestash;
401 std::string stashbase;
402 bool canwrite;
403 int createdstash;
Elliott Hughesbcabd092016-03-22 20:19:22 -0700404 android::base::unique_fd fd;
Tao Baobaad2d42015-12-06 16:56:27 -0800405 bool foundwrites;
406 bool isunresumable;
407 int version;
408 size_t written;
Tianjie Xudd874b12016-05-13 12:13:15 -0700409 size_t stashed;
Tao Baobaad2d42015-12-06 16:56:27 -0800410 NewThreadInfo nti;
411 pthread_t thread;
412 std::vector<uint8_t> buffer;
413 uint8_t* patch_start;
414};
415
Doug Zongker52ae67d2014-09-08 12:22:09 -0700416// Do a source/target load for move/bsdiff/imgdiff in version 1.
Tao Baobaad2d42015-12-06 16:56:27 -0800417// We expect to parse the remainder of the parameter tokens as:
Doug Zongker52ae67d2014-09-08 12:22:09 -0700418//
419// <src_range> <tgt_range>
420//
421// The source range is loaded into the provided buffer, reallocating
Tao Bao34847b22015-09-08 11:05:49 -0700422// it to make it larger if necessary.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700423
Tao Baobaad2d42015-12-06 16:56:27 -0800424static int LoadSrcTgtVersion1(CommandParameters& params, RangeSet& tgt, size_t& src_blocks,
Tao Bao612336d2015-08-27 16:41:21 -0700425 std::vector<uint8_t>& buffer, int fd) {
Tao Baobaad2d42015-12-06 16:56:27 -0800426
427 if (params.cpos + 1 >= params.tokens.size()) {
Tao Bao039f2da2016-11-22 16:29:50 -0800428 LOG(ERROR) << "invalid parameters";
Tao Baobaad2d42015-12-06 16:56:27 -0800429 return -1;
430 }
431
Tao Bao612336d2015-08-27 16:41:21 -0700432 // <src_range>
Tao Bao0940fe12015-08-27 16:41:21 -0700433 RangeSet src;
Tao Baobaad2d42015-12-06 16:56:27 -0800434 parse_range(params.tokens[params.cpos++], src);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700435
Tao Bao612336d2015-08-27 16:41:21 -0700436 // <tgt_range>
Tao Baobaad2d42015-12-06 16:56:27 -0800437 parse_range(params.tokens[params.cpos++], tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700438
Tao Bao612336d2015-08-27 16:41:21 -0700439 allocate(src.size * BLOCKSIZE, buffer);
440 int rc = ReadBlocks(src, buffer, fd);
Tao Bao0940fe12015-08-27 16:41:21 -0700441 src_blocks = src.size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000442
Sami Tolvanen90221202014-12-09 16:39:47 +0000443 return rc;
444}
445
Tao Bao612336d2015-08-27 16:41:21 -0700446static int VerifyBlocks(const std::string& expected, const std::vector<uint8_t>& buffer,
Tao Bao0940fe12015-08-27 16:41:21 -0700447 const size_t blocks, bool printerror) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800448 uint8_t digest[SHA_DIGEST_LENGTH];
Tao Bao612336d2015-08-27 16:41:21 -0700449 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000450
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800451 SHA1(data, blocks * BLOCKSIZE, digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000452
Tao Baoe6aa3322015-08-05 15:20:27 -0700453 std::string hexdigest = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000454
Tao Bao0940fe12015-08-27 16:41:21 -0700455 if (hexdigest != expected) {
456 if (printerror) {
Tao Bao039f2da2016-11-22 16:29:50 -0800457 LOG(ERROR) << "failed to verify blocks (expected " << expected << ", read "
458 << hexdigest << ")";
Tao Bao0940fe12015-08-27 16:41:21 -0700459 }
460 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000461 }
462
Tao Bao0940fe12015-08-27 16:41:21 -0700463 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000464}
465
Tao Bao0940fe12015-08-27 16:41:21 -0700466static std::string GetStashFileName(const std::string& base, const std::string& id,
467 const std::string& postfix) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700468 if (base.empty()) {
469 return "";
Sami Tolvanen90221202014-12-09 16:39:47 +0000470 }
471
Tao Baoe6aa3322015-08-05 15:20:27 -0700472 std::string fn(STASH_DIRECTORY_BASE);
473 fn += "/" + base + "/" + id + postfix;
Sami Tolvanen90221202014-12-09 16:39:47 +0000474
475 return fn;
476}
477
Tao Baoe6aa3322015-08-05 15:20:27 -0700478typedef void (*StashCallback)(const std::string&, void*);
Sami Tolvanen90221202014-12-09 16:39:47 +0000479
480// Does a best effort enumeration of stash files. Ignores possible non-file
481// items in the stash directory and continues despite of errors. Calls the
482// 'callback' function for each file and passes 'data' to the function as a
483// parameter.
484
Tao Baoe6aa3322015-08-05 15:20:27 -0700485static void EnumerateStash(const std::string& dirname, StashCallback callback, void* data) {
Tao Bao0940fe12015-08-27 16:41:21 -0700486 if (dirname.empty() || callback == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000487 return;
488 }
489
Tao Baoe6aa3322015-08-05 15:20:27 -0700490 std::unique_ptr<DIR, int(*)(DIR*)> directory(opendir(dirname.c_str()), closedir);
Sami Tolvanen90221202014-12-09 16:39:47 +0000491
Tao Bao0940fe12015-08-27 16:41:21 -0700492 if (directory == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000493 if (errno != ENOENT) {
Tao Bao039f2da2016-11-22 16:29:50 -0800494 PLOG(ERROR) << "opendir \"" << dirname << "\" failed";
Sami Tolvanen90221202014-12-09 16:39:47 +0000495 }
496 return;
497 }
498
Tao Baoe6aa3322015-08-05 15:20:27 -0700499 struct dirent* item;
Tao Bao0940fe12015-08-27 16:41:21 -0700500 while ((item = readdir(directory.get())) != nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000501 if (item->d_type != DT_REG) {
502 continue;
503 }
504
Tao Baoe6aa3322015-08-05 15:20:27 -0700505 std::string fn = dirname + "/" + std::string(item->d_name);
Sami Tolvanen90221202014-12-09 16:39:47 +0000506 callback(fn, data);
Sami Tolvanen90221202014-12-09 16:39:47 +0000507 }
508}
509
Tao Baoe6aa3322015-08-05 15:20:27 -0700510static void UpdateFileSize(const std::string& fn, void* data) {
Tao Bao51412212016-12-28 14:44:05 -0800511 if (fn.empty() || !data) {
512 return;
513 }
Tao Baoe6aa3322015-08-05 15:20:27 -0700514
Tao Bao51412212016-12-28 14:44:05 -0800515 struct stat sb;
516 if (stat(fn.c_str(), &sb) == -1) {
517 PLOG(ERROR) << "stat \"" << fn << "\" failed";
518 return;
519 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000520
Tao Bao51412212016-12-28 14:44:05 -0800521 size_t* size = static_cast<size_t*>(data);
522 *size += sb.st_size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000523}
524
525// Deletes the stash directory and all files in it. Assumes that it only
526// contains files. There is nothing we can do about unlikely, but possible
527// errors, so they are merely logged.
528
Tao Bao0940fe12015-08-27 16:41:21 -0700529static void DeleteFile(const std::string& fn, void* /* data */) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700530 if (!fn.empty()) {
Tao Bao039f2da2016-11-22 16:29:50 -0800531 LOG(INFO) << "deleting " << fn;
Sami Tolvanen90221202014-12-09 16:39:47 +0000532
Tao Baoe6aa3322015-08-05 15:20:27 -0700533 if (unlink(fn.c_str()) == -1 && errno != ENOENT) {
Tao Bao039f2da2016-11-22 16:29:50 -0800534 PLOG(ERROR) << "unlink \"" << fn << "\" failed";
Sami Tolvanen90221202014-12-09 16:39:47 +0000535 }
536 }
537}
538
Tao Baoe6aa3322015-08-05 15:20:27 -0700539static void DeletePartial(const std::string& fn, void* data) {
540 if (android::base::EndsWith(fn, ".partial")) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000541 DeleteFile(fn, data);
542 }
543}
544
Tao Baoe6aa3322015-08-05 15:20:27 -0700545static void DeleteStash(const std::string& base) {
546 if (base.empty()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000547 return;
548 }
549
Tao Bao039f2da2016-11-22 16:29:50 -0800550 LOG(INFO) << "deleting stash " << base;
Sami Tolvanen90221202014-12-09 16:39:47 +0000551
Tao Baoe6aa3322015-08-05 15:20:27 -0700552 std::string dirname = GetStashFileName(base, "", "");
Tao Bao0940fe12015-08-27 16:41:21 -0700553 EnumerateStash(dirname, DeleteFile, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000554
Tao Baoe6aa3322015-08-05 15:20:27 -0700555 if (rmdir(dirname.c_str()) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000556 if (errno != ENOENT && errno != ENOTDIR) {
Tao Bao039f2da2016-11-22 16:29:50 -0800557 PLOG(ERROR) << "rmdir \"" << dirname << "\" failed";
Sami Tolvanen90221202014-12-09 16:39:47 +0000558 }
559 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000560}
561
Tianjie Xu7eca97e2016-03-22 18:08:12 -0700562static int LoadStash(CommandParameters& params, const std::string& base, const std::string& id,
563 bool verify, size_t* blocks, std::vector<uint8_t>& buffer, bool printnoent) {
564 // In verify mode, if source range_set was saved for the given hash,
565 // check contents in the source blocks first. If the check fails,
566 // search for the stashed files on /cache as usual.
567 if (!params.canwrite) {
568 if (stash_map.find(id) != stash_map.end()) {
569 const RangeSet& src = stash_map[id];
570 allocate(src.size * BLOCKSIZE, buffer);
571
572 if (ReadBlocks(src, buffer, params.fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800573 LOG(ERROR) << "failed to read source blocks in stash map.";
Tianjie Xu7eca97e2016-03-22 18:08:12 -0700574 return -1;
575 }
576 if (VerifyBlocks(id, buffer, src.size, true) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800577 LOG(ERROR) << "failed to verify loaded source blocks in stash map.";
Tianjie Xu7eca97e2016-03-22 18:08:12 -0700578 return -1;
579 }
580 return 0;
581 }
582 }
583
Tao Bao612336d2015-08-27 16:41:21 -0700584 if (base.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -0700585 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000586 }
587
Tao Bao0940fe12015-08-27 16:41:21 -0700588 size_t blockcount = 0;
589
Sami Tolvanen90221202014-12-09 16:39:47 +0000590 if (!blocks) {
591 blocks = &blockcount;
592 }
593
Tao Bao0940fe12015-08-27 16:41:21 -0700594 std::string fn = GetStashFileName(base, id, "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000595
Tao Bao0940fe12015-08-27 16:41:21 -0700596 struct stat sb;
597 int res = stat(fn.c_str(), &sb);
Sami Tolvanen90221202014-12-09 16:39:47 +0000598
599 if (res == -1) {
600 if (errno != ENOENT || printnoent) {
Tao Bao039f2da2016-11-22 16:29:50 -0800601 PLOG(ERROR) << "stat \"" << fn << "\" failed";
Sami Tolvanen90221202014-12-09 16:39:47 +0000602 }
Tao Bao0940fe12015-08-27 16:41:21 -0700603 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000604 }
605
Tao Bao039f2da2016-11-22 16:29:50 -0800606 LOG(INFO) << " loading " << fn;
Sami Tolvanen90221202014-12-09 16:39:47 +0000607
Tao Bao0940fe12015-08-27 16:41:21 -0700608 if ((sb.st_size % BLOCKSIZE) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800609 LOG(ERROR) << fn << " size " << sb.st_size << " not multiple of block size " << BLOCKSIZE;
Tao Bao0940fe12015-08-27 16:41:21 -0700610 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000611 }
612
Elliott Hughesbcabd092016-03-22 20:19:22 -0700613 android::base::unique_fd fd(TEMP_FAILURE_RETRY(ota_open(fn.c_str(), O_RDONLY)));
Sami Tolvanen90221202014-12-09 16:39:47 +0000614 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800615 PLOG(ERROR) << "open \"" << fn << "\" failed";
Tao Bao0940fe12015-08-27 16:41:21 -0700616 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000617 }
618
Tao Bao612336d2015-08-27 16:41:21 -0700619 allocate(sb.st_size, buffer);
Sami Tolvanen90221202014-12-09 16:39:47 +0000620
Tao Bao612336d2015-08-27 16:41:21 -0700621 if (read_all(fd, buffer, sb.st_size) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700622 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000623 }
624
Tao Bao0940fe12015-08-27 16:41:21 -0700625 *blocks = sb.st_size / BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000626
Tao Bao612336d2015-08-27 16:41:21 -0700627 if (verify && VerifyBlocks(id, buffer, *blocks, true) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800628 LOG(ERROR) << "unexpected contents in " << fn;
Tao Bao0940fe12015-08-27 16:41:21 -0700629 DeleteFile(fn, nullptr);
630 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000631 }
632
Tao Bao0940fe12015-08-27 16:41:21 -0700633 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000634}
635
Tao Bao612336d2015-08-27 16:41:21 -0700636static int WriteStash(const std::string& base, const std::string& id, int blocks,
637 std::vector<uint8_t>& buffer, bool checkspace, bool *exists) {
638 if (base.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -0700639 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000640 }
641
642 if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800643 LOG(ERROR) << "not enough space to write stash";
Tao Bao0940fe12015-08-27 16:41:21 -0700644 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000645 }
646
Tao Bao0940fe12015-08-27 16:41:21 -0700647 std::string fn = GetStashFileName(base, id, ".partial");
648 std::string cn = GetStashFileName(base, id, "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000649
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100650 if (exists) {
Tao Bao0940fe12015-08-27 16:41:21 -0700651 struct stat sb;
652 int res = stat(cn.c_str(), &sb);
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100653
654 if (res == 0) {
655 // The file already exists and since the name is the hash of the contents,
656 // it's safe to assume the contents are identical (accidental hash collisions
657 // are unlikely)
Tao Bao039f2da2016-11-22 16:29:50 -0800658 LOG(INFO) << " skipping " << blocks << " existing blocks in " << cn;
Tao Bao0940fe12015-08-27 16:41:21 -0700659 *exists = true;
660 return 0;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100661 }
662
Tao Bao0940fe12015-08-27 16:41:21 -0700663 *exists = false;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100664 }
665
Tao Bao039f2da2016-11-22 16:29:50 -0800666 LOG(INFO) << " writing " << blocks << " blocks to " << cn;
Sami Tolvanen90221202014-12-09 16:39:47 +0000667
Tao Bao039f2da2016-11-22 16:29:50 -0800668 android::base::unique_fd fd(
669 TEMP_FAILURE_RETRY(ota_open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE)));
Sami Tolvanen90221202014-12-09 16:39:47 +0000670 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800671 PLOG(ERROR) << "failed to create \"" << fn << "\"";
Tao Bao0940fe12015-08-27 16:41:21 -0700672 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000673 }
674
675 if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700676 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000677 }
678
Jed Estepa7b9a462015-12-15 16:04:53 -0800679 if (ota_fsync(fd) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700680 failure_type = kFsyncFailure;
Tao Bao039f2da2016-11-22 16:29:50 -0800681 PLOG(ERROR) << "fsync \"" << fn << "\" failed";
Tao Bao0940fe12015-08-27 16:41:21 -0700682 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000683 }
684
Tao Baoe6aa3322015-08-05 15:20:27 -0700685 if (rename(fn.c_str(), cn.c_str()) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800686 PLOG(ERROR) << "rename(\"" << fn << "\", \"" << cn << "\") failed";
Tao Bao0940fe12015-08-27 16:41:21 -0700687 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000688 }
689
Tao Bao0940fe12015-08-27 16:41:21 -0700690 std::string dname = GetStashFileName(base, "", "");
Elliott Hughesbcabd092016-03-22 20:19:22 -0700691 android::base::unique_fd dfd(TEMP_FAILURE_RETRY(ota_open(dname.c_str(),
692 O_RDONLY | O_DIRECTORY)));
Tao Baodc392262015-07-31 15:56:44 -0700693 if (dfd == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700694 failure_type = kFileOpenFailure;
Tao Bao039f2da2016-11-22 16:29:50 -0800695 PLOG(ERROR) << "failed to open \"" << dname << "\" failed";
Tao Bao0940fe12015-08-27 16:41:21 -0700696 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700697 }
698
Jed Estepa7b9a462015-12-15 16:04:53 -0800699 if (ota_fsync(dfd) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700700 failure_type = kFsyncFailure;
Tao Bao039f2da2016-11-22 16:29:50 -0800701 PLOG(ERROR) << "fsync \"" << dname << "\" failed";
Tao Bao0940fe12015-08-27 16:41:21 -0700702 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700703 }
704
Tao Bao0940fe12015-08-27 16:41:21 -0700705 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000706}
707
708// Creates a directory for storing stash files and checks if the /cache partition
709// hash enough space for the expected amount of blocks we need to store. Returns
710// >0 if we created the directory, zero if it existed already, and <0 of failure.
711
Tao Bao51412212016-12-28 14:44:05 -0800712static int CreateStash(State* state, size_t maxblocks, const std::string& blockdev,
713 std::string& base) {
714 if (blockdev.empty()) {
715 return -1;
716 }
717
718 // Stash directory should be different for each partition to avoid conflicts
719 // when updating multiple partitions at the same time, so we use the hash of
720 // the block device name as the base directory
721 uint8_t digest[SHA_DIGEST_LENGTH];
722 SHA1(reinterpret_cast<const uint8_t*>(blockdev.data()), blockdev.size(), digest);
723 base = print_sha1(digest);
724
725 std::string dirname = GetStashFileName(base, "", "");
726 struct stat sb;
727 int res = stat(dirname.c_str(), &sb);
728 size_t max_stash_size = maxblocks * BLOCKSIZE;
729
730 if (res == -1 && errno != ENOENT) {
731 ErrorAbort(state, kStashCreationFailure, "stat \"%s\" failed: %s\n", dirname.c_str(),
732 strerror(errno));
733 return -1;
734 } else if (res != 0) {
735 LOG(INFO) << "creating stash " << dirname;
736 res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE);
737
738 if (res != 0) {
739 ErrorAbort(state, kStashCreationFailure, "mkdir \"%s\" failed: %s\n", dirname.c_str(),
740 strerror(errno));
741 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000742 }
743
Tao Bao51412212016-12-28 14:44:05 -0800744 if (CacheSizeCheck(max_stash_size) != 0) {
745 ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu needed)\n",
746 max_stash_size);
747 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000748 }
749
Tao Bao51412212016-12-28 14:44:05 -0800750 return 1; // Created directory
751 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000752
Tao Bao51412212016-12-28 14:44:05 -0800753 LOG(INFO) << "using existing stash " << dirname;
Sami Tolvanen90221202014-12-09 16:39:47 +0000754
Tao Bao51412212016-12-28 14:44:05 -0800755 // If the directory already exists, calculate the space already allocated to
756 // stash files and check if there's enough for all required blocks. Delete any
757 // partially completed stash files first.
Sami Tolvanen90221202014-12-09 16:39:47 +0000758
Tao Bao51412212016-12-28 14:44:05 -0800759 EnumerateStash(dirname, DeletePartial, nullptr);
760 size_t existing = 0;
761 EnumerateStash(dirname, UpdateFileSize, &existing);
Sami Tolvanen90221202014-12-09 16:39:47 +0000762
Tao Bao51412212016-12-28 14:44:05 -0800763 if (max_stash_size > existing) {
764 size_t needed = max_stash_size - existing;
765 if (CacheSizeCheck(needed) != 0) {
766 ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu more needed)\n",
767 needed);
768 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000769 }
Tao Bao51412212016-12-28 14:44:05 -0800770 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000771
Tao Bao51412212016-12-28 14:44:05 -0800772 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()) {
Tao Bao039f2da2016-11-22 16:29:50 -0800780 LOG(ERROR) << "missing id and/or src range fields in stash command";
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 Bao039f2da2016-11-22 16:29:50 -0800807 LOG(ERROR) << "failed to load source blocks for stash " << id;
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 Bao039f2da2016-11-22 16:29:50 -0800817 LOG(INFO) << "stashing " << blocks << " blocks to " << id;
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()) {
Tao Bao039f2da2016-11-22 16:29:50 -0800873 LOG(ERROR) << "invalid parameters";
Tao Baobaad2d42015-12-06 16:56:27 -0800874 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)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800883 LOG(ERROR) << "invalid src_block_count \"" << token << "\"";
Tao Baobaad2d42015-12-06 16:56:27 -0800884 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) {
Tao Bao039f2da2016-11-22 16:29:50 -0800923 LOG(ERROR) << "invalid parameter";
Tao Baobaad2d42015-12-06 16:56:27 -0800924 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 Bao039f2da2016-11-22 16:29:50 -0800933 LOG(ERROR) << "failed to load stash " << tokens[0];
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()) {
Tao Bao039f2da2016-11-22 16:29:50 -0800967 LOG(ERROR) << "missing source hash";
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()) {
Tao Bao039f2da2016-11-22 16:29:50 -0800978 LOG(ERROR) << "missing target hash";
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 Bao039f2da2016-11-22 16:29:50 -08001005 LOG(INFO) << "stashing " << src_blocks << " overlapping blocks to " << srchash;
Sami Tolvanen90221202014-12-09 16:39:47 +00001006
Tao Bao0940fe12015-08-27 16:41:21 -07001007 bool stash_exists = false;
Tao Bao612336d2015-08-27 16:41:21 -07001008 if (WriteStash(params.stashbase, srchash, src_blocks, params.buffer, true,
Tao Bao0940fe12015-08-27 16:41:21 -07001009 &stash_exists) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001010 LOG(ERROR) << "failed to stash overlapping source blocks";
Tao Bao0940fe12015-08-27 16:41:21 -07001011 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001012 }
1013
Tianjie Xudd874b12016-05-13 12:13:15 -07001014 params.stashed += src_blocks;
Sami Tolvanen90221202014-12-09 16:39:47 +00001015 // Can be deleted when the write has completed
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001016 if (!stash_exists) {
Tao Bao0940fe12015-08-27 16:41:21 -07001017 params.freestash = srchash;
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001018 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001019 }
1020
1021 // Source blocks have expected content, command can proceed
Tao Bao0940fe12015-08-27 16:41:21 -07001022 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001023 }
1024
Tianjie Xu7eca97e2016-03-22 18:08:12 -07001025 if (overlap && LoadStash(params, params.stashbase, srchash, true, nullptr, params.buffer,
1026 true) == 0) {
Sami Tolvanen43b748f2015-04-17 12:50:31 +01001027 // Overlapping source blocks were previously stashed, command can proceed.
1028 // We are recovering from an interrupted command, so we don't know if the
1029 // stash can safely be deleted after this command.
Tao Bao0940fe12015-08-27 16:41:21 -07001030 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001031 }
1032
1033 // Valid source data not available, update cannot be resumed
Tao Bao039f2da2016-11-22 16:29:50 -08001034 LOG(ERROR) << "partition has unexpected contents";
Tao Bao0940fe12015-08-27 16:41:21 -07001035 params.isunresumable = true;
Sami Tolvanen90221202014-12-09 16:39:47 +00001036
Tao Bao0940fe12015-08-27 16:41:21 -07001037 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001038}
1039
Tao Bao0940fe12015-08-27 16:41:21 -07001040static int PerformCommandMove(CommandParameters& params) {
1041 size_t blocks = 0;
Tao Baoe6aa3322015-08-05 15:20:27 -07001042 bool overlap = false;
Sami Tolvanen90221202014-12-09 16:39:47 +00001043 int status = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001044 RangeSet tgt;
Sami Tolvanen90221202014-12-09 16:39:47 +00001045
Tao Bao0940fe12015-08-27 16:41:21 -07001046 if (params.version == 1) {
Tao Baobaad2d42015-12-06 16:56:27 -08001047 status = LoadSrcTgtVersion1(params, tgt, blocks, params.buffer, params.fd);
Tao Bao0940fe12015-08-27 16:41:21 -07001048 } else if (params.version == 2) {
Tao Baobaad2d42015-12-06 16:56:27 -08001049 status = LoadSrcTgtVersion2(params, tgt, blocks, params.buffer, params.fd,
Tao Bao612336d2015-08-27 16:41:21 -07001050 params.stashbase, nullptr);
Tao Bao0940fe12015-08-27 16:41:21 -07001051 } else if (params.version >= 3) {
Tao Bao34847b22015-09-08 11:05:49 -07001052 status = LoadSrcTgtVersion3(params, tgt, blocks, true, overlap);
Sami Tolvanen90221202014-12-09 16:39:47 +00001053 }
1054
1055 if (status == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001056 LOG(ERROR) << "failed to read blocks for move";
Tao Bao0940fe12015-08-27 16:41:21 -07001057 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001058 }
1059
1060 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001061 params.foundwrites = true;
1062 } else if (params.foundwrites) {
Tao Bao039f2da2016-11-22 16:29:50 -08001063 LOG(WARNING) << "warning: commands executed out of order [" << params.cmdname << "]";
Sami Tolvanen90221202014-12-09 16:39:47 +00001064 }
1065
Tao Bao0940fe12015-08-27 16:41:21 -07001066 if (params.canwrite) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001067 if (status == 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001068 LOG(INFO) << " moving " << blocks << " blocks";
Sami Tolvanen90221202014-12-09 16:39:47 +00001069
Tao Bao0940fe12015-08-27 16:41:21 -07001070 if (WriteBlocks(tgt, params.buffer, params.fd) == -1) {
1071 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001072 }
1073 } else {
Tao Bao039f2da2016-11-22 16:29:50 -08001074 LOG(INFO) << "skipping " << blocks << " already moved blocks";
Sami Tolvanen90221202014-12-09 16:39:47 +00001075 }
1076
1077 }
1078
Tao Baobaad2d42015-12-06 16:56:27 -08001079 if (!params.freestash.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -07001080 FreeStash(params.stashbase, params.freestash);
Tao Baobaad2d42015-12-06 16:56:27 -08001081 params.freestash.clear();
Sami Tolvanen90221202014-12-09 16:39:47 +00001082 }
1083
Tao Bao0940fe12015-08-27 16:41:21 -07001084 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001085
Tao Bao0940fe12015-08-27 16:41:21 -07001086 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001087}
1088
Tao Bao0940fe12015-08-27 16:41:21 -07001089static int PerformCommandStash(CommandParameters& params) {
Tao Baobaad2d42015-12-06 16:56:27 -08001090 return SaveStash(params, params.stashbase, params.buffer, params.fd,
Tao Bao612336d2015-08-27 16:41:21 -07001091 (params.version >= 3));
Sami Tolvanen90221202014-12-09 16:39:47 +00001092}
1093
Tao Bao0940fe12015-08-27 16:41:21 -07001094static int PerformCommandFree(CommandParameters& params) {
Tao Baobaad2d42015-12-06 16:56:27 -08001095 // <stash_id>
1096 if (params.cpos >= params.tokens.size()) {
Tao Bao039f2da2016-11-22 16:29:50 -08001097 LOG(ERROR) << "missing stash id in free command";
Tao Baobaad2d42015-12-06 16:56:27 -08001098 return -1;
1099 }
1100
Tianjie Xu7eca97e2016-03-22 18:08:12 -07001101 const std::string& id = params.tokens[params.cpos++];
1102
1103 if (!params.canwrite && stash_map.find(id) != stash_map.end()) {
1104 stash_map.erase(id);
1105 return 0;
1106 }
1107
Tao Bao0940fe12015-08-27 16:41:21 -07001108 if (params.createdstash || params.canwrite) {
Tianjie Xu7eca97e2016-03-22 18:08:12 -07001109 return FreeStash(params.stashbase, id);
Sami Tolvanen90221202014-12-09 16:39:47 +00001110 }
1111
1112 return 0;
1113}
1114
Tao Bao0940fe12015-08-27 16:41:21 -07001115static int PerformCommandZero(CommandParameters& params) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001116
Tao Baobaad2d42015-12-06 16:56:27 -08001117 if (params.cpos >= params.tokens.size()) {
Tao Bao039f2da2016-11-22 16:29:50 -08001118 LOG(ERROR) << "missing target blocks for zero";
Tao Bao0940fe12015-08-27 16:41:21 -07001119 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001120 }
1121
Tao Bao0940fe12015-08-27 16:41:21 -07001122 RangeSet tgt;
Tao Baobaad2d42015-12-06 16:56:27 -08001123 parse_range(params.tokens[params.cpos++], tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001124
Tao Bao039f2da2016-11-22 16:29:50 -08001125 LOG(INFO) << " zeroing " << tgt.size << " blocks";
Sami Tolvanen90221202014-12-09 16:39:47 +00001126
Tao Bao612336d2015-08-27 16:41:21 -07001127 allocate(BLOCKSIZE, params.buffer);
1128 memset(params.buffer.data(), 0, BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +00001129
Tao Bao0940fe12015-08-27 16:41:21 -07001130 if (params.canwrite) {
1131 for (size_t i = 0; i < tgt.count; ++i) {
Tianjie Xu7ce287d2016-05-31 09:29:49 -07001132 off64_t offset = static_cast<off64_t>(tgt.pos[i * 2]) * BLOCKSIZE;
1133 size_t size = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * BLOCKSIZE;
1134 if (!discard_blocks(params.fd, offset, size)) {
1135 return -1;
1136 }
1137
1138 if (!check_lseek(params.fd, offset, SEEK_SET)) {
Tao Bao0940fe12015-08-27 16:41:21 -07001139 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001140 }
1141
Tao Bao0940fe12015-08-27 16:41:21 -07001142 for (size_t j = tgt.pos[i * 2]; j < tgt.pos[i * 2 + 1]; ++j) {
1143 if (write_all(params.fd, params.buffer, BLOCKSIZE) == -1) {
1144 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001145 }
1146 }
1147 }
1148 }
1149
Tao Bao0940fe12015-08-27 16:41:21 -07001150 if (params.cmdname[0] == 'z') {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001151 // Update only for the zero command, as the erase command will call
1152 // this if DEBUG_ERASE is defined.
Tao Bao0940fe12015-08-27 16:41:21 -07001153 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001154 }
1155
Tao Bao0940fe12015-08-27 16:41:21 -07001156 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001157}
1158
Tao Bao0940fe12015-08-27 16:41:21 -07001159static int PerformCommandNew(CommandParameters& params) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001160
Tao Baobaad2d42015-12-06 16:56:27 -08001161 if (params.cpos >= params.tokens.size()) {
Tao Bao039f2da2016-11-22 16:29:50 -08001162 LOG(ERROR) << "missing target blocks for new";
Tao Bao0940fe12015-08-27 16:41:21 -07001163 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001164 }
1165
Tao Bao0940fe12015-08-27 16:41:21 -07001166 RangeSet tgt;
Tao Baobaad2d42015-12-06 16:56:27 -08001167 parse_range(params.tokens[params.cpos++], tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001168
Tao Bao0940fe12015-08-27 16:41:21 -07001169 if (params.canwrite) {
Tao Bao039f2da2016-11-22 16:29:50 -08001170 LOG(INFO) << " writing " << tgt.size << " blocks of new data";
Sami Tolvanen90221202014-12-09 16:39:47 +00001171
Tao Bao0940fe12015-08-27 16:41:21 -07001172 RangeSinkState rss(tgt);
1173 rss.fd = params.fd;
Sami Tolvanen90221202014-12-09 16:39:47 +00001174 rss.p_block = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001175 rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001176
Tianjie Xu7ce287d2016-05-31 09:29:49 -07001177 off64_t offset = static_cast<off64_t>(tgt.pos[0]) * BLOCKSIZE;
1178 if (!discard_blocks(params.fd, offset, tgt.size * BLOCKSIZE)) {
1179 return -1;
1180 }
1181
1182 if (!check_lseek(params.fd, offset, SEEK_SET)) {
Tao Bao0940fe12015-08-27 16:41:21 -07001183 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001184 }
1185
Tao Bao0940fe12015-08-27 16:41:21 -07001186 pthread_mutex_lock(&params.nti.mu);
1187 params.nti.rss = &rss;
1188 pthread_cond_broadcast(&params.nti.cv);
Sami Tolvanen90221202014-12-09 16:39:47 +00001189
Tao Bao0940fe12015-08-27 16:41:21 -07001190 while (params.nti.rss) {
1191 pthread_cond_wait(&params.nti.cv, &params.nti.mu);
Sami Tolvanen90221202014-12-09 16:39:47 +00001192 }
1193
Tao Bao0940fe12015-08-27 16:41:21 -07001194 pthread_mutex_unlock(&params.nti.mu);
Sami Tolvanen90221202014-12-09 16:39:47 +00001195 }
1196
Tao Bao0940fe12015-08-27 16:41:21 -07001197 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001198
Tao Bao0940fe12015-08-27 16:41:21 -07001199 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001200}
1201
Tao Bao0940fe12015-08-27 16:41:21 -07001202static int PerformCommandDiff(CommandParameters& params) {
Tao Bao0940fe12015-08-27 16:41:21 -07001203
Tao Baobaad2d42015-12-06 16:56:27 -08001204 // <offset> <length>
1205 if (params.cpos + 1 >= params.tokens.size()) {
Tao Bao039f2da2016-11-22 16:29:50 -08001206 LOG(ERROR) << "missing patch offset or length for " << params.cmdname;
Tao Bao0940fe12015-08-27 16:41:21 -07001207 return -1;
1208 }
1209
Tao Baobaad2d42015-12-06 16:56:27 -08001210 size_t offset;
1211 if (!android::base::ParseUint(params.tokens[params.cpos++].c_str(), &offset)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001212 LOG(ERROR) << "invalid patch offset";
Tao Bao0940fe12015-08-27 16:41:21 -07001213 return -1;
1214 }
1215
Tao Baobaad2d42015-12-06 16:56:27 -08001216 size_t len;
1217 if (!android::base::ParseUint(params.tokens[params.cpos++].c_str(), &len)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001218 LOG(ERROR) << "invalid patch len";
Tao Baobaad2d42015-12-06 16:56:27 -08001219 return -1;
1220 }
Tao Bao0940fe12015-08-27 16:41:21 -07001221
Tao Bao612336d2015-08-27 16:41:21 -07001222 RangeSet tgt;
Tao Bao0940fe12015-08-27 16:41:21 -07001223 size_t blocks = 0;
Tao Bao612336d2015-08-27 16:41:21 -07001224 bool overlap = false;
Sami Tolvanen90221202014-12-09 16:39:47 +00001225 int status = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001226 if (params.version == 1) {
Tao Baobaad2d42015-12-06 16:56:27 -08001227 status = LoadSrcTgtVersion1(params, tgt, blocks, params.buffer, params.fd);
Tao Bao0940fe12015-08-27 16:41:21 -07001228 } else if (params.version == 2) {
Tao Baobaad2d42015-12-06 16:56:27 -08001229 status = LoadSrcTgtVersion2(params, tgt, blocks, params.buffer, params.fd,
Tao Bao612336d2015-08-27 16:41:21 -07001230 params.stashbase, nullptr);
Tao Bao0940fe12015-08-27 16:41:21 -07001231 } else if (params.version >= 3) {
Tao Bao34847b22015-09-08 11:05:49 -07001232 status = LoadSrcTgtVersion3(params, tgt, blocks, false, overlap);
Sami Tolvanen90221202014-12-09 16:39:47 +00001233 }
1234
1235 if (status == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001236 LOG(ERROR) << "failed to read blocks for diff";
Tao Bao0940fe12015-08-27 16:41:21 -07001237 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001238 }
1239
1240 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001241 params.foundwrites = true;
1242 } else if (params.foundwrites) {
Tao Bao039f2da2016-11-22 16:29:50 -08001243 LOG(WARNING) << "warning: commands executed out of order [" << params.cmdname << "]";
Sami Tolvanen90221202014-12-09 16:39:47 +00001244 }
1245
Tao Bao0940fe12015-08-27 16:41:21 -07001246 if (params.canwrite) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001247 if (status == 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001248 LOG(INFO) << "patching " << blocks << " blocks to " << tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001249
Tianjie Xuaced5d92016-10-12 10:55:04 -07001250 Value patch_value(VAL_BLOB,
1251 std::string(reinterpret_cast<const char*>(params.patch_start + offset), len));
Sami Tolvanen90221202014-12-09 16:39:47 +00001252
Tao Bao0940fe12015-08-27 16:41:21 -07001253 RangeSinkState rss(tgt);
1254 rss.fd = params.fd;
Sami Tolvanen90221202014-12-09 16:39:47 +00001255 rss.p_block = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001256 rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001257
Tianjie Xu7ce287d2016-05-31 09:29:49 -07001258 off64_t offset = static_cast<off64_t>(tgt.pos[0]) * BLOCKSIZE;
1259 if (!discard_blocks(params.fd, offset, rss.p_remain)) {
1260 return -1;
1261 }
1262
1263 if (!check_lseek(params.fd, offset, SEEK_SET)) {
Tao Bao0940fe12015-08-27 16:41:21 -07001264 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001265 }
1266
Tao Bao0940fe12015-08-27 16:41:21 -07001267 if (params.cmdname[0] == 'i') { // imgdiff
Tianjie Xu31f8cc82016-06-15 11:56:42 -07001268 if (ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value,
1269 &RangeSinkWrite, &rss, nullptr, nullptr) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001270 LOG(ERROR) << "Failed to apply image patch.";
Tianjie Xu31f8cc82016-06-15 11:56:42 -07001271 return -1;
1272 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001273 } else {
Tianjie Xu31f8cc82016-06-15 11:56:42 -07001274 if (ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value,
1275 0, &RangeSinkWrite, &rss, nullptr) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001276 LOG(ERROR) << "Failed to apply bsdiff patch.";
Tianjie Xu31f8cc82016-06-15 11:56:42 -07001277 return -1;
1278 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001279 }
1280
1281 // We expect the output of the patcher to fill the tgt ranges exactly.
Tao Bao0940fe12015-08-27 16:41:21 -07001282 if (rss.p_block != tgt.count || rss.p_remain != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001283 LOG(ERROR) << "range sink underrun?";
Sami Tolvanen90221202014-12-09 16:39:47 +00001284 }
1285 } else {
Tao Bao039f2da2016-11-22 16:29:50 -08001286 LOG(INFO) << "skipping " << blocks << " blocks already patched to " << tgt.size
1287 << " [" << params.cmdline << "]";
Sami Tolvanen90221202014-12-09 16:39:47 +00001288 }
1289 }
1290
Tao Baobaad2d42015-12-06 16:56:27 -08001291 if (!params.freestash.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -07001292 FreeStash(params.stashbase, params.freestash);
Tao Baobaad2d42015-12-06 16:56:27 -08001293 params.freestash.clear();
Sami Tolvanen90221202014-12-09 16:39:47 +00001294 }
1295
Tao Bao0940fe12015-08-27 16:41:21 -07001296 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001297
Tao Bao0940fe12015-08-27 16:41:21 -07001298 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001299}
1300
Tao Bao0940fe12015-08-27 16:41:21 -07001301static int PerformCommandErase(CommandParameters& params) {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001302 if (DEBUG_ERASE) {
1303 return PerformCommandZero(params);
Sami Tolvanen90221202014-12-09 16:39:47 +00001304 }
1305
Tao Bao0940fe12015-08-27 16:41:21 -07001306 struct stat sb;
1307 if (fstat(params.fd, &sb) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001308 PLOG(ERROR) << "failed to fstat device to erase";
Tao Bao0940fe12015-08-27 16:41:21 -07001309 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001310 }
1311
Tao Bao0940fe12015-08-27 16:41:21 -07001312 if (!S_ISBLK(sb.st_mode)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001313 LOG(ERROR) << "not a block device; skipping erase";
Tao Bao0940fe12015-08-27 16:41:21 -07001314 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001315 }
1316
Tao Baobaad2d42015-12-06 16:56:27 -08001317 if (params.cpos >= params.tokens.size()) {
Tao Bao039f2da2016-11-22 16:29:50 -08001318 LOG(ERROR) << "missing target blocks for erase";
Tao Bao0940fe12015-08-27 16:41:21 -07001319 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001320 }
1321
Tao Bao0940fe12015-08-27 16:41:21 -07001322 RangeSet tgt;
Tao Baobaad2d42015-12-06 16:56:27 -08001323 parse_range(params.tokens[params.cpos++], tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001324
Tao Bao0940fe12015-08-27 16:41:21 -07001325 if (params.canwrite) {
Tao Bao039f2da2016-11-22 16:29:50 -08001326 LOG(INFO) << " erasing " << tgt.size << " blocks";
Sami Tolvanen90221202014-12-09 16:39:47 +00001327
Tao Bao0940fe12015-08-27 16:41:21 -07001328 for (size_t i = 0; i < tgt.count; ++i) {
1329 uint64_t blocks[2];
Sami Tolvanen90221202014-12-09 16:39:47 +00001330 // offset in bytes
Tao Bao0940fe12015-08-27 16:41:21 -07001331 blocks[0] = tgt.pos[i * 2] * (uint64_t) BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001332 // length in bytes
Tao Bao0940fe12015-08-27 16:41:21 -07001333 blocks[1] = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * (uint64_t) BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001334
Tao Bao0940fe12015-08-27 16:41:21 -07001335 if (ioctl(params.fd, BLKDISCARD, &blocks) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001336 PLOG(ERROR) << "BLKDISCARD ioctl failed";
Tao Bao0940fe12015-08-27 16:41:21 -07001337 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001338 }
1339 }
1340 }
1341
Tao Bao0940fe12015-08-27 16:41:21 -07001342 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001343}
1344
1345// Definitions for transfer list command functions
Tao Bao0940fe12015-08-27 16:41:21 -07001346typedef int (*CommandFunction)(CommandParameters&);
Sami Tolvanen90221202014-12-09 16:39:47 +00001347
Tao Bao612336d2015-08-27 16:41:21 -07001348struct Command {
Sami Tolvanen90221202014-12-09 16:39:47 +00001349 const char* name;
1350 CommandFunction f;
Tao Bao612336d2015-08-27 16:41:21 -07001351};
Sami Tolvanen90221202014-12-09 16:39:47 +00001352
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001353// args:
1354// - block device (or file) to modify in-place
1355// - transfer list (blob)
1356// - new data stream (filename within package.zip)
1357// - patch stream (filename within package.zip, must be uncompressed)
1358
Tao Bao0940fe12015-08-27 16:41:21 -07001359static Value* PerformBlockImageUpdate(const char* name, State* state, int /* argc */, Expr* argv[],
1360 const Command* commands, size_t cmdcount, bool dryrun) {
Tao Bao73064612016-04-26 17:14:32 -07001361 CommandParameters params = {};
Sami Tolvanen90221202014-12-09 16:39:47 +00001362 params.canwrite = !dryrun;
1363
Tao Bao5354f602016-12-14 11:31:18 -08001364 LOG(INFO) << "performing " << (dryrun ? "verification" : "update");
Tianjie Xu7ce287d2016-05-31 09:29:49 -07001365 if (state->is_retry) {
1366 is_retry = true;
Tao Bao039f2da2016-11-22 16:29:50 -08001367 LOG(INFO) << "This update is a retry.";
Tianjie Xu7ce287d2016-05-31 09:29:49 -07001368 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001369
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001370 std::vector<std::unique_ptr<Value>> args;
1371 if (!ReadValueArgs(state, 4, argv, &args)) {
1372 return nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001373 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001374
1375 const Value* blockdev_filename = args[0].get();
1376 const Value* transfer_list_value = args[1].get();
1377 const Value* new_data_fn = args[2].get();
1378 const Value* patch_data_fn = args[3].get();
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001379
1380 if (blockdev_filename->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001381 ErrorAbort(state, kArgsParsingFailure, "blockdev_filename argument to %s must be string",
1382 name);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001383 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001384 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001385 if (transfer_list_value->type != VAL_BLOB) {
Tianjie Xu16255832016-04-30 11:49:59 -07001386 ErrorAbort(state, kArgsParsingFailure, "transfer_list argument to %s must be blob", name);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001387 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001388 }
1389 if (new_data_fn->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001390 ErrorAbort(state, kArgsParsingFailure, "new_data_fn argument to %s must be string", name);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001391 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001392 }
1393 if (patch_data_fn->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001394 ErrorAbort(state, kArgsParsingFailure, "patch_data_fn argument to %s must be string",
1395 name);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001396 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001397 }
1398
Tao Bao51412212016-12-28 14:44:05 -08001399 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
Tao Bao0940fe12015-08-27 16:41:21 -07001400 if (ui == nullptr) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001401 return StringValue("");
Sami Tolvanen90221202014-12-09 16:39:47 +00001402 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001403
Tao Bao612336d2015-08-27 16:41:21 -07001404 FILE* cmd_pipe = ui->cmd_pipe;
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -07001405 ZipArchiveHandle za = ui->package_zip;
Sami Tolvanen90221202014-12-09 16:39:47 +00001406
Tao Bao0940fe12015-08-27 16:41:21 -07001407 if (cmd_pipe == nullptr || za == nullptr) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001408 return StringValue("");
Sami Tolvanen90221202014-12-09 16:39:47 +00001409 }
1410
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -07001411 ZipString path_data(patch_data_fn->data.c_str());
1412 ZipEntry patch_entry;
1413 if (FindEntry(za, path_data, &patch_entry) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001414 LOG(ERROR) << name << "(): no file \"" << patch_data_fn->data << "\" in package";
Tianjie Xuaced5d92016-10-12 10:55:04 -07001415 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001416 }
1417
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -07001418 params.patch_start = ui->package_zip_addr + patch_entry.offset;
1419 ZipString new_data(new_data_fn->data.c_str());
1420 ZipEntry new_entry;
1421 if (FindEntry(za, new_data, &new_entry) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001422 LOG(ERROR) << name << "(): no file \"" << new_data_fn->data << "\" in package";
Tianjie Xuaced5d92016-10-12 10:55:04 -07001423 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001424 }
1425
Tianjie Xuaced5d92016-10-12 10:55:04 -07001426 params.fd.reset(TEMP_FAILURE_RETRY(ota_open(blockdev_filename->data.c_str(), O_RDWR)));
Sami Tolvanen90221202014-12-09 16:39:47 +00001427 if (params.fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001428 PLOG(ERROR) << "open \"" << blockdev_filename->data << "\" failed";
Tianjie Xuaced5d92016-10-12 10:55:04 -07001429 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001430 }
1431
Sami Tolvanen90221202014-12-09 16:39:47 +00001432 if (params.canwrite) {
1433 params.nti.za = za;
1434 params.nti.entry = new_entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001435
Tao Bao0940fe12015-08-27 16:41:21 -07001436 pthread_mutex_init(&params.nti.mu, nullptr);
1437 pthread_cond_init(&params.nti.cv, nullptr);
Tao Bao612336d2015-08-27 16:41:21 -07001438 pthread_attr_t attr;
Sami Tolvanen90221202014-12-09 16:39:47 +00001439 pthread_attr_init(&attr);
1440 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1441
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001442 int error = pthread_create(&params.thread, &attr, unzip_new_data, &params.nti);
1443 if (error != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001444 PLOG(ERROR) << "pthread_create failed";
Tianjie Xuaced5d92016-10-12 10:55:04 -07001445 return StringValue("");
Sami Tolvanen90221202014-12-09 16:39:47 +00001446 }
1447 }
1448
Tianjie Xuaced5d92016-10-12 10:55:04 -07001449 std::vector<std::string> lines = android::base::Split(transfer_list_value->data, "\n");
Tao Baobaad2d42015-12-06 16:56:27 -08001450 if (lines.size() < 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001451 ErrorAbort(state, kArgsParsingFailure, "too few lines in the transfer list [%zd]\n",
1452 lines.size());
Tianjie Xuaced5d92016-10-12 10:55:04 -07001453 return StringValue("");
Tao Baobaad2d42015-12-06 16:56:27 -08001454 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001455
Sami Tolvanen90221202014-12-09 16:39:47 +00001456 // First line in transfer list is the version number
Tao Bao51412212016-12-28 14:44:05 -08001457 if (!android::base::ParseInt(lines[0], &params.version, 1, 4)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001458 LOG(ERROR) << "unexpected transfer list version [" << lines[0] << "]";
Tianjie Xuaced5d92016-10-12 10:55:04 -07001459 return StringValue("");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001460 }
1461
Tao Bao039f2da2016-11-22 16:29:50 -08001462 LOG(INFO) << "blockimg version is " << params.version;
Sami Tolvanen90221202014-12-09 16:39:47 +00001463
1464 // Second line in transfer list is the total number of blocks we expect to write
Tao Bao51412212016-12-28 14:44:05 -08001465 size_t total_blocks;
1466 if (!android::base::ParseUint(lines[1], &total_blocks)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001467 ErrorAbort(state, kArgsParsingFailure, "unexpected block count [%s]\n", lines[1].c_str());