blob: 5c6c83f4f7dda8f0e3b91e26ba633eeb7e79f59a [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
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
Tao Bao40e144d2017-03-15 01:10:58 -070036#include <android-base/logging.h>
Tao Bao8fce75a2016-11-10 12:33:41 -080037#include <android-base/parseint.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080038#include <android-base/strings.h>
Tao Bao8fce75a2016-11-10 12:33:41 -080039#include <openssl/sha.h>
Tao Baoaca8e892015-07-17 11:47:44 -070040
Conn O'Griofad9201042014-03-25 01:26:49 +000041#include "bmlutils/bmlutils.h"
Doug Zongker512536a2010-02-17 16:11:44 -080042#include "mtdutils/mtdutils.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050043
Doug Zongkerc4351c72010-02-22 14:46:32 -080044#include "edify/expr.h"
Tao Baod33b2f82017-09-28 21:29:11 -070045#include "otafault/ota_io.h"
Tianjie Xu3bbb20f2018-02-27 15:56:11 -080046#include "otautil/cache_location.h"
Tao Bao09e468f2017-09-29 14:39:33 -070047#include "otautil/print_sha1.h"
Doug Zongker512536a2010-02-17 16:11:44 -080048
Tao Bao8fce75a2016-11-10 12:33:41 -080049static int LoadPartitionContents(const std::string& filename, FileContents* file);
Tao Baoc0e1c462017-02-01 10:20:10 -080050static size_t FileSink(const unsigned char* data, size_t len, int fd);
Tao Bao40e144d2017-03-15 01:10:58 -070051static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
52 const std::string& target_filename,
53 const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data);
Doug Zongker512536a2010-02-17 16:11:44 -080054
Tao Baoaca8e892015-07-17 11:47:44 -070055static bool mtd_partitions_scanned = false;
Doug Zongker512536a2010-02-17 16:11:44 -080056
Tao Bao8fce75a2016-11-10 12:33:41 -080057// Read a file into memory; store the file contents and associated metadata in *file.
Hristo Bojinovdb314d62010-08-02 10:29:49 -070058// Return 0 on success.
Doug Zongkera1bc1482014-02-13 15:18:19 -080059int LoadFileContents(const char* filename, FileContents* file) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -050060 // A special 'filename' beginning with "MTD:" or "EMMC:" means to
61 // load the contents of a partition.
62 if (strncmp(filename, "MTD:", 4) == 0 ||
63 strncmp(filename, "EMMC:", 5) == 0 ||
64 strncmp(filename, "BML:", 4) == 0) {
Tao Bao8fce75a2016-11-10 12:33:41 -080065 return LoadPartitionContents(filename, file);
66 }
Doug Zongker512536a2010-02-17 16:11:44 -080067
Tao Bao47e5a8d2017-12-01 10:53:34 -080068 struct stat sb;
69 if (stat(filename, &sb) == -1) {
Tao Bao8fce75a2016-11-10 12:33:41 -080070 printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
71 return -1;
72 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080073
Tao Bao47e5a8d2017-12-01 10:53:34 -080074 std::vector<unsigned char> data(sb.st_size);
Tao Bao358c2ec2016-11-28 11:48:43 -080075 unique_file f(ota_fopen(filename, "rb"));
Tao Bao8fce75a2016-11-10 12:33:41 -080076 if (!f) {
77 printf("failed to open \"%s\": %s\n", filename, strerror(errno));
78 return -1;
79 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080080
Tao Bao8fce75a2016-11-10 12:33:41 -080081 size_t bytes_read = ota_fread(data.data(), 1, data.size(), f.get());
82 if (bytes_read != data.size()) {
83 printf("short read of \"%s\" (%zu bytes of %zu)\n", filename, bytes_read, data.size());
84 return -1;
85 }
86 file->data = std::move(data);
87 SHA1(file->data.data(), file->data.size(), file->sha1);
88 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -080089}
90
Elliott Hughes63a31922016-06-09 17:41:22 -070091// Load the contents of an EMMC partition into the provided
Doug Zongker512536a2010-02-17 16:11:44 -080092// FileContents. filename should be a string of the form
Elliott Hughes63a31922016-06-09 17:41:22 -070093// "EMMC:<partition_device>:...". The smallest size_n bytes for
Doug Zongkerf291d852010-07-07 13:55:25 -070094// which that prefix of the partition contents has the corresponding
95// sha1 hash will be loaded. It is acceptable for a size value to be
96// repeated with different sha1s. Will return 0 on success.
Doug Zongker512536a2010-02-17 16:11:44 -080097//
98// This complexity is needed because if an OTA installation is
99// interrupted, the partition might contain either the source or the
100// target data, which might be of different lengths. We need to know
Doug Zongkerf291d852010-07-07 13:55:25 -0700101// the length in order to read from a partition (there is no
102// "end-of-file" marker), so the caller must specify the possible
103// lengths and the hash of the data, and we'll do the load expecting
104// to find one of those hashes.
105enum PartitionType { MTD, EMMC };
106
Tao Bao8fce75a2016-11-10 12:33:41 -0800107static int LoadPartitionContents(const std::string& filename, FileContents* file) {
108 std::vector<std::string> pieces = android::base::Split(filename, ":");
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500109 if (pieces.size() < 4 || pieces.size() % 2 != 0) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800110 printf("LoadPartitionContents called with bad filename \"%s\"\n", filename.c_str());
111 return -1;
112 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700113
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500114 enum PartitionType type;
115 if (pieces[0] == "MTD") {
116 type = MTD;
117 } else if (pieces[0] == "EMMC") {
118 type = EMMC;
119 } else if (pieces[0] == "BML") {
120 type = EMMC;
121 } else {
122 printf("LoadPartitionContents called with bad filename (%s)\n", filename.c_str());
123 return -1;
124 }
125
Tao Bao8fce75a2016-11-10 12:33:41 -0800126 size_t pair_count = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename
127 std::vector<std::pair<size_t, std::string>> pairs;
128 for (size_t i = 0; i < pair_count; ++i) {
129 size_t size;
130 if (!android::base::ParseUint(pieces[i * 2 + 2], &size) || size == 0) {
131 printf("LoadPartitionContents called with bad size \"%s\"\n", pieces[i * 2 + 2].c_str());
132 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800133 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800134 pairs.push_back({ size, pieces[i * 2 + 3] });
135 }
136
137 // Sort the pairs array so that they are in order of increasing size.
138 std::sort(pairs.begin(), pairs.end());
139
140 const char* partition = pieces[1].c_str();
Tao Bao358c2ec2016-11-28 11:48:43 -0800141 unique_file dev(ota_fopen(partition, "rb"));
Tao Bao8fce75a2016-11-10 12:33:41 -0800142 if (!dev) {
143 printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno));
144 return -1;
145 }
146
147 SHA_CTX sha_ctx;
148 SHA1_Init(&sha_ctx);
149
150 // Allocate enough memory to hold the largest size.
151 std::vector<unsigned char> buffer(pairs[pair_count - 1].first);
152 unsigned char* buffer_ptr = buffer.data();
153 size_t buffer_size = 0; // # bytes read so far
154 bool found = false;
155
156 for (const auto& pair : pairs) {
157 size_t current_size = pair.first;
158 const std::string& current_sha1 = pair.second;
159
160 // Read enough additional bytes to get us up to the next size. (Again,
161 // we're trying the possibilities in order of increasing size).
162 size_t next = current_size - buffer_size;
163 if (next > 0) {
164 size_t read = ota_fread(buffer_ptr, 1, next, dev.get());
165 if (next != read) {
166 printf("short read (%zu bytes of %zu) for partition \"%s\"\n", read, next, partition);
Tao Baoaca8e892015-07-17 11:47:44 -0700167 return -1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800168 }
169 SHA1_Update(&sha_ctx, buffer_ptr, read);
170 buffer_size += read;
171 buffer_ptr += read;
Tao Baoaca8e892015-07-17 11:47:44 -0700172 }
Doug Zongker512536a2010-02-17 16:11:44 -0800173
Ethan Yonkerb8e985c2016-08-31 13:42:11 -0500174 if (pieces[0] == "BML") {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500175 if (strcmp(partition, "boot") == 0) {
176 partition = BOARD_BML_BOOT;
177 } else if (strcmp(partition, "recovery") == 0) {
178 partition = BOARD_BML_RECOVERY;
179 }
Conn O'Griofad9201042014-03-25 01:26:49 +0000180 }
181
Tao Bao8fce75a2016-11-10 12:33:41 -0800182 // Duplicate the SHA context and finalize the duplicate so we can
183 // check it against this pair's expected hash.
184 SHA_CTX temp_ctx;
185 memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
186 uint8_t sha_so_far[SHA_DIGEST_LENGTH];
187 SHA1_Final(sha_so_far, &temp_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800188
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800189 uint8_t parsed_sha[SHA_DIGEST_LENGTH];
Tao Bao8fce75a2016-11-10 12:33:41 -0800190 if (ParseSha1(current_sha1.c_str(), parsed_sha) != 0) {
191 printf("failed to parse SHA-1 %s in %s\n", current_sha1.c_str(), filename.c_str());
192 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800193 }
194
Tao Bao8fce75a2016-11-10 12:33:41 -0800195 if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) {
196 // We have a match. Stop reading the partition; we'll return the data we've read so far.
197 printf("partition read matched size %zu SHA-1 %s\n", current_size, current_sha1.c_str());
198 found = true;
199 break;
Doug Zongkerf291d852010-07-07 13:55:25 -0700200 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800201 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700202
Tao Bao8fce75a2016-11-10 12:33:41 -0800203 if (!found) {
204 // Ran off the end of the list of (size, sha1) pairs without finding a match.
205 printf("contents of partition \"%s\" didn't match %s\n", partition, filename.c_str());
206 return -1;
207 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800208
Tao Bao8fce75a2016-11-10 12:33:41 -0800209 SHA1_Final(file->sha1, &sha_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800210
Tao Bao8fce75a2016-11-10 12:33:41 -0800211 buffer.resize(buffer_size);
212 file->data = std::move(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800213
Tao Bao8fce75a2016-11-10 12:33:41 -0800214 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800215}
216
Doug Zongker512536a2010-02-17 16:11:44 -0800217// Save the contents of the given FileContents object under the given
218// filename. Return 0 on success.
Doug Zongker1c43c972012-02-28 11:07:09 -0800219int SaveFileContents(const char* filename, const FileContents* file) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800220 unique_fd fd(ota_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
221 if (fd == -1) {
222 printf("failed to open \"%s\" for write: %s\n", filename, strerror(errno));
223 return -1;
224 }
Doug Zongker512536a2010-02-17 16:11:44 -0800225
Tao Baoc0e1c462017-02-01 10:20:10 -0800226 size_t bytes_written = FileSink(file->data.data(), file->data.size(), fd);
Tao Baof7eb7602017-03-27 15:12:48 -0700227 if (bytes_written != file->data.size()) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800228 printf("short write of \"%s\" (%zd bytes of %zu): %s\n", filename, bytes_written,
229 file->data.size(), strerror(errno));
230 return -1;
231 }
232 if (ota_fsync(fd) != 0) {
233 printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno));
234 return -1;
235 }
Tao Bao3dc14cb2016-11-22 11:37:13 -0800236 if (ota_close(fd) != 0) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800237 printf("close of \"%s\" failed: %s\n", filename, strerror(errno));
238 return -1;
239 }
Doug Zongker512536a2010-02-17 16:11:44 -0800240
Tao Bao6e02ea92016-11-17 11:24:07 -0800241 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800242}
243
Doug Zongkerf291d852010-07-07 13:55:25 -0700244// Write a memory buffer to 'target' partition, a string of the form
Elliott Hughes63a31922016-06-09 17:41:22 -0700245// "EMMC:<partition_device>[:...]". The target name
Tao Bao1ce7a2a2015-07-24 15:29:12 -0700246// might contain multiple colons, but WriteToPartition() only uses the first
247// two and ignores the rest. Return 0 on success.
Tao Bao6e02ea92016-11-17 11:24:07 -0800248int WriteToPartition(const unsigned char* data, size_t len, const std::string& target) {
Tao Baoaca8e892015-07-17 11:47:44 -0700249 std::string copy(target);
250 std::vector<std::string> pieces = android::base::Split(copy, ":");
251
Tao Bao1ce7a2a2015-07-24 15:29:12 -0700252 if (pieces.size() < 2) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500253 printf("WriteToPartition called with bad target (%s)\n", target.c_str());
Tao Baoaca8e892015-07-17 11:47:44 -0700254 return -1;
255 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700256
257 enum PartitionType type;
Tao Baoaca8e892015-07-17 11:47:44 -0700258 if (pieces[0] == "MTD") {
Doug Zongkerf291d852010-07-07 13:55:25 -0700259 type = MTD;
Tao Baoaca8e892015-07-17 11:47:44 -0700260 } else if (pieces[0] == "EMMC") {
Doug Zongkerf291d852010-07-07 13:55:25 -0700261 type = EMMC;
Ethan Yonkerb8e985c2016-08-31 13:42:11 -0500262 } else if (pieces[0] == "BML") {
Conn O'Griofad9201042014-03-25 01:26:49 +0000263 type = EMMC;
Doug Zongkerf291d852010-07-07 13:55:25 -0700264 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500265 printf("WriteToPartition called with bad target (%s)\n", target.c_str());
Doug Zongkerf291d852010-07-07 13:55:25 -0700266 return -1;
267 }
Ethan Yonker34ae4832016-08-24 15:32:18 -0500268
Tao Baoaca8e892015-07-17 11:47:44 -0700269 const char* partition = pieces[1].c_str();
Doug Zongkerf291d852010-07-07 13:55:25 -0700270
Ethan Yonkerb8e985c2016-08-31 13:42:11 -0500271 if (pieces[0] == "BML") {
Conn O'Griofad9201042014-03-25 01:26:49 +0000272 if (strcmp(partition, "boot") == 0) {
273 partition = BOARD_BML_BOOT;
274 } else if (strcmp(partition, "recovery") == 0) {
275 partition = BOARD_BML_RECOVERY;
276 }
277
278 int bmlpartition = open(partition, O_RDWR | O_LARGEFILE);
279 if (bmlpartition < 0)
280 return -1;
281 if (ioctl(bmlpartition, BML_UNLOCK_ALL, 0)) {
282 printf("failed to unlock BML partition: (%s)\n", partition);
283 return -1;
284 }
285 close(bmlpartition);
286 }
287
Doug Zongkerc4351c72010-02-22 14:46:32 -0800288 if (partition == NULL) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500289 printf("bad partition target name \"%s\"\n", target.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800290 return -1;
291 }
Doug Zongker512536a2010-02-17 16:11:44 -0800292
Doug Zongkerf291d852010-07-07 13:55:25 -0700293 switch (type) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700294 case MTD: {
Doug Zongkerf291d852010-07-07 13:55:25 -0700295 if (!mtd_partitions_scanned) {
296 mtd_scan_partitions();
Tao Baoaca8e892015-07-17 11:47:44 -0700297 mtd_partitions_scanned = true;
Doug Zongkerf291d852010-07-07 13:55:25 -0700298 }
299
300 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
301 if (mtd == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700302 printf("mtd partition \"%s\" not found for writing\n", partition);
Doug Zongkerf291d852010-07-07 13:55:25 -0700303 return -1;
304 }
305
306 MtdWriteContext* ctx = mtd_write_partition(mtd);
307 if (ctx == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700308 printf("failed to init mtd partition \"%s\" for writing\n", partition);
Doug Zongkerf291d852010-07-07 13:55:25 -0700309 return -1;
310 }
311
Yabin Cuid483c202016-02-03 17:08:52 -0800312 size_t written = mtd_write_data(ctx, reinterpret_cast<const char*>(data), len);
Doug Zongkerf291d852010-07-07 13:55:25 -0700313 if (written != len) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700314 printf("only wrote %zu of %zu bytes to MTD %s\n", written, len, partition);
Doug Zongkerf291d852010-07-07 13:55:25 -0700315 mtd_write_close(ctx);
316 return -1;
317 }
318
319 if (mtd_erase_blocks(ctx, -1) < 0) {
320 printf("error finishing mtd write of %s\n", partition);
321 mtd_write_close(ctx);
322 return -1;
323 }
324
325 if (mtd_write_close(ctx)) {
326 printf("error closing mtd write of %s\n", partition);
327 return -1;
328 }
329 break;
Tao Baoba9a42a2015-06-23 23:23:33 -0700330 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700331
Tao Baoba9a42a2015-06-23 23:23:33 -0700332 case EMMC: {
Doug Zongker044a0b42013-07-08 09:42:54 -0700333 size_t start = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700334 bool success = false;
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500335 unique_fd fd(ota_open(partition, O_RDWR | O_SYNC));
Doug Zongkerc870a992013-07-09 10:34:46 -0700336 if (fd < 0) {
Doug Zongker044a0b42013-07-08 09:42:54 -0700337 printf("failed to open %s: %s\n", partition, strerror(errno));
Doug Zongkerf291d852010-07-07 13:55:25 -0700338 return -1;
339 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700340
Tao Baoaca8e892015-07-17 11:47:44 -0700341 for (size_t attempt = 0; attempt < 2; ++attempt) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700342 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700343 printf("failed seek on %s: %s\n", partition, strerror(errno));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700344 return -1;
345 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700346 while (start < len) {
347 size_t to_write = len - start;
Doug Zongker168724c2013-12-19 15:16:57 -0800348 if (to_write > 1<<20) to_write = 1<<20;
Doug Zongker044a0b42013-07-08 09:42:54 -0700349
Jed Estepf1fc48c2015-12-15 16:04:53 -0800350 ssize_t written = TEMP_FAILURE_RETRY(ota_write(fd, data+start, to_write));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700351 if (written == -1) {
352 printf("failed write writing to %s: %s\n", partition, strerror(errno));
353 return -1;
Doug Zongker044a0b42013-07-08 09:42:54 -0700354 }
Doug Zongkerc870a992013-07-09 10:34:46 -0700355 start += written;
Doug Zongker044a0b42013-07-08 09:42:54 -0700356 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800357 if (ota_fsync(fd) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700358 printf("failed to sync to %s (%s)\n", partition, strerror(errno));
Michael Rungecddb68b2014-10-29 12:42:15 -0700359 return -1;
360 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800361 if (ota_close(fd) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700362 printf("failed to close %s (%s)\n", partition, strerror(errno));
Michael Rungecddb68b2014-10-29 12:42:15 -0700363 return -1;
364 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500365 unique_fd fd(ota_open(partition, O_RDONLY));
Michael Rungecddb68b2014-10-29 12:42:15 -0700366 if (fd < 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700367 printf("failed to reopen %s for verify (%s)\n", partition, strerror(errno));
Michael Rungecddb68b2014-10-29 12:42:15 -0700368 return -1;
369 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700370
Tao Baoba9a42a2015-06-23 23:23:33 -0700371 // Drop caches so our subsequent verification read
Doug Zongker044a0b42013-07-08 09:42:54 -0700372 // won't just be reading the cache.
373 sync();
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500374 unique_fd dc(ota_open("/proc/sys/vm/drop_caches", O_WRONLY));
Jed Estepf1fc48c2015-12-15 16:04:53 -0800375 if (TEMP_FAILURE_RETRY(ota_write(dc, "3\n", 2)) == -1) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700376 printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno));
377 } else {
378 printf(" caches dropped\n");
379 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800380 ota_close(dc);
Doug Zongker044a0b42013-07-08 09:42:54 -0700381 sleep(1);
Doug Zongker044a0b42013-07-08 09:42:54 -0700382
383 // verify
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700384 if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
385 printf("failed to seek back to beginning of %s: %s\n",
386 partition, strerror(errno));
387 return -1;
388 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700389 unsigned char buffer[4096];
390 start = len;
Tao Baoba9a42a2015-06-23 23:23:33 -0700391 for (size_t p = 0; p < len; p += sizeof(buffer)) {
Doug Zongker044a0b42013-07-08 09:42:54 -0700392 size_t to_read = len - p;
Tao Baoba9a42a2015-06-23 23:23:33 -0700393 if (to_read > sizeof(buffer)) {
394 to_read = sizeof(buffer);
395 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700396
Doug Zongkerc870a992013-07-09 10:34:46 -0700397 size_t so_far = 0;
398 while (so_far < to_read) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700399 ssize_t read_count =
Jed Estepf1fc48c2015-12-15 16:04:53 -0800400 TEMP_FAILURE_RETRY(ota_read(fd, buffer+so_far, to_read-so_far));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700401 if (read_count == -1) {
402 printf("verify read error %s at %zu: %s\n",
403 partition, p, strerror(errno));
404 return -1;
Doug Zongkerc870a992013-07-09 10:34:46 -0700405 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700406 if (static_cast<size_t>(read_count) < to_read) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700407 printf("short verify read %s at %zu: %zd %zu %s\n",
Doug Zongkerc870a992013-07-09 10:34:46 -0700408 partition, p, read_count, to_read, strerror(errno));
409 }
410 so_far += read_count;
Doug Zongker044a0b42013-07-08 09:42:54 -0700411 }
412
Tao Baoba9a42a2015-06-23 23:23:33 -0700413 if (memcmp(buffer, data+p, to_read) != 0) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700414 printf("verification failed starting at %zu\n", p);
Doug Zongker044a0b42013-07-08 09:42:54 -0700415 start = p;
416 break;
417 }
418 }
419
420 if (start == len) {
Tao Baoabba55b2015-07-17 18:11:12 -0700421 printf("verification read succeeded (attempt %zu)\n", attempt+1);
Doug Zongker044a0b42013-07-08 09:42:54 -0700422 success = true;
423 break;
424 }
425 }
426
427 if (!success) {
428 printf("failed to verify after all attempts\n");
429 return -1;
430 }
431
Jed Estepf1fc48c2015-12-15 16:04:53 -0800432 if (ota_close(fd) != 0) {
Doug Zongkerf291d852010-07-07 13:55:25 -0700433 printf("error closing %s (%s)\n", partition, strerror(errno));
434 return -1;
435 }
Doug Zongkerbf4a69a2013-07-10 13:39:50 -0700436 sync();
Doug Zongkerf291d852010-07-07 13:55:25 -0700437 break;
Doug Zongker044a0b42013-07-08 09:42:54 -0700438 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800439 }
Doug Zongker512536a2010-02-17 16:11:44 -0800440
Doug Zongkerc4351c72010-02-22 14:46:32 -0800441 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800442}
443
Doug Zongker512536a2010-02-17 16:11:44 -0800444// Take a string 'str' of 40 hex digits and parse it into the 20
445// byte array 'digest'. 'str' may contain only the digest or be of
446// the form "<digest>:<anything>". Return 0 on success, -1 on any
447// error.
448int ParseSha1(const char* str, uint8_t* digest) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800449 const char* ps = str;
450 uint8_t* pd = digest;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800451 for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800452 int digit;
453 if (*ps >= '0' && *ps <= '9') {
454 digit = *ps - '0';
455 } else if (*ps >= 'a' && *ps <= 'f') {
456 digit = *ps - 'a' + 10;
457 } else if (*ps >= 'A' && *ps <= 'F') {
458 digit = *ps - 'A' + 10;
459 } else {
460 return -1;
461 }
462 if (i % 2 == 0) {
463 *pd = digit << 4;
464 } else {
465 *pd |= digit;
466 ++pd;
467 }
Doug Zongker512536a2010-02-17 16:11:44 -0800468 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800469 if (*ps != '\0') return -1;
470 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800471}
472
Doug Zongkerc4351c72010-02-22 14:46:32 -0800473// Search an array of sha1 strings for one matching the given sha1.
474// Return the index of the match on success, or -1 if no match is
475// found.
Tao Baoc8e79342016-12-28 10:11:22 -0800476static int FindMatchingPatch(uint8_t* sha1, const std::vector<std::string>& patch_sha1_str) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800477 for (size_t i = 0; i < patch_sha1_str.size(); ++i) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800478 uint8_t patch_sha1[SHA_DIGEST_LENGTH];
Tao Bao8fce75a2016-11-10 12:33:41 -0800479 if (ParseSha1(patch_sha1_str[i].c_str(), patch_sha1) == 0 &&
480 memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
481 return i;
Doug Zongker512536a2010-02-17 16:11:44 -0800482 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800483 }
484 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800485}
486
487// Returns 0 if the contents of the file (argv[2]) or the cached file
488// match any of the sha1's on the command line (argv[3:]). Returns
489// nonzero otherwise.
Tianjie Xuaced5d92016-10-12 10:55:04 -0700490int applypatch_check(const char* filename, const std::vector<std::string>& patch_sha1_str) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800491 FileContents file;
Doug Zongker512536a2010-02-17 16:11:44 -0800492
Tao Bao8fce75a2016-11-10 12:33:41 -0800493 // It's okay to specify no sha1s; the check will pass if the
494 // LoadFileContents is successful. (Useful for reading
495 // partitions, where the filename encodes the sha1s; no need to
496 // check them twice.)
497 if (LoadFileContents(filename, &file) != 0 ||
498 (!patch_sha1_str.empty() && FindMatchingPatch(file.sha1, patch_sha1_str) < 0)) {
499 printf("file \"%s\" doesn't have any of expected sha1 sums; checking cache\n", filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800500
Tianjie Xua88cc542017-10-25 13:16:54 -0700501 // If the source file is missing or corrupted, it might be because we were killed in the middle
502 // of patching it. A copy of it should have been made in cache_temp_source. If that file
503 // exists and matches the sha1 we're looking for, the check still passes.
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800504 if (LoadFileContents(CacheLocation::location().cache_temp_source().c_str(), &file) != 0) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800505 printf("failed to load cache file\n");
506 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800507 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800508
509 if (FindMatchingPatch(file.sha1, patch_sha1_str) < 0) {
510 printf("cache bits don't match any sha1 for \"%s\"\n", filename);
511 return 1;
512 }
513 }
514 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800515}
516
517int ShowLicenses() {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800518 ShowBSDiffLicense();
519 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800520}
521
Tao Baoc0e1c462017-02-01 10:20:10 -0800522static size_t FileSink(const unsigned char* data, size_t len, int fd) {
Tao Baof7eb7602017-03-27 15:12:48 -0700523 size_t done = 0;
524 while (done < len) {
525 ssize_t wrote = TEMP_FAILURE_RETRY(ota_write(fd, data + done, len - done));
526 if (wrote == -1) {
527 printf("error writing %zd bytes: %s\n", (len - done), strerror(errno));
528 return done;
Doug Zongker512536a2010-02-17 16:11:44 -0800529 }
Tao Baof7eb7602017-03-27 15:12:48 -0700530 done += wrote;
531 }
532 return done;
Doug Zongker512536a2010-02-17 16:11:44 -0800533}
534
535// Return the amount of free space (in bytes) on the filesystem
536// containing filename. filename must exist. Return -1 on error.
537size_t FreeSpaceForFile(const char* filename) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800538 struct statfs sf;
539 if (statfs(filename, &sf) != 0) {
540 printf("failed to statfs %s: %s\n", filename, strerror(errno));
541 return -1;
542 }
caozhiyuancb9450e2015-05-19 17:21:00 +0800543 return sf.f_bsize * sf.f_bavail;
Doug Zongker512536a2010-02-17 16:11:44 -0800544}
545
Doug Zongkerc4351c72010-02-22 14:46:32 -0800546int CacheSizeCheck(size_t bytes) {
547 if (MakeFreeSpaceOnCache(bytes) < 0) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700548 printf("unable to make %zu bytes available on /cache\n", bytes);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800549 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800550 }
Tianjie Xue40c80d2018-02-03 17:20:56 -0800551 return 0;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800552}
553
Tao Bao40e144d2017-03-15 01:10:58 -0700554// This function applies binary patches to EMMC target files in a way that is safe (the original
555// file is not touched until we have the desired replacement for it) and idempotent (it's okay to
556// run this program multiple times).
Doug Zongker512536a2010-02-17 16:11:44 -0800557//
Tao Bao40e144d2017-03-15 01:10:58 -0700558// - If the SHA-1 hash of <target_filename> is <target_sha1_string>, does nothing and exits
559// successfully.
Doug Zongker512536a2010-02-17 16:11:44 -0800560//
Tao Bao40e144d2017-03-15 01:10:58 -0700561// - Otherwise, if the SHA-1 hash of <source_filename> is one of the entries in <patch_sha1_str>,
562// the corresponding patch from <patch_data> (which must be a VAL_BLOB) is applied to produce a
563// new file (the type of patch is automatically detected from the blob data). If that new file
564// has SHA-1 hash <target_sha1_str>, moves it to replace <target_filename>, and exits
565// successfully. Note that if <source_filename> and <target_filename> are not the same,
566// <source_filename> is NOT deleted on success. <target_filename> may be the string "-" to mean
567// "the same as <source_filename>".
Doug Zongker512536a2010-02-17 16:11:44 -0800568//
Tao Bao40e144d2017-03-15 01:10:58 -0700569// - Otherwise, or if any error is encountered, exits with non-zero status.
Doug Zongker512536a2010-02-17 16:11:44 -0800570//
Tao Bao40e144d2017-03-15 01:10:58 -0700571// <source_filename> must refer to an EMMC partition to read the source data. See the comments for
572// the LoadPartitionContents() function above for the format of such a filename. <target_size> has
573// become obsolete since we have dropped the support for patching non-EMMC targets (EMMC targets
574// have the size embedded in the filename).
575int applypatch(const char* source_filename, const char* target_filename,
Tianjie Xue40c80d2018-02-03 17:20:56 -0800576 const char* target_sha1_str, size_t /* target_size */,
Tianjie Xuaced5d92016-10-12 10:55:04 -0700577 const std::vector<std::string>& patch_sha1_str,
Tao Bao40e144d2017-03-15 01:10:58 -0700578 const std::vector<std::unique_ptr<Value>>& patch_data, const Value* bonus_data) {
579 printf("patch %s: ", source_filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800580
Tao Bao40e144d2017-03-15 01:10:58 -0700581 if (target_filename[0] == '-' && target_filename[1] == '\0') {
582 target_filename = source_filename;
583 }
Doug Zongker512536a2010-02-17 16:11:44 -0800584
Tao Bao40e144d2017-03-15 01:10:58 -0700585 if (strncmp(target_filename, "EMMC:", 5) != 0) {
586 printf("Supporting patching EMMC targets only.\n");
587 return 1;
588 }
589
590 uint8_t target_sha1[SHA_DIGEST_LENGTH];
591 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
592 printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
593 return 1;
594 }
595
596 // We try to load the target file into the source_file object.
597 FileContents source_file;
598 if (LoadFileContents(target_filename, &source_file) == 0) {
599 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
600 // The early-exit case: the patch was already applied, this file has the desired hash, nothing
601 // for us to do.
602 printf("already %s\n", short_sha1(target_sha1).c_str());
603 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800604 }
Tao Bao40e144d2017-03-15 01:10:58 -0700605 }
Doug Zongker512536a2010-02-17 16:11:44 -0800606
Tao Bao40e144d2017-03-15 01:10:58 -0700607 if (source_file.data.empty() ||
608 (target_filename != source_filename && strcmp(target_filename, source_filename) != 0)) {
609 // Need to load the source file: either we failed to load the target file, or we did but it's
610 // different from the expected.
611 source_file.data.clear();
612 LoadFileContents(source_filename, &source_file);
613 }
614
615 if (!source_file.data.empty()) {
616 int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str);
617 if (to_use != -1) {
618 return GenerateTarget(source_file, patch_data[to_use], target_filename, target_sha1,
619 bonus_data);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800620 }
Tao Bao40e144d2017-03-15 01:10:58 -0700621 }
Doug Zongker512536a2010-02-17 16:11:44 -0800622
Tao Bao40e144d2017-03-15 01:10:58 -0700623 printf("source file is bad; trying copy\n");
Doug Zongker512536a2010-02-17 16:11:44 -0800624
Tao Bao40e144d2017-03-15 01:10:58 -0700625 FileContents copy_file;
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800626 if (LoadFileContents(CacheLocation::location().cache_temp_source().c_str(), &copy_file) < 0) {
Tao Bao40e144d2017-03-15 01:10:58 -0700627 printf("failed to read copy file\n");
628 return 1;
629 }
Doug Zongker512536a2010-02-17 16:11:44 -0800630
Tao Bao40e144d2017-03-15 01:10:58 -0700631 int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str);
632 if (to_use == -1) {
633 printf("copy file doesn't match source SHA-1s either\n");
634 return 1;
635 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800636
Tao Bao40e144d2017-03-15 01:10:58 -0700637 return GenerateTarget(copy_file, patch_data[to_use], target_filename, target_sha1, bonus_data);
Doug Zongker1c43c972012-02-28 11:07:09 -0800638}
Doug Zongker1c43c972012-02-28 11:07:09 -0800639
Tao Baoabba55b2015-07-17 18:11:12 -0700640/*
641 * This function flashes a given image to the target partition. It verifies
642 * the target cheksum first, and will return if target has the desired hash.
643 * It checks the checksum of the given source image before flashing, and
644 * verifies the target partition afterwards. The function is idempotent.
645 * Returns zero on success.
646 */
647int applypatch_flash(const char* source_filename, const char* target_filename,
648 const char* target_sha1_str, size_t target_size) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800649 printf("flash %s: ", target_filename);
Tao Baoabba55b2015-07-17 18:11:12 -0700650
Tao Bao6e02ea92016-11-17 11:24:07 -0800651 uint8_t target_sha1[SHA_DIGEST_LENGTH];
652 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
653 printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
654 return 1;
655 }
Tao Baoabba55b2015-07-17 18:11:12 -0700656
Tao Bao6e02ea92016-11-17 11:24:07 -0800657 std::string target_str(target_filename);
658 std::vector<std::string> pieces = android::base::Split(target_str, ":");
659 if (pieces.size() != 2 || pieces[0] != "EMMC") {
660 printf("invalid target name \"%s\"", target_filename);
661 return 1;
662 }
Tao Baoabba55b2015-07-17 18:11:12 -0700663
Tao Bao6e02ea92016-11-17 11:24:07 -0800664 // Load the target into the source_file object to see if already applied.
665 pieces.push_back(std::to_string(target_size));
666 pieces.push_back(target_sha1_str);
667 std::string fullname = android::base::Join(pieces, ':');
668 FileContents source_file;
669 if (LoadPartitionContents(fullname, &source_file) == 0 &&
670 memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
671 // The early-exit case: the image was already applied, this partition
672 // has the desired hash, nothing for us to do.
673 printf("already %s\n", short_sha1(target_sha1).c_str());
Tao Baoabba55b2015-07-17 18:11:12 -0700674 return 0;
Tao Bao6e02ea92016-11-17 11:24:07 -0800675 }
676
677 if (LoadFileContents(source_filename, &source_file) == 0) {
678 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
679 // The source doesn't have desired checksum.
680 printf("source \"%s\" doesn't have expected sha1 sum\n", source_filename);
681 printf("expected: %s, found: %s\n", short_sha1(target_sha1).c_str(),
682 short_sha1(source_file.sha1).c_str());
683 return 1;
684 }
685 }
686
687 if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
688 printf("write of copied data to %s failed\n", target_filename);
689 return 1;
690 }
691 return 0;
Doug Zongker1c43c972012-02-28 11:07:09 -0800692}
693
Tao Bao40e144d2017-03-15 01:10:58 -0700694static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
695 const std::string& target_filename,
696 const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800697 if (patch->type != VAL_BLOB) {
698 printf("patch is not a blob\n");
699 return 1;
700 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800701
Tao Bao6e02ea92016-11-17 11:24:07 -0800702 const char* header = &patch->data[0];
703 size_t header_bytes_read = patch->data.size();
704 bool use_bsdiff = false;
705 if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
706 use_bsdiff = true;
707 } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
708 use_bsdiff = false;
709 } else {
710 printf("Unknown patch file format\n");
711 return 1;
712 }
Yabin Cuid483c202016-02-03 17:08:52 -0800713
Tao Bao40e144d2017-03-15 01:10:58 -0700714 CHECK(android::base::StartsWith(target_filename, "EMMC:"));
Yabin Cuid483c202016-02-03 17:08:52 -0800715
Tao Bao40e144d2017-03-15 01:10:58 -0700716 // We still write the original source to cache, in case the partition write is interrupted.
717 if (MakeFreeSpaceOnCache(source_file.data.size()) < 0) {
718 printf("not enough free space on /cache\n");
719 return 1;
720 }
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800721 if (SaveFileContents(CacheLocation::location().cache_temp_source().c_str(), &source_file) < 0) {
Tao Bao40e144d2017-03-15 01:10:58 -0700722 printf("failed to back up source file\n");
723 return 1;
724 }
Doug Zongker512536a2010-02-17 16:11:44 -0800725
Tao Bao40e144d2017-03-15 01:10:58 -0700726 // We store the decoded output in memory.
Tao Bao6e02ea92016-11-17 11:24:07 -0800727 std::string memory_sink_str; // Don't need to reserve space.
Tao Baoc0e1c462017-02-01 10:20:10 -0800728 SinkFn sink = [&memory_sink_str](const unsigned char* data, size_t len) {
729 memory_sink_str.append(reinterpret_cast<const char*>(data), len);
730 return len;
731 };
Doug Zongkerc4351c72010-02-22 14:46:32 -0800732
Tao Bao40e144d2017-03-15 01:10:58 -0700733 SHA_CTX ctx;
734 SHA1_Init(&ctx);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800735
Tao Bao40e144d2017-03-15 01:10:58 -0700736 int result;
737 if (use_bsdiff) {
Tao Bao1e0941f2017-11-10 11:49:53 -0800738 result =
739 ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), *patch, 0, sink, &ctx);
Tao Bao40e144d2017-03-15 01:10:58 -0700740 } else {
Tao Bao1e0941f2017-11-10 11:49:53 -0800741 result = ApplyImagePatch(source_file.data.data(), source_file.data.size(), *patch, sink, &ctx,
742 bonus_data);
Tao Bao40e144d2017-03-15 01:10:58 -0700743 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800744
Tao Bao40e144d2017-03-15 01:10:58 -0700745 if (result != 0) {
746 printf("applying patch failed\n");
747 return 1;
748 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800749
Tao Bao6e02ea92016-11-17 11:24:07 -0800750 uint8_t current_target_sha1[SHA_DIGEST_LENGTH];
751 SHA1_Final(current_target_sha1, &ctx);
752 if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
753 printf("patch did not produce expected sha1\n");
754 return 1;
755 } else {
756 printf("now %s\n", short_sha1(target_sha1).c_str());
757 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800758
Tao Bao40e144d2017-03-15 01:10:58 -0700759 // Write back the temp file to the partition.
760 if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
761 memory_sink_str.size(), target_filename) != 0) {
762 printf("write of patched data to %s failed\n", target_filename.c_str());
763 return 1;
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.
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800767 unlink(CacheLocation::location().cache_temp_source().c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800768
Tao Bao6e02ea92016-11-17 11:24:07 -0800769 // Success!
770 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800771}