blob: f7955566d1a8895f23ddd713b5ebbae857d12989 [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
Tianjie Xu01889352016-03-22 18:08:12 -070036#include <map>
Tao Baoe6aa3322015-08-05 15:20:27 -070037#include <memory>
38#include <string>
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>
Tao Baoe6aa3322015-08-05 15:20:27 -070043
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070044#include "applypatch/applypatch.h"
45#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070046#include "error_code.h"
Tianjie Xu30bf4762015-12-15 11:47:30 -080047#include "install.h"
Sen Jiangc48cb5e2016-02-04 16:23:21 +080048#include "openssl/sha.h"
Sami Tolvanen90221202014-12-09 16:39:47 +000049#include "minzip/Hash.h"
Jed Estepf73abf32015-12-15 16:04:53 -080050#include "ota_io.h"
Tao Baoe6aa3322015-08-05 15:20:27 -070051#include "print_sha1.h"
Tao Bao0940fe12015-08-27 16:41:21 -070052#include "unique_fd.h"
53#include "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 Xu01889352016-03-22 18:08:12 -070074static std::map<std::string, RangeSet> stash_map;
75
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 Estepf1fc48c2015-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;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700154 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700155 so_far += r;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700156 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000157 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700158}
159
Tao Bao612336d2015-08-27 16:41:21 -0700160static int read_all(int fd, std::vector<uint8_t>& buffer, size_t size) {
161 return read_all(fd, buffer.data(), size);
162}
163
Sami Tolvanen90221202014-12-09 16:39:47 +0000164static int write_all(int fd, const uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700165 size_t written = 0;
166 while (written < size) {
Jed Estepf1fc48c2015-12-15 16:04:53 -0800167 ssize_t w = TEMP_FAILURE_RETRY(ota_write(fd, data+written, size-written));
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700168 if (w == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700169 failure_type = kFwriteFailure;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700170 fprintf(stderr, "write failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000171 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700172 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700173 written += w;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700174 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000175
Sami Tolvanen90221202014-12-09 16:39:47 +0000176 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700177}
178
Tao Bao612336d2015-08-27 16:41:21 -0700179static int write_all(int fd, const std::vector<uint8_t>& buffer, size_t size) {
180 return write_all(fd, buffer.data(), size);
181}
182
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700183static bool discard_blocks(int fd, off64_t offset, uint64_t size) {
184 // Don't discard blocks unless the update is a retry run.
185 if (!is_retry) {
186 return true;
187 }
188
189 uint64_t args[2] = {static_cast<uint64_t>(offset), size};
190 int status = ioctl(fd, BLKDISCARD, &args);
191 if (status == -1) {
192 fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno));
193 return false;
194 }
195 return true;
196}
197
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700198static bool check_lseek(int fd, off64_t offset, int whence) {
199 off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
200 if (rc == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700201 failure_type = kLseekFailure;
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700202 fprintf(stderr, "lseek64 failed: %s\n", strerror(errno));
203 return false;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700204 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700205 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700206}
207
Tao Bao612336d2015-08-27 16:41:21 -0700208static void allocate(size_t size, std::vector<uint8_t>& buffer) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700209 // if the buffer's big enough, reuse it.
Tao Bao612336d2015-08-27 16:41:21 -0700210 if (size <= buffer.size()) return;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700211
Tao Bao612336d2015-08-27 16:41:21 -0700212 buffer.resize(size);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700213}
214
Tao Bao0940fe12015-08-27 16:41:21 -0700215struct RangeSinkState {
216 RangeSinkState(RangeSet& rs) : tgt(rs) { };
217
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700218 int fd;
Tao Bao0940fe12015-08-27 16:41:21 -0700219 const RangeSet& tgt;
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530220 size_t p_block;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700221 size_t p_remain;
Tao Bao0940fe12015-08-27 16:41:21 -0700222};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700223
224static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) {
Tao Bao0940fe12015-08-27 16:41:21 -0700225 RangeSinkState* rss = reinterpret_cast<RangeSinkState*>(token);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700226
Tao Bao0940fe12015-08-27 16:41:21 -0700227 if (rss->p_remain == 0) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700228 fprintf(stderr, "range sink write overrun");
Sami Tolvanen90221202014-12-09 16:39:47 +0000229 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700230 }
231
232 ssize_t written = 0;
233 while (size > 0) {
234 size_t write_now = size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000235
236 if (rss->p_remain < write_now) {
237 write_now = rss->p_remain;
238 }
239
240 if (write_all(rss->fd, data, write_now) == -1) {
241 break;
242 }
243
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700244 data += write_now;
245 size -= write_now;
246
247 rss->p_remain -= write_now;
248 written += write_now;
249
250 if (rss->p_remain == 0) {
251 // move to the next block
252 ++rss->p_block;
Tao Bao0940fe12015-08-27 16:41:21 -0700253 if (rss->p_block < rss->tgt.count) {
254 rss->p_remain = (rss->tgt.pos[rss->p_block * 2 + 1] -
255 rss->tgt.pos[rss->p_block * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000256
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700257 off64_t offset = static_cast<off64_t>(rss->tgt.pos[rss->p_block*2]) * BLOCKSIZE;
258 if (!discard_blocks(rss->fd, offset, rss->p_remain)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000259 break;
260 }
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700261
262 if (!check_lseek(rss->fd, offset, SEEK_SET)) {
263 break;
264 }
265
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700266 } else {
267 // we can't write any more; return how many bytes have
268 // been written so far.
Sami Tolvanen90221202014-12-09 16:39:47 +0000269 break;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700270 }
271 }
272 }
273
274 return written;
275}
276
277// All of the data for all the 'new' transfers is contained in one
278// file in the update package, concatenated together in the order in
279// which transfers.list will need it. We want to stream it out of the
280// archive (it's compressed) without writing it to a temp file, but we
281// can't write each section until it's that transfer's turn to go.
282//
283// To achieve this, we expand the new data from the archive in a
284// background thread, and block that threads 'receive uncompressed
285// data' function until the main thread has reached a point where we
286// want some new data to be written. We signal the background thread
287// with the destination for the data and block the main thread,
288// waiting for the background thread to complete writing that section.
289// Then it signals the main thread to wake up and goes back to
290// blocking waiting for a transfer.
291//
292// NewThreadInfo is the struct used to pass information back and forth
293// between the two threads. When the main thread wants some data
294// written, it sets rss to the destination location and signals the
295// condition. When the background thread is done writing, it clears
296// rss and signals the condition again.
297
Tao Bao0940fe12015-08-27 16:41:21 -0700298struct NewThreadInfo {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700299 ZipArchive* za;
300 const ZipEntry* entry;
301
302 RangeSinkState* rss;
303
304 pthread_mutex_t mu;
305 pthread_cond_t cv;
Tao Bao0940fe12015-08-27 16:41:21 -0700306};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700307
308static bool receive_new_data(const unsigned char* data, int size, void* cookie) {
Tao Bao0940fe12015-08-27 16:41:21 -0700309 NewThreadInfo* nti = reinterpret_cast<NewThreadInfo*>(cookie);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700310
311 while (size > 0) {
Tao Bao0940fe12015-08-27 16:41:21 -0700312 // Wait for nti->rss to be non-null, indicating some of this
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700313 // data is wanted.
314 pthread_mutex_lock(&nti->mu);
Tao Bao0940fe12015-08-27 16:41:21 -0700315 while (nti->rss == nullptr) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700316 pthread_cond_wait(&nti->cv, &nti->mu);
317 }
318 pthread_mutex_unlock(&nti->mu);
319
320 // At this point nti->rss is set, and we own it. The main
321 // thread is waiting for it to disappear from nti.
322 ssize_t written = RangeSinkWrite(data, size, nti->rss);
323 data += written;
324 size -= written;
325
Tao Bao0940fe12015-08-27 16:41:21 -0700326 if (nti->rss->p_block == nti->rss->tgt.count) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700327 // we have written all the bytes desired by this rss.
328
329 pthread_mutex_lock(&nti->mu);
Tao Bao0940fe12015-08-27 16:41:21 -0700330 nti->rss = nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700331 pthread_cond_broadcast(&nti->cv);
332 pthread_mutex_unlock(&nti->mu);
333 }
334 }
335
336 return true;
337}
338
339static void* unzip_new_data(void* cookie) {
340 NewThreadInfo* nti = (NewThreadInfo*) cookie;
341 mzProcessZipEntryContents(nti->za, nti->entry, receive_new_data, nti);
Tao Bao0940fe12015-08-27 16:41:21 -0700342 return nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700343}
344
Tao Bao612336d2015-08-27 16:41:21 -0700345static int ReadBlocks(const RangeSet& src, std::vector<uint8_t>& buffer, int fd) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000346 size_t p = 0;
Tao Bao612336d2015-08-27 16:41:21 -0700347 uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000348
Tao Bao0940fe12015-08-27 16:41:21 -0700349 for (size_t i = 0; i < src.count; ++i) {
350 if (!check_lseek(fd, (off64_t) src.pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000351 return -1;
352 }
353
Tao Bao0940fe12015-08-27 16:41:21 -0700354 size_t size = (src.pos[i * 2 + 1] - src.pos[i * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000355
Tao Bao612336d2015-08-27 16:41:21 -0700356 if (read_all(fd, data + p, size) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000357 return -1;
358 }
359
360 p += size;
361 }
362
363 return 0;
364}
365
Tao Bao612336d2015-08-27 16:41:21 -0700366static int WriteBlocks(const RangeSet& tgt, const std::vector<uint8_t>& buffer, int fd) {
367 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000368
Tao Bao0940fe12015-08-27 16:41:21 -0700369 size_t p = 0;
370 for (size_t i = 0; i < tgt.count; ++i) {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700371 off64_t offset = static_cast<off64_t>(tgt.pos[i * 2]) * BLOCKSIZE;
372 size_t size = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * BLOCKSIZE;
373 if (!discard_blocks(fd, offset, size)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000374 return -1;
375 }
376
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700377 if (!check_lseek(fd, offset, SEEK_SET)) {
378 return -1;
379 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000380
Tao Bao612336d2015-08-27 16:41:21 -0700381 if (write_all(fd, data + p, size) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000382 return -1;
383 }
384
385 p += size;
386 }
387
388 return 0;
389}
390
Tao Baobaad2d42015-12-06 16:56:27 -0800391// Parameters for transfer list command functions
392struct CommandParameters {
393 std::vector<std::string> tokens;
394 size_t cpos;
395 const char* cmdname;
396 const char* cmdline;
397 std::string freestash;
398 std::string stashbase;
399 bool canwrite;
400 int createdstash;
401 int fd;
402 bool foundwrites;
403 bool isunresumable;
404 int version;
405 size_t written;
Tianjie Xudd874b12016-05-13 12:13:15 -0700406 size_t stashed;
Tao Baobaad2d42015-12-06 16:56:27 -0800407 NewThreadInfo nti;
408 pthread_t thread;
409 std::vector<uint8_t> buffer;
410 uint8_t* patch_start;
411};
412
Doug Zongker52ae67d2014-09-08 12:22:09 -0700413// Do a source/target load for move/bsdiff/imgdiff in version 1.
Tao Baobaad2d42015-12-06 16:56:27 -0800414// We expect to parse the remainder of the parameter tokens as:
Doug Zongker52ae67d2014-09-08 12:22:09 -0700415//
416// <src_range> <tgt_range>
417//
418// The source range is loaded into the provided buffer, reallocating
Tao Bao34847b22015-09-08 11:05:49 -0700419// it to make it larger if necessary.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700420
Tao Baobaad2d42015-12-06 16:56:27 -0800421static int LoadSrcTgtVersion1(CommandParameters& params, RangeSet& tgt, size_t& src_blocks,
Tao Bao612336d2015-08-27 16:41:21 -0700422 std::vector<uint8_t>& buffer, int fd) {
Tao Baobaad2d42015-12-06 16:56:27 -0800423
424 if (params.cpos + 1 >= params.tokens.size()) {
425 fprintf(stderr, "invalid parameters\n");
426 return -1;
427 }
428
Tao Bao612336d2015-08-27 16:41:21 -0700429 // <src_range>
Tao Bao0940fe12015-08-27 16:41:21 -0700430 RangeSet src;
Tao Baobaad2d42015-12-06 16:56:27 -0800431 parse_range(params.tokens[params.cpos++], src);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700432
Tao Bao612336d2015-08-27 16:41:21 -0700433 // <tgt_range>
Tao Baobaad2d42015-12-06 16:56:27 -0800434 parse_range(params.tokens[params.cpos++], tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700435
Tao Bao612336d2015-08-27 16:41:21 -0700436 allocate(src.size * BLOCKSIZE, buffer);
437 int rc = ReadBlocks(src, buffer, fd);
Tao Bao0940fe12015-08-27 16:41:21 -0700438 src_blocks = src.size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000439
Sami Tolvanen90221202014-12-09 16:39:47 +0000440 return rc;
441}
442
Tao Bao612336d2015-08-27 16:41:21 -0700443static int VerifyBlocks(const std::string& expected, const std::vector<uint8_t>& buffer,
Tao Bao0940fe12015-08-27 16:41:21 -0700444 const size_t blocks, bool printerror) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800445 uint8_t digest[SHA_DIGEST_LENGTH];
Tao Bao612336d2015-08-27 16:41:21 -0700446 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000447
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800448 SHA1(data, blocks * BLOCKSIZE, digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000449
Tao Baoe6aa3322015-08-05 15:20:27 -0700450 std::string hexdigest = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000451
Tao Bao0940fe12015-08-27 16:41:21 -0700452 if (hexdigest != expected) {
453 if (printerror) {
454 fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
455 expected.c_str(), hexdigest.c_str());
456 }
457 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000458 }
459
Tao Bao0940fe12015-08-27 16:41:21 -0700460 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000461}
462
Tao Bao0940fe12015-08-27 16:41:21 -0700463static std::string GetStashFileName(const std::string& base, const std::string& id,
464 const std::string& postfix) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700465 if (base.empty()) {
466 return "";
Sami Tolvanen90221202014-12-09 16:39:47 +0000467 }
468
Tao Baoe6aa3322015-08-05 15:20:27 -0700469 std::string fn(STASH_DIRECTORY_BASE);
470 fn += "/" + base + "/" + id + postfix;
Sami Tolvanen90221202014-12-09 16:39:47 +0000471
472 return fn;
473}
474
Tao Baoe6aa3322015-08-05 15:20:27 -0700475typedef void (*StashCallback)(const std::string&, void*);
Sami Tolvanen90221202014-12-09 16:39:47 +0000476
477// Does a best effort enumeration of stash files. Ignores possible non-file
478// items in the stash directory and continues despite of errors. Calls the
479// 'callback' function for each file and passes 'data' to the function as a
480// parameter.
481
Tao Baoe6aa3322015-08-05 15:20:27 -0700482static void EnumerateStash(const std::string& dirname, StashCallback callback, void* data) {
Tao Bao0940fe12015-08-27 16:41:21 -0700483 if (dirname.empty() || callback == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000484 return;
485 }
486
Tao Baoe6aa3322015-08-05 15:20:27 -0700487 std::unique_ptr<DIR, int(*)(DIR*)> directory(opendir(dirname.c_str()), closedir);
Sami Tolvanen90221202014-12-09 16:39:47 +0000488
Tao Bao0940fe12015-08-27 16:41:21 -0700489 if (directory == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000490 if (errno != ENOENT) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700491 fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000492 }
493 return;
494 }
495
Tao Baoe6aa3322015-08-05 15:20:27 -0700496 struct dirent* item;
Tao Bao0940fe12015-08-27 16:41:21 -0700497 while ((item = readdir(directory.get())) != nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000498 if (item->d_type != DT_REG) {
499 continue;
500 }
501
Tao Baoe6aa3322015-08-05 15:20:27 -0700502 std::string fn = dirname + "/" + std::string(item->d_name);
Sami Tolvanen90221202014-12-09 16:39:47 +0000503 callback(fn, data);
Sami Tolvanen90221202014-12-09 16:39:47 +0000504 }
505}
506
Tao Baoe6aa3322015-08-05 15:20:27 -0700507static void UpdateFileSize(const std::string& fn, void* data) {
508 if (fn.empty() || !data) {
509 return;
510 }
511
Tao Bao0940fe12015-08-27 16:41:21 -0700512 struct stat sb;
513 if (stat(fn.c_str(), &sb) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700514 fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000515 return;
516 }
517
Tao Baoe6aa3322015-08-05 15:20:27 -0700518 int* size = reinterpret_cast<int*>(data);
Tao Bao0940fe12015-08-27 16:41:21 -0700519 *size += sb.st_size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000520}
521
522// Deletes the stash directory and all files in it. Assumes that it only
523// contains files. There is nothing we can do about unlikely, but possible
524// errors, so they are merely logged.
525
Tao Bao0940fe12015-08-27 16:41:21 -0700526static void DeleteFile(const std::string& fn, void* /* data */) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700527 if (!fn.empty()) {
528 fprintf(stderr, "deleting %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000529
Tao Baoe6aa3322015-08-05 15:20:27 -0700530 if (unlink(fn.c_str()) == -1 && errno != ENOENT) {
531 fprintf(stderr, "unlink \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000532 }
533 }
534}
535
Tao Baoe6aa3322015-08-05 15:20:27 -0700536static void DeletePartial(const std::string& fn, void* data) {
537 if (android::base::EndsWith(fn, ".partial")) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000538 DeleteFile(fn, data);
539 }
540}
541
Tao Baoe6aa3322015-08-05 15:20:27 -0700542static void DeleteStash(const std::string& base) {
543 if (base.empty()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000544 return;
545 }
546
Tao Baoe6aa3322015-08-05 15:20:27 -0700547 fprintf(stderr, "deleting stash %s\n", base.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000548
Tao Baoe6aa3322015-08-05 15:20:27 -0700549 std::string dirname = GetStashFileName(base, "", "");
Tao Bao0940fe12015-08-27 16:41:21 -0700550 EnumerateStash(dirname, DeleteFile, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000551
Tao Baoe6aa3322015-08-05 15:20:27 -0700552 if (rmdir(dirname.c_str()) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000553 if (errno != ENOENT && errno != ENOTDIR) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700554 fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000555 }
556 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000557}
558
Tianjie Xu01889352016-03-22 18:08:12 -0700559static int LoadStash(CommandParameters& params, const std::string& base, const std::string& id,
560 bool verify, size_t* blocks, std::vector<uint8_t>& buffer, bool printnoent) {
561 // In verify mode, if source range_set was saved for the given hash,
562 // check contents in the source blocks first. If the check fails,
563 // search for the stashed files on /cache as usual.
564 if (!params.canwrite) {
565 if (stash_map.find(id) != stash_map.end()) {
566 const RangeSet& src = stash_map[id];
567 allocate(src.size * BLOCKSIZE, buffer);
568
569 if (ReadBlocks(src, buffer, params.fd) == -1) {
570 fprintf(stderr, "failed to read source blocks in stash map.\n");
571 return -1;
572 }
573 if (VerifyBlocks(id, buffer, src.size, true) != 0) {
574 fprintf(stderr, "failed to verify loaded source blocks in stash map.\n");
575 return -1;
576 }
577 return 0;
578 }
579 }
580
Tao Bao612336d2015-08-27 16:41:21 -0700581 if (base.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -0700582 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000583 }
584
Tao Bao0940fe12015-08-27 16:41:21 -0700585 size_t blockcount = 0;
586
Sami Tolvanen90221202014-12-09 16:39:47 +0000587 if (!blocks) {
588 blocks = &blockcount;
589 }
590
Tao Bao0940fe12015-08-27 16:41:21 -0700591 std::string fn = GetStashFileName(base, id, "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000592
Tao Bao0940fe12015-08-27 16:41:21 -0700593 struct stat sb;
594 int res = stat(fn.c_str(), &sb);
Sami Tolvanen90221202014-12-09 16:39:47 +0000595
596 if (res == -1) {
597 if (errno != ENOENT || printnoent) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700598 fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000599 }
Tao Bao0940fe12015-08-27 16:41:21 -0700600 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000601 }
602
Tao Baoe6aa3322015-08-05 15:20:27 -0700603 fprintf(stderr, " loading %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000604
Tao Bao0940fe12015-08-27 16:41:21 -0700605 if ((sb.st_size % BLOCKSIZE) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700606 fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d",
Tao Bao0940fe12015-08-27 16:41:21 -0700607 fn.c_str(), static_cast<int64_t>(sb.st_size), BLOCKSIZE);
608 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000609 }
610
Tao Bao0940fe12015-08-27 16:41:21 -0700611 int fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY));
612 unique_fd fd_holder(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000613
614 if (fd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700615 fprintf(stderr, "open \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
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 Baoe6aa3322015-08-05 15:20:27 -0700628 fprintf(stderr, "unexpected contents in %s\n", fn.c_str());
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) {
643 fprintf(stderr, "not enough space to write stash\n");
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 Baoe6aa3322015-08-05 15:20:27 -0700658 fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn.c_str());
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 Baoe6aa3322015-08-05 15:20:27 -0700666 fprintf(stderr, " writing %d blocks to %s\n", blocks, cn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000667
Tao Bao0940fe12015-08-27 16:41:21 -0700668 int fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE));
669 unique_fd fd_holder(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000670
671 if (fd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700672 fprintf(stderr, "failed to create \"%s\": %s\n", fn.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700673 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000674 }
675
676 if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700677 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000678 }
679
Jed Estepf1fc48c2015-12-15 16:04:53 -0800680 if (ota_fsync(fd) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700681 failure_type = kFsyncFailure;
Tao Baoe6aa3322015-08-05 15:20:27 -0700682 fprintf(stderr, "fsync \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700683 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000684 }
685
Tao Baoe6aa3322015-08-05 15:20:27 -0700686 if (rename(fn.c_str(), cn.c_str()) == -1) {
687 fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn.c_str(), cn.c_str(),
688 strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700689 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000690 }
691
Tao Bao0940fe12015-08-27 16:41:21 -0700692 std::string dname = GetStashFileName(base, "", "");
693 int dfd = TEMP_FAILURE_RETRY(open(dname.c_str(), O_RDONLY | O_DIRECTORY));
694 unique_fd dfd_holder(dfd);
Tao Baodc392262015-07-31 15:56:44 -0700695
696 if (dfd == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700697 failure_type = kFileOpenFailure;
Tao Baoe6aa3322015-08-05 15:20:27 -0700698 fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700699 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700700 }
701
Jed Estepf1fc48c2015-12-15 16:04:53 -0800702 if (ota_fsync(dfd) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700703 failure_type = kFsyncFailure;
Tao Baoe6aa3322015-08-05 15:20:27 -0700704 fprintf(stderr, "fsync \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700705 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700706 }
707
Tao Bao0940fe12015-08-27 16:41:21 -0700708 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000709}
710
711// Creates a directory for storing stash files and checks if the /cache partition
712// hash enough space for the expected amount of blocks we need to store. Returns
713// >0 if we created the directory, zero if it existed already, and <0 of failure.
714
Tao Bao0940fe12015-08-27 16:41:21 -0700715static int CreateStash(State* state, int maxblocks, const char* blockdev, std::string& base) {
716 if (blockdev == nullptr) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700717 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000718 }
719
720 // Stash directory should be different for each partition to avoid conflicts
721 // when updating multiple partitions at the same time, so we use the hash of
722 // the block device name as the base directory
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800723 uint8_t digest[SHA_DIGEST_LENGTH];
724 SHA1(reinterpret_cast<const uint8_t*>(blockdev), strlen(blockdev), digest);
Tao Baoe6aa3322015-08-05 15:20:27 -0700725 base = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000726
Tao Baoe6aa3322015-08-05 15:20:27 -0700727 std::string dirname = GetStashFileName(base, "", "");
Tao Bao0940fe12015-08-27 16:41:21 -0700728 struct stat sb;
729 int res = stat(dirname.c_str(), &sb);
Sami Tolvanen90221202014-12-09 16:39:47 +0000730
731 if (res == -1 && errno != ENOENT) {
Tianjie Xu16255832016-04-30 11:49:59 -0700732 ErrorAbort(state, kStashCreationFailure, "stat \"%s\" failed: %s\n",
733 dirname.c_str(), strerror(errno));
Tao Baoe6aa3322015-08-05 15:20:27 -0700734 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000735 } else if (res != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700736 fprintf(stderr, "creating stash %s\n", dirname.c_str());
737 res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000738
739 if (res != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700740 ErrorAbort(state, kStashCreationFailure, "mkdir \"%s\" failed: %s\n",
741 dirname.c_str(), strerror(errno));
Tao Baoe6aa3322015-08-05 15:20:27 -0700742 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000743 }
744
745 if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700746 ErrorAbort(state, kStashCreationFailure, "not enough space for stash\n");
Tao Baoe6aa3322015-08-05 15:20:27 -0700747 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000748 }
749
Tao Baoe6aa3322015-08-05 15:20:27 -0700750 return 1; // Created directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000751 }
752
Tao Baoe6aa3322015-08-05 15:20:27 -0700753 fprintf(stderr, "using existing stash %s\n", dirname.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000754
755 // 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.
758
Tao Bao0940fe12015-08-27 16:41:21 -0700759 EnumerateStash(dirname, DeletePartial, nullptr);
Tao Baoe6aa3322015-08-05 15:20:27 -0700760 int size = 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000761 EnumerateStash(dirname, UpdateFileSize, &size);
762
Tao Bao0940fe12015-08-27 16:41:21 -0700763 size = maxblocks * BLOCKSIZE - size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000764
765 if (size > 0 && CacheSizeCheck(size) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700766 ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%d more needed)\n",
767 size);
Tao Baoe6aa3322015-08-05 15:20:27 -0700768 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000769 }
770
Tao Baoe6aa3322015-08-05 15:20:27 -0700771 return 0; // Using existing directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000772}
773
Tao Baobaad2d42015-12-06 16:56:27 -0800774static int SaveStash(CommandParameters& params, const std::string& base,
775 std::vector<uint8_t>& buffer, int fd, bool usehash) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000776
Tao Baobaad2d42015-12-06 16:56:27 -0800777 // <stash_id> <src_range>
778 if (params.cpos + 1 >= params.tokens.size()) {
779 fprintf(stderr, "missing id and/or src range fields in stash command\n");
Sami Tolvanen90221202014-12-09 16:39:47 +0000780 return -1;
781 }
Tao Baobaad2d42015-12-06 16:56:27 -0800782 const std::string& id = params.tokens[params.cpos++];
Sami Tolvanen90221202014-12-09 16:39:47 +0000783
Tao Bao0940fe12015-08-27 16:41:21 -0700784 size_t blocks = 0;
Tianjie Xu01889352016-03-22 18:08:12 -0700785 if (usehash && LoadStash(params, base, id, true, &blocks, buffer, false) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000786 // Stash file already exists and has expected contents. Do not
787 // read from source again, as the source may have been already
788 // overwritten during a previous attempt.
789 return 0;
790 }
791
Tao Bao34847b22015-09-08 11:05:49 -0700792 RangeSet src;
Tao Baobaad2d42015-12-06 16:56:27 -0800793 parse_range(params.tokens[params.cpos++], src);
Tao Bao34847b22015-09-08 11:05:49 -0700794
Tao Bao612336d2015-08-27 16:41:21 -0700795 allocate(src.size * BLOCKSIZE, buffer);
796 if (ReadBlocks(src, buffer, fd) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000797 return -1;
798 }
Tao Bao34847b22015-09-08 11:05:49 -0700799 blocks = src.size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000800
Tao Bao612336d2015-08-27 16:41:21 -0700801 if (usehash && VerifyBlocks(id, buffer, blocks, true) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000802 // Source blocks have unexpected contents. If we actually need this
803 // data later, this is an unrecoverable error. However, the command
804 // that uses the data may have already completed previously, so the
805 // possible failure will occur during source block verification.
Tao Bao0940fe12015-08-27 16:41:21 -0700806 fprintf(stderr, "failed to load source blocks for stash %s\n", id.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000807 return 0;
808 }
809
Tianjie Xu01889352016-03-22 18:08:12 -0700810 // In verify mode, save source range_set instead of stashing blocks.
811 if (!params.canwrite && usehash) {
812 stash_map[id] = src;
813 return 0;
814 }
815
Tao Bao0940fe12015-08-27 16:41:21 -0700816 fprintf(stderr, "stashing %zu blocks to %s\n", blocks, id.c_str());
Tianjie Xudd874b12016-05-13 12:13:15 -0700817 params.stashed += blocks;
Tao Bao612336d2015-08-27 16:41:21 -0700818 return WriteStash(base, id, blocks, buffer, false, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000819}
820
Tao Baobaad2d42015-12-06 16:56:27 -0800821static int FreeStash(const std::string& base, const std::string& id) {
822 if (base.empty() || id.empty()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000823 return -1;
824 }
825
Tao Baobaad2d42015-12-06 16:56:27 -0800826 std::string fn = GetStashFileName(base, id, "");
Tao Bao0940fe12015-08-27 16:41:21 -0700827 DeleteFile(fn, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000828
829 return 0;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700830}
831
Tao Bao612336d2015-08-27 16:41:21 -0700832static void MoveRange(std::vector<uint8_t>& dest, const RangeSet& locs,
833 const std::vector<uint8_t>& source) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700834 // source contains packed data, which we want to move to the
Tao Bao612336d2015-08-27 16:41:21 -0700835 // locations given in locs in the dest buffer. source and dest
Doug Zongker52ae67d2014-09-08 12:22:09 -0700836 // may be the same buffer.
837
Tao Bao612336d2015-08-27 16:41:21 -0700838 const uint8_t* from = source.data();
839 uint8_t* to = dest.data();
Tao Bao0940fe12015-08-27 16:41:21 -0700840 size_t start = locs.size;
841 for (int i = locs.count-1; i >= 0; --i) {
842 size_t blocks = locs.pos[i*2+1] - locs.pos[i*2];
Doug Zongker52ae67d2014-09-08 12:22:09 -0700843 start -= blocks;
Tao Bao612336d2015-08-27 16:41:21 -0700844 memmove(to + (locs.pos[i*2] * BLOCKSIZE), from + (start * BLOCKSIZE),
Doug Zongker52ae67d2014-09-08 12:22:09 -0700845 blocks * BLOCKSIZE);
846 }
847}
848
849// Do a source/target load for move/bsdiff/imgdiff in version 2.
Tao Baobaad2d42015-12-06 16:56:27 -0800850// We expect to parse the remainder of the parameter tokens as one of:
Doug Zongker52ae67d2014-09-08 12:22:09 -0700851//
852// <tgt_range> <src_block_count> <src_range>
853// (loads data from source image only)
854//
855// <tgt_range> <src_block_count> - <[stash_id:stash_range] ...>
856// (loads data from stashes only)
857//
858// <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...>
859// (loads data from both source image and stashes)
860//
861// On return, buffer is filled with the loaded source data (rearranged
862// and combined with stashed data as necessary). buffer may be
863// reallocated if needed to accommodate the source data. *tgt is the
Sami Tolvanen90221202014-12-09 16:39:47 +0000864// target RangeSet. Any stashes required are loaded using LoadStash.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700865
Tao Baobaad2d42015-12-06 16:56:27 -0800866static int LoadSrcTgtVersion2(CommandParameters& params, RangeSet& tgt, size_t& src_blocks,
Tao Bao612336d2015-08-27 16:41:21 -0700867 std::vector<uint8_t>& buffer, int fd, const std::string& stashbase, bool* overlap) {
Tao Baobaad2d42015-12-06 16:56:27 -0800868
869 // At least it needs to provide three parameters: <tgt_range>,
870 // <src_block_count> and "-"/<src_range>.
871 if (params.cpos + 2 >= params.tokens.size()) {
872 fprintf(stderr, "invalid parameters\n");
873 return -1;
874 }
875
Tao Bao612336d2015-08-27 16:41:21 -0700876 // <tgt_range>
Tao Baobaad2d42015-12-06 16:56:27 -0800877 parse_range(params.tokens[params.cpos++], tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700878
Tao Bao612336d2015-08-27 16:41:21 -0700879 // <src_block_count>
Tao Baobaad2d42015-12-06 16:56:27 -0800880 const std::string& token = params.tokens[params.cpos++];
881 if (!android::base::ParseUint(token.c_str(), &src_blocks)) {
882 fprintf(stderr, "invalid src_block_count \"%s\"\n", token.c_str());
883 return -1;
884 }
Doug Zongker52ae67d2014-09-08 12:22:09 -0700885
Tao Bao612336d2015-08-27 16:41:21 -0700886 allocate(src_blocks * BLOCKSIZE, buffer);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700887
Tao Bao612336d2015-08-27 16:41:21 -0700888 // "-" or <src_range> [<src_loc>]
Tao Baobaad2d42015-12-06 16:56:27 -0800889 if (params.tokens[params.cpos] == "-") {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700890 // no source ranges, only stashes
Tao Baobaad2d42015-12-06 16:56:27 -0800891 params.cpos++;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700892 } else {
Tao Bao0940fe12015-08-27 16:41:21 -0700893 RangeSet src;
Tao Baobaad2d42015-12-06 16:56:27 -0800894 parse_range(params.tokens[params.cpos++], src);
Tao Bao612336d2015-08-27 16:41:21 -0700895 int res = ReadBlocks(src, buffer, fd);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700896
Tao Bao34847b22015-09-08 11:05:49 -0700897 if (overlap) {
898 *overlap = range_overlaps(src, tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700899 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000900
Sami Tolvanen90221202014-12-09 16:39:47 +0000901 if (res == -1) {
902 return -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700903 }
904
Tao Baobaad2d42015-12-06 16:56:27 -0800905 if (params.cpos >= params.tokens.size()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000906 // no stashes, only source range
907 return 0;
908 }
909
Tao Bao612336d2015-08-27 16:41:21 -0700910 RangeSet locs;
Tao Baobaad2d42015-12-06 16:56:27 -0800911 parse_range(params.tokens[params.cpos++], locs);
Tao Bao612336d2015-08-27 16:41:21 -0700912 MoveRange(buffer, locs, buffer);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700913 }
914
Tao Baobaad2d42015-12-06 16:56:27 -0800915 // <[stash_id:stash_range]>
916 while (params.cpos < params.tokens.size()) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700917 // Each word is a an index into the stash table, a colon, and
918 // then a rangeset describing where in the source block that
919 // stashed data should go.
Tao Baobaad2d42015-12-06 16:56:27 -0800920 std::vector<std::string> tokens = android::base::Split(params.tokens[params.cpos++], ":");
921 if (tokens.size() != 2) {
922 fprintf(stderr, "invalid parameter\n");
923 return -1;
924 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000925
Tao Bao612336d2015-08-27 16:41:21 -0700926 std::vector<uint8_t> stash;
Tianjie Xu01889352016-03-22 18:08:12 -0700927 int res = LoadStash(params, stashbase, tokens[0], false, nullptr, stash, true);
Sami Tolvanen90221202014-12-09 16:39:47 +0000928
929 if (res == -1) {
930 // These source blocks will fail verification if used later, but we
931 // will let the caller decide if this is a fatal failure
Tao Baobaad2d42015-12-06 16:56:27 -0800932 fprintf(stderr, "failed to load stash %s\n", tokens[0].c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000933 continue;
934 }
935
Tao Bao612336d2015-08-27 16:41:21 -0700936 RangeSet locs;
Tao Baobaad2d42015-12-06 16:56:27 -0800937 parse_range(tokens[1], locs);
Sami Tolvanen90221202014-12-09 16:39:47 +0000938
Tao Bao612336d2015-08-27 16:41:21 -0700939 MoveRange(buffer, locs, stash);
Sami Tolvanen90221202014-12-09 16:39:47 +0000940 }
941
942 return 0;
943}
944
Sami Tolvanen90221202014-12-09 16:39:47 +0000945// Do a source/target load for move/bsdiff/imgdiff in version 3.
946//
947// Parameters are the same as for LoadSrcTgtVersion2, except for 'onehash', which
948// tells the function whether to expect separate source and targe block hashes, or
949// if they are both the same and only one hash should be expected, and
950// 'isunresumable', which receives a non-zero value if block verification fails in
951// a way that the update cannot be resumed anymore.
952//
953// If the function is unable to load the necessary blocks or their contents don't
954// match the hashes, the return value is -1 and the command should be aborted.
955//
956// If the return value is 1, the command has already been completed according to
957// the contents of the target blocks, and should not be performed again.
958//
959// If the return value is 0, source blocks have expected content and the command
960// can be performed.
961
Tao Bao34847b22015-09-08 11:05:49 -0700962static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t& src_blocks,
Tao Bao0940fe12015-08-27 16:41:21 -0700963 bool onehash, bool& overlap) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000964
Tao Baobaad2d42015-12-06 16:56:27 -0800965 if (params.cpos >= params.tokens.size()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000966 fprintf(stderr, "missing source hash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700967 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000968 }
969
Tao Baobaad2d42015-12-06 16:56:27 -0800970 std::string srchash = params.tokens[params.cpos++];
971 std::string tgthash;
972
Sami Tolvanen90221202014-12-09 16:39:47 +0000973 if (onehash) {
974 tgthash = srchash;
975 } else {
Tao Baobaad2d42015-12-06 16:56:27 -0800976 if (params.cpos >= params.tokens.size()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000977 fprintf(stderr, "missing target hash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700978 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000979 }
Tao Baobaad2d42015-12-06 16:56:27 -0800980 tgthash = params.tokens[params.cpos++];
Sami Tolvanen90221202014-12-09 16:39:47 +0000981 }
982
Tao Baobaad2d42015-12-06 16:56:27 -0800983 if (LoadSrcTgtVersion2(params, tgt, src_blocks, params.buffer, params.fd, params.stashbase,
984 &overlap) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700985 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000986 }
987
Tao Bao34847b22015-09-08 11:05:49 -0700988 std::vector<uint8_t> tgtbuffer(tgt.size * BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000989
Tao Bao612336d2015-08-27 16:41:21 -0700990 if (ReadBlocks(tgt, tgtbuffer, params.fd) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700991 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000992 }
993
Tao Bao612336d2015-08-27 16:41:21 -0700994 if (VerifyBlocks(tgthash, tgtbuffer, tgt.size, false) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000995 // Target blocks already have expected content, command should be skipped
Tao Bao0940fe12015-08-27 16:41:21 -0700996 return 1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000997 }
998
Tao Bao0940fe12015-08-27 16:41:21 -0700999 if (VerifyBlocks(srchash, params.buffer, src_blocks, true) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001000 // If source and target blocks overlap, stash the source blocks so we can
Tianjie Xu01889352016-03-22 18:08:12 -07001001 // resume from possible write errors. In verify mode, we can skip stashing
1002 // because the source blocks won't be overwritten.
1003 if (overlap && params.canwrite) {
Tao Baobaad2d42015-12-06 16:56:27 -08001004 fprintf(stderr, "stashing %zu overlapping blocks to %s\n", src_blocks,
1005 srchash.c_str());
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) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001010 fprintf(stderr, "failed to stash overlapping source blocks\n");
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 Xu01889352016-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
1034 fprintf(stderr, "partition has unexpected contents\n");
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) {
1056 fprintf(stderr, "failed to read blocks for move\n");
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) {
1063 fprintf(stderr, "warning: commands executed out of order [%s]\n", 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 Bao0940fe12015-08-27 16:41:21 -07001068 fprintf(stderr, " moving %zu blocks\n", 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 Bao0940fe12015-08-27 16:41:21 -07001074 fprintf(stderr, "skipping %zu already moved blocks\n", 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()) {
1097 fprintf(stderr, "missing stash id in free command\n");
1098 return -1;
1099 }
1100
Tianjie Xu01889352016-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 Xu01889352016-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()) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001118 fprintf(stderr, "missing target blocks for zero\n");
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 Bao0940fe12015-08-27 16:41:21 -07001125 fprintf(stderr, " zeroing %zu blocks\n", tgt.size);
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()) {
1162 fprintf(stderr, "missing target blocks for new\n");
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) {
1170 fprintf(stderr, " writing %zu blocks of new data\n", tgt.size);
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()) {
1206 fprintf(stderr, "missing patch offset or length for %s\n", 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)) {
1212 fprintf(stderr, "invalid patch offset\n");
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)) {
1218 fprintf(stderr, "invalid patch offset\n");
1219 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) {
1236 fprintf(stderr, "failed to read blocks for diff\n");
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) {
1243 fprintf(stderr, "warning: commands executed out of order [%s]\n", 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 Bao0940fe12015-08-27 16:41:21 -07001248 fprintf(stderr, "patching %zu blocks to %zu\n", blocks, tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001249
Tao Bao0940fe12015-08-27 16:41:21 -07001250 Value patch_value;
Sami Tolvanen90221202014-12-09 16:39:47 +00001251 patch_value.type = VAL_BLOB;
1252 patch_value.size = len;
Tao Bao0940fe12015-08-27 16:41:21 -07001253 patch_value.data = (char*) (params.patch_start + offset);
Sami Tolvanen90221202014-12-09 16:39:47 +00001254
Tao Bao0940fe12015-08-27 16:41:21 -07001255 RangeSinkState rss(tgt);
1256 rss.fd = params.fd;
Sami Tolvanen90221202014-12-09 16:39:47 +00001257 rss.p_block = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001258 rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001259
Tianjie Xu7ce287d2016-05-31 09:29:49 -07001260 off64_t offset = static_cast<off64_t>(tgt.pos[0]) * BLOCKSIZE;
1261 if (!discard_blocks(params.fd, offset, rss.p_remain)) {
1262 return -1;
1263 }
1264
1265 if (!check_lseek(params.fd, offset, SEEK_SET)) {
Tao Bao0940fe12015-08-27 16:41:21 -07001266 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001267 }
1268
Tao Bao0940fe12015-08-27 16:41:21 -07001269 if (params.cmdname[0] == 'i') { // imgdiff
Tao Bao612336d2015-08-27 16:41:21 -07001270 ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value,
Tao Bao0940fe12015-08-27 16:41:21 -07001271 &RangeSinkWrite, &rss, nullptr, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +00001272 } else {
Tao Bao612336d2015-08-27 16:41:21 -07001273 ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value, 0,
1274 &RangeSinkWrite, &rss, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +00001275 }
1276
1277 // We expect the output of the patcher to fill the tgt ranges exactly.
Tao Bao0940fe12015-08-27 16:41:21 -07001278 if (rss.p_block != tgt.count || rss.p_remain != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001279 fprintf(stderr, "range sink underrun?\n");
1280 }
1281 } else {
Tao Bao0940fe12015-08-27 16:41:21 -07001282 fprintf(stderr, "skipping %zu blocks already patched to %zu [%s]\n",
Tao Baobaad2d42015-12-06 16:56:27 -08001283 blocks, tgt.size, params.cmdline);
Sami Tolvanen90221202014-12-09 16:39:47 +00001284 }
1285 }
1286
Tao Baobaad2d42015-12-06 16:56:27 -08001287 if (!params.freestash.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -07001288 FreeStash(params.stashbase, params.freestash);
Tao Baobaad2d42015-12-06 16:56:27 -08001289 params.freestash.clear();
Sami Tolvanen90221202014-12-09 16:39:47 +00001290 }
1291
Tao Bao0940fe12015-08-27 16:41:21 -07001292 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001293
Tao Bao0940fe12015-08-27 16:41:21 -07001294 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001295}
1296
Tao Bao0940fe12015-08-27 16:41:21 -07001297static int PerformCommandErase(CommandParameters& params) {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001298 if (DEBUG_ERASE) {
1299 return PerformCommandZero(params);
Sami Tolvanen90221202014-12-09 16:39:47 +00001300 }
1301
Tao Bao0940fe12015-08-27 16:41:21 -07001302 struct stat sb;
1303 if (fstat(params.fd, &sb) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001304 fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -07001305 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001306 }
1307
Tao Bao0940fe12015-08-27 16:41:21 -07001308 if (!S_ISBLK(sb.st_mode)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001309 fprintf(stderr, "not a block device; skipping erase\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001310 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001311 }
1312
Tao Baobaad2d42015-12-06 16:56:27 -08001313 if (params.cpos >= params.tokens.size()) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001314 fprintf(stderr, "missing target blocks for erase\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001315 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001316 }
1317
Tao Bao0940fe12015-08-27 16:41:21 -07001318 RangeSet tgt;
Tao Baobaad2d42015-12-06 16:56:27 -08001319 parse_range(params.tokens[params.cpos++], tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001320
Tao Bao0940fe12015-08-27 16:41:21 -07001321 if (params.canwrite) {
1322 fprintf(stderr, " erasing %zu blocks\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001323
Tao Bao0940fe12015-08-27 16:41:21 -07001324 for (size_t i = 0; i < tgt.count; ++i) {
1325 uint64_t blocks[2];
Sami Tolvanen90221202014-12-09 16:39:47 +00001326 // offset in bytes
Tao Bao0940fe12015-08-27 16:41:21 -07001327 blocks[0] = tgt.pos[i * 2] * (uint64_t) BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001328 // length in bytes
Tao Bao0940fe12015-08-27 16:41:21 -07001329 blocks[1] = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * (uint64_t) BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001330
Tao Bao0940fe12015-08-27 16:41:21 -07001331 if (ioctl(params.fd, BLKDISCARD, &blocks) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001332 fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -07001333 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001334 }
1335 }
1336 }
1337
Tao Bao0940fe12015-08-27 16:41:21 -07001338 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001339}
1340
1341// Definitions for transfer list command functions
Tao Bao0940fe12015-08-27 16:41:21 -07001342typedef int (*CommandFunction)(CommandParameters&);
Sami Tolvanen90221202014-12-09 16:39:47 +00001343
Tao Bao612336d2015-08-27 16:41:21 -07001344struct Command {
Sami Tolvanen90221202014-12-09 16:39:47 +00001345 const char* name;
1346 CommandFunction f;
Tao Bao612336d2015-08-27 16:41:21 -07001347};
Sami Tolvanen90221202014-12-09 16:39:47 +00001348
1349// CompareCommands and CompareCommandNames are for the hash table
1350
1351static int CompareCommands(const void* c1, const void* c2) {
1352 return strcmp(((const Command*) c1)->name, ((const Command*) c2)->name);
1353}
1354
1355static int CompareCommandNames(const void* c1, const void* c2) {
1356 return strcmp(((const Command*) c1)->name, (const char*) c2);
1357}
1358
1359// HashString is used to hash command names for the hash table
1360
1361static unsigned int HashString(const char *s) {
1362 unsigned int hash = 0;
1363 if (s) {
1364 while (*s) {
1365 hash = hash * 33 + *s++;
1366 }
1367 }
1368 return hash;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001369}
1370
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001371// args:
1372// - block device (or file) to modify in-place
1373// - transfer list (blob)
1374// - new data stream (filename within package.zip)
1375// - patch stream (filename within package.zip, must be uncompressed)
1376
Tao Bao0940fe12015-08-27 16:41:21 -07001377static Value* PerformBlockImageUpdate(const char* name, State* state, int /* argc */, Expr* argv[],
1378 const Command* commands, size_t cmdcount, bool dryrun) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001379 CommandParameters params;
Sami Tolvanen90221202014-12-09 16:39:47 +00001380 memset(&params, 0, sizeof(params));
1381 params.canwrite = !dryrun;
1382
1383 fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update");
Tianjie Xu7ce287d2016-05-31 09:29:49 -07001384 if (state->is_retry) {
1385 is_retry = true;
1386 fprintf(stderr, "This update is a retry.\n");
1387 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001388
Tao Bao612336d2015-08-27 16:41:21 -07001389 Value* blockdev_filename = nullptr;
1390 Value* transfer_list_value = nullptr;
1391 Value* new_data_fn = nullptr;
1392 Value* patch_data_fn = nullptr;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001393 if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value,
Sami Tolvanen90221202014-12-09 16:39:47 +00001394 &new_data_fn, &patch_data_fn) < 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001395 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001396 }
Tao Bao612336d2015-08-27 16:41:21 -07001397 std::unique_ptr<Value, decltype(&FreeValue)> blockdev_filename_holder(blockdev_filename,
1398 FreeValue);
1399 std::unique_ptr<Value, decltype(&FreeValue)> transfer_list_value_holder(transfer_list_value,
1400 FreeValue);
1401 std::unique_ptr<Value, decltype(&FreeValue)> new_data_fn_holder(new_data_fn, FreeValue);
1402 std::unique_ptr<Value, decltype(&FreeValue)> patch_data_fn_holder(patch_data_fn, FreeValue);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001403
1404 if (blockdev_filename->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001405 ErrorAbort(state, kArgsParsingFailure, "blockdev_filename argument to %s must be string",
1406 name);
Tao Bao612336d2015-08-27 16:41:21 -07001407 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001408 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001409 if (transfer_list_value->type != VAL_BLOB) {
Tianjie Xu16255832016-04-30 11:49:59 -07001410 ErrorAbort(state, kArgsParsingFailure, "transfer_list argument to %s must be blob", name);
Tao Bao612336d2015-08-27 16:41:21 -07001411 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001412 }
1413 if (new_data_fn->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001414 ErrorAbort(state, kArgsParsingFailure, "new_data_fn argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001415 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001416 }
1417 if (patch_data_fn->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001418 ErrorAbort(state, kArgsParsingFailure, "patch_data_fn argument to %s must be string",
1419 name);
Tao Bao612336d2015-08-27 16:41:21 -07001420 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001421 }
1422
Tao Bao612336d2015-08-27 16:41:21 -07001423 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001424
Tao Bao0940fe12015-08-27 16:41:21 -07001425 if (ui == nullptr) {
Tao Bao612336d2015-08-27 16:41:21 -07001426 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001427 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001428
Tao Bao612336d2015-08-27 16:41:21 -07001429 FILE* cmd_pipe = ui->cmd_pipe;
1430 ZipArchive* za = ui->package_zip;
Sami Tolvanen90221202014-12-09 16:39:47 +00001431
Tao Bao0940fe12015-08-27 16:41:21 -07001432 if (cmd_pipe == nullptr || za == nullptr) {
Tao Bao612336d2015-08-27 16:41:21 -07001433 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001434 }
1435
Tao Bao612336d2015-08-27 16:41:21 -07001436 const ZipEntry* patch_entry = mzFindZipEntry(za, patch_data_fn->data);
Tao Bao0940fe12015-08-27 16:41:21 -07001437 if (patch_entry == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001438 fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data);
Tao Bao612336d2015-08-27 16:41:21 -07001439 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001440 }
1441
Sami Tolvanen90221202014-12-09 16:39:47 +00001442 params.patch_start = ui->package_zip_addr + mzGetZipEntryOffset(patch_entry);
Tao Bao612336d2015-08-27 16:41:21 -07001443 const ZipEntry* new_entry = mzFindZipEntry(za, new_data_fn->data);
Tao Bao0940fe12015-08-27 16:41:21 -07001444 if (new_entry == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001445 fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data);
Tao Bao612336d2015-08-27 16:41:21 -07001446 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001447 }
1448
Sami Tolvanen90221202014-12-09 16:39:47 +00001449 params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR));
Tao Bao612336d2015-08-27 16:41:21 -07001450 unique_fd fd_holder(params.fd);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001451
Sami Tolvanen90221202014-12-09 16:39:47 +00001452 if (params.fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001453 fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data, strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001454 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001455 }
1456
Sami Tolvanen90221202014-12-09 16:39:47 +00001457 if (params.canwrite) {
1458 params.nti.za = za;
1459 params.nti.entry = new_entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001460
Tao Bao0940fe12015-08-27 16:41:21 -07001461 pthread_mutex_init(&params.nti.mu, nullptr);
1462 pthread_cond_init(&params.nti.cv, nullptr);
Tao Bao612336d2015-08-27 16:41:21 -07001463 pthread_attr_t attr;
Sami Tolvanen90221202014-12-09 16:39:47 +00001464 pthread_attr_init(&attr);
1465 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1466
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001467 int error = pthread_create(&params.thread, &attr, unzip_new_data, &params.nti);
1468 if (error != 0) {
1469 fprintf(stderr, "pthread_create failed: %s\n", strerror(error));
Tao Bao612336d2015-08-27 16:41:21 -07001470 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001471 }
1472 }
1473
Tao Baobaad2d42015-12-06 16:56:27 -08001474 // Copy all the lines in transfer_list_value into std::string for
1475 // processing.
Tao Bao612336d2015-08-27 16:41:21 -07001476 const std::string transfer_list(transfer_list_value->data, transfer_list_value->size);
1477 std::vector<std::string> lines = android::base::Split(transfer_list, "\n");
Tao Baobaad2d42015-12-06 16:56:27 -08001478 if (lines.size() < 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001479 ErrorAbort(state, kArgsParsingFailure, "too few lines in the transfer list [%zd]\n",
1480 lines.size());
Tao Baobaad2d42015-12-06 16:56:27 -08001481 return StringValue(strdup(""));
1482 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001483
Sami Tolvanen90221202014-12-09 16:39:47 +00001484 // First line in transfer list is the version number
Tao Bao1fdec862015-10-21 14:57:44 -07001485 if (!android::base::ParseInt(lines[0].c_str(), &params.version, 1, 4)) {
Tao Bao612336d2015-08-27 16:41:21 -07001486 fprintf(stderr, "unexpected transfer list version [%s]\n", lines[0].c_str());
1487 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001488 }
1489
Sami Tolvanen90221202014-12-09 16:39:47 +00001490 fprintf(stderr, "blockimg version is %d\n", params.version);
1491
1492 // Second line in transfer list is the total number of blocks we expect to write
Tao Baob15fd222015-09-24 11:10:51 -07001493 int total_blocks;
1494 if (!android::base::ParseInt(lines[1].c_str(), &total_blocks, 0)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001495 ErrorAbort(state, kArgsParsingFailure, "unexpected block count [%s]\n", lines[1].c_str());
Tao Bao612336d2015-08-27 16:41:21 -07001496 return StringValue(strdup(""));
Tao Baob15fd222015-09-24 11:10:51 -07001497 }
1498
1499 if (total_blocks == 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001500 return StringValue(strdup("t"));
Sami Tolvanen90221202014-12-09 16:39:47 +00001501 }
1502
Tao Bao612336d2015-08-27 16:41:21 -07001503 size_t start = 2;
Sami Tolvanen90221202014-12-09 16:39:47 +00001504 if (params.version >= 2) {
Tao Baobaad2d42015-12-06 16:56:27 -08001505 if (lines.size() < 4) {
Tianjie Xu16255832016-04-30 11:49:59 -07001506 ErrorAbort(state, kArgsParsingFailure, "too few lines in the transfer list [%zu]\n",
1507 lines.size());
Tao Baobaad2d42015-12-06 16:56:27 -08001508 return StringValue(strdup(""));
1509 }
1510
Sami Tolvanen90221202014-12-09 16:39:47 +00001511 // Third line is how many stash entries are needed simultaneously
Tao Bao612336d2015-08-27 16:41:21 -07001512 fprintf(stderr, "maximum stash entries %s\n", lines[2].c_str());
Doug Zongker52ae67d2014-09-08 12:22:09 -07001513
Sami Tolvanen90221202014-12-09 16:39:47 +00001514 // Fourth line is the maximum number of blocks that will be stashed simultaneously
Tao Baob15fd222015-09-24 11:10:51 -07001515 int stash_max_blocks;
1516 if (!android::base::ParseInt(lines[3].c_str(), &stash_max_blocks, 0)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001517 ErrorAbort(state, kArgsParsingFailure, "unexpected maximum stash blocks [%s]\n",
1518 lines[3].c_str());
Tao Bao612336d2015-08-27 16:41:21 -07001519 return StringValue(strdup(""));
Doug Zongker52ae67d2014-09-08 12:22:09 -07001520 }
1521
Tao Baob15fd222015-09-24 11:10:51 -07001522 int res = CreateStash(state, stash_max_blocks, blockdev_filename->data, params.stashbase);
Tao Baob15fd222015-09-24 11:10:51 -07001523 if (res == -1) {
1524 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001525 }
Tao Bao612336d2015-08-27 16:41:21 -07001526
Tao Baob15fd222015-09-24 11:10:51 -07001527 params.createdstash = res;
1528
Tao Bao612336d2015-08-27 16:41:21 -07001529 start += 2;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001530 }
1531
Sami Tolvanen90221202014-12-09 16:39:47 +00001532 // Build a hash table of the available commands
Tao Bao612336d2015-08-27 16:41:21 -07001533 HashTable* cmdht = mzHashTableCreate(cmdcount, nullptr);
1534 std::unique_ptr<HashTable, decltype(&mzHashTableFree)> cmdht_holder(cmdht, mzHashTableFree);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001535
Tao Bao0940fe12015-08-27 16:41:21 -07001536 for (size_t i = 0; i < cmdcount; ++i) {
1537 unsigned int cmdhash = HashString(commands[i].name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001538 mzHashTableLookup(cmdht, cmdhash, (void*) &commands[i], CompareCommands, true);
1539 }
1540
Tao Bao612336d2015-08-27 16:41:21 -07001541 int rc = -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001542
Tao Bao612336d2015-08-27 16:41:21 -07001543 // Subsequent lines are all individual transfer commands
1544 for (auto it = lines.cbegin() + start; it != lines.cend(); it++) {
1545 const std::string& line_str(*it);
Tao Bao6a47dff2015-09-25 17:12:28 -07001546 if (line_str.empty()) {
1547 continue;
1548 }
1549
Tao Baobaad2d42015-12-06 16:56:27 -08001550 params.tokens = android::base::Split(line_str, " ");
1551 params.cpos = 0;
1552 params.cmdname = params.tokens[params.cpos++].c_str();
1553 params.cmdline = line_str.c_str();
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001554
Tao Bao0940fe12015-08-27 16:41:21 -07001555 unsigned int cmdhash = HashString(params.cmdname);
Tao Bao612336d2015-08-27 16:41:21 -07001556 const Command* cmd = reinterpret_cast<const Command*>(mzHashTableLookup(cmdht, cmdhash,
Tao Baobaad2d42015-12-06 16:56:27 -08001557 const_cast<char*>(params.cmdname), CompareCommandNames,
1558 false));
Doug Zongker52ae67d2014-09-08 12:22:09 -07001559
Tao Bao0940fe12015-08-27 16:41:21 -07001560 if (cmd == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001561 fprintf(stderr, "unexpected command [%s]\n", params.cmdname);
1562 goto pbiudone;
1563 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001564
Tao Bao0940fe12015-08-27 16:41:21 -07001565 if (cmd->f != nullptr && cmd->f(params) == -1) {
Tao Bao612336d2015-08-27 16:41:21 -07001566 fprintf(stderr, "failed to execute command [%s]\n", line_str.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +00001567 goto pbiudone;
1568 }
1569
Sami Tolvanen90221202014-12-09 16:39:47 +00001570 if (params.canwrite) {
Jed Estepf1fc48c2015-12-15 16:04:53 -08001571 if (ota_fsync(params.fd) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001572 failure_type = kFsyncFailure;
Tao Bao187efff2015-07-27 14:07:08 -07001573 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
1574 goto pbiudone;
1575 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001576 fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001577 fflush(cmd_pipe);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001578 }
1579 }
1580
Sami Tolvanen90221202014-12-09 16:39:47 +00001581 if (params.canwrite) {
Tao Bao0940fe12015-08-27 16:41:21 -07001582 pthread_join(params.thread, nullptr);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001583
Tao Bao0940fe12015-08-27 16:41:21 -07001584 fprintf(stderr, "wrote %zu blocks; expected %d\n", params.written, total_blocks);
Tianjie Xudd874b12016-05-13 12:13:15 -07001585 fprintf(stderr, "stashed %zu blocks\n", params.stashed);
Tao Bao612336d2015-08-27 16:41:21 -07001586 fprintf(stderr, "max alloc needed was %zu\n", params.buffer.size());
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001587
Tianjie Xudd874b12016-05-13 12:13:15 -07001588 const char* partition = strrchr(blockdev_filename->data, '/');
1589 if (partition != nullptr && *(partition+1) != 0) {
1590 fprintf(cmd_pipe, "log bytes_written_%s: %zu\n", partition + 1,
1591 params.written * BLOCKSIZE);
1592 fprintf(cmd_pipe, "log bytes_stashed_%s: %zu\n", partition + 1,
1593 params.stashed * BLOCKSIZE);
1594 fflush(cmd_pipe);
1595 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001596 // Delete stash only after successfully completing the update, as it
1597 // may contain blocks needed to complete the update later.
1598 DeleteStash(params.stashbase);
1599 } else {
1600 fprintf(stderr, "verified partition contents; update may be resumed\n");
1601 }
1602
1603 rc = 0;
1604
1605pbiudone:
Jed Estepf1fc48c2015-12-15 16:04:53 -08001606 if (ota_fsync(params.fd) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001607 failure_type = kFsyncFailure;
Tao Baobaad2d42015-12-06 16:56:27 -08001608 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001609 }
Tao Baobaad2d42015-12-06 16:56:27 -08001610 // params.fd will be automatically closed because of the fd_holder above.
Sami Tolvanen90221202014-12-09 16:39:47 +00001611
Sami Tolvanen90221202014-12-09 16:39:47 +00001612 // Only delete the stash if the update cannot be resumed, or it's
1613 // a verification run and we created the stash.
1614 if (params.isunresumable || (!params.canwrite && params.createdstash)) {
1615 DeleteStash(params.stashbase);
1616 }
1617
Tianjie Xu16255832016-04-30 11:49:59 -07001618 if (failure_type != kNoCause && state->cause_code == kNoCause) {
1619 state->cause_code = failure_type;
1620 }
1621
Sami Tolvanen90221202014-12-09 16:39:47 +00001622 return StringValue(rc == 0 ? strdup("t") : strdup(""));
1623}
1624
1625// The transfer list is a text file containing commands to
1626// transfer data from one place to another on the target
1627// partition. We parse it and execute the commands in order:
1628//
1629// zero [rangeset]
1630// - fill the indicated blocks with zeros
1631//
1632// new [rangeset]
1633// - fill the blocks with data read from the new_data file
1634//
1635// erase [rangeset]
1636// - mark the given blocks as empty
1637//
1638// move <...>
1639// bsdiff <patchstart> <patchlen> <...>
1640// imgdiff <patchstart> <patchlen> <...>
1641// - read the source blocks, apply a patch (or not in the
1642// case of move), write result to target blocks. bsdiff or
1643// imgdiff specifies the type of patch; move means no patch
1644// at all.
1645//
1646// The format of <...> differs between versions 1 and 2;
1647// see the LoadSrcTgtVersion{1,2}() functions for a
1648// description of what's expected.
1649//
1650// stash <stash_id> <src_range>
1651// - (version 2+ only) load the given source range and stash
1652// the data in the given slot of the stash table.
1653//
Tao Baobaad2d42015-12-06 16:56:27 -08001654// free <stash_id>
1655// - (version 3+ only) free the given stash data.
1656//
Sami Tolvanen90221202014-12-09 16:39:47 +00001657// The creator of the transfer list will guarantee that no block
1658// is read (ie, used as the source for a patch or move) after it
1659// has been written.
1660//
1661// In version 2, the creator will guarantee that a given stash is
1662// loaded (with a stash command) before it's used in a
1663// move/bsdiff/imgdiff command.
1664//
1665// Within one command the source and target ranges may overlap so
1666// in general we need to read the entire source into memory before
1667// writing anything to the target blocks.
1668//
1669// All the patch data is concatenated into one patch_data file in
1670// the update package. It must be stored uncompressed because we
1671// memory-map it in directly from the archive. (Since patches are
1672// already compressed, we lose very little by not compressing
1673// their concatenation.)
1674//
1675// In version 3, commands that read data from the partition (i.e.
1676// move/bsdiff/imgdiff/stash) have one or more additional hashes
1677// before the range parameters, which are used to check if the
1678// command has already been completed and verify the integrity of
1679// the source data.
1680
1681Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao0940fe12015-08-27 16:41:21 -07001682 // Commands which are not tested are set to nullptr to skip them completely
Sami Tolvanen90221202014-12-09 16:39:47 +00001683 const Command commands[] = {
1684 { "bsdiff", PerformCommandDiff },
Tao Bao0940fe12015-08-27 16:41:21 -07001685 { "erase", nullptr },
Sami Tolvanen90221202014-12-09 16:39:47 +00001686 { "free", PerformCommandFree },
1687 { "imgdiff", PerformCommandDiff },
1688 { "move", PerformCommandMove },
Tao Bao0940fe12015-08-27 16:41:21 -07001689 { "new", nullptr },
Sami Tolvanen90221202014-12-09 16:39:47 +00001690 { "stash", PerformCommandStash },
Tao Bao0940fe12015-08-27 16:41:21 -07001691 { "zero", nullptr }
Sami Tolvanen90221202014-12-09 16:39:47 +00001692 };
1693
1694 // Perform a dry run without writing to test if an update can proceed
1695 return PerformBlockImageUpdate(name, state, argc, argv, commands,
Tao Baoe6aa3322015-08-05 15:20:27 -07001696 sizeof(commands) / sizeof(commands[0]), true);
Sami Tolvanen90221202014-12-09 16:39:47 +00001697}
1698
1699Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) {
1700 const Command commands[] = {
1701 { "bsdiff", PerformCommandDiff },
1702 { "erase", PerformCommandErase },
1703 { "free", PerformCommandFree },
1704 { "imgdiff", PerformCommandDiff },
1705 { "move", PerformCommandMove },
1706 { "new", PerformCommandNew },
1707 { "stash", PerformCommandStash },
1708 { "zero", PerformCommandZero }
1709 };
1710
1711 return PerformBlockImageUpdate(name, state, argc, argv, commands,
Tao Baoe6aa3322015-08-05 15:20:27 -07001712 sizeof(commands) / sizeof(commands[0]), false);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001713}
1714
Tao Bao0940fe12015-08-27 16:41:21 -07001715Value* RangeSha1Fn(const char* name, State* state, int /* argc */, Expr* argv[]) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001716 Value* blockdev_filename;
1717 Value* ranges;
Tao Bao0940fe12015-08-27 16:41:21 -07001718
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001719 if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001720 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001721 }
Tao Bao612336d2015-08-27 16:41:21 -07001722 std::unique_ptr<Value, decltype(&FreeValue)> ranges_holder(ranges, FreeValue);
1723 std::unique_ptr<Value, decltype(&FreeValue)> blockdev_filename_holder(blockdev_filename,
1724 FreeValue);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001725
1726 if (blockdev_filename->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001727 ErrorAbort(state, kArgsParsingFailure, "blockdev_filename argument to %s must be string",
1728 name);
Tao Bao612336d2015-08-27 16:41:21 -07001729 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001730 }
1731 if (ranges->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001732 ErrorAbort(state, kArgsParsingFailure, "ranges argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001733 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001734 }
1735
Tao Bao612336d2015-08-27 16:41:21 -07001736 int fd = open(blockdev_filename->data, O_RDWR);
1737 unique_fd fd_holder(fd);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001738 if (fd < 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001739 ErrorAbort(state, kFileOpenFailure, "open \"%s\" failed: %s", blockdev_filename->data,
1740 strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001741 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001742 }
1743
Tao Bao612336d2015-08-27 16:41:21 -07001744 RangeSet rs;
Tao Bao0940fe12015-08-27 16:41:21 -07001745 parse_range(ranges->data, rs);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001746
1747 SHA_CTX ctx;
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001748 SHA1_Init(&ctx);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001749
Tao Bao612336d2015-08-27 16:41:21 -07001750 std::vector<uint8_t> buffer(BLOCKSIZE);
Tao Bao0940fe12015-08-27 16:41:21 -07001751 for (size_t i = 0; i < rs.count; ++i) {
1752 if (!check_lseek(fd, (off64_t)rs.pos[i*2] * BLOCKSIZE, SEEK_SET)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001753 ErrorAbort(state, kLseekFailure, "failed to seek %s: %s", blockdev_filename->data,
1754 strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001755 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001756 }
1757
Tao Bao0940fe12015-08-27 16:41:21 -07001758 for (size_t j = rs.pos[i*2]; j < rs.pos[i*2+1]; ++j) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001759 if (read_all(fd, buffer, BLOCKSIZE) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001760 ErrorAbort(state, kFreadFailure, "failed to read %s: %s", blockdev_filename->data,
Tao Bao0940fe12015-08-27 16:41:21 -07001761 strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001762 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001763 }
1764
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001765 SHA1_Update(&ctx, buffer.data(), BLOCKSIZE);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001766 }
1767 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001768 uint8_t digest[SHA_DIGEST_LENGTH];
1769 SHA1_Final(digest, &ctx);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001770
Tao Bao612336d2015-08-27 16:41:21 -07001771 return StringValue(strdup(print_sha1(digest).c_str()));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001772}
1773
Tianjie Xu30bf4762015-12-15 11:47:30 -08001774// This function checks if a device has been remounted R/W prior to an incremental
1775// OTA update. This is an common cause of update abortion. The function reads the
1776// 1st block of each partition and check for mounting time/count. It return string "t"
1777// if executes successfully and an empty string otherwise.
1778
1779Value* CheckFirstBlockFn(const char* name, State* state, int argc, Expr* argv[]) {
1780 Value* arg_filename;
1781
1782 if (ReadValueArgs(state, argv, 1, &arg_filename) < 0) {
1783 return nullptr;
1784 }
1785 std::unique_ptr<Value, decltype(&FreeValue)> filename(arg_filename, FreeValue);
1786
1787 if (filename->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001788 ErrorAbort(state, kArgsParsingFailure, "filename argument to %s must be string", name);
Tianjie Xu30bf4762015-12-15 11:47:30 -08001789 return StringValue(strdup(""));
1790 }
1791
1792 int fd = open(arg_filename->data, O_RDONLY);
1793 unique_fd fd_holder(fd);
1794 if (fd == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001795 ErrorAbort(state, kFileOpenFailure, "open \"%s\" failed: %s", arg_filename->data,
1796 strerror(errno));
Tianjie Xu30bf4762015-12-15 11:47:30 -08001797 return StringValue(strdup(""));
1798 }
1799
1800 RangeSet blk0 {1 /*count*/, 1/*size*/, std::vector<size_t> {0, 1}/*position*/};
1801 std::vector<uint8_t> block0_buffer(BLOCKSIZE);
1802
1803 if (ReadBlocks(blk0, block0_buffer, fd) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001804 ErrorAbort(state, kFreadFailure, "failed to read %s: %s", arg_filename->data,
Tianjie Xu30bf4762015-12-15 11:47:30 -08001805 strerror(errno));
1806 return StringValue(strdup(""));
1807 }
1808
1809 // https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout
1810 // Super block starts from block 0, offset 0x400
1811 // 0x2C: len32 Mount time
1812 // 0x30: len32 Write time
1813 // 0x34: len16 Number of mounts since the last fsck
1814 // 0x38: len16 Magic signature 0xEF53
1815
1816 time_t mount_time = *reinterpret_cast<uint32_t*>(&block0_buffer[0x400+0x2C]);
1817 uint16_t mount_count = *reinterpret_cast<uint16_t*>(&block0_buffer[0x400+0x34]);
1818
1819 if (mount_count > 0) {
1820 uiPrintf(state, "Device was remounted R/W %d times\n", mount_count);
1821 uiPrintf(state, "Last remount happened on %s", ctime(&mount_time));
1822 }
1823
1824 return StringValue(strdup("t"));
1825}
1826
1827
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001828Value* BlockImageRecoverFn(const char* name, State* state, int argc, Expr* argv[]) {
1829 Value* arg_filename;
1830 Value* arg_ranges;
1831
1832 if (ReadValueArgs(state, argv, 2, &arg_filename, &arg_ranges) < 0) {
1833 return NULL;
1834 }
1835
1836 std::unique_ptr<Value, decltype(&FreeValue)> filename(arg_filename, FreeValue);
1837 std::unique_ptr<Value, decltype(&FreeValue)> ranges(arg_ranges, FreeValue);
1838
1839 if (filename->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001840 ErrorAbort(state, kArgsParsingFailure, "filename argument to %s must be string", name);
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001841 return StringValue(strdup(""));
1842 }
1843 if (ranges->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001844 ErrorAbort(state, kArgsParsingFailure, "ranges argument to %s must be string", name);
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001845 return StringValue(strdup(""));
1846 }
1847
Tianjie Xub686ba22015-12-09 15:29:45 -08001848 // Output notice to log when recover is attempted
1849 fprintf(stderr, "%s image corrupted, attempting to recover...\n", filename->data);
1850
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001851 // When opened with O_RDWR, libfec rewrites corrupted blocks when they are read
1852 fec::io fh(filename->data, O_RDWR);
1853
1854 if (!fh) {
Tianjie Xu16255832016-04-30 11:49:59 -07001855 ErrorAbort(state, kLibfecFailure, "fec_open \"%s\" failed: %s", filename->data,
1856 strerror(errno));
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001857 return StringValue(strdup(""));
1858 }
1859
1860 if (!fh.has_ecc() || !fh.has_verity()) {
Tianjie Xu16255832016-04-30 11:49:59 -07001861 ErrorAbort(state, kLibfecFailure, "unable to use metadata to correct errors");
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001862 return StringValue(strdup(""));
1863 }
1864
1865 fec_status status;
1866
1867 if (!fh.get_status(status)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001868 ErrorAbort(state, kLibfecFailure, "failed to read FEC status");
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001869 return StringValue(strdup(""));
1870 }
1871
1872 RangeSet rs;
1873 parse_range(ranges->data, rs);
1874
1875 uint8_t buffer[BLOCKSIZE];
1876
1877 for (size_t i = 0; i < rs.count; ++i) {
1878 for (size_t j = rs.pos[i * 2]; j < rs.pos[i * 2 + 1]; ++j) {
1879 // Stay within the data area, libfec validates and corrects metadata
1880 if (status.data_size <= (uint64_t)j * BLOCKSIZE) {
1881 continue;
1882 }
1883
1884 if (fh.pread(buffer, BLOCKSIZE, (off64_t)j * BLOCKSIZE) != BLOCKSIZE) {
Tianjie Xu16255832016-04-30 11:49:59 -07001885 ErrorAbort(state, kLibfecFailure, "failed to recover %s (block %zu): %s",
1886 filename->data, j, strerror(errno));
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001887 return StringValue(strdup(""));
1888 }
1889
1890 // If we want to be able to recover from a situation where rewriting a corrected
1891 // block doesn't guarantee the same data will be returned when re-read later, we
1892 // can save a copy of corrected blocks to /cache. Note:
1893 //
1894 // 1. Maximum space required from /cache is the same as the maximum number of
1895 // corrupted blocks we can correct. For RS(255, 253) and a 2 GiB partition,
1896 // this would be ~16 MiB, for example.
1897 //
1898 // 2. To find out if this block was corrupted, call fec_get_status after each
1899 // read and check if the errors field value has increased.
1900 }
1901 }
Tianjie Xub686ba22015-12-09 15:29:45 -08001902 fprintf(stderr, "...%s image recovered successfully.\n", filename->data);
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001903 return StringValue(strdup("t"));
1904}
1905
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001906void RegisterBlockImageFunctions() {
Sami Tolvanen90221202014-12-09 16:39:47 +00001907 RegisterFunction("block_image_verify", BlockImageVerifyFn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001908 RegisterFunction("block_image_update", BlockImageUpdateFn);
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001909 RegisterFunction("block_image_recover", BlockImageRecoverFn);
Tianjie Xu30bf4762015-12-15 11:47:30 -08001910 RegisterFunction("check_first_block", CheckFirstBlockFn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001911 RegisterFunction("range_sha1", RangeSha1Fn);
1912}