blob: e6fd5f6ae5b749be9e214a369cde80f8816f4bfa [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -08001/*
2 * Copyright (C) 2008 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
Tao Bao8fce75a2016-11-10 12:33:41 -080017#include "applypatch/applypatch.h"
18
Doug Zongker512536a2010-02-17 16:11:44 -080019#include <errno.h>
Tao Baoba9a42a2015-06-23 23:23:33 -070020#include <fcntl.h>
Doug Zongker512536a2010-02-17 16:11:44 -080021#include <libgen.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/statfs.h>
27#include <sys/types.h>
Doug Zongker512536a2010-02-17 16:11:44 -080028#include <unistd.h>
29
Tao Baoc0e1c462017-02-01 10:20:10 -080030#include <functional>
Yabin Cuid483c202016-02-03 17:08:52 -080031#include <memory>
32#include <string>
Tao Bao8fce75a2016-11-10 12:33:41 -080033#include <utility>
34#include <vector>
Yabin Cuid483c202016-02-03 17:08:52 -080035
Tao Bao40e144d2017-03-15 01:10:58 -070036#include <android-base/logging.h>
Tao Bao8fce75a2016-11-10 12:33:41 -080037#include <android-base/parseint.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080038#include <android-base/strings.h>
Tao Bao8fce75a2016-11-10 12:33:41 -080039#include <openssl/sha.h>
Tao Baoaca8e892015-07-17 11:47:44 -070040
Doug Zongkerc4351c72010-02-22 14:46:32 -080041#include "edify/expr.h"
Tao Baod33b2f82017-09-28 21:29:11 -070042#include "otafault/ota_io.h"
Tao Bao641fa972018-04-25 18:59:40 -070043#include "otautil/paths.h"
Tao Bao09e468f2017-09-29 14:39:33 -070044#include "otautil/print_sha1.h"
Doug Zongker512536a2010-02-17 16:11:44 -080045
Tao Bao8fce75a2016-11-10 12:33:41 -080046static int LoadPartitionContents(const std::string& filename, FileContents* file);
Tao Baoc0e1c462017-02-01 10:20:10 -080047static size_t FileSink(const unsigned char* data, size_t len, int fd);
Tao Bao40e144d2017-03-15 01:10:58 -070048static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
49 const std::string& target_filename,
50 const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data);
Doug Zongker512536a2010-02-17 16:11:44 -080051
Tao Bao8dc70492018-06-20 10:14:40 -070052int LoadFileContents(const std::string& filename, FileContents* file) {
Tao Bao8fce75a2016-11-10 12:33:41 -080053 // A special 'filename' beginning with "EMMC:" means to load the contents of a partition.
Tao Bao8dc70492018-06-20 10:14:40 -070054 if (android::base::StartsWith(filename, "EMMC:")) {
Tao Bao8fce75a2016-11-10 12:33:41 -080055 return LoadPartitionContents(filename, file);
56 }
Doug Zongker512536a2010-02-17 16:11:44 -080057
Tao Bao47e5a8d2017-12-01 10:53:34 -080058 struct stat sb;
Tao Bao8dc70492018-06-20 10:14:40 -070059 if (stat(filename.c_str(), &sb) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -070060 PLOG(ERROR) << "Failed to stat \"" << filename << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -080061 return -1;
62 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080063
Tao Bao47e5a8d2017-12-01 10:53:34 -080064 std::vector<unsigned char> data(sb.st_size);
Tao Bao8dc70492018-06-20 10:14:40 -070065 unique_file f(ota_fopen(filename.c_str(), "rb"));
Tao Bao8fce75a2016-11-10 12:33:41 -080066 if (!f) {
Tao Bao859bfc52018-04-25 23:00:27 -070067 PLOG(ERROR) << "Failed to open \"" << filename << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -080068 return -1;
69 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080070
Tao Bao8fce75a2016-11-10 12:33:41 -080071 size_t bytes_read = ota_fread(data.data(), 1, data.size(), f.get());
72 if (bytes_read != data.size()) {
Tao Bao859bfc52018-04-25 23:00:27 -070073 LOG(ERROR) << "Short read of \"" << filename << "\" (" << bytes_read << " bytes of "
74 << data.size() << ")";
Tao Bao8fce75a2016-11-10 12:33:41 -080075 return -1;
76 }
77 file->data = std::move(data);
78 SHA1(file->data.data(), file->data.size(), file->sha1);
79 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -080080}
81
Tao Bao155771b2018-06-05 11:26:01 -070082// Loads the contents of an EMMC partition into the provided FileContents. filename should be a
83// string of the form "EMMC:<partition_device>:...". The smallest size_n bytes for which that prefix
84// of the partition contents has the corresponding sha1 hash will be loaded. It is acceptable for a
85// size value to be repeated with different sha1s. Returns 0 on success.
Doug Zongker512536a2010-02-17 16:11:44 -080086//
Tao Bao155771b2018-06-05 11:26:01 -070087// This complexity is needed because if an OTA installation is interrupted, the partition might
88// contain either the source or the target data, which might be of different lengths. We need to
89// know the length in order to read from a partition (there is no "end-of-file" marker), so the
90// caller must specify the possible lengths and the hash of the data, and we'll do the load
91// expecting to find one of those hashes.
Tao Bao8fce75a2016-11-10 12:33:41 -080092static int LoadPartitionContents(const std::string& filename, FileContents* file) {
93 std::vector<std::string> pieces = android::base::Split(filename, ":");
94 if (pieces.size() < 4 || pieces.size() % 2 != 0 || pieces[0] != "EMMC") {
Tao Bao859bfc52018-04-25 23:00:27 -070095 LOG(ERROR) << "LoadPartitionContents called with bad filename \"" << filename << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -080096 return -1;
97 }
98
99 size_t pair_count = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename
100 std::vector<std::pair<size_t, std::string>> pairs;
101 for (size_t i = 0; i < pair_count; ++i) {
102 size_t size;
103 if (!android::base::ParseUint(pieces[i * 2 + 2], &size) || size == 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700104 LOG(ERROR) << "LoadPartitionContents called with bad size \"" << pieces[i * 2 + 2] << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800105 return -1;
106 }
107 pairs.push_back({ size, pieces[i * 2 + 3] });
108 }
109
110 // Sort the pairs array so that they are in order of increasing size.
111 std::sort(pairs.begin(), pairs.end());
112
113 const char* partition = pieces[1].c_str();
Tao Bao358c2ec2016-11-28 11:48:43 -0800114 unique_file dev(ota_fopen(partition, "rb"));
Tao Bao8fce75a2016-11-10 12:33:41 -0800115 if (!dev) {
Tao Bao859bfc52018-04-25 23:00:27 -0700116 PLOG(ERROR) << "Failed to open eMMC partition \"" << partition << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800117 return -1;
118 }
119
120 SHA_CTX sha_ctx;
121 SHA1_Init(&sha_ctx);
122
123 // Allocate enough memory to hold the largest size.
124 std::vector<unsigned char> buffer(pairs[pair_count - 1].first);
125 unsigned char* buffer_ptr = buffer.data();
126 size_t buffer_size = 0; // # bytes read so far
127 bool found = false;
128
129 for (const auto& pair : pairs) {
130 size_t current_size = pair.first;
131 const std::string& current_sha1 = pair.second;
132
133 // Read enough additional bytes to get us up to the next size. (Again,
134 // we're trying the possibilities in order of increasing size).
135 size_t next = current_size - buffer_size;
136 if (next > 0) {
137 size_t read = ota_fread(buffer_ptr, 1, next, dev.get());
138 if (next != read) {
Tao Bao859bfc52018-04-25 23:00:27 -0700139 LOG(ERROR) << "Short read (" << read << " bytes of " << next << ") for partition \""
140 << partition << "\"";
Tao Baoaca8e892015-07-17 11:47:44 -0700141 return -1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800142 }
143 SHA1_Update(&sha_ctx, buffer_ptr, read);
144 buffer_size += read;
145 buffer_ptr += read;
Tao Baoaca8e892015-07-17 11:47:44 -0700146 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700147
Tao Bao8fce75a2016-11-10 12:33:41 -0800148 // Duplicate the SHA context and finalize the duplicate so we can
149 // check it against this pair's expected hash.
150 SHA_CTX temp_ctx;
151 memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
152 uint8_t sha_so_far[SHA_DIGEST_LENGTH];
153 SHA1_Final(sha_so_far, &temp_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800154
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800155 uint8_t parsed_sha[SHA_DIGEST_LENGTH];
Tao Bao8dc70492018-06-20 10:14:40 -0700156 if (ParseSha1(current_sha1, parsed_sha) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700157 LOG(ERROR) << "Failed to parse SHA-1 \"" << current_sha1 << "\" in " << filename;
Tao Bao8fce75a2016-11-10 12:33:41 -0800158 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800159 }
160
Tao Bao8fce75a2016-11-10 12:33:41 -0800161 if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) {
162 // We have a match. Stop reading the partition; we'll return the data we've read so far.
Tao Bao859bfc52018-04-25 23:00:27 -0700163 LOG(INFO) << "Partition read matched size " << current_size << " SHA-1 " << current_sha1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800164 found = true;
165 break;
Doug Zongker512536a2010-02-17 16:11:44 -0800166 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800167 }
Doug Zongker512536a2010-02-17 16:11:44 -0800168
Tao Bao8fce75a2016-11-10 12:33:41 -0800169 if (!found) {
170 // Ran off the end of the list of (size, sha1) pairs without finding a match.
Tao Bao859bfc52018-04-25 23:00:27 -0700171 LOG(ERROR) << "Contents of partition \"" << partition << "\" didn't match " << filename;
Tao Bao8fce75a2016-11-10 12:33:41 -0800172 return -1;
173 }
Doug Zongker512536a2010-02-17 16:11:44 -0800174
Tao Bao8fce75a2016-11-10 12:33:41 -0800175 SHA1_Final(file->sha1, &sha_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800176
Tao Bao8fce75a2016-11-10 12:33:41 -0800177 buffer.resize(buffer_size);
178 file->data = std::move(buffer);
Tao Bao8fce75a2016-11-10 12:33:41 -0800179
180 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800181}
182
Tao Bao8dc70492018-06-20 10:14:40 -0700183int SaveFileContents(const std::string& filename, const FileContents* file) {
184 unique_fd fd(
185 ota_open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
Tao Bao6e02ea92016-11-17 11:24:07 -0800186 if (fd == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700187 PLOG(ERROR) << "Failed to open \"" << filename << "\" for write";
Tao Bao6e02ea92016-11-17 11:24:07 -0800188 return -1;
189 }
Doug Zongker512536a2010-02-17 16:11:44 -0800190
Tao Baoc0e1c462017-02-01 10:20:10 -0800191 size_t bytes_written = FileSink(file->data.data(), file->data.size(), fd);
Tao Baof7eb7602017-03-27 15:12:48 -0700192 if (bytes_written != file->data.size()) {
Tao Bao859bfc52018-04-25 23:00:27 -0700193 PLOG(ERROR) << "Short write of \"" << filename << "\" (" << bytes_written << " bytes of "
194 << file->data.size();
Tao Bao6e02ea92016-11-17 11:24:07 -0800195 return -1;
196 }
197 if (ota_fsync(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700198 PLOG(ERROR) << "Failed to fsync \"" << filename << "\"";
Tao Bao6e02ea92016-11-17 11:24:07 -0800199 return -1;
200 }
Tao Bao3dc14cb2016-11-22 11:37:13 -0800201 if (ota_close(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700202 PLOG(ERROR) << "Failed to close \"" << filename << "\"";
Tao Bao6e02ea92016-11-17 11:24:07 -0800203 return -1;
204 }
Doug Zongker512536a2010-02-17 16:11:44 -0800205
Tao Bao6e02ea92016-11-17 11:24:07 -0800206 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800207}
208
Tao Bao155771b2018-06-05 11:26:01 -0700209// Writes a memory buffer to 'target' partition, a string of the form
210// "EMMC:<partition_device>[:...]". The target name might contain multiple colons, but
211// WriteToPartition() only uses the first two and ignores the rest. Returns 0 on success.
212static int WriteToPartition(const unsigned char* data, size_t len, const std::string& target) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800213 std::vector<std::string> pieces = android::base::Split(target, ":");
Tao Bao8fce75a2016-11-10 12:33:41 -0800214 if (pieces.size() < 2 || pieces[0] != "EMMC") {
Tao Bao859bfc52018-04-25 23:00:27 -0700215 LOG(ERROR) << "WriteToPartition called with bad target \"" << target << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800216 return -1;
217 }
218
219 const char* partition = pieces[1].c_str();
Tao Bao6e02ea92016-11-17 11:24:07 -0800220 unique_fd fd(ota_open(partition, O_RDWR));
221 if (fd == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700222 PLOG(ERROR) << "Failed to open \"" << partition << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800223 return -1;
224 }
225
Tao Bao6e02ea92016-11-17 11:24:07 -0800226 size_t start = 0;
227 bool success = false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800228 for (size_t attempt = 0; attempt < 2; ++attempt) {
229 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700230 PLOG(ERROR) << "Failed to seek to " << start << " on \"" << partition << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800231 return -1;
232 }
233 while (start < len) {
234 size_t to_write = len - start;
235 if (to_write > 1 << 20) to_write = 1 << 20;
236
237 ssize_t written = TEMP_FAILURE_RETRY(ota_write(fd, data + start, to_write));
238 if (written == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700239 PLOG(ERROR) << "Failed to write to \"" << partition << "\"";
Tao Baoaca8e892015-07-17 11:47:44 -0700240 return -1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800241 }
242 start += written;
Tao Baoaca8e892015-07-17 11:47:44 -0700243 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700244
Tao Bao8fce75a2016-11-10 12:33:41 -0800245 if (ota_fsync(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700246 PLOG(ERROR) << "Failed to sync \"" << partition << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800247 return -1;
Doug Zongkerf291d852010-07-07 13:55:25 -0700248 }
Tao Bao3dc14cb2016-11-22 11:37:13 -0800249 if (ota_close(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700250 PLOG(ERROR) << "Failed to close \"" << partition << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800251 return -1;
Elliott Hughes63a31922016-06-09 17:41:22 -0700252 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800253
Tao Bao6e02ea92016-11-17 11:24:07 -0800254 fd.reset(ota_open(partition, O_RDONLY));
255 if (fd == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700256 PLOG(ERROR) << "Failed to reopen \"" << partition << "\" for verification";
Tao Bao8fce75a2016-11-10 12:33:41 -0800257 return -1;
258 }
259
260 // Drop caches so our subsequent verification read won't just be reading the cache.
Elliott Hughes63a31922016-06-09 17:41:22 -0700261 sync();
Tao Bao6e02ea92016-11-17 11:24:07 -0800262 unique_fd dc(ota_open("/proc/sys/vm/drop_caches", O_WRONLY));
Tao Bao8fce75a2016-11-10 12:33:41 -0800263 if (TEMP_FAILURE_RETRY(ota_write(dc, "3\n", 2)) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700264 PLOG(ERROR) << "Failed to write to /proc/sys/vm/drop_caches";
Tao Bao8fce75a2016-11-10 12:33:41 -0800265 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700266 LOG(INFO) << " caches dropped";
Tao Bao8fce75a2016-11-10 12:33:41 -0800267 }
Tao Bao3dc14cb2016-11-22 11:37:13 -0800268 ota_close(dc);
Tao Bao8fce75a2016-11-10 12:33:41 -0800269 sleep(1);
Elliott Hughes63a31922016-06-09 17:41:22 -0700270
Tao Bao6e02ea92016-11-17 11:24:07 -0800271 // Verify.
Tao Bao8fce75a2016-11-10 12:33:41 -0800272 if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700273 PLOG(ERROR) << "Failed to seek to 0 on " << partition;
Tao Bao8fce75a2016-11-10 12:33:41 -0800274 return -1;
275 }
276
277 unsigned char buffer[4096];
278 start = len;
279 for (size_t p = 0; p < len; p += sizeof(buffer)) {
280 size_t to_read = len - p;
281 if (to_read > sizeof(buffer)) {
282 to_read = sizeof(buffer);
283 }
284
285 size_t so_far = 0;
286 while (so_far < to_read) {
287 ssize_t read_count = TEMP_FAILURE_RETRY(ota_read(fd, buffer + so_far, to_read - so_far));
288 if (read_count == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700289 PLOG(ERROR) << "Failed to verify-read " << partition << " at " << p;
Tao Bao8fce75a2016-11-10 12:33:41 -0800290 return -1;
291 } else if (read_count == 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700292 LOG(ERROR) << "Verify-reading " << partition << " reached unexpected EOF at " << p;
Tao Bao8fce75a2016-11-10 12:33:41 -0800293 return -1;
294 }
295 if (static_cast<size_t>(read_count) < to_read) {
Tao Bao859bfc52018-04-25 23:00:27 -0700296 LOG(INFO) << "Short verify-read " << partition << " at " << p << ": expected " << to_read
297 << " actual " << read_count;
Tao Bao8fce75a2016-11-10 12:33:41 -0800298 }
299 so_far += read_count;
300 }
301
302 if (memcmp(buffer, data + p, to_read) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700303 LOG(ERROR) << "Verification failed starting at " << p;
Tao Bao8fce75a2016-11-10 12:33:41 -0800304 start = p;
305 break;
306 }
307 }
308
309 if (start == len) {
Tao Bao859bfc52018-04-25 23:00:27 -0700310 LOG(INFO) << "Verification read succeeded (attempt " << attempt + 1 << ")";
Tao Bao8fce75a2016-11-10 12:33:41 -0800311 success = true;
312 break;
313 }
katao9a6f5202016-12-19 11:22:30 +0800314
315 if (ota_close(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700316 PLOG(ERROR) << "Failed to close " << partition;
katao9a6f5202016-12-19 11:22:30 +0800317 return -1;
318 }
319
320 fd.reset(ota_open(partition, O_RDWR));
321 if (fd == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700322 PLOG(ERROR) << "Failed to reopen " << partition << " for next attempt";
katao9a6f5202016-12-19 11:22:30 +0800323 return -1;
324 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800325 }
326
327 if (!success) {
Tao Bao859bfc52018-04-25 23:00:27 -0700328 LOG(ERROR) << "Failed to verify after all attempts";
Tao Bao8fce75a2016-11-10 12:33:41 -0800329 return -1;
330 }
331
Tao Bao3dc14cb2016-11-22 11:37:13 -0800332 if (ota_close(fd) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700333 PLOG(ERROR) << "Failed to close " << partition;
Tao Bao8fce75a2016-11-10 12:33:41 -0800334 return -1;
335 }
336 sync();
337
338 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800339}
340
Tao Bao8dc70492018-06-20 10:14:40 -0700341int ParseSha1(const std::string& str, uint8_t* digest) {
342 const char* ps = str.c_str();
Tao Bao155771b2018-06-05 11:26:01 -0700343 uint8_t* pd = digest;
344 for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
345 int digit;
346 if (*ps >= '0' && *ps <= '9') {
347 digit = *ps - '0';
348 } else if (*ps >= 'a' && *ps <= 'f') {
349 digit = *ps - 'a' + 10;
350 } else if (*ps >= 'A' && *ps <= 'F') {
351 digit = *ps - 'A' + 10;
352 } else {
353 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800354 }
Tao Bao155771b2018-06-05 11:26:01 -0700355 if (i % 2 == 0) {
356 *pd = digit << 4;
357 } else {
358 *pd |= digit;
359 ++pd;
360 }
361 }
362 if (*ps != '\0') return -1;
363 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800364}
365
Tao Bao155771b2018-06-05 11:26:01 -0700366// Searches a vector of SHA-1 strings for one matching the given SHA-1. Returns the index of the
367// match on success, or -1 if no match is found.
368static int FindMatchingPatch(const uint8_t* sha1, const std::vector<std::string>& patch_sha1s) {
369 for (size_t i = 0; i < patch_sha1s.size(); ++i) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800370 uint8_t patch_sha1[SHA_DIGEST_LENGTH];
Tao Bao8dc70492018-06-20 10:14:40 -0700371 if (ParseSha1(patch_sha1s[i], patch_sha1) == 0 &&
Tao Bao8fce75a2016-11-10 12:33:41 -0800372 memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
373 return i;
Doug Zongker512536a2010-02-17 16:11:44 -0800374 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800375 }
376 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800377}
378
Tao Bao155771b2018-06-05 11:26:01 -0700379int applypatch_check(const char* filename, const std::vector<std::string>& patch_sha1s) {
380 // It's okay to specify no SHA-1s; the check will pass if the LoadFileContents is successful.
381 // (Useful for reading partitions, where the filename encodes the SHA-1s; no need to check them
382 // twice.)
Tao Bao8fce75a2016-11-10 12:33:41 -0800383 FileContents file;
Tao Bao8fce75a2016-11-10 12:33:41 -0800384 if (LoadFileContents(filename, &file) != 0 ||
Tao Bao155771b2018-06-05 11:26:01 -0700385 (!patch_sha1s.empty() && FindMatchingPatch(file.sha1, patch_sha1s) < 0)) {
Tao Bao859bfc52018-04-25 23:00:27 -0700386 LOG(INFO) << "\"" << filename << "\" doesn't have any of expected SHA-1 sums; checking cache";
Doug Zongker512536a2010-02-17 16:11:44 -0800387
Tianjie Xua88cc542017-10-25 13:16:54 -0700388 // If the source file is missing or corrupted, it might be because we were killed in the middle
Tao Bao155771b2018-06-05 11:26:01 -0700389 // of patching it. A copy should have been made in cache_temp_source. If that file exists and
390 // matches the SHA-1 we're looking for, the check still passes.
Tao Bao8dc70492018-06-20 10:14:40 -0700391 if (LoadFileContents(Paths::Get().cache_temp_source(), &file) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700392 LOG(ERROR) << "Failed to load cache file";
Tao Bao8fce75a2016-11-10 12:33:41 -0800393 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800394 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800395
Tao Bao155771b2018-06-05 11:26:01 -0700396 if (FindMatchingPatch(file.sha1, patch_sha1s) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700397 LOG(ERROR) << "The cache bits don't match any SHA-1 for \"" << filename << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800398 return 1;
399 }
400 }
401 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800402}
403
404int ShowLicenses() {
Tao Bao155771b2018-06-05 11:26:01 -0700405 ShowBSDiffLicense();
406 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800407}
408
Tao Baoc0e1c462017-02-01 10:20:10 -0800409static size_t FileSink(const unsigned char* data, size_t len, int fd) {
Tao Baof7eb7602017-03-27 15:12:48 -0700410 size_t done = 0;
411 while (done < len) {
412 ssize_t wrote = TEMP_FAILURE_RETRY(ota_write(fd, data + done, len - done));
413 if (wrote == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700414 PLOG(ERROR) << "Failed to write " << len - done << " bytes";
Tao Baof7eb7602017-03-27 15:12:48 -0700415 return done;
Doug Zongker512536a2010-02-17 16:11:44 -0800416 }
Tao Baof7eb7602017-03-27 15:12:48 -0700417 done += wrote;
418 }
419 return done;
Doug Zongker512536a2010-02-17 16:11:44 -0800420}
421
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700422size_t FreeSpaceForFile(const std::string& filename) {
423 struct statfs sf;
424 if (statfs(filename.c_str(), &sf) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700425 PLOG(ERROR) << "Failed to statfs " << filename;
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700426 return -1;
427 }
428 return sf.f_bsize * sf.f_bavail;
Doug Zongker512536a2010-02-17 16:11:44 -0800429}
430
Doug Zongkerc4351c72010-02-22 14:46:32 -0800431int CacheSizeCheck(size_t bytes) {
Tao Bao155771b2018-06-05 11:26:01 -0700432 if (MakeFreeSpaceOnCache(bytes) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700433 LOG(ERROR) << "Failed to make " << bytes << " bytes available on /cache";
Tao Bao155771b2018-06-05 11:26:01 -0700434 return 1;
435 }
436 return 0;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800437}
438
Tao Bao40e144d2017-03-15 01:10:58 -0700439int applypatch(const char* source_filename, const char* target_filename,
Tianjie Xue40c80d2018-02-03 17:20:56 -0800440 const char* target_sha1_str, size_t /* target_size */,
Tao Bao155771b2018-06-05 11:26:01 -0700441 const std::vector<std::string>& patch_sha1s,
Tao Bao40e144d2017-03-15 01:10:58 -0700442 const std::vector<std::unique_ptr<Value>>& patch_data, const Value* bonus_data) {
Tao Bao859bfc52018-04-25 23:00:27 -0700443 LOG(INFO) << "Patching " << source_filename;
Doug Zongker512536a2010-02-17 16:11:44 -0800444
Tao Bao40e144d2017-03-15 01:10:58 -0700445 if (target_filename[0] == '-' && target_filename[1] == '\0') {
446 target_filename = source_filename;
447 }
448
449 if (strncmp(target_filename, "EMMC:", 5) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700450 LOG(ERROR) << "Supporting patching EMMC targets only";
Tao Bao40e144d2017-03-15 01:10:58 -0700451 return 1;
452 }
453
454 uint8_t target_sha1[SHA_DIGEST_LENGTH];
455 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700456 LOG(ERROR) << "Failed to parse target SHA-1 \"" << target_sha1_str << "\"";
Tao Bao40e144d2017-03-15 01:10:58 -0700457 return 1;
458 }
459
460 // We try to load the target file into the source_file object.
461 FileContents source_file;
462 if (LoadFileContents(target_filename, &source_file) == 0) {
463 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
464 // The early-exit case: the patch was already applied, this file has the desired hash, nothing
465 // for us to do.
Tao Bao859bfc52018-04-25 23:00:27 -0700466 LOG(INFO) << " already " << short_sha1(target_sha1);
Tao Bao40e144d2017-03-15 01:10:58 -0700467 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800468 }
Tao Bao40e144d2017-03-15 01:10:58 -0700469 }
Doug Zongker512536a2010-02-17 16:11:44 -0800470
Tao Bao40e144d2017-03-15 01:10:58 -0700471 if (source_file.data.empty() ||
472 (target_filename != source_filename && strcmp(target_filename, source_filename) != 0)) {
473 // Need to load the source file: either we failed to load the target file, or we did but it's
474 // different from the expected.
475 source_file.data.clear();
476 LoadFileContents(source_filename, &source_file);
477 }
478
479 if (!source_file.data.empty()) {
Tao Bao155771b2018-06-05 11:26:01 -0700480 int to_use = FindMatchingPatch(source_file.sha1, patch_sha1s);
Tao Bao40e144d2017-03-15 01:10:58 -0700481 if (to_use != -1) {
482 return GenerateTarget(source_file, patch_data[to_use], target_filename, target_sha1,
483 bonus_data);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800484 }
Tao Bao40e144d2017-03-15 01:10:58 -0700485 }
Doug Zongker512536a2010-02-17 16:11:44 -0800486
Tao Bao859bfc52018-04-25 23:00:27 -0700487 LOG(INFO) << "Source file is bad; trying copy";
Doug Zongker512536a2010-02-17 16:11:44 -0800488
Tao Bao40e144d2017-03-15 01:10:58 -0700489 FileContents copy_file;
Tao Bao8dc70492018-06-20 10:14:40 -0700490 if (LoadFileContents(Paths::Get().cache_temp_source(), &copy_file) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700491 LOG(ERROR) << "Failed to read copy file";
Tao Bao40e144d2017-03-15 01:10:58 -0700492 return 1;
493 }
Doug Zongker512536a2010-02-17 16:11:44 -0800494
Tao Bao155771b2018-06-05 11:26:01 -0700495 int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1s);
Tao Bao40e144d2017-03-15 01:10:58 -0700496 if (to_use == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700497 LOG(ERROR) << "The copy on /cache doesn't match source SHA-1s either";
Tao Bao40e144d2017-03-15 01:10:58 -0700498 return 1;
499 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800500
Tao Bao40e144d2017-03-15 01:10:58 -0700501 return GenerateTarget(copy_file, patch_data[to_use], target_filename, target_sha1, bonus_data);
Doug Zongker1c43c972012-02-28 11:07:09 -0800502}
503
Tao Baoabba55b2015-07-17 18:11:12 -0700504int applypatch_flash(const char* source_filename, const char* target_filename,
505 const char* target_sha1_str, size_t target_size) {
Tao Bao859bfc52018-04-25 23:00:27 -0700506 LOG(INFO) << "Flashing " << target_filename;
Tao Baoabba55b2015-07-17 18:11:12 -0700507
Tao Bao6e02ea92016-11-17 11:24:07 -0800508 uint8_t target_sha1[SHA_DIGEST_LENGTH];
509 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700510 LOG(ERROR) << "Failed to parse target SHA-1 \"" << target_sha1_str << "\"";
Tao Bao6e02ea92016-11-17 11:24:07 -0800511 return 1;
512 }
Tao Baoabba55b2015-07-17 18:11:12 -0700513
Tao Bao6e02ea92016-11-17 11:24:07 -0800514 std::string target_str(target_filename);
515 std::vector<std::string> pieces = android::base::Split(target_str, ":");
516 if (pieces.size() != 2 || pieces[0] != "EMMC") {
Tao Bao859bfc52018-04-25 23:00:27 -0700517 LOG(ERROR) << "Invalid target name \"" << target_filename << "\"";
Tao Bao6e02ea92016-11-17 11:24:07 -0800518 return 1;
519 }
Tao Baoabba55b2015-07-17 18:11:12 -0700520
Tao Bao6e02ea92016-11-17 11:24:07 -0800521 // Load the target into the source_file object to see if already applied.
522 pieces.push_back(std::to_string(target_size));
523 pieces.push_back(target_sha1_str);
524 std::string fullname = android::base::Join(pieces, ':');
525 FileContents source_file;
526 if (LoadPartitionContents(fullname, &source_file) == 0 &&
527 memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700528 // The early-exit case: the image was already applied, this partition has the desired hash,
529 // nothing for us to do.
530 LOG(INFO) << " already " << short_sha1(target_sha1);
Tao Baoabba55b2015-07-17 18:11:12 -0700531 return 0;
Tao Bao6e02ea92016-11-17 11:24:07 -0800532 }
533
534 if (LoadFileContents(source_filename, &source_file) == 0) {
535 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
536 // The source doesn't have desired checksum.
Tao Bao859bfc52018-04-25 23:00:27 -0700537 LOG(ERROR) << "source \"" << source_filename << "\" doesn't have expected SHA-1 sum";
538 LOG(ERROR) << "expected: " << short_sha1(target_sha1)
539 << ", found: " << short_sha1(source_file.sha1);
Tao Bao6e02ea92016-11-17 11:24:07 -0800540 return 1;
541 }
542 }
543
544 if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700545 LOG(ERROR) << "Failed to write copied data to " << target_filename;
Tao Bao6e02ea92016-11-17 11:24:07 -0800546 return 1;
547 }
548 return 0;
Tao Baoabba55b2015-07-17 18:11:12 -0700549}
550
Tao Bao40e144d2017-03-15 01:10:58 -0700551static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
552 const std::string& target_filename,
553 const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800554 if (patch->type != VAL_BLOB) {
Tao Bao859bfc52018-04-25 23:00:27 -0700555 LOG(ERROR) << "patch is not a blob";
Tao Bao6e02ea92016-11-17 11:24:07 -0800556 return 1;
557 }
Yabin Cuid483c202016-02-03 17:08:52 -0800558
Tao Bao6e02ea92016-11-17 11:24:07 -0800559 const char* header = &patch->data[0];
560 size_t header_bytes_read = patch->data.size();
561 bool use_bsdiff = false;
562 if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
563 use_bsdiff = true;
564 } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
565 use_bsdiff = false;
566 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700567 LOG(ERROR) << "Unknown patch file format";
Tao Bao6e02ea92016-11-17 11:24:07 -0800568 return 1;
569 }
Doug Zongker512536a2010-02-17 16:11:44 -0800570
Tao Bao40e144d2017-03-15 01:10:58 -0700571 CHECK(android::base::StartsWith(target_filename, "EMMC:"));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800572
Tao Bao40e144d2017-03-15 01:10:58 -0700573 // We still write the original source to cache, in case the partition write is interrupted.
574 if (MakeFreeSpaceOnCache(source_file.data.size()) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700575 LOG(ERROR) << "Not enough free space on /cache";
Tao Bao40e144d2017-03-15 01:10:58 -0700576 return 1;
577 }
Tao Bao8dc70492018-06-20 10:14:40 -0700578 if (SaveFileContents(Paths::Get().cache_temp_source(), &source_file) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700579 LOG(ERROR) << "Failed to back up source file";
Tao Bao40e144d2017-03-15 01:10:58 -0700580 return 1;
581 }
582
583 // We store the decoded output in memory.
Tao Bao6e02ea92016-11-17 11:24:07 -0800584 std::string memory_sink_str; // Don't need to reserve space.
Tao Bao8b0b0f12018-04-19 21:02:13 -0700585 SHA_CTX ctx;
586 SHA1_Init(&ctx);
587 SinkFn sink = [&memory_sink_str, &ctx](const unsigned char* data, size_t len) {
Tianjie Xuffed57a2018-04-23 17:51:45 -0700588 if (len != 0) {
589 uint8_t digest[SHA_DIGEST_LENGTH];
590 SHA1(data, len, digest);
Tao Bao859bfc52018-04-25 23:00:27 -0700591 LOG(DEBUG) << "Appending " << len << " bytes data, SHA-1: " << short_sha1(digest);
Tianjie Xuffed57a2018-04-23 17:51:45 -0700592 }
Tao Bao8b0b0f12018-04-19 21:02:13 -0700593 SHA1_Update(&ctx, data, len);
Tao Baoc0e1c462017-02-01 10:20:10 -0800594 memory_sink_str.append(reinterpret_cast<const char*>(data), len);
595 return len;
596 };
Doug Zongkerc4351c72010-02-22 14:46:32 -0800597
Tao Bao40e144d2017-03-15 01:10:58 -0700598 int result;
599 if (use_bsdiff) {
Tao Bao8b0b0f12018-04-19 21:02:13 -0700600 result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), *patch, 0, sink);
Tao Bao40e144d2017-03-15 01:10:58 -0700601 } else {
Tao Bao8b0b0f12018-04-19 21:02:13 -0700602 result =
603 ApplyImagePatch(source_file.data.data(), source_file.data.size(), *patch, sink, bonus_data);
Tao Bao40e144d2017-03-15 01:10:58 -0700604 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800605
Tao Bao40e144d2017-03-15 01:10:58 -0700606 if (result != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700607 LOG(ERROR) << "Failed to apply the patch: " << result;
Tao Bao40e144d2017-03-15 01:10:58 -0700608 return 1;
609 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800610
611 uint8_t current_target_sha1[SHA_DIGEST_LENGTH];
612 SHA1_Final(current_target_sha1, &ctx);
613 if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700614 LOG(ERROR) << "Patching did not produce the expected SHA-1 of " << short_sha1(target_sha1);
Tao Bao4f834302018-04-19 12:35:14 -0700615
Tao Bao859bfc52018-04-25 23:00:27 -0700616 LOG(ERROR) << "target size " << memory_sink_str.size() << " SHA-1 "
617 << short_sha1(current_target_sha1);
618 LOG(ERROR) << "source size " << source_file.data.size() << " SHA-1 "
619 << short_sha1(source_file.sha1);
Tao Bao4f834302018-04-19 12:35:14 -0700620
621 uint8_t patch_digest[SHA_DIGEST_LENGTH];
622 SHA1(reinterpret_cast<const uint8_t*>(patch->data.data()), patch->data.size(), patch_digest);
Tao Bao859bfc52018-04-25 23:00:27 -0700623 LOG(ERROR) << "patch size " << patch->data.size() << " SHA-1 " << short_sha1(patch_digest);
Tao Bao4f834302018-04-19 12:35:14 -0700624
625 uint8_t bonus_digest[SHA_DIGEST_LENGTH];
626 SHA1(reinterpret_cast<const uint8_t*>(bonus_data->data.data()), bonus_data->data.size(),
627 bonus_digest);
Tao Bao859bfc52018-04-25 23:00:27 -0700628 LOG(ERROR) << "bonus size " << bonus_data->data.size() << " SHA-1 " << short_sha1(bonus_digest);
Tao Bao4f834302018-04-19 12:35:14 -0700629
Tianjie Xu3f638ee2018-04-26 17:23:23 -0700630 // TODO(b/67849209) Remove after debugging the unit test flakiness.
631 if (android::base::GetMinimumLogSeverity() <= android::base::LogSeverity::DEBUG) {
632 if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
633 memory_sink_str.size(), target_filename) != 0) {
634 LOG(DEBUG) << "Failed to write patched data " << target_filename;
635 }
636 }
637
Tao Bao6e02ea92016-11-17 11:24:07 -0800638 return 1;
639 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700640 LOG(INFO) << " now " << short_sha1(target_sha1);
Tao Bao6e02ea92016-11-17 11:24:07 -0800641 }
642
Tao Bao40e144d2017-03-15 01:10:58 -0700643 // Write back the temp file to the partition.
644 if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
645 memory_sink_str.size(), target_filename) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700646 LOG(ERROR) << "Failed to write patched data to " << target_filename;
Tao Bao40e144d2017-03-15 01:10:58 -0700647 return 1;
Tao Bao6e02ea92016-11-17 11:24:07 -0800648 }
649
Tao Bao40e144d2017-03-15 01:10:58 -0700650 // Delete the backup copy of the source.
Tao Bao641fa972018-04-25 18:59:40 -0700651 unlink(Paths::Get().cache_temp_source().c_str());
Tao Bao6e02ea92016-11-17 11:24:07 -0800652
653 // Success!
654 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800655}