blob: a2d38f15cfa02658b8f8e444435e34f2e509a38e [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
bigbiff26d5d5f2020-03-23 09:56:16 -040052<<<<<<< HEAD
Tao Baoaca8e892015-07-17 11:47:44 -070053static bool mtd_partitions_scanned = false;
Doug Zongker512536a2010-02-17 16:11:44 -080054
Tao Bao8fce75a2016-11-10 12:33:41 -080055// Read a file into memory; store the file contents and associated metadata in *file.
Hristo Bojinovdb314d62010-08-02 10:29:49 -070056// Return 0 on success.
Doug Zongkera1bc1482014-02-13 15:18:19 -080057int LoadFileContents(const char* filename, FileContents* file) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -050058 // A special 'filename' beginning with "MTD:" or "EMMC:" means to
59 // load the contents of a partition.
60 if (strncmp(filename, "MTD:", 4) == 0 ||
61 strncmp(filename, "EMMC:", 5) == 0 ||
62 strncmp(filename, "BML:", 4) == 0) {
Tao Bao8fce75a2016-11-10 12:33:41 -080063 return LoadPartitionContents(filename, file);
64 }
bigbiff26d5d5f2020-03-23 09:56:16 -040065=======
Tao Bao5609bc82018-06-20 00:30:48 -070066static bool GenerateTarget(const Partition& target, const FileContents& source_file,
67 const Value& patch, const Value* bonus_data);
bigbiff26d5d5f2020-03-23 09:56:16 -040068>>>>>>> android-10.0.0_r25
Doug Zongker512536a2010-02-17 16:11:44 -080069
Tao Bao09e84932018-08-31 11:25:05 -070070bool LoadFileContents(const std::string& filename, FileContents* file) {
Tao Bao5609bc82018-06-20 00:30:48 -070071 // No longer allow loading contents from eMMC partitions.
Tao Bao8dc70492018-06-20 10:14:40 -070072 if (android::base::StartsWith(filename, "EMMC:")) {
Tao Bao09e84932018-08-31 11:25:05 -070073 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -080074 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080075
Tianjie Xu22f11202018-08-27 10:50:31 -070076 std::string data;
77 if (!android::base::ReadFileToString(filename, &data)) {
78 PLOG(ERROR) << "Failed to read \"" << filename << "\"";
Tao Bao09e84932018-08-31 11:25:05 -070079 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -080080 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080081
Tianjie Xu22f11202018-08-27 10:50:31 -070082 file->data = std::vector<unsigned char>(data.begin(), data.end());
Tao Bao8fce75a2016-11-10 12:33:41 -080083 SHA1(file->data.data(), file->data.size(), file->sha1);
Tao Bao09e84932018-08-31 11:25:05 -070084 return true;
Doug Zongker512536a2010-02-17 16:11:44 -080085}
86
bigbiff26d5d5f2020-03-23 09:56:16 -040087<<<<<<< HEAD
Elliott Hughes63a31922016-06-09 17:41:22 -070088// Load the contents of an EMMC partition into the provided
Doug Zongker512536a2010-02-17 16:11:44 -080089// FileContents. filename should be a string of the form
Elliott Hughes63a31922016-06-09 17:41:22 -070090// "EMMC:<partition_device>:...". The smallest size_n bytes for
Doug Zongkerf291d852010-07-07 13:55:25 -070091// which that prefix of the partition contents has the corresponding
92// sha1 hash will be loaded. It is acceptable for a size value to be
93// repeated with different sha1s. Will return 0 on success.
Doug Zongker512536a2010-02-17 16:11:44 -080094//
95// This complexity is needed because if an OTA installation is
96// interrupted, the partition might contain either the source or the
97// target data, which might be of different lengths. We need to know
Doug Zongkerf291d852010-07-07 13:55:25 -070098// the length in order to read from a partition (there is no
99// "end-of-file" marker), so the caller must specify the possible
100// lengths and the hash of the data, and we'll do the load expecting
101// to find one of those hashes.
102enum PartitionType { MTD, EMMC };
103
Tao Bao8fce75a2016-11-10 12:33:41 -0800104static int LoadPartitionContents(const std::string& filename, FileContents* file) {
105 std::vector<std::string> pieces = android::base::Split(filename, ":");
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500106 if (pieces.size() < 4 || pieces.size() % 2 != 0) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800107 printf("LoadPartitionContents called with bad filename \"%s\"\n", filename.c_str());
108 return -1;
109 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700110
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500111 enum PartitionType type;
112 if (pieces[0] == "MTD") {
113 type = MTD;
114 } else if (pieces[0] == "EMMC") {
115 type = EMMC;
116 } else if (pieces[0] == "BML") {
117 type = EMMC;
118 } else {
119 printf("LoadPartitionContents called with bad filename (%s)\n", filename.c_str());
120 return -1;
121 }
122
Tao Bao8fce75a2016-11-10 12:33:41 -0800123 size_t pair_count = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename
124 std::vector<std::pair<size_t, std::string>> pairs;
125 for (size_t i = 0; i < pair_count; ++i) {
126 size_t size;
127 if (!android::base::ParseUint(pieces[i * 2 + 2], &size) || size == 0) {
128 printf("LoadPartitionContents called with bad size \"%s\"\n", pieces[i * 2 + 2].c_str());
129 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800130 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800131 pairs.push_back({ size, pieces[i * 2 + 3] });
132 }
133
134 // Sort the pairs array so that they are in order of increasing size.
135 std::sort(pairs.begin(), pairs.end());
136
137 const char* partition = pieces[1].c_str();
Tao Bao358c2ec2016-11-28 11:48:43 -0800138 unique_file dev(ota_fopen(partition, "rb"));
Tao Bao8fce75a2016-11-10 12:33:41 -0800139 if (!dev) {
140 printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno));
141 return -1;
bigbiff26d5d5f2020-03-23 09:56:16 -0400142=======
Tao Bao5609bc82018-06-20 00:30:48 -0700143// Reads the contents of a Partition to the given FileContents buffer.
144static bool ReadPartitionToBuffer(const Partition& partition, FileContents* out,
145 bool check_backup) {
146 uint8_t expected_sha1[SHA_DIGEST_LENGTH];
147 if (ParseSha1(partition.hash, expected_sha1) != 0) {
148 LOG(ERROR) << "Failed to parse target hash \"" << partition.hash << "\"";
149 return false;
bigbiff26d5d5f2020-03-23 09:56:16 -0400150>>>>>>> android-10.0.0_r25
Tao Bao8fce75a2016-11-10 12:33:41 -0800151 }
152
Tao Bao5609bc82018-06-20 00:30:48 -0700153 android::base::unique_fd dev(open(partition.name.c_str(), O_RDONLY));
Bernie Innocenti8bd6f452019-03-28 15:48:08 +0900154 if (dev == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700155 PLOG(ERROR) << "Failed to open eMMC partition \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700156 } else {
157 std::vector<unsigned char> buffer(partition.size);
158 if (!android::base::ReadFully(dev, buffer.data(), buffer.size())) {
159 PLOG(ERROR) << "Failed to read " << buffer.size() << " bytes of data for partition "
160 << partition;
161 } else {
162 SHA1(buffer.data(), buffer.size(), out->sha1);
163 if (memcmp(out->sha1, expected_sha1, SHA_DIGEST_LENGTH) == 0) {
164 out->data = std::move(buffer);
165 return true;
Tao Bao8fce75a2016-11-10 12:33:41 -0800166 }
bigbiff26d5d5f2020-03-23 09:56:16 -0400167<<<<<<< HEAD
Tao Bao8fce75a2016-11-10 12:33:41 -0800168 SHA1_Update(&sha_ctx, buffer_ptr, read);
169 buffer_size += read;
170 buffer_ptr += read;
Tao Baoaca8e892015-07-17 11:47:44 -0700171 }
Doug Zongker512536a2010-02-17 16:11:44 -0800172
Ethan Yonkerb8e985c2016-08-31 13:42:11 -0500173 if (pieces[0] == "BML") {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500174 if (strcmp(partition, "boot") == 0) {
175 partition = BOARD_BML_BOOT;
176 } else if (strcmp(partition, "recovery") == 0) {
177 partition = BOARD_BML_RECOVERY;
178 }
Conn O'Griofad9201042014-03-25 01:26:49 +0000179 }
180
Tao Bao8fce75a2016-11-10 12:33:41 -0800181 // Duplicate the SHA context and finalize the duplicate so we can
182 // check it against this pair's expected hash.
183 SHA_CTX temp_ctx;
184 memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
185 uint8_t sha_so_far[SHA_DIGEST_LENGTH];
186 SHA1_Final(sha_so_far, &temp_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800187
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800188 uint8_t parsed_sha[SHA_DIGEST_LENGTH];
Tao Bao8fce75a2016-11-10 12:33:41 -0800189 if (ParseSha1(current_sha1.c_str(), parsed_sha) != 0) {
190 printf("failed to parse SHA-1 %s in %s\n", current_sha1.c_str(), filename.c_str());
191 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800192 }
193
Tao Bao8fce75a2016-11-10 12:33:41 -0800194 if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) {
195 // We have a match. Stop reading the partition; we'll return the data we've read so far.
196 printf("partition read matched size %zu SHA-1 %s\n", current_size, current_sha1.c_str());
197 found = true;
198 break;
bigbiff26d5d5f2020-03-23 09:56:16 -0400199=======
200>>>>>>> android-10.0.0_r25
Doug Zongkerf291d852010-07-07 13:55:25 -0700201 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800202 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700203
Tao Bao5609bc82018-06-20 00:30:48 -0700204 if (!check_backup) {
205 LOG(ERROR) << "Partition contents don't have the expected checksum";
206 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800207 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800208
Tao Bao09e84932018-08-31 11:25:05 -0700209 if (LoadFileContents(Paths::Get().cache_temp_source(), out) &&
Tao Bao5609bc82018-06-20 00:30:48 -0700210 memcmp(out->sha1, expected_sha1, SHA_DIGEST_LENGTH) == 0) {
211 return true;
212 }
Doug Zongker512536a2010-02-17 16:11:44 -0800213
Tao Bao5609bc82018-06-20 00:30:48 -0700214 LOG(ERROR) << "Both of partition contents and backup don't have the expected checksum";
215 return false;
Doug Zongker512536a2010-02-17 16:11:44 -0800216}
217
Tao Bao09e84932018-08-31 11:25:05 -0700218bool SaveFileContents(const std::string& filename, const FileContents* file) {
Tianjie Xu22f11202018-08-27 10:50:31 -0700219 android::base::unique_fd fd(
220 open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
Tao Bao6e02ea92016-11-17 11:24:07 -0800221 if (fd == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700222 PLOG(ERROR) << "Failed to open \"" << filename << "\" for write";
Tao Bao09e84932018-08-31 11:25:05 -0700223 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800224 }
Doug Zongker512536a2010-02-17 16:11:44 -0800225
Tianjie Xu22f11202018-08-27 10:50:31 -0700226 if (!android::base::WriteFully(fd, file->data.data(), file->data.size())) {
227 PLOG(ERROR) << "Failed to write " << file->data.size() << " bytes of data to " << filename;
Tao Bao09e84932018-08-31 11:25:05 -0700228 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800229 }
Doug Zongker512536a2010-02-17 16:11:44 -0800230
bigbiff26d5d5f2020-03-23 09:56:16 -0400231<<<<<<< HEAD
Tao Bao6e02ea92016-11-17 11:24:07 -0800232 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800233}
234
Doug Zongkerf291d852010-07-07 13:55:25 -0700235// Write a memory buffer to 'target' partition, a string of the form
Elliott Hughes63a31922016-06-09 17:41:22 -0700236// "EMMC:<partition_device>[:...]". The target name
Tao Bao1ce7a2a2015-07-24 15:29:12 -0700237// might contain multiple colons, but WriteToPartition() only uses the first
238// two and ignores the rest. Return 0 on success.
Tao Bao6e02ea92016-11-17 11:24:07 -0800239int WriteToPartition(const unsigned char* data, size_t len, const std::string& target) {
Tao Baoaca8e892015-07-17 11:47:44 -0700240 std::string copy(target);
241 std::vector<std::string> pieces = android::base::Split(copy, ":");
242
Tao Bao1ce7a2a2015-07-24 15:29:12 -0700243 if (pieces.size() < 2) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500244 printf("WriteToPartition called with bad target (%s)\n", target.c_str());
Tao Baoaca8e892015-07-17 11:47:44 -0700245 return -1;
246 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700247
248 enum PartitionType type;
Tao Baoaca8e892015-07-17 11:47:44 -0700249 if (pieces[0] == "MTD") {
Doug Zongkerf291d852010-07-07 13:55:25 -0700250 type = MTD;
Tao Baoaca8e892015-07-17 11:47:44 -0700251 } else if (pieces[0] == "EMMC") {
Doug Zongkerf291d852010-07-07 13:55:25 -0700252 type = EMMC;
Ethan Yonkerb8e985c2016-08-31 13:42:11 -0500253 } else if (pieces[0] == "BML") {
Conn O'Griofad9201042014-03-25 01:26:49 +0000254 type = EMMC;
Doug Zongkerf291d852010-07-07 13:55:25 -0700255 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500256 printf("WriteToPartition called with bad target (%s)\n", target.c_str());
Doug Zongkerf291d852010-07-07 13:55:25 -0700257 return -1;
bigbiff26d5d5f2020-03-23 09:56:16 -0400258=======
Tianjie Xu22f11202018-08-27 10:50:31 -0700259 if (fsync(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700260 PLOG(ERROR) << "Failed to fsync \"" << filename << "\"";
Tao Bao09e84932018-08-31 11:25:05 -0700261 return false;
Doug Zongker512536a2010-02-17 16:11:44 -0800262 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700263
264 if (close(fd.release()) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700265 PLOG(ERROR) << "Failed to close \"" << filename << "\"";
Tao Bao09e84932018-08-31 11:25:05 -0700266 return false;
Michael Rungebe81e512014-10-29 12:42:15 -0700267 }
268
Tao Bao09e84932018-08-31 11:25:05 -0700269 return true;
Doug Zongker512536a2010-02-17 16:11:44 -0800270}
271
Tao Bao5609bc82018-06-20 00:30:48 -0700272// Writes a memory buffer to 'target' Partition.
273static bool WriteBufferToPartition(const FileContents& file_contents, const Partition& partition) {
274 const unsigned char* data = file_contents.data.data();
275 size_t len = file_contents.data.size();
Tao Bao6e02ea92016-11-17 11:24:07 -0800276 size_t start = 0;
277 bool success = false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800278 for (size_t attempt = 0; attempt < 2; ++attempt) {
Tao Bao5609bc82018-06-20 00:30:48 -0700279 android::base::unique_fd fd(open(partition.name.c_str(), O_RDWR));
Tianjie Xu22f11202018-08-27 10:50:31 -0700280 if (fd == -1) {
281 PLOG(ERROR) << "Failed to open \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700282 return false;
Tianjie Xu22f11202018-08-27 10:50:31 -0700283 }
284
Tao Bao8fce75a2016-11-10 12:33:41 -0800285 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700286 PLOG(ERROR) << "Failed to seek to " << start << " on \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700287 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800288 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800289
Tianjie Xu22f11202018-08-27 10:50:31 -0700290 if (!android::base::WriteFully(fd, data + start, len - start)) {
291 PLOG(ERROR) << "Failed to write " << len - start << " bytes to \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700292 return false;
Doug Zongkerf291d852010-07-07 13:55:25 -0700293 }
294
Tianjie Xu22f11202018-08-27 10:50:31 -0700295 if (fsync(fd) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700296 PLOG(ERROR) << "Failed to sync \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700297 return false;
Doug Zongkerf291d852010-07-07 13:55:25 -0700298 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700299 if (close(fd.release()) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700300 PLOG(ERROR) << "Failed to close \"" << partition << "\"";
Tao Bao5609bc82018-06-20 00:30:48 -0700301 return false;
Elliott Hughes63a31922016-06-09 17:41:22 -0700302 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800303
Tao Bao5609bc82018-06-20 00:30:48 -0700304 fd.reset(open(partition.name.c_str(), O_RDONLY));
Tao Bao6e02ea92016-11-17 11:24:07 -0800305 if (fd == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700306 PLOG(ERROR) << "Failed to reopen \"" << partition << "\" for verification";
Tao Bao5609bc82018-06-20 00:30:48 -0700307 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800308 }
309
310 // Drop caches so our subsequent verification read won't just be reading the cache.
Elliott Hughes63a31922016-06-09 17:41:22 -0700311 sync();
Tianjie Xu22f11202018-08-27 10:50:31 -0700312 std::string drop_cache = "/proc/sys/vm/drop_caches";
313 if (!android::base::WriteStringToFile("3\n", drop_cache)) {
314 PLOG(ERROR) << "Failed to write to " << drop_cache;
Tao Bao8fce75a2016-11-10 12:33:41 -0800315 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700316 LOG(INFO) << " caches dropped";
Tao Bao8fce75a2016-11-10 12:33:41 -0800317 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800318 sleep(1);
Elliott Hughes63a31922016-06-09 17:41:22 -0700319
Tao Bao6e02ea92016-11-17 11:24:07 -0800320 // Verify.
Tao Bao8fce75a2016-11-10 12:33:41 -0800321 if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
Tao Bao859bfc52018-04-25 23:00:27 -0700322 PLOG(ERROR) << "Failed to seek to 0 on " << partition;
Tao Bao5609bc82018-06-20 00:30:48 -0700323 return false;
bigbiff26d5d5f2020-03-23 09:56:16 -0400324>>>>>>> android-10.0.0_r25
Doug Zongkerf291d852010-07-07 13:55:25 -0700325 }
Ethan Yonker34ae4832016-08-24 15:32:18 -0500326
Tao Baoaca8e892015-07-17 11:47:44 -0700327 const char* partition = pieces[1].c_str();
Doug Zongkerf291d852010-07-07 13:55:25 -0700328
bigbiff26d5d5f2020-03-23 09:56:16 -0400329<<<<<<< HEAD
Ethan Yonkerb8e985c2016-08-31 13:42:11 -0500330 if (pieces[0] == "BML") {
Conn O'Griofad9201042014-03-25 01:26:49 +0000331 if (strcmp(partition, "boot") == 0) {
332 partition = BOARD_BML_BOOT;
333 } else if (strcmp(partition, "recovery") == 0) {
334 partition = BOARD_BML_RECOVERY;
335 }
336
337 int bmlpartition = open(partition, O_RDWR | O_LARGEFILE);
338 if (bmlpartition < 0)
339 return -1;
340 if (ioctl(bmlpartition, BML_UNLOCK_ALL, 0)) {
341 printf("failed to unlock BML partition: (%s)\n", partition);
342 return -1;
343 }
344 close(bmlpartition);
345 }
346
Doug Zongkerc4351c72010-02-22 14:46:32 -0800347 if (partition == NULL) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500348 printf("bad partition target name \"%s\"\n", target.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800349 return -1;
350 }
Doug Zongker512536a2010-02-17 16:11:44 -0800351
Doug Zongkerf291d852010-07-07 13:55:25 -0700352 switch (type) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700353 case MTD: {
Doug Zongkerf291d852010-07-07 13:55:25 -0700354 if (!mtd_partitions_scanned) {
355 mtd_scan_partitions();
Tao Baoaca8e892015-07-17 11:47:44 -0700356 mtd_partitions_scanned = true;
Doug Zongkerf291d852010-07-07 13:55:25 -0700357 }
358
359 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
360 if (mtd == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700361 printf("mtd partition \"%s\" not found for writing\n", partition);
Doug Zongkerf291d852010-07-07 13:55:25 -0700362 return -1;
363 }
364
365 MtdWriteContext* ctx = mtd_write_partition(mtd);
366 if (ctx == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700367 printf("failed to init mtd partition \"%s\" for writing\n", partition);
Doug Zongkerf291d852010-07-07 13:55:25 -0700368 return -1;
369 }
370
Yabin Cuid483c202016-02-03 17:08:52 -0800371 size_t written = mtd_write_data(ctx, reinterpret_cast<const char*>(data), len);
Doug Zongkerf291d852010-07-07 13:55:25 -0700372 if (written != len) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700373 printf("only wrote %zu of %zu bytes to MTD %s\n", written, len, partition);
Doug Zongkerf291d852010-07-07 13:55:25 -0700374 mtd_write_close(ctx);
375 return -1;
376 }
377
378 if (mtd_erase_blocks(ctx, -1) < 0) {
379 printf("error finishing mtd write of %s\n", partition);
380 mtd_write_close(ctx);
381 return -1;
382 }
383
384 if (mtd_write_close(ctx)) {
385 printf("error closing mtd write of %s\n", partition);
386 return -1;
387 }
388 break;
Tao Baoba9a42a2015-06-23 23:23:33 -0700389 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700390
Tao Baoba9a42a2015-06-23 23:23:33 -0700391 case EMMC: {
Doug Zongker044a0b42013-07-08 09:42:54 -0700392 size_t start = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700393 bool success = false;
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500394 unique_fd fd(ota_open(partition, O_RDWR | O_SYNC));
Doug Zongkerc870a992013-07-09 10:34:46 -0700395 if (fd < 0) {
Doug Zongker044a0b42013-07-08 09:42:54 -0700396 printf("failed to open %s: %s\n", partition, strerror(errno));
Doug Zongkerf291d852010-07-07 13:55:25 -0700397 return -1;
398 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700399
Tao Baoaca8e892015-07-17 11:47:44 -0700400 for (size_t attempt = 0; attempt < 2; ++attempt) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700401 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700402 printf("failed seek on %s: %s\n", partition, strerror(errno));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700403 return -1;
404 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700405 while (start < len) {
406 size_t to_write = len - start;
Doug Zongker168724c2013-12-19 15:16:57 -0800407 if (to_write > 1<<20) to_write = 1<<20;
Doug Zongker044a0b42013-07-08 09:42:54 -0700408
Jed Estepf1fc48c2015-12-15 16:04:53 -0800409 ssize_t written = TEMP_FAILURE_RETRY(ota_write(fd, data+start, to_write));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700410 if (written == -1) {
411 printf("failed write writing to %s: %s\n", partition, strerror(errno));
412 return -1;
Doug Zongker044a0b42013-07-08 09:42:54 -0700413 }
Doug Zongkerc870a992013-07-09 10:34:46 -0700414 start += written;
Doug Zongker044a0b42013-07-08 09:42:54 -0700415 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800416 if (ota_fsync(fd) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700417 printf("failed to sync to %s (%s)\n", partition, strerror(errno));
Michael Rungecddb68b2014-10-29 12:42:15 -0700418 return -1;
419 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800420 if (ota_close(fd) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700421 printf("failed to close %s (%s)\n", partition, strerror(errno));
Michael Rungecddb68b2014-10-29 12:42:15 -0700422 return -1;
423 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500424 unique_fd fd(ota_open(partition, O_RDONLY));
Michael Rungecddb68b2014-10-29 12:42:15 -0700425 if (fd < 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700426 printf("failed to reopen %s for verify (%s)\n", partition, strerror(errno));
Michael Rungecddb68b2014-10-29 12:42:15 -0700427 return -1;
428 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700429
Tao Baoba9a42a2015-06-23 23:23:33 -0700430 // Drop caches so our subsequent verification read
Doug Zongker044a0b42013-07-08 09:42:54 -0700431 // won't just be reading the cache.
432 sync();
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500433 unique_fd dc(ota_open("/proc/sys/vm/drop_caches", O_WRONLY));
Jed Estepf1fc48c2015-12-15 16:04:53 -0800434 if (TEMP_FAILURE_RETRY(ota_write(dc, "3\n", 2)) == -1) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700435 printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno));
436 } else {
437 printf(" caches dropped\n");
438 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800439 ota_close(dc);
Doug Zongker044a0b42013-07-08 09:42:54 -0700440 sleep(1);
Doug Zongker044a0b42013-07-08 09:42:54 -0700441
442 // verify
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700443 if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
444 printf("failed to seek back to beginning of %s: %s\n",
445 partition, strerror(errno));
446 return -1;
447 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700448 unsigned char buffer[4096];
449 start = len;
Tao Baoba9a42a2015-06-23 23:23:33 -0700450 for (size_t p = 0; p < len; p += sizeof(buffer)) {
Doug Zongker044a0b42013-07-08 09:42:54 -0700451 size_t to_read = len - p;
Tao Baoba9a42a2015-06-23 23:23:33 -0700452 if (to_read > sizeof(buffer)) {
453 to_read = sizeof(buffer);
454 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700455
Doug Zongkerc870a992013-07-09 10:34:46 -0700456 size_t so_far = 0;
457 while (so_far < to_read) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700458 ssize_t read_count =
Jed Estepf1fc48c2015-12-15 16:04:53 -0800459 TEMP_FAILURE_RETRY(ota_read(fd, buffer+so_far, to_read-so_far));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700460 if (read_count == -1) {
461 printf("verify read error %s at %zu: %s\n",
462 partition, p, strerror(errno));
463 return -1;
Doug Zongkerc870a992013-07-09 10:34:46 -0700464 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700465 if (static_cast<size_t>(read_count) < to_read) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700466 printf("short verify read %s at %zu: %zd %zu %s\n",
Doug Zongkerc870a992013-07-09 10:34:46 -0700467 partition, p, read_count, to_read, strerror(errno));
468 }
469 so_far += read_count;
Doug Zongker044a0b42013-07-08 09:42:54 -0700470 }
471
Tao Baoba9a42a2015-06-23 23:23:33 -0700472 if (memcmp(buffer, data+p, to_read) != 0) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700473 printf("verification failed starting at %zu\n", p);
Doug Zongker044a0b42013-07-08 09:42:54 -0700474 start = p;
475 break;
476 }
477 }
478
479 if (start == len) {
Tao Baoabba55b2015-07-17 18:11:12 -0700480 printf("verification read succeeded (attempt %zu)\n", attempt+1);
Doug Zongker044a0b42013-07-08 09:42:54 -0700481 success = true;
482 break;
483 }
484 }
485
486 if (!success) {
487 printf("failed to verify after all attempts\n");
488 return -1;
489 }
490
Jed Estepf1fc48c2015-12-15 16:04:53 -0800491 if (ota_close(fd) != 0) {
Doug Zongkerf291d852010-07-07 13:55:25 -0700492 printf("error closing %s (%s)\n", partition, strerror(errno));
493 return -1;
494 }
Doug Zongkerbf4a69a2013-07-10 13:39:50 -0700495 sync();
Doug Zongkerf291d852010-07-07 13:55:25 -0700496 break;
Doug Zongker044a0b42013-07-08 09:42:54 -0700497 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800498 }
Doug Zongker512536a2010-02-17 16:11:44 -0800499
Doug Zongkerc4351c72010-02-22 14:46:32 -0800500 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800501}
502
Doug Zongker512536a2010-02-17 16:11:44 -0800503// Take a string 'str' of 40 hex digits and parse it into the 20
504// byte array 'digest'. 'str' may contain only the digest or be of
505// the form "<digest>:<anything>". Return 0 on success, -1 on any
506// error.
507int ParseSha1(const char* str, uint8_t* digest) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800508 const char* ps = str;
509 uint8_t* pd = digest;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800510 for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800511 int digit;
512 if (*ps >= '0' && *ps <= '9') {
513 digit = *ps - '0';
514 } else if (*ps >= 'a' && *ps <= 'f') {
515 digit = *ps - 'a' + 10;
516 } else if (*ps >= 'A' && *ps <= 'F') {
517 digit = *ps - 'A' + 10;
518 } else {
519 return -1;
520 }
521 if (i % 2 == 0) {
522 *pd = digit << 4;
523 } else {
524 *pd |= digit;
525 ++pd;
526 }
Doug Zongker512536a2010-02-17 16:11:44 -0800527 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800528 if (*ps != '\0') return -1;
529 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800530}
531
Doug Zongkerc4351c72010-02-22 14:46:32 -0800532// Search an array of sha1 strings for one matching the given sha1.
533// Return the index of the match on success, or -1 if no match is
534// found.
Tao Baoc8e79342016-12-28 10:11:22 -0800535static int FindMatchingPatch(uint8_t* sha1, const std::vector<std::string>& patch_sha1_str) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800536 for (size_t i = 0; i < patch_sha1_str.size(); ++i) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800537 uint8_t patch_sha1[SHA_DIGEST_LENGTH];
Tao Bao8fce75a2016-11-10 12:33:41 -0800538 if (ParseSha1(patch_sha1_str[i].c_str(), patch_sha1) == 0 &&
539 memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
540 return i;
Doug Zongker512536a2010-02-17 16:11:44 -0800541 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800542 }
543 return -1;
bigbiff26d5d5f2020-03-23 09:56:16 -0400544=======
Tianjie Xu22f11202018-08-27 10:50:31 -0700545 if (!android::base::ReadFully(fd, buffer, to_read)) {
546 PLOG(ERROR) << "Failed to verify-read " << partition << " at " << p;
Tao Bao5609bc82018-06-20 00:30:48 -0700547 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800548 }
Doug Zongker512536a2010-02-17 16:11:44 -0800549
Hristo Bojinovdb314d62010-08-02 10:29:49 -0700550 if (memcmp(buffer, data + p, to_read) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700551 LOG(ERROR) << "Verification failed starting at " << p;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800552 start = p;
553 break;
554 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800555 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800556
Jed Estepa7b9a462015-12-15 16:04:53 -0800557 if (start == len) {
Tao Bao859bfc52018-04-25 23:00:27 -0700558 LOG(INFO) << "Verification read succeeded (attempt " << attempt + 1 << ")";
Doug Zongker512536a2010-02-17 16:11:44 -0800559 success = true;
560 break;
Doug Zongker512536a2010-02-17 16:11:44 -0800561 }
Doug Zongker512536a2010-02-17 16:11:44 -0800562
Tianjie Xu22f11202018-08-27 10:50:31 -0700563 if (close(fd.release()) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700564 PLOG(ERROR) << "Failed to close " << partition;
Tao Bao5609bc82018-06-20 00:30:48 -0700565 return false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800566 }
567 }
Doug Zongker512536a2010-02-17 16:11:44 -0800568
569 if (!success) {
Tao Bao859bfc52018-04-25 23:00:27 -0700570 LOG(ERROR) << "Failed to verify after all attempts";
Tao Bao5609bc82018-06-20 00:30:48 -0700571 return false;
Doug Zongker512536a2010-02-17 16:11:44 -0800572 }
573
Doug Zongker512536a2010-02-17 16:11:44 -0800574 sync();
575
Tao Bao5609bc82018-06-20 00:30:48 -0700576 return true;
bigbiff26d5d5f2020-03-23 09:56:16 -0400577>>>>>>> android-10.0.0_r25
Doug Zongker512536a2010-02-17 16:11:44 -0800578}
Tao Baoba9a42a2015-06-23 23:23:33 -0700579
Tao Bao8dc70492018-06-20 10:14:40 -0700580int ParseSha1(const std::string& str, uint8_t* digest) {
581 const char* ps = str.c_str();
Tao Bao155771b2018-06-05 11:26:01 -0700582 uint8_t* pd = digest;
583 for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
584 int digit;
585 if (*ps >= '0' && *ps <= '9') {
586 digit = *ps - '0';
587 } else if (*ps >= 'a' && *ps <= 'f') {
588 digit = *ps - 'a' + 10;
589 } else if (*ps >= 'A' && *ps <= 'F') {
590 digit = *ps - 'A' + 10;
591 } else {
592 return -1;
Tianjie Xuaced5d92016-10-12 10:55:04 -0700593 }
Tao Bao155771b2018-06-05 11:26:01 -0700594 if (i % 2 == 0) {
595 *pd = digit << 4;
596 } else {
597 *pd |= digit;
598 ++pd;
599 }
600 }
601 if (*ps != '\0') return -1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800602 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800603}
604
Tao Bao5609bc82018-06-20 00:30:48 -0700605bool PatchPartitionCheck(const Partition& target, const Partition& source) {
606 FileContents target_file;
607 FileContents source_file;
608 return (ReadPartitionToBuffer(target, &target_file, false) ||
609 ReadPartitionToBuffer(source, &source_file, true));
Doug Zongker512536a2010-02-17 16:11:44 -0800610}
611
612int ShowLicenses() {
Tao Bao155771b2018-06-05 11:26:01 -0700613 ShowBSDiffLicense();
Tao Bao6e02ea92016-11-17 11:24:07 -0800614 return 0;
Doug Zongker1c43c972012-02-28 11:07:09 -0800615}
616
Tao Bao5609bc82018-06-20 00:30:48 -0700617bool PatchPartition(const Partition& target, const Partition& source, const Value& patch,
618 const Value* bonus) {
619 LOG(INFO) << "Patching " << target.name;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800620
Tao Bao5609bc82018-06-20 00:30:48 -0700621 // We try to load and check against the target hash first.
622 FileContents target_file;
623 if (ReadPartitionToBuffer(target, &target_file, false)) {
624 // The early-exit case: the patch was already applied, this file has the desired hash, nothing
625 // for us to do.
626 LOG(INFO) << " already " << target.hash.substr(0, 8);
627 return true;
Tao Bao6e02ea92016-11-17 11:24:07 -0800628 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800629
Doug Zongker512536a2010-02-17 16:11:44 -0800630 FileContents source_file;
Tao Bao5609bc82018-06-20 00:30:48 -0700631 if (ReadPartitionToBuffer(source, &source_file, true)) {
632 return GenerateTarget(target, source_file, patch, bonus);
Doug Zongker512536a2010-02-17 16:11:44 -0800633 }
634
Tao Bao5609bc82018-06-20 00:30:48 -0700635 LOG(ERROR) << "Failed to find any match";
636 return false;
Tao Baoabba55b2015-07-17 18:11:12 -0700637}
638
Tao Bao5609bc82018-06-20 00:30:48 -0700639bool FlashPartition(const Partition& partition, const std::string& source_filename) {
640 LOG(INFO) << "Flashing " << partition;
Tao Bao8fce75a2016-11-10 12:33:41 -0800641
Tao Bao5609bc82018-06-20 00:30:48 -0700642 // We try to load and check against the target hash first.
643 FileContents target_file;
644 if (ReadPartitionToBuffer(partition, &target_file, false)) {
645 // The early-exit case: the patch was already applied, this file has the desired hash, nothing
646 // for us to do.
647 LOG(INFO) << " already " << partition.hash.substr(0, 8);
648 return true;
Tao Bao8fce75a2016-11-10 12:33:41 -0800649 }
Tao Baoabba55b2015-07-17 18:11:12 -0700650
Yabin Cuid6c93af2016-02-10 16:41:10 -0800651 FileContents source_file;
Tao Bao09e84932018-08-31 11:25:05 -0700652 if (!LoadFileContents(source_filename, &source_file)) {
Tao Bao5609bc82018-06-20 00:30:48 -0700653 LOG(ERROR) << "Failed to load source file";
654 return false;
Doug Zongker512536a2010-02-17 16:11:44 -0800655 }
656
Tao Bao5609bc82018-06-20 00:30:48 -0700657 uint8_t expected_sha1[SHA_DIGEST_LENGTH];
658 if (ParseSha1(partition.hash, expected_sha1) != 0) {
659 LOG(ERROR) << "Failed to parse source hash \"" << partition.hash << "\"";
660 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800661 }
Yabin Cuid483c202016-02-03 17:08:52 -0800662
Tao Bao5609bc82018-06-20 00:30:48 -0700663 if (memcmp(source_file.sha1, expected_sha1, SHA_DIGEST_LENGTH) != 0) {
664 // The source doesn't have desired checksum.
665 LOG(ERROR) << "source \"" << source_filename << "\" doesn't have expected SHA-1 sum";
666 LOG(ERROR) << "expected: " << partition.hash.substr(0, 8)
667 << ", found: " << short_sha1(source_file.sha1);
668 return false;
Elliott Hughes63a31922016-06-09 17:41:22 -0700669 }
Tao Bao5609bc82018-06-20 00:30:48 -0700670 if (!WriteBufferToPartition(source_file, partition)) {
671 LOG(ERROR) << "Failed to write to " << partition;
672 return false;
673 }
674 return true;
Yabin Cuid483c202016-02-03 17:08:52 -0800675}
Doug Zongkerc4351c72010-02-22 14:46:32 -0800676
Tao Bao5609bc82018-06-20 00:30:48 -0700677static bool GenerateTarget(const Partition& target, const FileContents& source_file,
678 const Value& patch, const Value* bonus_data) {
679 uint8_t expected_sha1[SHA_DIGEST_LENGTH];
680 if (ParseSha1(target.hash, expected_sha1) != 0) {
681 LOG(ERROR) << "Failed to parse target hash \"" << target.hash << "\"";
682 return false;
683 }
684
685 if (patch.type != Value::Type::BLOB) {
Tao Bao859bfc52018-04-25 23:00:27 -0700686 LOG(ERROR) << "patch is not a blob";
Tao Bao5609bc82018-06-20 00:30:48 -0700687 return false;
Yabin Cuid483c202016-02-03 17:08:52 -0800688 }
689
Tao Bao5609bc82018-06-20 00:30:48 -0700690 const char* header = patch.data.data();
691 size_t header_bytes_read = patch.data.size();
Tao Bao6e02ea92016-11-17 11:24:07 -0800692 bool use_bsdiff = false;
693 if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
694 use_bsdiff = true;
695 } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
696 use_bsdiff = false;
697 } else {
Tao Bao859bfc52018-04-25 23:00:27 -0700698 LOG(ERROR) << "Unknown patch file format";
Tao Bao5609bc82018-06-20 00:30:48 -0700699 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800700 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800701
Tao Bao5ee25662018-07-11 15:55:32 -0700702 // We write the original source to cache, in case the partition write is interrupted.
703 if (!CheckAndFreeSpaceOnCache(source_file.data.size())) {
Tao Bao859bfc52018-04-25 23:00:27 -0700704 LOG(ERROR) << "Not enough free space on /cache";
Tao Bao5609bc82018-06-20 00:30:48 -0700705 return false;
Tao Bao40e144d2017-03-15 01:10:58 -0700706 }
Tao Bao09e84932018-08-31 11:25:05 -0700707 if (!SaveFileContents(Paths::Get().cache_temp_source(), &source_file)) {
Tao Bao859bfc52018-04-25 23:00:27 -0700708 LOG(ERROR) << "Failed to back up source file";
Tao Bao5609bc82018-06-20 00:30:48 -0700709 return false;
Tao Bao40e144d2017-03-15 01:10:58 -0700710 }
Doug Zongker512536a2010-02-17 16:11:44 -0800711
Tao Bao40e144d2017-03-15 01:10:58 -0700712 // We store the decoded output in memory.
Tao Bao5609bc82018-06-20 00:30:48 -0700713 FileContents patched;
Tao Bao8b0b0f12018-04-19 21:02:13 -0700714 SHA_CTX ctx;
715 SHA1_Init(&ctx);
Tao Bao5609bc82018-06-20 00:30:48 -0700716 SinkFn sink = [&patched, &ctx](const unsigned char* data, size_t len) {
Tao Bao8b0b0f12018-04-19 21:02:13 -0700717 SHA1_Update(&ctx, data, len);
Tao Bao5609bc82018-06-20 00:30:48 -0700718 patched.data.insert(patched.data.end(), data, data + len);
Tao Baoc0e1c462017-02-01 10:20:10 -0800719 return len;
720 };
Doug Zongkerc4351c72010-02-22 14:46:32 -0800721
Tao Bao40e144d2017-03-15 01:10:58 -0700722 int result;
723 if (use_bsdiff) {
Tao Bao5609bc82018-06-20 00:30:48 -0700724 result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), patch, 0, sink);
Tao Bao40e144d2017-03-15 01:10:58 -0700725 } else {
Tao Bao8b0b0f12018-04-19 21:02:13 -0700726 result =
Tao Bao5609bc82018-06-20 00:30:48 -0700727 ApplyImagePatch(source_file.data.data(), source_file.data.size(), patch, sink, bonus_data);
Tao Bao40e144d2017-03-15 01:10:58 -0700728 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800729
Tao Bao40e144d2017-03-15 01:10:58 -0700730 if (result != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700731 LOG(ERROR) << "Failed to apply the patch: " << result;
Tao Bao5609bc82018-06-20 00:30:48 -0700732 return false;
Tao Bao40e144d2017-03-15 01:10:58 -0700733 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800734
Tao Bao5609bc82018-06-20 00:30:48 -0700735 SHA1_Final(patched.sha1, &ctx);
736 if (memcmp(patched.sha1, expected_sha1, SHA_DIGEST_LENGTH) != 0) {
737 LOG(ERROR) << "Patching did not produce the expected SHA-1 of " << short_sha1(expected_sha1);
Tao Bao4f834302018-04-19 12:35:14 -0700738
Tao Bao5609bc82018-06-20 00:30:48 -0700739 LOG(ERROR) << "target size " << patched.data.size() << " SHA-1 " << short_sha1(patched.sha1);
Tao Bao859bfc52018-04-25 23:00:27 -0700740 LOG(ERROR) << "source size " << source_file.data.size() << " SHA-1 "
741 << short_sha1(source_file.sha1);
Tao Bao4f834302018-04-19 12:35:14 -0700742
743 uint8_t patch_digest[SHA_DIGEST_LENGTH];
Tao Bao5609bc82018-06-20 00:30:48 -0700744 SHA1(reinterpret_cast<const uint8_t*>(patch.data.data()), patch.data.size(), patch_digest);
745 LOG(ERROR) << "patch size " << patch.data.size() << " SHA-1 " << short_sha1(patch_digest);
Tao Bao4f834302018-04-19 12:35:14 -0700746
Tao Bao7ea515e2018-07-09 15:16:13 -0700747 if (bonus_data != nullptr) {
748 uint8_t bonus_digest[SHA_DIGEST_LENGTH];
749 SHA1(reinterpret_cast<const uint8_t*>(bonus_data->data.data()), bonus_data->data.size(),
750 bonus_digest);
751 LOG(ERROR) << "bonus size " << bonus_data->data.size() << " SHA-1 "
752 << short_sha1(bonus_digest);
753 }
Tao Bao4f834302018-04-19 12:35:14 -0700754
Tao Bao5609bc82018-06-20 00:30:48 -0700755 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800756 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800757
Tao Bao5609bc82018-06-20 00:30:48 -0700758 LOG(INFO) << " now " << short_sha1(expected_sha1);
759
Tao Bao40e144d2017-03-15 01:10:58 -0700760 // Write back the temp file to the partition.
Tao Bao5609bc82018-06-20 00:30:48 -0700761 if (!WriteBufferToPartition(patched, target)) {
762 LOG(ERROR) << "Failed to write patched data to " << target.name;
763 return false;
Tao Bao6e02ea92016-11-17 11:24:07 -0800764 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800765
Tao Bao40e144d2017-03-15 01:10:58 -0700766 // Delete the backup copy of the source.
Tao Bao641fa972018-04-25 18:59:40 -0700767 unlink(Paths::Get().cache_temp_source().c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800768
Tao Bao6e02ea92016-11-17 11:24:07 -0800769 // Success!
Tao Bao5609bc82018-06-20 00:30:48 -0700770 return true;
771}
772
773bool CheckPartition(const Partition& partition) {
774 FileContents target_file;
775 return ReadPartitionToBuffer(partition, &target_file, false);
776}
777
778Partition Partition::Parse(const std::string& input_str, std::string* err) {
779 std::vector<std::string> pieces = android::base::Split(input_str, ":");
780 if (pieces.size() != 4 || pieces[0] != "EMMC") {
781 *err = "Invalid number of tokens or non-eMMC target";
782 return {};
783 }
784
785 size_t size;
786 if (!android::base::ParseUint(pieces[2], &size) || size == 0) {
787 *err = "Failed to parse \"" + pieces[2] + "\" as byte count";
788 return {};
789 }
790
791 return Partition(pieces[1], size, pieces[3]);
792}
793
794std::string Partition::ToString() const {
795 if (*this) {
796 return "EMMC:"s + name + ":" + std::to_string(size) + ":" + hash;
797 }
798 return "<invalid-partition>";
799}
800
801std::ostream& operator<<(std::ostream& os, const Partition& partition) {
802 os << partition.ToString();
803 return os;
Doug Zongker512536a2010-02-17 16:11:44 -0800804}