blob: b1f5607a6cc69f0d0618d52231782acc022784ed [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 Bao7c1d4262018-07-06 23:18:14 -0700379int applypatch_check(const std::string& filename, const std::vector<std::string>& sha1s) {
380 if (!android::base::StartsWith(filename, "EMMC:")) {
381 return 1;
382 }
383
384 // The check will pass if LoadPartitionContents is successful, because the filename already
385 // encodes the desired SHA-1s.
Tao Bao8fce75a2016-11-10 12:33:41 -0800386 FileContents file;
Tao Bao7c1d4262018-07-06 23:18:14 -0700387 if (LoadPartitionContents(filename, &file) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700388 LOG(INFO) << "\"" << filename << "\" doesn't have any of expected SHA-1 sums; checking cache";
Doug Zongker512536a2010-02-17 16:11:44 -0800389
Tao Bao7c1d4262018-07-06 23:18:14 -0700390 // If the partition is corrupted, it might be because we were killed in the middle of patching
391 // it. A copy should have been made in cache_temp_source. If that file exists and matches the
392 // SHA-1 we're looking for, the check still passes.
Tao Bao8dc70492018-06-20 10:14:40 -0700393 if (LoadFileContents(Paths::Get().cache_temp_source(), &file) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700394 LOG(ERROR) << "Failed to load cache file";
Tao Bao8fce75a2016-11-10 12:33:41 -0800395 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800396 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800397
Tao Bao7c1d4262018-07-06 23:18:14 -0700398 if (FindMatchingPatch(file.sha1, sha1s) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700399 LOG(ERROR) << "The cache bits don't match any SHA-1 for \"" << filename << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800400 return 1;
401 }
402 }
403 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800404}
405
406int ShowLicenses() {
Tao Bao155771b2018-06-05 11:26:01 -0700407 ShowBSDiffLicense();
408 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800409}
410
Tao Baoc0e1c462017-02-01 10:20:10 -0800411static size_t FileSink(const unsigned char* data, size_t len, int fd) {
Tao Baof7eb7602017-03-27 15:12:48 -0700412 size_t done = 0;
413 while (done < len) {
414 ssize_t wrote = TEMP_FAILURE_RETRY(ota_write(fd, data + done, len - done));
415 if (wrote == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700416 PLOG(ERROR) << "Failed to write " << len - done << " bytes";
Tao Baof7eb7602017-03-27 15:12:48 -0700417 return done;
Doug Zongker512536a2010-02-17 16:11:44 -0800418 }
Tao Baof7eb7602017-03-27 15:12:48 -0700419 done += wrote;
420 }
421 return done;
Doug Zongker512536a2010-02-17 16:11:44 -0800422}
423
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700424size_t FreeSpaceForFile(const std::string& filename) {
425 struct statfs sf;
426 if (statfs(filename.c_str(), &sf) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700427 PLOG(ERROR) << "Failed to statfs " << filename;
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700428 return -1;
429 }
430 return sf.f_bsize * sf.f_bavail;
Doug Zongker512536a2010-02-17 16:11:44 -0800431}
432
Doug Zongkerc4351c72010-02-22 14:46:32 -0800433int CacheSizeCheck(size_t bytes) {
Tao Bao155771b2018-06-05 11:26:01 -0700434 if (MakeFreeSpaceOnCache(bytes) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700435 LOG(ERROR) << "Failed to make " << bytes << " bytes available on /cache";
Tao Bao155771b2018-06-05 11:26:01 -0700436 return 1;
437 }
438 return 0;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800439}
440
Tao Bao40e144d2017-03-15 01:10:58 -0700441int applypatch(const char* source_filename, const char* target_filename,
Tianjie Xue40c80d2018-02-03 17:20:56 -0800442 const char* target_sha1_str, size_t /* target_size */,
Tao Bao155771b2018-06-05 11:26:01 -0700443 const std::vector<std::string>& patch_sha1s,
Tao Bao40e144d2017-03-15 01:10:58 -0700444 const std::vector<std::unique_ptr<Value>>& patch_data, const Value* bonus_data) {
Tao Bao859bfc52018-04-25 23:00:27 -0700445 LOG(INFO) << "Patching " << source_filename;
Doug Zongker512536a2010-02-17 16:11:44 -0800446
Tao Bao40e144d2017-03-15 01:10:58 -0700447 if (target_filename[0] == '-' && target_filename[1] == '\0') {
448 target_filename = source_filename;
449 }
450
451 if (strncmp(target_filename, "EMMC:", 5) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700452 LOG(ERROR) << "Supporting patching EMMC targets only";
Tao Bao40e144d2017-03-15 01:10:58 -0700453 return 1;
454 }
455
456 uint8_t target_sha1[SHA_DIGEST_LENGTH];
457 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700458 LOG(ERROR) << "Failed to parse target SHA-1 \"" << target_sha1_str << "\"";
Tao Bao40e144d2017-03-15 01:10:58 -0700459 return 1;
460 }
461
462 // We try to load the target file into the source_file object.
463 FileContents source_file;
464 if (LoadFileContents(target_filename, &source_file) == 0) {
465 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
466 // The early-exit case: the patch was already applied, this file has the desired hash, nothing
467 // for us to do.
Tao Bao859bfc52018-04-25 23:00:27 -0700468 LOG(INFO) << " already " << short_sha1(target_sha1);
Tao Bao40e144d2017-03-15 01:10:58 -0700469 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800470 }
Tao Bao40e144d2017-03-15 01:10:58 -0700471 }
Doug Zongker512536a2010-02-17 16:11:44 -0800472
Tao Bao40e144d2017-03-15 01:10:58 -0700473 if (source_file.data.empty() ||
474 (target_filename != source_filename && strcmp(target_filename, source_filename) != 0)) {
475 // Need to load the source file: either we failed to load the target file, or we did but it's
476 // different from the expected.
477 source_file.data.clear();
478 LoadFileContents(source_filename, &source_file);
479 }
480
481 if (!source_file.data.empty()) {
Tao Bao155771b2018-06-05 11:26:01 -0700482 int to_use = FindMatchingPatch(source_file.sha1, patch_sha1s);
Tao Bao40e144d2017-03-15 01:10:58 -0700483 if (to_use != -1) {
484 return GenerateTarget(source_file, patch_data[to_use], target_filename, target_sha1,
485 bonus_data);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800486 }
Tao Bao40e144d2017-03-15 01:10:58 -0700487 }
Doug Zongker512536a2010-02-17 16:11:44 -0800488
Tao Bao859bfc52018-04-25 23:00:27 -0700489 LOG(INFO) << "Source file is bad; trying copy";
Doug Zongker512536a2010-02-17 16:11:44 -0800490
Tao Bao40e144d2017-03-15 01:10:58 -0700491 FileContents copy_file;
Tao Bao8dc70492018-06-20 10:14:40 -0700492 if (LoadFileContents(Paths::Get().cache_temp_source(), &copy_file) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700493 LOG(ERROR) << "Failed to read copy file";
Tao Bao40e144d2017-03-15 01:10:58 -0700494 return 1;
495 }
Doug Zongker512536a2010-02-17 16:11:44 -0800496
Tao Bao155771b2018-06-05 11:26:01 -0700497 int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1s);
Tao Bao40e144d2017-03-15 01:10:58 -0700498 if (to_use == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700499 LOG(ERROR) << "The copy on /cache doesn't match source SHA-1s either";
Tao Bao40e144d2017-03-15 01:10:58 -0700500 return 1;
501 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800502
Tao Bao40e144d2017-03-15 01:10:58 -0700503 return GenerateTarget(copy_file, patch_data[to_use], target_filename, target_sha1, bonus_data);
Doug Zongker1c43c972012-02-28 11:07:09 -0800504}
505
Tao Baoabba55b2015-07-17 18:11:12 -0700506int applypatch_flash(const char* source_filename, const char* target_filename,
507 const char* target_sha1_str, size_t target_size) {
Tao Bao859bfc52018-04-25 23:00:27 -0700508 LOG(INFO) << "Flashing " << target_filename;
Tao Baoabba55b2015-07-17 18:11:12 -0700509
Tao Bao6e02ea92016-11-17 11:24:07 -0800510 uint8_t target_sha1[SHA_DIGEST_LENGTH];
511 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700512 LOG(ERROR) << "Failed to parse target SHA-1 \"" << target_sha1_str << "\"";
Tao Bao6e02ea92016-11-17 11:24:07 -0800513 return 1;
514 }
Tao Baoabba55b2015-07-17 18:11:12 -0700515
Tao Bao6e02ea92016-11-17 11:24:07 -0800516 std::string target_str(target_filename);
517 std::vector<std::string> pieces = android::base::Split(target_str, ":");
518 if (pieces.size() != 2 || pieces[0] != "EMMC") {
Tao Bao859bfc52018-04-25 23:00:27 -0700519 LOG(ERROR) << "Invalid target name \"" << target_filename << "\"";
Tao Bao6e02ea92016-11-17 11:24:07 -0800520 return 1;
521 }
Tao Baoabba55b2015-07-17 18:11:12 -0700522
Tao Bao6e02ea92016-11-17 11:24:07 -0800523 // Load the target into the source_file object to see if already applied.
524 pieces.push_back(std::to_string(target_size));
525 pieces.push_back(target_sha1_str);
526 std::string fullname = android::base::Join(pieces, ':');
527 FileContents source_file;
528 if (LoadPartitionContents(fullname, &source_file) == 0 &&
529 memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700530 // The early-exit case: the image was already applied, this partition has the desired hash,
531 // nothing for us to do.
532 LOG(INFO) << " already " << short_sha1(target_sha1);
Tao Baoabba55b2015-07-17 18:11:12 -0700533 return 0;
Tao Bao6e02ea92016-11-17 11:24:07 -0800534 }
535
536 if (LoadFileContents(source_filename, &source_file) == 0) {
537 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
538 // The source doesn't have desired checksum.
Tao Bao859bfc52018-04-25 23:00:27 -0700539 LOG(ERROR) << "source \"" << source_filename << "\" doesn't have expected SHA-1 sum";
540 LOG(ERROR) << "expected: " << short_sha1(target_sha1)
541 << ", found: " << short_sha1(source_file.sha1);
Tao Bao6e02ea92016-11-17 11:24:07 -0800542 return 1;
543 }
544 }
545
546 if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700547 LOG(ERROR) << "Failed to write copied data to " << target_filename;
Tao Bao6e02ea92016-11-17 11:24:07 -0800548 return 1;
549 }
550 return 0;
Tao Baoabba55b2015-07-17 18:11:12 -0700551}
552
Tao Bao40e144d2017-03-15 01:10:58 -0700553static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
554 const std::string& target_filename,
555 const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data) {
Tao Bao511d7592018-06-19 15:56:49 -0700556 if (patch->type != Value::Type::BLOB) {
Tao Bao859bfc52018-04-25 23:00:27 -0700557 LOG(ERROR) << "patch is not a blob";
Tao Bao6e02ea92016-11-17 11:24:07 -0800558 return 1;
559 }
Yabin Cuid483c202016-02-03 17:08:52 -0800560
Tao Bao6e02ea92016-11-17 11:24:07 -0800561 const char* header = &patch->data[0];
562 size_t header_bytes_read = patch->data.size();
563 bool use_bsdiff = false;
564 if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
565 use_bsdiff = true;
566 } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
567 use_bsdiff = false;
568 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700569 LOG(ERROR) << "Unknown patch file format";
Tao Bao6e02ea92016-11-17 11:24:07 -0800570 return 1;
571 }
Doug Zongker512536a2010-02-17 16:11:44 -0800572
Tao Bao40e144d2017-03-15 01:10:58 -0700573 CHECK(android::base::StartsWith(target_filename, "EMMC:"));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800574
Tao Bao40e144d2017-03-15 01:10:58 -0700575 // We still write the original source to cache, in case the partition write is interrupted.
576 if (MakeFreeSpaceOnCache(source_file.data.size()) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700577 LOG(ERROR) << "Not enough free space on /cache";
Tao Bao40e144d2017-03-15 01:10:58 -0700578 return 1;
579 }
Tao Bao8dc70492018-06-20 10:14:40 -0700580 if (SaveFileContents(Paths::Get().cache_temp_source(), &source_file) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700581 LOG(ERROR) << "Failed to back up source file";
Tao Bao40e144d2017-03-15 01:10:58 -0700582 return 1;
583 }
584
585 // We store the decoded output in memory.
Tao Bao6e02ea92016-11-17 11:24:07 -0800586 std::string memory_sink_str; // Don't need to reserve space.
Tao Bao8b0b0f12018-04-19 21:02:13 -0700587 SHA_CTX ctx;
588 SHA1_Init(&ctx);
589 SinkFn sink = [&memory_sink_str, &ctx](const unsigned char* data, size_t len) {
Tianjie Xuffed57a2018-04-23 17:51:45 -0700590 if (len != 0) {
591 uint8_t digest[SHA_DIGEST_LENGTH];
592 SHA1(data, len, digest);
Tao Bao859bfc52018-04-25 23:00:27 -0700593 LOG(DEBUG) << "Appending " << len << " bytes data, SHA-1: " << short_sha1(digest);
Tianjie Xuffed57a2018-04-23 17:51:45 -0700594 }
Tao Bao8b0b0f12018-04-19 21:02:13 -0700595 SHA1_Update(&ctx, data, len);
Tao Baoc0e1c462017-02-01 10:20:10 -0800596 memory_sink_str.append(reinterpret_cast<const char*>(data), len);
597 return len;
598 };
Doug Zongkerc4351c72010-02-22 14:46:32 -0800599
Tao Bao40e144d2017-03-15 01:10:58 -0700600 int result;
601 if (use_bsdiff) {
Tao Bao8b0b0f12018-04-19 21:02:13 -0700602 result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), *patch, 0, sink);
Tao Bao40e144d2017-03-15 01:10:58 -0700603 } else {
Tao Bao8b0b0f12018-04-19 21:02:13 -0700604 result =
605 ApplyImagePatch(source_file.data.data(), source_file.data.size(), *patch, sink, bonus_data);
Tao Bao40e144d2017-03-15 01:10:58 -0700606 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800607
Tao Bao40e144d2017-03-15 01:10:58 -0700608 if (result != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700609 LOG(ERROR) << "Failed to apply the patch: " << result;
Tao Bao40e144d2017-03-15 01:10:58 -0700610 return 1;
611 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800612
613 uint8_t current_target_sha1[SHA_DIGEST_LENGTH];
614 SHA1_Final(current_target_sha1, &ctx);
615 if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700616 LOG(ERROR) << "Patching did not produce the expected SHA-1 of " << short_sha1(target_sha1);
Tao Bao4f834302018-04-19 12:35:14 -0700617
Tao Bao859bfc52018-04-25 23:00:27 -0700618 LOG(ERROR) << "target size " << memory_sink_str.size() << " SHA-1 "
619 << short_sha1(current_target_sha1);
620 LOG(ERROR) << "source size " << source_file.data.size() << " SHA-1 "
621 << short_sha1(source_file.sha1);
Tao Bao4f834302018-04-19 12:35:14 -0700622
623 uint8_t patch_digest[SHA_DIGEST_LENGTH];
624 SHA1(reinterpret_cast<const uint8_t*>(patch->data.data()), patch->data.size(), patch_digest);
Tao Bao859bfc52018-04-25 23:00:27 -0700625 LOG(ERROR) << "patch size " << patch->data.size() << " SHA-1 " << short_sha1(patch_digest);
Tao Bao4f834302018-04-19 12:35:14 -0700626
Tao Bao7ea515e2018-07-09 15:16:13 -0700627 if (bonus_data != nullptr) {
628 uint8_t bonus_digest[SHA_DIGEST_LENGTH];
629 SHA1(reinterpret_cast<const uint8_t*>(bonus_data->data.data()), bonus_data->data.size(),
630 bonus_digest);
631 LOG(ERROR) << "bonus size " << bonus_data->data.size() << " SHA-1 "
632 << short_sha1(bonus_digest);
633 }
Tao Bao4f834302018-04-19 12:35:14 -0700634
Tianjie Xu3f638ee2018-04-26 17:23:23 -0700635 // TODO(b/67849209) Remove after debugging the unit test flakiness.
636 if (android::base::GetMinimumLogSeverity() <= android::base::LogSeverity::DEBUG) {
637 if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
638 memory_sink_str.size(), target_filename) != 0) {
639 LOG(DEBUG) << "Failed to write patched data " << target_filename;
640 }
641 }
642
Tao Bao6e02ea92016-11-17 11:24:07 -0800643 return 1;
644 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700645 LOG(INFO) << " now " << short_sha1(target_sha1);
Tao Bao6e02ea92016-11-17 11:24:07 -0800646 }
647
Tao Bao40e144d2017-03-15 01:10:58 -0700648 // Write back the temp file to the partition.
649 if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
650 memory_sink_str.size(), target_filename) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700651 LOG(ERROR) << "Failed to write patched data to " << target_filename;
Tao Bao40e144d2017-03-15 01:10:58 -0700652 return 1;
Tao Bao6e02ea92016-11-17 11:24:07 -0800653 }
654
Tao Bao40e144d2017-03-15 01:10:58 -0700655 // Delete the backup copy of the source.
Tao Bao641fa972018-04-25 18:59:40 -0700656 unlink(Paths::Get().cache_temp_source().c_str());
Tao Bao6e02ea92016-11-17 11:24:07 -0800657
658 // Success!
659 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800660}