blob: 43e9b80dcae7730c45882f24b1e3b521d6a127f2 [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"
Jed Estepff6df892015-12-15 16:04:53 -080045#include "ota_io.h"
Tao Baoe6aa3322015-08-05 15:20:27 -070046#include "print_sha1.h"
Doug Zongker512536a2010-02-17 16:11:44 -080047
Tao Bao8fce75a2016-11-10 12:33:41 -080048static int LoadPartitionContents(const std::string& filename, FileContents* file);
Tao Baoc0e1c462017-02-01 10:20:10 -080049static size_t FileSink(const unsigned char* data, size_t len, int fd);
Tao Bao40e144d2017-03-15 01:10:58 -070050static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
51 const std::string& target_filename,
52 const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data);
Doug Zongker512536a2010-02-17 16:11:44 -080053
Tao Baoaca8e892015-07-17 11:47:44 -070054static bool mtd_partitions_scanned = false;
Doug Zongker512536a2010-02-17 16:11:44 -080055
Tao Bao8fce75a2016-11-10 12:33:41 -080056// Read a file into memory; store the file contents and associated metadata in *file.
Hristo Bojinovdb314d62010-08-02 10:29:49 -070057// Return 0 on success.
Doug Zongkera1bc1482014-02-13 15:18:19 -080058int LoadFileContents(const char* filename, FileContents* file) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -050059 // A special 'filename' beginning with "MTD:" or "EMMC:" means to
60 // load the contents of a partition.
61 if (strncmp(filename, "MTD:", 4) == 0 ||
62 strncmp(filename, "EMMC:", 5) == 0 ||
63 strncmp(filename, "BML:", 4) == 0) {
Tao Bao8fce75a2016-11-10 12:33:41 -080064 return LoadPartitionContents(filename, file);
65 }
Doug Zongker512536a2010-02-17 16:11:44 -080066
Tao Bao8fce75a2016-11-10 12:33:41 -080067 if (stat(filename, &file->st) == -1) {
68 printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
69 return -1;
70 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080071
Tao Bao8fce75a2016-11-10 12:33:41 -080072 std::vector<unsigned char> data(file->st.st_size);
Tao Bao358c2ec2016-11-28 11:48:43 -080073 unique_file f(ota_fopen(filename, "rb"));
Tao Bao8fce75a2016-11-10 12:33:41 -080074 if (!f) {
75 printf("failed to open \"%s\": %s\n", filename, strerror(errno));
76 return -1;
77 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080078
Tao Bao8fce75a2016-11-10 12:33:41 -080079 size_t bytes_read = ota_fread(data.data(), 1, data.size(), f.get());
80 if (bytes_read != data.size()) {
81 printf("short read of \"%s\" (%zu bytes of %zu)\n", filename, bytes_read, data.size());
82 return -1;
83 }
84 file->data = std::move(data);
85 SHA1(file->data.data(), file->data.size(), file->sha1);
86 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -080087}
88
Elliott Hughes63a31922016-06-09 17:41:22 -070089// Load the contents of an EMMC partition into the provided
Doug Zongker512536a2010-02-17 16:11:44 -080090// FileContents. filename should be a string of the form
Elliott Hughes63a31922016-06-09 17:41:22 -070091// "EMMC:<partition_device>:...". The smallest size_n bytes for
Doug Zongkerf291d852010-07-07 13:55:25 -070092// which that prefix of the partition contents has the corresponding
93// sha1 hash will be loaded. It is acceptable for a size value to be
94// repeated with different sha1s. Will return 0 on success.
Doug Zongker512536a2010-02-17 16:11:44 -080095//
96// This complexity is needed because if an OTA installation is
97// interrupted, the partition might contain either the source or the
98// target data, which might be of different lengths. We need to know
Doug Zongkerf291d852010-07-07 13:55:25 -070099// the length in order to read from a partition (there is no
100// "end-of-file" marker), so the caller must specify the possible
101// lengths and the hash of the data, and we'll do the load expecting
102// to find one of those hashes.
103enum PartitionType { MTD, EMMC };
104
Tao Bao8fce75a2016-11-10 12:33:41 -0800105static int LoadPartitionContents(const std::string& filename, FileContents* file) {
106 std::vector<std::string> pieces = android::base::Split(filename, ":");
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500107 if (pieces.size() < 4 || pieces.size() % 2 != 0) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800108 printf("LoadPartitionContents called with bad filename \"%s\"\n", filename.c_str());
109 return -1;
110 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700111
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500112 enum PartitionType type;
113 if (pieces[0] == "MTD") {
114 type = MTD;
115 } else if (pieces[0] == "EMMC") {
116 type = EMMC;
117 } else if (pieces[0] == "BML") {
118 type = EMMC;
119 } else {
120 printf("LoadPartitionContents called with bad filename (%s)\n", filename.c_str());
121 return -1;
122 }
123
Tao Bao8fce75a2016-11-10 12:33:41 -0800124 size_t pair_count = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename
125 std::vector<std::pair<size_t, std::string>> pairs;
126 for (size_t i = 0; i < pair_count; ++i) {
127 size_t size;
128 if (!android::base::ParseUint(pieces[i * 2 + 2], &size) || size == 0) {
129 printf("LoadPartitionContents called with bad size \"%s\"\n", pieces[i * 2 + 2].c_str());
130 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800131 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800132 pairs.push_back({ size, pieces[i * 2 + 3] });
133 }
134
135 // Sort the pairs array so that they are in order of increasing size.
136 std::sort(pairs.begin(), pairs.end());
137
138 const char* partition = pieces[1].c_str();
Tao Bao358c2ec2016-11-28 11:48:43 -0800139 unique_file dev(ota_fopen(partition, "rb"));
Tao Bao8fce75a2016-11-10 12:33:41 -0800140 if (!dev) {
141 printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno));
142 return -1;
143 }
144
145 SHA_CTX sha_ctx;
146 SHA1_Init(&sha_ctx);
147
148 // Allocate enough memory to hold the largest size.
149 std::vector<unsigned char> buffer(pairs[pair_count - 1].first);
150 unsigned char* buffer_ptr = buffer.data();
151 size_t buffer_size = 0; // # bytes read so far
152 bool found = false;
153
154 for (const auto& pair : pairs) {
155 size_t current_size = pair.first;
156 const std::string& current_sha1 = pair.second;
157
158 // Read enough additional bytes to get us up to the next size. (Again,
159 // we're trying the possibilities in order of increasing size).
160 size_t next = current_size - buffer_size;
161 if (next > 0) {
162 size_t read = ota_fread(buffer_ptr, 1, next, dev.get());
163 if (next != read) {
164 printf("short read (%zu bytes of %zu) for partition \"%s\"\n", read, next, partition);
Tao Baoaca8e892015-07-17 11:47:44 -0700165 return -1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800166 }
167 SHA1_Update(&sha_ctx, buffer_ptr, read);
168 buffer_size += read;
169 buffer_ptr += read;
Tao Baoaca8e892015-07-17 11:47:44 -0700170 }
Doug Zongker512536a2010-02-17 16:11:44 -0800171
Ethan Yonkerb8e985c2016-08-31 13:42:11 -0500172 if (pieces[0] == "BML") {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500173 if (strcmp(partition, "boot") == 0) {
174 partition = BOARD_BML_BOOT;
175 } else if (strcmp(partition, "recovery") == 0) {
176 partition = BOARD_BML_RECOVERY;
177 }
Conn O'Griofad9201042014-03-25 01:26:49 +0000178 }
179
Tao Bao8fce75a2016-11-10 12:33:41 -0800180 // Duplicate the SHA context and finalize the duplicate so we can
181 // check it against this pair's expected hash.
182 SHA_CTX temp_ctx;
183 memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
184 uint8_t sha_so_far[SHA_DIGEST_LENGTH];
185 SHA1_Final(sha_so_far, &temp_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800186
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800187 uint8_t parsed_sha[SHA_DIGEST_LENGTH];
Tao Bao8fce75a2016-11-10 12:33:41 -0800188 if (ParseSha1(current_sha1.c_str(), parsed_sha) != 0) {
189 printf("failed to parse SHA-1 %s in %s\n", current_sha1.c_str(), filename.c_str());
190 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800191 }
192
Tao Bao8fce75a2016-11-10 12:33:41 -0800193 if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) {
194 // We have a match. Stop reading the partition; we'll return the data we've read so far.
195 printf("partition read matched size %zu SHA-1 %s\n", current_size, current_sha1.c_str());
196 found = true;
197 break;
Doug Zongkerf291d852010-07-07 13:55:25 -0700198 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800199 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700200
Tao Bao8fce75a2016-11-10 12:33:41 -0800201 if (!found) {
202 // Ran off the end of the list of (size, sha1) pairs without finding a match.
203 printf("contents of partition \"%s\" didn't match %s\n", partition, filename.c_str());
204 return -1;
205 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800206
Tao Bao8fce75a2016-11-10 12:33:41 -0800207 SHA1_Final(file->sha1, &sha_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800208
Tao Bao8fce75a2016-11-10 12:33:41 -0800209 buffer.resize(buffer_size);
210 file->data = std::move(buffer);
211 // Fake some stat() info.
212 file->st.st_mode = 0644;
213 file->st.st_uid = 0;
214 file->st.st_gid = 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800215
Tao Bao8fce75a2016-11-10 12:33:41 -0800216 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800217}
218
Doug Zongker512536a2010-02-17 16:11:44 -0800219// Save the contents of the given FileContents object under the given
220// filename. Return 0 on success.
Doug Zongker1c43c972012-02-28 11:07:09 -0800221int SaveFileContents(const char* filename, const FileContents* file) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800222 unique_fd fd(ota_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
223 if (fd == -1) {
224 printf("failed to open \"%s\" for write: %s\n", filename, strerror(errno));
225 return -1;
226 }
Doug Zongker512536a2010-02-17 16:11:44 -0800227
Tao Baoc0e1c462017-02-01 10:20:10 -0800228 size_t bytes_written = FileSink(file->data.data(), file->data.size(), fd);
Tao Baof7eb7602017-03-27 15:12:48 -0700229 if (bytes_written != file->data.size()) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800230 printf("short write of \"%s\" (%zd bytes of %zu): %s\n", filename, bytes_written,
231 file->data.size(), strerror(errno));
232 return -1;
233 }
234 if (ota_fsync(fd) != 0) {
235 printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno));
236 return -1;
237 }
Tao Bao3dc14cb2016-11-22 11:37:13 -0800238 if (ota_close(fd) != 0) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800239 printf("close of \"%s\" failed: %s\n", filename, strerror(errno));
240 return -1;
241 }
Doug Zongker512536a2010-02-17 16:11:44 -0800242
Tao Bao6e02ea92016-11-17 11:24:07 -0800243 if (chmod(filename, file->st.st_mode) != 0) {
244 printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno));
245 return -1;
246 }
247 if (chown(filename, file->st.st_uid, file->st.st_gid) != 0) {
248 printf("chown of \"%s\" failed: %s\n", filename, strerror(errno));
249 return -1;
250 }
Doug Zongker512536a2010-02-17 16:11:44 -0800251
Tao Bao6e02ea92016-11-17 11:24:07 -0800252 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800253}
254
Doug Zongkerf291d852010-07-07 13:55:25 -0700255// Write a memory buffer to 'target' partition, a string of the form
Elliott Hughes63a31922016-06-09 17:41:22 -0700256// "EMMC:<partition_device>[:...]". The target name
Tao Bao1ce7a2a2015-07-24 15:29:12 -0700257// might contain multiple colons, but WriteToPartition() only uses the first
258// two and ignores the rest. Return 0 on success.
Tao Bao6e02ea92016-11-17 11:24:07 -0800259int WriteToPartition(const unsigned char* data, size_t len, const std::string& target) {
Tao Baoaca8e892015-07-17 11:47:44 -0700260 std::string copy(target);
261 std::vector<std::string> pieces = android::base::Split(copy, ":");
262
Tao Bao1ce7a2a2015-07-24 15:29:12 -0700263 if (pieces.size() < 2) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500264 printf("WriteToPartition called with bad target (%s)\n", target.c_str());
Tao Baoaca8e892015-07-17 11:47:44 -0700265 return -1;
266 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700267
268 enum PartitionType type;
Tao Baoaca8e892015-07-17 11:47:44 -0700269 if (pieces[0] == "MTD") {
Doug Zongkerf291d852010-07-07 13:55:25 -0700270 type = MTD;
Tao Baoaca8e892015-07-17 11:47:44 -0700271 } else if (pieces[0] == "EMMC") {
Doug Zongkerf291d852010-07-07 13:55:25 -0700272 type = EMMC;
Ethan Yonkerb8e985c2016-08-31 13:42:11 -0500273 } else if (pieces[0] == "BML") {
Conn O'Griofad9201042014-03-25 01:26:49 +0000274 type = EMMC;
Doug Zongkerf291d852010-07-07 13:55:25 -0700275 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500276 printf("WriteToPartition called with bad target (%s)\n", target.c_str());
Doug Zongkerf291d852010-07-07 13:55:25 -0700277 return -1;
278 }
Ethan Yonker34ae4832016-08-24 15:32:18 -0500279
Tao Baoaca8e892015-07-17 11:47:44 -0700280 const char* partition = pieces[1].c_str();
Doug Zongkerf291d852010-07-07 13:55:25 -0700281
Ethan Yonkerb8e985c2016-08-31 13:42:11 -0500282 if (pieces[0] == "BML") {
Conn O'Griofad9201042014-03-25 01:26:49 +0000283 if (strcmp(partition, "boot") == 0) {
284 partition = BOARD_BML_BOOT;
285 } else if (strcmp(partition, "recovery") == 0) {
286 partition = BOARD_BML_RECOVERY;
287 }
288
289 int bmlpartition = open(partition, O_RDWR | O_LARGEFILE);
290 if (bmlpartition < 0)
291 return -1;
292 if (ioctl(bmlpartition, BML_UNLOCK_ALL, 0)) {
293 printf("failed to unlock BML partition: (%s)\n", partition);
294 return -1;
295 }
296 close(bmlpartition);
297 }
298
Doug Zongkerc4351c72010-02-22 14:46:32 -0800299 if (partition == NULL) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500300 printf("bad partition target name \"%s\"\n", target.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800301 return -1;
302 }
Doug Zongker512536a2010-02-17 16:11:44 -0800303
Doug Zongkerf291d852010-07-07 13:55:25 -0700304 switch (type) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700305 case MTD: {
Doug Zongkerf291d852010-07-07 13:55:25 -0700306 if (!mtd_partitions_scanned) {
307 mtd_scan_partitions();
Tao Baoaca8e892015-07-17 11:47:44 -0700308 mtd_partitions_scanned = true;
Doug Zongkerf291d852010-07-07 13:55:25 -0700309 }
310
311 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
312 if (mtd == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700313 printf("mtd partition \"%s\" not found for writing\n", partition);
Doug Zongkerf291d852010-07-07 13:55:25 -0700314 return -1;
315 }
316
317 MtdWriteContext* ctx = mtd_write_partition(mtd);
318 if (ctx == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700319 printf("failed to init mtd partition \"%s\" for writing\n", partition);
Doug Zongkerf291d852010-07-07 13:55:25 -0700320 return -1;
321 }
322
Yabin Cuid483c202016-02-03 17:08:52 -0800323 size_t written = mtd_write_data(ctx, reinterpret_cast<const char*>(data), len);
Doug Zongkerf291d852010-07-07 13:55:25 -0700324 if (written != len) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700325 printf("only wrote %zu of %zu bytes to MTD %s\n", written, len, partition);
Doug Zongkerf291d852010-07-07 13:55:25 -0700326 mtd_write_close(ctx);
327 return -1;
328 }
329
330 if (mtd_erase_blocks(ctx, -1) < 0) {
331 printf("error finishing mtd write of %s\n", partition);
332 mtd_write_close(ctx);
333 return -1;
334 }
335
336 if (mtd_write_close(ctx)) {
337 printf("error closing mtd write of %s\n", partition);
338 return -1;
339 }
340 break;
Tao Baoba9a42a2015-06-23 23:23:33 -0700341 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700342
Tao Baoba9a42a2015-06-23 23:23:33 -0700343 case EMMC: {
Doug Zongker044a0b42013-07-08 09:42:54 -0700344 size_t start = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700345 bool success = false;
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500346 unique_fd fd(ota_open(partition, O_RDWR | O_SYNC));
Doug Zongkerc870a992013-07-09 10:34:46 -0700347 if (fd < 0) {
Doug Zongker044a0b42013-07-08 09:42:54 -0700348 printf("failed to open %s: %s\n", partition, strerror(errno));
Doug Zongkerf291d852010-07-07 13:55:25 -0700349 return -1;
350 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700351
Tao Baoaca8e892015-07-17 11:47:44 -0700352 for (size_t attempt = 0; attempt < 2; ++attempt) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700353 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700354 printf("failed seek on %s: %s\n", partition, strerror(errno));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700355 return -1;
356 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700357 while (start < len) {
358 size_t to_write = len - start;
Doug Zongker168724c2013-12-19 15:16:57 -0800359 if (to_write > 1<<20) to_write = 1<<20;
Doug Zongker044a0b42013-07-08 09:42:54 -0700360
Jed Estepf1fc48c2015-12-15 16:04:53 -0800361 ssize_t written = TEMP_FAILURE_RETRY(ota_write(fd, data+start, to_write));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700362 if (written == -1) {
363 printf("failed write writing to %s: %s\n", partition, strerror(errno));
364 return -1;
Doug Zongker044a0b42013-07-08 09:42:54 -0700365 }
Doug Zongkerc870a992013-07-09 10:34:46 -0700366 start += written;
Doug Zongker044a0b42013-07-08 09:42:54 -0700367 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800368 if (ota_fsync(fd) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700369 printf("failed to sync to %s (%s)\n", partition, strerror(errno));
Michael Rungecddb68b2014-10-29 12:42:15 -0700370 return -1;
371 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800372 if (ota_close(fd) != 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700373 printf("failed to close %s (%s)\n", partition, strerror(errno));
Michael Rungecddb68b2014-10-29 12:42:15 -0700374 return -1;
375 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500376 unique_fd fd(ota_open(partition, O_RDONLY));
Michael Rungecddb68b2014-10-29 12:42:15 -0700377 if (fd < 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700378 printf("failed to reopen %s for verify (%s)\n", partition, strerror(errno));
Michael Rungecddb68b2014-10-29 12:42:15 -0700379 return -1;
380 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700381
Tao Baoba9a42a2015-06-23 23:23:33 -0700382 // Drop caches so our subsequent verification read
Doug Zongker044a0b42013-07-08 09:42:54 -0700383 // won't just be reading the cache.
384 sync();
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500385 unique_fd dc(ota_open("/proc/sys/vm/drop_caches", O_WRONLY));
Jed Estepf1fc48c2015-12-15 16:04:53 -0800386 if (TEMP_FAILURE_RETRY(ota_write(dc, "3\n", 2)) == -1) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700387 printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno));
388 } else {
389 printf(" caches dropped\n");
390 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800391 ota_close(dc);
Doug Zongker044a0b42013-07-08 09:42:54 -0700392 sleep(1);
Doug Zongker044a0b42013-07-08 09:42:54 -0700393
394 // verify
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700395 if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
396 printf("failed to seek back to beginning of %s: %s\n",
397 partition, strerror(errno));
398 return -1;
399 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700400 unsigned char buffer[4096];
401 start = len;
Tao Baoba9a42a2015-06-23 23:23:33 -0700402 for (size_t p = 0; p < len; p += sizeof(buffer)) {
Doug Zongker044a0b42013-07-08 09:42:54 -0700403 size_t to_read = len - p;
Tao Baoba9a42a2015-06-23 23:23:33 -0700404 if (to_read > sizeof(buffer)) {
405 to_read = sizeof(buffer);
406 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700407
Doug Zongkerc870a992013-07-09 10:34:46 -0700408 size_t so_far = 0;
409 while (so_far < to_read) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700410 ssize_t read_count =
Jed Estepf1fc48c2015-12-15 16:04:53 -0800411 TEMP_FAILURE_RETRY(ota_read(fd, buffer+so_far, to_read-so_far));
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700412 if (read_count == -1) {
413 printf("verify read error %s at %zu: %s\n",
414 partition, p, strerror(errno));
415 return -1;
Doug Zongkerc870a992013-07-09 10:34:46 -0700416 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700417 if (static_cast<size_t>(read_count) < to_read) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700418 printf("short verify read %s at %zu: %zd %zu %s\n",
Doug Zongkerc870a992013-07-09 10:34:46 -0700419 partition, p, read_count, to_read, strerror(errno));
420 }
421 so_far += read_count;
Doug Zongker044a0b42013-07-08 09:42:54 -0700422 }
423
Tao Baoba9a42a2015-06-23 23:23:33 -0700424 if (memcmp(buffer, data+p, to_read) != 0) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700425 printf("verification failed starting at %zu\n", p);
Doug Zongker044a0b42013-07-08 09:42:54 -0700426 start = p;
427 break;
428 }
429 }
430
431 if (start == len) {
Tao Baoabba55b2015-07-17 18:11:12 -0700432 printf("verification read succeeded (attempt %zu)\n", attempt+1);
Doug Zongker044a0b42013-07-08 09:42:54 -0700433 success = true;
434 break;
435 }
436 }
437
438 if (!success) {
439 printf("failed to verify after all attempts\n");
440 return -1;
441 }
442
Jed Estepf1fc48c2015-12-15 16:04:53 -0800443 if (ota_close(fd) != 0) {
Doug Zongkerf291d852010-07-07 13:55:25 -0700444 printf("error closing %s (%s)\n", partition, strerror(errno));
445 return -1;
446 }
Doug Zongkerbf4a69a2013-07-10 13:39:50 -0700447 sync();
Doug Zongkerf291d852010-07-07 13:55:25 -0700448 break;
Doug Zongker044a0b42013-07-08 09:42:54 -0700449 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800450 }
Doug Zongker512536a2010-02-17 16:11:44 -0800451
Doug Zongkerc4351c72010-02-22 14:46:32 -0800452 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800453}
454
Doug Zongker512536a2010-02-17 16:11:44 -0800455// Take a string 'str' of 40 hex digits and parse it into the 20
456// byte array 'digest'. 'str' may contain only the digest or be of
457// the form "<digest>:<anything>". Return 0 on success, -1 on any
458// error.
459int ParseSha1(const char* str, uint8_t* digest) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800460 const char* ps = str;
461 uint8_t* pd = digest;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800462 for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800463 int digit;
464 if (*ps >= '0' && *ps <= '9') {
465 digit = *ps - '0';
466 } else if (*ps >= 'a' && *ps <= 'f') {
467 digit = *ps - 'a' + 10;
468 } else if (*ps >= 'A' && *ps <= 'F') {
469 digit = *ps - 'A' + 10;
470 } else {
471 return -1;
472 }
473 if (i % 2 == 0) {
474 *pd = digit << 4;
475 } else {
476 *pd |= digit;
477 ++pd;
478 }
Doug Zongker512536a2010-02-17 16:11:44 -0800479 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800480 if (*ps != '\0') return -1;
481 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800482}
483
Doug Zongkerc4351c72010-02-22 14:46:32 -0800484// Search an array of sha1 strings for one matching the given sha1.
485// Return the index of the match on success, or -1 if no match is
486// found.
Tao Baoc8e79342016-12-28 10:11:22 -0800487static int FindMatchingPatch(uint8_t* sha1, const std::vector<std::string>& patch_sha1_str) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800488 for (size_t i = 0; i < patch_sha1_str.size(); ++i) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800489 uint8_t patch_sha1[SHA_DIGEST_LENGTH];
Tao Bao8fce75a2016-11-10 12:33:41 -0800490 if (ParseSha1(patch_sha1_str[i].c_str(), patch_sha1) == 0 &&
491 memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
492 return i;
Doug Zongker512536a2010-02-17 16:11:44 -0800493 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800494 }
495 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800496}
497
498// Returns 0 if the contents of the file (argv[2]) or the cached file
499// match any of the sha1's on the command line (argv[3:]). Returns
500// nonzero otherwise.
Tianjie Xuaced5d92016-10-12 10:55:04 -0700501int applypatch_check(const char* filename, const std::vector<std::string>& patch_sha1_str) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800502 FileContents file;
Doug Zongker512536a2010-02-17 16:11:44 -0800503
Tao Bao8fce75a2016-11-10 12:33:41 -0800504 // It's okay to specify no sha1s; the check will pass if the
505 // LoadFileContents is successful. (Useful for reading
506 // partitions, where the filename encodes the sha1s; no need to
507 // check them twice.)
508 if (LoadFileContents(filename, &file) != 0 ||
509 (!patch_sha1_str.empty() && FindMatchingPatch(file.sha1, patch_sha1_str) < 0)) {
510 printf("file \"%s\" doesn't have any of expected sha1 sums; checking cache\n", filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800511
Tao Bao8fce75a2016-11-10 12:33:41 -0800512 // If the source file is missing or corrupted, it might be because
513 // we were killed in the middle of patching it. A copy of it
514 // should have been made in CACHE_TEMP_SOURCE. If that file
515 // exists and matches the sha1 we're looking for, the check still
516 // passes.
517 if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
518 printf("failed to load cache file\n");
519 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800520 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800521
522 if (FindMatchingPatch(file.sha1, patch_sha1_str) < 0) {
523 printf("cache bits don't match any sha1 for \"%s\"\n", filename);
524 return 1;
525 }
526 }
527 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800528}
529
530int ShowLicenses() {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800531 ShowBSDiffLicense();
532 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800533}
534
Tao Baoc0e1c462017-02-01 10:20:10 -0800535static size_t FileSink(const unsigned char* data, size_t len, int fd) {
Tao Baof7eb7602017-03-27 15:12:48 -0700536 size_t done = 0;
537 while (done < len) {
538 ssize_t wrote = TEMP_FAILURE_RETRY(ota_write(fd, data + done, len - done));
539 if (wrote == -1) {
540 printf("error writing %zd bytes: %s\n", (len - done), strerror(errno));
541 return done;
Doug Zongker512536a2010-02-17 16:11:44 -0800542 }
Tao Baof7eb7602017-03-27 15:12:48 -0700543 done += wrote;
544 }
545 return done;
Doug Zongker512536a2010-02-17 16:11:44 -0800546}
547
548// Return the amount of free space (in bytes) on the filesystem
549// containing filename. filename must exist. Return -1 on error.
550size_t FreeSpaceForFile(const char* filename) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800551 struct statfs sf;
552 if (statfs(filename, &sf) != 0) {
553 printf("failed to statfs %s: %s\n", filename, strerror(errno));
554 return -1;
555 }
caozhiyuancb9450e2015-05-19 17:21:00 +0800556 return sf.f_bsize * sf.f_bavail;
Doug Zongker512536a2010-02-17 16:11:44 -0800557}
558
Doug Zongkerc4351c72010-02-22 14:46:32 -0800559int CacheSizeCheck(size_t bytes) {
560 if (MakeFreeSpaceOnCache(bytes) < 0) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700561 printf("unable to make %zu bytes available on /cache\n", bytes);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800562 return 1;
563 } else {
564 return 0;
565 }
566}
567
Tao Bao40e144d2017-03-15 01:10:58 -0700568// This function applies binary patches to EMMC target files in a way that is safe (the original
569// file is not touched until we have the desired replacement for it) and idempotent (it's okay to
570// run this program multiple times).
Doug Zongker512536a2010-02-17 16:11:44 -0800571//
Tao Bao40e144d2017-03-15 01:10:58 -0700572// - If the SHA-1 hash of <target_filename> is <target_sha1_string>, does nothing and exits
573// successfully.
Doug Zongker512536a2010-02-17 16:11:44 -0800574//
Tao Bao40e144d2017-03-15 01:10:58 -0700575// - Otherwise, if the SHA-1 hash of <source_filename> is one of the entries in <patch_sha1_str>,
576// the corresponding patch from <patch_data> (which must be a VAL_BLOB) is applied to produce a
577// new file (the type of patch is automatically detected from the blob data). If that new file
578// has SHA-1 hash <target_sha1_str>, moves it to replace <target_filename>, and exits
579// successfully. Note that if <source_filename> and <target_filename> are not the same,
580// <source_filename> is NOT deleted on success. <target_filename> may be the string "-" to mean
581// "the same as <source_filename>".
Doug Zongker512536a2010-02-17 16:11:44 -0800582//
Tao Bao40e144d2017-03-15 01:10:58 -0700583// - Otherwise, or if any error is encountered, exits with non-zero status.
Doug Zongker512536a2010-02-17 16:11:44 -0800584//
Tao Bao40e144d2017-03-15 01:10:58 -0700585// <source_filename> must refer to an EMMC partition to read the source data. See the comments for
586// the LoadPartitionContents() function above for the format of such a filename. <target_size> has
587// become obsolete since we have dropped the support for patching non-EMMC targets (EMMC targets
588// have the size embedded in the filename).
589int applypatch(const char* source_filename, const char* target_filename,
590 const char* target_sha1_str, size_t target_size __unused,
Tianjie Xuaced5d92016-10-12 10:55:04 -0700591 const std::vector<std::string>& patch_sha1_str,
Tao Bao40e144d2017-03-15 01:10:58 -0700592 const std::vector<std::unique_ptr<Value>>& patch_data, const Value* bonus_data) {
593 printf("patch %s: ", source_filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800594
Tao Bao40e144d2017-03-15 01:10:58 -0700595 if (target_filename[0] == '-' && target_filename[1] == '\0') {
596 target_filename = source_filename;
597 }
Doug Zongker512536a2010-02-17 16:11:44 -0800598
Tao Bao40e144d2017-03-15 01:10:58 -0700599 if (strncmp(target_filename, "EMMC:", 5) != 0) {
600 printf("Supporting patching EMMC targets only.\n");
601 return 1;
602 }
603
604 uint8_t target_sha1[SHA_DIGEST_LENGTH];
605 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
606 printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
607 return 1;
608 }
609
610 // We try to load the target file into the source_file object.
611 FileContents source_file;
612 if (LoadFileContents(target_filename, &source_file) == 0) {
613 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
614 // The early-exit case: the patch was already applied, this file has the desired hash, nothing
615 // for us to do.
616 printf("already %s\n", short_sha1(target_sha1).c_str());
617 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800618 }
Tao Bao40e144d2017-03-15 01:10:58 -0700619 }
Doug Zongker512536a2010-02-17 16:11:44 -0800620
Tao Bao40e144d2017-03-15 01:10:58 -0700621 if (source_file.data.empty() ||
622 (target_filename != source_filename && strcmp(target_filename, source_filename) != 0)) {
623 // Need to load the source file: either we failed to load the target file, or we did but it's
624 // different from the expected.
625 source_file.data.clear();
626 LoadFileContents(source_filename, &source_file);
627 }
628
629 if (!source_file.data.empty()) {
630 int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str);
631 if (to_use != -1) {
632 return GenerateTarget(source_file, patch_data[to_use], target_filename, target_sha1,
633 bonus_data);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800634 }
Tao Bao40e144d2017-03-15 01:10:58 -0700635 }
Doug Zongker512536a2010-02-17 16:11:44 -0800636
Tao Bao40e144d2017-03-15 01:10:58 -0700637 printf("source file is bad; trying copy\n");
Doug Zongker512536a2010-02-17 16:11:44 -0800638
Tao Bao40e144d2017-03-15 01:10:58 -0700639 FileContents copy_file;
640 if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file) < 0) {
641 printf("failed to read copy file\n");
642 return 1;
643 }
Doug Zongker512536a2010-02-17 16:11:44 -0800644
Tao Bao40e144d2017-03-15 01:10:58 -0700645 int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str);
646 if (to_use == -1) {
647 printf("copy file doesn't match source SHA-1s either\n");
648 return 1;
649 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800650
Tao Bao40e144d2017-03-15 01:10:58 -0700651 return GenerateTarget(copy_file, patch_data[to_use], target_filename, target_sha1, bonus_data);
Doug Zongker1c43c972012-02-28 11:07:09 -0800652}
Doug Zongker1c43c972012-02-28 11:07:09 -0800653
Tao Baoabba55b2015-07-17 18:11:12 -0700654/*
655 * This function flashes a given image to the target partition. It verifies
656 * the target cheksum first, and will return if target has the desired hash.
657 * It checks the checksum of the given source image before flashing, and
658 * verifies the target partition afterwards. The function is idempotent.
659 * Returns zero on success.
660 */
661int applypatch_flash(const char* source_filename, const char* target_filename,
662 const char* target_sha1_str, size_t target_size) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800663 printf("flash %s: ", target_filename);
Tao Baoabba55b2015-07-17 18:11:12 -0700664
Tao Bao6e02ea92016-11-17 11:24:07 -0800665 uint8_t target_sha1[SHA_DIGEST_LENGTH];
666 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
667 printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
668 return 1;
669 }
Tao Baoabba55b2015-07-17 18:11:12 -0700670
Tao Bao6e02ea92016-11-17 11:24:07 -0800671 std::string target_str(target_filename);
672 std::vector<std::string> pieces = android::base::Split(target_str, ":");
673 if (pieces.size() != 2 || pieces[0] != "EMMC") {
674 printf("invalid target name \"%s\"", target_filename);
675 return 1;
676 }
Tao Baoabba55b2015-07-17 18:11:12 -0700677
Tao Bao6e02ea92016-11-17 11:24:07 -0800678 // Load the target into the source_file object to see if already applied.
679 pieces.push_back(std::to_string(target_size));
680 pieces.push_back(target_sha1_str);
681 std::string fullname = android::base::Join(pieces, ':');
682 FileContents source_file;
683 if (LoadPartitionContents(fullname, &source_file) == 0 &&
684 memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
685 // The early-exit case: the image was already applied, this partition
686 // has the desired hash, nothing for us to do.
687 printf("already %s\n", short_sha1(target_sha1).c_str());
Tao Baoabba55b2015-07-17 18:11:12 -0700688 return 0;
Tao Bao6e02ea92016-11-17 11:24:07 -0800689 }
690
691 if (LoadFileContents(source_filename, &source_file) == 0) {
692 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
693 // The source doesn't have desired checksum.
694 printf("source \"%s\" doesn't have expected sha1 sum\n", source_filename);
695 printf("expected: %s, found: %s\n", short_sha1(target_sha1).c_str(),
696 short_sha1(source_file.sha1).c_str());
697 return 1;
698 }
699 }
700
701 if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
702 printf("write of copied data to %s failed\n", target_filename);
703 return 1;
704 }
705 return 0;
Doug Zongker1c43c972012-02-28 11:07:09 -0800706}
707
Tao Bao40e144d2017-03-15 01:10:58 -0700708static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
709 const std::string& target_filename,
710 const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800711 if (patch->type != VAL_BLOB) {
712 printf("patch is not a blob\n");
713 return 1;
714 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800715
Tao Bao6e02ea92016-11-17 11:24:07 -0800716 const char* header = &patch->data[0];
717 size_t header_bytes_read = patch->data.size();
718 bool use_bsdiff = false;
719 if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
720 use_bsdiff = true;
721 } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
722 use_bsdiff = false;
723 } else {
724 printf("Unknown patch file format\n");
725 return 1;
726 }
Yabin Cuid483c202016-02-03 17:08:52 -0800727
Tao Bao40e144d2017-03-15 01:10:58 -0700728 CHECK(android::base::StartsWith(target_filename, "EMMC:"));
Yabin Cuid483c202016-02-03 17:08:52 -0800729
Tao Bao40e144d2017-03-15 01:10:58 -0700730 // We still write the original source to cache, in case the partition write is interrupted.
731 if (MakeFreeSpaceOnCache(source_file.data.size()) < 0) {
732 printf("not enough free space on /cache\n");
733 return 1;
734 }
735 if (SaveFileContents(CACHE_TEMP_SOURCE, &source_file) < 0) {
736 printf("failed to back up source file\n");
737 return 1;
738 }
Doug Zongker512536a2010-02-17 16:11:44 -0800739
Tao Bao40e144d2017-03-15 01:10:58 -0700740 // We store the decoded output in memory.
Tao Bao6e02ea92016-11-17 11:24:07 -0800741 std::string memory_sink_str; // Don't need to reserve space.
Tao Baoc0e1c462017-02-01 10:20:10 -0800742 SinkFn sink = [&memory_sink_str](const unsigned char* data, size_t len) {
743 memory_sink_str.append(reinterpret_cast<const char*>(data), len);
744 return len;
745 };
Doug Zongkerc4351c72010-02-22 14:46:32 -0800746
Tao Bao40e144d2017-03-15 01:10:58 -0700747 SHA_CTX ctx;
748 SHA1_Init(&ctx);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800749
Tao Bao40e144d2017-03-15 01:10:58 -0700750 int result;
751 if (use_bsdiff) {
752 result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), patch.get(), 0,
Tao Baoc0e1c462017-02-01 10:20:10 -0800753 sink, &ctx);
Tao Bao40e144d2017-03-15 01:10:58 -0700754 } else {
755 result = ApplyImagePatch(source_file.data.data(), source_file.data.size(), patch.get(), sink,
Tao Baoc0e1c462017-02-01 10:20:10 -0800756 &ctx, bonus_data);
Tao Bao40e144d2017-03-15 01:10:58 -0700757 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800758
Tao Bao40e144d2017-03-15 01:10:58 -0700759 if (result != 0) {
760 printf("applying patch failed\n");
761 return 1;
762 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800763
Tao Bao6e02ea92016-11-17 11:24:07 -0800764 uint8_t current_target_sha1[SHA_DIGEST_LENGTH];
765 SHA1_Final(current_target_sha1, &ctx);
766 if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
767 printf("patch did not produce expected sha1\n");
768 return 1;
769 } else {
770 printf("now %s\n", short_sha1(target_sha1).c_str());
771 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800772
Tao Bao40e144d2017-03-15 01:10:58 -0700773 // Write back the temp file to the partition.
774 if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
775 memory_sink_str.size(), target_filename) != 0) {
776 printf("write of patched data to %s failed\n", target_filename.c_str());
777 return 1;
Tao Bao6e02ea92016-11-17 11:24:07 -0800778 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800779
Tao Bao40e144d2017-03-15 01:10:58 -0700780 // Delete the backup copy of the source.
781 unlink(CACHE_TEMP_SOURCE);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800782
Tao Bao6e02ea92016-11-17 11:24:07 -0800783 // Success!
784 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800785}