blob: 54c37eb343df302c928eacbc90809b85203ef19a [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao8fce75a2016-11-10 12:33:41 -080017#include "applypatch/applypatch.h"
18
Doug Zongker512536a2010-02-17 16:11:44 -080019#include <errno.h>
Tao Baoba9a42a2015-06-23 23:23:33 -070020#include <fcntl.h>
Doug Zongker512536a2010-02-17 16:11:44 -080021#include <libgen.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/statfs.h>
27#include <sys/types.h>
Doug Zongker512536a2010-02-17 16:11:44 -080028#include <unistd.h>
29
Yabin Cuid483c202016-02-03 17:08:52 -080030#include <memory>
31#include <string>
Tao Bao8fce75a2016-11-10 12:33:41 -080032#include <utility>
33#include <vector>
Yabin Cuid483c202016-02-03 17:08:52 -080034
Tao Bao40e144d2017-03-15 01:10:58 -070035#include <android-base/logging.h>
Tao Bao8fce75a2016-11-10 12:33:41 -080036#include <android-base/parseint.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080037#include <android-base/strings.h>
Tao Bao8fce75a2016-11-10 12:33:41 -080038#include <openssl/sha.h>
Tao Baoaca8e892015-07-17 11:47:44 -070039
Conn O'Griofad9201042014-03-25 01:26:49 +000040#include "bmlutils/bmlutils.h"
Doug Zongker512536a2010-02-17 16:11:44 -080041#include "mtdutils/mtdutils.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050042
Doug Zongkerc4351c72010-02-22 14:46:32 -080043#include "edify/expr.h"
Jed Estepff6df892015-12-15 16:04:53 -080044#include "ota_io.h"
Tao Baoe6aa3322015-08-05 15:20:27 -070045#include "print_sha1.h"
Doug Zongker512536a2010-02-17 16:11:44 -080046
Tao Bao8fce75a2016-11-10 12:33:41 -080047static int LoadPartitionContents(const std::string& filename, FileContents* file);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070048static ssize_t FileSink(const unsigned char* data, ssize_t len, void* token);
Tao Bao40e144d2017-03-15 01:10:58 -070049static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
50 const std::string& target_filename,
51 const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data);
Doug Zongker512536a2010-02-17 16:11:44 -080052
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 }
Doug Zongker512536a2010-02-17 16:11:44 -080065
Tao Bao8fce75a2016-11-10 12:33:41 -080066 if (stat(filename, &file->st) == -1) {
67 printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
68 return -1;
69 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080070
Tao Bao8fce75a2016-11-10 12:33:41 -080071 std::vector<unsigned char> data(file->st.st_size);
Tao Bao358c2ec2016-11-28 11:48:43 -080072 unique_file f(ota_fopen(filename, "rb"));
Tao Bao8fce75a2016-11-10 12:33:41 -080073 if (!f) {
74 printf("failed to open \"%s\": %s\n", filename, strerror(errno));
75 return -1;
76 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080077
Tao Bao8fce75a2016-11-10 12:33:41 -080078 size_t bytes_read = ota_fread(data.data(), 1, data.size(), f.get());
79 if (bytes_read != data.size()) {
80 printf("short read of \"%s\" (%zu bytes of %zu)\n", filename, bytes_read, data.size());
81 return -1;
82 }
83 file->data = std::move(data);
84 SHA1(file->data.data(), file->data.size(), file->sha1);
85 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -080086}
87
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;
142 }
143
144 SHA_CTX sha_ctx;
145 SHA1_Init(&sha_ctx);
146
147 // Allocate enough memory to hold the largest size.
148 std::vector<unsigned char> buffer(pairs[pair_count - 1].first);
149 unsigned char* buffer_ptr = buffer.data();
150 size_t buffer_size = 0; // # bytes read so far
151 bool found = false;
152
153 for (const auto& pair : pairs) {
154 size_t current_size = pair.first;
155 const std::string& current_sha1 = pair.second;
156
157 // Read enough additional bytes to get us up to the next size. (Again,
158 // we're trying the possibilities in order of increasing size).
159 size_t next = current_size - buffer_size;
160 if (next > 0) {
161 size_t read = ota_fread(buffer_ptr, 1, next, dev.get());
162 if (next != read) {
163 printf("short read (%zu bytes of %zu) for partition \"%s\"\n", read, next, partition);
Tao Baoaca8e892015-07-17 11:47:44 -0700164 return -1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800165 }
166 SHA1_Update(&sha_ctx, buffer_ptr, read);
167 buffer_size += read;
168 buffer_ptr += read;
Tao Baoaca8e892015-07-17 11:47:44 -0700169 }
Doug Zongker512536a2010-02-17 16:11:44 -0800170
Ethan Yonkerb8e985c2016-08-31 13:42:11 -0500171 if (pieces[0] == "BML") {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500172 if (strcmp(partition, "boot") == 0) {
173 partition = BOARD_BML_BOOT;
174 } else if (strcmp(partition, "recovery") == 0) {
175 partition = BOARD_BML_RECOVERY;
176 }
Conn O'Griofad9201042014-03-25 01:26:49 +0000177 }
178
Tao Bao8fce75a2016-11-10 12:33:41 -0800179 // Duplicate the SHA context and finalize the duplicate so we can
180 // check it against this pair's expected hash.
181 SHA_CTX temp_ctx;
182 memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
183 uint8_t sha_so_far[SHA_DIGEST_LENGTH];
184 SHA1_Final(sha_so_far, &temp_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800185
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800186 uint8_t parsed_sha[SHA_DIGEST_LENGTH];
Tao Bao8fce75a2016-11-10 12:33:41 -0800187 if (ParseSha1(current_sha1.c_str(), parsed_sha) != 0) {
188 printf("failed to parse SHA-1 %s in %s\n", current_sha1.c_str(), filename.c_str());
189 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800190 }
191
Tao Bao8fce75a2016-11-10 12:33:41 -0800192 if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) {
193 // We have a match. Stop reading the partition; we'll return the data we've read so far.
194 printf("partition read matched size %zu SHA-1 %s\n", current_size, current_sha1.c_str());
195 found = true;
196 break;
Doug Zongkerf291d852010-07-07 13:55:25 -0700197 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800198 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700199
Tao Bao8fce75a2016-11-10 12:33:41 -0800200 if (!found) {
201 // Ran off the end of the list of (size, sha1) pairs without finding a match.
202 printf("contents of partition \"%s\" didn't match %s\n", partition, filename.c_str());
203 return -1;
204 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800205
Tao Bao8fce75a2016-11-10 12:33:41 -0800206 SHA1_Final(file->sha1, &sha_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800207
Tao Bao8fce75a2016-11-10 12:33:41 -0800208 buffer.resize(buffer_size);
209 file->data = std::move(buffer);
210 // Fake some stat() info.
211 file->st.st_mode = 0644;
212 file->st.st_uid = 0;
213 file->st.st_gid = 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800214
Tao Bao8fce75a2016-11-10 12:33:41 -0800215 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800216}
217
Doug Zongker512536a2010-02-17 16:11:44 -0800218// Save the contents of the given FileContents object under the given
219// filename. Return 0 on success.
Doug Zongker1c43c972012-02-28 11:07:09 -0800220int SaveFileContents(const char* filename, const FileContents* file) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800221 unique_fd fd(ota_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
222 if (fd == -1) {
223 printf("failed to open \"%s\" for write: %s\n", filename, strerror(errno));
224 return -1;
225 }
Doug Zongker512536a2010-02-17 16:11:44 -0800226
Tao Bao6e02ea92016-11-17 11:24:07 -0800227 ssize_t bytes_written = FileSink(file->data.data(), file->data.size(), &fd);
228 if (bytes_written != static_cast<ssize_t>(file->data.size())) {
229 printf("short write of \"%s\" (%zd bytes of %zu): %s\n", filename, bytes_written,
230 file->data.size(), strerror(errno));
231 return -1;
232 }
233 if (ota_fsync(fd) != 0) {
234 printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno));
235 return -1;
236 }
Tao Bao3dc14cb2016-11-22 11:37:13 -0800237 if (ota_close(fd) != 0) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800238 printf("close of \"%s\" failed: %s\n", filename, strerror(errno));
239 return -1;
240 }
Doug Zongker512536a2010-02-17 16:11:44 -0800241
Tao Bao6e02ea92016-11-17 11:24:07 -0800242 if (chmod(filename, file->st.st_mode) != 0) {
243 printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno));
244 return -1;
245 }
246 if (chown(filename, file->st.st_uid, file->st.st_gid) != 0) {
247 printf("chown of \"%s\" failed: %s\n", filename, strerror(errno));
248 return -1;
249 }
Doug Zongker512536a2010-02-17 16:11:44 -0800250
Tao Bao6e02ea92016-11-17 11:24:07 -0800251 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800252}
253
Doug Zongkerf291d852010-07-07 13:55:25 -0700254// Write a memory buffer to 'target' partition, a string of the form
Elliott Hughes63a31922016-06-09 17:41:22 -0700255// "EMMC:<partition_device>[:...]". The target name
Tao Bao1ce7a2a2015-07-24 15:29:12 -0700256// might contain multiple colons, but WriteToPartition() only uses the first
257// two and ignores the rest. Return 0 on success.
Tao Bao6e02ea92016-11-17 11:24:07 -0800258int WriteToPartition(const unsigned char* data, size_t len, const std::string& target) {
Tao Baoaca8e892015-07-17 11:47:44 -0700259 std::string copy(target);
260 std::vector<std::string> pieces = android::base::Split(copy, ":");
261
Tao Bao1ce7a2a2015-07-24 15:29:12 -0700262 if (pieces.size() < 2) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500263 printf("WriteToPartition called with bad target (%s)\n", target.c_str());
Tao Baoaca8e892015-07-17 11:47:44 -0700264 return -1;
265 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700266
267 enum PartitionType type;
Tao Baoaca8e892015-07-17 11:47:44 -0700268 if (pieces[0] == "MTD") {
Doug Zongkerf291d852010-07-07 13:55:25 -0700269 type = MTD;
Tao Baoaca8e892015-07-17 11:47:44 -0700270 } else if (pieces[0] == "EMMC") {
Doug Zongkerf291d852010-07-07 13:55:25 -0700271 type = EMMC;
Ethan Yonkerb8e985c2016-08-31 13:42:11 -0500272 } else if (pieces[0] == "BML") {
Conn O'Griofad9201042014-03-25 01:26:49 +0000273 type = EMMC;
Doug Zongkerf291d852010-07-07 13:55:25 -0700274 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500275 printf("WriteToPartition called with bad target (%s)\n", target.c_str());
Doug Zongkerf291d852010-07-07 13:55:25 -0700276 return -1;
277 }
Ethan Yonker34ae4832016-08-24 15:32:18 -0500278
Tao Baoaca8e892015-07-17 11:47:44 -0700279 const char* partition = pieces[1].c_str();
Doug Zongkerf291d852010-07-07 13:55:25 -0700280
Ethan Yonkerb8e985c2016-08-31 13:42:11 -0500281 if (pieces[0] == "BML") {
Conn O'Griofad9201042014-03-25 01:26:49 +0000282 if (strcmp(partition, "boot") == 0) {
283 partition = BOARD_BML_BOOT;
284 } else if (strcmp(partition, "recovery") == 0) {
285 partition = BOARD_BML_RECOVERY;
286 }
287
288 int bmlpartition = open(partition, O_RDWR | O_LARGEFILE);
289 if (bmlpartition < 0)
290 return -1;
291 if (ioctl(bmlpartition, BML_UNLOCK_ALL, 0)) {
292 printf("failed to unlock BML partition: (%s)\n", partition);
293 return -1;
294 }
295 close(bmlpartition);
296 }
297
Doug Zongkerc4351c72010-02-22 14:46:32 -0800298 if (partition == NULL) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500299 printf("bad partition target name \"%s\"\n", target.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800300 return -1;
301 }
Doug Zongker512536a2010-02-17 16:11:44 -0800302
Doug Zongkerf291d852010-07-07 13:55:25 -0700303 switch (type) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700304 case MTD: {
Doug Zongkerf291d852010-07-07 13:55:25 -0700305 if (!mtd_partitions_scanned) {
306 mtd_scan_partitions();
Tao Baoaca8e892015-07-17 11:47:44 -0700307 mtd_partitions_scanned = true;
Doug Zongkerf291d852010-07-07 13:55:25 -0700308 }
309
310 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
311 if (mtd == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700312 printf("mtd partition \"%s\" not found for writing\n", partition);
Doug Zongkerf291d852010-07-07 13:55:25 -0700313 return -1;
314 }
315
316 MtdWriteContext* ctx = mtd_write_partition(mtd);
317 if (ctx == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700318 printf("failed to init mtd partition \"%s\" for writing\n", partition);
Doug Zongkerf291d852010-07-07 13:55:25 -0700319 return -1;
320 }
321
Yabin Cuid483c202016-02-03 17:08:52 -0800322 size_t written = mtd_write_data(ctx, reinterpret_cast<const char*>(data), len);
Doug Zongkerf291d852010-07-07 13:55:25 -0700323 if (written != len) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700324 printf("only wrote %zu of %zu bytes to MTD %s\n", written, len, partition);
Doug Zongkerf291d852010-07-07 13:55:25 -0700325 mtd_write_close(ctx);
326 return -1;
327 }
328
329 if (mtd_erase_blocks(ctx, -1) < 0) {
330 printf("error finishing mtd write of %s\n", partition);
331 mtd_write_close(ctx);
332 return -1;
333 }
334
335 if (mtd_write_close(ctx)) {
336 printf("error closing mtd write of %s\n", partition);
337 return -1;
338 }
339 break;
Tao Baoba9a42a2015-06-23 23:23:33 -0700340 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700341
Tao Baoba9a42a2015-06-23 23:23:33 -0700342 case EMMC: {
Doug Zongker044a0b42013-07-08 09:42:54 -0700343 size_t start = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700344 bool success = false;
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500345 unique_fd fd(ota_open(partition, O_RDWR | O_SYNC));
Doug Zongkerc870a992013-07-09 10:34:46 -0700346 if (fd < 0) {
Doug Zongker044a0b42013-07-08 09:42:54 -0700347 printf("failed to open %s: %s\n", partition, strerror(errno));
Doug Zongkerf291d852010-07-07 13:55:25 -0700348 return -1;
349 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700350
Tao Baoaca8e892015-07-17 11:47:44 -0700351 for (size_t attempt = 0; attempt < 2; ++attempt) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700352 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700353 printf("failed seek on %s: %s\n", partition, strerror(errno));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700354 return -1;
355 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700356 while (start < len) {
357 size_t to_write = len - start;
Doug Zongker168724c2013-12-19 15:16:57 -0800358 if (to_write > 1<<20) to_write = 1<<20;
Doug Zongker044a0b42013-07-08 09:42:54 -0700359
Jed Estepf1fc48c2015-12-15 16:04:53 -0800360 ssize_t written = TEMP_FAILURE_RETRY(ota_write(fd, data+start, to_write));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700361 if (written == -1) {
362 printf("failed write writing to %s: %s\n", partition, strerror(errno));
363 return -1;
Doug Zongker044a0b42013-07-08 09:42:54 -0700364 }
Doug Zongkerc870a992013-07-09 10:34:46 -0700365 start += written;
Doug Zongker044a0b42013-07-08 09:42:54 -0700366 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800367 if (ota_fsync(fd) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700368 printf("failed to sync to %s (%s)\n", partition, strerror(errno));
Michael Rungecddb68b2014-10-29 12:42:15 -0700369 return -1;
370 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800371 if (ota_close(fd) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700372 printf("failed to close %s (%s)\n", partition, strerror(errno));
Michael Rungecddb68b2014-10-29 12:42:15 -0700373 return -1;
374 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500375 unique_fd fd(ota_open(partition, O_RDONLY));
Michael Rungecddb68b2014-10-29 12:42:15 -0700376 if (fd < 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700377 printf("failed to reopen %s for verify (%s)\n", partition, strerror(errno));
Michael Rungecddb68b2014-10-29 12:42:15 -0700378 return -1;
379 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700380
Tao Baoba9a42a2015-06-23 23:23:33 -0700381 // Drop caches so our subsequent verification read
Doug Zongker044a0b42013-07-08 09:42:54 -0700382 // won't just be reading the cache.
383 sync();
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500384 unique_fd dc(ota_open("/proc/sys/vm/drop_caches", O_WRONLY));
Jed Estepf1fc48c2015-12-15 16:04:53 -0800385 if (TEMP_FAILURE_RETRY(ota_write(dc, "3\n", 2)) == -1) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700386 printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno));
387 } else {
388 printf(" caches dropped\n");
389 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800390 ota_close(dc);
Doug Zongker044a0b42013-07-08 09:42:54 -0700391 sleep(1);
Doug Zongker044a0b42013-07-08 09:42:54 -0700392
393 // verify
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700394 if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
395 printf("failed to seek back to beginning of %s: %s\n",
396 partition, strerror(errno));
397 return -1;
398 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700399 unsigned char buffer[4096];
400 start = len;
Tao Baoba9a42a2015-06-23 23:23:33 -0700401 for (size_t p = 0; p < len; p += sizeof(buffer)) {
Doug Zongker044a0b42013-07-08 09:42:54 -0700402 size_t to_read = len - p;
Tao Baoba9a42a2015-06-23 23:23:33 -0700403 if (to_read > sizeof(buffer)) {
404 to_read = sizeof(buffer);
405 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700406
Doug Zongkerc870a992013-07-09 10:34:46 -0700407 size_t so_far = 0;
408 while (so_far < to_read) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700409 ssize_t read_count =
Jed Estepf1fc48c2015-12-15 16:04:53 -0800410 TEMP_FAILURE_RETRY(ota_read(fd, buffer+so_far, to_read-so_far));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700411 if (read_count == -1) {
412 printf("verify read error %s at %zu: %s\n",
413 partition, p, strerror(errno));
414 return -1;
Doug Zongkerc870a992013-07-09 10:34:46 -0700415 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700416 if (static_cast<size_t>(read_count) < to_read) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700417 printf("short verify read %s at %zu: %zd %zu %s\n",
Doug Zongkerc870a992013-07-09 10:34:46 -0700418 partition, p, read_count, to_read, strerror(errno));
419 }
420 so_far += read_count;
Doug Zongker044a0b42013-07-08 09:42:54 -0700421 }
422
Tao Baoba9a42a2015-06-23 23:23:33 -0700423 if (memcmp(buffer, data+p, to_read) != 0) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700424 printf("verification failed starting at %zu\n", p);
Doug Zongker044a0b42013-07-08 09:42:54 -0700425 start = p;
426 break;
427 }
428 }
429
430 if (start == len) {
Tao Baoabba55b2015-07-17 18:11:12 -0700431 printf("verification read succeeded (attempt %zu)\n", attempt+1);
Doug Zongker044a0b42013-07-08 09:42:54 -0700432 success = true;
433 break;
434 }
435 }
436
437 if (!success) {
438 printf("failed to verify after all attempts\n");
439 return -1;
440 }
441
Jed Estepf1fc48c2015-12-15 16:04:53 -0800442 if (ota_close(fd) != 0) {
Doug Zongkerf291d852010-07-07 13:55:25 -0700443 printf("error closing %s (%s)\n", partition, strerror(errno));
444 return -1;
445 }
Doug Zongkerbf4a69a2013-07-10 13:39:50 -0700446 sync();
Doug Zongkerf291d852010-07-07 13:55:25 -0700447 break;
Doug Zongker044a0b42013-07-08 09:42:54 -0700448 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800449 }
Doug Zongker512536a2010-02-17 16:11:44 -0800450
Doug Zongkerc4351c72010-02-22 14:46:32 -0800451 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800452}
453
Doug Zongker512536a2010-02-17 16:11:44 -0800454// Take a string 'str' of 40 hex digits and parse it into the 20
455// byte array 'digest'. 'str' may contain only the digest or be of
456// the form "<digest>:<anything>". Return 0 on success, -1 on any
457// error.
458int ParseSha1(const char* str, uint8_t* digest) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800459 const char* ps = str;
460 uint8_t* pd = digest;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800461 for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800462 int digit;
463 if (*ps >= '0' && *ps <= '9') {
464 digit = *ps - '0';
465 } else if (*ps >= 'a' && *ps <= 'f') {
466 digit = *ps - 'a' + 10;
467 } else if (*ps >= 'A' && *ps <= 'F') {
468 digit = *ps - 'A' + 10;
469 } else {
470 return -1;
471 }
472 if (i % 2 == 0) {
473 *pd = digit << 4;
474 } else {
475 *pd |= digit;
476 ++pd;
477 }
Doug Zongker512536a2010-02-17 16:11:44 -0800478 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800479 if (*ps != '\0') return -1;
480 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800481}
482
Doug Zongkerc4351c72010-02-22 14:46:32 -0800483// Search an array of sha1 strings for one matching the given sha1.
484// Return the index of the match on success, or -1 if no match is
485// found.
Tao Baoc8e79342016-12-28 10:11:22 -0800486static int FindMatchingPatch(uint8_t* sha1, const std::vector<std::string>& patch_sha1_str) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800487 for (size_t i = 0; i < patch_sha1_str.size(); ++i) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800488 uint8_t patch_sha1[SHA_DIGEST_LENGTH];
Tao Bao8fce75a2016-11-10 12:33:41 -0800489 if (ParseSha1(patch_sha1_str[i].c_str(), patch_sha1) == 0 &&
490 memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
491 return i;
Doug Zongker512536a2010-02-17 16:11:44 -0800492 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800493 }
494 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800495}
496
497// Returns 0 if the contents of the file (argv[2]) or the cached file
498// match any of the sha1's on the command line (argv[3:]). Returns
499// nonzero otherwise.
Tianjie Xuaced5d92016-10-12 10:55:04 -0700500int applypatch_check(const char* filename, const std::vector<std::string>& patch_sha1_str) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800501 FileContents file;
Doug Zongker512536a2010-02-17 16:11:44 -0800502
Tao Bao8fce75a2016-11-10 12:33:41 -0800503 // It's okay to specify no sha1s; the check will pass if the
504 // LoadFileContents is successful. (Useful for reading
505 // partitions, where the filename encodes the sha1s; no need to
506 // check them twice.)
507 if (LoadFileContents(filename, &file) != 0 ||
508 (!patch_sha1_str.empty() && FindMatchingPatch(file.sha1, patch_sha1_str) < 0)) {
509 printf("file \"%s\" doesn't have any of expected sha1 sums; checking cache\n", filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800510
Tao Bao8fce75a2016-11-10 12:33:41 -0800511 // If the source file is missing or corrupted, it might be because
512 // we were killed in the middle of patching it. A copy of it
513 // should have been made in CACHE_TEMP_SOURCE. If that file
514 // exists and matches the sha1 we're looking for, the check still
515 // passes.
516 if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
517 printf("failed to load cache file\n");
518 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800519 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800520
521 if (FindMatchingPatch(file.sha1, patch_sha1_str) < 0) {
522 printf("cache bits don't match any sha1 for \"%s\"\n", filename);
523 return 1;
524 }
525 }
526 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800527}
528
529int ShowLicenses() {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800530 ShowBSDiffLicense();
531 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800532}
533
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700534ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) {
Yabin Cuid483c202016-02-03 17:08:52 -0800535 int fd = *static_cast<int*>(token);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800536 ssize_t done = 0;
537 ssize_t wrote;
Tao Baoba9a42a2015-06-23 23:23:33 -0700538 while (done < len) {
Jed Estepf1fc48c2015-12-15 16:04:53 -0800539 wrote = TEMP_FAILURE_RETRY(ota_write(fd, data+done, len-done));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700540 if (wrote == -1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700541 printf("error writing %zd bytes: %s\n", (len-done), strerror(errno));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800542 return done;
543 }
544 done += wrote;
Doug Zongker512536a2010-02-17 16:11:44 -0800545 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800546 return done;
Doug Zongker512536a2010-02-17 16:11:44 -0800547}
548
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700549ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) {
Yabin Cuid483c202016-02-03 17:08:52 -0800550 std::string* s = static_cast<std::string*>(token);
551 s->append(reinterpret_cast<const char*>(data), len);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800552 return len;
Doug Zongker512536a2010-02-17 16:11:44 -0800553}
554
555// Return the amount of free space (in bytes) on the filesystem
556// containing filename. filename must exist. Return -1 on error.
557size_t FreeSpaceForFile(const char* filename) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800558 struct statfs sf;
559 if (statfs(filename, &sf) != 0) {
560 printf("failed to statfs %s: %s\n", filename, strerror(errno));
561 return -1;
562 }
caozhiyuancb9450e2015-05-19 17:21:00 +0800563 return sf.f_bsize * sf.f_bavail;
Doug Zongker512536a2010-02-17 16:11:44 -0800564}
565
Doug Zongkerc4351c72010-02-22 14:46:32 -0800566int CacheSizeCheck(size_t bytes) {
567 if (MakeFreeSpaceOnCache(bytes) < 0) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700568 printf("unable to make %zu bytes available on /cache\n", bytes);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800569 return 1;
570 } else {
571 return 0;
572 }
573}
574
Tao Bao40e144d2017-03-15 01:10:58 -0700575// This function applies binary patches to EMMC target files in a way that is safe (the original
576// file is not touched until we have the desired replacement for it) and idempotent (it's okay to
577// run this program multiple times).
Doug Zongker512536a2010-02-17 16:11:44 -0800578//
Tao Bao40e144d2017-03-15 01:10:58 -0700579// - If the SHA-1 hash of <target_filename> is <target_sha1_string>, does nothing and exits
580// successfully.
Doug Zongker512536a2010-02-17 16:11:44 -0800581//
Tao Bao40e144d2017-03-15 01:10:58 -0700582// - Otherwise, if the SHA-1 hash of <source_filename> is one of the entries in <patch_sha1_str>,
583// the corresponding patch from <patch_data> (which must be a VAL_BLOB) is applied to produce a
584// new file (the type of patch is automatically detected from the blob data). If that new file
585// has SHA-1 hash <target_sha1_str>, moves it to replace <target_filename>, and exits
586// successfully. Note that if <source_filename> and <target_filename> are not the same,
587// <source_filename> is NOT deleted on success. <target_filename> may be the string "-" to mean
588// "the same as <source_filename>".
Doug Zongker512536a2010-02-17 16:11:44 -0800589//
Tao Bao40e144d2017-03-15 01:10:58 -0700590// - Otherwise, or if any error is encountered, exits with non-zero status.
Doug Zongker512536a2010-02-17 16:11:44 -0800591//
Tao Bao40e144d2017-03-15 01:10:58 -0700592// <source_filename> must refer to an EMMC partition to read the source data. See the comments for
593// the LoadPartitionContents() function above for the format of such a filename. <target_size> has
594// become obsolete since we have dropped the support for patching non-EMMC targets (EMMC targets
595// have the size embedded in the filename).
596int applypatch(const char* source_filename, const char* target_filename,
597 const char* target_sha1_str, size_t target_size __unused,
Tianjie Xuaced5d92016-10-12 10:55:04 -0700598 const std::vector<std::string>& patch_sha1_str,
Tao Bao40e144d2017-03-15 01:10:58 -0700599 const std::vector<std::unique_ptr<Value>>& patch_data, const Value* bonus_data) {
600 printf("patch %s: ", source_filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800601
Tao Bao40e144d2017-03-15 01:10:58 -0700602 if (target_filename[0] == '-' && target_filename[1] == '\0') {
603 target_filename = source_filename;
604 }
Doug Zongker512536a2010-02-17 16:11:44 -0800605
Tao Bao40e144d2017-03-15 01:10:58 -0700606 if (strncmp(target_filename, "EMMC:", 5) != 0) {
607 printf("Supporting patching EMMC targets only.\n");
608 return 1;
609 }
610
611 uint8_t target_sha1[SHA_DIGEST_LENGTH];
612 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
613 printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
614 return 1;
615 }
616
617 // We try to load the target file into the source_file object.
618 FileContents source_file;
619 if (LoadFileContents(target_filename, &source_file) == 0) {
620 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
621 // The early-exit case: the patch was already applied, this file has the desired hash, nothing
622 // for us to do.
623 printf("already %s\n", short_sha1(target_sha1).c_str());
624 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800625 }
Tao Bao40e144d2017-03-15 01:10:58 -0700626 }
Doug Zongker512536a2010-02-17 16:11:44 -0800627
Tao Bao40e144d2017-03-15 01:10:58 -0700628 if (source_file.data.empty() ||
629 (target_filename != source_filename && strcmp(target_filename, source_filename) != 0)) {
630 // Need to load the source file: either we failed to load the target file, or we did but it's
631 // different from the expected.
632 source_file.data.clear();
633 LoadFileContents(source_filename, &source_file);
634 }
635
636 if (!source_file.data.empty()) {
637 int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str);
638 if (to_use != -1) {
639 return GenerateTarget(source_file, patch_data[to_use], target_filename, target_sha1,
640 bonus_data);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800641 }
Tao Bao40e144d2017-03-15 01:10:58 -0700642 }
Doug Zongker512536a2010-02-17 16:11:44 -0800643
Tao Bao40e144d2017-03-15 01:10:58 -0700644 printf("source file is bad; trying copy\n");
Doug Zongker512536a2010-02-17 16:11:44 -0800645
Tao Bao40e144d2017-03-15 01:10:58 -0700646 FileContents copy_file;
647 if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file) < 0) {
648 printf("failed to read copy file\n");
649 return 1;
650 }
Doug Zongker512536a2010-02-17 16:11:44 -0800651
Tao Bao40e144d2017-03-15 01:10:58 -0700652 int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str);
653 if (to_use == -1) {
654 printf("copy file doesn't match source SHA-1s either\n");
655 return 1;
656 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800657
Tao Bao40e144d2017-03-15 01:10:58 -0700658 return GenerateTarget(copy_file, patch_data[to_use], target_filename, target_sha1, bonus_data);
Doug Zongker1c43c972012-02-28 11:07:09 -0800659}
Doug Zongker1c43c972012-02-28 11:07:09 -0800660
Tao Baoabba55b2015-07-17 18:11:12 -0700661/*
662 * This function flashes a given image to the target partition. It verifies
663 * the target cheksum first, and will return if target has the desired hash.
664 * It checks the checksum of the given source image before flashing, and
665 * verifies the target partition afterwards. The function is idempotent.
666 * Returns zero on success.
667 */
668int applypatch_flash(const char* source_filename, const char* target_filename,
669 const char* target_sha1_str, size_t target_size) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800670 printf("flash %s: ", target_filename);
Tao Baoabba55b2015-07-17 18:11:12 -0700671
Tao Bao6e02ea92016-11-17 11:24:07 -0800672 uint8_t target_sha1[SHA_DIGEST_LENGTH];
673 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
674 printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
675 return 1;
676 }
Tao Baoabba55b2015-07-17 18:11:12 -0700677
Tao Bao6e02ea92016-11-17 11:24:07 -0800678 std::string target_str(target_filename);
679 std::vector<std::string> pieces = android::base::Split(target_str, ":");
680 if (pieces.size() != 2 || pieces[0] != "EMMC") {
681 printf("invalid target name \"%s\"", target_filename);
682 return 1;
683 }
Tao Baoabba55b2015-07-17 18:11:12 -0700684
Tao Bao6e02ea92016-11-17 11:24:07 -0800685 // Load the target into the source_file object to see if already applied.
686 pieces.push_back(std::to_string(target_size));
687 pieces.push_back(target_sha1_str);
688 std::string fullname = android::base::Join(pieces, ':');
689 FileContents source_file;
690 if (LoadPartitionContents(fullname, &source_file) == 0 &&
691 memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
692 // The early-exit case: the image was already applied, this partition
693 // has the desired hash, nothing for us to do.
694 printf("already %s\n", short_sha1(target_sha1).c_str());
Tao Baoabba55b2015-07-17 18:11:12 -0700695 return 0;
Tao Bao6e02ea92016-11-17 11:24:07 -0800696 }
697
698 if (LoadFileContents(source_filename, &source_file) == 0) {
699 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
700 // The source doesn't have desired checksum.
701 printf("source \"%s\" doesn't have expected sha1 sum\n", source_filename);
702 printf("expected: %s, found: %s\n", short_sha1(target_sha1).c_str(),
703 short_sha1(source_file.sha1).c_str());
704 return 1;
705 }
706 }
707
708 if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
709 printf("write of copied data to %s failed\n", target_filename);
710 return 1;
711 }
712 return 0;
Doug Zongker1c43c972012-02-28 11:07:09 -0800713}
714
Tao Bao40e144d2017-03-15 01:10:58 -0700715static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
716 const std::string& target_filename,
717 const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800718 if (patch->type != VAL_BLOB) {
719 printf("patch is not a blob\n");
720 return 1;
721 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800722
Tao Bao6e02ea92016-11-17 11:24:07 -0800723 const char* header = &patch->data[0];
724 size_t header_bytes_read = patch->data.size();
725 bool use_bsdiff = false;
726 if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
727 use_bsdiff = true;
728 } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
729 use_bsdiff = false;
730 } else {
731 printf("Unknown patch file format\n");
732 return 1;
733 }
Yabin Cuid483c202016-02-03 17:08:52 -0800734
Tao Bao40e144d2017-03-15 01:10:58 -0700735 CHECK(android::base::StartsWith(target_filename, "EMMC:"));
Yabin Cuid483c202016-02-03 17:08:52 -0800736
Tao Bao40e144d2017-03-15 01:10:58 -0700737 // We still write the original source to cache, in case the partition write is interrupted.
738 if (MakeFreeSpaceOnCache(source_file.data.size()) < 0) {
739 printf("not enough free space on /cache\n");
740 return 1;
741 }
742 if (SaveFileContents(CACHE_TEMP_SOURCE, &source_file) < 0) {
743 printf("failed to back up source file\n");
744 return 1;
745 }
Doug Zongker512536a2010-02-17 16:11:44 -0800746
Tao Bao40e144d2017-03-15 01:10:58 -0700747 // We store the decoded output in memory.
748 SinkFn sink = MemorySink;
Tao Bao6e02ea92016-11-17 11:24:07 -0800749 std::string memory_sink_str; // Don't need to reserve space.
Tao Bao40e144d2017-03-15 01:10:58 -0700750 void* token = &memory_sink_str;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800751
Tao Bao40e144d2017-03-15 01:10:58 -0700752 SHA_CTX ctx;
753 SHA1_Init(&ctx);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800754
Tao Bao40e144d2017-03-15 01:10:58 -0700755 int result;
756 if (use_bsdiff) {
757 result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), patch.get(), 0,
758 sink, token, &ctx);
759 } else {
760 result = ApplyImagePatch(source_file.data.data(), source_file.data.size(), patch.get(), sink,
761 token, &ctx, bonus_data);
762 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800763
Tao Bao40e144d2017-03-15 01:10:58 -0700764 if (result != 0) {
765 printf("applying patch failed\n");
766 return 1;
767 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800768
Tao Bao6e02ea92016-11-17 11:24:07 -0800769 uint8_t current_target_sha1[SHA_DIGEST_LENGTH];
770 SHA1_Final(current_target_sha1, &ctx);
771 if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
772 printf("patch did not produce expected sha1\n");
773 return 1;
774 } else {
775 printf("now %s\n", short_sha1(target_sha1).c_str());
776 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800777
Tao Bao40e144d2017-03-15 01:10:58 -0700778 // Write back the temp file to the partition.
779 if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
780 memory_sink_str.size(), target_filename) != 0) {
781 printf("write of patched data to %s failed\n", target_filename.c_str());
782 return 1;
Tao Bao6e02ea92016-11-17 11:24:07 -0800783 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800784
Tao Bao40e144d2017-03-15 01:10:58 -0700785 // Delete the backup copy of the source.
786 unlink(CACHE_TEMP_SOURCE);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800787
Tao Bao6e02ea92016-11-17 11:24:07 -0800788 // Success!
789 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800790}