blob: adda6976dfb4fed60d050c833e98e8d6697016ec [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 Bao5609bc82018-06-20 00:30:48 -070047using namespace std::string_literals;
48
49static bool GenerateTarget(const Partition& target, const FileContents& source_file,
Tao Bao5234ad42019-09-23 10:28:54 -070050 const Value& patch, const Value* bonus_data, bool backup_source);
Doug Zongker512536a2010-02-17 16:11:44 -080051
Tao Bao09e84932018-08-31 11:25:05 -070052bool LoadFileContents(const std::string& filename, FileContents* file) {
Tao Bao5609bc82018-06-20 00:30:48 -070053 // No longer allow loading contents from eMMC partitions.
Tao Bao8dc70492018-06-20 10:14:40 -070054 if (android::base::StartsWith(filename, "EMMC:")) {
Tao Bao09e84932018-08-31 11:25:05 -070055 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -080056 }
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 Bao09e84932018-08-31 11:25:05 -070061 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -080062 }
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);
Tao Bao09e84932018-08-31 11:25:05 -070066 return true;
Doug Zongker512536a2010-02-17 16:11:44 -080067}
68
Tao Bao5609bc82018-06-20 00:30:48 -070069// Reads the contents of a Partition to the given FileContents buffer.
70static bool ReadPartitionToBuffer(const Partition& partition, FileContents* out,
71 bool check_backup) {
72 uint8_t expected_sha1[SHA_DIGEST_LENGTH];
73 if (ParseSha1(partition.hash, expected_sha1) != 0) {
74 LOG(ERROR) << "Failed to parse target hash \"" << partition.hash << "\"";
75 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -080076 }
77
Tao Bao5609bc82018-06-20 00:30:48 -070078 android::base::unique_fd dev(open(partition.name.c_str(), O_RDONLY));
Bernie Innocenti8bd6f452019-03-28 15:48:08 +090079 if (dev == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -070080 PLOG(ERROR) << "Failed to open eMMC partition \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -070081 } else {
82 std::vector<unsigned char> buffer(partition.size);
83 if (!android::base::ReadFully(dev, buffer.data(), buffer.size())) {
84 PLOG(ERROR) << "Failed to read " << buffer.size() << " bytes of data for partition "
85 << partition;
86 } else {
87 SHA1(buffer.data(), buffer.size(), out->sha1);
88 if (memcmp(out->sha1, expected_sha1, SHA_DIGEST_LENGTH) == 0) {
89 out->data = std::move(buffer);
90 return true;
Tao Bao8fce75a2016-11-10 12:33:41 -080091 }
Doug Zongker512536a2010-02-17 16:11:44 -080092 }
Tao Bao8fce75a2016-11-10 12:33:41 -080093 }
Doug Zongker512536a2010-02-17 16:11:44 -080094
Tao Bao5609bc82018-06-20 00:30:48 -070095 if (!check_backup) {
96 LOG(ERROR) << "Partition contents don't have the expected checksum";
97 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -080098 }
Doug Zongker512536a2010-02-17 16:11:44 -080099
Tao Bao09e84932018-08-31 11:25:05 -0700100 if (LoadFileContents(Paths::Get().cache_temp_source(), out) &&
Tao Bao5609bc82018-06-20 00:30:48 -0700101 memcmp(out->sha1, expected_sha1, SHA_DIGEST_LENGTH) == 0) {
102 return true;
103 }
Doug Zongker512536a2010-02-17 16:11:44 -0800104
Tao Bao5609bc82018-06-20 00:30:48 -0700105 LOG(ERROR) << "Both of partition contents and backup don't have the expected checksum";
106 return false;
Doug Zongker512536a2010-02-17 16:11:44 -0800107}
108
Tao Bao09e84932018-08-31 11:25:05 -0700109bool SaveFileContents(const std::string& filename, const FileContents* file) {
Tianjie Xu22f11202018-08-27 10:50:31 -0700110 android::base::unique_fd fd(
111 open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
Tao Bao6e02ea92016-11-17 11:24:07 -0800112 if (fd == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700113 PLOG(ERROR) << "Failed to open \"" << filename << "\" for write";
Tao Bao09e84932018-08-31 11:25:05 -0700114 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800115 }
Doug Zongker512536a2010-02-17 16:11:44 -0800116
Tianjie Xu22f11202018-08-27 10:50:31 -0700117 if (!android::base::WriteFully(fd, file->data.data(), file->data.size())) {
118 PLOG(ERROR) << "Failed to write " << file->data.size() << " bytes of data to " << filename;
Tao Bao09e84932018-08-31 11:25:05 -0700119 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800120 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700121
122 if (fsync(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700123 PLOG(ERROR) << "Failed to fsync \"" << filename << "\"";
Tao Bao09e84932018-08-31 11:25:05 -0700124 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800125 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700126
127 if (close(fd.release()) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700128 PLOG(ERROR) << "Failed to close \"" << filename << "\"";
Tao Bao09e84932018-08-31 11:25:05 -0700129 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800130 }
Doug Zongker512536a2010-02-17 16:11:44 -0800131
Tao Bao09e84932018-08-31 11:25:05 -0700132 return true;
Doug Zongker512536a2010-02-17 16:11:44 -0800133}
134
Tao Bao5609bc82018-06-20 00:30:48 -0700135// Writes a memory buffer to 'target' Partition.
136static bool WriteBufferToPartition(const FileContents& file_contents, const Partition& partition) {
137 const unsigned char* data = file_contents.data.data();
138 size_t len = file_contents.data.size();
Tao Bao6e02ea92016-11-17 11:24:07 -0800139 size_t start = 0;
140 bool success = false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800141 for (size_t attempt = 0; attempt < 2; ++attempt) {
Tao Bao5609bc82018-06-20 00:30:48 -0700142 android::base::unique_fd fd(open(partition.name.c_str(), O_RDWR));
Tianjie Xu22f11202018-08-27 10:50:31 -0700143 if (fd == -1) {
144 PLOG(ERROR) << "Failed to open \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700145 return false;
Tianjie Xu22f11202018-08-27 10:50:31 -0700146 }
147
Tao Bao8fce75a2016-11-10 12:33:41 -0800148 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700149 PLOG(ERROR) << "Failed to seek to " << start << " on \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700150 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800151 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800152
Tianjie Xu22f11202018-08-27 10:50:31 -0700153 if (!android::base::WriteFully(fd, data + start, len - start)) {
154 PLOG(ERROR) << "Failed to write " << len - start << " bytes to \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700155 return false;
Tao Baoaca8e892015-07-17 11:47:44 -0700156 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700157
Tianjie Xu22f11202018-08-27 10:50:31 -0700158 if (fsync(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700159 PLOG(ERROR) << "Failed to sync \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700160 return false;
Doug Zongkerf291d852010-07-07 13:55:25 -0700161 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700162 if (close(fd.release()) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700163 PLOG(ERROR) << "Failed to close \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700164 return false;
Elliott Hughes63a31922016-06-09 17:41:22 -0700165 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800166
Tao Bao5609bc82018-06-20 00:30:48 -0700167 fd.reset(open(partition.name.c_str(), O_RDONLY));
Tao Bao6e02ea92016-11-17 11:24:07 -0800168 if (fd == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700169 PLOG(ERROR) << "Failed to reopen \"" << partition << "\" for verification";
Tao Bao5609bc82018-06-20 00:30:48 -0700170 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800171 }
172
173 // Drop caches so our subsequent verification read won't just be reading the cache.
Elliott Hughes63a31922016-06-09 17:41:22 -0700174 sync();
Tianjie Xu22f11202018-08-27 10:50:31 -0700175 std::string drop_cache = "/proc/sys/vm/drop_caches";
176 if (!android::base::WriteStringToFile("3\n", drop_cache)) {
177 PLOG(ERROR) << "Failed to write to " << drop_cache;
Tao Bao8fce75a2016-11-10 12:33:41 -0800178 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700179 LOG(INFO) << " caches dropped";
Tao Bao8fce75a2016-11-10 12:33:41 -0800180 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800181 sleep(1);
Elliott Hughes63a31922016-06-09 17:41:22 -0700182
Tao Bao6e02ea92016-11-17 11:24:07 -0800183 // Verify.
Tao Bao8fce75a2016-11-10 12:33:41 -0800184 if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700185 PLOG(ERROR) << "Failed to seek to 0 on " << partition;
Tao Bao5609bc82018-06-20 00:30:48 -0700186 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800187 }
188
189 unsigned char buffer[4096];
190 start = len;
191 for (size_t p = 0; p < len; p += sizeof(buffer)) {
192 size_t to_read = len - p;
193 if (to_read > sizeof(buffer)) {
194 to_read = sizeof(buffer);
195 }
196
Tianjie Xu22f11202018-08-27 10:50:31 -0700197 if (!android::base::ReadFully(fd, buffer, to_read)) {
198 PLOG(ERROR) << "Failed to verify-read " << partition << " at " << p;
Tao Bao5609bc82018-06-20 00:30:48 -0700199 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800200 }
201
202 if (memcmp(buffer, data + p, to_read) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700203 LOG(ERROR) << "Verification failed starting at " << p;
Tao Bao8fce75a2016-11-10 12:33:41 -0800204 start = p;
205 break;
206 }
207 }
208
209 if (start == len) {
Tao Bao859bfc52018-04-25 23:00:27 -0700210 LOG(INFO) << "Verification read succeeded (attempt " << attempt + 1 << ")";
Tao Bao8fce75a2016-11-10 12:33:41 -0800211 success = true;
212 break;
213 }
katao9a6f5202016-12-19 11:22:30 +0800214
Tianjie Xu22f11202018-08-27 10:50:31 -0700215 if (close(fd.release()) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700216 PLOG(ERROR) << "Failed to close " << partition;
Tao Bao5609bc82018-06-20 00:30:48 -0700217 return false;
katao9a6f5202016-12-19 11:22:30 +0800218 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800219 }
220
221 if (!success) {
Tao Bao859bfc52018-04-25 23:00:27 -0700222 LOG(ERROR) << "Failed to verify after all attempts";
Tao Bao5609bc82018-06-20 00:30:48 -0700223 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800224 }
225
Tao Bao8fce75a2016-11-10 12:33:41 -0800226 sync();
227
Tao Bao5609bc82018-06-20 00:30:48 -0700228 return true;
Doug Zongker512536a2010-02-17 16:11:44 -0800229}
230
Tao Bao8dc70492018-06-20 10:14:40 -0700231int ParseSha1(const std::string& str, uint8_t* digest) {
232 const char* ps = str.c_str();
Tao Bao155771b2018-06-05 11:26:01 -0700233 uint8_t* pd = digest;
234 for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
235 int digit;
236 if (*ps >= '0' && *ps <= '9') {
237 digit = *ps - '0';
238 } else if (*ps >= 'a' && *ps <= 'f') {
239 digit = *ps - 'a' + 10;
240 } else if (*ps >= 'A' && *ps <= 'F') {
241 digit = *ps - 'A' + 10;
242 } else {
243 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800244 }
Tao Bao155771b2018-06-05 11:26:01 -0700245 if (i % 2 == 0) {
246 *pd = digit << 4;
247 } else {
248 *pd |= digit;
249 ++pd;
250 }
251 }
252 if (*ps != '\0') return -1;
253 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800254}
255
Tao Bao5609bc82018-06-20 00:30:48 -0700256bool PatchPartitionCheck(const Partition& target, const Partition& source) {
257 FileContents target_file;
258 FileContents source_file;
259 return (ReadPartitionToBuffer(target, &target_file, false) ||
260 ReadPartitionToBuffer(source, &source_file, true));
Doug Zongker512536a2010-02-17 16:11:44 -0800261}
262
263int ShowLicenses() {
Tao Bao155771b2018-06-05 11:26:01 -0700264 ShowBSDiffLicense();
265 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800266}
267
Tao Bao5609bc82018-06-20 00:30:48 -0700268bool PatchPartition(const Partition& target, const Partition& source, const Value& patch,
Tao Bao5234ad42019-09-23 10:28:54 -0700269 const Value* bonus, bool backup_source) {
Tao Bao5609bc82018-06-20 00:30:48 -0700270 LOG(INFO) << "Patching " << target.name;
Doug Zongker512536a2010-02-17 16:11:44 -0800271
Tao Bao5609bc82018-06-20 00:30:48 -0700272 // We try to load and check against the target hash first.
273 FileContents target_file;
274 if (ReadPartitionToBuffer(target, &target_file, false)) {
275 // The early-exit case: the patch was already applied, this file has the desired hash, nothing
276 // for us to do.
277 LOG(INFO) << " already " << target.hash.substr(0, 8);
278 return true;
Tao Bao40e144d2017-03-15 01:10:58 -0700279 }
280
Tao Bao40e144d2017-03-15 01:10:58 -0700281 FileContents source_file;
Bill Peckham341644d2019-09-17 17:11:50 -0700282 if (ReadPartitionToBuffer(source, &source_file, backup_source)) {
Tao Bao5234ad42019-09-23 10:28:54 -0700283 return GenerateTarget(target, source_file, patch, bonus, backup_source);
Tao Bao40e144d2017-03-15 01:10:58 -0700284 }
Doug Zongker512536a2010-02-17 16:11:44 -0800285
Tao Bao5609bc82018-06-20 00:30:48 -0700286 LOG(ERROR) << "Failed to find any match";
287 return false;
Doug Zongker1c43c972012-02-28 11:07:09 -0800288}
289
Tao Bao5609bc82018-06-20 00:30:48 -0700290bool FlashPartition(const Partition& partition, const std::string& source_filename) {
291 LOG(INFO) << "Flashing " << partition;
Tao Baoabba55b2015-07-17 18:11:12 -0700292
Tao Bao5609bc82018-06-20 00:30:48 -0700293 // We try to load and check against the target hash first.
294 FileContents target_file;
295 if (ReadPartitionToBuffer(partition, &target_file, false)) {
296 // The early-exit case: the patch was already applied, this file has the desired hash, nothing
297 // for us to do.
298 LOG(INFO) << " already " << partition.hash.substr(0, 8);
299 return true;
Tao Bao6e02ea92016-11-17 11:24:07 -0800300 }
Tao Baoabba55b2015-07-17 18:11:12 -0700301
Tao Bao6e02ea92016-11-17 11:24:07 -0800302 FileContents source_file;
Tao Bao09e84932018-08-31 11:25:05 -0700303 if (!LoadFileContents(source_filename, &source_file)) {
Tao Bao5609bc82018-06-20 00:30:48 -0700304 LOG(ERROR) << "Failed to load source file";
305 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800306 }
307
Tao Bao5609bc82018-06-20 00:30:48 -0700308 uint8_t expected_sha1[SHA_DIGEST_LENGTH];
309 if (ParseSha1(partition.hash, expected_sha1) != 0) {
310 LOG(ERROR) << "Failed to parse source hash \"" << partition.hash << "\"";
311 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800312 }
313
Tao Bao5609bc82018-06-20 00:30:48 -0700314 if (memcmp(source_file.sha1, expected_sha1, SHA_DIGEST_LENGTH) != 0) {
315 // The source doesn't have desired checksum.
316 LOG(ERROR) << "source \"" << source_filename << "\" doesn't have expected SHA-1 sum";
317 LOG(ERROR) << "expected: " << partition.hash.substr(0, 8)
318 << ", found: " << short_sha1(source_file.sha1);
319 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800320 }
Tao Bao5609bc82018-06-20 00:30:48 -0700321 if (!WriteBufferToPartition(source_file, partition)) {
322 LOG(ERROR) << "Failed to write to " << partition;
323 return false;
324 }
325 return true;
Tao Baoabba55b2015-07-17 18:11:12 -0700326}
327
Tao Bao5609bc82018-06-20 00:30:48 -0700328static bool GenerateTarget(const Partition& target, const FileContents& source_file,
Tao Bao5234ad42019-09-23 10:28:54 -0700329 const Value& patch, const Value* bonus_data, bool backup_source) {
Tao Bao5609bc82018-06-20 00:30:48 -0700330 uint8_t expected_sha1[SHA_DIGEST_LENGTH];
331 if (ParseSha1(target.hash, expected_sha1) != 0) {
332 LOG(ERROR) << "Failed to parse target hash \"" << target.hash << "\"";
333 return false;
334 }
335
336 if (patch.type != Value::Type::BLOB) {
Tao Bao859bfc52018-04-25 23:00:27 -0700337 LOG(ERROR) << "patch is not a blob";
Tao Bao5609bc82018-06-20 00:30:48 -0700338 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800339 }
Yabin Cuid483c202016-02-03 17:08:52 -0800340
Tao Bao5609bc82018-06-20 00:30:48 -0700341 const char* header = patch.data.data();
342 size_t header_bytes_read = patch.data.size();
Tao Bao6e02ea92016-11-17 11:24:07 -0800343 bool use_bsdiff = false;
344 if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
345 use_bsdiff = true;
346 } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
347 use_bsdiff = false;
348 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700349 LOG(ERROR) << "Unknown patch file format";
Tao Bao5609bc82018-06-20 00:30:48 -0700350 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800351 }
Doug Zongker512536a2010-02-17 16:11:44 -0800352
Tao Bao5ee25662018-07-11 15:55:32 -0700353 // We write the original source to cache, in case the partition write is interrupted.
Tao Bao5234ad42019-09-23 10:28:54 -0700354 if (backup_source && !CheckAndFreeSpaceOnCache(source_file.data.size())) {
Tao Bao859bfc52018-04-25 23:00:27 -0700355 LOG(ERROR) << "Not enough free space on /cache";
Tao Bao5609bc82018-06-20 00:30:48 -0700356 return false;
Tao Bao40e144d2017-03-15 01:10:58 -0700357 }
Tao Bao5234ad42019-09-23 10:28:54 -0700358 if (backup_source && !SaveFileContents(Paths::Get().cache_temp_source(), &source_file)) {
Tao Bao859bfc52018-04-25 23:00:27 -0700359 LOG(ERROR) << "Failed to back up source file";
Tao Bao5609bc82018-06-20 00:30:48 -0700360 return false;
Tao Bao40e144d2017-03-15 01:10:58 -0700361 }
362
363 // We store the decoded output in memory.
Tao Bao5609bc82018-06-20 00:30:48 -0700364 FileContents patched;
Tao Bao8b0b0f12018-04-19 21:02:13 -0700365 SHA_CTX ctx;
366 SHA1_Init(&ctx);
Tao Bao5609bc82018-06-20 00:30:48 -0700367 SinkFn sink = [&patched, &ctx](const unsigned char* data, size_t len) {
Tao Bao8b0b0f12018-04-19 21:02:13 -0700368 SHA1_Update(&ctx, data, len);
Tao Bao5609bc82018-06-20 00:30:48 -0700369 patched.data.insert(patched.data.end(), data, data + len);
Tao Baoc0e1c462017-02-01 10:20:10 -0800370 return len;
371 };
Doug Zongkerc4351c72010-02-22 14:46:32 -0800372
Tao Bao40e144d2017-03-15 01:10:58 -0700373 int result;
374 if (use_bsdiff) {
Tao Bao5609bc82018-06-20 00:30:48 -0700375 result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), patch, 0, sink);
Tao Bao40e144d2017-03-15 01:10:58 -0700376 } else {
Tao Bao8b0b0f12018-04-19 21:02:13 -0700377 result =
Tao Bao5609bc82018-06-20 00:30:48 -0700378 ApplyImagePatch(source_file.data.data(), source_file.data.size(), patch, sink, bonus_data);
Tao Bao40e144d2017-03-15 01:10:58 -0700379 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800380
Tao Bao40e144d2017-03-15 01:10:58 -0700381 if (result != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700382 LOG(ERROR) << "Failed to apply the patch: " << result;
Tao Bao5609bc82018-06-20 00:30:48 -0700383 return false;
Tao Bao40e144d2017-03-15 01:10:58 -0700384 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800385
Tao Bao5609bc82018-06-20 00:30:48 -0700386 SHA1_Final(patched.sha1, &ctx);
387 if (memcmp(patched.sha1, expected_sha1, SHA_DIGEST_LENGTH) != 0) {
388 LOG(ERROR) << "Patching did not produce the expected SHA-1 of " << short_sha1(expected_sha1);
Tao Bao4f834302018-04-19 12:35:14 -0700389
Tao Bao5609bc82018-06-20 00:30:48 -0700390 LOG(ERROR) << "target size " << patched.data.size() << " SHA-1 " << short_sha1(patched.sha1);
Tao Bao859bfc52018-04-25 23:00:27 -0700391 LOG(ERROR) << "source size " << source_file.data.size() << " SHA-1 "
392 << short_sha1(source_file.sha1);
Tao Bao4f834302018-04-19 12:35:14 -0700393
394 uint8_t patch_digest[SHA_DIGEST_LENGTH];
Tao Bao5609bc82018-06-20 00:30:48 -0700395 SHA1(reinterpret_cast<const uint8_t*>(patch.data.data()), patch.data.size(), patch_digest);
396 LOG(ERROR) << "patch size " << patch.data.size() << " SHA-1 " << short_sha1(patch_digest);
Tao Bao4f834302018-04-19 12:35:14 -0700397
Tao Bao7ea515e2018-07-09 15:16:13 -0700398 if (bonus_data != nullptr) {
399 uint8_t bonus_digest[SHA_DIGEST_LENGTH];
400 SHA1(reinterpret_cast<const uint8_t*>(bonus_data->data.data()), bonus_data->data.size(),
401 bonus_digest);
402 LOG(ERROR) << "bonus size " << bonus_data->data.size() << " SHA-1 "
403 << short_sha1(bonus_digest);
404 }
Tao Bao4f834302018-04-19 12:35:14 -0700405
Tao Bao5609bc82018-06-20 00:30:48 -0700406 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800407 }
408
Tao Bao5609bc82018-06-20 00:30:48 -0700409 LOG(INFO) << " now " << short_sha1(expected_sha1);
410
Tao Bao40e144d2017-03-15 01:10:58 -0700411 // Write back the temp file to the partition.
Tao Bao5609bc82018-06-20 00:30:48 -0700412 if (!WriteBufferToPartition(patched, target)) {
413 LOG(ERROR) << "Failed to write patched data to " << target.name;
414 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800415 }
416
Tao Bao40e144d2017-03-15 01:10:58 -0700417 // Delete the backup copy of the source.
Tao Bao5234ad42019-09-23 10:28:54 -0700418 if (backup_source) {
419 unlink(Paths::Get().cache_temp_source().c_str());
420 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800421
422 // Success!
Tao Bao5609bc82018-06-20 00:30:48 -0700423 return true;
424}
425
426bool CheckPartition(const Partition& partition) {
427 FileContents target_file;
428 return ReadPartitionToBuffer(partition, &target_file, false);
429}
430
431Partition Partition::Parse(const std::string& input_str, std::string* err) {
432 std::vector<std::string> pieces = android::base::Split(input_str, ":");
433 if (pieces.size() != 4 || pieces[0] != "EMMC") {
434 *err = "Invalid number of tokens or non-eMMC target";
435 return {};
436 }
437
438 size_t size;
439 if (!android::base::ParseUint(pieces[2], &size) || size == 0) {
440 *err = "Failed to parse \"" + pieces[2] + "\" as byte count";
441 return {};
442 }
443
444 return Partition(pieces[1], size, pieces[3]);
445}
446
447std::string Partition::ToString() const {
448 if (*this) {
449 return "EMMC:"s + name + ":" + std::to_string(size) + ":" + hash;
450 }
451 return "<invalid-partition>";
452}
453
454std::ostream& operator<<(std::ostream& os, const Partition& partition) {
455 os << partition.ToString();
456 return os;
Doug Zongker512536a2010-02-17 16:11:44 -0800457}