blob: 206a3a33428771c8a1cc9ff217ed35f2a8fffc27 [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
Conn O'Griofad9201042014-03-25 01:26:49 +000043#include "bmlutils/bmlutils.h"
Doug Zongker512536a2010-02-17 16:11:44 -080044#include "mtdutils/mtdutils.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050045
Doug Zongkerc4351c72010-02-22 14:46:32 -080046#include "edify/expr.h"
Tao Bao641fa972018-04-25 18:59:40 -070047#include "otautil/paths.h"
Tao Bao09e468f2017-09-29 14:39:33 -070048#include "otautil/print_sha1.h"
Doug Zongker512536a2010-02-17 16:11:44 -080049
Tao Bao5609bc82018-06-20 00:30:48 -070050using namespace std::string_literals;
Doug Zongker512536a2010-02-17 16:11:44 -080051
Tao Bao5609bc82018-06-20 00:30:48 -070052static bool GenerateTarget(const Partition& target, const FileContents& source_file,
Tao Bao5234ad42019-09-23 10:28:54 -070053 const Value& patch, const Value* bonus_data, bool backup_source);
Doug Zongker512536a2010-02-17 16:11:44 -080054
Tao Bao09e84932018-08-31 11:25:05 -070055bool LoadFileContents(const std::string& filename, FileContents* file) {
Tao Bao5609bc82018-06-20 00:30:48 -070056 // No longer allow loading contents from eMMC partitions.
Tao Bao8dc70492018-06-20 10:14:40 -070057 if (android::base::StartsWith(filename, "EMMC:")) {
Tao Bao09e84932018-08-31 11:25:05 -070058 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -080059 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080060
Tianjie Xu22f11202018-08-27 10:50:31 -070061 std::string data;
62 if (!android::base::ReadFileToString(filename, &data)) {
63 PLOG(ERROR) << "Failed to read \"" << filename << "\"";
Tao Bao09e84932018-08-31 11:25:05 -070064 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -080065 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080066
Tianjie Xu22f11202018-08-27 10:50:31 -070067 file->data = std::vector<unsigned char>(data.begin(), data.end());
Tao Bao8fce75a2016-11-10 12:33:41 -080068 SHA1(file->data.data(), file->data.size(), file->sha1);
Tao Bao09e84932018-08-31 11:25:05 -070069 return true;
Doug Zongker512536a2010-02-17 16:11:44 -080070}
71
Tao Bao5609bc82018-06-20 00:30:48 -070072// Reads the contents of a Partition to the given FileContents buffer.
73static bool ReadPartitionToBuffer(const Partition& partition, FileContents* out,
74 bool check_backup) {
75 uint8_t expected_sha1[SHA_DIGEST_LENGTH];
76 if (ParseSha1(partition.hash, expected_sha1) != 0) {
77 LOG(ERROR) << "Failed to parse target hash \"" << partition.hash << "\"";
78 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -080079 }
80
Tao Bao5609bc82018-06-20 00:30:48 -070081 android::base::unique_fd dev(open(partition.name.c_str(), O_RDONLY));
Bernie Innocenti8bd6f452019-03-28 15:48:08 +090082 if (dev == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -070083 PLOG(ERROR) << "Failed to open eMMC partition \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -070084 } else {
85 std::vector<unsigned char> buffer(partition.size);
86 if (!android::base::ReadFully(dev, buffer.data(), buffer.size())) {
87 PLOG(ERROR) << "Failed to read " << buffer.size() << " bytes of data for partition "
88 << partition;
89 } else {
90 SHA1(buffer.data(), buffer.size(), out->sha1);
91 if (memcmp(out->sha1, expected_sha1, SHA_DIGEST_LENGTH) == 0) {
92 out->data = std::move(buffer);
93 return true;
Tao Bao8fce75a2016-11-10 12:33:41 -080094 }
Doug Zongkerf291d852010-07-07 13:55:25 -070095 }
Tao Bao8fce75a2016-11-10 12:33:41 -080096 }
Doug Zongkerf291d852010-07-07 13:55:25 -070097
Tao Bao5609bc82018-06-20 00:30:48 -070098 if (!check_backup) {
99 LOG(ERROR) << "Partition contents don't have the expected checksum";
100 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800101 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800102
Tao Bao09e84932018-08-31 11:25:05 -0700103 if (LoadFileContents(Paths::Get().cache_temp_source(), out) &&
Tao Bao5609bc82018-06-20 00:30:48 -0700104 memcmp(out->sha1, expected_sha1, SHA_DIGEST_LENGTH) == 0) {
105 return true;
106 }
Doug Zongker512536a2010-02-17 16:11:44 -0800107
Tao Bao5609bc82018-06-20 00:30:48 -0700108 LOG(ERROR) << "Both of partition contents and backup don't have the expected checksum";
109 return false;
Doug Zongker512536a2010-02-17 16:11:44 -0800110}
111
Tao Bao09e84932018-08-31 11:25:05 -0700112bool SaveFileContents(const std::string& filename, const FileContents* file) {
Tianjie Xu22f11202018-08-27 10:50:31 -0700113 android::base::unique_fd fd(
114 open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
Tao Bao6e02ea92016-11-17 11:24:07 -0800115 if (fd == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700116 PLOG(ERROR) << "Failed to open \"" << filename << "\" for write";
Tao Bao09e84932018-08-31 11:25:05 -0700117 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800118 }
Doug Zongker512536a2010-02-17 16:11:44 -0800119
Tianjie Xu22f11202018-08-27 10:50:31 -0700120 if (!android::base::WriteFully(fd, file->data.data(), file->data.size())) {
121 PLOG(ERROR) << "Failed to write " << file->data.size() << " bytes of data to " << filename;
Tao Bao09e84932018-08-31 11:25:05 -0700122 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800123 }
Doug Zongker512536a2010-02-17 16:11:44 -0800124
Tianjie Xu22f11202018-08-27 10:50:31 -0700125 if (fsync(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700126 PLOG(ERROR) << "Failed to fsync \"" << filename << "\"";
Tao Bao09e84932018-08-31 11:25:05 -0700127 return false;
Michael Rungebe81e512014-10-29 12:42:15 -0700128 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700129
130 if (close(fd.release()) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700131 PLOG(ERROR) << "Failed to close \"" << filename << "\"";
Tao Bao09e84932018-08-31 11:25:05 -0700132 return false;
Michael Rungebe81e512014-10-29 12:42:15 -0700133 }
Doug Zongker512536a2010-02-17 16:11:44 -0800134
Tao Bao09e84932018-08-31 11:25:05 -0700135 return true;
Doug Zongker512536a2010-02-17 16:11:44 -0800136}
137
Tao Bao5609bc82018-06-20 00:30:48 -0700138// Writes a memory buffer to 'target' Partition.
139static bool WriteBufferToPartition(const FileContents& file_contents, const Partition& partition) {
140 const unsigned char* data = file_contents.data.data();
141 size_t len = file_contents.data.size();
Tao Bao6e02ea92016-11-17 11:24:07 -0800142 size_t start = 0;
143 bool success = false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800144 for (size_t attempt = 0; attempt < 2; ++attempt) {
Tao Bao5609bc82018-06-20 00:30:48 -0700145 android::base::unique_fd fd(open(partition.name.c_str(), O_RDWR));
Tianjie Xu22f11202018-08-27 10:50:31 -0700146 if (fd == -1) {
147 PLOG(ERROR) << "Failed to open \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700148 return false;
Tianjie Xu22f11202018-08-27 10:50:31 -0700149 }
150
Tao Bao8fce75a2016-11-10 12:33:41 -0800151 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700152 PLOG(ERROR) << "Failed to seek to " << start << " on \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700153 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800154 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800155
Tianjie Xu22f11202018-08-27 10:50:31 -0700156 if (!android::base::WriteFully(fd, data + start, len - start)) {
157 PLOG(ERROR) << "Failed to write " << len - start << " bytes to \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700158 return false;
Tao Baoaca8e892015-07-17 11:47:44 -0700159 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700160
Tianjie Xu22f11202018-08-27 10:50:31 -0700161 if (fsync(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700162 PLOG(ERROR) << "Failed to sync \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700163 return false;
Doug Zongkerf291d852010-07-07 13:55:25 -0700164 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700165 if (close(fd.release()) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700166 PLOG(ERROR) << "Failed to close \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700167 return false;
Elliott Hughes63a31922016-06-09 17:41:22 -0700168 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800169
Tao Bao5609bc82018-06-20 00:30:48 -0700170 fd.reset(open(partition.name.c_str(), O_RDONLY));
Tao Bao6e02ea92016-11-17 11:24:07 -0800171 if (fd == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700172 PLOG(ERROR) << "Failed to reopen \"" << partition << "\" for verification";
Tao Bao5609bc82018-06-20 00:30:48 -0700173 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800174 }
175
176 // Drop caches so our subsequent verification read won't just be reading the cache.
Elliott Hughes63a31922016-06-09 17:41:22 -0700177 sync();
Tianjie Xu22f11202018-08-27 10:50:31 -0700178 std::string drop_cache = "/proc/sys/vm/drop_caches";
179 if (!android::base::WriteStringToFile("3\n", drop_cache)) {
180 PLOG(ERROR) << "Failed to write to " << drop_cache;
Tao Bao8fce75a2016-11-10 12:33:41 -0800181 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700182 LOG(INFO) << " caches dropped";
Tao Bao8fce75a2016-11-10 12:33:41 -0800183 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800184 sleep(1);
Elliott Hughes63a31922016-06-09 17:41:22 -0700185
Tao Bao6e02ea92016-11-17 11:24:07 -0800186 // Verify.
Tao Bao8fce75a2016-11-10 12:33:41 -0800187 if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700188 PLOG(ERROR) << "Failed to seek to 0 on " << partition;
Tao Bao5609bc82018-06-20 00:30:48 -0700189 return false;
Doug Zongkerf291d852010-07-07 13:55:25 -0700190 }
Ethan Yonker34ae4832016-08-24 15:32:18 -0500191
Tao Baoaca8e892015-07-17 11:47:44 -0700192 const char* partition = pieces[1].c_str();
Doug Zongkerf291d852010-07-07 13:55:25 -0700193
Tianjie Xu22f11202018-08-27 10:50:31 -0700194 PLOG(ERROR) << "Failed to verify-read " << partition << " at " << p;
Tao Bao5609bc82018-06-20 00:30:48 -0700195 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800196 }
Doug Zongker512536a2010-02-17 16:11:44 -0800197
Tao Bao8fce75a2016-11-10 12:33:41 -0800198 if (memcmp(buffer, data + p, to_read) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700199 LOG(ERROR) << "Verification failed starting at " << p;
Tao Bao8fce75a2016-11-10 12:33:41 -0800200 start = p;
201 break;
202 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800203 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800204
Jed Estepa7b9a462015-12-15 16:04:53 -0800205 if (start == len) {
Tao Bao859bfc52018-04-25 23:00:27 -0700206 LOG(INFO) << "Verification read succeeded (attempt " << attempt + 1 << ")";
Doug Zongker512536a2010-02-17 16:11:44 -0800207 success = true;
208 break;
Doug Zongker512536a2010-02-17 16:11:44 -0800209 }
Doug Zongker512536a2010-02-17 16:11:44 -0800210
Tianjie Xu22f11202018-08-27 10:50:31 -0700211 if (close(fd.release()) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700212 PLOG(ERROR) << "Failed to close " << partition;
Tao Bao5609bc82018-06-20 00:30:48 -0700213 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800214 }
215 }
Doug Zongker512536a2010-02-17 16:11:44 -0800216
217 if (!success) {
Tao Bao859bfc52018-04-25 23:00:27 -0700218 LOG(ERROR) << "Failed to verify after all attempts";
Tao Bao5609bc82018-06-20 00:30:48 -0700219 return false;
Doug Zongker512536a2010-02-17 16:11:44 -0800220 }
221
Doug Zongker512536a2010-02-17 16:11:44 -0800222 sync();
223
Tao Bao5609bc82018-06-20 00:30:48 -0700224 return true;
Doug Zongker512536a2010-02-17 16:11:44 -0800225}
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800226
Tao Bao8dc70492018-06-20 10:14:40 -0700227int ParseSha1(const std::string& str, uint8_t* digest) {
228 const char* ps = str.c_str();
Tao Bao155771b2018-06-05 11:26:01 -0700229 uint8_t* pd = digest;
230 for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
231 int digit;
232 if (*ps >= '0' && *ps <= '9') {
233 digit = *ps - '0';
234 } else if (*ps >= 'a' && *ps <= 'f') {
235 digit = *ps - 'a' + 10;
236 } else if (*ps >= 'A' && *ps <= 'F') {
237 digit = *ps - 'A' + 10;
238 } else {
239 return -1;
Tianjie Xuaced5d92016-10-12 10:55:04 -0700240 }
Tao Bao155771b2018-06-05 11:26:01 -0700241 if (i % 2 == 0) {
242 *pd = digit << 4;
243 } else {
244 *pd |= digit;
245 ++pd;
246 }
247 }
248 if (*ps != '\0') return -1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800249 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800250}
251
Tao Bao5609bc82018-06-20 00:30:48 -0700252bool PatchPartitionCheck(const Partition& target, const Partition& source) {
253 FileContents target_file;
254 FileContents source_file;
255 return (ReadPartitionToBuffer(target, &target_file, false) ||
256 ReadPartitionToBuffer(source, &source_file, true));
Doug Zongker512536a2010-02-17 16:11:44 -0800257}
258
259int ShowLicenses() {
Tao Bao155771b2018-06-05 11:26:01 -0700260 ShowBSDiffLicense();
Tao Bao6e02ea92016-11-17 11:24:07 -0800261 return 0;
Doug Zongker1c43c972012-02-28 11:07:09 -0800262}
263
Tao Bao5609bc82018-06-20 00:30:48 -0700264bool PatchPartition(const Partition& target, const Partition& source, const Value& patch,
Tao Bao5234ad42019-09-23 10:28:54 -0700265 const Value* bonus, bool backup_source) {
Tao Bao5609bc82018-06-20 00:30:48 -0700266 LOG(INFO) << "Patching " << target.name;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800267
Tao Bao5609bc82018-06-20 00:30:48 -0700268 // We try to load and check against the target hash first.
269 FileContents target_file;
270 if (ReadPartitionToBuffer(target, &target_file, false)) {
271 // The early-exit case: the patch was already applied, this file has the desired hash, nothing
272 // for us to do.
273 LOG(INFO) << " already " << target.hash.substr(0, 8);
274 return true;
Tao Bao6e02ea92016-11-17 11:24:07 -0800275 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800276
Doug Zongker512536a2010-02-17 16:11:44 -0800277 FileContents source_file;
Bill Peckham341644d2019-09-17 17:11:50 -0700278 if (ReadPartitionToBuffer(source, &source_file, backup_source)) {
Tao Bao5234ad42019-09-23 10:28:54 -0700279 return GenerateTarget(target, source_file, patch, bonus, backup_source);
Doug Zongker512536a2010-02-17 16:11:44 -0800280 }
281
Tao Bao5609bc82018-06-20 00:30:48 -0700282 LOG(ERROR) << "Failed to find any match";
283 return false;
Tao Baoabba55b2015-07-17 18:11:12 -0700284}
285
Tao Bao5609bc82018-06-20 00:30:48 -0700286bool FlashPartition(const Partition& partition, const std::string& source_filename) {
287 LOG(INFO) << "Flashing " << partition;
Tao Bao8fce75a2016-11-10 12:33:41 -0800288
Tao Bao5609bc82018-06-20 00:30:48 -0700289 // We try to load and check against the target hash first.
290 FileContents target_file;
291 if (ReadPartitionToBuffer(partition, &target_file, false)) {
292 // The early-exit case: the patch was already applied, this file has the desired hash, nothing
293 // for us to do.
294 LOG(INFO) << " already " << partition.hash.substr(0, 8);
295 return true;
Tao Bao8fce75a2016-11-10 12:33:41 -0800296 }
Tao Baoabba55b2015-07-17 18:11:12 -0700297
Yabin Cuid6c93af2016-02-10 16:41:10 -0800298 FileContents source_file;
Tao Bao09e84932018-08-31 11:25:05 -0700299 if (!LoadFileContents(source_filename, &source_file)) {
Tao Bao5609bc82018-06-20 00:30:48 -0700300 LOG(ERROR) << "Failed to load source file";
301 return false;
Doug Zongker512536a2010-02-17 16:11:44 -0800302 }
303
Tao Bao5609bc82018-06-20 00:30:48 -0700304 uint8_t expected_sha1[SHA_DIGEST_LENGTH];
305 if (ParseSha1(partition.hash, expected_sha1) != 0) {
306 LOG(ERROR) << "Failed to parse source hash \"" << partition.hash << "\"";
307 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800308 }
Yabin Cuid483c202016-02-03 17:08:52 -0800309
Tao Bao5609bc82018-06-20 00:30:48 -0700310 if (memcmp(source_file.sha1, expected_sha1, SHA_DIGEST_LENGTH) != 0) {
311 // The source doesn't have desired checksum.
312 LOG(ERROR) << "source \"" << source_filename << "\" doesn't have expected SHA-1 sum";
313 LOG(ERROR) << "expected: " << partition.hash.substr(0, 8)
314 << ", found: " << short_sha1(source_file.sha1);
315 return false;
Elliott Hughes63a31922016-06-09 17:41:22 -0700316 }
Tao Bao5609bc82018-06-20 00:30:48 -0700317 if (!WriteBufferToPartition(source_file, partition)) {
318 LOG(ERROR) << "Failed to write to " << partition;
319 return false;
320 }
321 return true;
Yabin Cuid483c202016-02-03 17:08:52 -0800322}
Doug Zongkerc4351c72010-02-22 14:46:32 -0800323
Tao Bao5609bc82018-06-20 00:30:48 -0700324static bool GenerateTarget(const Partition& target, const FileContents& source_file,
Tao Bao5234ad42019-09-23 10:28:54 -0700325 const Value& patch, const Value* bonus_data, bool backup_source) {
Tao Bao5609bc82018-06-20 00:30:48 -0700326 uint8_t expected_sha1[SHA_DIGEST_LENGTH];
327 if (ParseSha1(target.hash, expected_sha1) != 0) {
328 LOG(ERROR) << "Failed to parse target hash \"" << target.hash << "\"";
329 return false;
330 }
331
332 if (patch.type != Value::Type::BLOB) {
Tao Bao859bfc52018-04-25 23:00:27 -0700333 LOG(ERROR) << "patch is not a blob";
Tao Bao5609bc82018-06-20 00:30:48 -0700334 return false;
Yabin Cuid483c202016-02-03 17:08:52 -0800335 }
336
Tao Bao5609bc82018-06-20 00:30:48 -0700337 const char* header = patch.data.data();
338 size_t header_bytes_read = patch.data.size();
Tao Bao6e02ea92016-11-17 11:24:07 -0800339 bool use_bsdiff = false;
340 if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
341 use_bsdiff = true;
342 } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
343 use_bsdiff = false;
344 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700345 LOG(ERROR) << "Unknown patch file format";
Tao Bao5609bc82018-06-20 00:30:48 -0700346 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800347 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800348
Tao Bao5ee25662018-07-11 15:55:32 -0700349 // We write the original source to cache, in case the partition write is interrupted.
Tao Bao5234ad42019-09-23 10:28:54 -0700350 if (backup_source && !CheckAndFreeSpaceOnCache(source_file.data.size())) {
Tao Bao859bfc52018-04-25 23:00:27 -0700351 LOG(ERROR) << "Not enough free space on /cache";
Tao Bao5609bc82018-06-20 00:30:48 -0700352 return false;
Tao Bao40e144d2017-03-15 01:10:58 -0700353 }
Tao Bao5234ad42019-09-23 10:28:54 -0700354 if (backup_source && !SaveFileContents(Paths::Get().cache_temp_source(), &source_file)) {
Tao Bao859bfc52018-04-25 23:00:27 -0700355 LOG(ERROR) << "Failed to back up source file";
Tao Bao5609bc82018-06-20 00:30:48 -0700356 return false;
Tao Bao40e144d2017-03-15 01:10:58 -0700357 }
Doug Zongker512536a2010-02-17 16:11:44 -0800358
Tao Bao40e144d2017-03-15 01:10:58 -0700359 // We store the decoded output in memory.
Tao Bao5609bc82018-06-20 00:30:48 -0700360 FileContents patched;
Tao Bao8b0b0f12018-04-19 21:02:13 -0700361 SHA_CTX ctx;
362 SHA1_Init(&ctx);
Tao Bao5609bc82018-06-20 00:30:48 -0700363 SinkFn sink = [&patched, &ctx](const unsigned char* data, size_t len) {
Tao Bao8b0b0f12018-04-19 21:02:13 -0700364 SHA1_Update(&ctx, data, len);
Tao Bao5609bc82018-06-20 00:30:48 -0700365 patched.data.insert(patched.data.end(), data, data + len);
Tao Baoc0e1c462017-02-01 10:20:10 -0800366 return len;
367 };
Doug Zongkerc4351c72010-02-22 14:46:32 -0800368
Tao Bao40e144d2017-03-15 01:10:58 -0700369 int result;
370 if (use_bsdiff) {
Tao Bao5609bc82018-06-20 00:30:48 -0700371 result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), patch, 0, sink);
Tao Bao40e144d2017-03-15 01:10:58 -0700372 } else {
Tao Bao8b0b0f12018-04-19 21:02:13 -0700373 result =
Tao Bao5609bc82018-06-20 00:30:48 -0700374 ApplyImagePatch(source_file.data.data(), source_file.data.size(), patch, sink, bonus_data);
Tao Bao40e144d2017-03-15 01:10:58 -0700375 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800376
Tao Bao40e144d2017-03-15 01:10:58 -0700377 if (result != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700378 LOG(ERROR) << "Failed to apply the patch: " << result;
Tao Bao5609bc82018-06-20 00:30:48 -0700379 return false;
Tao Bao40e144d2017-03-15 01:10:58 -0700380 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800381
Tao Bao5609bc82018-06-20 00:30:48 -0700382 SHA1_Final(patched.sha1, &ctx);
383 if (memcmp(patched.sha1, expected_sha1, SHA_DIGEST_LENGTH) != 0) {
384 LOG(ERROR) << "Patching did not produce the expected SHA-1 of " << short_sha1(expected_sha1);
Tao Bao4f834302018-04-19 12:35:14 -0700385
Tao Bao5609bc82018-06-20 00:30:48 -0700386 LOG(ERROR) << "target size " << patched.data.size() << " SHA-1 " << short_sha1(patched.sha1);
Tao Bao859bfc52018-04-25 23:00:27 -0700387 LOG(ERROR) << "source size " << source_file.data.size() << " SHA-1 "
388 << short_sha1(source_file.sha1);
Tao Bao4f834302018-04-19 12:35:14 -0700389
390 uint8_t patch_digest[SHA_DIGEST_LENGTH];
Tao Bao5609bc82018-06-20 00:30:48 -0700391 SHA1(reinterpret_cast<const uint8_t*>(patch.data.data()), patch.data.size(), patch_digest);
392 LOG(ERROR) << "patch size " << patch.data.size() << " SHA-1 " << short_sha1(patch_digest);
Tao Bao4f834302018-04-19 12:35:14 -0700393
Tao Bao7ea515e2018-07-09 15:16:13 -0700394 if (bonus_data != nullptr) {
395 uint8_t bonus_digest[SHA_DIGEST_LENGTH];
396 SHA1(reinterpret_cast<const uint8_t*>(bonus_data->data.data()), bonus_data->data.size(),
397 bonus_digest);
398 LOG(ERROR) << "bonus size " << bonus_data->data.size() << " SHA-1 "
399 << short_sha1(bonus_digest);
400 }
Tao Bao4f834302018-04-19 12:35:14 -0700401
Tao Bao5609bc82018-06-20 00:30:48 -0700402 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800403 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800404
Tao Bao5609bc82018-06-20 00:30:48 -0700405 LOG(INFO) << " now " << short_sha1(expected_sha1);
406
Tao Bao40e144d2017-03-15 01:10:58 -0700407 // Write back the temp file to the partition.
Tao Bao5609bc82018-06-20 00:30:48 -0700408 if (!WriteBufferToPartition(patched, target)) {
409 LOG(ERROR) << "Failed to write patched data to " << target.name;
410 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800411 }
412
Tao Bao40e144d2017-03-15 01:10:58 -0700413 // Delete the backup copy of the source.
Tao Bao5234ad42019-09-23 10:28:54 -0700414 if (backup_source) {
415 unlink(Paths::Get().cache_temp_source().c_str());
416 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800417
418 // Success!
Tao Bao5609bc82018-06-20 00:30:48 -0700419 return true;
420}
421
422bool CheckPartition(const Partition& partition) {
423 FileContents target_file;
424 return ReadPartitionToBuffer(partition, &target_file, false);
425}
426
427Partition Partition::Parse(const std::string& input_str, std::string* err) {
428 std::vector<std::string> pieces = android::base::Split(input_str, ":");
429 if (pieces.size() != 4 || pieces[0] != "EMMC") {
430 *err = "Invalid number of tokens or non-eMMC target";
431 return {};
432 }
433
434 size_t size;
435 if (!android::base::ParseUint(pieces[2], &size) || size == 0) {
436 *err = "Failed to parse \"" + pieces[2] + "\" as byte count";
437 return {};
438 }
439
440 return Partition(pieces[1], size, pieces[3]);
441}
442
443std::string Partition::ToString() const {
444 if (*this) {
445 return "EMMC:"s + name + ":" + std::to_string(size) + ":" + hash;
446 }
447 return "<invalid-partition>";
448}
449
450std::ostream& operator<<(std::ostream& os, const Partition& partition) {
451 os << partition.ToString();
452 return os;
Doug Zongker512536a2010-02-17 16:11:44 -0800453}