2010-02-18 01:11:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
#include "applypatch/applypatch.h"
|
|
|
|
|
2010-02-18 01:11:44 +01:00
|
|
|
#include <errno.h>
|
2015-06-24 08:23:33 +02:00
|
|
|
#include <fcntl.h>
|
2010-02-18 01:11:44 +01:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-02-01 19:20:10 +01:00
|
|
|
#include <functional>
|
2016-02-04 02:08:52 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2016-11-10 21:33:41 +01:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2016-02-04 02:08:52 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
#include <android-base/logging.h>
|
2016-11-10 21:33:41 +01:00
|
|
|
#include <android-base/parseint.h>
|
2015-12-05 00:30:20 +01:00
|
|
|
#include <android-base/strings.h>
|
2016-11-10 21:33:41 +01:00
|
|
|
#include <openssl/sha.h>
|
2015-07-17 20:47:44 +02:00
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
#include "edify/expr.h"
|
2017-09-29 06:29:11 +02:00
|
|
|
#include "otafault/ota_io.h"
|
2018-04-26 03:59:40 +02:00
|
|
|
#include "otautil/paths.h"
|
2017-09-29 23:39:33 +02:00
|
|
|
#include "otautil/print_sha1.h"
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
static int LoadPartitionContents(const std::string& filename, FileContents* file);
|
2017-02-01 19:20:10 +01:00
|
|
|
static size_t FileSink(const unsigned char* data, size_t len, int fd);
|
2017-03-15 09:10:58 +01:00
|
|
|
static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
|
|
|
|
const std::string& target_filename,
|
|
|
|
const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data);
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2018-06-20 19:14:40 +02:00
|
|
|
int LoadFileContents(const std::string& filename, FileContents* file) {
|
2016-11-10 21:33:41 +01:00
|
|
|
// A special 'filename' beginning with "EMMC:" means to load the contents of a partition.
|
2018-06-20 19:14:40 +02:00
|
|
|
if (android::base::StartsWith(filename, "EMMC:")) {
|
2016-11-10 21:33:41 +01:00
|
|
|
return LoadPartitionContents(filename, file);
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-12-01 19:53:34 +01:00
|
|
|
struct stat sb;
|
2018-06-20 19:14:40 +02:00
|
|
|
if (stat(filename.c_str(), &sb) == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to stat \"" << filename << "\"";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-12-01 19:53:34 +01:00
|
|
|
std::vector<unsigned char> data(sb.st_size);
|
2018-06-20 19:14:40 +02:00
|
|
|
unique_file f(ota_fopen(filename.c_str(), "rb"));
|
2016-11-10 21:33:41 +01:00
|
|
|
if (!f) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to open \"" << filename << "\"";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
size_t bytes_read = ota_fread(data.data(), 1, data.size(), f.get());
|
|
|
|
if (bytes_read != data.size()) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Short read of \"" << filename << "\" (" << bytes_read << " bytes of "
|
|
|
|
<< data.size() << ")";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
file->data = std::move(data);
|
|
|
|
SHA1(file->data.data(), file->data.size(), file->sha1);
|
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2018-06-05 20:26:01 +02:00
|
|
|
// Loads the contents of an EMMC partition into the provided FileContents. filename should be a
|
|
|
|
// string of the form "EMMC:<partition_device>:...". The smallest size_n bytes for which that prefix
|
|
|
|
// of the partition contents has the corresponding sha1 hash will be loaded. It is acceptable for a
|
|
|
|
// size value to be repeated with different sha1s. Returns 0 on success.
|
2010-02-18 01:11:44 +01:00
|
|
|
//
|
2018-06-05 20:26:01 +02:00
|
|
|
// This complexity is needed because if an OTA installation is interrupted, the partition might
|
|
|
|
// contain either the source or the target data, which might be of different lengths. We need to
|
|
|
|
// know the length in order to read from a partition (there is no "end-of-file" marker), so the
|
|
|
|
// caller must specify the possible lengths and the hash of the data, and we'll do the load
|
|
|
|
// expecting to find one of those hashes.
|
2016-11-10 21:33:41 +01:00
|
|
|
static int LoadPartitionContents(const std::string& filename, FileContents* file) {
|
|
|
|
std::vector<std::string> pieces = android::base::Split(filename, ":");
|
|
|
|
if (pieces.size() < 4 || pieces.size() % 2 != 0 || pieces[0] != "EMMC") {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "LoadPartitionContents called with bad filename \"" << filename << "\"";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t pair_count = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename
|
|
|
|
std::vector<std::pair<size_t, std::string>> pairs;
|
|
|
|
for (size_t i = 0; i < pair_count; ++i) {
|
|
|
|
size_t size;
|
|
|
|
if (!android::base::ParseUint(pieces[i * 2 + 2], &size) || size == 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "LoadPartitionContents called with bad size \"" << pieces[i * 2 + 2] << "\"";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pairs.push_back({ size, pieces[i * 2 + 3] });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the pairs array so that they are in order of increasing size.
|
|
|
|
std::sort(pairs.begin(), pairs.end());
|
|
|
|
|
|
|
|
const char* partition = pieces[1].c_str();
|
2016-11-28 20:48:43 +01:00
|
|
|
unique_file dev(ota_fopen(partition, "rb"));
|
2016-11-10 21:33:41 +01:00
|
|
|
if (!dev) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to open eMMC partition \"" << partition << "\"";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SHA_CTX sha_ctx;
|
|
|
|
SHA1_Init(&sha_ctx);
|
|
|
|
|
|
|
|
// Allocate enough memory to hold the largest size.
|
|
|
|
std::vector<unsigned char> buffer(pairs[pair_count - 1].first);
|
|
|
|
unsigned char* buffer_ptr = buffer.data();
|
|
|
|
size_t buffer_size = 0; // # bytes read so far
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
for (const auto& pair : pairs) {
|
|
|
|
size_t current_size = pair.first;
|
|
|
|
const std::string& current_sha1 = pair.second;
|
|
|
|
|
|
|
|
// Read enough additional bytes to get us up to the next size. (Again,
|
|
|
|
// we're trying the possibilities in order of increasing size).
|
|
|
|
size_t next = current_size - buffer_size;
|
|
|
|
if (next > 0) {
|
|
|
|
size_t read = ota_fread(buffer_ptr, 1, next, dev.get());
|
|
|
|
if (next != read) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Short read (" << read << " bytes of " << next << ") for partition \""
|
|
|
|
<< partition << "\"";
|
2010-02-18 01:11:44 +01:00
|
|
|
return -1;
|
2016-11-10 21:33:41 +01:00
|
|
|
}
|
|
|
|
SHA1_Update(&sha_ctx, buffer_ptr, read);
|
|
|
|
buffer_size += read;
|
|
|
|
buffer_ptr += read;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
// Duplicate the SHA context and finalize the duplicate so we can
|
|
|
|
// check it against this pair's expected hash.
|
|
|
|
SHA_CTX temp_ctx;
|
|
|
|
memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
|
|
|
|
uint8_t sha_so_far[SHA_DIGEST_LENGTH];
|
|
|
|
SHA1_Final(sha_so_far, &temp_ctx);
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-02-04 09:23:21 +01:00
|
|
|
uint8_t parsed_sha[SHA_DIGEST_LENGTH];
|
2018-06-20 19:14:40 +02:00
|
|
|
if (ParseSha1(current_sha1, parsed_sha) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Failed to parse SHA-1 \"" << current_sha1 << "\" in " << filename;
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) {
|
|
|
|
// We have a match. Stop reading the partition; we'll return the data we've read so far.
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(INFO) << "Partition read matched size " << current_size << " SHA-1 " << current_sha1;
|
2016-11-10 21:33:41 +01:00
|
|
|
found = true;
|
|
|
|
break;
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
2016-11-10 21:33:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
// Ran off the end of the list of (size, sha1) pairs without finding a match.
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Contents of partition \"" << partition << "\" didn't match " << filename;
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
SHA1_Final(file->sha1, &sha_ctx);
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
buffer.resize(buffer_size);
|
|
|
|
file->data = std::move(buffer);
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 19:14:40 +02:00
|
|
|
int SaveFileContents(const std::string& filename, const FileContents* file) {
|
|
|
|
unique_fd fd(
|
|
|
|
ota_open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
|
2016-11-17 20:24:07 +01:00
|
|
|
if (fd == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to open \"" << filename << "\" for write";
|
2016-11-17 20:24:07 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-02-01 19:20:10 +01:00
|
|
|
size_t bytes_written = FileSink(file->data.data(), file->data.size(), fd);
|
2017-03-28 00:12:48 +02:00
|
|
|
if (bytes_written != file->data.size()) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Short write of \"" << filename << "\" (" << bytes_written << " bytes of "
|
|
|
|
<< file->data.size();
|
2016-11-17 20:24:07 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ota_fsync(fd) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to fsync \"" << filename << "\"";
|
2016-11-17 20:24:07 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2016-11-22 20:37:13 +01:00
|
|
|
if (ota_close(fd) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to close \"" << filename << "\"";
|
2016-11-17 20:24:07 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-11-17 20:24:07 +01:00
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2018-06-05 20:26:01 +02:00
|
|
|
// Writes a memory buffer to 'target' partition, a string of the form
|
|
|
|
// "EMMC:<partition_device>[:...]". The target name might contain multiple colons, but
|
|
|
|
// WriteToPartition() only uses the first two and ignores the rest. Returns 0 on success.
|
|
|
|
static int WriteToPartition(const unsigned char* data, size_t len, const std::string& target) {
|
2016-11-17 20:24:07 +01:00
|
|
|
std::vector<std::string> pieces = android::base::Split(target, ":");
|
2016-11-10 21:33:41 +01:00
|
|
|
if (pieces.size() < 2 || pieces[0] != "EMMC") {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "WriteToPartition called with bad target \"" << target << "\"";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
const char* partition = pieces[1].c_str();
|
2016-11-17 20:24:07 +01:00
|
|
|
unique_fd fd(ota_open(partition, O_RDWR));
|
|
|
|
if (fd == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to open \"" << partition << "\"";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-17 20:24:07 +01:00
|
|
|
size_t start = 0;
|
|
|
|
bool success = false;
|
2016-11-10 21:33:41 +01:00
|
|
|
for (size_t attempt = 0; attempt < 2; ++attempt) {
|
|
|
|
if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to seek to " << start << " on \"" << partition << "\"";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
2016-11-10 21:33:41 +01:00
|
|
|
while (start < len) {
|
|
|
|
size_t to_write = len - start;
|
|
|
|
if (to_write > 1 << 20) to_write = 1 << 20;
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
ssize_t written = TEMP_FAILURE_RETRY(ota_write(fd, data + start, to_write));
|
|
|
|
if (written == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to write to \"" << partition << "\"";
|
2016-06-10 02:41:22 +02:00
|
|
|
return -1;
|
2016-11-10 21:33:41 +01:00
|
|
|
}
|
|
|
|
start += written;
|
2016-06-10 02:41:22 +02:00
|
|
|
}
|
2010-07-07 22:55:25 +02:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
if (ota_fsync(fd) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to sync \"" << partition << "\"";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2016-11-22 20:37:13 +01:00
|
|
|
if (ota_close(fd) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to close \"" << partition << "\"";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-07-07 22:55:25 +02:00
|
|
|
|
2016-11-17 20:24:07 +01:00
|
|
|
fd.reset(ota_open(partition, O_RDONLY));
|
|
|
|
if (fd == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to reopen \"" << partition << "\" for verification";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-07-07 22:55:25 +02:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
// Drop caches so our subsequent verification read won't just be reading the cache.
|
|
|
|
sync();
|
2016-11-17 20:24:07 +01:00
|
|
|
unique_fd dc(ota_open("/proc/sys/vm/drop_caches", O_WRONLY));
|
2016-11-10 21:33:41 +01:00
|
|
|
if (TEMP_FAILURE_RETRY(ota_write(dc, "3\n", 2)) == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to write to /proc/sys/vm/drop_caches";
|
2016-11-10 21:33:41 +01:00
|
|
|
} else {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(INFO) << " caches dropped";
|
2016-11-10 21:33:41 +01:00
|
|
|
}
|
2016-11-22 20:37:13 +01:00
|
|
|
ota_close(dc);
|
2016-11-10 21:33:41 +01:00
|
|
|
sleep(1);
|
|
|
|
|
2016-11-17 20:24:07 +01:00
|
|
|
// Verify.
|
2016-11-10 21:33:41 +01:00
|
|
|
if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to seek to 0 on " << partition;
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char buffer[4096];
|
|
|
|
start = len;
|
|
|
|
for (size_t p = 0; p < len; p += sizeof(buffer)) {
|
|
|
|
size_t to_read = len - p;
|
|
|
|
if (to_read > sizeof(buffer)) {
|
|
|
|
to_read = sizeof(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t so_far = 0;
|
|
|
|
while (so_far < to_read) {
|
|
|
|
ssize_t read_count = TEMP_FAILURE_RETRY(ota_read(fd, buffer + so_far, to_read - so_far));
|
|
|
|
if (read_count == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to verify-read " << partition << " at " << p;
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
} else if (read_count == 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Verify-reading " << partition << " reached unexpected EOF at " << p;
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
2016-06-10 02:41:22 +02:00
|
|
|
}
|
2016-11-10 21:33:41 +01:00
|
|
|
if (static_cast<size_t>(read_count) < to_read) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(INFO) << "Short verify-read " << partition << " at " << p << ": expected " << to_read
|
|
|
|
<< " actual " << read_count;
|
2016-06-10 02:41:22 +02:00
|
|
|
}
|
2016-11-10 21:33:41 +01:00
|
|
|
so_far += read_count;
|
|
|
|
}
|
2013-07-08 18:42:54 +02:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
if (memcmp(buffer, data + p, to_read) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Verification failed starting at " << p;
|
2016-11-10 21:33:41 +01:00
|
|
|
start = p;
|
|
|
|
break;
|
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
if (start == len) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(INFO) << "Verification read succeeded (attempt " << attempt + 1 << ")";
|
2016-11-10 21:33:41 +01:00
|
|
|
success = true;
|
|
|
|
break;
|
2016-06-10 02:41:22 +02:00
|
|
|
}
|
2016-12-19 04:22:30 +01:00
|
|
|
|
|
|
|
if (ota_close(fd) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to close " << partition;
|
2016-12-19 04:22:30 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd.reset(ota_open(partition, O_RDWR));
|
|
|
|
if (fd == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to reopen " << partition << " for next attempt";
|
2016-12-19 04:22:30 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2016-11-10 21:33:41 +01:00
|
|
|
}
|
2016-06-10 02:41:22 +02:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
if (!success) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Failed to verify after all attempts";
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2016-06-10 02:41:22 +02:00
|
|
|
|
2016-11-22 20:37:13 +01:00
|
|
|
if (ota_close(fd) == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to close " << partition;
|
2016-11-10 21:33:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sync();
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-11-10 21:33:41 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2018-06-20 19:14:40 +02:00
|
|
|
int ParseSha1(const std::string& str, uint8_t* digest) {
|
|
|
|
const char* ps = str.c_str();
|
2018-06-05 20:26:01 +02:00
|
|
|
uint8_t* pd = digest;
|
|
|
|
for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
|
|
|
|
int digit;
|
|
|
|
if (*ps >= '0' && *ps <= '9') {
|
|
|
|
digit = *ps - '0';
|
|
|
|
} else if (*ps >= 'a' && *ps <= 'f') {
|
|
|
|
digit = *ps - 'a' + 10;
|
|
|
|
} else if (*ps >= 'A' && *ps <= 'F') {
|
|
|
|
digit = *ps - 'A' + 10;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (i % 2 == 0) {
|
|
|
|
*pd = digit << 4;
|
|
|
|
} else {
|
|
|
|
*pd |= digit;
|
|
|
|
++pd;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
2018-06-05 20:26:01 +02:00
|
|
|
}
|
|
|
|
if (*ps != '\0') return -1;
|
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2018-06-05 20:26:01 +02:00
|
|
|
// Searches a vector of SHA-1 strings for one matching the given SHA-1. Returns the index of the
|
|
|
|
// match on success, or -1 if no match is found.
|
|
|
|
static int FindMatchingPatch(const uint8_t* sha1, const std::vector<std::string>& patch_sha1s) {
|
|
|
|
for (size_t i = 0; i < patch_sha1s.size(); ++i) {
|
2016-11-10 21:33:41 +01:00
|
|
|
uint8_t patch_sha1[SHA_DIGEST_LENGTH];
|
2018-06-20 19:14:40 +02:00
|
|
|
if (ParseSha1(patch_sha1s[i], patch_sha1) == 0 &&
|
2016-11-10 21:33:41 +01:00
|
|
|
memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2018-06-05 20:26:01 +02:00
|
|
|
int applypatch_check(const char* filename, const std::vector<std::string>& patch_sha1s) {
|
|
|
|
// It's okay to specify no SHA-1s; the check will pass if the LoadFileContents is successful.
|
|
|
|
// (Useful for reading partitions, where the filename encodes the SHA-1s; no need to check them
|
|
|
|
// twice.)
|
2016-11-10 21:33:41 +01:00
|
|
|
FileContents file;
|
|
|
|
if (LoadFileContents(filename, &file) != 0 ||
|
2018-06-05 20:26:01 +02:00
|
|
|
(!patch_sha1s.empty() && FindMatchingPatch(file.sha1, patch_sha1s) < 0)) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(INFO) << "\"" << filename << "\" doesn't have any of expected SHA-1 sums; checking cache";
|
2016-11-10 21:33:41 +01:00
|
|
|
|
2017-10-25 22:16:54 +02:00
|
|
|
// If the source file is missing or corrupted, it might be because we were killed in the middle
|
2018-06-05 20:26:01 +02:00
|
|
|
// of patching it. A copy should have been made in cache_temp_source. If that file exists and
|
|
|
|
// matches the SHA-1 we're looking for, the check still passes.
|
2018-06-20 19:14:40 +02:00
|
|
|
if (LoadFileContents(Paths::Get().cache_temp_source(), &file) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Failed to load cache file";
|
2016-11-10 21:33:41 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:26:01 +02:00
|
|
|
if (FindMatchingPatch(file.sha1, patch_sha1s) < 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "The cache bits don't match any SHA-1 for \"" << filename << "\"";
|
2016-11-10 21:33:41 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ShowLicenses() {
|
2018-06-05 20:26:01 +02:00
|
|
|
ShowBSDiffLicense();
|
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2017-02-01 19:20:10 +01:00
|
|
|
static size_t FileSink(const unsigned char* data, size_t len, int fd) {
|
2017-03-28 00:12:48 +02:00
|
|
|
size_t done = 0;
|
|
|
|
while (done < len) {
|
|
|
|
ssize_t wrote = TEMP_FAILURE_RETRY(ota_write(fd, data + done, len - done));
|
|
|
|
if (wrote == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to write " << len - done << " bytes";
|
2017-03-28 00:12:48 +02:00
|
|
|
return done;
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
2017-03-28 00:12:48 +02:00
|
|
|
done += wrote;
|
|
|
|
}
|
|
|
|
return done;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 23:38:09 +02:00
|
|
|
size_t FreeSpaceForFile(const std::string& filename) {
|
|
|
|
struct statfs sf;
|
|
|
|
if (statfs(filename.c_str(), &sf) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to statfs " << filename;
|
2018-04-11 23:38:09 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return sf.f_bsize * sf.f_bavail;
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CacheSizeCheck(size_t bytes) {
|
2018-06-05 20:26:01 +02:00
|
|
|
if (MakeFreeSpaceOnCache(bytes) < 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Failed to make " << bytes << " bytes available on /cache";
|
2018-06-05 20:26:01 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
int applypatch(const char* source_filename, const char* target_filename,
|
2018-02-04 02:20:56 +01:00
|
|
|
const char* target_sha1_str, size_t /* target_size */,
|
2018-06-05 20:26:01 +02:00
|
|
|
const std::vector<std::string>& patch_sha1s,
|
2017-03-15 09:10:58 +01:00
|
|
|
const std::vector<std::unique_ptr<Value>>& patch_data, const Value* bonus_data) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(INFO) << "Patching " << source_filename;
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
if (target_filename[0] == '-' && target_filename[1] == '\0') {
|
|
|
|
target_filename = source_filename;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
if (strncmp(target_filename, "EMMC:", 5) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Supporting patching EMMC targets only";
|
2017-03-15 09:10:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
uint8_t target_sha1[SHA_DIGEST_LENGTH];
|
|
|
|
if (ParseSha1(target_sha1_str, target_sha1) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Failed to parse target SHA-1 \"" << target_sha1_str << "\"";
|
2017-03-15 09:10:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
// We try to load the target file into the source_file object.
|
|
|
|
FileContents source_file;
|
|
|
|
if (LoadFileContents(target_filename, &source_file) == 0) {
|
|
|
|
if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
|
|
|
|
// The early-exit case: the patch was already applied, this file has the desired hash, nothing
|
|
|
|
// for us to do.
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(INFO) << " already " << short_sha1(target_sha1);
|
2017-03-15 09:10:58 +01:00
|
|
|
return 0;
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
2017-03-15 09:10:58 +01:00
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
if (source_file.data.empty() ||
|
|
|
|
(target_filename != source_filename && strcmp(target_filename, source_filename) != 0)) {
|
|
|
|
// Need to load the source file: either we failed to load the target file, or we did but it's
|
|
|
|
// different from the expected.
|
|
|
|
source_file.data.clear();
|
|
|
|
LoadFileContents(source_filename, &source_file);
|
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
if (!source_file.data.empty()) {
|
2018-06-05 20:26:01 +02:00
|
|
|
int to_use = FindMatchingPatch(source_file.sha1, patch_sha1s);
|
2017-03-15 09:10:58 +01:00
|
|
|
if (to_use != -1) {
|
|
|
|
return GenerateTarget(source_file, patch_data[to_use], target_filename, target_sha1,
|
|
|
|
bonus_data);
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
2017-03-15 09:10:58 +01:00
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(INFO) << "Source file is bad; trying copy";
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
FileContents copy_file;
|
2018-06-20 19:14:40 +02:00
|
|
|
if (LoadFileContents(Paths::Get().cache_temp_source(), ©_file) < 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Failed to read copy file";
|
2017-03-15 09:10:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2018-06-05 20:26:01 +02:00
|
|
|
int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1s);
|
2017-03-15 09:10:58 +01:00
|
|
|
if (to_use == -1) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "The copy on /cache doesn't match source SHA-1s either";
|
2017-03-15 09:10:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
return GenerateTarget(copy_file, patch_data[to_use], target_filename, target_sha1, bonus_data);
|
2012-02-28 20:07:09 +01:00
|
|
|
}
|
|
|
|
|
2015-07-18 03:11:12 +02:00
|
|
|
int applypatch_flash(const char* source_filename, const char* target_filename,
|
|
|
|
const char* target_sha1_str, size_t target_size) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(INFO) << "Flashing " << target_filename;
|
2015-07-18 03:11:12 +02:00
|
|
|
|
2016-11-17 20:24:07 +01:00
|
|
|
uint8_t target_sha1[SHA_DIGEST_LENGTH];
|
|
|
|
if (ParseSha1(target_sha1_str, target_sha1) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Failed to parse target SHA-1 \"" << target_sha1_str << "\"";
|
2016-11-17 20:24:07 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2015-07-18 03:11:12 +02:00
|
|
|
|
2016-11-17 20:24:07 +01:00
|
|
|
std::string target_str(target_filename);
|
|
|
|
std::vector<std::string> pieces = android::base::Split(target_str, ":");
|
|
|
|
if (pieces.size() != 2 || pieces[0] != "EMMC") {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Invalid target name \"" << target_filename << "\"";
|
2016-11-17 20:24:07 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2015-07-18 03:11:12 +02:00
|
|
|
|
2016-11-17 20:24:07 +01:00
|
|
|
// Load the target into the source_file object to see if already applied.
|
|
|
|
pieces.push_back(std::to_string(target_size));
|
|
|
|
pieces.push_back(target_sha1_str);
|
|
|
|
std::string fullname = android::base::Join(pieces, ':');
|
|
|
|
FileContents source_file;
|
|
|
|
if (LoadPartitionContents(fullname, &source_file) == 0 &&
|
|
|
|
memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
// The early-exit case: the image was already applied, this partition has the desired hash,
|
|
|
|
// nothing for us to do.
|
|
|
|
LOG(INFO) << " already " << short_sha1(target_sha1);
|
2016-11-17 20:24:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-07-18 03:11:12 +02:00
|
|
|
|
2016-11-17 20:24:07 +01:00
|
|
|
if (LoadFileContents(source_filename, &source_file) == 0) {
|
|
|
|
if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
|
|
|
|
// The source doesn't have desired checksum.
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "source \"" << source_filename << "\" doesn't have expected SHA-1 sum";
|
|
|
|
LOG(ERROR) << "expected: " << short_sha1(target_sha1)
|
|
|
|
<< ", found: " << short_sha1(source_file.sha1);
|
2016-11-17 20:24:07 +01:00
|
|
|
return 1;
|
2015-07-18 03:11:12 +02:00
|
|
|
}
|
2016-11-17 20:24:07 +01:00
|
|
|
}
|
2015-07-18 03:11:12 +02:00
|
|
|
|
2016-11-17 20:24:07 +01:00
|
|
|
if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Failed to write copied data to " << target_filename;
|
2016-11-17 20:24:07 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2015-07-18 03:11:12 +02:00
|
|
|
}
|
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
|
|
|
|
const std::string& target_filename,
|
|
|
|
const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data) {
|
2016-11-17 20:24:07 +01:00
|
|
|
if (patch->type != VAL_BLOB) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "patch is not a blob";
|
2016-11-17 20:24:07 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2016-11-17 20:24:07 +01:00
|
|
|
const char* header = &patch->data[0];
|
|
|
|
size_t header_bytes_read = patch->data.size();
|
|
|
|
bool use_bsdiff = false;
|
|
|
|
if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
|
|
|
|
use_bsdiff = true;
|
|
|
|
} else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
|
|
|
|
use_bsdiff = false;
|
|
|
|
} else {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Unknown patch file format";
|
2016-11-17 20:24:07 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
CHECK(android::base::StartsWith(target_filename, "EMMC:"));
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
// We still write the original source to cache, in case the partition write is interrupted.
|
|
|
|
if (MakeFreeSpaceOnCache(source_file.data.size()) < 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Not enough free space on /cache";
|
2017-03-15 09:10:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2018-06-20 19:14:40 +02:00
|
|
|
if (SaveFileContents(Paths::Get().cache_temp_source(), &source_file) < 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Failed to back up source file";
|
2017-03-15 09:10:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2016-11-17 20:24:07 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
// We store the decoded output in memory.
|
|
|
|
std::string memory_sink_str; // Don't need to reserve space.
|
2018-04-20 06:02:13 +02:00
|
|
|
SHA_CTX ctx;
|
|
|
|
SHA1_Init(&ctx);
|
|
|
|
SinkFn sink = [&memory_sink_str, &ctx](const unsigned char* data, size_t len) {
|
2018-04-24 02:51:45 +02:00
|
|
|
if (len != 0) {
|
|
|
|
uint8_t digest[SHA_DIGEST_LENGTH];
|
|
|
|
SHA1(data, len, digest);
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(DEBUG) << "Appending " << len << " bytes data, SHA-1: " << short_sha1(digest);
|
2018-04-24 02:51:45 +02:00
|
|
|
}
|
2018-04-20 06:02:13 +02:00
|
|
|
SHA1_Update(&ctx, data, len);
|
2017-02-01 19:20:10 +01:00
|
|
|
memory_sink_str.append(reinterpret_cast<const char*>(data), len);
|
|
|
|
return len;
|
|
|
|
};
|
2016-11-17 20:24:07 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
int result;
|
|
|
|
if (use_bsdiff) {
|
2018-04-20 06:02:13 +02:00
|
|
|
result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), *patch, 0, sink);
|
2017-03-15 09:10:58 +01:00
|
|
|
} else {
|
2018-04-20 06:02:13 +02:00
|
|
|
result =
|
|
|
|
ApplyImagePatch(source_file.data.data(), source_file.data.size(), *patch, sink, bonus_data);
|
2017-03-15 09:10:58 +01:00
|
|
|
}
|
2016-11-17 20:24:07 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
if (result != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Failed to apply the patch: " << result;
|
2017-03-15 09:10:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2016-11-17 20:24:07 +01:00
|
|
|
uint8_t current_target_sha1[SHA_DIGEST_LENGTH];
|
|
|
|
SHA1_Final(current_target_sha1, &ctx);
|
|
|
|
if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Patching did not produce the expected SHA-1 of " << short_sha1(target_sha1);
|
2018-04-19 21:35:14 +02:00
|
|
|
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "target size " << memory_sink_str.size() << " SHA-1 "
|
|
|
|
<< short_sha1(current_target_sha1);
|
|
|
|
LOG(ERROR) << "source size " << source_file.data.size() << " SHA-1 "
|
|
|
|
<< short_sha1(source_file.sha1);
|
2018-04-19 21:35:14 +02:00
|
|
|
|
|
|
|
uint8_t patch_digest[SHA_DIGEST_LENGTH];
|
|
|
|
SHA1(reinterpret_cast<const uint8_t*>(patch->data.data()), patch->data.size(), patch_digest);
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "patch size " << patch->data.size() << " SHA-1 " << short_sha1(patch_digest);
|
2018-04-19 21:35:14 +02:00
|
|
|
|
|
|
|
uint8_t bonus_digest[SHA_DIGEST_LENGTH];
|
|
|
|
SHA1(reinterpret_cast<const uint8_t*>(bonus_data->data.data()), bonus_data->data.size(),
|
|
|
|
bonus_digest);
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "bonus size " << bonus_data->data.size() << " SHA-1 " << short_sha1(bonus_digest);
|
2018-04-19 21:35:14 +02:00
|
|
|
|
2018-04-27 02:23:23 +02:00
|
|
|
// TODO(b/67849209) Remove after debugging the unit test flakiness.
|
|
|
|
if (android::base::GetMinimumLogSeverity() <= android::base::LogSeverity::DEBUG) {
|
|
|
|
if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
|
|
|
|
memory_sink_str.size(), target_filename) != 0) {
|
|
|
|
LOG(DEBUG) << "Failed to write patched data " << target_filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 20:24:07 +01:00
|
|
|
return 1;
|
|
|
|
} else {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(INFO) << " now " << short_sha1(target_sha1);
|
2016-11-17 20:24:07 +01:00
|
|
|
}
|
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
// Write back the temp file to the partition.
|
|
|
|
if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
|
|
|
|
memory_sink_str.size(), target_filename) != 0) {
|
2018-04-26 08:00:27 +02:00
|
|
|
LOG(ERROR) << "Failed to write patched data to " << target_filename;
|
2017-03-15 09:10:58 +01:00
|
|
|
return 1;
|
2016-11-17 20:24:07 +01:00
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2017-03-15 09:10:58 +01:00
|
|
|
// Delete the backup copy of the source.
|
2018-04-26 03:59:40 +02:00
|
|
|
unlink(Paths::Get().cache_temp_source().c_str());
|
2016-11-17 20:24:07 +01:00
|
|
|
|
|
|
|
// Success!
|
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|