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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
2016-02-04 02:08:52 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2015-12-05 00:30:20 +01:00
|
|
|
#include <android-base/strings.h>
|
2015-07-17 20:47:44 +02:00
|
|
|
|
2016-02-04 09:23:21 +01:00
|
|
|
#include "openssl/sha.h"
|
2016-03-03 20:43:47 +01:00
|
|
|
#include "applypatch/applypatch.h"
|
2010-02-22 23:46:32 +01:00
|
|
|
#include "edify/expr.h"
|
2015-12-16 01:04:53 +01:00
|
|
|
#include "ota_io.h"
|
2015-08-06 00:20:27 +02:00
|
|
|
#include "print_sha1.h"
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2010-07-07 22:55:25 +02:00
|
|
|
static int LoadPartitionContents(const char* filename, FileContents* file);
|
2014-08-15 23:31:52 +02:00
|
|
|
static ssize_t FileSink(const unsigned char* data, ssize_t len, void* token);
|
2012-02-28 20:07:09 +01:00
|
|
|
static int GenerateTarget(FileContents* source_file,
|
|
|
|
const Value* source_patch_value,
|
|
|
|
FileContents* copy_file,
|
|
|
|
const Value* copy_patch_value,
|
|
|
|
const char* source_filename,
|
|
|
|
const char* target_filename,
|
2016-02-04 09:23:21 +01:00
|
|
|
const uint8_t target_sha1[SHA_DIGEST_LENGTH],
|
2012-08-21 00:28:02 +02:00
|
|
|
size_t target_size,
|
|
|
|
const Value* bonus_data);
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2014-02-14 00:18:19 +01:00
|
|
|
// Read a file into memory; store the file contents and associated
|
2010-08-02 19:29:49 +02:00
|
|
|
// metadata in *file.
|
|
|
|
//
|
|
|
|
// Return 0 on success.
|
2014-02-14 00:18:19 +01:00
|
|
|
int LoadFileContents(const char* filename, FileContents* file) {
|
2016-06-10 02:41:22 +02:00
|
|
|
// A special 'filename' beginning with "EMMC:" means to
|
2010-07-07 22:55:25 +02:00
|
|
|
// load the contents of a partition.
|
2016-06-10 02:41:22 +02:00
|
|
|
if (strncmp(filename, "EMMC:", 5) == 0) {
|
2010-07-07 22:55:25 +02:00
|
|
|
return LoadPartitionContents(filename, file);
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
if (stat(filename, &file->st) != 0) {
|
|
|
|
printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-02-11 01:41:10 +01:00
|
|
|
std::vector<unsigned char> data(file->st.st_size);
|
2015-12-16 01:04:53 +01:00
|
|
|
FILE* f = ota_fopen(filename, "rb");
|
2010-02-22 23:46:32 +01:00
|
|
|
if (f == NULL) {
|
|
|
|
printf("failed to open \"%s\": %s\n", filename, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-02-11 01:41:10 +01:00
|
|
|
size_t bytes_read = ota_fread(data.data(), 1, data.size(), f);
|
|
|
|
if (bytes_read != data.size()) {
|
|
|
|
printf("short read of \"%s\" (%zu bytes of %zu)\n", filename, bytes_read, data.size());
|
2015-12-16 01:04:53 +01:00
|
|
|
ota_fclose(f);
|
2010-02-22 23:46:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-12-16 01:04:53 +01:00
|
|
|
ota_fclose(f);
|
2016-02-11 01:41:10 +01:00
|
|
|
file->data = std::move(data);
|
|
|
|
SHA1(file->data.data(), file->data.size(), file->sha1);
|
2010-02-22 23:46:32 +01:00
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
// Load the contents of an EMMC partition into the provided
|
2010-02-18 01:11:44 +01:00
|
|
|
// FileContents. filename should be a string of the form
|
2016-06-10 02:41:22 +02:00
|
|
|
// "EMMC:<partition_device>:...". The smallest size_n bytes for
|
2010-07-07 22:55:25 +02:00
|
|
|
// 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. Will return 0 on success.
|
2010-02-18 01:11:44 +01: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
|
2010-07-07 22:55:25 +02:00
|
|
|
// 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.
|
|
|
|
static int LoadPartitionContents(const char* filename, FileContents* file) {
|
2015-07-17 20:47:44 +02:00
|
|
|
std::string copy(filename);
|
|
|
|
std::vector<std::string> pieces = android::base::Split(copy, ":");
|
|
|
|
if (pieces.size() < 4 || pieces.size() % 2 != 0) {
|
|
|
|
printf("LoadPartitionContents called with bad filename (%s)\n", filename);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-07-07 22:55:25 +02:00
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
if (pieces[0] != "EMMC") {
|
2015-06-24 08:23:33 +02:00
|
|
|
printf("LoadPartitionContents called with bad filename (%s)\n", filename);
|
2010-02-18 01:11:44 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-07-17 20:47:44 +02:00
|
|
|
const char* partition = pieces[1].c_str();
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2015-07-17 20:47:44 +02:00
|
|
|
size_t pairs = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename
|
|
|
|
std::vector<size_t> index(pairs);
|
|
|
|
std::vector<size_t> size(pairs);
|
|
|
|
std::vector<std::string> sha1sum(pairs);
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2015-07-17 20:47:44 +02:00
|
|
|
for (size_t i = 0; i < pairs; ++i) {
|
|
|
|
size[i] = strtol(pieces[i*2+2].c_str(), NULL, 10);
|
2010-02-22 23:46:32 +01:00
|
|
|
if (size[i] == 0) {
|
2010-07-07 22:55:25 +02:00
|
|
|
printf("LoadPartitionContents called with bad size (%s)\n", filename);
|
2010-02-22 23:46:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-07-17 20:47:44 +02:00
|
|
|
sha1sum[i] = pieces[i*2+3].c_str();
|
2010-02-22 23:46:32 +01:00
|
|
|
index[i] = i;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2015-07-17 20:47:44 +02:00
|
|
|
// Sort the index[] array so it indexes the pairs in order of increasing size.
|
|
|
|
sort(index.begin(), index.end(),
|
|
|
|
[&](const size_t& i, const size_t& j) {
|
|
|
|
return (size[i] < size[j]);
|
|
|
|
}
|
|
|
|
);
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
FILE* dev = ota_fopen(partition, "rb");
|
|
|
|
if (dev == NULL) {
|
|
|
|
printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno));
|
|
|
|
return -1;
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
SHA_CTX sha_ctx;
|
2016-02-04 09:23:21 +01:00
|
|
|
SHA1_Init(&sha_ctx);
|
|
|
|
uint8_t parsed_sha[SHA_DIGEST_LENGTH];
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2015-07-17 20:47:44 +02:00
|
|
|
// Allocate enough memory to hold the largest size.
|
2016-02-11 01:41:10 +01:00
|
|
|
std::vector<unsigned char> data(size[index[pairs-1]]);
|
|
|
|
char* p = reinterpret_cast<char*>(data.data());
|
|
|
|
size_t data_size = 0; // # bytes read so far
|
2015-07-17 20:47:44 +02:00
|
|
|
bool found = false;
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2015-07-17 20:47:44 +02:00
|
|
|
for (size_t i = 0; i < pairs; ++i) {
|
|
|
|
// Read enough additional bytes to get us up to the next size. (Again,
|
|
|
|
// we're trying the possibilities in order of increasing size).
|
2016-02-11 01:41:10 +01:00
|
|
|
size_t next = size[index[i]] - data_size;
|
2010-02-22 23:46:32 +01:00
|
|
|
if (next > 0) {
|
2016-06-10 02:41:22 +02:00
|
|
|
size_t read = ota_fread(p, 1, next, dev);
|
2010-02-22 23:46:32 +01:00
|
|
|
if (next != read) {
|
2014-03-14 17:39:48 +01:00
|
|
|
printf("short read (%zu bytes of %zu) for partition \"%s\"\n",
|
2010-02-22 23:46:32 +01:00
|
|
|
read, next, partition);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-02-04 09:23:21 +01:00
|
|
|
SHA1_Update(&sha_ctx, p, read);
|
2016-02-11 01:41:10 +01:00
|
|
|
data_size += read;
|
|
|
|
p += read;
|
2010-02-22 23:46:32 +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));
|
2016-02-04 09:23:21 +01:00
|
|
|
uint8_t sha_so_far[SHA_DIGEST_LENGTH];
|
|
|
|
SHA1_Final(sha_so_far, &temp_ctx);
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2015-07-17 20:47:44 +02:00
|
|
|
if (ParseSha1(sha1sum[index[i]].c_str(), parsed_sha) != 0) {
|
|
|
|
printf("failed to parse sha1 %s in %s\n", sha1sum[index[i]].c_str(), filename);
|
2010-02-22 23:46:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-02-04 09:23:21 +01:00
|
|
|
if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) {
|
2010-02-22 23:46:32 +01:00
|
|
|
// we have a match. stop reading the partition; we'll return
|
|
|
|
// the data we've read so far.
|
2014-03-14 17:39:48 +01:00
|
|
|
printf("partition read matched size %zu sha %s\n",
|
2015-07-17 20:47:44 +02:00
|
|
|
size[index[i]], sha1sum[index[i]].c_str());
|
|
|
|
found = true;
|
2010-02-22 23:46:32 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
ota_fclose(dev);
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2015-07-17 20:47:44 +02:00
|
|
|
if (!found) {
|
|
|
|
// Ran off the end of the list of (size,sha1) pairs without finding a match.
|
2015-06-24 08:23:33 +02:00
|
|
|
printf("contents of partition \"%s\" didn't match %s\n", partition, filename);
|
2010-02-22 23:46:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-02-04 09:23:21 +01:00
|
|
|
SHA1_Final(file->sha1, &sha_ctx);
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-02-11 01:41:10 +01:00
|
|
|
data.resize(data_size);
|
|
|
|
file->data = std::move(data);
|
2010-02-22 23:46:32 +01:00
|
|
|
// Fake some stat() info.
|
|
|
|
file->st.st_mode = 0644;
|
|
|
|
file->st.st_uid = 0;
|
|
|
|
file->st.st_gid = 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Save the contents of the given FileContents object under the given
|
|
|
|
// filename. Return 0 on success.
|
2012-02-28 20:07:09 +01:00
|
|
|
int SaveFileContents(const char* filename, const FileContents* file) {
|
2015-12-16 01:04:53 +01:00
|
|
|
int fd = ota_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR);
|
2010-02-22 23:46:32 +01:00
|
|
|
if (fd < 0) {
|
2015-06-24 08:23:33 +02:00
|
|
|
printf("failed to open \"%s\" for write: %s\n", filename, strerror(errno));
|
2010-02-22 23:46:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-02-11 01:41:10 +01:00
|
|
|
ssize_t bytes_written = FileSink(file->data.data(), file->data.size(), &fd);
|
|
|
|
if (bytes_written != static_cast<ssize_t>(file->data.size())) {
|
|
|
|
printf("short write of \"%s\" (%zd bytes of %zu) (%s)\n",
|
|
|
|
filename, bytes_written, file->data.size(), strerror(errno));
|
2015-12-16 01:04:53 +01:00
|
|
|
ota_close(fd);
|
2010-02-22 23:46:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-12-16 01:04:53 +01:00
|
|
|
if (ota_fsync(fd) != 0) {
|
2014-10-29 20:42:15 +01:00
|
|
|
printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2015-12-16 01:04:53 +01:00
|
|
|
if (ota_close(fd) != 0) {
|
2014-10-29 20:42:15 +01:00
|
|
|
printf("close of \"%s\" failed: %s\n", filename, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2012-02-28 20:07:09 +01:00
|
|
|
if (chmod(filename, file->st.st_mode) != 0) {
|
2010-02-22 23:46:32 +01:00
|
|
|
printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-28 20:07:09 +01:00
|
|
|
if (chown(filename, file->st.st_uid, file->st.st_gid) != 0) {
|
2010-02-22 23:46:32 +01:00
|
|
|
printf("chown of \"%s\" failed: %s\n", filename, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2010-07-07 22:55:25 +02:00
|
|
|
// Write a memory buffer to 'target' partition, a string of the form
|
2016-06-10 02:41:22 +02:00
|
|
|
// "EMMC:<partition_device>[:...]". The target name
|
2015-07-25 00:29:12 +02:00
|
|
|
// might contain multiple colons, but WriteToPartition() only uses the first
|
|
|
|
// two and ignores the rest. Return 0 on success.
|
2016-02-04 02:08:52 +01:00
|
|
|
int WriteToPartition(const unsigned char* data, size_t len, const char* target) {
|
2015-07-17 20:47:44 +02:00
|
|
|
std::string copy(target);
|
|
|
|
std::vector<std::string> pieces = android::base::Split(copy, ":");
|
|
|
|
|
2015-07-25 00:29:12 +02:00
|
|
|
if (pieces.size() < 2) {
|
2015-07-17 20:47:44 +02:00
|
|
|
printf("WriteToPartition called with bad target (%s)\n", target);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
if (pieces[0] != "EMMC") {
|
2010-07-07 22:55:25 +02:00
|
|
|
printf("WriteToPartition called with bad target (%s)\n", target);
|
2010-02-22 23:46:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-07-17 20:47:44 +02:00
|
|
|
const char* partition = pieces[1].c_str();
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
size_t start = 0;
|
|
|
|
bool success = false;
|
|
|
|
int fd = ota_open(partition, O_RDWR | O_SYNC);
|
|
|
|
if (fd < 0) {
|
|
|
|
printf("failed to open %s: %s\n", partition, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2010-07-07 22:55:25 +02:00
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
for (size_t attempt = 0; attempt < 2; ++attempt) {
|
|
|
|
if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
|
|
|
|
printf("failed seek on %s: %s\n", partition, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (start < len) {
|
|
|
|
size_t to_write = len - start;
|
|
|
|
if (to_write > 1<<20) to_write = 1<<20;
|
2010-07-07 22:55:25 +02:00
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
ssize_t written = TEMP_FAILURE_RETRY(ota_write(fd, data+start, to_write));
|
|
|
|
if (written == -1) {
|
|
|
|
printf("failed write writing to %s: %s\n", partition, strerror(errno));
|
2010-07-07 22:55:25 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2016-06-10 02:41:22 +02:00
|
|
|
start += written;
|
|
|
|
}
|
|
|
|
if (ota_fsync(fd) != 0) {
|
|
|
|
printf("failed to sync to %s (%s)\n", partition, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ota_close(fd) != 0) {
|
|
|
|
printf("failed to close %s (%s)\n", partition, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fd = ota_open(partition, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
printf("failed to reopen %s for verify (%s)\n", partition, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2010-07-07 22:55:25 +02:00
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
// Drop caches so our subsequent verification read
|
|
|
|
// won't just be reading the cache.
|
|
|
|
sync();
|
|
|
|
int dc = ota_open("/proc/sys/vm/drop_caches", O_WRONLY);
|
|
|
|
if (TEMP_FAILURE_RETRY(ota_write(dc, "3\n", 2)) == -1) {
|
|
|
|
printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno));
|
|
|
|
} else {
|
|
|
|
printf(" caches dropped\n");
|
2015-06-24 08:23:33 +02:00
|
|
|
}
|
2016-06-10 02:41:22 +02:00
|
|
|
ota_close(dc);
|
|
|
|
sleep(1);
|
2010-07-07 22:55:25 +02:00
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
// verify
|
|
|
|
if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
|
|
|
|
printf("failed to seek back to beginning of %s: %s\n",
|
|
|
|
partition, strerror(errno));
|
|
|
|
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);
|
2013-07-08 18:42:54 +02:00
|
|
|
}
|
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
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) {
|
|
|
|
printf("verify read error %s at %zu: %s\n",
|
|
|
|
partition, p, strerror(errno));
|
2015-04-29 02:24:24 +02:00
|
|
|
return -1;
|
2016-09-01 03:06:33 +02:00
|
|
|
} else if (read_count == 0) {
|
|
|
|
printf("verify read reached unexpected EOF, %s at %zu\n", partition, p);
|
|
|
|
return -1;
|
2015-04-29 02:24:24 +02:00
|
|
|
}
|
2016-06-10 02:41:22 +02:00
|
|
|
if (static_cast<size_t>(read_count) < to_read) {
|
|
|
|
printf("short verify read %s at %zu: %zd %zu %s\n",
|
|
|
|
partition, p, read_count, to_read, strerror(errno));
|
2013-07-08 18:42:54 +02:00
|
|
|
}
|
2016-06-10 02:41:22 +02:00
|
|
|
so_far += read_count;
|
2013-07-08 18:42:54 +02:00
|
|
|
}
|
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
if (memcmp(buffer, data+p, to_read) != 0) {
|
|
|
|
printf("verification failed starting at %zu\n", p);
|
|
|
|
start = p;
|
|
|
|
break;
|
2010-07-07 22:55:25 +02:00
|
|
|
}
|
2016-06-10 02:41:22 +02:00
|
|
|
}
|
2013-07-08 18:42:54 +02:00
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
if (start == len) {
|
|
|
|
printf("verification read succeeded (attempt %zu)\n", attempt+1);
|
|
|
|
success = true;
|
2010-07-07 22:55:25 +02:00
|
|
|
break;
|
2013-07-08 18:42:54 +02:00
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
if (!success) {
|
|
|
|
printf("failed to verify after all attempts\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ota_close(fd) != 0) {
|
|
|
|
printf("error closing %s (%s)\n", partition, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sync();
|
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Take a string 'str' of 40 hex digits and parse it into the 20
|
|
|
|
// byte array 'digest'. 'str' may contain only the digest or be of
|
|
|
|
// the form "<digest>:<anything>". Return 0 on success, -1 on any
|
|
|
|
// error.
|
|
|
|
int ParseSha1(const char* str, uint8_t* digest) {
|
2010-02-22 23:46:32 +01:00
|
|
|
const char* ps = str;
|
|
|
|
uint8_t* pd = digest;
|
2016-02-04 09:23:21 +01:00
|
|
|
for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
|
2010-02-22 23:46:32 +01:00
|
|
|
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
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
if (*ps != '\0') return -1;
|
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
// Search an array of sha1 strings for one matching the given sha1.
|
|
|
|
// Return the index of the match on success, or -1 if no match is
|
|
|
|
// found.
|
2013-07-08 18:42:54 +02:00
|
|
|
int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str,
|
2010-02-22 23:46:32 +01:00
|
|
|
int num_patches) {
|
2016-02-04 09:23:21 +01:00
|
|
|
uint8_t patch_sha1[SHA_DIGEST_LENGTH];
|
2015-06-24 08:23:33 +02:00
|
|
|
for (int i = 0; i < num_patches; ++i) {
|
2010-02-22 23:46:32 +01:00
|
|
|
if (ParseSha1(patch_sha1_str[i], patch_sha1) == 0 &&
|
2016-02-04 09:23:21 +01:00
|
|
|
memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
|
2010-02-22 23:46:32 +01:00
|
|
|
return i;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
return -1;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns 0 if the contents of the file (argv[2]) or the cached file
|
|
|
|
// match any of the sha1's on the command line (argv[3:]). Returns
|
|
|
|
// nonzero otherwise.
|
2015-06-24 08:23:33 +02:00
|
|
|
int applypatch_check(const char* filename, int num_patches,
|
|
|
|
char** const patch_sha1_str) {
|
2010-02-22 23:46:32 +01:00
|
|
|
FileContents file;
|
|
|
|
|
|
|
|
// It's okay to specify no sha1s; the check will pass if the
|
2010-07-07 22:55:25 +02:00
|
|
|
// LoadFileContents is successful. (Useful for reading
|
2010-02-22 23:46:32 +01:00
|
|
|
// partitions, where the filename encodes the sha1s; no need to
|
|
|
|
// check them twice.)
|
2014-02-14 00:18:19 +01:00
|
|
|
if (LoadFileContents(filename, &file) != 0 ||
|
2010-02-22 23:46:32 +01:00
|
|
|
(num_patches > 0 &&
|
|
|
|
FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0)) {
|
|
|
|
printf("file \"%s\" doesn't have any of expected "
|
|
|
|
"sha1 sums; checking cache\n", filename);
|
|
|
|
|
|
|
|
// If the source file is missing or corrupted, it might be because
|
|
|
|
// we were killed in the middle of patching it. A copy of it
|
|
|
|
// should have been made in CACHE_TEMP_SOURCE. If that file
|
|
|
|
// exists and matches the sha1 we're looking for, the check still
|
|
|
|
// passes.
|
|
|
|
|
2014-02-14 00:18:19 +01:00
|
|
|
if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
|
2010-02-22 23:46:32 +01:00
|
|
|
printf("failed to load cache file\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
if (FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0) {
|
|
|
|
printf("cache bits don't match any sha1 for \"%s\"\n", filename);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ShowLicenses() {
|
2010-02-22 23:46:32 +01:00
|
|
|
ShowBSDiffLicense();
|
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2014-08-15 23:31:52 +02:00
|
|
|
ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) {
|
2016-02-04 02:08:52 +01:00
|
|
|
int fd = *static_cast<int*>(token);
|
2010-02-22 23:46:32 +01:00
|
|
|
ssize_t done = 0;
|
|
|
|
ssize_t wrote;
|
2015-06-24 08:23:33 +02:00
|
|
|
while (done < len) {
|
2015-12-16 01:04:53 +01:00
|
|
|
wrote = TEMP_FAILURE_RETRY(ota_write(fd, data+done, len-done));
|
2015-04-29 02:24:24 +02:00
|
|
|
if (wrote == -1) {
|
2015-06-24 08:23:33 +02:00
|
|
|
printf("error writing %zd bytes: %s\n", (len-done), strerror(errno));
|
2010-02-22 23:46:32 +01:00
|
|
|
return done;
|
|
|
|
}
|
|
|
|
done += wrote;
|
|
|
|
}
|
|
|
|
return done;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2014-08-15 23:31:52 +02:00
|
|
|
ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) {
|
2016-02-04 02:08:52 +01:00
|
|
|
std::string* s = static_cast<std::string*>(token);
|
|
|
|
s->append(reinterpret_cast<const char*>(data), len);
|
2010-02-22 23:46:32 +01:00
|
|
|
return len;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the amount of free space (in bytes) on the filesystem
|
|
|
|
// containing filename. filename must exist. Return -1 on error.
|
|
|
|
size_t FreeSpaceForFile(const char* filename) {
|
2010-02-22 23:46:32 +01:00
|
|
|
struct statfs sf;
|
|
|
|
if (statfs(filename, &sf) != 0) {
|
|
|
|
printf("failed to statfs %s: %s\n", filename, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-19 11:21:00 +02:00
|
|
|
return sf.f_bsize * sf.f_bavail;
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CacheSizeCheck(size_t bytes) {
|
|
|
|
if (MakeFreeSpaceOnCache(bytes) < 0) {
|
2016-04-18 20:30:55 +02:00
|
|
|
printf("unable to make %zu bytes available on /cache\n", bytes);
|
2010-02-22 23:46:32 +01:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
// This function applies binary patches to files in a way that is safe
|
2010-02-18 01:11:44 +01:00
|
|
|
// (the original file is not touched until we have the desired
|
|
|
|
// replacement for it) and idempotent (it's okay to run this program
|
|
|
|
// multiple times).
|
|
|
|
//
|
2010-02-22 23:46:32 +01:00
|
|
|
// - if the sha1 hash of <target_filename> is <target_sha1_string>,
|
|
|
|
// does nothing and exits successfully.
|
2010-02-18 01:11:44 +01:00
|
|
|
//
|
2010-02-22 23:46:32 +01:00
|
|
|
// - otherwise, if the sha1 hash of <source_filename> is one of the
|
|
|
|
// entries in <patch_sha1_str>, the corresponding patch from
|
|
|
|
// <patch_data> (which must be a VAL_BLOB) is applied to produce a
|
|
|
|
// new file (the type of patch is automatically detected from the
|
2015-07-18 03:11:12 +02:00
|
|
|
// blob data). If that new file has sha1 hash <target_sha1_str>,
|
2010-02-22 23:46:32 +01:00
|
|
|
// moves it to replace <target_filename>, and exits successfully.
|
|
|
|
// Note that if <source_filename> and <target_filename> are not the
|
|
|
|
// same, <source_filename> is NOT deleted on success.
|
|
|
|
// <target_filename> may be the string "-" to mean "the same as
|
|
|
|
// source_filename".
|
2010-02-18 01:11:44 +01:00
|
|
|
//
|
|
|
|
// - otherwise, or if any error is encountered, exits with non-zero
|
|
|
|
// status.
|
|
|
|
//
|
2010-07-07 22:55:25 +02:00
|
|
|
// <source_filename> may refer to a partition to read the source data.
|
2015-07-18 03:11:12 +02:00
|
|
|
// See the comments for the LoadPartitionContents() function above
|
2010-02-22 23:46:32 +01:00
|
|
|
// for the format of such a filename.
|
|
|
|
|
|
|
|
int applypatch(const char* source_filename,
|
|
|
|
const char* target_filename,
|
|
|
|
const char* target_sha1_str,
|
|
|
|
size_t target_size,
|
|
|
|
int num_patches,
|
|
|
|
char** const patch_sha1_str,
|
2012-08-21 00:28:02 +02:00
|
|
|
Value** patch_data,
|
|
|
|
Value* bonus_data) {
|
2012-10-19 21:24:26 +02:00
|
|
|
printf("patch %s: ", source_filename);
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2015-06-24 08:23:33 +02:00
|
|
|
if (target_filename[0] == '-' && target_filename[1] == '\0') {
|
2010-02-22 23:46:32 +01:00
|
|
|
target_filename = source_filename;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-02-04 09:23:21 +01:00
|
|
|
uint8_t target_sha1[SHA_DIGEST_LENGTH];
|
2010-02-22 23:46:32 +01:00
|
|
|
if (ParseSha1(target_sha1_str, target_sha1) != 0) {
|
|
|
|
printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
FileContents copy_file;
|
|
|
|
FileContents source_file;
|
|
|
|
const Value* source_patch_value = NULL;
|
|
|
|
const Value* copy_patch_value = NULL;
|
|
|
|
|
|
|
|
// We try to load the target file into the source_file object.
|
2014-02-14 00:18:19 +01:00
|
|
|
if (LoadFileContents(target_filename, &source_file) == 0) {
|
2016-02-04 09:23:21 +01:00
|
|
|
if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
|
2010-02-22 23:46:32 +01:00
|
|
|
// The early-exit case: the patch was already applied, this file
|
|
|
|
// has the desired hash, nothing for us to do.
|
2015-07-18 03:11:12 +02:00
|
|
|
printf("already %s\n", short_sha1(target_sha1).c_str());
|
2010-02-22 23:46:32 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-02-11 01:41:10 +01:00
|
|
|
if (source_file.data.empty() ||
|
2010-02-22 23:46:32 +01:00
|
|
|
(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 source file.
|
2016-02-11 01:41:10 +01:00
|
|
|
source_file.data.clear();
|
2014-02-14 00:18:19 +01:00
|
|
|
LoadFileContents(source_filename, &source_file);
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2016-02-11 01:41:10 +01:00
|
|
|
if (!source_file.data.empty()) {
|
2015-06-24 08:23:33 +02:00
|
|
|
int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str, num_patches);
|
2010-02-22 23:46:32 +01:00
|
|
|
if (to_use >= 0) {
|
|
|
|
source_patch_value = patch_data[to_use];
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
if (source_patch_value == NULL) {
|
2016-02-11 01:41:10 +01:00
|
|
|
source_file.data.clear();
|
2010-02-22 23:46:32 +01:00
|
|
|
printf("source file is bad; trying copy\n");
|
|
|
|
|
2014-02-14 00:18:19 +01:00
|
|
|
if (LoadFileContents(CACHE_TEMP_SOURCE, ©_file) < 0) {
|
2010-02-22 23:46:32 +01:00
|
|
|
// fail.
|
|
|
|
printf("failed to read copy file\n");
|
|
|
|
return 1;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2015-06-24 08:23:33 +02:00
|
|
|
int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str, num_patches);
|
2010-08-13 02:38:09 +02:00
|
|
|
if (to_use >= 0) {
|
2010-02-22 23:46:32 +01:00
|
|
|
copy_patch_value = patch_data[to_use];
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
if (copy_patch_value == NULL) {
|
|
|
|
// fail.
|
|
|
|
printf("copy file doesn't match source SHA-1s either\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2016-02-11 01:41:10 +01:00
|
|
|
return GenerateTarget(&source_file, source_patch_value,
|
|
|
|
©_file, copy_patch_value,
|
|
|
|
source_filename, target_filename,
|
|
|
|
target_sha1, target_size, bonus_data);
|
2012-02-28 20:07:09 +01:00
|
|
|
}
|
|
|
|
|
2015-07-18 03:11:12 +02:00
|
|
|
/*
|
|
|
|
* This function flashes a given image to the target partition. It verifies
|
|
|
|
* the target cheksum first, and will return if target has the desired hash.
|
|
|
|
* It checks the checksum of the given source image before flashing, and
|
|
|
|
* verifies the target partition afterwards. The function is idempotent.
|
|
|
|
* Returns zero on success.
|
|
|
|
*/
|
|
|
|
int applypatch_flash(const char* source_filename, const char* target_filename,
|
|
|
|
const char* target_sha1_str, size_t target_size) {
|
|
|
|
printf("flash %s: ", target_filename);
|
|
|
|
|
2016-02-04 09:23:21 +01:00
|
|
|
uint8_t target_sha1[SHA_DIGEST_LENGTH];
|
2015-07-18 03:11:12 +02:00
|
|
|
if (ParseSha1(target_sha1_str, target_sha1) != 0) {
|
|
|
|
printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileContents source_file;
|
|
|
|
std::string target_str(target_filename);
|
|
|
|
|
|
|
|
std::vector<std::string> pieces = android::base::Split(target_str, ":");
|
2016-06-10 02:41:22 +02:00
|
|
|
if (pieces.size() != 2 || pieces[0] != "EMMC") {
|
2015-07-18 03:11:12 +02:00
|
|
|
printf("invalid target name \"%s\"", target_filename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, ':');
|
|
|
|
if (LoadPartitionContents(fullname.c_str(), &source_file) == 0 &&
|
2016-02-04 09:23:21 +01:00
|
|
|
memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
|
2015-07-18 03:11:12 +02:00
|
|
|
// The early-exit case: the image was already applied, this partition
|
|
|
|
// has the desired hash, nothing for us to do.
|
|
|
|
printf("already %s\n", short_sha1(target_sha1).c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LoadFileContents(source_filename, &source_file) == 0) {
|
2016-02-04 09:23:21 +01:00
|
|
|
if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
|
2015-07-18 03:11:12 +02:00
|
|
|
// The source doesn't have desired checksum.
|
|
|
|
printf("source \"%s\" doesn't have expected sha1 sum\n", source_filename);
|
|
|
|
printf("expected: %s, found: %s\n", short_sha1(target_sha1).c_str(),
|
|
|
|
short_sha1(source_file.sha1).c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 01:41:10 +01:00
|
|
|
if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
|
2015-07-18 03:11:12 +02:00
|
|
|
printf("write of copied data to %s failed\n", target_filename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-28 20:07:09 +01:00
|
|
|
static int GenerateTarget(FileContents* source_file,
|
|
|
|
const Value* source_patch_value,
|
|
|
|
FileContents* copy_file,
|
|
|
|
const Value* copy_patch_value,
|
|
|
|
const char* source_filename,
|
|
|
|
const char* target_filename,
|
2016-02-04 09:23:21 +01:00
|
|
|
const uint8_t target_sha1[SHA_DIGEST_LENGTH],
|
2012-08-21 00:28:02 +02:00
|
|
|
size_t target_size,
|
|
|
|
const Value* bonus_data) {
|
2010-02-22 23:46:32 +01:00
|
|
|
int retry = 1;
|
|
|
|
SHA_CTX ctx;
|
2016-02-04 02:08:52 +01:00
|
|
|
std::string memory_sink_str;
|
2010-02-22 23:46:32 +01:00
|
|
|
FileContents* source_to_use;
|
2012-02-28 20:07:09 +01:00
|
|
|
int made_copy = 0;
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
bool target_is_partition = (strncmp(target_filename, "EMMC:", 5) == 0);
|
2016-02-04 02:08:52 +01:00
|
|
|
const std::string tmp_target_filename = std::string(target_filename) + ".patch";
|
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
// assume that target_filename (eg "/system/app/Foo.apk") is located
|
|
|
|
// on the same filesystem as its top-level directory ("/system").
|
|
|
|
// We need something that exists for calling statfs().
|
2016-02-04 02:08:52 +01:00
|
|
|
std::string target_fs = target_filename;
|
|
|
|
auto slash_pos = target_fs.find('/', 1);
|
|
|
|
if (slash_pos != std::string::npos) {
|
|
|
|
target_fs.resize(slash_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Value* patch;
|
|
|
|
if (source_patch_value != NULL) {
|
|
|
|
source_to_use = source_file;
|
|
|
|
patch = source_patch_value;
|
2010-02-18 01:11:44 +01:00
|
|
|
} else {
|
2016-02-04 02:08:52 +01:00
|
|
|
source_to_use = copy_file;
|
|
|
|
patch = copy_patch_value;
|
|
|
|
}
|
|
|
|
if (patch->type != VAL_BLOB) {
|
|
|
|
printf("patch is not a blob\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
char* header = patch->data;
|
|
|
|
ssize_t header_bytes_read = patch->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 {
|
|
|
|
printf("Unknown patch file format\n");
|
|
|
|
return 1;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2010-02-22 23:46:32 +01:00
|
|
|
do {
|
|
|
|
// Is there enough room in the target filesystem to hold the patched
|
|
|
|
// file?
|
|
|
|
|
2016-02-04 02:08:52 +01:00
|
|
|
if (target_is_partition) {
|
2010-07-07 22:55:25 +02:00
|
|
|
// If the target is a partition, we're actually going to
|
|
|
|
// write the output to /tmp and then copy it to the
|
|
|
|
// partition. statfs() always returns 0 blocks free for
|
|
|
|
// /tmp, so instead we'll just assume that /tmp has enough
|
|
|
|
// space to hold the file.
|
2010-02-22 23:46:32 +01:00
|
|
|
|
2010-07-07 22:55:25 +02:00
|
|
|
// We still write the original source to cache, in case
|
|
|
|
// the partition write is interrupted.
|
2016-02-11 01:41:10 +01:00
|
|
|
if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) {
|
2010-02-22 23:46:32 +01:00
|
|
|
printf("not enough free space on /cache\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
|
|
|
|
printf("failed to back up source file\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
made_copy = 1;
|
|
|
|
retry = 0;
|
|
|
|
} else {
|
|
|
|
int enough_space = 0;
|
|
|
|
if (retry > 0) {
|
2016-02-04 02:08:52 +01:00
|
|
|
size_t free_space = FreeSpaceForFile(target_fs.c_str());
|
2010-08-13 18:41:21 +02:00
|
|
|
enough_space =
|
|
|
|
(free_space > (256 << 10)) && // 256k (two-block) minimum
|
2010-02-22 23:46:32 +01:00
|
|
|
(free_space > (target_size * 3 / 2)); // 50% margin of error
|
2012-10-19 21:24:26 +02:00
|
|
|
if (!enough_space) {
|
2015-06-24 08:23:33 +02:00
|
|
|
printf("target %zu bytes; free space %zu bytes; retry %d; enough %d\n",
|
|
|
|
target_size, free_space, retry, enough_space);
|
2012-10-19 21:24:26 +02:00
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!enough_space) {
|
|
|
|
retry = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!enough_space && source_patch_value != NULL) {
|
|
|
|
// Using the original source, but not enough free space. First
|
|
|
|
// copy the source file to cache, then delete it from the original
|
|
|
|
// location.
|
|
|
|
|
2016-06-10 02:41:22 +02:00
|
|
|
if (strncmp(source_filename, "EMMC:", 5) == 0) {
|
2010-02-22 23:46:32 +01:00
|
|
|
// It's impossible to free space on the target filesystem by
|
2010-07-07 22:55:25 +02:00
|
|
|
// deleting the source if the source is a partition. If
|
2010-02-22 23:46:32 +01:00
|
|
|
// we're ever in a state where we need to do this, fail.
|
2015-06-24 08:23:33 +02:00
|
|
|
printf("not enough free space for target but source is partition\n");
|
2010-02-22 23:46:32 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-02-11 01:41:10 +01:00
|
|
|
if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) {
|
2010-02-22 23:46:32 +01:00
|
|
|
printf("not enough free space on /cache\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
|
|
|
|
printf("failed to back up source file\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
made_copy = 1;
|
|
|
|
unlink(source_filename);
|
|
|
|
|
2016-02-04 02:08:52 +01:00
|
|
|
size_t free_space = FreeSpaceForFile(target_fs.c_str());
|
2015-06-24 08:23:33 +02:00
|
|
|
printf("(now %zu bytes free for target) ", free_space);
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SinkFn sink = NULL;
|
|
|
|
void* token = NULL;
|
2016-02-04 02:08:52 +01:00
|
|
|
int output_fd = -1;
|
|
|
|
if (target_is_partition) {
|
2010-02-22 23:46:32 +01:00
|
|
|
// We store the decoded output in memory.
|
|
|
|
sink = MemorySink;
|
2016-02-04 02:08:52 +01:00
|
|
|
token = &memory_sink_str;
|
2010-02-22 23:46:32 +01:00
|
|
|
} else {
|
|
|
|
// We write the decoded output to "<tgt-file>.patch".
|
2015-12-16 01:04:53 +01:00
|
|
|
output_fd = ota_open(tmp_target_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
|
|
|
|
S_IRUSR | S_IWUSR);
|
2016-02-04 02:08:52 +01:00
|
|
|
if (output_fd < 0) {
|
|
|
|
printf("failed to open output file %s: %s\n", tmp_target_filename.c_str(),
|
|
|
|
strerror(errno));
|
2010-02-22 23:46:32 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
sink = FileSink;
|
2016-02-04 02:08:52 +01:00
|
|
|
token = &output_fd;
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-04 09:23:21 +01:00
|
|
|
SHA1_Init(&ctx);
|
2010-02-22 23:46:32 +01:00
|
|
|
|
|
|
|
int result;
|
2016-02-04 02:08:52 +01:00
|
|
|
if (use_bsdiff) {
|
2016-02-11 01:41:10 +01:00
|
|
|
result = ApplyBSDiffPatch(source_to_use->data.data(), source_to_use->data.size(),
|
2010-02-22 23:46:32 +01:00
|
|
|
patch, 0, sink, token, &ctx);
|
2016-02-04 02:08:52 +01:00
|
|
|
} else {
|
2016-02-11 01:41:10 +01:00
|
|
|
result = ApplyImagePatch(source_to_use->data.data(), source_to_use->data.size(),
|
2012-08-21 00:28:02 +02:00
|
|
|
patch, sink, token, &ctx, bonus_data);
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
|
|
|
|
2016-02-04 02:08:52 +01:00
|
|
|
if (!target_is_partition) {
|
2015-12-16 01:04:53 +01:00
|
|
|
if (ota_fsync(output_fd) != 0) {
|
2016-02-04 02:08:52 +01:00
|
|
|
printf("failed to fsync file \"%s\" (%s)\n", tmp_target_filename.c_str(),
|
|
|
|
strerror(errno));
|
2014-10-29 20:42:15 +01:00
|
|
|
result = 1;
|
|
|
|
}
|
2015-12-16 01:04:53 +01:00
|
|
|
if (ota_close(output_fd) != 0) {
|
2016-02-04 02:08:52 +01:00
|
|
|
printf("failed to close file \"%s\" (%s)\n", tmp_target_filename.c_str(),
|
|
|
|
strerror(errno));
|
2014-10-29 20:42:15 +01:00
|
|
|
result = 1;
|
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result != 0) {
|
|
|
|
if (retry == 0) {
|
|
|
|
printf("applying patch failed\n");
|
|
|
|
return result != 0;
|
|
|
|
} else {
|
|
|
|
printf("applying patch failed; retrying\n");
|
|
|
|
}
|
2016-02-04 02:08:52 +01:00
|
|
|
if (!target_is_partition) {
|
|
|
|
unlink(tmp_target_filename.c_str());
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// succeeded; no need to retry
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (retry-- > 0);
|
|
|
|
|
2016-02-04 09:23:21 +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) {
|
2010-02-22 23:46:32 +01:00
|
|
|
printf("patch did not produce expected sha1\n");
|
|
|
|
return 1;
|
2012-10-19 21:24:26 +02:00
|
|
|
} else {
|
2015-07-18 03:11:12 +02:00
|
|
|
printf("now %s\n", short_sha1(target_sha1).c_str());
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2016-02-04 02:08:52 +01:00
|
|
|
if (target_is_partition) {
|
2010-07-07 22:55:25 +02:00
|
|
|
// Copy the temp file to the partition.
|
2016-02-04 02:08:52 +01:00
|
|
|
if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
|
|
|
|
memory_sink_str.size(), target_filename) != 0) {
|
2010-02-22 23:46:32 +01:00
|
|
|
printf("write of patched data to %s failed\n", target_filename);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
} else {
|
2010-02-22 23:46:32 +01:00
|
|
|
// Give the .patch file the same owner, group, and mode of the
|
|
|
|
// original source file.
|
2016-02-04 02:08:52 +01:00
|
|
|
if (chmod(tmp_target_filename.c_str(), source_to_use->st.st_mode) != 0) {
|
|
|
|
printf("chmod of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno));
|
2010-02-22 23:46:32 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2016-02-04 02:08:52 +01:00
|
|
|
if (chown(tmp_target_filename.c_str(), source_to_use->st.st_uid, source_to_use->st.st_gid) != 0) {
|
|
|
|
printf("chown of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno));
|
2010-02-22 23:46:32 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, rename the .patch file to replace the target file.
|
2016-02-04 02:08:52 +01:00
|
|
|
if (rename(tmp_target_filename.c_str(), target_filename) != 0) {
|
2015-06-24 08:23:33 +02:00
|
|
|
printf("rename of .patch to \"%s\" failed: %s\n", target_filename, strerror(errno));
|
2010-02-22 23:46:32 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this run of applypatch created the copy, and we're here, we
|
|
|
|
// can delete it.
|
2015-06-24 08:23:33 +02:00
|
|
|
if (made_copy) {
|
|
|
|
unlink(CACHE_TEMP_SOURCE);
|
|
|
|
}
|
2010-02-22 23:46:32 +01:00
|
|
|
|
|
|
|
// Success!
|
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|