blob: 6c7b3efcf7974d52758e1079b28297d9687645e1 [file] [log] [blame]
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ctype.h>
18#include <errno.h>
Sami Tolvanen90221202014-12-09 16:39:47 +000019#include <dirent.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070020#include <fcntl.h>
Tao Bao0bbc7642017-03-29 23:57:47 -070021#include <inttypes.h>
Tao Baoba9a42a2015-06-23 23:23:33 -070022#include <linux/fs.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070023#include <pthread.h>
24#include <stdarg.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Sami Tolvanen90221202014-12-09 16:39:47 +000028#include <sys/stat.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070029#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/ioctl.h>
32#include <time.h>
33#include <unistd.h>
Sami Tolvanen0a7b4732015-06-25 10:25:36 +010034#include <fec/io.h>
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070035
Tao Baoec8272f2017-03-15 17:39:01 -070036#include <functional>
Tao Baoe6aa3322015-08-05 15:20:27 -070037#include <memory>
38#include <string>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070039#include <unordered_map>
Tao Bao0940fe12015-08-27 16:41:21 -070040#include <vector>
Tao Baoe6aa3322015-08-05 15:20:27 -070041
Tao Bao039f2da2016-11-22 16:29:50 -080042#include <android-base/logging.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080043#include <android-base/parseint.h>
44#include <android-base/strings.h>
Elliott Hughesbcabd092016-03-22 20:19:22 -070045#include <android-base/unique_fd.h>
Tao Bao51412212016-12-28 14:44:05 -080046#include <applypatch/applypatch.h>
Tianjie Xu107a34f2017-06-29 17:04:21 -070047#include <brotli/decode.h>
Tao Bao51412212016-12-28 14:44:05 -080048#include <openssl/sha.h>
Tianjie Xua946b9e2017-03-21 16:24:57 -070049#include <private/android_filesystem_config.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070050#include <ziparchive/zip_archive.h>
Tao Baoe6aa3322015-08-05 15:20:27 -070051
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070052#include "edify/expr.h"
Tao Baod33b2f82017-09-28 21:29:11 -070053#include "otafault/ota_io.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070054#include "otautil/error_code.h"
Tao Bao09e468f2017-09-29 14:39:33 -070055#include "otautil/print_sha1.h"
56#include "otautil/rangeset.h"
Tao Bao8f237572017-03-26 13:36:49 -070057#include "updater/install.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070058#include "updater/updater.h"
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070059
Sami Tolvanene82fa182015-06-10 15:58:12 +000060// Set this to 0 to interpret 'erase' transfers to mean do a
61// BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret
62// erase to mean fill the region with zeroes.
63#define DEBUG_ERASE 0
64
Tao Bao51412212016-12-28 14:44:05 -080065static constexpr size_t BLOCKSIZE = 4096;
66static constexpr const char* STASH_DIRECTORY_BASE = "/cache/recovery";
67static constexpr mode_t STASH_DIRECTORY_MODE = 0700;
68static constexpr mode_t STASH_FILE_MODE = 0600;
Sami Tolvanen90221202014-12-09 16:39:47 +000069
Tianjie Xu16255832016-04-30 11:49:59 -070070static CauseCode failure_type = kNoCause;
Tianjie Xu7ce287d2016-05-31 09:29:49 -070071static bool is_retry = false;
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070072static std::unordered_map<std::string, RangeSet> stash_map;
Tianjie Xu7eca97e2016-03-22 18:08:12 -070073
Sami Tolvanen90221202014-12-09 16:39:47 +000074static int read_all(int fd, uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070075 size_t so_far = 0;
76 while (so_far < size) {
Jed Estepa7b9a462015-12-15 16:04:53 -080077 ssize_t r = TEMP_FAILURE_RETRY(ota_read(fd, data+so_far, size-so_far));
Elliott Hughes7bad7c42015-04-28 17:24:24 -070078 if (r == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -070079 failure_type = kFreadFailure;
Tao Bao039f2da2016-11-22 16:29:50 -080080 PLOG(ERROR) << "read failed";
Sami Tolvanen90221202014-12-09 16:39:47 +000081 return -1;
Tianjie Xu71e182b2016-08-31 18:06:33 -070082 } else if (r == 0) {
83 failure_type = kFreadFailure;
Tao Bao039f2da2016-11-22 16:29:50 -080084 LOG(ERROR) << "read reached unexpected EOF.";
Tianjie Xu71e182b2016-08-31 18:06:33 -070085 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070086 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -070087 so_far += r;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070088 }
Sami Tolvanen90221202014-12-09 16:39:47 +000089 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070090}
91
Tao Bao612336d2015-08-27 16:41:21 -070092static int read_all(int fd, std::vector<uint8_t>& buffer, size_t size) {
93 return read_all(fd, buffer.data(), size);
94}
95
Sami Tolvanen90221202014-12-09 16:39:47 +000096static int write_all(int fd, const uint8_t* data, size_t size) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070097 size_t written = 0;
98 while (written < size) {
Jed Estepa7b9a462015-12-15 16:04:53 -080099 ssize_t w = TEMP_FAILURE_RETRY(ota_write(fd, data+written, size-written));
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700100 if (w == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700101 failure_type = kFwriteFailure;
Tao Bao039f2da2016-11-22 16:29:50 -0800102 PLOG(ERROR) << "write failed";
Sami Tolvanen90221202014-12-09 16:39:47 +0000103 return -1;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700104 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700105 written += w;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700106 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000107
Sami Tolvanen90221202014-12-09 16:39:47 +0000108 return 0;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700109}
110
Tao Bao612336d2015-08-27 16:41:21 -0700111static int write_all(int fd, const std::vector<uint8_t>& buffer, size_t size) {
112 return write_all(fd, buffer.data(), size);
113}
114
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700115static bool discard_blocks(int fd, off64_t offset, uint64_t size) {
Tao Baobf5b77d2017-03-30 16:57:29 -0700116 // Don't discard blocks unless the update is a retry run.
117 if (!is_retry) {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700118 return true;
Tao Baobf5b77d2017-03-30 16:57:29 -0700119 }
120
121 uint64_t args[2] = { static_cast<uint64_t>(offset), size };
122 if (ioctl(fd, BLKDISCARD, &args) == -1) {
123 PLOG(ERROR) << "BLKDISCARD ioctl failed";
124 return false;
125 }
126 return true;
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700127}
128
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700129static bool check_lseek(int fd, off64_t offset, int whence) {
130 off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
131 if (rc == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700132 failure_type = kLseekFailure;
Tao Bao039f2da2016-11-22 16:29:50 -0800133 PLOG(ERROR) << "lseek64 failed";
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700134 return false;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700135 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700136 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700137}
138
Tao Bao612336d2015-08-27 16:41:21 -0700139static void allocate(size_t size, std::vector<uint8_t>& buffer) {
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700140 // if the buffer's big enough, reuse it.
Tao Bao612336d2015-08-27 16:41:21 -0700141 if (size <= buffer.size()) return;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700142
Tao Bao612336d2015-08-27 16:41:21 -0700143 buffer.resize(size);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700144}
145
Tao Bao60a70af2017-03-26 14:03:52 -0700146/**
147 * RangeSinkWriter reads data from the given FD, and writes them to the destination specified by the
148 * given RangeSet.
149 */
150class RangeSinkWriter {
151 public:
152 RangeSinkWriter(int fd, const RangeSet& tgt)
Tianjie Xu107a34f2017-06-29 17:04:21 -0700153 : fd_(fd),
154 tgt_(tgt),
155 next_range_(0),
156 current_range_left_(0),
157 bytes_written_(0) {
Tao Baobf5b77d2017-03-30 16:57:29 -0700158 CHECK_NE(tgt.size(), static_cast<size_t>(0));
Tao Bao60a70af2017-03-26 14:03:52 -0700159 };
Tao Bao0940fe12015-08-27 16:41:21 -0700160
Tao Bao60a70af2017-03-26 14:03:52 -0700161 bool Finished() const {
Tao Baobf5b77d2017-03-30 16:57:29 -0700162 return next_range_ == tgt_.size() && current_range_left_ == 0;
Tao Baof7eb7602017-03-27 15:12:48 -0700163 }
164
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700165 size_t AvailableSpace() const {
166 return tgt_.blocks() * BLOCKSIZE - bytes_written_;
167 }
168
169 // Return number of bytes written; and 0 indicates a writing failure.
170 size_t Write(const uint8_t* data, size_t size) {
Tao Bao60a70af2017-03-26 14:03:52 -0700171 if (Finished()) {
172 LOG(ERROR) << "range sink write overrun; can't write " << size << " bytes";
173 return 0;
Tao Baof7eb7602017-03-27 15:12:48 -0700174 }
175
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700176 size_t written = 0;
Tao Bao60a70af2017-03-26 14:03:52 -0700177 while (size > 0) {
178 // Move to the next range as needed.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700179 if (!SeekToOutputRange()) {
180 break;
Tao Bao60a70af2017-03-26 14:03:52 -0700181 }
Tao Baof7eb7602017-03-27 15:12:48 -0700182
Tao Bao60a70af2017-03-26 14:03:52 -0700183 size_t write_now = size;
184 if (current_range_left_ < write_now) {
185 write_now = current_range_left_;
186 }
Tao Baof7eb7602017-03-27 15:12:48 -0700187
Tao Bao60a70af2017-03-26 14:03:52 -0700188 if (write_all(fd_, data, write_now) == -1) {
Tao Baof7eb7602017-03-27 15:12:48 -0700189 break;
190 }
Tao Bao60a70af2017-03-26 14:03:52 -0700191
192 data += write_now;
193 size -= write_now;
194
195 current_range_left_ -= write_now;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700196 written += write_now;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700197 }
Tao Bao60a70af2017-03-26 14:03:52 -0700198
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700199 bytes_written_ += written;
200 return written;
Tao Baof7eb7602017-03-27 15:12:48 -0700201 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700202
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700203 size_t BytesWritten() const {
204 return bytes_written_;
205 }
206
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700207 private:
Tianjie Xu107a34f2017-06-29 17:04:21 -0700208 // Set up the output cursor, move to next range if needed.
209 bool SeekToOutputRange() {
210 // We haven't finished the current range yet.
211 if (current_range_left_ != 0) {
212 return true;
213 }
214 // We can't write any more; let the write function return how many bytes have been written
215 // so far.
216 if (next_range_ >= tgt_.size()) {
217 return false;
218 }
219
220 const Range& range = tgt_[next_range_];
221 off64_t offset = static_cast<off64_t>(range.first) * BLOCKSIZE;
222 current_range_left_ = (range.second - range.first) * BLOCKSIZE;
223 next_range_++;
224
225 if (!discard_blocks(fd_, offset, current_range_left_)) {
226 return false;
227 }
228 if (!check_lseek(fd_, offset, SEEK_SET)) {
229 return false;
230 }
231 return true;
232 }
233
234 // The output file descriptor.
Tao Bao60a70af2017-03-26 14:03:52 -0700235 int fd_;
Tianjie Xu107a34f2017-06-29 17:04:21 -0700236 // The destination ranges for the data.
Tao Bao60a70af2017-03-26 14:03:52 -0700237 const RangeSet& tgt_;
238 // The next range that we should write to.
239 size_t next_range_;
240 // The number of bytes to write before moving to the next range.
241 size_t current_range_left_;
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700242 // Total bytes written by the writer.
243 size_t bytes_written_;
Tao Bao60a70af2017-03-26 14:03:52 -0700244};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700245
Tao Bao60a70af2017-03-26 14:03:52 -0700246/**
247 * All of the data for all the 'new' transfers is contained in one file in the update package,
248 * concatenated together in the order in which transfers.list will need it. We want to stream it out
249 * of the archive (it's compressed) without writing it to a temp file, but we can't write each
250 * section until it's that transfer's turn to go.
251 *
252 * To achieve this, we expand the new data from the archive in a background thread, and block that
253 * threads 'receive uncompressed data' function until the main thread has reached a point where we
254 * want some new data to be written. We signal the background thread with the destination for the
255 * data and block the main thread, waiting for the background thread to complete writing that
256 * section. Then it signals the main thread to wake up and goes back to blocking waiting for a
257 * transfer.
258 *
259 * NewThreadInfo is the struct used to pass information back and forth between the two threads. When
260 * the main thread wants some data written, it sets writer to the destination location and signals
261 * the condition. When the background thread is done writing, it clears writer and signals the
262 * condition again.
263 */
Tao Bao0940fe12015-08-27 16:41:21 -0700264struct NewThreadInfo {
Tao Bao60a70af2017-03-26 14:03:52 -0700265 ZipArchiveHandle za;
266 ZipEntry entry;
Tianjie Xu107a34f2017-06-29 17:04:21 -0700267 bool brotli_compressed;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700268
Tianjie Xu107a34f2017-06-29 17:04:21 -0700269 std::unique_ptr<RangeSinkWriter> writer;
270 BrotliDecoderState* brotli_decoder_state;
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700271 bool receiver_available;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700272
Tao Bao60a70af2017-03-26 14:03:52 -0700273 pthread_mutex_t mu;
274 pthread_cond_t cv;
Tao Bao0940fe12015-08-27 16:41:21 -0700275};
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700276
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700277static bool receive_new_data(const uint8_t* data, size_t size, void* cookie) {
Tao Bao60a70af2017-03-26 14:03:52 -0700278 NewThreadInfo* nti = static_cast<NewThreadInfo*>(cookie);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700279
Tao Bao60a70af2017-03-26 14:03:52 -0700280 while (size > 0) {
281 // Wait for nti->writer to be non-null, indicating some of this data is wanted.
282 pthread_mutex_lock(&nti->mu);
283 while (nti->writer == nullptr) {
Tianjie Xu5450c842017-10-18 13:15:21 -0700284 // End the new data receiver if we encounter an error when performing block image update.
285 if (!nti->receiver_available) {
286 pthread_mutex_unlock(&nti->mu);
287 return false;
288 }
Tao Bao60a70af2017-03-26 14:03:52 -0700289 pthread_cond_wait(&nti->cv, &nti->mu);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700290 }
Tao Bao60a70af2017-03-26 14:03:52 -0700291 pthread_mutex_unlock(&nti->mu);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700292
Tao Bao60a70af2017-03-26 14:03:52 -0700293 // At this point nti->writer is set, and we own it. The main thread is waiting for it to
294 // disappear from nti.
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700295 size_t write_now = std::min(size, nti->writer->AvailableSpace());
296 if (nti->writer->Write(data, write_now) != write_now) {
297 LOG(ERROR) << "Failed to write " << write_now << " bytes.";
Tianjie Xu107a34f2017-06-29 17:04:21 -0700298 return false;
299 }
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700300
301 data += write_now;
302 size -= write_now;
303
304 if (nti->writer->Finished()) {
305 // We have written all the bytes desired by this writer.
306
307 pthread_mutex_lock(&nti->mu);
308 nti->writer = nullptr;
309 pthread_cond_broadcast(&nti->cv);
310 pthread_mutex_unlock(&nti->mu);
311 }
312 }
313
314 return true;
315}
316
317static bool receive_brotli_new_data(const uint8_t* data, size_t size, void* cookie) {
318 NewThreadInfo* nti = static_cast<NewThreadInfo*>(cookie);
319
320 while (size > 0 || BrotliDecoderHasMoreOutput(nti->brotli_decoder_state)) {
321 // Wait for nti->writer to be non-null, indicating some of this data is wanted.
322 pthread_mutex_lock(&nti->mu);
323 while (nti->writer == nullptr) {
Tianjie Xu5450c842017-10-18 13:15:21 -0700324 // End the receiver if we encounter an error when performing block image update.
325 if (!nti->receiver_available) {
326 pthread_mutex_unlock(&nti->mu);
327 return false;
328 }
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700329 pthread_cond_wait(&nti->cv, &nti->mu);
330 }
331 pthread_mutex_unlock(&nti->mu);
332
333 // At this point nti->writer is set, and we own it. The main thread is waiting for it to
334 // disappear from nti.
335
336 size_t buffer_size = std::min<size_t>(32768, nti->writer->AvailableSpace());
337 if (buffer_size == 0) {
338 LOG(ERROR) << "No space left in output range";
339 return false;
340 }
341 uint8_t buffer[buffer_size];
342 size_t available_in = size;
343 size_t available_out = buffer_size;
344 uint8_t* next_out = buffer;
345
346 // The brotli decoder will update |data|, |available_in|, |next_out| and |available_out|.
347 BrotliDecoderResult result = BrotliDecoderDecompressStream(
348 nti->brotli_decoder_state, &available_in, &data, &available_out, &next_out, nullptr);
349
350 if (result == BROTLI_DECODER_RESULT_ERROR) {
351 LOG(ERROR) << "Decompression failed with "
352 << BrotliDecoderErrorString(BrotliDecoderGetErrorCode(nti->brotli_decoder_state));
353 return false;
354 }
355
356 LOG(DEBUG) << "bytes to write: " << buffer_size - available_out << ", bytes consumed "
357 << size - available_in << ", decoder status " << result;
358
359 size_t write_now = buffer_size - available_out;
360 if (nti->writer->Write(buffer, write_now) != write_now) {
361 LOG(ERROR) << "Failed to write " << write_now << " bytes.";
362 return false;
363 }
364
365 // Update the remaining size. The input data ptr is already updated by brotli decoder function.
366 size = available_in;
Tao Bao60a70af2017-03-26 14:03:52 -0700367
368 if (nti->writer->Finished()) {
369 // We have written all the bytes desired by this writer.
370
371 pthread_mutex_lock(&nti->mu);
372 nti->writer = nullptr;
373 pthread_cond_broadcast(&nti->cv);
374 pthread_mutex_unlock(&nti->mu);
375 }
376 }
377
378 return true;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700379}
380
381static void* unzip_new_data(void* cookie) {
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700382 NewThreadInfo* nti = static_cast<NewThreadInfo*>(cookie);
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700383 if (nti->brotli_compressed) {
384 ProcessZipEntryContents(nti->za, &nti->entry, receive_brotli_new_data, nti);
385 } else {
386 ProcessZipEntryContents(nti->za, &nti->entry, receive_new_data, nti);
387 }
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700388 pthread_mutex_lock(&nti->mu);
389 nti->receiver_available = false;
390 if (nti->writer != nullptr) {
391 pthread_cond_broadcast(&nti->cv);
392 }
393 pthread_mutex_unlock(&nti->mu);
394 return nullptr;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700395}
396
Tao Bao612336d2015-08-27 16:41:21 -0700397static int ReadBlocks(const RangeSet& src, std::vector<uint8_t>& buffer, int fd) {
Tao Baobf5b77d2017-03-30 16:57:29 -0700398 size_t p = 0;
399 for (const auto& range : src) {
400 if (!check_lseek(fd, static_cast<off64_t>(range.first) * BLOCKSIZE, SEEK_SET)) {
401 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000402 }
403
Tao Baobf5b77d2017-03-30 16:57:29 -0700404 size_t size = (range.second - range.first) * BLOCKSIZE;
405 if (read_all(fd, buffer.data() + p, size) == -1) {
406 return -1;
407 }
408
409 p += size;
410 }
411
412 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000413}
414
Tao Bao612336d2015-08-27 16:41:21 -0700415static int WriteBlocks(const RangeSet& tgt, const std::vector<uint8_t>& buffer, int fd) {
Tao Bao60a70af2017-03-26 14:03:52 -0700416 size_t written = 0;
Tao Baobf5b77d2017-03-30 16:57:29 -0700417 for (const auto& range : tgt) {
418 off64_t offset = static_cast<off64_t>(range.first) * BLOCKSIZE;
419 size_t size = (range.second - range.first) * BLOCKSIZE;
Tao Bao60a70af2017-03-26 14:03:52 -0700420 if (!discard_blocks(fd, offset, size)) {
421 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000422 }
423
Tao Bao60a70af2017-03-26 14:03:52 -0700424 if (!check_lseek(fd, offset, SEEK_SET)) {
425 return -1;
426 }
427
428 if (write_all(fd, buffer.data() + written, size) == -1) {
429 return -1;
430 }
431
432 written += size;
433 }
434
435 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000436}
437
Tao Baobaad2d42015-12-06 16:56:27 -0800438// Parameters for transfer list command functions
439struct CommandParameters {
440 std::vector<std::string> tokens;
441 size_t cpos;
442 const char* cmdname;
443 const char* cmdline;
444 std::string freestash;
445 std::string stashbase;
446 bool canwrite;
447 int createdstash;
Elliott Hughesbcabd092016-03-22 20:19:22 -0700448 android::base::unique_fd fd;
Tao Baobaad2d42015-12-06 16:56:27 -0800449 bool foundwrites;
450 bool isunresumable;
451 int version;
452 size_t written;
Tianjie Xudd874b12016-05-13 12:13:15 -0700453 size_t stashed;
Tao Baobaad2d42015-12-06 16:56:27 -0800454 NewThreadInfo nti;
455 pthread_t thread;
456 std::vector<uint8_t> buffer;
457 uint8_t* patch_start;
458};
459
Tianjie Xu2cd36ba2017-03-15 23:52:46 +0000460// Print the hash in hex for corrupted source blocks (excluding the stashed blocks which is
461// handled separately).
462static void PrintHashForCorruptedSourceBlocks(const CommandParameters& params,
463 const std::vector<uint8_t>& buffer) {
464 LOG(INFO) << "unexpected contents of source blocks in cmd:\n" << params.cmdline;
Tianjie Xu2cd36ba2017-03-15 23:52:46 +0000465 CHECK(params.tokens[0] == "move" || params.tokens[0] == "bsdiff" ||
466 params.tokens[0] == "imgdiff");
467
468 size_t pos = 0;
469 // Command example:
470 // move <onehash> <tgt_range> <src_blk_count> <src_range> [<loc_range> <stashed_blocks>]
471 // bsdiff <offset> <len> <src_hash> <tgt_hash> <tgt_range> <src_blk_count> <src_range>
472 // [<loc_range> <stashed_blocks>]
473 if (params.tokens[0] == "move") {
474 // src_range for move starts at the 4th position.
475 if (params.tokens.size() < 5) {
476 LOG(ERROR) << "failed to parse source range in cmd:\n" << params.cmdline;
477 return;
478 }
479 pos = 4;
480 } else {
481 // src_range for diff starts at the 7th position.
482 if (params.tokens.size() < 8) {
483 LOG(ERROR) << "failed to parse source range in cmd:\n" << params.cmdline;
484 return;
485 }
486 pos = 7;
487 }
488
489 // Source blocks in stash only, no work to do.
490 if (params.tokens[pos] == "-") {
491 return;
492 }
493
Tao Bao8f237572017-03-26 13:36:49 -0700494 RangeSet src = RangeSet::Parse(params.tokens[pos++]);
Tianjie Xu2cd36ba2017-03-15 23:52:46 +0000495
496 RangeSet locs;
497 // If there's no stashed blocks, content in the buffer is consecutive and has the same
498 // order as the source blocks.
499 if (pos == params.tokens.size()) {
Tao Baobf5b77d2017-03-30 16:57:29 -0700500 locs = RangeSet(std::vector<Range>{ Range{ 0, src.blocks() } });
Tianjie Xu2cd36ba2017-03-15 23:52:46 +0000501 } else {
502 // Otherwise, the next token is the offset of the source blocks in the target range.
503 // Example: for the tokens <4,63946,63947,63948,63979> <4,6,7,8,39> <stashed_blocks>;
504 // We want to print SHA-1 for the data in buffer[6], buffer[8], buffer[9] ... buffer[38];
505 // this corresponds to the 32 src blocks #63946, #63948, #63949 ... #63978.
Tao Bao8f237572017-03-26 13:36:49 -0700506 locs = RangeSet::Parse(params.tokens[pos++]);
Tao Baobf5b77d2017-03-30 16:57:29 -0700507 CHECK_EQ(src.blocks(), locs.blocks());
Tianjie Xu2cd36ba2017-03-15 23:52:46 +0000508 }
509
Tao Baobf5b77d2017-03-30 16:57:29 -0700510 LOG(INFO) << "printing hash in hex for " << src.blocks() << " source blocks";
511 for (size_t i = 0; i < src.blocks(); i++) {
Tao Bao8f237572017-03-26 13:36:49 -0700512 size_t block_num = src.GetBlockNumber(i);
513 size_t buffer_index = locs.GetBlockNumber(i);
Tianjie Xu2cd36ba2017-03-15 23:52:46 +0000514 CHECK_LE((buffer_index + 1) * BLOCKSIZE, buffer.size());
515
516 uint8_t digest[SHA_DIGEST_LENGTH];
517 SHA1(buffer.data() + buffer_index * BLOCKSIZE, BLOCKSIZE, digest);
518 std::string hexdigest = print_sha1(digest);
519 LOG(INFO) << " block number: " << block_num << ", SHA-1: " << hexdigest;
520 }
521}
522
523// If the calculated hash for the whole stash doesn't match the stash id, print the SHA-1
524// in hex for each block.
525static void PrintHashForCorruptedStashedBlocks(const std::string& id,
526 const std::vector<uint8_t>& buffer,
527 const RangeSet& src) {
528 LOG(INFO) << "printing hash in hex for stash_id: " << id;
Tao Baobf5b77d2017-03-30 16:57:29 -0700529 CHECK_EQ(src.blocks() * BLOCKSIZE, buffer.size());
Tianjie Xu2cd36ba2017-03-15 23:52:46 +0000530
Tao Baobf5b77d2017-03-30 16:57:29 -0700531 for (size_t i = 0; i < src.blocks(); i++) {
Tao Bao8f237572017-03-26 13:36:49 -0700532 size_t block_num = src.GetBlockNumber(i);
Tianjie Xu2cd36ba2017-03-15 23:52:46 +0000533
534 uint8_t digest[SHA_DIGEST_LENGTH];
535 SHA1(buffer.data() + i * BLOCKSIZE, BLOCKSIZE, digest);
536 std::string hexdigest = print_sha1(digest);
537 LOG(INFO) << " block number: " << block_num << ", SHA-1: " << hexdigest;
538 }
539}
540
541// If the stash file doesn't exist, read the source blocks this stash contains and print the
542// SHA-1 for these blocks.
543static void PrintHashForMissingStashedBlocks(const std::string& id, int fd) {
544 if (stash_map.find(id) == stash_map.end()) {
545 LOG(ERROR) << "No stash saved for id: " << id;
546 return;
547 }
548
549 LOG(INFO) << "print hash in hex for source blocks in missing stash: " << id;
550 const RangeSet& src = stash_map[id];
Tao Baobf5b77d2017-03-30 16:57:29 -0700551 std::vector<uint8_t> buffer(src.blocks() * BLOCKSIZE);
Tianjie Xu2cd36ba2017-03-15 23:52:46 +0000552 if (ReadBlocks(src, buffer, fd) == -1) {
553 LOG(ERROR) << "failed to read source blocks for stash: " << id;
554 return;
555 }
556 PrintHashForCorruptedStashedBlocks(id, buffer, src);
557}
558
Tao Bao612336d2015-08-27 16:41:21 -0700559static int VerifyBlocks(const std::string& expected, const std::vector<uint8_t>& buffer,
Tao Bao0940fe12015-08-27 16:41:21 -0700560 const size_t blocks, bool printerror) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800561 uint8_t digest[SHA_DIGEST_LENGTH];
Tao Bao612336d2015-08-27 16:41:21 -0700562 const uint8_t* data = buffer.data();
Sami Tolvanen90221202014-12-09 16:39:47 +0000563
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800564 SHA1(data, blocks * BLOCKSIZE, digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000565
Tao Baoe6aa3322015-08-05 15:20:27 -0700566 std::string hexdigest = print_sha1(digest);
Sami Tolvanen90221202014-12-09 16:39:47 +0000567
Tao Bao0940fe12015-08-27 16:41:21 -0700568 if (hexdigest != expected) {
569 if (printerror) {
Tao Bao039f2da2016-11-22 16:29:50 -0800570 LOG(ERROR) << "failed to verify blocks (expected " << expected << ", read "
571 << hexdigest << ")";
Tao Bao0940fe12015-08-27 16:41:21 -0700572 }
573 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000574 }
575
Tao Bao0940fe12015-08-27 16:41:21 -0700576 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000577}
578
Tao Bao0940fe12015-08-27 16:41:21 -0700579static std::string GetStashFileName(const std::string& base, const std::string& id,
580 const std::string& postfix) {
Tao Baoe6aa3322015-08-05 15:20:27 -0700581 if (base.empty()) {
582 return "";
Sami Tolvanen90221202014-12-09 16:39:47 +0000583 }
584
Tao Baoe6aa3322015-08-05 15:20:27 -0700585 std::string fn(STASH_DIRECTORY_BASE);
586 fn += "/" + base + "/" + id + postfix;
Sami Tolvanen90221202014-12-09 16:39:47 +0000587
588 return fn;
589}
590
Tao Baoec8272f2017-03-15 17:39:01 -0700591// Does a best effort enumeration of stash files. Ignores possible non-file items in the stash
592// directory and continues despite of errors. Calls the 'callback' function for each file.
593static void EnumerateStash(const std::string& dirname,
594 const std::function<void(const std::string&)>& callback) {
595 if (dirname.empty()) return;
Sami Tolvanen90221202014-12-09 16:39:47 +0000596
Tao Baoec8272f2017-03-15 17:39:01 -0700597 std::unique_ptr<DIR, decltype(&closedir)> directory(opendir(dirname.c_str()), closedir);
Sami Tolvanen90221202014-12-09 16:39:47 +0000598
Tao Baoec8272f2017-03-15 17:39:01 -0700599 if (directory == nullptr) {
600 if (errno != ENOENT) {
601 PLOG(ERROR) << "opendir \"" << dirname << "\" failed";
Sami Tolvanen90221202014-12-09 16:39:47 +0000602 }
Tao Bao51412212016-12-28 14:44:05 -0800603 return;
604 }
Tao Baoe6aa3322015-08-05 15:20:27 -0700605
Tao Baoec8272f2017-03-15 17:39:01 -0700606 dirent* item;
607 while ((item = readdir(directory.get())) != nullptr) {
608 if (item->d_type != DT_REG) continue;
609 callback(dirname + "/" + item->d_name);
Tao Bao51412212016-12-28 14:44:05 -0800610 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000611}
612
613// Deletes the stash directory and all files in it. Assumes that it only
614// contains files. There is nothing we can do about unlikely, but possible
615// errors, so they are merely logged.
Tao Baoec8272f2017-03-15 17:39:01 -0700616static void DeleteFile(const std::string& fn) {
617 if (fn.empty()) return;
Sami Tolvanen90221202014-12-09 16:39:47 +0000618
Tao Baoec8272f2017-03-15 17:39:01 -0700619 LOG(INFO) << "deleting " << fn;
Sami Tolvanen90221202014-12-09 16:39:47 +0000620
Tao Baoec8272f2017-03-15 17:39:01 -0700621 if (unlink(fn.c_str()) == -1 && errno != ENOENT) {
622 PLOG(ERROR) << "unlink \"" << fn << "\" failed";
623 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000624}
625
Tao Baoe6aa3322015-08-05 15:20:27 -0700626static void DeleteStash(const std::string& base) {
Tao Baoec8272f2017-03-15 17:39:01 -0700627 if (base.empty()) return;
628
629 LOG(INFO) << "deleting stash " << base;
630
631 std::string dirname = GetStashFileName(base, "", "");
632 EnumerateStash(dirname, DeleteFile);
633
634 if (rmdir(dirname.c_str()) == -1) {
635 if (errno != ENOENT && errno != ENOTDIR) {
636 PLOG(ERROR) << "rmdir \"" << dirname << "\" failed";
Sami Tolvanen90221202014-12-09 16:39:47 +0000637 }
Tao Baoec8272f2017-03-15 17:39:01 -0700638 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000639}
640
Tao Baobcf46492017-03-23 15:28:20 -0700641static int LoadStash(CommandParameters& params, const std::string& id, bool verify, size_t* blocks,
642 std::vector<uint8_t>& buffer, bool printnoent) {
Tao Baobf5b77d2017-03-30 16:57:29 -0700643 // In verify mode, if source range_set was saved for the given hash, check contents in the source
644 // blocks first. If the check fails, search for the stashed files on /cache as usual.
645 if (!params.canwrite) {
646 if (stash_map.find(id) != stash_map.end()) {
647 const RangeSet& src = stash_map[id];
648 allocate(src.blocks() * BLOCKSIZE, buffer);
Tianjie Xu7eca97e2016-03-22 18:08:12 -0700649
Tao Baobf5b77d2017-03-30 16:57:29 -0700650 if (ReadBlocks(src, buffer, params.fd) == -1) {
651 LOG(ERROR) << "failed to read source blocks in stash map.";
Tao Bao0940fe12015-08-27 16:41:21 -0700652 return -1;
Tao Baobf5b77d2017-03-30 16:57:29 -0700653 }
654 if (VerifyBlocks(id, buffer, src.blocks(), true) != 0) {
655 LOG(ERROR) << "failed to verify loaded source blocks in stash map.";
656 PrintHashForCorruptedStashedBlocks(id, buffer, src);
Tao Bao0940fe12015-08-27 16:41:21 -0700657 return -1;
Tao Baobf5b77d2017-03-30 16:57:29 -0700658 }
659 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000660 }
Tao Baobf5b77d2017-03-30 16:57:29 -0700661 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000662
Tao Baobf5b77d2017-03-30 16:57:29 -0700663 size_t blockcount = 0;
664 if (!blocks) {
665 blocks = &blockcount;
666 }
667
668 std::string fn = GetStashFileName(params.stashbase, id, "");
669
670 struct stat sb;
671 if (stat(fn.c_str(), &sb) == -1) {
672 if (errno != ENOENT || printnoent) {
673 PLOG(ERROR) << "stat \"" << fn << "\" failed";
674 PrintHashForMissingStashedBlocks(id, params.fd);
Sami Tolvanen90221202014-12-09 16:39:47 +0000675 }
Tao Baobf5b77d2017-03-30 16:57:29 -0700676 return -1;
677 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000678
Tao Baobf5b77d2017-03-30 16:57:29 -0700679 LOG(INFO) << " loading " << fn;
Sami Tolvanen90221202014-12-09 16:39:47 +0000680
Tao Baobf5b77d2017-03-30 16:57:29 -0700681 if ((sb.st_size % BLOCKSIZE) != 0) {
682 LOG(ERROR) << fn << " size " << sb.st_size << " not multiple of block size " << BLOCKSIZE;
683 return -1;
684 }
685
686 android::base::unique_fd fd(TEMP_FAILURE_RETRY(ota_open(fn.c_str(), O_RDONLY)));
687 if (fd == -1) {
688 PLOG(ERROR) << "open \"" << fn << "\" failed";
689 return -1;
690 }
691
692 allocate(sb.st_size, buffer);
693
694 if (read_all(fd, buffer, sb.st_size) == -1) {
695 return -1;
696 }
697
698 *blocks = sb.st_size / BLOCKSIZE;
699
700 if (verify && VerifyBlocks(id, buffer, *blocks, true) != 0) {
701 LOG(ERROR) << "unexpected contents in " << fn;
702 if (stash_map.find(id) == stash_map.end()) {
703 LOG(ERROR) << "failed to find source blocks number for stash " << id
704 << " when executing command: " << params.cmdname;
705 } else {
706 const RangeSet& src = stash_map[id];
707 PrintHashForCorruptedStashedBlocks(id, buffer, src);
Sami Tolvanen90221202014-12-09 16:39:47 +0000708 }
Tao Baobf5b77d2017-03-30 16:57:29 -0700709 DeleteFile(fn);
710 return -1;
711 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000712
Tao Baobf5b77d2017-03-30 16:57:29 -0700713 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000714}
715
Tao Bao612336d2015-08-27 16:41:21 -0700716static int WriteStash(const std::string& base, const std::string& id, int blocks,
Tao Baod2aecd42017-03-23 14:43:44 -0700717 std::vector<uint8_t>& buffer, bool checkspace, bool* exists) {
Tao Bao612336d2015-08-27 16:41:21 -0700718 if (base.empty()) {
Tao Bao0940fe12015-08-27 16:41:21 -0700719 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000720 }
721
722 if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800723 LOG(ERROR) << "not enough space to write stash";
Tao Bao0940fe12015-08-27 16:41:21 -0700724 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000725 }
726
Tao Bao0940fe12015-08-27 16:41:21 -0700727 std::string fn = GetStashFileName(base, id, ".partial");
728 std::string cn = GetStashFileName(base, id, "");
Sami Tolvanen90221202014-12-09 16:39:47 +0000729
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100730 if (exists) {
Tao Bao0940fe12015-08-27 16:41:21 -0700731 struct stat sb;
732 int res = stat(cn.c_str(), &sb);
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100733
734 if (res == 0) {
735 // The file already exists and since the name is the hash of the contents,
736 // it's safe to assume the contents are identical (accidental hash collisions
737 // are unlikely)
Tao Bao039f2da2016-11-22 16:29:50 -0800738 LOG(INFO) << " skipping " << blocks << " existing blocks in " << cn;
Tao Bao0940fe12015-08-27 16:41:21 -0700739 *exists = true;
740 return 0;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100741 }
742
Tao Bao0940fe12015-08-27 16:41:21 -0700743 *exists = false;
Sami Tolvanen43b748f2015-04-17 12:50:31 +0100744 }
745
Tao Bao039f2da2016-11-22 16:29:50 -0800746 LOG(INFO) << " writing " << blocks << " blocks to " << cn;
Sami Tolvanen90221202014-12-09 16:39:47 +0000747
Tao Bao039f2da2016-11-22 16:29:50 -0800748 android::base::unique_fd fd(
749 TEMP_FAILURE_RETRY(ota_open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE)));
Sami Tolvanen90221202014-12-09 16:39:47 +0000750 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800751 PLOG(ERROR) << "failed to create \"" << fn << "\"";
Tao Bao0940fe12015-08-27 16:41:21 -0700752 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000753 }
754
Tianjie Xua946b9e2017-03-21 16:24:57 -0700755 if (fchown(fd, AID_SYSTEM, AID_SYSTEM) != 0) { // system user
756 PLOG(ERROR) << "failed to chown \"" << fn << "\"";
757 return -1;
758 }
759
Sami Tolvanen90221202014-12-09 16:39:47 +0000760 if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) {
Tao Bao0940fe12015-08-27 16:41:21 -0700761 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000762 }
763
Jed Estepa7b9a462015-12-15 16:04:53 -0800764 if (ota_fsync(fd) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700765 failure_type = kFsyncFailure;
Tao Bao039f2da2016-11-22 16:29:50 -0800766 PLOG(ERROR) << "fsync \"" << fn << "\" failed";
Tao Bao0940fe12015-08-27 16:41:21 -0700767 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000768 }
769
Tao Baoe6aa3322015-08-05 15:20:27 -0700770 if (rename(fn.c_str(), cn.c_str()) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800771 PLOG(ERROR) << "rename(\"" << fn << "\", \"" << cn << "\") failed";
Tao Bao0940fe12015-08-27 16:41:21 -0700772 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000773 }
774
Tao Bao0940fe12015-08-27 16:41:21 -0700775 std::string dname = GetStashFileName(base, "", "");
Elliott Hughesbcabd092016-03-22 20:19:22 -0700776 android::base::unique_fd dfd(TEMP_FAILURE_RETRY(ota_open(dname.c_str(),
777 O_RDONLY | O_DIRECTORY)));
Tao Baodc392262015-07-31 15:56:44 -0700778 if (dfd == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700779 failure_type = kFileOpenFailure;
Tao Bao039f2da2016-11-22 16:29:50 -0800780 PLOG(ERROR) << "failed to open \"" << dname << "\" failed";
Tao Bao0940fe12015-08-27 16:41:21 -0700781 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700782 }
783
Jed Estepa7b9a462015-12-15 16:04:53 -0800784 if (ota_fsync(dfd) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700785 failure_type = kFsyncFailure;
Tao Bao039f2da2016-11-22 16:29:50 -0800786 PLOG(ERROR) << "fsync \"" << dname << "\" failed";
Tao Bao0940fe12015-08-27 16:41:21 -0700787 return -1;
Tao Baodc392262015-07-31 15:56:44 -0700788 }
789
Tao Bao0940fe12015-08-27 16:41:21 -0700790 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000791}
792
793// Creates a directory for storing stash files and checks if the /cache partition
794// hash enough space for the expected amount of blocks we need to store. Returns
795// >0 if we created the directory, zero if it existed already, and <0 of failure.
796
Tao Bao51412212016-12-28 14:44:05 -0800797static int CreateStash(State* state, size_t maxblocks, const std::string& blockdev,
798 std::string& base) {
799 if (blockdev.empty()) {
800 return -1;
801 }
802
803 // Stash directory should be different for each partition to avoid conflicts
804 // when updating multiple partitions at the same time, so we use the hash of
805 // the block device name as the base directory
806 uint8_t digest[SHA_DIGEST_LENGTH];
807 SHA1(reinterpret_cast<const uint8_t*>(blockdev.data()), blockdev.size(), digest);
808 base = print_sha1(digest);
809
810 std::string dirname = GetStashFileName(base, "", "");
811 struct stat sb;
812 int res = stat(dirname.c_str(), &sb);
813 size_t max_stash_size = maxblocks * BLOCKSIZE;
814
815 if (res == -1 && errno != ENOENT) {
816 ErrorAbort(state, kStashCreationFailure, "stat \"%s\" failed: %s\n", dirname.c_str(),
817 strerror(errno));
818 return -1;
819 } else if (res != 0) {
820 LOG(INFO) << "creating stash " << dirname;
821 res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE);
822
823 if (res != 0) {
824 ErrorAbort(state, kStashCreationFailure, "mkdir \"%s\" failed: %s\n", dirname.c_str(),
825 strerror(errno));
826 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000827 }
828
Tianjie Xua946b9e2017-03-21 16:24:57 -0700829 if (chown(dirname.c_str(), AID_SYSTEM, AID_SYSTEM) != 0) { // system user
830 ErrorAbort(state, kStashCreationFailure, "chown \"%s\" failed: %s\n", dirname.c_str(),
831 strerror(errno));
832 return -1;
833 }
834
Tao Bao51412212016-12-28 14:44:05 -0800835 if (CacheSizeCheck(max_stash_size) != 0) {
836 ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu needed)\n",
837 max_stash_size);
838 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000839 }
840
Tao Bao51412212016-12-28 14:44:05 -0800841 return 1; // Created directory
842 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000843
Tao Bao51412212016-12-28 14:44:05 -0800844 LOG(INFO) << "using existing stash " << dirname;
Sami Tolvanen90221202014-12-09 16:39:47 +0000845
Tao Baoec8272f2017-03-15 17:39:01 -0700846 // If the directory already exists, calculate the space already allocated to stash files and check
847 // if there's enough for all required blocks. Delete any partially completed stash files first.
848 EnumerateStash(dirname, [](const std::string& fn) {
849 if (android::base::EndsWith(fn, ".partial")) {
850 DeleteFile(fn);
851 }
852 });
Sami Tolvanen90221202014-12-09 16:39:47 +0000853
Tao Bao51412212016-12-28 14:44:05 -0800854 size_t existing = 0;
Tao Baoec8272f2017-03-15 17:39:01 -0700855 EnumerateStash(dirname, [&existing](const std::string& fn) {
856 if (fn.empty()) return;
857 struct stat sb;
858 if (stat(fn.c_str(), &sb) == -1) {
859 PLOG(ERROR) << "stat \"" << fn << "\" failed";
860 return;
861 }
862 existing += static_cast<size_t>(sb.st_size);
863 });
Sami Tolvanen90221202014-12-09 16:39:47 +0000864
Tao Bao51412212016-12-28 14:44:05 -0800865 if (max_stash_size > existing) {
866 size_t needed = max_stash_size - existing;
867 if (CacheSizeCheck(needed) != 0) {
868 ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu more needed)\n",
869 needed);
870 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +0000871 }
Tao Bao51412212016-12-28 14:44:05 -0800872 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000873
Tao Bao51412212016-12-28 14:44:05 -0800874 return 0; // Using existing directory
Sami Tolvanen90221202014-12-09 16:39:47 +0000875}
876
Tao Baobaad2d42015-12-06 16:56:27 -0800877static int FreeStash(const std::string& base, const std::string& id) {
Tao Baoec8272f2017-03-15 17:39:01 -0700878 if (base.empty() || id.empty()) {
879 return -1;
880 }
Sami Tolvanen90221202014-12-09 16:39:47 +0000881
Tao Baoec8272f2017-03-15 17:39:01 -0700882 DeleteFile(GetStashFileName(base, id, ""));
Sami Tolvanen90221202014-12-09 16:39:47 +0000883
Tao Baoec8272f2017-03-15 17:39:01 -0700884 return 0;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700885}
886
Tao Baobf5b77d2017-03-30 16:57:29 -0700887// Source contains packed data, which we want to move to the locations given in locs in the dest
888// buffer. source and dest may be the same buffer.
Tao Bao612336d2015-08-27 16:41:21 -0700889static void MoveRange(std::vector<uint8_t>& dest, const RangeSet& locs,
Tao Baobf5b77d2017-03-30 16:57:29 -0700890 const std::vector<uint8_t>& source) {
891 const uint8_t* from = source.data();
892 uint8_t* to = dest.data();
893 size_t start = locs.blocks();
894 // Must do the movement backward.
895 for (auto it = locs.crbegin(); it != locs.crend(); it++) {
896 size_t blocks = it->second - it->first;
897 start -= blocks;
898 memmove(to + (it->first * BLOCKSIZE), from + (start * BLOCKSIZE), blocks * BLOCKSIZE);
899 }
Doug Zongker52ae67d2014-09-08 12:22:09 -0700900}
901
Tao Baod2aecd42017-03-23 14:43:44 -0700902/**
903 * We expect to parse the remainder of the parameter tokens as one of:
904 *
905 * <src_block_count> <src_range>
906 * (loads data from source image only)
907 *
908 * <src_block_count> - <[stash_id:stash_range] ...>
909 * (loads data from stashes only)
910 *
911 * <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...>
912 * (loads data from both source image and stashes)
913 *
914 * On return, params.buffer is filled with the loaded source data (rearranged and combined with
915 * stashed data as necessary). buffer may be reallocated if needed to accommodate the source data.
916 * tgt is the target RangeSet for detecting overlaps. Any stashes required are loaded using
917 * LoadStash.
918 */
919static int LoadSourceBlocks(CommandParameters& params, const RangeSet& tgt, size_t* src_blocks,
920 bool* overlap) {
921 CHECK(src_blocks != nullptr);
922 CHECK(overlap != nullptr);
Doug Zongker52ae67d2014-09-08 12:22:09 -0700923
Tao Baod2aecd42017-03-23 14:43:44 -0700924 // <src_block_count>
925 const std::string& token = params.tokens[params.cpos++];
926 if (!android::base::ParseUint(token, src_blocks)) {
927 LOG(ERROR) << "invalid src_block_count \"" << token << "\"";
928 return -1;
929 }
Tao Baobaad2d42015-12-06 16:56:27 -0800930
Tao Baod2aecd42017-03-23 14:43:44 -0700931 allocate(*src_blocks * BLOCKSIZE, params.buffer);
932
933 // "-" or <src_range> [<src_loc>]
934 if (params.tokens[params.cpos] == "-") {
935 // no source ranges, only stashes
936 params.cpos++;
937 } else {
Tao Bao8f237572017-03-26 13:36:49 -0700938 RangeSet src = RangeSet::Parse(params.tokens[params.cpos++]);
939 *overlap = src.Overlaps(tgt);
Tao Baod2aecd42017-03-23 14:43:44 -0700940
941 if (ReadBlocks(src, params.buffer, params.fd) == -1) {
942 return -1;
Tao Baobaad2d42015-12-06 16:56:27 -0800943 }
944
Tao Baod2aecd42017-03-23 14:43:44 -0700945 if (params.cpos >= params.tokens.size()) {
946 // no stashes, only source range
947 return 0;
Tao Baobaad2d42015-12-06 16:56:27 -0800948 }
Doug Zongker52ae67d2014-09-08 12:22:09 -0700949
Tao Bao8f237572017-03-26 13:36:49 -0700950 RangeSet locs = RangeSet::Parse(params.tokens[params.cpos++]);
Tao Baod2aecd42017-03-23 14:43:44 -0700951 MoveRange(params.buffer, locs, params.buffer);
952 }
Doug Zongker52ae67d2014-09-08 12:22:09 -0700953
Tao Baod2aecd42017-03-23 14:43:44 -0700954 // <[stash_id:stash_range]>
955 while (params.cpos < params.tokens.size()) {
956 // Each word is a an index into the stash table, a colon, and then a RangeSet describing where
957 // in the source block that stashed data should go.
958 std::vector<std::string> tokens = android::base::Split(params.tokens[params.cpos++], ":");
959 if (tokens.size() != 2) {
960 LOG(ERROR) << "invalid parameter";
961 return -1;
Doug Zongker52ae67d2014-09-08 12:22:09 -0700962 }
963
Tao Baod2aecd42017-03-23 14:43:44 -0700964 std::vector<uint8_t> stash;
965 if (LoadStash(params, tokens[0], false, nullptr, stash, true) == -1) {
966 // These source blocks will fail verification if used later, but we
967 // will let the caller decide if this is a fatal failure
968 LOG(ERROR) << "failed to load stash " << tokens[0];
969 continue;
Sami Tolvanen90221202014-12-09 16:39:47 +0000970 }
971
Tao Bao8f237572017-03-26 13:36:49 -0700972 RangeSet locs = RangeSet::Parse(tokens[1]);
Tao Baod2aecd42017-03-23 14:43:44 -0700973 MoveRange(params.buffer, locs, stash);
974 }
975
976 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +0000977}
978
Tao Bao33567772017-03-13 14:57:34 -0700979/**
980 * Do a source/target load for move/bsdiff/imgdiff in version 3.
981 *
982 * We expect to parse the remainder of the parameter tokens as one of:
983 *
984 * <tgt_range> <src_block_count> <src_range>
985 * (loads data from source image only)
986 *
987 * <tgt_range> <src_block_count> - <[stash_id:stash_range] ...>
988 * (loads data from stashes only)
989 *
990 * <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...>
991 * (loads data from both source image and stashes)
992 *
Tao Baod2aecd42017-03-23 14:43:44 -0700993 * 'onehash' tells whether to expect separate source and targe block hashes, or if they are both the
994 * same and only one hash should be expected. params.isunresumable will be set to true if block
Tao Bao33567772017-03-13 14:57:34 -0700995 * verification fails in a way that the update cannot be resumed anymore.
996 *
997 * If the function is unable to load the necessary blocks or their contents don't match the hashes,
998 * the return value is -1 and the command should be aborted.
999 *
1000 * If the return value is 1, the command has already been completed according to the contents of the
1001 * target blocks, and should not be performed again.
1002 *
1003 * If the return value is 0, source blocks have expected content and the command can be performed.
1004 */
Tao Baod2aecd42017-03-23 14:43:44 -07001005static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t* src_blocks,
1006 bool onehash, bool* overlap) {
1007 CHECK(src_blocks != nullptr);
1008 CHECK(overlap != nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +00001009
Tao Baod2aecd42017-03-23 14:43:44 -07001010 if (params.cpos >= params.tokens.size()) {
1011 LOG(ERROR) << "missing source hash";
Tao Bao0940fe12015-08-27 16:41:21 -07001012 return -1;
Tao Baod2aecd42017-03-23 14:43:44 -07001013 }
1014
1015 std::string srchash = params.tokens[params.cpos++];
1016 std::string tgthash;
1017
1018 if (onehash) {
1019 tgthash = srchash;
1020 } else {
1021 if (params.cpos >= params.tokens.size()) {
1022 LOG(ERROR) << "missing target hash";
1023 return -1;
1024 }
1025 tgthash = params.tokens[params.cpos++];
1026 }
1027
1028 // At least it needs to provide three parameters: <tgt_range>, <src_block_count> and
1029 // "-"/<src_range>.
1030 if (params.cpos + 2 >= params.tokens.size()) {
1031 LOG(ERROR) << "invalid parameters";
1032 return -1;
1033 }
1034
1035 // <tgt_range>
Tao Bao8f237572017-03-26 13:36:49 -07001036 tgt = RangeSet::Parse(params.tokens[params.cpos++]);
Tao Baod2aecd42017-03-23 14:43:44 -07001037
Tao Baobf5b77d2017-03-30 16:57:29 -07001038 std::vector<uint8_t> tgtbuffer(tgt.blocks() * BLOCKSIZE);
Tao Baod2aecd42017-03-23 14:43:44 -07001039 if (ReadBlocks(tgt, tgtbuffer, params.fd) == -1) {
1040 return -1;
1041 }
1042
1043 // Return now if target blocks already have expected content.
Tao Baobf5b77d2017-03-30 16:57:29 -07001044 if (VerifyBlocks(tgthash, tgtbuffer, tgt.blocks(), false) == 0) {
Tao Baod2aecd42017-03-23 14:43:44 -07001045 return 1;
1046 }
1047
1048 // Load source blocks.
1049 if (LoadSourceBlocks(params, tgt, src_blocks, overlap) == -1) {
1050 return -1;
1051 }
1052
1053 if (VerifyBlocks(srchash, params.buffer, *src_blocks, true) == 0) {
1054 // If source and target blocks overlap, stash the source blocks so we can
1055 // resume from possible write errors. In verify mode, we can skip stashing
1056 // because the source blocks won't be overwritten.
1057 if (*overlap && params.canwrite) {
1058 LOG(INFO) << "stashing " << *src_blocks << " overlapping blocks to " << srchash;
1059
1060 bool stash_exists = false;
1061 if (WriteStash(params.stashbase, srchash, *src_blocks, params.buffer, true,
1062 &stash_exists) != 0) {
1063 LOG(ERROR) << "failed to stash overlapping source blocks";
1064 return -1;
1065 }
1066
1067 params.stashed += *src_blocks;
1068 // Can be deleted when the write has completed.
1069 if (!stash_exists) {
1070 params.freestash = srchash;
1071 }
1072 }
1073
1074 // Source blocks have expected content, command can proceed.
1075 return 0;
1076 }
1077
1078 if (*overlap && LoadStash(params, srchash, true, nullptr, params.buffer, true) == 0) {
1079 // Overlapping source blocks were previously stashed, command can proceed. We are recovering
1080 // from an interrupted command, so we don't know if the stash can safely be deleted after this
1081 // command.
1082 return 0;
1083 }
1084
1085 // Valid source data not available, update cannot be resumed.
1086 LOG(ERROR) << "partition has unexpected contents";
1087 PrintHashForCorruptedSourceBlocks(params, params.buffer);
1088
1089 params.isunresumable = true;
1090
1091 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001092}
1093
Tao Bao0940fe12015-08-27 16:41:21 -07001094static int PerformCommandMove(CommandParameters& params) {
Tao Bao33567772017-03-13 14:57:34 -07001095 size_t blocks = 0;
1096 bool overlap = false;
1097 RangeSet tgt;
Tao Baod2aecd42017-03-23 14:43:44 -07001098 int status = LoadSrcTgtVersion3(params, tgt, &blocks, true, &overlap);
Sami Tolvanen90221202014-12-09 16:39:47 +00001099
Tao Bao33567772017-03-13 14:57:34 -07001100 if (status == -1) {
1101 LOG(ERROR) << "failed to read blocks for move";
1102 return -1;
1103 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001104
Tao Bao33567772017-03-13 14:57:34 -07001105 if (status == 0) {
1106 params.foundwrites = true;
1107 } else if (params.foundwrites) {
1108 LOG(WARNING) << "warning: commands executed out of order [" << params.cmdname << "]";
1109 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001110
Tao Bao33567772017-03-13 14:57:34 -07001111 if (params.canwrite) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001112 if (status == 0) {
Tao Bao33567772017-03-13 14:57:34 -07001113 LOG(INFO) << " moving " << blocks << " blocks";
1114
1115 if (WriteBlocks(tgt, params.buffer, params.fd) == -1) {
1116 return -1;
1117 }
1118 } else {
1119 LOG(INFO) << "skipping " << blocks << " already moved blocks";
Sami Tolvanen90221202014-12-09 16:39:47 +00001120 }
Tao Bao33567772017-03-13 14:57:34 -07001121 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001122
Tao Bao33567772017-03-13 14:57:34 -07001123 if (!params.freestash.empty()) {
1124 FreeStash(params.stashbase, params.freestash);
1125 params.freestash.clear();
1126 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001127
Tao Baobf5b77d2017-03-30 16:57:29 -07001128 params.written += tgt.blocks();
Sami Tolvanen90221202014-12-09 16:39:47 +00001129
Tao Bao33567772017-03-13 14:57:34 -07001130 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001131}
1132
Tao Bao0940fe12015-08-27 16:41:21 -07001133static int PerformCommandStash(CommandParameters& params) {
Tao Baobcf46492017-03-23 15:28:20 -07001134 // <stash_id> <src_range>
1135 if (params.cpos + 1 >= params.tokens.size()) {
1136 LOG(ERROR) << "missing id and/or src range fields in stash command";
1137 return -1;
1138 }
1139
1140 const std::string& id = params.tokens[params.cpos++];
1141 size_t blocks = 0;
1142 if (LoadStash(params, id, true, &blocks, params.buffer, false) == 0) {
1143 // Stash file already exists and has expected contents. Do not read from source again, as the
1144 // source may have been already overwritten during a previous attempt.
1145 return 0;
1146 }
1147
Tao Bao8f237572017-03-26 13:36:49 -07001148 RangeSet src = RangeSet::Parse(params.tokens[params.cpos++]);
Tao Baobcf46492017-03-23 15:28:20 -07001149
Tao Baobf5b77d2017-03-30 16:57:29 -07001150 allocate(src.blocks() * BLOCKSIZE, params.buffer);
Tao Baobcf46492017-03-23 15:28:20 -07001151 if (ReadBlocks(src, params.buffer, params.fd) == -1) {
1152 return -1;
1153 }
Tao Baobf5b77d2017-03-30 16:57:29 -07001154 blocks = src.blocks();
Tao Baobcf46492017-03-23 15:28:20 -07001155 stash_map[id] = src;
1156
1157 if (VerifyBlocks(id, params.buffer, blocks, true) != 0) {
1158 // Source blocks have unexpected contents. If we actually need this data later, this is an
1159 // unrecoverable error. However, the command that uses the data may have already completed
1160 // previously, so the possible failure will occur during source block verification.
1161 LOG(ERROR) << "failed to load source blocks for stash " << id;
1162 return 0;
1163 }
1164
1165 // In verify mode, we don't need to stash any blocks.
1166 if (!params.canwrite) {
1167 return 0;
1168 }
1169
1170 LOG(INFO) << "stashing " << blocks << " blocks to " << id;
1171 params.stashed += blocks;
1172 return WriteStash(params.stashbase, id, blocks, params.buffer, false, nullptr);
Sami Tolvanen90221202014-12-09 16:39:47 +00001173}
1174
Tao Bao0940fe12015-08-27 16:41:21 -07001175static int PerformCommandFree(CommandParameters& params) {
Tao Baobcf46492017-03-23 15:28:20 -07001176 // <stash_id>
1177 if (params.cpos >= params.tokens.size()) {
1178 LOG(ERROR) << "missing stash id in free command";
1179 return -1;
1180 }
Tao Baobaad2d42015-12-06 16:56:27 -08001181
Tao Baobcf46492017-03-23 15:28:20 -07001182 const std::string& id = params.tokens[params.cpos++];
1183 stash_map.erase(id);
Tianjie Xu7eca97e2016-03-22 18:08:12 -07001184
Tao Baobcf46492017-03-23 15:28:20 -07001185 if (params.createdstash || params.canwrite) {
1186 return FreeStash(params.stashbase, id);
1187 }
Tianjie Xu7eca97e2016-03-22 18:08:12 -07001188
Tao Baobcf46492017-03-23 15:28:20 -07001189 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001190}
1191
Tao Bao0940fe12015-08-27 16:41:21 -07001192static int PerformCommandZero(CommandParameters& params) {
Tao Baobf5b77d2017-03-30 16:57:29 -07001193 if (params.cpos >= params.tokens.size()) {
1194 LOG(ERROR) << "missing target blocks for zero";
1195 return -1;
1196 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001197
Tao Baobf5b77d2017-03-30 16:57:29 -07001198 RangeSet tgt = RangeSet::Parse(params.tokens[params.cpos++]);
1199
1200 LOG(INFO) << " zeroing " << tgt.blocks() << " blocks";
1201
1202 allocate(BLOCKSIZE, params.buffer);
1203 memset(params.buffer.data(), 0, BLOCKSIZE);
1204
1205 if (params.canwrite) {
1206 for (const auto& range : tgt) {
1207 off64_t offset = static_cast<off64_t>(range.first) * BLOCKSIZE;
1208 size_t size = (range.second - range.first) * BLOCKSIZE;
1209 if (!discard_blocks(params.fd, offset, size)) {
Tao Bao0940fe12015-08-27 16:41:21 -07001210 return -1;
Tao Baobf5b77d2017-03-30 16:57:29 -07001211 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001212
Tao Baobf5b77d2017-03-30 16:57:29 -07001213 if (!check_lseek(params.fd, offset, SEEK_SET)) {
1214 return -1;
1215 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001216
Tao Baobf5b77d2017-03-30 16:57:29 -07001217 for (size_t j = range.first; j < range.second; ++j) {
1218 if (write_all(params.fd, params.buffer, BLOCKSIZE) == -1) {
1219 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001220 }
Tao Baobf5b77d2017-03-30 16:57:29 -07001221 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001222 }
Tao Baobf5b77d2017-03-30 16:57:29 -07001223 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001224
Tao Baobf5b77d2017-03-30 16:57:29 -07001225 if (params.cmdname[0] == 'z') {
1226 // Update only for the zero command, as the erase command will call
1227 // this if DEBUG_ERASE is defined.
1228 params.written += tgt.blocks();
1229 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001230
Tao Baobf5b77d2017-03-30 16:57:29 -07001231 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001232}
1233
Tao Bao0940fe12015-08-27 16:41:21 -07001234static int PerformCommandNew(CommandParameters& params) {
Tao Bao60a70af2017-03-26 14:03:52 -07001235 if (params.cpos >= params.tokens.size()) {
1236 LOG(ERROR) << "missing target blocks for new";
1237 return -1;
1238 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001239
Tao Bao8f237572017-03-26 13:36:49 -07001240 RangeSet tgt = RangeSet::Parse(params.tokens[params.cpos++]);
Tao Bao60a70af2017-03-26 14:03:52 -07001241
1242 if (params.canwrite) {
Tao Baobf5b77d2017-03-30 16:57:29 -07001243 LOG(INFO) << " writing " << tgt.blocks() << " blocks of new data";
Tao Bao60a70af2017-03-26 14:03:52 -07001244
Tao Bao60a70af2017-03-26 14:03:52 -07001245 pthread_mutex_lock(&params.nti.mu);
Tianjie Xu6ed175d2017-07-18 11:29:40 -07001246 params.nti.writer = std::make_unique<RangeSinkWriter>(params.fd, tgt);
Tao Bao60a70af2017-03-26 14:03:52 -07001247 pthread_cond_broadcast(&params.nti.cv);
1248
1249 while (params.nti.writer != nullptr) {
Tianjie Xu3a8d98d2017-04-03 20:01:17 -07001250 if (!params.nti.receiver_available) {
1251 LOG(ERROR) << "missing " << (tgt.blocks() * BLOCKSIZE - params.nti.writer->BytesWritten())
1252 << " bytes of new data";
1253 pthread_mutex_unlock(&params.nti.mu);
1254 return -1;
1255 }
Tao Bao60a70af2017-03-26 14:03:52 -07001256 pthread_cond_wait(&params.nti.cv, &params.nti.mu);
Sami Tolvanen90221202014-12-09 16:39:47 +00001257 }
1258
Tao Bao60a70af2017-03-26 14:03:52 -07001259 pthread_mutex_unlock(&params.nti.mu);
1260 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001261
Tao Baobf5b77d2017-03-30 16:57:29 -07001262 params.written += tgt.blocks();
Sami Tolvanen90221202014-12-09 16:39:47 +00001263
Tao Bao60a70af2017-03-26 14:03:52 -07001264 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001265}
1266
Tao Bao0940fe12015-08-27 16:41:21 -07001267static int PerformCommandDiff(CommandParameters& params) {
Tao Baoc0e1c462017-02-01 10:20:10 -08001268 // <offset> <length>
1269 if (params.cpos + 1 >= params.tokens.size()) {
1270 LOG(ERROR) << "missing patch offset or length for " << params.cmdname;
1271 return -1;
1272 }
Tao Bao0940fe12015-08-27 16:41:21 -07001273
Tao Baoc0e1c462017-02-01 10:20:10 -08001274 size_t offset;
1275 if (!android::base::ParseUint(params.tokens[params.cpos++], &offset)) {
1276 LOG(ERROR) << "invalid patch offset";
1277 return -1;
1278 }
Tao Bao0940fe12015-08-27 16:41:21 -07001279
Tao Baoc0e1c462017-02-01 10:20:10 -08001280 size_t len;
1281 if (!android::base::ParseUint(params.tokens[params.cpos++], &len)) {
1282 LOG(ERROR) << "invalid patch len";
1283 return -1;
1284 }
Tao Bao0940fe12015-08-27 16:41:21 -07001285
Tao Baoc0e1c462017-02-01 10:20:10 -08001286 RangeSet tgt;
1287 size_t blocks = 0;
1288 bool overlap = false;
1289 int status = LoadSrcTgtVersion3(params, tgt, &blocks, false, &overlap);
Tao Bao0940fe12015-08-27 16:41:21 -07001290
Tao Baoc0e1c462017-02-01 10:20:10 -08001291 if (status == -1) {
1292 LOG(ERROR) << "failed to read blocks for diff";
1293 return -1;
1294 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001295
Tao Baoc0e1c462017-02-01 10:20:10 -08001296 if (status == 0) {
1297 params.foundwrites = true;
1298 } else if (params.foundwrites) {
1299 LOG(WARNING) << "warning: commands executed out of order [" << params.cmdname << "]";
1300 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001301
Tao Baoc0e1c462017-02-01 10:20:10 -08001302 if (params.canwrite) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001303 if (status == 0) {
Tao Baobf5b77d2017-03-30 16:57:29 -07001304 LOG(INFO) << "patching " << blocks << " blocks to " << tgt.blocks();
Tao Baoc0e1c462017-02-01 10:20:10 -08001305 Value patch_value(
1306 VAL_BLOB, std::string(reinterpret_cast<const char*>(params.patch_start + offset), len));
Sami Tolvanen90221202014-12-09 16:39:47 +00001307
Tao Bao60a70af2017-03-26 14:03:52 -07001308 RangeSinkWriter writer(params.fd, tgt);
Tao Baoc0e1c462017-02-01 10:20:10 -08001309 if (params.cmdname[0] == 'i') { // imgdiff
Tao Bao60a70af2017-03-26 14:03:52 -07001310 if (ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value,
1311 std::bind(&RangeSinkWriter::Write, &writer, std::placeholders::_1,
1312 std::placeholders::_2),
1313 nullptr, nullptr) != 0) {
Tao Baoc0e1c462017-02-01 10:20:10 -08001314 LOG(ERROR) << "Failed to apply image patch.";
Tianjie Xu69575552017-05-16 15:51:46 -07001315 failure_type = kPatchApplicationFailure;
Tao Baoc0e1c462017-02-01 10:20:10 -08001316 return -1;
Sami Tolvanen90221202014-12-09 16:39:47 +00001317 }
Tao Baoc0e1c462017-02-01 10:20:10 -08001318 } else {
Tao Bao60a70af2017-03-26 14:03:52 -07001319 if (ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value, 0,
1320 std::bind(&RangeSinkWriter::Write, &writer, std::placeholders::_1,
1321 std::placeholders::_2),
1322 nullptr) != 0) {
Tao Baoc0e1c462017-02-01 10:20:10 -08001323 LOG(ERROR) << "Failed to apply bsdiff patch.";
Tianjie Xu69575552017-05-16 15:51:46 -07001324 failure_type = kPatchApplicationFailure;
Tao Baoc0e1c462017-02-01 10:20:10 -08001325 return -1;
1326 }
1327 }
1328
1329 // We expect the output of the patcher to fill the tgt ranges exactly.
Tao Bao60a70af2017-03-26 14:03:52 -07001330 if (!writer.Finished()) {
Tao Baoc0e1c462017-02-01 10:20:10 -08001331 LOG(ERROR) << "range sink underrun?";
1332 }
1333 } else {
Tao Baobf5b77d2017-03-30 16:57:29 -07001334 LOG(INFO) << "skipping " << blocks << " blocks already patched to " << tgt.blocks() << " ["
Tao Baoc0e1c462017-02-01 10:20:10 -08001335 << params.cmdline << "]";
Sami Tolvanen90221202014-12-09 16:39:47 +00001336 }
Tao Baoc0e1c462017-02-01 10:20:10 -08001337 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001338
Tao Baoc0e1c462017-02-01 10:20:10 -08001339 if (!params.freestash.empty()) {
1340 FreeStash(params.stashbase, params.freestash);
1341 params.freestash.clear();
1342 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001343
Tao Baobf5b77d2017-03-30 16:57:29 -07001344 params.written += tgt.blocks();
Sami Tolvanen90221202014-12-09 16:39:47 +00001345
Tao Baoc0e1c462017-02-01 10:20:10 -08001346 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001347}
1348
Tao Bao0940fe12015-08-27 16:41:21 -07001349static int PerformCommandErase(CommandParameters& params) {
Tao Baobf5b77d2017-03-30 16:57:29 -07001350 if (DEBUG_ERASE) {
1351 return PerformCommandZero(params);
1352 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001353
Tao Baobf5b77d2017-03-30 16:57:29 -07001354 struct stat sb;
1355 if (fstat(params.fd, &sb) == -1) {
1356 PLOG(ERROR) << "failed to fstat device to erase";
1357 return -1;
1358 }
1359
1360 if (!S_ISBLK(sb.st_mode)) {
1361 LOG(ERROR) << "not a block device; skipping erase";
1362 return -1;
1363 }
1364
1365 if (params.cpos >= params.tokens.size()) {
1366 LOG(ERROR) << "missing target blocks for erase";
1367 return -1;
1368 }
1369
1370 RangeSet tgt = RangeSet::Parse(params.tokens[params.cpos++]);
1371
1372 if (params.canwrite) {
1373 LOG(INFO) << " erasing " << tgt.blocks() << " blocks";
1374
1375 for (const auto& range : tgt) {
1376 uint64_t blocks[2];
1377 // offset in bytes
1378 blocks[0] = range.first * static_cast<uint64_t>(BLOCKSIZE);
1379 // length in bytes
1380 blocks[1] = (range.second - range.first) * static_cast<uint64_t>(BLOCKSIZE);
1381
1382 if (ioctl(params.fd, BLKDISCARD, &blocks) == -1) {
1383 PLOG(ERROR) << "BLKDISCARD ioctl failed";
Tao Bao0940fe12015-08-27 16:41:21 -07001384 return -1;
Tao Baobf5b77d2017-03-30 16:57:29 -07001385 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001386 }
Tao Baobf5b77d2017-03-30 16:57:29 -07001387 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001388
Tao Baobf5b77d2017-03-30 16:57:29 -07001389 return 0;
Sami Tolvanen90221202014-12-09 16:39:47 +00001390}
1391
1392// Definitions for transfer list command functions
Tao Bao0940fe12015-08-27 16:41:21 -07001393typedef int (*CommandFunction)(CommandParameters&);
Sami Tolvanen90221202014-12-09 16:39:47 +00001394
Tao Bao612336d2015-08-27 16:41:21 -07001395struct Command {
Sami Tolvanen90221202014-12-09 16:39:47 +00001396 const char* name;
1397 CommandFunction f;
Tao Bao612336d2015-08-27 16:41:21 -07001398};
Sami Tolvanen90221202014-12-09 16:39:47 +00001399
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001400// args:
1401// - block device (or file) to modify in-place
1402// - transfer list (blob)
1403// - new data stream (filename within package.zip)
1404// - patch stream (filename within package.zip, must be uncompressed)
1405
Tianjie Xuc4447322017-03-06 14:44:59 -08001406static Value* PerformBlockImageUpdate(const char* name, State* state,
1407 const std::vector<std::unique_ptr<Expr>>& argv,
1408 const Command* commands, size_t cmdcount, bool dryrun) {
Tao Bao33567772017-03-13 14:57:34 -07001409 CommandParameters params = {};
1410 params.canwrite = !dryrun;
Sami Tolvanen90221202014-12-09 16:39:47 +00001411
Tao Bao33567772017-03-13 14:57:34 -07001412 LOG(INFO) << "performing " << (dryrun ? "verification" : "update");
1413 if (state->is_retry) {
1414 is_retry = true;
1415 LOG(INFO) << "This update is a retry.";
1416 }
1417 if (argv.size() != 4) {
1418 ErrorAbort(state, kArgsParsingFailure, "block_image_update expects 4 arguments, got %zu",
1419 argv.size());
1420 return StringValue("");
1421 }
1422
1423 std::vector<std::unique_ptr<Value>> args;
1424 if (!ReadValueArgs(state, argv, &args)) {
1425 return nullptr;
1426 }
1427
Tao Baoc97edcb2017-03-31 01:18:13 -07001428 const std::unique_ptr<Value>& blockdev_filename = args[0];
1429 const std::unique_ptr<Value>& transfer_list_value = args[1];
1430 const std::unique_ptr<Value>& new_data_fn = args[2];
1431 const std::unique_ptr<Value>& patch_data_fn = args[3];
Tao Bao33567772017-03-13 14:57:34 -07001432
1433 if (blockdev_filename->type != VAL_STRING) {
1434 ErrorAbort(state, kArgsParsingFailure, "blockdev_filename argument to %s must be string", name);
1435 return StringValue("");
1436 }
1437 if (transfer_list_value->type != VAL_BLOB) {
1438 ErrorAbort(state, kArgsParsingFailure, "transfer_list argument to %s must be blob", name);
1439 return StringValue("");
1440 }
1441 if (new_data_fn->type != VAL_STRING) {
1442 ErrorAbort(state, kArgsParsingFailure, "new_data_fn argument to %s must be string", name);
1443 return StringValue("");
1444 }
1445 if (patch_data_fn->type != VAL_STRING) {
1446 ErrorAbort(state, kArgsParsingFailure, "patch_data_fn argument to %s must be string", name);
1447 return StringValue("");
1448 }
1449
1450 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
1451 if (ui == nullptr) {
1452 return StringValue("");
1453 }
1454
1455 FILE* cmd_pipe = ui->cmd_pipe;
1456 ZipArchiveHandle za = ui->package_zip;
1457
1458 if (cmd_pipe == nullptr || za == nullptr) {
1459 return StringValue("");
1460 }
1461
1462 ZipString path_data(patch_data_fn->data.c_str());
1463 ZipEntry patch_entry;
1464 if (FindEntry(za, path_data, &patch_entry) != 0) {
1465 LOG(ERROR) << name << "(): no file \"" << patch_data_fn->data << "\" in package";
1466 return StringValue("");
1467 }
1468
1469 params.patch_start = ui->package_zip_addr + patch_entry.offset;
1470 ZipString new_data(new_data_fn->data.c_str());
1471 ZipEntry new_entry;
1472 if (FindEntry(za, new_data, &new_entry) != 0) {
1473 LOG(ERROR) << name << "(): no file \"" << new_data_fn->data << "\" in package";
1474 return StringValue("");
1475 }
1476
1477 params.fd.reset(TEMP_FAILURE_RETRY(ota_open(blockdev_filename->data.c_str(), O_RDWR)));
1478 if (params.fd == -1) {
1479 PLOG(ERROR) << "open \"" << blockdev_filename->data << "\" failed";
1480 return StringValue("");
1481 }
1482
1483 if (params.canwrite) {
1484 params.nti.za = za;
1485 params.nti.entry = new_entry;
Tianjie Xu107a34f2017-06-29 17:04:21 -07001486 params.nti.brotli_compressed = android::base::EndsWith(new_data_fn->data, ".br");
1487 if (params.nti.brotli_compressed) {
1488 // Initialize brotli decoder state.
1489 params.nti.brotli_decoder_state = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
1490 }
Tianjie Xu3a8d98d2017-04-03 20:01:17 -07001491 params.nti.receiver_available = true;
Tao Bao33567772017-03-13 14:57:34 -07001492
1493 pthread_mutex_init(&params.nti.mu, nullptr);
1494 pthread_cond_init(&params.nti.cv, nullptr);
1495 pthread_attr_t attr;
1496 pthread_attr_init(&attr);
1497 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1498
1499 int error = pthread_create(&params.thread, &attr, unzip_new_data, &params.nti);
1500 if (error != 0) {
1501 PLOG(ERROR) << "pthread_create failed";
1502 return StringValue("");
Tianjie Xu7ce287d2016-05-31 09:29:49 -07001503 }
Tao Bao33567772017-03-13 14:57:34 -07001504 }
1505
1506 std::vector<std::string> lines = android::base::Split(transfer_list_value->data, "\n");
1507 if (lines.size() < 2) {
1508 ErrorAbort(state, kArgsParsingFailure, "too few lines in the transfer list [%zd]\n",
1509 lines.size());
1510 return StringValue("");
1511 }
1512
1513 // First line in transfer list is the version number.
1514 if (!android::base::ParseInt(lines[0], &params.version, 3, 4)) {
1515 LOG(ERROR) << "unexpected transfer list version [" << lines[0] << "]";
1516 return StringValue("");
1517 }
1518
1519 LOG(INFO) << "blockimg version is " << params.version;
1520
1521 // Second line in transfer list is the total number of blocks we expect to write.
1522 size_t total_blocks;
1523 if (!android::base::ParseUint(lines[1], &total_blocks)) {
1524 ErrorAbort(state, kArgsParsingFailure, "unexpected block count [%s]\n", lines[1].c_str());
1525 return StringValue("");
1526 }
1527
1528 if (total_blocks == 0) {
1529 return StringValue("t");
1530 }
1531
1532 size_t start = 2;
1533 if (lines.size() < 4) {
1534 ErrorAbort(state, kArgsParsingFailure, "too few lines in the transfer list [%zu]\n",
1535 lines.size());
1536 return StringValue("");
1537 }
1538
1539 // Third line is how many stash entries are needed simultaneously.
1540 LOG(INFO) << "maximum stash entries " << lines[2];
1541
1542 // Fourth line is the maximum number of blocks that will be stashed simultaneously
1543 size_t stash_max_blocks;
1544 if (!android::base::ParseUint(lines[3], &stash_max_blocks)) {
1545 ErrorAbort(state, kArgsParsingFailure, "unexpected maximum stash blocks [%s]\n",
1546 lines[3].c_str());
1547 return StringValue("");
1548 }
1549
1550 int res = CreateStash(state, stash_max_blocks, blockdev_filename->data, params.stashbase);
1551 if (res == -1) {
1552 return StringValue("");
1553 }
1554
1555 params.createdstash = res;
1556
1557 start += 2;
1558
1559 // Build a map of the available commands
1560 std::unordered_map<std::string, const Command*> cmd_map;
1561 for (size_t i = 0; i < cmdcount; ++i) {
1562 if (cmd_map.find(commands[i].name) != cmd_map.end()) {
1563 LOG(ERROR) << "Error: command [" << commands[i].name << "] already exists in the cmd map.";
1564 return StringValue(strdup(""));
1565 }
1566 cmd_map[commands[i].name] = &commands[i];
1567 }
1568
1569 int rc = -1;
1570
1571 // Subsequent lines are all individual transfer commands
1572 for (auto it = lines.cbegin() + start; it != lines.cend(); it++) {
1573 const std::string& line(*it);
1574 if (line.empty()) continue;
1575
1576 params.tokens = android::base::Split(line, " ");
1577 params.cpos = 0;
1578 params.cmdname = params.tokens[params.cpos++].c_str();
1579 params.cmdline = line.c_str();
1580
1581 if (cmd_map.find(params.cmdname) == cmd_map.end()) {
1582 LOG(ERROR) << "unexpected command [" << params.cmdname << "]";
1583 goto pbiudone;
Tianjie Xuc4447322017-03-06 14:44:59 -08001584 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001585
Tao Bao33567772017-03-13 14:57:34 -07001586 const Command* cmd = cmd_map[params.cmdname];
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001587
Tao Bao33567772017-03-13 14:57:34 -07001588 if (cmd->f != nullptr && cmd->f(params) == -1) {
1589 LOG(ERROR) << "failed to execute command [" << line << "]";
1590 goto pbiudone;
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001591 }
1592
Sami Tolvanen90221202014-12-09 16:39:47 +00001593 if (params.canwrite) {
Tao Bao33567772017-03-13 14:57:34 -07001594 if (ota_fsync(params.fd) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001595 failure_type = kFsyncFailure;
Tao Bao039f2da2016-11-22 16:29:50 -08001596 PLOG(ERROR) << "fsync failed";
Tao Bao33567772017-03-13 14:57:34 -07001597 goto pbiudone;
1598 }
1599 fprintf(cmd_pipe, "set_progress %.4f\n", static_cast<double>(params.written) / total_blocks);
1600 fflush(cmd_pipe);
Sami Tolvanen90221202014-12-09 16:39:47 +00001601 }
Tao Bao33567772017-03-13 14:57:34 -07001602 }
Sami Tolvanen90221202014-12-09 16:39:47 +00001603
Tao Bao33567772017-03-13 14:57:34 -07001604 rc = 0;
Tianjie Xu16255832016-04-30 11:49:59 -07001605
Tao Bao33567772017-03-13 14:57:34 -07001606pbiudone:
Tianjie Xu5450c842017-10-18 13:15:21 -07001607 if (params.canwrite) {
1608 pthread_mutex_lock(&params.nti.mu);
1609 if (params.nti.receiver_available) {
1610 LOG(WARNING) << "new data receiver is still available after executing all commands.";
1611 }
1612 params.nti.receiver_available = false;
1613 pthread_cond_broadcast(&params.nti.cv);
1614 pthread_mutex_unlock(&params.nti.mu);
1615 int ret = pthread_join(params.thread, nullptr);
1616 if (ret != 0) {
1617 LOG(WARNING) << "pthread join returned with " << strerror(ret);
1618 }
1619
1620 if (rc == 0) {
1621 LOG(INFO) << "wrote " << params.written << " blocks; expected " << total_blocks;
1622 LOG(INFO) << "stashed " << params.stashed << " blocks";
1623 LOG(INFO) << "max alloc needed was " << params.buffer.size();
1624
1625 const char* partition = strrchr(blockdev_filename->data.c_str(), '/');
1626 if (partition != nullptr && *(partition + 1) != 0) {
1627 fprintf(cmd_pipe, "log bytes_written_%s: %zu\n", partition + 1, params.written * BLOCKSIZE);
1628 fprintf(cmd_pipe, "log bytes_stashed_%s: %zu\n", partition + 1, params.stashed * BLOCKSIZE);
1629 fflush(cmd_pipe);
1630 }
1631 // Delete stash only after successfully completing the update, as it may contain blocks needed
1632 // to complete the update later.
1633 DeleteStash(params.stashbase);
1634 }
1635
1636 pthread_mutex_destroy(&params.nti.mu);
1637 pthread_cond_destroy(&params.nti.cv);
1638 } else if (rc == 0) {
1639 LOG(INFO) << "verified partition contents; update may be resumed";
1640 }
1641
Tao Bao33567772017-03-13 14:57:34 -07001642 if (ota_fsync(params.fd) == -1) {
1643 failure_type = kFsyncFailure;
1644 PLOG(ERROR) << "fsync failed";
1645 }
1646 // params.fd will be automatically closed because it's a unique_fd.
1647
Tianjie Xu107a34f2017-06-29 17:04:21 -07001648 if (params.nti.brotli_decoder_state != nullptr) {
1649 BrotliDecoderDestroyInstance(params.nti.brotli_decoder_state);
1650 }
1651
Tao Bao33567772017-03-13 14:57:34 -07001652 // Only delete the stash if the update cannot be resumed, or it's a verification run and we
1653 // created the stash.
1654 if (params.isunresumable || (!params.canwrite && params.createdstash)) {
1655 DeleteStash(params.stashbase);
1656 }
1657
1658 if (failure_type != kNoCause && state->cause_code == kNoCause) {
1659 state->cause_code = failure_type;
1660 }
1661
1662 return StringValue(rc == 0 ? "t" : "");
Sami Tolvanen90221202014-12-09 16:39:47 +00001663}
1664
Tao Bao33567772017-03-13 14:57:34 -07001665/**
1666 * The transfer list is a text file containing commands to transfer data from one place to another
1667 * on the target partition. We parse it and execute the commands in order:
1668 *
1669 * zero [rangeset]
1670 * - Fill the indicated blocks with zeros.
1671 *
1672 * new [rangeset]
1673 * - Fill the blocks with data read from the new_data file.
1674 *
1675 * erase [rangeset]
1676 * - Mark the given blocks as empty.
1677 *
1678 * move <...>
1679 * bsdiff <patchstart> <patchlen> <...>
1680 * imgdiff <patchstart> <patchlen> <...>
1681 * - Read the source blocks, apply a patch (or not in the case of move), write result to target
1682 * blocks. bsdiff or imgdiff specifies the type of patch; move means no patch at all.
1683 *
1684 * See the comments in LoadSrcTgtVersion3() for a description of the <...> format.
1685 *
1686 * stash <stash_id> <src_range>
1687 * - Load the given source range and stash the data in the given slot of the stash table.
1688 *
1689 * free <stash_id>
1690 * - Free the given stash data.
1691 *
1692 * The creator of the transfer list will guarantee that no block is read (ie, used as the source for
1693 * a patch or move) after it has been written.
1694 *
1695 * The creator will guarantee that a given stash is loaded (with a stash command) before it's used
1696 * in a move/bsdiff/imgdiff command.
1697 *
1698 * Within one command the source and target ranges may overlap so in general we need to read the
1699 * entire source into memory before writing anything to the target blocks.
1700 *
1701 * All the patch data is concatenated into one patch_data file in the update package. It must be
1702 * stored uncompressed because we memory-map it in directly from the archive. (Since patches are
1703 * already compressed, we lose very little by not compressing their concatenation.)
1704 *
1705 * Commands that read data from the partition (i.e. move/bsdiff/imgdiff/stash) have one or more
1706 * additional hashes before the range parameters, which are used to check if the command has already
1707 * been completed and verify the integrity of the source data.
1708 */
Tianjie Xuc4447322017-03-06 14:44:59 -08001709Value* BlockImageVerifyFn(const char* name, State* state,
1710 const std::vector<std::unique_ptr<Expr>>& argv) {
Tao Bao0940fe12015-08-27 16:41:21 -07001711 // Commands which are not tested are set to nullptr to skip them completely
Sami Tolvanen90221202014-12-09 16:39:47 +00001712 const Command commands[] = {
1713 { "bsdiff", PerformCommandDiff },
Tao Bao0940fe12015-08-27 16:41:21 -07001714 { "erase", nullptr },
Sami Tolvanen90221202014-12-09 16:39:47 +00001715 { "free", PerformCommandFree },
1716 { "imgdiff", PerformCommandDiff },
1717 { "move", PerformCommandMove },
Tao Bao0940fe12015-08-27 16:41:21 -07001718 { "new", nullptr },
Sami Tolvanen90221202014-12-09 16:39:47 +00001719 { "stash", PerformCommandStash },
Tao Bao0940fe12015-08-27 16:41:21 -07001720 { "zero", nullptr }
Sami Tolvanen90221202014-12-09 16:39:47 +00001721 };
1722
1723 // Perform a dry run without writing to test if an update can proceed
Tianjie Xuc4447322017-03-06 14:44:59 -08001724 return PerformBlockImageUpdate(name, state, argv, commands,
Tao Baoe6aa3322015-08-05 15:20:27 -07001725 sizeof(commands) / sizeof(commands[0]), true);
Sami Tolvanen90221202014-12-09 16:39:47 +00001726}
1727
Tianjie Xuc4447322017-03-06 14:44:59 -08001728Value* BlockImageUpdateFn(const char* name, State* state,
1729 const std::vector<std::unique_ptr<Expr>>& argv) {
Sami Tolvanen90221202014-12-09 16:39:47 +00001730 const Command commands[] = {
1731 { "bsdiff", PerformCommandDiff },
1732 { "erase", PerformCommandErase },
1733 { "free", PerformCommandFree },
1734 { "imgdiff", PerformCommandDiff },
1735 { "move", PerformCommandMove },
1736 { "new", PerformCommandNew },
1737 { "stash", PerformCommandStash },
1738 { "zero", PerformCommandZero }
1739 };
1740
Tianjie Xuc4447322017-03-06 14:44:59 -08001741 return PerformBlockImageUpdate(name, state, argv, commands,
Tao Baoe6aa3322015-08-05 15:20:27 -07001742 sizeof(commands) / sizeof(commands[0]), false);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001743}
1744
Tianjie Xuc4447322017-03-06 14:44:59 -08001745Value* RangeSha1Fn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
Tao Baoc97edcb2017-03-31 01:18:13 -07001746 if (argv.size() != 2) {
1747 ErrorAbort(state, kArgsParsingFailure, "range_sha1 expects 2 arguments, got %zu", argv.size());
1748 return StringValue("");
1749 }
1750
1751 std::vector<std::unique_ptr<Value>> args;
1752 if (!ReadValueArgs(state, argv, &args)) {
1753 return nullptr;
1754 }
1755
1756 const std::unique_ptr<Value>& blockdev_filename = args[0];
1757 const std::unique_ptr<Value>& ranges = args[1];
1758
1759 if (blockdev_filename->type != VAL_STRING) {
1760 ErrorAbort(state, kArgsParsingFailure, "blockdev_filename argument to %s must be string", name);
1761 return StringValue("");
1762 }
1763 if (ranges->type != VAL_STRING) {
1764 ErrorAbort(state, kArgsParsingFailure, "ranges argument to %s must be string", name);
1765 return StringValue("");
1766 }
1767
1768 android::base::unique_fd fd(ota_open(blockdev_filename->data.c_str(), O_RDWR));
1769 if (fd == -1) {
1770 ErrorAbort(state, kFileOpenFailure, "open \"%s\" failed: %s", blockdev_filename->data.c_str(),
1771 strerror(errno));
1772 return StringValue("");
1773 }
1774
1775 RangeSet rs = RangeSet::Parse(ranges->data);
1776
1777 SHA_CTX ctx;
1778 SHA1_Init(&ctx);
1779
1780 std::vector<uint8_t> buffer(BLOCKSIZE);
Tao Baobf5b77d2017-03-30 16:57:29 -07001781 for (const auto& range : rs) {
1782 if (!check_lseek(fd, static_cast<off64_t>(range.first) * BLOCKSIZE, SEEK_SET)) {
Tao Baoc97edcb2017-03-31 01:18:13 -07001783 ErrorAbort(state, kLseekFailure, "failed to seek %s: %s", blockdev_filename->data.c_str(),
1784 strerror(errno));
1785 return StringValue("");
1786 }
1787
Tao Baobf5b77d2017-03-30 16:57:29 -07001788 for (size_t j = range.first; j < range.second; ++j) {
Tao Baoc97edcb2017-03-31 01:18:13 -07001789 if (read_all(fd, buffer, BLOCKSIZE) == -1) {
1790 ErrorAbort(state, kFreadFailure, "failed to read %s: %s", blockdev_filename->data.c_str(),
1791 strerror(errno));
Tianjie Xuc4447322017-03-06 14:44:59 -08001792 return StringValue("");
Tao Baoc97edcb2017-03-31 01:18:13 -07001793 }
1794
1795 SHA1_Update(&ctx, buffer.data(), BLOCKSIZE);
Tianjie Xuc4447322017-03-06 14:44:59 -08001796 }
Tao Baoc97edcb2017-03-31 01:18:13 -07001797 }
1798 uint8_t digest[SHA_DIGEST_LENGTH];
1799 SHA1_Final(digest, &ctx);
Tianjie Xuc4447322017-03-06 14:44:59 -08001800
Tao Baoc97edcb2017-03-31 01:18:13 -07001801 return StringValue(print_sha1(digest));
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001802}
1803
Tianjie Xu57bed6d2015-12-15 11:47:30 -08001804// This function checks if a device has been remounted R/W prior to an incremental
1805// OTA update. This is an common cause of update abortion. The function reads the
1806// 1st block of each partition and check for mounting time/count. It return string "t"
1807// if executes successfully and an empty string otherwise.
1808
Tianjie Xuc4447322017-03-06 14:44:59 -08001809Value* CheckFirstBlockFn(const char* name, State* state,
1810 const std::vector<std::unique_ptr<Expr>>& argv) {
Tao Baoc97edcb2017-03-31 01:18:13 -07001811 if (argv.size() != 1) {
1812 ErrorAbort(state, kArgsParsingFailure, "check_first_block expects 1 argument, got %zu",
1813 argv.size());
1814 return StringValue("");
1815 }
Tianjie Xuc4447322017-03-06 14:44:59 -08001816
Tao Baoc97edcb2017-03-31 01:18:13 -07001817 std::vector<std::unique_ptr<Value>> args;
1818 if (!ReadValueArgs(state, argv, &args)) {
1819 return nullptr;
1820 }
Tianjie Xu57bed6d2015-12-15 11:47:30 -08001821
Tao Baoc97edcb2017-03-31 01:18:13 -07001822 const std::unique_ptr<Value>& arg_filename = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001823
Tao Baoc97edcb2017-03-31 01:18:13 -07001824 if (arg_filename->type != VAL_STRING) {
1825 ErrorAbort(state, kArgsParsingFailure, "filename argument to %s must be string", name);
1826 return StringValue("");
1827 }
Tianjie Xu57bed6d2015-12-15 11:47:30 -08001828
Tao Baoc97edcb2017-03-31 01:18:13 -07001829 android::base::unique_fd fd(ota_open(arg_filename->data.c_str(), O_RDONLY));
1830 if (fd == -1) {
1831 ErrorAbort(state, kFileOpenFailure, "open \"%s\" failed: %s", arg_filename->data.c_str(),
1832 strerror(errno));
1833 return StringValue("");
1834 }
Tianjie Xu57bed6d2015-12-15 11:47:30 -08001835
Tao Baobf5b77d2017-03-30 16:57:29 -07001836 RangeSet blk0(std::vector<Range>{ Range{ 0, 1 } });
Tao Baoc97edcb2017-03-31 01:18:13 -07001837 std::vector<uint8_t> block0_buffer(BLOCKSIZE);
Tianjie Xu57bed6d2015-12-15 11:47:30 -08001838
Tao Baoc97edcb2017-03-31 01:18:13 -07001839 if (ReadBlocks(blk0, block0_buffer, fd) == -1) {
1840 ErrorAbort(state, kFreadFailure, "failed to read %s: %s", arg_filename->data.c_str(),
1841 strerror(errno));
1842 return StringValue("");
1843 }
Tianjie Xu57bed6d2015-12-15 11:47:30 -08001844
Tao Baoc97edcb2017-03-31 01:18:13 -07001845 // https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout
1846 // Super block starts from block 0, offset 0x400
1847 // 0x2C: len32 Mount time
1848 // 0x30: len32 Write time
1849 // 0x34: len16 Number of mounts since the last fsck
1850 // 0x38: len16 Magic signature 0xEF53
Tianjie Xu57bed6d2015-12-15 11:47:30 -08001851
Tao Baoc97edcb2017-03-31 01:18:13 -07001852 time_t mount_time = *reinterpret_cast<uint32_t*>(&block0_buffer[0x400 + 0x2C]);
1853 uint16_t mount_count = *reinterpret_cast<uint16_t*>(&block0_buffer[0x400 + 0x34]);
Tianjie Xu57bed6d2015-12-15 11:47:30 -08001854
Tao Baoc97edcb2017-03-31 01:18:13 -07001855 if (mount_count > 0) {
1856 uiPrintf(state, "Device was remounted R/W %" PRIu16 " times", mount_count);
1857 uiPrintf(state, "Last remount happened on %s", ctime(&mount_time));
1858 }
Tianjie Xu57bed6d2015-12-15 11:47:30 -08001859
Tao Baoc97edcb2017-03-31 01:18:13 -07001860 return StringValue("t");
Tianjie Xu57bed6d2015-12-15 11:47:30 -08001861}
1862
Tianjie Xuc4447322017-03-06 14:44:59 -08001863Value* BlockImageRecoverFn(const char* name, State* state,
1864 const std::vector<std::unique_ptr<Expr>>& argv) {
Tao Baoc97edcb2017-03-31 01:18:13 -07001865 if (argv.size() != 2) {
1866 ErrorAbort(state, kArgsParsingFailure, "block_image_recover expects 2 arguments, got %zu",
1867 argv.size());
1868 return StringValue("");
1869 }
1870
1871 std::vector<std::unique_ptr<Value>> args;
1872 if (!ReadValueArgs(state, argv, &args)) {
1873 return nullptr;
1874 }
1875
1876 const std::unique_ptr<Value>& filename = args[0];
1877 const std::unique_ptr<Value>& ranges = args[1];
1878
1879 if (filename->type != VAL_STRING) {
1880 ErrorAbort(state, kArgsParsingFailure, "filename argument to %s must be string", name);
1881 return StringValue("");
1882 }
1883 if (ranges->type != VAL_STRING) {
1884 ErrorAbort(state, kArgsParsingFailure, "ranges argument to %s must be string", name);
1885 return StringValue("");
1886 }
1887
1888 // Output notice to log when recover is attempted
1889 LOG(INFO) << filename->data << " image corrupted, attempting to recover...";
1890
1891 // When opened with O_RDWR, libfec rewrites corrupted blocks when they are read
1892 fec::io fh(filename->data, O_RDWR);
1893
1894 if (!fh) {
1895 ErrorAbort(state, kLibfecFailure, "fec_open \"%s\" failed: %s", filename->data.c_str(),
1896 strerror(errno));
1897 return StringValue("");
1898 }
1899
1900 if (!fh.has_ecc() || !fh.has_verity()) {
1901 ErrorAbort(state, kLibfecFailure, "unable to use metadata to correct errors");
1902 return StringValue("");
1903 }
1904
1905 fec_status status;
Tao Baoc97edcb2017-03-31 01:18:13 -07001906 if (!fh.get_status(status)) {
1907 ErrorAbort(state, kLibfecFailure, "failed to read FEC status");
1908 return StringValue("");
1909 }
1910
Tao Baoc97edcb2017-03-31 01:18:13 -07001911 uint8_t buffer[BLOCKSIZE];
Tao Baobf5b77d2017-03-30 16:57:29 -07001912 for (const auto& range : RangeSet::Parse(ranges->data)) {
1913 for (size_t j = range.first; j < range.second; ++j) {
Tao Baoc97edcb2017-03-31 01:18:13 -07001914 // Stay within the data area, libfec validates and corrects metadata
Tao Baobf5b77d2017-03-30 16:57:29 -07001915 if (status.data_size <= static_cast<uint64_t>(j) * BLOCKSIZE) {
Tao Baoc97edcb2017-03-31 01:18:13 -07001916 continue;
1917 }
1918
Tao Baobf5b77d2017-03-30 16:57:29 -07001919 if (fh.pread(buffer, BLOCKSIZE, static_cast<off64_t>(j) * BLOCKSIZE) != BLOCKSIZE) {
Tao Baoc97edcb2017-03-31 01:18:13 -07001920 ErrorAbort(state, kLibfecFailure, "failed to recover %s (block %zu): %s",
1921 filename->data.c_str(), j, strerror(errno));
Tianjie Xuc4447322017-03-06 14:44:59 -08001922 return StringValue("");
Tao Baoc97edcb2017-03-31 01:18:13 -07001923 }
1924
1925 // If we want to be able to recover from a situation where rewriting a corrected
1926 // block doesn't guarantee the same data will be returned when re-read later, we
1927 // can save a copy of corrected blocks to /cache. Note:
1928 //
1929 // 1. Maximum space required from /cache is the same as the maximum number of
1930 // corrupted blocks we can correct. For RS(255, 253) and a 2 GiB partition,
1931 // this would be ~16 MiB, for example.
1932 //
1933 // 2. To find out if this block was corrupted, call fec_get_status after each
1934 // read and check if the errors field value has increased.
Tianjie Xuc4447322017-03-06 14:44:59 -08001935 }
Tao Baoc97edcb2017-03-31 01:18:13 -07001936 }
1937 LOG(INFO) << "..." << filename->data << " image recovered successfully.";
1938 return StringValue("t");
Sami Tolvanen0a7b4732015-06-25 10:25:36 +01001939}
1940
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001941void RegisterBlockImageFunctions() {
Tao Baoc97edcb2017-03-31 01:18:13 -07001942 RegisterFunction("block_image_verify", BlockImageVerifyFn);
1943 RegisterFunction("block_image_update", BlockImageUpdateFn);
1944 RegisterFunction("block_image_recover", BlockImageRecoverFn);
1945 RegisterFunction("check_first_block", CheckFirstBlockFn);
1946 RegisterFunction("range_sha1", RangeSha1Fn);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -07001947}