blob: bc8ea79a987747bd5051065f35a8cc42ccc514b4 [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>
Doug Zongker512536a2010-02-17 16:11:44 -080026#include <sys/types.h>
Doug Zongker512536a2010-02-17 16:11:44 -080027#include <unistd.h>
28
Tianjie Xu22f11202018-08-27 10:50:31 -070029#include <algorithm>
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
Tianjie Xu22f11202018-08-27 10:50:31 -070036#include <android-base/file.h>
Tao Bao40e144d2017-03-15 01:10:58 -070037#include <android-base/logging.h>
Tao Bao8fce75a2016-11-10 12:33:41 -080038#include <android-base/parseint.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080039#include <android-base/strings.h>
Tianjie Xu22f11202018-08-27 10:50:31 -070040#include <android-base/unique_fd.h>
Tao Bao8fce75a2016-11-10 12:33:41 -080041#include <openssl/sha.h>
Tao Baoaca8e892015-07-17 11:47:44 -070042
Doug Zongkerc4351c72010-02-22 14:46:32 -080043#include "edify/expr.h"
Tao Bao641fa972018-04-25 18:59:40 -070044#include "otautil/paths.h"
Tao Bao09e468f2017-09-29 14:39:33 -070045#include "otautil/print_sha1.h"
Doug Zongker512536a2010-02-17 16:11:44 -080046
Tao Bao8fce75a2016-11-10 12:33:41 -080047static int LoadPartitionContents(const std::string& filename, FileContents* file);
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
Tianjie Xu22f11202018-08-27 10:50:31 -070058 std::string data;
59 if (!android::base::ReadFileToString(filename, &data)) {
60 PLOG(ERROR) << "Failed to read \"" << filename << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -080061 return -1;
62 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080063
Tianjie Xu22f11202018-08-27 10:50:31 -070064 file->data = std::vector<unsigned char>(data.begin(), data.end());
Tao Bao8fce75a2016-11-10 12:33:41 -080065 SHA1(file->data.data(), file->data.size(), file->sha1);
66 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -080067}
68
Tao Bao155771b2018-06-05 11:26:01 -070069// Loads the contents of an EMMC partition into the provided FileContents. filename should be a
70// string of the form "EMMC:<partition_device>:...". The smallest size_n bytes for which that prefix
71// of the partition contents has the corresponding sha1 hash will be loaded. It is acceptable for a
72// size value to be repeated with different sha1s. Returns 0 on success.
Doug Zongker512536a2010-02-17 16:11:44 -080073//
Tao Bao155771b2018-06-05 11:26:01 -070074// This complexity is needed because if an OTA installation is interrupted, the partition might
75// contain either the source or the target data, which might be of different lengths. We need to
76// know the length in order to read from a partition (there is no "end-of-file" marker), so the
77// caller must specify the possible lengths and the hash of the data, and we'll do the load
78// expecting to find one of those hashes.
Tao Bao8fce75a2016-11-10 12:33:41 -080079static int LoadPartitionContents(const std::string& filename, FileContents* file) {
80 std::vector<std::string> pieces = android::base::Split(filename, ":");
81 if (pieces.size() < 4 || pieces.size() % 2 != 0 || pieces[0] != "EMMC") {
Tao Bao859bfc52018-04-25 23:00:27 -070082 LOG(ERROR) << "LoadPartitionContents called with bad filename \"" << filename << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -080083 return -1;
84 }
85
86 size_t pair_count = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename
87 std::vector<std::pair<size_t, std::string>> pairs;
88 for (size_t i = 0; i < pair_count; ++i) {
89 size_t size;
90 if (!android::base::ParseUint(pieces[i * 2 + 2], &size) || size == 0) {
Tao Bao859bfc52018-04-25 23:00:27 -070091 LOG(ERROR) << "LoadPartitionContents called with bad size \"" << pieces[i * 2 + 2] << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -080092 return -1;
93 }
94 pairs.push_back({ size, pieces[i * 2 + 3] });
95 }
96
97 // Sort the pairs array so that they are in order of increasing size.
98 std::sort(pairs.begin(), pairs.end());
99
100 const char* partition = pieces[1].c_str();
Tianjie Xu22f11202018-08-27 10:50:31 -0700101 android::base::unique_fd dev(open(partition, O_RDONLY));
102 if (dev == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700103 PLOG(ERROR) << "Failed to open eMMC partition \"" << partition << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800104 return -1;
105 }
106
107 SHA_CTX sha_ctx;
108 SHA1_Init(&sha_ctx);
109
110 // Allocate enough memory to hold the largest size.
111 std::vector<unsigned char> buffer(pairs[pair_count - 1].first);
Tianjie Xu22f11202018-08-27 10:50:31 -0700112 size_t offset = 0; // # bytes read so far
Tao Bao8fce75a2016-11-10 12:33:41 -0800113 bool found = false;
114
115 for (const auto& pair : pairs) {
116 size_t current_size = pair.first;
117 const std::string& current_sha1 = pair.second;
118
119 // Read enough additional bytes to get us up to the next size. (Again,
120 // we're trying the possibilities in order of increasing size).
Tianjie Xu22f11202018-08-27 10:50:31 -0700121 if (current_size - offset > 0) {
122 if (!android::base::ReadFully(dev, buffer.data() + offset, current_size - offset)) {
123 PLOG(ERROR) << "Failed to read " << current_size - offset << " bytes of data at offset "
124 << offset << " for partition " << partition;
Tao Baoaca8e892015-07-17 11:47:44 -0700125 return -1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800126 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700127
Tianjie Xu22f11202018-08-27 10:50:31 -0700128 SHA1_Update(&sha_ctx, buffer.data() + offset, current_size - offset);
129 offset = current_size;
130 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800131 // Duplicate the SHA context and finalize the duplicate so we can
132 // check it against this pair's expected hash.
133 SHA_CTX temp_ctx;
134 memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
135 uint8_t sha_so_far[SHA_DIGEST_LENGTH];
136 SHA1_Final(sha_so_far, &temp_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800137
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800138 uint8_t parsed_sha[SHA_DIGEST_LENGTH];
Tao Bao8dc70492018-06-20 10:14:40 -0700139 if (ParseSha1(current_sha1, parsed_sha) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700140 LOG(ERROR) << "Failed to parse SHA-1 \"" << current_sha1 << "\" in " << filename;
Tao Bao8fce75a2016-11-10 12:33:41 -0800141 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800142 }
143
Tao Bao8fce75a2016-11-10 12:33:41 -0800144 if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) {
145 // 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 -0700146 LOG(INFO) << "Partition read matched size " << current_size << " SHA-1 " << current_sha1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800147 found = true;
148 break;
Doug Zongker512536a2010-02-17 16:11:44 -0800149 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800150 }
Doug Zongker512536a2010-02-17 16:11:44 -0800151
Tao Bao8fce75a2016-11-10 12:33:41 -0800152 if (!found) {
153 // Ran off the end of the list of (size, sha1) pairs without finding a match.
Tao Bao859bfc52018-04-25 23:00:27 -0700154 LOG(ERROR) << "Contents of partition \"" << partition << "\" didn't match " << filename;
Tao Bao8fce75a2016-11-10 12:33:41 -0800155 return -1;
156 }
Doug Zongker512536a2010-02-17 16:11:44 -0800157
Tao Bao8fce75a2016-11-10 12:33:41 -0800158 SHA1_Final(file->sha1, &sha_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800159
Tianjie Xu22f11202018-08-27 10:50:31 -0700160 buffer.resize(offset);
Tao Bao8fce75a2016-11-10 12:33:41 -0800161 file->data = std::move(buffer);
Tao Bao8fce75a2016-11-10 12:33:41 -0800162
163 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800164}
165
Tao Bao8dc70492018-06-20 10:14:40 -0700166int SaveFileContents(const std::string& filename, const FileContents* file) {
Tianjie Xu22f11202018-08-27 10:50:31 -0700167 android::base::unique_fd fd(
168 open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
Tao Bao6e02ea92016-11-17 11:24:07 -0800169 if (fd == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700170 PLOG(ERROR) << "Failed to open \"" << filename << "\" for write";
Tao Bao6e02ea92016-11-17 11:24:07 -0800171 return -1;
172 }
Doug Zongker512536a2010-02-17 16:11:44 -0800173
Tianjie Xu22f11202018-08-27 10:50:31 -0700174 if (!android::base::WriteFully(fd, file->data.data(), file->data.size())) {
175 PLOG(ERROR) << "Failed to write " << file->data.size() << " bytes of data to " << filename;
Tao Bao6e02ea92016-11-17 11:24:07 -0800176 return -1;
177 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700178
179 if (fsync(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700180 PLOG(ERROR) << "Failed to fsync \"" << filename << "\"";
Tao Bao6e02ea92016-11-17 11:24:07 -0800181 return -1;
182 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700183
184 if (close(fd.release()) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700185 PLOG(ERROR) << "Failed to close \"" << filename << "\"";
Tao Bao6e02ea92016-11-17 11:24:07 -0800186 return -1;
187 }
Doug Zongker512536a2010-02-17 16:11:44 -0800188
Tao Bao6e02ea92016-11-17 11:24:07 -0800189 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800190}
191
Tao Bao155771b2018-06-05 11:26:01 -0700192// Writes a memory buffer to 'target' partition, a string of the form
193// "EMMC:<partition_device>[:...]". The target name might contain multiple colons, but
194// WriteToPartition() only uses the first two and ignores the rest. Returns 0 on success.
195static int WriteToPartition(const unsigned char* data, size_t len, const std::string& target) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800196 std::vector<std::string> pieces = android::base::Split(target, ":");
Tao Bao8fce75a2016-11-10 12:33:41 -0800197 if (pieces.size() < 2 || pieces[0] != "EMMC") {
Tao Bao859bfc52018-04-25 23:00:27 -0700198 LOG(ERROR) << "WriteToPartition called with bad target \"" << target << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800199 return -1;
200 }
201
Tao Bao6e02ea92016-11-17 11:24:07 -0800202 size_t start = 0;
203 bool success = false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800204 for (size_t attempt = 0; attempt < 2; ++attempt) {
Tianjie Xu22f11202018-08-27 10:50:31 -0700205 std::string partition = pieces[1];
206 android::base::unique_fd fd(open(partition.c_str(), O_RDWR));
207 if (fd == -1) {
208 PLOG(ERROR) << "Failed to open \"" << partition << "\"";
209 return -1;
210 }
211
Tao Bao8fce75a2016-11-10 12:33:41 -0800212 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700213 PLOG(ERROR) << "Failed to seek to " << start << " on \"" << partition << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800214 return -1;
215 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800216
Tianjie Xu22f11202018-08-27 10:50:31 -0700217 if (!android::base::WriteFully(fd, data + start, len - start)) {
218 PLOG(ERROR) << "Failed to write " << len - start << " bytes to \"" << partition << "\"";
219 return -1;
Tao Baoaca8e892015-07-17 11:47:44 -0700220 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700221
Tianjie Xu22f11202018-08-27 10:50:31 -0700222 if (fsync(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700223 PLOG(ERROR) << "Failed to sync \"" << partition << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800224 return -1;
Doug Zongkerf291d852010-07-07 13:55:25 -0700225 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700226 if (close(fd.release()) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700227 PLOG(ERROR) << "Failed to close \"" << partition << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800228 return -1;
Elliott Hughes63a31922016-06-09 17:41:22 -0700229 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800230
Tianjie Xu22f11202018-08-27 10:50:31 -0700231 fd.reset(open(partition.c_str(), O_RDONLY));
Tao Bao6e02ea92016-11-17 11:24:07 -0800232 if (fd == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700233 PLOG(ERROR) << "Failed to reopen \"" << partition << "\" for verification";
Tao Bao8fce75a2016-11-10 12:33:41 -0800234 return -1;
235 }
236
237 // Drop caches so our subsequent verification read won't just be reading the cache.
Elliott Hughes63a31922016-06-09 17:41:22 -0700238 sync();
Tianjie Xu22f11202018-08-27 10:50:31 -0700239 std::string drop_cache = "/proc/sys/vm/drop_caches";
240 if (!android::base::WriteStringToFile("3\n", drop_cache)) {
241 PLOG(ERROR) << "Failed to write to " << drop_cache;
Tao Bao8fce75a2016-11-10 12:33:41 -0800242 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700243 LOG(INFO) << " caches dropped";
Tao Bao8fce75a2016-11-10 12:33:41 -0800244 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800245 sleep(1);
Elliott Hughes63a31922016-06-09 17:41:22 -0700246
Tao Bao6e02ea92016-11-17 11:24:07 -0800247 // Verify.
Tao Bao8fce75a2016-11-10 12:33:41 -0800248 if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700249 PLOG(ERROR) << "Failed to seek to 0 on " << partition;
Tao Bao8fce75a2016-11-10 12:33:41 -0800250 return -1;
251 }
252
253 unsigned char buffer[4096];
254 start = len;
255 for (size_t p = 0; p < len; p += sizeof(buffer)) {
256 size_t to_read = len - p;
257 if (to_read > sizeof(buffer)) {
258 to_read = sizeof(buffer);
259 }
260
Tianjie Xu22f11202018-08-27 10:50:31 -0700261 if (!android::base::ReadFully(fd, buffer, to_read)) {
262 PLOG(ERROR) << "Failed to verify-read " << partition << " at " << p;
263 return -1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800264 }
265
266 if (memcmp(buffer, data + p, to_read) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700267 LOG(ERROR) << "Verification failed starting at " << p;
Tao Bao8fce75a2016-11-10 12:33:41 -0800268 start = p;
269 break;
270 }
271 }
272
273 if (start == len) {
Tao Bao859bfc52018-04-25 23:00:27 -0700274 LOG(INFO) << "Verification read succeeded (attempt " << attempt + 1 << ")";
Tao Bao8fce75a2016-11-10 12:33:41 -0800275 success = true;
276 break;
277 }
katao9a6f5202016-12-19 11:22:30 +0800278
Tianjie Xu22f11202018-08-27 10:50:31 -0700279 if (close(fd.release()) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700280 PLOG(ERROR) << "Failed to close " << partition;
katao9a6f5202016-12-19 11:22:30 +0800281 return -1;
282 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800283 }
284
285 if (!success) {
Tao Bao859bfc52018-04-25 23:00:27 -0700286 LOG(ERROR) << "Failed to verify after all attempts";
Tao Bao8fce75a2016-11-10 12:33:41 -0800287 return -1;
288 }
289
Tao Bao8fce75a2016-11-10 12:33:41 -0800290 sync();
291
292 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800293}
294
Tao Bao8dc70492018-06-20 10:14:40 -0700295int ParseSha1(const std::string& str, uint8_t* digest) {
296 const char* ps = str.c_str();
Tao Bao155771b2018-06-05 11:26:01 -0700297 uint8_t* pd = digest;
298 for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
299 int digit;
300 if (*ps >= '0' && *ps <= '9') {
301 digit = *ps - '0';
302 } else if (*ps >= 'a' && *ps <= 'f') {
303 digit = *ps - 'a' + 10;
304 } else if (*ps >= 'A' && *ps <= 'F') {
305 digit = *ps - 'A' + 10;
306 } else {
307 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800308 }
Tao Bao155771b2018-06-05 11:26:01 -0700309 if (i % 2 == 0) {
310 *pd = digit << 4;
311 } else {
312 *pd |= digit;
313 ++pd;
314 }
315 }
316 if (*ps != '\0') return -1;
317 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800318}
319
Tao Bao155771b2018-06-05 11:26:01 -0700320// Searches a vector of SHA-1 strings for one matching the given SHA-1. Returns the index of the
321// match on success, or -1 if no match is found.
322static int FindMatchingPatch(const uint8_t* sha1, const std::vector<std::string>& patch_sha1s) {
323 for (size_t i = 0; i < patch_sha1s.size(); ++i) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800324 uint8_t patch_sha1[SHA_DIGEST_LENGTH];
Tao Bao8dc70492018-06-20 10:14:40 -0700325 if (ParseSha1(patch_sha1s[i], patch_sha1) == 0 &&
Tao Bao8fce75a2016-11-10 12:33:41 -0800326 memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
327 return i;
Doug Zongker512536a2010-02-17 16:11:44 -0800328 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800329 }
330 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800331}
332
Tao Bao7c1d4262018-07-06 23:18:14 -0700333int applypatch_check(const std::string& filename, const std::vector<std::string>& sha1s) {
334 if (!android::base::StartsWith(filename, "EMMC:")) {
335 return 1;
336 }
337
338 // The check will pass if LoadPartitionContents is successful, because the filename already
339 // encodes the desired SHA-1s.
Tao Bao8fce75a2016-11-10 12:33:41 -0800340 FileContents file;
Tao Bao7c1d4262018-07-06 23:18:14 -0700341 if (LoadPartitionContents(filename, &file) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700342 LOG(INFO) << "\"" << filename << "\" doesn't have any of expected SHA-1 sums; checking cache";
Doug Zongker512536a2010-02-17 16:11:44 -0800343
Tao Bao7c1d4262018-07-06 23:18:14 -0700344 // If the partition is corrupted, it might be because we were killed in the middle of patching
345 // it. A copy should have been made in cache_temp_source. If that file exists and matches the
346 // SHA-1 we're looking for, the check still passes.
Tao Bao8dc70492018-06-20 10:14:40 -0700347 if (LoadFileContents(Paths::Get().cache_temp_source(), &file) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700348 LOG(ERROR) << "Failed to load cache file";
Tao Bao8fce75a2016-11-10 12:33:41 -0800349 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800350 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800351
Tao Bao7c1d4262018-07-06 23:18:14 -0700352 if (FindMatchingPatch(file.sha1, sha1s) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700353 LOG(ERROR) << "The cache bits don't match any SHA-1 for \"" << filename << "\"";
Tao Bao8fce75a2016-11-10 12:33:41 -0800354 return 1;
355 }
356 }
357 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800358}
359
360int ShowLicenses() {
Tao Bao155771b2018-06-05 11:26:01 -0700361 ShowBSDiffLicense();
362 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800363}
364
Tao Bao40e144d2017-03-15 01:10:58 -0700365int applypatch(const char* source_filename, const char* target_filename,
Tianjie Xue40c80d2018-02-03 17:20:56 -0800366 const char* target_sha1_str, size_t /* target_size */,
Tao Bao155771b2018-06-05 11:26:01 -0700367 const std::vector<std::string>& patch_sha1s,
Tao Bao40e144d2017-03-15 01:10:58 -0700368 const std::vector<std::unique_ptr<Value>>& patch_data, const Value* bonus_data) {
Tao Bao859bfc52018-04-25 23:00:27 -0700369 LOG(INFO) << "Patching " << source_filename;
Doug Zongker512536a2010-02-17 16:11:44 -0800370
Tao Bao40e144d2017-03-15 01:10:58 -0700371 if (target_filename[0] == '-' && target_filename[1] == '\0') {
372 target_filename = source_filename;
373 }
374
375 if (strncmp(target_filename, "EMMC:", 5) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700376 LOG(ERROR) << "Supporting patching EMMC targets only";
Tao Bao40e144d2017-03-15 01:10:58 -0700377 return 1;
378 }
379
380 uint8_t target_sha1[SHA_DIGEST_LENGTH];
381 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700382 LOG(ERROR) << "Failed to parse target SHA-1 \"" << target_sha1_str << "\"";
Tao Bao40e144d2017-03-15 01:10:58 -0700383 return 1;
384 }
385
386 // We try to load the target file into the source_file object.
387 FileContents source_file;
388 if (LoadFileContents(target_filename, &source_file) == 0) {
389 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
390 // The early-exit case: the patch was already applied, this file has the desired hash, nothing
391 // for us to do.
Tao Bao859bfc52018-04-25 23:00:27 -0700392 LOG(INFO) << " already " << short_sha1(target_sha1);
Tao Bao40e144d2017-03-15 01:10:58 -0700393 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800394 }
Tao Bao40e144d2017-03-15 01:10:58 -0700395 }
Doug Zongker512536a2010-02-17 16:11:44 -0800396
Tao Bao40e144d2017-03-15 01:10:58 -0700397 if (source_file.data.empty() ||
398 (target_filename != source_filename && strcmp(target_filename, source_filename) != 0)) {
399 // Need to load the source file: either we failed to load the target file, or we did but it's
400 // different from the expected.
401 source_file.data.clear();
402 LoadFileContents(source_filename, &source_file);
403 }
404
405 if (!source_file.data.empty()) {
Tao Bao155771b2018-06-05 11:26:01 -0700406 int to_use = FindMatchingPatch(source_file.sha1, patch_sha1s);
Tao Bao40e144d2017-03-15 01:10:58 -0700407 if (to_use != -1) {
408 return GenerateTarget(source_file, patch_data[to_use], target_filename, target_sha1,
409 bonus_data);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800410 }
Tao Bao40e144d2017-03-15 01:10:58 -0700411 }
Doug Zongker512536a2010-02-17 16:11:44 -0800412
Tao Bao859bfc52018-04-25 23:00:27 -0700413 LOG(INFO) << "Source file is bad; trying copy";
Doug Zongker512536a2010-02-17 16:11:44 -0800414
Tao Bao40e144d2017-03-15 01:10:58 -0700415 FileContents copy_file;
Tao Bao8dc70492018-06-20 10:14:40 -0700416 if (LoadFileContents(Paths::Get().cache_temp_source(), &copy_file) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700417 LOG(ERROR) << "Failed to read copy file";
Tao Bao40e144d2017-03-15 01:10:58 -0700418 return 1;
419 }
Doug Zongker512536a2010-02-17 16:11:44 -0800420
Tao Bao155771b2018-06-05 11:26:01 -0700421 int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1s);
Tao Bao40e144d2017-03-15 01:10:58 -0700422 if (to_use == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700423 LOG(ERROR) << "The copy on /cache doesn't match source SHA-1s either";
Tao Bao40e144d2017-03-15 01:10:58 -0700424 return 1;
425 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800426
Tao Bao40e144d2017-03-15 01:10:58 -0700427 return GenerateTarget(copy_file, patch_data[to_use], target_filename, target_sha1, bonus_data);
Doug Zongker1c43c972012-02-28 11:07:09 -0800428}
429
Tao Baoabba55b2015-07-17 18:11:12 -0700430int applypatch_flash(const char* source_filename, const char* target_filename,
431 const char* target_sha1_str, size_t target_size) {
Tao Bao859bfc52018-04-25 23:00:27 -0700432 LOG(INFO) << "Flashing " << target_filename;
Tao Baoabba55b2015-07-17 18:11:12 -0700433
Tao Bao6e02ea92016-11-17 11:24:07 -0800434 uint8_t target_sha1[SHA_DIGEST_LENGTH];
435 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700436 LOG(ERROR) << "Failed to parse target SHA-1 \"" << target_sha1_str << "\"";
Tao Bao6e02ea92016-11-17 11:24:07 -0800437 return 1;
438 }
Tao Baoabba55b2015-07-17 18:11:12 -0700439
Tao Baod34e6bc2018-07-13 13:11:09 -0700440 std::vector<std::string> pieces = android::base::Split(target_filename, ":");
441 if (pieces.size() != 4 || pieces[0] != "EMMC") {
Tao Bao859bfc52018-04-25 23:00:27 -0700442 LOG(ERROR) << "Invalid target name \"" << target_filename << "\"";
Tao Bao6e02ea92016-11-17 11:24:07 -0800443 return 1;
444 }
Tao Baoabba55b2015-07-17 18:11:12 -0700445
Tao Bao6e02ea92016-11-17 11:24:07 -0800446 // Load the target into the source_file object to see if already applied.
Tao Bao6e02ea92016-11-17 11:24:07 -0800447 FileContents source_file;
Tao Baod34e6bc2018-07-13 13:11:09 -0700448 if (LoadPartitionContents(target_filename, &source_file) == 0 &&
Tao Bao6e02ea92016-11-17 11:24:07 -0800449 memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700450 // The early-exit case: the image was already applied, this partition has the desired hash,
451 // nothing for us to do.
452 LOG(INFO) << " already " << short_sha1(target_sha1);
Tao Baoabba55b2015-07-17 18:11:12 -0700453 return 0;
Tao Bao6e02ea92016-11-17 11:24:07 -0800454 }
455
456 if (LoadFileContents(source_filename, &source_file) == 0) {
457 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
458 // The source doesn't have desired checksum.
Tao Bao859bfc52018-04-25 23:00:27 -0700459 LOG(ERROR) << "source \"" << source_filename << "\" doesn't have expected SHA-1 sum";
460 LOG(ERROR) << "expected: " << short_sha1(target_sha1)
461 << ", found: " << short_sha1(source_file.sha1);
Tao Bao6e02ea92016-11-17 11:24:07 -0800462 return 1;
463 }
464 }
465
466 if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700467 LOG(ERROR) << "Failed to write copied data to " << target_filename;
Tao Bao6e02ea92016-11-17 11:24:07 -0800468 return 1;
469 }
470 return 0;
Tao Baoabba55b2015-07-17 18:11:12 -0700471}
472
Tao Bao40e144d2017-03-15 01:10:58 -0700473static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
474 const std::string& target_filename,
475 const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data) {
Tao Bao511d7592018-06-19 15:56:49 -0700476 if (patch->type != Value::Type::BLOB) {
Tao Bao859bfc52018-04-25 23:00:27 -0700477 LOG(ERROR) << "patch is not a blob";
Tao Bao6e02ea92016-11-17 11:24:07 -0800478 return 1;
479 }
Yabin Cuid483c202016-02-03 17:08:52 -0800480
Tao Bao6e02ea92016-11-17 11:24:07 -0800481 const char* header = &patch->data[0];
482 size_t header_bytes_read = patch->data.size();
483 bool use_bsdiff = false;
484 if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
485 use_bsdiff = true;
486 } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
487 use_bsdiff = false;
488 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700489 LOG(ERROR) << "Unknown patch file format";
Tao Bao6e02ea92016-11-17 11:24:07 -0800490 return 1;
491 }
Doug Zongker512536a2010-02-17 16:11:44 -0800492
Tao Bao40e144d2017-03-15 01:10:58 -0700493 CHECK(android::base::StartsWith(target_filename, "EMMC:"));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800494
Tao Bao5ee25662018-07-11 15:55:32 -0700495 // We write the original source to cache, in case the partition write is interrupted.
496 if (!CheckAndFreeSpaceOnCache(source_file.data.size())) {
Tao Bao859bfc52018-04-25 23:00:27 -0700497 LOG(ERROR) << "Not enough free space on /cache";
Tao Bao40e144d2017-03-15 01:10:58 -0700498 return 1;
499 }
Tao Bao8dc70492018-06-20 10:14:40 -0700500 if (SaveFileContents(Paths::Get().cache_temp_source(), &source_file) < 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700501 LOG(ERROR) << "Failed to back up source file";
Tao Bao40e144d2017-03-15 01:10:58 -0700502 return 1;
503 }
504
505 // We store the decoded output in memory.
Tao Bao6e02ea92016-11-17 11:24:07 -0800506 std::string memory_sink_str; // Don't need to reserve space.
Tao Bao8b0b0f12018-04-19 21:02:13 -0700507 SHA_CTX ctx;
508 SHA1_Init(&ctx);
509 SinkFn sink = [&memory_sink_str, &ctx](const unsigned char* data, size_t len) {
510 SHA1_Update(&ctx, data, len);
Tao Baoc0e1c462017-02-01 10:20:10 -0800511 memory_sink_str.append(reinterpret_cast<const char*>(data), len);
512 return len;
513 };
Doug Zongkerc4351c72010-02-22 14:46:32 -0800514
Tao Bao40e144d2017-03-15 01:10:58 -0700515 int result;
516 if (use_bsdiff) {
Tao Bao8b0b0f12018-04-19 21:02:13 -0700517 result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), *patch, 0, sink);
Tao Bao40e144d2017-03-15 01:10:58 -0700518 } else {
Tao Bao8b0b0f12018-04-19 21:02:13 -0700519 result =
520 ApplyImagePatch(source_file.data.data(), source_file.data.size(), *patch, sink, bonus_data);
Tao Bao40e144d2017-03-15 01:10:58 -0700521 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800522
Tao Bao40e144d2017-03-15 01:10:58 -0700523 if (result != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700524 LOG(ERROR) << "Failed to apply the patch: " << result;
Tao Bao40e144d2017-03-15 01:10:58 -0700525 return 1;
526 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800527
528 uint8_t current_target_sha1[SHA_DIGEST_LENGTH];
529 SHA1_Final(current_target_sha1, &ctx);
530 if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700531 LOG(ERROR) << "Patching did not produce the expected SHA-1 of " << short_sha1(target_sha1);
Tao Bao4f834302018-04-19 12:35:14 -0700532
Tao Bao859bfc52018-04-25 23:00:27 -0700533 LOG(ERROR) << "target size " << memory_sink_str.size() << " SHA-1 "
534 << short_sha1(current_target_sha1);
535 LOG(ERROR) << "source size " << source_file.data.size() << " SHA-1 "
536 << short_sha1(source_file.sha1);
Tao Bao4f834302018-04-19 12:35:14 -0700537
538 uint8_t patch_digest[SHA_DIGEST_LENGTH];
539 SHA1(reinterpret_cast<const uint8_t*>(patch->data.data()), patch->data.size(), patch_digest);
Tao Bao859bfc52018-04-25 23:00:27 -0700540 LOG(ERROR) << "patch size " << patch->data.size() << " SHA-1 " << short_sha1(patch_digest);
Tao Bao4f834302018-04-19 12:35:14 -0700541
Tao Bao7ea515e2018-07-09 15:16:13 -0700542 if (bonus_data != nullptr) {
543 uint8_t bonus_digest[SHA_DIGEST_LENGTH];
544 SHA1(reinterpret_cast<const uint8_t*>(bonus_data->data.data()), bonus_data->data.size(),
545 bonus_digest);
546 LOG(ERROR) << "bonus size " << bonus_data->data.size() << " SHA-1 "
547 << short_sha1(bonus_digest);
548 }
Tao Bao4f834302018-04-19 12:35:14 -0700549
Tao Bao6e02ea92016-11-17 11:24:07 -0800550 return 1;
551 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700552 LOG(INFO) << " now " << short_sha1(target_sha1);
Tao Bao6e02ea92016-11-17 11:24:07 -0800553 }
554
Tao Bao40e144d2017-03-15 01:10:58 -0700555 // Write back the temp file to the partition.
556 if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
557 memory_sink_str.size(), target_filename) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700558 LOG(ERROR) << "Failed to write patched data to " << target_filename;
Tao Bao40e144d2017-03-15 01:10:58 -0700559 return 1;
Tao Bao6e02ea92016-11-17 11:24:07 -0800560 }
561
Tao Bao40e144d2017-03-15 01:10:58 -0700562 // Delete the backup copy of the source.
Tao Bao641fa972018-04-25 18:59:40 -0700563 unlink(Paths::Get().cache_temp_source().c_str());
Tao Bao6e02ea92016-11-17 11:24:07 -0800564
565 // Success!
566 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800567}