blob: 6111d0d1e3f5d9e264fc1ea967b88441ff40ce73 [file] [log] [blame]
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ctype.h>
18#include <errno.h>
Sami Tolvanen90221202014-12-09 16:39:47 +000019#include <dirent.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070020#include <fcntl.h>
21#include <inttypes.h>
Tao Baoba9a42a2015-06-23 23:23:33 -070022#include <linux/fs.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070023#include <pthread.h>
24#include <stdarg.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Sami Tolvanen90221202014-12-09 16:39:47 +000028#include <sys/stat.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070029#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/ioctl.h>
32#include <time.h>
33#include <unistd.h>
34
Tao Baoe6aa3322015-08-05 15:20:27 -070035#include <memory>
36#include <string>
Tao Bao0940fe12015-08-27 16:41:21 -070037#include <vector>
Tao Baoe6aa3322015-08-05 15:20:27 -070038
39#include <base/strings.h>
40
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070041#include "applypatch/applypatch.h"
42#include "edify/expr.h"
43#include "mincrypt/sha.h"
Sami Tolvanen90221202014-12-09 16:39:47 +000044#include "minzip/Hash.h"
Tao Baoe6aa3322015-08-05 15:20:27 -070045#include "print_sha1.h"
Tao Bao0940fe12015-08-27 16:41:21 -070046#include "unique_fd.h"
47#include "updater.h"
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070048
49#define BLOCKSIZE 4096
50
Sami Tolvanene82fa182015-06-10 15:58:12 +000051// Set this to 0 to interpret 'erase' transfers to mean do a
52// BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret
53// erase to mean fill the region with zeroes.
54#define DEBUG_ERASE 0
55
Sami Tolvanen90221202014-12-09 16:39:47 +000056#define STASH_DIRECTORY_BASE "/cache/recovery"
57#define STASH_DIRECTORY_MODE 0700
58#define STASH_FILE_MODE 0600
59
Tao Bao0940fe12015-08-27 16:41:21 -070060struct RangeSet {
61 size_t count; // Limit is INT_MAX.
Shrinivas Sahukara6153df2015-08-19 13:01:45 +053062 size_t size;
Tao Bao0940fe12015-08-27 16:41:21 -070063 std::vector<size_t> pos; // Actual limit is INT_MAX.
64};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070065
Tao Bao0940fe12015-08-27 16:41:21 -070066static void parse_range(const char* range_text, RangeSet& rs) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010067
Tao Bao0940fe12015-08-27 16:41:21 -070068 if (range_text == nullptr) {
69 fprintf(stderr, "failed to parse range: null range\n");
70 exit(1);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070071 }
Sami Tolvanenf2bac042015-05-12 12:48:46 +010072
Tao Bao0940fe12015-08-27 16:41:21 -070073 std::vector<std::string> pieces = android::base::Split(std::string(range_text), ",");
74 long int val;
Sami Tolvanenf2bac042015-05-12 12:48:46 +010075
Tao Bao0940fe12015-08-27 16:41:21 -070076 if (pieces.size() < 3) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010077 goto err;
78 }
79
Shrinivas Sahukara6153df2015-08-19 13:01:45 +053080 errno = 0;
Tao Bao0940fe12015-08-27 16:41:21 -070081 val = strtol(pieces[0].c_str(), nullptr, 0);
Sami Tolvanenf2bac042015-05-12 12:48:46 +010082
Tao Bao0940fe12015-08-27 16:41:21 -070083 if (errno != 0 || val < 2 || val > INT_MAX) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010084 goto err;
85 } else if (val % 2) {
86 goto err; // must be even
Tao Bao0940fe12015-08-27 16:41:21 -070087 } else if (val != static_cast<long int>(pieces.size() - 1)) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +010088 goto err;
89 }
90
Tao Bao0940fe12015-08-27 16:41:21 -070091 size_t num;
92 num = static_cast<size_t>(val);
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) {
99 const char* token = pieces[i+1].c_str();
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530100 errno = 0;
Tao Bao0940fe12015-08-27 16:41:21 -0700101 val = strtol(token, nullptr, 0);
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530102 if (errno != 0 || val < 0 || val > INT_MAX) {
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100103 goto err;
104 }
Tao Bao0940fe12015-08-27 16:41:21 -0700105 rs.pos[i] = static_cast<size_t>(val);
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100106
Tao Bao0940fe12015-08-27 16:41:21 -0700107 token = pieces[i+2].c_str();
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530108 errno = 0;
Tao Bao0940fe12015-08-27 16:41:21 -0700109 val = strtol(token, nullptr, 0);
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530110 if (errno != 0 || val < 0 || val > INT_MAX) {
111 goto err;
112 }
Tao Bao0940fe12015-08-27 16:41:21 -0700113 rs.pos[i+1] = static_cast<size_t>(val);
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530114
Tao Bao0940fe12015-08-27 16:41:21 -0700115 if (rs.pos[i] >= rs.pos[i+1]) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530116 goto err; // empty or negative range
117 }
118
Tao Bao0940fe12015-08-27 16:41:21 -0700119 size_t sz = rs.pos[i+1] - rs.pos[i];
120 if (rs.size > SIZE_MAX - sz) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530121 goto err; // overflow
122 }
123
Tao Bao0940fe12015-08-27 16:41:21 -0700124 rs.size += sz;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700125 }
126
Tao Bao0940fe12015-08-27 16:41:21 -0700127 return;
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100128
129err:
Tao Bao0940fe12015-08-27 16:41:21 -0700130 fprintf(stderr, "failed to parse range '%s'\n", range_text);
Sami Tolvanenf2bac042015-05-12 12:48:46 +0100131 exit(1);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700132}
133
Tao Baoe6aa3322015-08-05 15:20:27 -0700134static bool range_overlaps(const RangeSet& r1, const RangeSet& r2) {
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530135 for (size_t i = 0; i < r1.count; ++i) {
136 size_t r1_0 = r1.pos[i * 2];
137 size_t r1_1 = r1.pos[i * 2 + 1];
Sami Tolvanen90221202014-12-09 16:39:47 +0000138
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530139 for (size_t j = 0; j < r2.count; ++j) {
140 size_t r2_0 = r2.pos[j * 2];
141 size_t r2_1 = r2.pos[j * 2 + 1];
Sami Tolvanen90221202014-12-09 16:39:47 +0000142
Tao Baoc0f56ad2015-06-25 14:00:31 -0700143 if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700144 return true;
Sami Tolvanen90221202014-12-09 16:39:47 +0000145 }
146 }
147 }
148
Tao Baoe6aa3322015-08-05 15:20:27 -0700149 return false;
Sami Tolvanen90221202014-12-09 16:39:47 +0000150}
151
152static int read_all(int fd, uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700153 size_t so_far = 0;
154 while (so_far < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700155 ssize_t r = TEMP_FAILURE_RETRY(read(fd, data+so_far, size-so_far));
156 if (r == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700157 fprintf(stderr, "read failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000158 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700159 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700160 so_far += r;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700161 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000162 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700163}
164
Tao Bao612336d2015-08-27 16:41:21 -0700165static int read_all(int fd, std::vector<uint8_t>& buffer, size_t size) {
166 return read_all(fd, buffer.data(), size);
167}
168
Sami Tolvanen90221202014-12-09 16:39:47 +0000169static int write_all(int fd, const uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700170 size_t written = 0;
171 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700172 ssize_t w = TEMP_FAILURE_RETRY(write(fd, data+written, size-written));
173 if (w == -1) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700174 fprintf(stderr, "write failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000175 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700176 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700177 written += w;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700178 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000179
Sami Tolvanen90221202014-12-09 16:39:47 +0000180 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700181}
182
Tao Bao612336d2015-08-27 16:41:21 -0700183static int write_all(int fd, const std::vector<uint8_t>& buffer, size_t size) {
184 return write_all(fd, buffer.data(), size);
185}
186
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700187static bool check_lseek(int fd, off64_t offset, int whence) {
188 off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
189 if (rc == -1) {
190 fprintf(stderr, "lseek64 failed: %s\n", strerror(errno));
191 return false;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700192 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700193 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700194}
195
Tao Bao612336d2015-08-27 16:41:21 -0700196static void allocate(size_t size, std::vector<uint8_t>& buffer) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700197 // if the buffer's big enough, reuse it.
Tao Bao612336d2015-08-27 16:41:21 -0700198 if (size <= buffer.size()) return;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700199
Tao Bao612336d2015-08-27 16:41:21 -0700200 buffer.resize(size);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700201}
202
Tao Bao0940fe12015-08-27 16:41:21 -0700203struct RangeSinkState {
204 RangeSinkState(RangeSet& rs) : tgt(rs) { };
205
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700206 int fd;
Tao Bao0940fe12015-08-27 16:41:21 -0700207 const RangeSet& tgt;
Shrinivas Sahukara6153df2015-08-19 13:01:45 +0530208 size_t p_block;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700209 size_t p_remain;
Tao Bao0940fe12015-08-27 16:41:21 -0700210};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700211
212static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) {
Tao Bao0940fe12015-08-27 16:41:21 -0700213 RangeSinkState* rss = reinterpret_cast<RangeSinkState*>(token);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700214
Tao Bao0940fe12015-08-27 16:41:21 -0700215 if (rss->p_remain == 0) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700216 fprintf(stderr, "range sink write overrun");
Sami Tolvanen90221202014-12-09 16:39:47 +0000217 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700218 }
219
220 ssize_t written = 0;
221 while (size > 0) {
222 size_t write_now = size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000223
224 if (rss->p_remain < write_now) {
225 write_now = rss->p_remain;
226 }
227
228 if (write_all(rss->fd, data, write_now) == -1) {
229 break;
230 }
231
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700232 data += write_now;
233 size -= write_now;
234
235 rss->p_remain -= write_now;
236 written += write_now;
237
238 if (rss->p_remain == 0) {
239 // move to the next block
240 ++rss->p_block;
Tao Bao0940fe12015-08-27 16:41:21 -0700241 if (rss->p_block < rss->tgt.count) {
242 rss->p_remain = (rss->tgt.pos[rss->p_block * 2 + 1] -
243 rss->tgt.pos[rss->p_block * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000244
Tao Bao0940fe12015-08-27 16:41:21 -0700245 if (!check_lseek(rss->fd, (off64_t)rss->tgt.pos[rss->p_block*2] * BLOCKSIZE,
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700246 SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000247 break;
248 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700249 } else {
250 // we can't write any more; return how many bytes have
251 // been written so far.
Sami Tolvanen90221202014-12-09 16:39:47 +0000252 break;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700253 }
254 }
255 }
256
257 return written;
258}
259
260// All of the data for all the 'new' transfers is contained in one
261// file in the update package, concatenated together in the order in
262// which transfers.list will need it. We want to stream it out of the
263// archive (it's compressed) without writing it to a temp file, but we
264// can't write each section until it's that transfer's turn to go.
265//
266// To achieve this, we expand the new data from the archive in a
267// background thread, and block that threads 'receive uncompressed
268// data' function until the main thread has reached a point where we
269// want some new data to be written. We signal the background thread
270// with the destination for the data and block the main thread,
271// waiting for the background thread to complete writing that section.
272// Then it signals the main thread to wake up and goes back to
273// blocking waiting for a transfer.
274//
275// NewThreadInfo is the struct used to pass information back and forth
276// between the two threads. When the main thread wants some data
277// written, it sets rss to the destination location and signals the
278// condition. When the background thread is done writing, it clears
279// rss and signals the condition again.
280
Tao Bao0940fe12015-08-27 16:41:21 -0700281struct NewThreadInfo {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700282 ZipArchive* za;
283 const ZipEntry* entry;
284
285 RangeSinkState* rss;
286
287 pthread_mutex_t mu;
288 pthread_cond_t cv;
Tao Bao0940fe12015-08-27 16:41:21 -0700289};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700290
291static bool receive_new_data(const unsigned char* data, int size, void* cookie) {
Tao Bao0940fe12015-08-27 16:41:21 -0700292 NewThreadInfo* nti = reinterpret_cast<NewThreadInfo*>(cookie);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700293
294 while (size > 0) {
Tao Bao0940fe12015-08-27 16:41:21 -0700295 // Wait for nti->rss to be non-null, indicating some of this
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700296 // data is wanted.
297 pthread_mutex_lock(&nti->mu);
Tao Bao0940fe12015-08-27 16:41:21 -0700298 while (nti->rss == nullptr) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700299 pthread_cond_wait(&nti->cv, &nti->mu);
300 }
301 pthread_mutex_unlock(&nti->mu);
302
303 // At this point nti->rss is set, and we own it. The main
304 // thread is waiting for it to disappear from nti.
305 ssize_t written = RangeSinkWrite(data, size, nti->rss);
306 data += written;
307 size -= written;
308
Tao Bao0940fe12015-08-27 16:41:21 -0700309 if (nti->rss->p_block == nti->rss->tgt.count) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700310 // we have written all the bytes desired by this rss.
311
312 pthread_mutex_lock(&nti->mu);
Tao Bao0940fe12015-08-27 16:41:21 -0700313 nti->rss = nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700314 pthread_cond_broadcast(&nti->cv);
315 pthread_mutex_unlock(&nti->mu);
316 }
317 }
318
319 return true;
320}
321
322static void* unzip_new_data(void* cookie) {
323 NewThreadInfo* nti = (NewThreadInfo*) cookie;
324 mzProcessZipEntryContents(nti->za, nti->entry, receive_new_data, nti);
Tao Bao0940fe12015-08-27 16:41:21 -0700325 return nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700326}
327
Tao Bao612336d2015-08-27 16:41:21 -0700328static int ReadBlocks(const RangeSet& src, std::vector<uint8_t>& buffer, int fd) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000329 size_t p = 0;
Tao Bao612336d2015-08-27 16:41:21 -0700330 uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000331
Tao Bao0940fe12015-08-27 16:41:21 -0700332 for (size_t i = 0; i < src.count; ++i) {
333 if (!check_lseek(fd, (off64_t) src.pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000334 return -1;
335 }
336
Tao Bao0940fe12015-08-27 16:41:21 -0700337 size_t size = (src.pos[i * 2 + 1] - src.pos[i * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000338
Tao Bao612336d2015-08-27 16:41:21 -0700339 if (read_all(fd, data + p, size) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000340 return -1;
341 }
342
343 p += size;
344 }
345
346 return 0;
347}
348
Tao Bao612336d2015-08-27 16:41:21 -0700349static int WriteBlocks(const RangeSet& tgt, const std::vector<uint8_t>& buffer, int fd) {
350 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000351
Tao Bao0940fe12015-08-27 16:41:21 -0700352 size_t p = 0;
353 for (size_t i = 0; i < tgt.count; ++i) {
354 if (!check_lseek(fd, (off64_t) tgt.pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000355 return -1;
356 }
357
Tao Bao0940fe12015-08-27 16:41:21 -0700358 size_t size = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000359
Tao Bao612336d2015-08-27 16:41:21 -0700360 if (write_all(fd, data + p, size) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000361 return -1;
362 }
363
364 p += size;
365 }
366
367 return 0;
368}
369
Doug Zongker52ae67d2014-09-08 12:22:09 -0700370// Do a source/target load for move/bsdiff/imgdiff in version 1.
371// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
372// to parse the remainder of the string as:
373//
374// <src_range> <tgt_range>
375//
376// The source range is loaded into the provided buffer, reallocating
Tao Bao34847b22015-09-08 11:05:49 -0700377// it to make it larger if necessary.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700378
Tao Bao34847b22015-09-08 11:05:49 -0700379static int LoadSrcTgtVersion1(char** wordsave, RangeSet& tgt, size_t& src_blocks,
Tao Bao612336d2015-08-27 16:41:21 -0700380 std::vector<uint8_t>& buffer, int fd) {
381 // <src_range>
Tao Bao0940fe12015-08-27 16:41:21 -0700382 char* word = strtok_r(nullptr, " ", wordsave);
383 RangeSet src;
384 parse_range(word, src);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700385
Tao Bao612336d2015-08-27 16:41:21 -0700386 // <tgt_range>
Tao Bao34847b22015-09-08 11:05:49 -0700387 word = strtok_r(nullptr, " ", wordsave);
388 parse_range(word, tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700389
Tao Bao612336d2015-08-27 16:41:21 -0700390 allocate(src.size * BLOCKSIZE, buffer);
391 int rc = ReadBlocks(src, buffer, fd);
Tao Bao0940fe12015-08-27 16:41:21 -0700392 src_blocks = src.size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000393
Sami Tolvanen90221202014-12-09 16:39:47 +0000394 return rc;
395}
396
Tao Bao612336d2015-08-27 16:41:21 -0700397static int VerifyBlocks(const std::string& expected, const std::vector<uint8_t>& buffer,
Tao Bao0940fe12015-08-27 16:41:21 -0700398 const size_t blocks, bool printerror) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000399 uint8_t digest[SHA_DIGEST_SIZE];
Tao Bao612336d2015-08-27 16:41:21 -0700400 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000401
Tao Bao612336d2015-08-27 16:41:21 -0700402 SHA_hash(data, blocks * BLOCKSIZE, digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000403
Tao Baoe6aa3322015-08-05 15:20:27 -0700404 std::string hexdigest = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000405
Tao Bao0940fe12015-08-27 16:41:21 -0700406 if (hexdigest != expected) {
407 if (printerror) {
408 fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
409 expected.c_str(), hexdigest.c_str());
410 }
411 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000412 }
413
Tao Bao0940fe12015-08-27 16:41:21 -0700414 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000415}
416
Tao Bao0940fe12015-08-27 16:41:21 -0700417static std::string GetStashFileName(const std::string& base, const std::string& id,
418 const std::string& postfix) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700419 if (base.empty()) {
420 return "";
Sami Tolvanen90221202014-12-09 16:39:47 +0000421 }
422
Tao Baoe6aa3322015-08-05 15:20:27 -0700423 std::string fn(STASH_DIRECTORY_BASE);
424 fn += "/" + base + "/" + id + postfix;
Sami Tolvanen90221202014-12-09 16:39:47 +0000425
426 return fn;
427}
428
Tao Baoe6aa3322015-08-05 15:20:27 -0700429typedef void (*StashCallback)(const std::string&, void*);
Sami Tolvanen90221202014-12-09 16:39:47 +0000430
431// Does a best effort enumeration of stash files. Ignores possible non-file
432// items in the stash directory and continues despite of errors. Calls the
433// 'callback' function for each file and passes 'data' to the function as a
434// parameter.
435
Tao Baoe6aa3322015-08-05 15:20:27 -0700436static void EnumerateStash(const std::string& dirname, StashCallback callback, void* data) {
Tao Bao0940fe12015-08-27 16:41:21 -0700437 if (dirname.empty() || callback == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000438 return;
439 }
440
Tao Baoe6aa3322015-08-05 15:20:27 -0700441 std::unique_ptr<DIR, int(*)(DIR*)> directory(opendir(dirname.c_str()), closedir);
Sami Tolvanen90221202014-12-09 16:39:47 +0000442
Tao Bao0940fe12015-08-27 16:41:21 -0700443 if (directory == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000444 if (errno != ENOENT) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700445 fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000446 }
447 return;
448 }
449
Tao Baoe6aa3322015-08-05 15:20:27 -0700450 struct dirent* item;
Tao Bao0940fe12015-08-27 16:41:21 -0700451 while ((item = readdir(directory.get())) != nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000452 if (item->d_type != DT_REG) {
453 continue;
454 }
455
Tao Baoe6aa3322015-08-05 15:20:27 -0700456 std::string fn = dirname + "/" + std::string(item->d_name);
Sami Tolvanen90221202014-12-09 16:39:47 +0000457 callback(fn, data);
Sami Tolvanen90221202014-12-09 16:39:47 +0000458 }
459}
460
Tao Baoe6aa3322015-08-05 15:20:27 -0700461static void UpdateFileSize(const std::string& fn, void* data) {
462 if (fn.empty() || !data) {
463 return;
464 }
465
Tao Bao0940fe12015-08-27 16:41:21 -0700466 struct stat sb;
467 if (stat(fn.c_str(), &sb) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700468 fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000469 return;
470 }
471
Tao Baoe6aa3322015-08-05 15:20:27 -0700472 int* size = reinterpret_cast<int*>(data);
Tao Bao0940fe12015-08-27 16:41:21 -0700473 *size += sb.st_size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000474}
475
476// Deletes the stash directory and all files in it. Assumes that it only
477// contains files. There is nothing we can do about unlikely, but possible
478// errors, so they are merely logged.
479
Tao Bao0940fe12015-08-27 16:41:21 -0700480static void DeleteFile(const std::string& fn, void* /* data */) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700481 if (!fn.empty()) {
482 fprintf(stderr, "deleting %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000483
Tao Baoe6aa3322015-08-05 15:20:27 -0700484 if (unlink(fn.c_str()) == -1 && errno != ENOENT) {
485 fprintf(stderr, "unlink \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000486 }
487 }
488}
489
Tao Baoe6aa3322015-08-05 15:20:27 -0700490static void DeletePartial(const std::string& fn, void* data) {
491 if (android::base::EndsWith(fn, ".partial")) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000492 DeleteFile(fn, data);
493 }
494}
495
Tao Baoe6aa3322015-08-05 15:20:27 -0700496static void DeleteStash(const std::string& base) {
497 if (base.empty()) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000498 return;
499 }
500
Tao Baoe6aa3322015-08-05 15:20:27 -0700501 fprintf(stderr, "deleting stash %s\n", base.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000502
Tao Baoe6aa3322015-08-05 15:20:27 -0700503 std::string dirname = GetStashFileName(base, "", "");
Tao Bao0940fe12015-08-27 16:41:21 -0700504 EnumerateStash(dirname, DeleteFile, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000505
Tao Baoe6aa3322015-08-05 15:20:27 -0700506 if (rmdir(dirname.c_str()) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000507 if (errno != ENOENT && errno != ENOTDIR) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700508 fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000509 }
510 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000511}
512
Tao Bao0940fe12015-08-27 16:41:21 -0700513static int LoadStash(const std::string& base, const std::string& id, bool verify, size_t* blocks,
Tao Bao612336d2015-08-27 16:41:21 -0700514 std::vector<uint8_t>& buffer, bool printnoent) {
515 if (base.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -0700516 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000517 }
518
Tao Bao0940fe12015-08-27 16:41:21 -0700519 size_t blockcount = 0;
520
Sami Tolvanen90221202014-12-09 16:39:47 +0000521 if (!blocks) {
522 blocks = &blockcount;
523 }
524
Tao Bao0940fe12015-08-27 16:41:21 -0700525 std::string fn = GetStashFileName(base, id, "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000526
Tao Bao0940fe12015-08-27 16:41:21 -0700527 struct stat sb;
528 int res = stat(fn.c_str(), &sb);
Sami Tolvanen90221202014-12-09 16:39:47 +0000529
530 if (res == -1) {
531 if (errno != ENOENT || printnoent) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700532 fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +0000533 }
Tao Bao0940fe12015-08-27 16:41:21 -0700534 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000535 }
536
Tao Baoe6aa3322015-08-05 15:20:27 -0700537 fprintf(stderr, " loading %s\n", fn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000538
Tao Bao0940fe12015-08-27 16:41:21 -0700539 if ((sb.st_size % BLOCKSIZE) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700540 fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d",
Tao Bao0940fe12015-08-27 16:41:21 -0700541 fn.c_str(), static_cast<int64_t>(sb.st_size), BLOCKSIZE);
542 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000543 }
544
Tao Bao0940fe12015-08-27 16:41:21 -0700545 int fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY));
546 unique_fd fd_holder(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000547
548 if (fd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700549 fprintf(stderr, "open \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700550 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000551 }
552
Tao Bao612336d2015-08-27 16:41:21 -0700553 allocate(sb.st_size, buffer);
Sami Tolvanen90221202014-12-09 16:39:47 +0000554
Tao Bao612336d2015-08-27 16:41:21 -0700555 if (read_all(fd, buffer, sb.st_size) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700556 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000557 }
558
Tao Bao0940fe12015-08-27 16:41:21 -0700559 *blocks = sb.st_size / BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +0000560
Tao Bao612336d2015-08-27 16:41:21 -0700561 if (verify && VerifyBlocks(id, buffer, *blocks, true) != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700562 fprintf(stderr, "unexpected contents in %s\n", fn.c_str());
Tao Bao0940fe12015-08-27 16:41:21 -0700563 DeleteFile(fn, nullptr);
564 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000565 }
566
Tao Bao0940fe12015-08-27 16:41:21 -0700567 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000568}
569
Tao Bao612336d2015-08-27 16:41:21 -0700570static int WriteStash(const std::string& base, const std::string& id, int blocks,
571 std::vector<uint8_t>& buffer, bool checkspace, bool *exists) {
572 if (base.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -0700573 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000574 }
575
576 if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) {
577 fprintf(stderr, "not enough space to write stash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700578 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000579 }
580
Tao Bao0940fe12015-08-27 16:41:21 -0700581 std::string fn = GetStashFileName(base, id, ".partial");
582 std::string cn = GetStashFileName(base, id, "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000583
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100584 if (exists) {
Tao Bao0940fe12015-08-27 16:41:21 -0700585 struct stat sb;
586 int res = stat(cn.c_str(), &sb);
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100587
588 if (res == 0) {
589 // The file already exists and since the name is the hash of the contents,
590 // it's safe to assume the contents are identical (accidental hash collisions
591 // are unlikely)
Tao Baoe6aa3322015-08-05 15:20:27 -0700592 fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn.c_str());
Tao Bao0940fe12015-08-27 16:41:21 -0700593 *exists = true;
594 return 0;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100595 }
596
Tao Bao0940fe12015-08-27 16:41:21 -0700597 *exists = false;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100598 }
599
Tao Baoe6aa3322015-08-05 15:20:27 -0700600 fprintf(stderr, " writing %d blocks to %s\n", blocks, cn.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000601
Tao Bao0940fe12015-08-27 16:41:21 -0700602 int fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE));
603 unique_fd fd_holder(fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000604
605 if (fd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700606 fprintf(stderr, "failed to create \"%s\": %s\n", fn.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700607 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000608 }
609
610 if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700611 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000612 }
613
614 if (fsync(fd) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700615 fprintf(stderr, "fsync \"%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 Baoe6aa3322015-08-05 15:20:27 -0700619 if (rename(fn.c_str(), cn.c_str()) == -1) {
620 fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn.c_str(), cn.c_str(),
621 strerror(errno));
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 std::string dname = GetStashFileName(base, "", "");
626 int dfd = TEMP_FAILURE_RETRY(open(dname.c_str(), O_RDONLY | O_DIRECTORY));
627 unique_fd dfd_holder(dfd);
Tao Baodc392262015-07-31 15:56:44 -0700628
629 if (dfd == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700630 fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700631 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700632 }
633
634 if (fsync(dfd) == -1) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700635 fprintf(stderr, "fsync \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -0700636 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700637 }
638
Tao Bao0940fe12015-08-27 16:41:21 -0700639 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000640}
641
642// Creates a directory for storing stash files and checks if the /cache partition
643// hash enough space for the expected amount of blocks we need to store. Returns
644// >0 if we created the directory, zero if it existed already, and <0 of failure.
645
Tao Bao0940fe12015-08-27 16:41:21 -0700646static int CreateStash(State* state, int maxblocks, const char* blockdev, std::string& base) {
647 if (blockdev == nullptr) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700648 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000649 }
650
651 // Stash directory should be different for each partition to avoid conflicts
652 // when updating multiple partitions at the same time, so we use the hash of
653 // the block device name as the base directory
Tao Baoe6aa3322015-08-05 15:20:27 -0700654 SHA_CTX ctx;
Sami Tolvanen90221202014-12-09 16:39:47 +0000655 SHA_init(&ctx);
656 SHA_update(&ctx, blockdev, strlen(blockdev));
Tao Baoe6aa3322015-08-05 15:20:27 -0700657 const uint8_t* digest = SHA_final(&ctx);
658 base = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000659
Tao Baoe6aa3322015-08-05 15:20:27 -0700660 std::string dirname = GetStashFileName(base, "", "");
Tao Bao0940fe12015-08-27 16:41:21 -0700661 struct stat sb;
662 int res = stat(dirname.c_str(), &sb);
Sami Tolvanen90221202014-12-09 16:39:47 +0000663
664 if (res == -1 && errno != ENOENT) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700665 ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
666 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000667 } else if (res != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700668 fprintf(stderr, "creating stash %s\n", dirname.c_str());
669 res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000670
671 if (res != 0) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700672 ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
673 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000674 }
675
676 if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) {
677 ErrorAbort(state, "not enough space for stash\n");
Tao Baoe6aa3322015-08-05 15:20:27 -0700678 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000679 }
680
Tao Baoe6aa3322015-08-05 15:20:27 -0700681 return 1; // Created directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000682 }
683
Tao Baoe6aa3322015-08-05 15:20:27 -0700684 fprintf(stderr, "using existing stash %s\n", dirname.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000685
686 // If the directory already exists, calculate the space already allocated to
687 // stash files and check if there's enough for all required blocks. Delete any
688 // partially completed stash files first.
689
Tao Bao0940fe12015-08-27 16:41:21 -0700690 EnumerateStash(dirname, DeletePartial, nullptr);
Tao Baoe6aa3322015-08-05 15:20:27 -0700691 int size = 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000692 EnumerateStash(dirname, UpdateFileSize, &size);
693
Tao Bao0940fe12015-08-27 16:41:21 -0700694 size = maxblocks * BLOCKSIZE - size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000695
696 if (size > 0 && CacheSizeCheck(size) != 0) {
697 ErrorAbort(state, "not enough space for stash (%d more needed)\n", size);
Tao Baoe6aa3322015-08-05 15:20:27 -0700698 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000699 }
700
Tao Baoe6aa3322015-08-05 15:20:27 -0700701 return 0; // Using existing directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000702}
703
Tao Bao612336d2015-08-27 16:41:21 -0700704static int SaveStash(const std::string& base, char** wordsave, std::vector<uint8_t>& buffer,
705 int fd, bool usehash) {
706 if (!wordsave) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000707 return -1;
708 }
709
Tao Bao0940fe12015-08-27 16:41:21 -0700710 char *id_tok = strtok_r(nullptr, " ", wordsave);
711 if (id_tok == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000712 fprintf(stderr, "missing id field in stash command\n");
713 return -1;
714 }
Tao Bao0940fe12015-08-27 16:41:21 -0700715 std::string id(id_tok);
Sami Tolvanen90221202014-12-09 16:39:47 +0000716
Tao Bao0940fe12015-08-27 16:41:21 -0700717 size_t blocks = 0;
Tao Bao612336d2015-08-27 16:41:21 -0700718 if (usehash && LoadStash(base, id, true, &blocks, buffer, false) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000719 // Stash file already exists and has expected contents. Do not
720 // read from source again, as the source may have been already
721 // overwritten during a previous attempt.
722 return 0;
723 }
724
Tao Bao34847b22015-09-08 11:05:49 -0700725 char* word = strtok_r(nullptr, " ", wordsave);
726 RangeSet src;
727 parse_range(word, src);
728
Tao Bao612336d2015-08-27 16:41:21 -0700729 allocate(src.size * BLOCKSIZE, buffer);
730 if (ReadBlocks(src, buffer, fd) == -1) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000731 return -1;
732 }
Tao Bao34847b22015-09-08 11:05:49 -0700733 blocks = src.size;
Sami Tolvanen90221202014-12-09 16:39:47 +0000734
Tao Bao612336d2015-08-27 16:41:21 -0700735 if (usehash && VerifyBlocks(id, buffer, blocks, true) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000736 // Source blocks have unexpected contents. If we actually need this
737 // data later, this is an unrecoverable error. However, the command
738 // that uses the data may have already completed previously, so the
739 // possible failure will occur during source block verification.
Tao Bao0940fe12015-08-27 16:41:21 -0700740 fprintf(stderr, "failed to load source blocks for stash %s\n", id.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +0000741 return 0;
742 }
743
Tao Bao0940fe12015-08-27 16:41:21 -0700744 fprintf(stderr, "stashing %zu blocks to %s\n", blocks, id.c_str());
Tao Bao612336d2015-08-27 16:41:21 -0700745 return WriteStash(base, id, blocks, buffer, false, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000746}
747
Tao Baoe6aa3322015-08-05 15:20:27 -0700748static int FreeStash(const std::string& base, const char* id) {
Tao Bao0940fe12015-08-27 16:41:21 -0700749 if (base.empty() || id == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000750 return -1;
751 }
752
Tao Baoe6aa3322015-08-05 15:20:27 -0700753 std::string fn = GetStashFileName(base, std::string(id), "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000754
Tao Bao0940fe12015-08-27 16:41:21 -0700755 DeleteFile(fn, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +0000756
757 return 0;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700758}
759
Tao Bao612336d2015-08-27 16:41:21 -0700760static void MoveRange(std::vector<uint8_t>& dest, const RangeSet& locs,
761 const std::vector<uint8_t>& source) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700762 // source contains packed data, which we want to move to the
Tao Bao612336d2015-08-27 16:41:21 -0700763 // locations given in locs in the dest buffer. source and dest
Doug Zongker52ae67d2014-09-08 12:22:09 -0700764 // may be the same buffer.
765
Tao Bao612336d2015-08-27 16:41:21 -0700766 const uint8_t* from = source.data();
767 uint8_t* to = dest.data();
Tao Bao0940fe12015-08-27 16:41:21 -0700768 size_t start = locs.size;
769 for (int i = locs.count-1; i >= 0; --i) {
770 size_t blocks = locs.pos[i*2+1] - locs.pos[i*2];
Doug Zongker52ae67d2014-09-08 12:22:09 -0700771 start -= blocks;
Tao Bao612336d2015-08-27 16:41:21 -0700772 memmove(to + (locs.pos[i*2] * BLOCKSIZE), from + (start * BLOCKSIZE),
Doug Zongker52ae67d2014-09-08 12:22:09 -0700773 blocks * BLOCKSIZE);
774 }
775}
776
777// Do a source/target load for move/bsdiff/imgdiff in version 2.
778// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect
779// to parse the remainder of the string as one of:
780//
781// <tgt_range> <src_block_count> <src_range>
782// (loads data from source image only)
783//
784// <tgt_range> <src_block_count> - <[stash_id:stash_range] ...>
785// (loads data from stashes only)
786//
787// <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...>
788// (loads data from both source image and stashes)
789//
790// On return, buffer is filled with the loaded source data (rearranged
791// and combined with stashed data as necessary). buffer may be
792// reallocated if needed to accommodate the source data. *tgt is the
Sami Tolvanen90221202014-12-09 16:39:47 +0000793// target RangeSet. Any stashes required are loaded using LoadStash.
Doug Zongker52ae67d2014-09-08 12:22:09 -0700794
Tao Bao612336d2015-08-27 16:41:21 -0700795static int LoadSrcTgtVersion2(char** wordsave, RangeSet& tgt, size_t& src_blocks,
796 std::vector<uint8_t>& buffer, int fd, const std::string& stashbase, bool* overlap) {
797 // <tgt_range>
798 char* word = strtok_r(nullptr, " ", wordsave);
Tao Bao34847b22015-09-08 11:05:49 -0700799 parse_range(word, tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700800
Tao Bao612336d2015-08-27 16:41:21 -0700801 // <src_block_count>
Tao Bao0940fe12015-08-27 16:41:21 -0700802 word = strtok_r(nullptr, " ", wordsave);
803 src_blocks = strtol(word, nullptr, 0);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700804
Tao Bao612336d2015-08-27 16:41:21 -0700805 allocate(src_blocks * BLOCKSIZE, buffer);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700806
Tao Bao612336d2015-08-27 16:41:21 -0700807 // "-" or <src_range> [<src_loc>]
Tao Bao0940fe12015-08-27 16:41:21 -0700808 word = strtok_r(nullptr, " ", wordsave);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700809 if (word[0] == '-' && word[1] == '\0') {
810 // no source ranges, only stashes
811 } else {
Tao Bao0940fe12015-08-27 16:41:21 -0700812 RangeSet src;
813 parse_range(word, src);
Tao Bao612336d2015-08-27 16:41:21 -0700814 int res = ReadBlocks(src, buffer, fd);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700815
Tao Bao34847b22015-09-08 11:05:49 -0700816 if (overlap) {
817 *overlap = range_overlaps(src, tgt);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700818 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000819
Sami Tolvanen90221202014-12-09 16:39:47 +0000820 if (res == -1) {
821 return -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700822 }
823
Tao Bao0940fe12015-08-27 16:41:21 -0700824 word = strtok_r(nullptr, " ", wordsave);
825 if (word == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000826 // no stashes, only source range
827 return 0;
828 }
829
Tao Bao612336d2015-08-27 16:41:21 -0700830 RangeSet locs;
Tao Bao0940fe12015-08-27 16:41:21 -0700831 parse_range(word, locs);
Tao Bao612336d2015-08-27 16:41:21 -0700832 MoveRange(buffer, locs, buffer);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700833 }
834
Tao Bao612336d2015-08-27 16:41:21 -0700835 // <[stash_id:stash-range]>
836 char* colonsave;
Tao Bao0940fe12015-08-27 16:41:21 -0700837 while ((word = strtok_r(nullptr, " ", wordsave)) != nullptr) {
Doug Zongker52ae67d2014-09-08 12:22:09 -0700838 // Each word is a an index into the stash table, a colon, and
839 // then a rangeset describing where in the source block that
840 // stashed data should go.
Tao Bao0940fe12015-08-27 16:41:21 -0700841 colonsave = nullptr;
Tao Bao612336d2015-08-27 16:41:21 -0700842 char* colon = strtok_r(word, ":", &colonsave);
Sami Tolvanen90221202014-12-09 16:39:47 +0000843
Tao Bao612336d2015-08-27 16:41:21 -0700844 std::vector<uint8_t> stash;
845 int res = LoadStash(stashbase, std::string(colon), false, nullptr, stash, true);
Sami Tolvanen90221202014-12-09 16:39:47 +0000846
847 if (res == -1) {
848 // These source blocks will fail verification if used later, but we
849 // will let the caller decide if this is a fatal failure
850 fprintf(stderr, "failed to load stash %s\n", colon);
851 continue;
852 }
853
Tao Bao0940fe12015-08-27 16:41:21 -0700854 colon = strtok_r(nullptr, ":", &colonsave);
Tao Bao612336d2015-08-27 16:41:21 -0700855 RangeSet locs;
Tao Bao0940fe12015-08-27 16:41:21 -0700856 parse_range(colon, locs);
Sami Tolvanen90221202014-12-09 16:39:47 +0000857
Tao Bao612336d2015-08-27 16:41:21 -0700858 MoveRange(buffer, locs, stash);
Sami Tolvanen90221202014-12-09 16:39:47 +0000859 }
860
861 return 0;
862}
863
864// Parameters for transfer list command functions
Tao Bao0940fe12015-08-27 16:41:21 -0700865struct CommandParameters {
Sami Tolvanen90221202014-12-09 16:39:47 +0000866 char* cmdname;
867 char* cpos;
868 char* freestash;
Tao Baoe6aa3322015-08-05 15:20:27 -0700869 std::string stashbase;
870 bool canwrite;
Sami Tolvanen90221202014-12-09 16:39:47 +0000871 int createdstash;
872 int fd;
Tao Bao0940fe12015-08-27 16:41:21 -0700873 bool foundwrites;
Tao Baoe6aa3322015-08-05 15:20:27 -0700874 bool isunresumable;
Sami Tolvanen90221202014-12-09 16:39:47 +0000875 int version;
Tao Bao0940fe12015-08-27 16:41:21 -0700876 size_t written;
Sami Tolvanen90221202014-12-09 16:39:47 +0000877 NewThreadInfo nti;
878 pthread_t thread;
Tao Bao612336d2015-08-27 16:41:21 -0700879 std::vector<uint8_t> buffer;
Sami Tolvanen90221202014-12-09 16:39:47 +0000880 uint8_t* patch_start;
Tao Bao0940fe12015-08-27 16:41:21 -0700881};
Sami Tolvanen90221202014-12-09 16:39:47 +0000882
883// Do a source/target load for move/bsdiff/imgdiff in version 3.
884//
885// Parameters are the same as for LoadSrcTgtVersion2, except for 'onehash', which
886// tells the function whether to expect separate source and targe block hashes, or
887// if they are both the same and only one hash should be expected, and
888// 'isunresumable', which receives a non-zero value if block verification fails in
889// a way that the update cannot be resumed anymore.
890//
891// If the function is unable to load the necessary blocks or their contents don't
892// match the hashes, the return value is -1 and the command should be aborted.
893//
894// If the return value is 1, the command has already been completed according to
895// the contents of the target blocks, and should not be performed again.
896//
897// If the return value is 0, source blocks have expected content and the command
898// can be performed.
899
Tao Bao34847b22015-09-08 11:05:49 -0700900static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t& src_blocks,
Tao Bao0940fe12015-08-27 16:41:21 -0700901 bool onehash, bool& overlap) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000902
Tao Bao0940fe12015-08-27 16:41:21 -0700903 char* srchash = strtok_r(nullptr, " ", &params.cpos);
904 if (srchash == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000905 fprintf(stderr, "missing source hash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700906 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000907 }
908
Tao Bao0940fe12015-08-27 16:41:21 -0700909 char* tgthash = nullptr;
Sami Tolvanen90221202014-12-09 16:39:47 +0000910 if (onehash) {
911 tgthash = srchash;
912 } else {
Tao Bao0940fe12015-08-27 16:41:21 -0700913 tgthash = strtok_r(nullptr, " ", &params.cpos);
Sami Tolvanen90221202014-12-09 16:39:47 +0000914
Tao Bao0940fe12015-08-27 16:41:21 -0700915 if (tgthash == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000916 fprintf(stderr, "missing target hash\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700917 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000918 }
919 }
920
Tao Bao612336d2015-08-27 16:41:21 -0700921 if (LoadSrcTgtVersion2(&params.cpos, tgt, src_blocks, params.buffer, params.fd,
922 params.stashbase, &overlap) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700923 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000924 }
925
Tao Bao34847b22015-09-08 11:05:49 -0700926 std::vector<uint8_t> tgtbuffer(tgt.size * BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +0000927
Tao Bao612336d2015-08-27 16:41:21 -0700928 if (ReadBlocks(tgt, tgtbuffer, params.fd) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700929 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000930 }
931
Tao Bao612336d2015-08-27 16:41:21 -0700932 if (VerifyBlocks(tgthash, tgtbuffer, tgt.size, false) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000933 // Target blocks already have expected content, command should be skipped
Tao Bao0940fe12015-08-27 16:41:21 -0700934 return 1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000935 }
936
Tao Bao0940fe12015-08-27 16:41:21 -0700937 if (VerifyBlocks(srchash, params.buffer, src_blocks, true) == 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000938 // If source and target blocks overlap, stash the source blocks so we can
939 // resume from possible write errors
Tao Bao0940fe12015-08-27 16:41:21 -0700940 if (overlap) {
941 fprintf(stderr, "stashing %zu overlapping blocks to %s\n", src_blocks, srchash);
Sami Tolvanen90221202014-12-09 16:39:47 +0000942
Tao Bao0940fe12015-08-27 16:41:21 -0700943 bool stash_exists = false;
Tao Bao612336d2015-08-27 16:41:21 -0700944 if (WriteStash(params.stashbase, srchash, src_blocks, params.buffer, true,
Tao Bao0940fe12015-08-27 16:41:21 -0700945 &stash_exists) != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +0000946 fprintf(stderr, "failed to stash overlapping source blocks\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700947 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000948 }
949
950 // Can be deleted when the write has completed
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100951 if (!stash_exists) {
Tao Bao0940fe12015-08-27 16:41:21 -0700952 params.freestash = srchash;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100953 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000954 }
955
956 // Source blocks have expected content, command can proceed
Tao Bao0940fe12015-08-27 16:41:21 -0700957 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000958 }
959
Tao Bao612336d2015-08-27 16:41:21 -0700960 if (overlap && LoadStash(params.stashbase, srchash, true, nullptr, params.buffer, true) == 0) {
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100961 // Overlapping source blocks were previously stashed, command can proceed.
962 // We are recovering from an interrupted command, so we don't know if the
963 // stash can safely be deleted after this command.
Tao Bao0940fe12015-08-27 16:41:21 -0700964 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000965 }
966
967 // Valid source data not available, update cannot be resumed
968 fprintf(stderr, "partition has unexpected contents\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700969 params.isunresumable = true;
Sami Tolvanen90221202014-12-09 16:39:47 +0000970
Tao Bao0940fe12015-08-27 16:41:21 -0700971 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000972}
973
Tao Bao0940fe12015-08-27 16:41:21 -0700974static int PerformCommandMove(CommandParameters& params) {
975 size_t blocks = 0;
Tao Baoe6aa3322015-08-05 15:20:27 -0700976 bool overlap = false;
Sami Tolvanen90221202014-12-09 16:39:47 +0000977 int status = 0;
Tao Bao0940fe12015-08-27 16:41:21 -0700978 RangeSet tgt;
Sami Tolvanen90221202014-12-09 16:39:47 +0000979
Tao Bao0940fe12015-08-27 16:41:21 -0700980 if (params.version == 1) {
Tao Bao612336d2015-08-27 16:41:21 -0700981 status = LoadSrcTgtVersion1(&params.cpos, tgt, blocks, params.buffer, params.fd);
Tao Bao0940fe12015-08-27 16:41:21 -0700982 } else if (params.version == 2) {
Tao Bao612336d2015-08-27 16:41:21 -0700983 status = LoadSrcTgtVersion2(&params.cpos, tgt, blocks, params.buffer, params.fd,
984 params.stashbase, nullptr);
Tao Bao0940fe12015-08-27 16:41:21 -0700985 } else if (params.version >= 3) {
Tao Bao34847b22015-09-08 11:05:49 -0700986 status = LoadSrcTgtVersion3(params, tgt, blocks, true, overlap);
Sami Tolvanen90221202014-12-09 16:39:47 +0000987 }
988
989 if (status == -1) {
990 fprintf(stderr, "failed to read blocks for move\n");
Tao Bao0940fe12015-08-27 16:41:21 -0700991 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000992 }
993
994 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -0700995 params.foundwrites = true;
996 } else if (params.foundwrites) {
997 fprintf(stderr, "warning: commands executed out of order [%s]\n", params.cmdname);
Sami Tolvanen90221202014-12-09 16:39:47 +0000998 }
999
Tao Bao0940fe12015-08-27 16:41:21 -07001000 if (params.canwrite) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001001 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001002 fprintf(stderr, " moving %zu blocks\n", blocks);
Sami Tolvanen90221202014-12-09 16:39:47 +00001003
Tao Bao0940fe12015-08-27 16:41:21 -07001004 if (WriteBlocks(tgt, params.buffer, params.fd) == -1) {
1005 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001006 }
1007 } else {
Tao Bao0940fe12015-08-27 16:41:21 -07001008 fprintf(stderr, "skipping %zu already moved blocks\n", blocks);
Sami Tolvanen90221202014-12-09 16:39:47 +00001009 }
1010
1011 }
1012
Tao Bao0940fe12015-08-27 16:41:21 -07001013 if (params.freestash) {
1014 FreeStash(params.stashbase, params.freestash);
1015 params.freestash = nullptr;
Sami Tolvanen90221202014-12-09 16:39:47 +00001016 }
1017
Tao Bao0940fe12015-08-27 16:41:21 -07001018 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001019
Tao Bao0940fe12015-08-27 16:41:21 -07001020 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001021}
1022
Tao Bao0940fe12015-08-27 16:41:21 -07001023static int PerformCommandStash(CommandParameters& params) {
Tao Bao612336d2015-08-27 16:41:21 -07001024 return SaveStash(params.stashbase, &params.cpos, params.buffer, params.fd,
1025 (params.version >= 3));
Sami Tolvanen90221202014-12-09 16:39:47 +00001026}
1027
Tao Bao0940fe12015-08-27 16:41:21 -07001028static int PerformCommandFree(CommandParameters& params) {
1029 if (params.createdstash || params.canwrite) {
1030 return FreeStash(params.stashbase, params.cpos);
Sami Tolvanen90221202014-12-09 16:39:47 +00001031 }
1032
1033 return 0;
1034}
1035
Tao Bao0940fe12015-08-27 16:41:21 -07001036static int PerformCommandZero(CommandParameters& params) {
1037 char* range = strtok_r(nullptr, " ", &params.cpos);
Sami Tolvanen90221202014-12-09 16:39:47 +00001038
Tao Bao0940fe12015-08-27 16:41:21 -07001039 if (range == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001040 fprintf(stderr, "missing target blocks for zero\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001041 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001042 }
1043
Tao Bao0940fe12015-08-27 16:41:21 -07001044 RangeSet tgt;
1045 parse_range(range, tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001046
Tao Bao0940fe12015-08-27 16:41:21 -07001047 fprintf(stderr, " zeroing %zu blocks\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001048
Tao Bao612336d2015-08-27 16:41:21 -07001049 allocate(BLOCKSIZE, params.buffer);
1050 memset(params.buffer.data(), 0, BLOCKSIZE);
Sami Tolvanen90221202014-12-09 16:39:47 +00001051
Tao Bao0940fe12015-08-27 16:41:21 -07001052 if (params.canwrite) {
1053 for (size_t i = 0; i < tgt.count; ++i) {
1054 if (!check_lseek(params.fd, (off64_t) tgt.pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
1055 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001056 }
1057
Tao Bao0940fe12015-08-27 16:41:21 -07001058 for (size_t j = tgt.pos[i * 2]; j < tgt.pos[i * 2 + 1]; ++j) {
1059 if (write_all(params.fd, params.buffer, BLOCKSIZE) == -1) {
1060 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001061 }
1062 }
1063 }
1064 }
1065
Tao Bao0940fe12015-08-27 16:41:21 -07001066 if (params.cmdname[0] == 'z') {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001067 // Update only for the zero command, as the erase command will call
1068 // this if DEBUG_ERASE is defined.
Tao Bao0940fe12015-08-27 16:41:21 -07001069 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001070 }
1071
Tao Bao0940fe12015-08-27 16:41:21 -07001072 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001073}
1074
Tao Bao0940fe12015-08-27 16:41:21 -07001075static int PerformCommandNew(CommandParameters& params) {
1076 char* range = strtok_r(nullptr, " ", &params.cpos);
Sami Tolvanen90221202014-12-09 16:39:47 +00001077
Tao Bao0940fe12015-08-27 16:41:21 -07001078 if (range == nullptr) {
1079 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001080 }
1081
Tao Bao0940fe12015-08-27 16:41:21 -07001082 RangeSet tgt;
1083 parse_range(range, tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001084
Tao Bao0940fe12015-08-27 16:41:21 -07001085 if (params.canwrite) {
1086 fprintf(stderr, " writing %zu blocks of new data\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001087
Tao Bao0940fe12015-08-27 16:41:21 -07001088 RangeSinkState rss(tgt);
1089 rss.fd = params.fd;
Sami Tolvanen90221202014-12-09 16:39:47 +00001090 rss.p_block = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001091 rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001092
Tao Bao0940fe12015-08-27 16:41:21 -07001093 if (!check_lseek(params.fd, (off64_t) tgt.pos[0] * BLOCKSIZE, SEEK_SET)) {
1094 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001095 }
1096
Tao Bao0940fe12015-08-27 16:41:21 -07001097 pthread_mutex_lock(&params.nti.mu);
1098 params.nti.rss = &rss;
1099 pthread_cond_broadcast(&params.nti.cv);
Sami Tolvanen90221202014-12-09 16:39:47 +00001100
Tao Bao0940fe12015-08-27 16:41:21 -07001101 while (params.nti.rss) {
1102 pthread_cond_wait(&params.nti.cv, &params.nti.mu);
Sami Tolvanen90221202014-12-09 16:39:47 +00001103 }
1104
Tao Bao0940fe12015-08-27 16:41:21 -07001105 pthread_mutex_unlock(&params.nti.mu);
Sami Tolvanen90221202014-12-09 16:39:47 +00001106 }
1107
Tao Bao0940fe12015-08-27 16:41:21 -07001108 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001109
Tao Bao0940fe12015-08-27 16:41:21 -07001110 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001111}
1112
Tao Bao0940fe12015-08-27 16:41:21 -07001113static int PerformCommandDiff(CommandParameters& params) {
Tao Bao0940fe12015-08-27 16:41:21 -07001114
1115 const std::string logparams(params.cpos);
Tao Bao612336d2015-08-27 16:41:21 -07001116 char* value = strtok_r(nullptr, " ", &params.cpos);
Tao Bao0940fe12015-08-27 16:41:21 -07001117
1118 if (value == nullptr) {
1119 fprintf(stderr, "missing patch offset for %s\n", params.cmdname);
1120 return -1;
1121 }
1122
1123 size_t offset = strtoul(value, nullptr, 0);
1124
1125 value = strtok_r(nullptr, " ", &params.cpos);
1126
1127 if (value == nullptr) {
1128 fprintf(stderr, "missing patch length for %s\n", params.cmdname);
1129 return -1;
1130 }
1131
1132 size_t len = strtoul(value, nullptr, 0);
1133
Tao Bao612336d2015-08-27 16:41:21 -07001134 RangeSet tgt;
Tao Bao0940fe12015-08-27 16:41:21 -07001135 size_t blocks = 0;
Tao Bao612336d2015-08-27 16:41:21 -07001136 bool overlap = false;
Sami Tolvanen90221202014-12-09 16:39:47 +00001137 int status = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001138 if (params.version == 1) {
Tao Bao612336d2015-08-27 16:41:21 -07001139 status = LoadSrcTgtVersion1(&params.cpos, tgt, blocks, params.buffer, params.fd);
Tao Bao0940fe12015-08-27 16:41:21 -07001140 } else if (params.version == 2) {
Tao Bao612336d2015-08-27 16:41:21 -07001141 status = LoadSrcTgtVersion2(&params.cpos, tgt, blocks, params.buffer, params.fd,
1142 params.stashbase, nullptr);
Tao Bao0940fe12015-08-27 16:41:21 -07001143 } else if (params.version >= 3) {
Tao Bao34847b22015-09-08 11:05:49 -07001144 status = LoadSrcTgtVersion3(params, tgt, blocks, false, overlap);
Sami Tolvanen90221202014-12-09 16:39:47 +00001145 }
1146
1147 if (status == -1) {
1148 fprintf(stderr, "failed to read blocks for diff\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001149 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001150 }
1151
1152 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001153 params.foundwrites = true;
1154 } else if (params.foundwrites) {
1155 fprintf(stderr, "warning: commands executed out of order [%s]\n", params.cmdname);
Sami Tolvanen90221202014-12-09 16:39:47 +00001156 }
1157
Tao Bao0940fe12015-08-27 16:41:21 -07001158 if (params.canwrite) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001159 if (status == 0) {
Tao Bao0940fe12015-08-27 16:41:21 -07001160 fprintf(stderr, "patching %zu blocks to %zu\n", blocks, tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001161
Tao Bao0940fe12015-08-27 16:41:21 -07001162 Value patch_value;
Sami Tolvanen90221202014-12-09 16:39:47 +00001163 patch_value.type = VAL_BLOB;
1164 patch_value.size = len;
Tao Bao0940fe12015-08-27 16:41:21 -07001165 patch_value.data = (char*) (params.patch_start + offset);
Sami Tolvanen90221202014-12-09 16:39:47 +00001166
Tao Bao0940fe12015-08-27 16:41:21 -07001167 RangeSinkState rss(tgt);
1168 rss.fd = params.fd;
Sami Tolvanen90221202014-12-09 16:39:47 +00001169 rss.p_block = 0;
Tao Bao0940fe12015-08-27 16:41:21 -07001170 rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001171
Tao Bao0940fe12015-08-27 16:41:21 -07001172 if (!check_lseek(params.fd, (off64_t) tgt.pos[0] * BLOCKSIZE, SEEK_SET)) {
1173 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001174 }
1175
Tao Bao0940fe12015-08-27 16:41:21 -07001176 if (params.cmdname[0] == 'i') { // imgdiff
Tao Bao612336d2015-08-27 16:41:21 -07001177 ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value,
Tao Bao0940fe12015-08-27 16:41:21 -07001178 &RangeSinkWrite, &rss, nullptr, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +00001179 } else {
Tao Bao612336d2015-08-27 16:41:21 -07001180 ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value, 0,
1181 &RangeSinkWrite, &rss, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +00001182 }
1183
1184 // We expect the output of the patcher to fill the tgt ranges exactly.
Tao Bao0940fe12015-08-27 16:41:21 -07001185 if (rss.p_block != tgt.count || rss.p_remain != 0) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001186 fprintf(stderr, "range sink underrun?\n");
1187 }
1188 } else {
Tao Bao0940fe12015-08-27 16:41:21 -07001189 fprintf(stderr, "skipping %zu blocks already patched to %zu [%s]\n",
1190 blocks, tgt.size, logparams.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +00001191 }
1192 }
1193
Tao Bao0940fe12015-08-27 16:41:21 -07001194 if (params.freestash) {
1195 FreeStash(params.stashbase, params.freestash);
1196 params.freestash = nullptr;
Sami Tolvanen90221202014-12-09 16:39:47 +00001197 }
1198
Tao Bao0940fe12015-08-27 16:41:21 -07001199 params.written += tgt.size;
Sami Tolvanen90221202014-12-09 16:39:47 +00001200
Tao Bao0940fe12015-08-27 16:41:21 -07001201 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001202}
1203
Tao Bao0940fe12015-08-27 16:41:21 -07001204static int PerformCommandErase(CommandParameters& params) {
Sami Tolvanene82fa182015-06-10 15:58:12 +00001205 if (DEBUG_ERASE) {
1206 return PerformCommandZero(params);
Sami Tolvanen90221202014-12-09 16:39:47 +00001207 }
1208
Tao Bao0940fe12015-08-27 16:41:21 -07001209 struct stat sb;
1210 if (fstat(params.fd, &sb) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001211 fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -07001212 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001213 }
1214
Tao Bao0940fe12015-08-27 16:41:21 -07001215 if (!S_ISBLK(sb.st_mode)) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001216 fprintf(stderr, "not a block device; skipping erase\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001217 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001218 }
1219
Tao Bao0940fe12015-08-27 16:41:21 -07001220 char* range = strtok_r(nullptr, " ", &params.cpos);
Sami Tolvanen90221202014-12-09 16:39:47 +00001221
Tao Bao0940fe12015-08-27 16:41:21 -07001222 if (range == nullptr) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001223 fprintf(stderr, "missing target blocks for erase\n");
Tao Bao0940fe12015-08-27 16:41:21 -07001224 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001225 }
1226
Tao Bao0940fe12015-08-27 16:41:21 -07001227 RangeSet tgt;
1228 parse_range(range, tgt);
Sami Tolvanen90221202014-12-09 16:39:47 +00001229
Tao Bao0940fe12015-08-27 16:41:21 -07001230 if (params.canwrite) {
1231 fprintf(stderr, " erasing %zu blocks\n", tgt.size);
Sami Tolvanen90221202014-12-09 16:39:47 +00001232
Tao Bao0940fe12015-08-27 16:41:21 -07001233 for (size_t i = 0; i < tgt.count; ++i) {
1234 uint64_t blocks[2];
Sami Tolvanen90221202014-12-09 16:39:47 +00001235 // offset in bytes
Tao Bao0940fe12015-08-27 16:41:21 -07001236 blocks[0] = tgt.pos[i * 2] * (uint64_t) BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001237 // length in bytes
Tao Bao0940fe12015-08-27 16:41:21 -07001238 blocks[1] = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * (uint64_t) BLOCKSIZE;
Sami Tolvanen90221202014-12-09 16:39:47 +00001239
Tao Bao0940fe12015-08-27 16:41:21 -07001240 if (ioctl(params.fd, BLKDISCARD, &blocks) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001241 fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno));
Tao Bao0940fe12015-08-27 16:41:21 -07001242 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001243 }
1244 }
1245 }
1246
Tao Bao0940fe12015-08-27 16:41:21 -07001247 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001248}
1249
1250// Definitions for transfer list command functions
Tao Bao0940fe12015-08-27 16:41:21 -07001251typedef int (*CommandFunction)(CommandParameters&);
Sami Tolvanen90221202014-12-09 16:39:47 +00001252
Tao Bao612336d2015-08-27 16:41:21 -07001253struct Command {
Sami Tolvanen90221202014-12-09 16:39:47 +00001254 const char* name;
1255 CommandFunction f;
Tao Bao612336d2015-08-27 16:41:21 -07001256};
Sami Tolvanen90221202014-12-09 16:39:47 +00001257
1258// CompareCommands and CompareCommandNames are for the hash table
1259
1260static int CompareCommands(const void* c1, const void* c2) {
1261 return strcmp(((const Command*) c1)->name, ((const Command*) c2)->name);
1262}
1263
1264static int CompareCommandNames(const void* c1, const void* c2) {
1265 return strcmp(((const Command*) c1)->name, (const char*) c2);
1266}
1267
1268// HashString is used to hash command names for the hash table
1269
1270static unsigned int HashString(const char *s) {
1271 unsigned int hash = 0;
1272 if (s) {
1273 while (*s) {
1274 hash = hash * 33 + *s++;
1275 }
1276 }
1277 return hash;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001278}
1279
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001280// args:
1281// - block device (or file) to modify in-place
1282// - transfer list (blob)
1283// - new data stream (filename within package.zip)
1284// - patch stream (filename within package.zip, must be uncompressed)
1285
Tao Bao0940fe12015-08-27 16:41:21 -07001286static Value* PerformBlockImageUpdate(const char* name, State* state, int /* argc */, Expr* argv[],
1287 const Command* commands, size_t cmdcount, bool dryrun) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001288
Sami Tolvanen90221202014-12-09 16:39:47 +00001289 CommandParameters params;
Sami Tolvanen90221202014-12-09 16:39:47 +00001290 memset(&params, 0, sizeof(params));
1291 params.canwrite = !dryrun;
1292
1293 fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update");
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001294
Tao Bao612336d2015-08-27 16:41:21 -07001295 Value* blockdev_filename = nullptr;
1296 Value* transfer_list_value = nullptr;
1297 Value* new_data_fn = nullptr;
1298 Value* patch_data_fn = nullptr;
Doug Zongker1d5d6092014-08-21 10:47:24 -07001299 if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value,
Sami Tolvanen90221202014-12-09 16:39:47 +00001300 &new_data_fn, &patch_data_fn) < 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001301 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001302 }
Tao Bao612336d2015-08-27 16:41:21 -07001303 std::unique_ptr<Value, decltype(&FreeValue)> blockdev_filename_holder(blockdev_filename,
1304 FreeValue);
1305 std::unique_ptr<Value, decltype(&FreeValue)> transfer_list_value_holder(transfer_list_value,
1306 FreeValue);
1307 std::unique_ptr<Value, decltype(&FreeValue)> new_data_fn_holder(new_data_fn, FreeValue);
1308 std::unique_ptr<Value, decltype(&FreeValue)> patch_data_fn_holder(patch_data_fn, FreeValue);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001309
1310 if (blockdev_filename->type != VAL_STRING) {
1311 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001312 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001313 }
Doug Zongker1d5d6092014-08-21 10:47:24 -07001314 if (transfer_list_value->type != VAL_BLOB) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001315 ErrorAbort(state, "transfer_list argument to %s must be blob", name);
Tao Bao612336d2015-08-27 16:41:21 -07001316 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001317 }
1318 if (new_data_fn->type != VAL_STRING) {
1319 ErrorAbort(state, "new_data_fn argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001320 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001321 }
1322 if (patch_data_fn->type != VAL_STRING) {
1323 ErrorAbort(state, "patch_data_fn argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001324 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001325 }
1326
Tao Bao612336d2015-08-27 16:41:21 -07001327 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001328
Tao Bao0940fe12015-08-27 16:41:21 -07001329 if (ui == nullptr) {
Tao Bao612336d2015-08-27 16:41:21 -07001330 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001331 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001332
Tao Bao612336d2015-08-27 16:41:21 -07001333 FILE* cmd_pipe = ui->cmd_pipe;
1334 ZipArchive* za = ui->package_zip;
Sami Tolvanen90221202014-12-09 16:39:47 +00001335
Tao Bao0940fe12015-08-27 16:41:21 -07001336 if (cmd_pipe == nullptr || za == nullptr) {
Tao Bao612336d2015-08-27 16:41:21 -07001337 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001338 }
1339
Tao Bao612336d2015-08-27 16:41:21 -07001340 const ZipEntry* patch_entry = mzFindZipEntry(za, patch_data_fn->data);
Tao Bao0940fe12015-08-27 16:41:21 -07001341 if (patch_entry == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001342 fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data);
Tao Bao612336d2015-08-27 16:41:21 -07001343 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001344 }
1345
Sami Tolvanen90221202014-12-09 16:39:47 +00001346 params.patch_start = ui->package_zip_addr + mzGetZipEntryOffset(patch_entry);
Tao Bao612336d2015-08-27 16:41:21 -07001347 const ZipEntry* new_entry = mzFindZipEntry(za, new_data_fn->data);
Tao Bao0940fe12015-08-27 16:41:21 -07001348 if (new_entry == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001349 fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data);
Tao Bao612336d2015-08-27 16:41:21 -07001350 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001351 }
1352
Sami Tolvanen90221202014-12-09 16:39:47 +00001353 params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR));
Tao Bao612336d2015-08-27 16:41:21 -07001354 unique_fd fd_holder(params.fd);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001355
Sami Tolvanen90221202014-12-09 16:39:47 +00001356 if (params.fd == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001357 fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data, strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001358 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001359 }
1360
Sami Tolvanen90221202014-12-09 16:39:47 +00001361 if (params.canwrite) {
1362 params.nti.za = za;
1363 params.nti.entry = new_entry;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001364
Tao Bao0940fe12015-08-27 16:41:21 -07001365 pthread_mutex_init(&params.nti.mu, nullptr);
1366 pthread_cond_init(&params.nti.cv, nullptr);
Tao Bao612336d2015-08-27 16:41:21 -07001367 pthread_attr_t attr;
Sami Tolvanen90221202014-12-09 16:39:47 +00001368 pthread_attr_init(&attr);
1369 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1370
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001371 int error = pthread_create(&params.thread, &attr, unzip_new_data, &params.nti);
1372 if (error != 0) {
1373 fprintf(stderr, "pthread_create failed: %s\n", strerror(error));
Tao Bao612336d2015-08-27 16:41:21 -07001374 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001375 }
1376 }
1377
1378 // The data in transfer_list_value is not necessarily null-terminated, so we need
1379 // to copy it to a new buffer and add the null that strtok_r will need.
Tao Bao612336d2015-08-27 16:41:21 -07001380 const std::string transfer_list(transfer_list_value->data, transfer_list_value->size);
1381 std::vector<std::string> lines = android::base::Split(transfer_list, "\n");
Doug Zongker1d5d6092014-08-21 10:47:24 -07001382
Sami Tolvanen90221202014-12-09 16:39:47 +00001383 // First line in transfer list is the version number
Tao Bao612336d2015-08-27 16:41:21 -07001384 long int val;
1385 errno = 0;
1386 val = strtol(lines[0].c_str(), nullptr, 0);
1387 if (errno != 0 || val < 1 || val > 3) {
1388 fprintf(stderr, "unexpected transfer list version [%s]\n", lines[0].c_str());
1389 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001390 }
Tao Bao612336d2015-08-27 16:41:21 -07001391 params.version = static_cast<int>(val);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001392
Sami Tolvanen90221202014-12-09 16:39:47 +00001393 fprintf(stderr, "blockimg version is %d\n", params.version);
1394
1395 // Second line in transfer list is the total number of blocks we expect to write
Tao Bao612336d2015-08-27 16:41:21 -07001396 int total_blocks = strtol(lines[1].c_str(), nullptr, 0);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001397
Sami Tolvanen90221202014-12-09 16:39:47 +00001398 if (total_blocks < 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001399 ErrorAbort(state, "unexpected block count [%s]\n", lines[1].c_str());
1400 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001401 } else if (total_blocks == 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001402 return StringValue(strdup("t"));
Sami Tolvanen90221202014-12-09 16:39:47 +00001403 }
1404
Tao Bao612336d2015-08-27 16:41:21 -07001405 size_t start = 2;
Sami Tolvanen90221202014-12-09 16:39:47 +00001406 if (params.version >= 2) {
1407 // Third line is how many stash entries are needed simultaneously
Tao Bao612336d2015-08-27 16:41:21 -07001408 fprintf(stderr, "maximum stash entries %s\n", lines[2].c_str());
Doug Zongker52ae67d2014-09-08 12:22:09 -07001409
Sami Tolvanen90221202014-12-09 16:39:47 +00001410 // Fourth line is the maximum number of blocks that will be stashed simultaneously
Tao Bao612336d2015-08-27 16:41:21 -07001411 int stash_max_blocks = strtol(lines[3].c_str(), nullptr, 0);
Sami Tolvanen90221202014-12-09 16:39:47 +00001412
1413 if (stash_max_blocks < 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001414 ErrorAbort(state, "unexpected maximum stash blocks [%s]\n", lines[3].c_str());
1415 return StringValue(strdup(""));
Doug Zongker52ae67d2014-09-08 12:22:09 -07001416 }
1417
Jesse Zhao1df64d32015-02-17 17:09:23 -08001418 if (stash_max_blocks >= 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001419 int res = CreateStash(state, stash_max_blocks, blockdev_filename->data,
1420 params.stashbase);
Sami Tolvanen90221202014-12-09 16:39:47 +00001421
1422 if (res == -1) {
Tao Bao612336d2015-08-27 16:41:21 -07001423 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001424 }
1425
1426 params.createdstash = res;
1427 }
Tao Bao612336d2015-08-27 16:41:21 -07001428
1429 start += 2;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001430 }
1431
Sami Tolvanen90221202014-12-09 16:39:47 +00001432 // Build a hash table of the available commands
Tao Bao612336d2015-08-27 16:41:21 -07001433 HashTable* cmdht = mzHashTableCreate(cmdcount, nullptr);
1434 std::unique_ptr<HashTable, decltype(&mzHashTableFree)> cmdht_holder(cmdht, mzHashTableFree);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001435
Tao Bao0940fe12015-08-27 16:41:21 -07001436 for (size_t i = 0; i < cmdcount; ++i) {
1437 unsigned int cmdhash = HashString(commands[i].name);
Sami Tolvanen90221202014-12-09 16:39:47 +00001438 mzHashTableLookup(cmdht, cmdhash, (void*) &commands[i], CompareCommands, true);
1439 }
1440
Tao Bao612336d2015-08-27 16:41:21 -07001441 int rc = -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -07001442
Tao Bao612336d2015-08-27 16:41:21 -07001443 // Subsequent lines are all individual transfer commands
1444 for (auto it = lines.cbegin() + start; it != lines.cend(); it++) {
1445 const std::string& line_str(*it);
1446 char* line = strdup(line_str.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +00001447 params.cmdname = strtok_r(line, " ", &params.cpos);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001448
Tao Bao0940fe12015-08-27 16:41:21 -07001449 if (params.cmdname == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001450 fprintf(stderr, "missing command [%s]\n", line);
1451 goto pbiudone;
1452 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001453
Tao Bao0940fe12015-08-27 16:41:21 -07001454 unsigned int cmdhash = HashString(params.cmdname);
Tao Bao612336d2015-08-27 16:41:21 -07001455 const Command* cmd = reinterpret_cast<const Command*>(mzHashTableLookup(cmdht, cmdhash,
1456 params.cmdname, CompareCommandNames, false));
Doug Zongker52ae67d2014-09-08 12:22:09 -07001457
Tao Bao0940fe12015-08-27 16:41:21 -07001458 if (cmd == nullptr) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001459 fprintf(stderr, "unexpected command [%s]\n", params.cmdname);
1460 goto pbiudone;
1461 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001462
Tao Bao0940fe12015-08-27 16:41:21 -07001463 if (cmd->f != nullptr && cmd->f(params) == -1) {
Tao Bao612336d2015-08-27 16:41:21 -07001464 fprintf(stderr, "failed to execute command [%s]\n", line_str.c_str());
Sami Tolvanen90221202014-12-09 16:39:47 +00001465 goto pbiudone;
1466 }
1467
Sami Tolvanen90221202014-12-09 16:39:47 +00001468 if (params.canwrite) {
Tao Bao187efff2015-07-27 14:07:08 -07001469 if (fsync(params.fd) == -1) {
1470 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
1471 goto pbiudone;
1472 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001473 fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001474 fflush(cmd_pipe);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001475 }
1476 }
1477
Sami Tolvanen90221202014-12-09 16:39:47 +00001478 if (params.canwrite) {
Tao Bao0940fe12015-08-27 16:41:21 -07001479 pthread_join(params.thread, nullptr);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001480
Tao Bao0940fe12015-08-27 16:41:21 -07001481 fprintf(stderr, "wrote %zu blocks; expected %d\n", params.written, total_blocks);
Tao Bao612336d2015-08-27 16:41:21 -07001482 fprintf(stderr, "max alloc needed was %zu\n", params.buffer.size());
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001483
Sami Tolvanen90221202014-12-09 16:39:47 +00001484 // Delete stash only after successfully completing the update, as it
1485 // may contain blocks needed to complete the update later.
1486 DeleteStash(params.stashbase);
1487 } else {
1488 fprintf(stderr, "verified partition contents; update may be resumed\n");
1489 }
1490
1491 rc = 0;
1492
1493pbiudone:
1494 if (params.fd != -1) {
1495 if (fsync(params.fd) == -1) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001496 fprintf(stderr, "fsync failed: %s\n", strerror(errno));
Sami Tolvanen90221202014-12-09 16:39:47 +00001497 }
Elliott Hughesb47afed2015-05-15 16:19:20 -07001498 close(params.fd);
Sami Tolvanen90221202014-12-09 16:39:47 +00001499 }
1500
Sami Tolvanen90221202014-12-09 16:39:47 +00001501 // Only delete the stash if the update cannot be resumed, or it's
1502 // a verification run and we created the stash.
1503 if (params.isunresumable || (!params.canwrite && params.createdstash)) {
1504 DeleteStash(params.stashbase);
1505 }
1506
Sami Tolvanen90221202014-12-09 16:39:47 +00001507 return StringValue(rc == 0 ? strdup("t") : strdup(""));
1508}
1509
1510// The transfer list is a text file containing commands to
1511// transfer data from one place to another on the target
1512// partition. We parse it and execute the commands in order:
1513//
1514// zero [rangeset]
1515// - fill the indicated blocks with zeros
1516//
1517// new [rangeset]
1518// - fill the blocks with data read from the new_data file
1519//
1520// erase [rangeset]
1521// - mark the given blocks as empty
1522//
1523// move <...>
1524// bsdiff <patchstart> <patchlen> <...>
1525// imgdiff <patchstart> <patchlen> <...>
1526// - read the source blocks, apply a patch (or not in the
1527// case of move), write result to target blocks. bsdiff or
1528// imgdiff specifies the type of patch; move means no patch
1529// at all.
1530//
1531// The format of <...> differs between versions 1 and 2;
1532// see the LoadSrcTgtVersion{1,2}() functions for a
1533// description of what's expected.
1534//
1535// stash <stash_id> <src_range>
1536// - (version 2+ only) load the given source range and stash
1537// the data in the given slot of the stash table.
1538//
1539// The creator of the transfer list will guarantee that no block
1540// is read (ie, used as the source for a patch or move) after it
1541// has been written.
1542//
1543// In version 2, the creator will guarantee that a given stash is
1544// loaded (with a stash command) before it's used in a
1545// move/bsdiff/imgdiff command.
1546//
1547// Within one command the source and target ranges may overlap so
1548// in general we need to read the entire source into memory before
1549// writing anything to the target blocks.
1550//
1551// All the patch data is concatenated into one patch_data file in
1552// the update package. It must be stored uncompressed because we
1553// memory-map it in directly from the archive. (Since patches are
1554// already compressed, we lose very little by not compressing
1555// their concatenation.)
1556//
1557// In version 3, commands that read data from the partition (i.e.
1558// move/bsdiff/imgdiff/stash) have one or more additional hashes
1559// before the range parameters, which are used to check if the
1560// command has already been completed and verify the integrity of
1561// the source data.
1562
1563Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao0940fe12015-08-27 16:41:21 -07001564 // Commands which are not tested are set to nullptr to skip them completely
Sami Tolvanen90221202014-12-09 16:39:47 +00001565 const Command commands[] = {
1566 { "bsdiff", PerformCommandDiff },
Tao Bao0940fe12015-08-27 16:41:21 -07001567 { "erase", nullptr },
Sami Tolvanen90221202014-12-09 16:39:47 +00001568 { "free", PerformCommandFree },
1569 { "imgdiff", PerformCommandDiff },
1570 { "move", PerformCommandMove },
Tao Bao0940fe12015-08-27 16:41:21 -07001571 { "new", nullptr },
Sami Tolvanen90221202014-12-09 16:39:47 +00001572 { "stash", PerformCommandStash },
Tao Bao0940fe12015-08-27 16:41:21 -07001573 { "zero", nullptr }
Sami Tolvanen90221202014-12-09 16:39:47 +00001574 };
1575
1576 // Perform a dry run without writing to test if an update can proceed
1577 return PerformBlockImageUpdate(name, state, argc, argv, commands,
Tao Baoe6aa3322015-08-05 15:20:27 -07001578 sizeof(commands) / sizeof(commands[0]), true);
Sami Tolvanen90221202014-12-09 16:39:47 +00001579}
1580
1581Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) {
1582 const Command commands[] = {
1583 { "bsdiff", PerformCommandDiff },
1584 { "erase", PerformCommandErase },
1585 { "free", PerformCommandFree },
1586 { "imgdiff", PerformCommandDiff },
1587 { "move", PerformCommandMove },
1588 { "new", PerformCommandNew },
1589 { "stash", PerformCommandStash },
1590 { "zero", PerformCommandZero }
1591 };
1592
1593 return PerformBlockImageUpdate(name, state, argc, argv, commands,
Tao Baoe6aa3322015-08-05 15:20:27 -07001594 sizeof(commands) / sizeof(commands[0]), false);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001595}
1596
Tao Bao0940fe12015-08-27 16:41:21 -07001597Value* RangeSha1Fn(const char* name, State* state, int /* argc */, Expr* argv[]) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001598 Value* blockdev_filename;
1599 Value* ranges;
Tao Bao0940fe12015-08-27 16:41:21 -07001600
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001601 if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) {
Tao Bao612336d2015-08-27 16:41:21 -07001602 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001603 }
Tao Bao612336d2015-08-27 16:41:21 -07001604 std::unique_ptr<Value, decltype(&FreeValue)> ranges_holder(ranges, FreeValue);
1605 std::unique_ptr<Value, decltype(&FreeValue)> blockdev_filename_holder(blockdev_filename,
1606 FreeValue);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001607
1608 if (blockdev_filename->type != VAL_STRING) {
1609 ErrorAbort(state, "blockdev_filename argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001610 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001611 }
1612 if (ranges->type != VAL_STRING) {
1613 ErrorAbort(state, "ranges argument to %s must be string", name);
Tao Bao612336d2015-08-27 16:41:21 -07001614 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001615 }
1616
Tao Bao612336d2015-08-27 16:41:21 -07001617 int fd = open(blockdev_filename->data, O_RDWR);
1618 unique_fd fd_holder(fd);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001619 if (fd < 0) {
Elliott Hughes1fdd4522015-03-23 13:33:57 -07001620 ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001621 return StringValue(strdup(""));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001622 }
1623
Tao Bao612336d2015-08-27 16:41:21 -07001624 RangeSet rs;
Tao Bao0940fe12015-08-27 16:41:21 -07001625 parse_range(ranges->data, rs);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001626
1627 SHA_CTX ctx;
1628 SHA_init(&ctx);
1629
Tao Bao612336d2015-08-27 16:41:21 -07001630 std::vector<uint8_t> buffer(BLOCKSIZE);
Tao Bao0940fe12015-08-27 16:41:21 -07001631 for (size_t i = 0; i < rs.count; ++i) {
1632 if (!check_lseek(fd, (off64_t)rs.pos[i*2] * BLOCKSIZE, SEEK_SET)) {
Tao Bao612336d2015-08-27 16:41:21 -07001633 ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data, strerror(errno));
1634 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001635 }
1636
Tao Bao0940fe12015-08-27 16:41:21 -07001637 for (size_t j = rs.pos[i*2]; j < rs.pos[i*2+1]; ++j) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001638 if (read_all(fd, buffer, BLOCKSIZE) == -1) {
1639 ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data,
Tao Bao0940fe12015-08-27 16:41:21 -07001640 strerror(errno));
Tao Bao612336d2015-08-27 16:41:21 -07001641 return StringValue(strdup(""));
Sami Tolvanen90221202014-12-09 16:39:47 +00001642 }
1643
Tao Bao612336d2015-08-27 16:41:21 -07001644 SHA_update(&ctx, buffer.data(), BLOCKSIZE);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001645 }
1646 }
Tao Bao612336d2015-08-27 16:41:21 -07001647 const uint8_t* digest = SHA_final(&ctx);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001648
Tao Bao612336d2015-08-27 16:41:21 -07001649 return StringValue(strdup(print_sha1(digest).c_str()));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001650}
1651
1652void RegisterBlockImageFunctions() {
Sami Tolvanen90221202014-12-09 16:39:47 +00001653 RegisterFunction("block_image_verify", BlockImageVerifyFn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001654 RegisterFunction("block_image_update", BlockImageUpdateFn);
1655 RegisterFunction("range_sha1", RangeSha1Fn);
1656}