summaryrefslogtreecommitdiffstats
path: root/applypatch/applypatch.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--applypatch/applypatch.cpp (renamed from applypatch/applypatch.c)654
1 files changed, 303 insertions, 351 deletions
diff --git a/applypatch/applypatch.c b/applypatch/applypatch.cpp
index bc45e3c45..cc2858551 100644
--- a/applypatch/applypatch.c
+++ b/applypatch/applypatch.cpp
@@ -15,6 +15,7 @@
*/
#include <errno.h>
+#include <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
@@ -22,15 +23,20 @@
#include <sys/stat.h>
#include <sys/statfs.h>
#include <sys/types.h>
-#include <fcntl.h>
#include <unistd.h>
-#include <stdbool.h>
-#include "mincrypt/sha.h"
+#include <memory>
+#include <string>
+
+#include <android-base/strings.h>
+
+#include "openssl/sha.h"
#include "applypatch.h"
#include "bmlutils/bmlutils.h"
#include "mtdutils/mtdutils.h"
#include "edify/expr.h"
+#include "ota_io.h"
+#include "print_sha1.h"
static int LoadPartitionContents(const char* filename, FileContents* file);
static ssize_t FileSink(const unsigned char* data, ssize_t len, void* token);
@@ -40,19 +46,17 @@ static int GenerateTarget(FileContents* source_file,
const Value* copy_patch_value,
const char* source_filename,
const char* target_filename,
- const uint8_t target_sha1[SHA_DIGEST_SIZE],
+ const uint8_t target_sha1[SHA_DIGEST_LENGTH],
size_t target_size,
const Value* bonus_data);
-static int mtd_partitions_scanned = 0;
+static bool mtd_partitions_scanned = false;
// Read a file into memory; store the file contents and associated
// metadata in *file.
//
// Return 0 on success.
int LoadFileContents(const char* filename, FileContents* file) {
- file->data = NULL;
-
// A special 'filename' beginning with "MTD:" or "EMMC:" means to
// load the contents of a partition.
if (strncmp(filename, "MTD:", 4) == 0 ||
@@ -66,46 +70,25 @@ int LoadFileContents(const char* filename, FileContents* file) {
return -1;
}
- file->size = file->st.st_size;
- file->data = malloc(file->size);
-
- FILE* f = fopen(filename, "rb");
+ std::vector<unsigned char> data(file->st.st_size);
+ FILE* f = ota_fopen(filename, "rb");
if (f == NULL) {
printf("failed to open \"%s\": %s\n", filename, strerror(errno));
- free(file->data);
- file->data = NULL;
return -1;
}
- ssize_t bytes_read = fread(file->data, 1, file->size, f);
- if (bytes_read != file->size) {
- printf("short read of \"%s\" (%ld bytes of %ld)\n",
- filename, (long)bytes_read, (long)file->size);
- free(file->data);
- file->data = NULL;
+ 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 %zd)\n", filename, bytes_read, data.size());
+ ota_fclose(f);
return -1;
}
- fclose(f);
-
- SHA_hash(file->data, file->size, file->sha1);
+ ota_fclose(f);
+ file->data = std::move(data);
+ SHA1(file->data.data(), file->data.size(), file->sha1);
return 0;
}
-static size_t* size_array;
-// comparison function for qsort()ing an int array of indexes into
-// size_array[].
-static int compare_size_indices(const void* a, const void* b) {
- int aa = *(int*)a;
- int bb = *(int*)b;
- if (size_array[aa] < size_array[bb]) {
- return -1;
- } else if (size_array[aa] > size_array[bb]) {
- return 1;
- } else {
- return 0;
- }
-}
-
// Load the contents of an MTD or EMMC partition into the provided
// FileContents. filename should be a string of the form
// "MTD:<partition_name>:<size_1>:<sha1_1>:<size_2>:<sha1_2>:..." (or
@@ -124,23 +107,25 @@ static int compare_size_indices(const void* a, const void* b) {
enum PartitionType { MTD, EMMC };
static int LoadPartitionContents(const char* filename, FileContents* file) {
- char* copy = strdup(filename);
- const char* magic = strtok(copy, ":");
+ 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;
+ }
enum PartitionType type;
-
- if (strcmp(magic, "MTD") == 0) {
+ if (pieces[0] == "MTD") {
type = MTD;
- } else if (strcmp(magic, "EMMC") == 0) {
+ } else if (pieces[0] == "EMMC") {
type = EMMC;
} else if (strcmp(magic, "BML") == 0) {
type = EMMC;
} else {
- printf("LoadPartitionContents called with bad filename (%s)\n",
- filename);
+ printf("LoadPartitionContents called with bad filename (%s)\n", filename);
return -1;
}
- const char* partition = strtok(NULL, ":");
+ const char* partition = pieces[1].c_str();
if (strcmp(magic, "BML") == 0) {
if (strcmp(partition, "boot") == 0) {
@@ -150,132 +135,115 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
}
}
- int i;
- int colons = 0;
- for (i = 0; filename[i] != '\0'; ++i) {
- if (filename[i] == ':') {
- ++colons;
- }
- }
- if (colons < 3 || colons%2 == 0) {
- printf("LoadPartitionContents called with bad filename (%s)\n",
- filename);
- }
-
- int pairs = (colons-1)/2; // # of (size,sha1) pairs in filename
- int* index = malloc(pairs * sizeof(int));
- size_t* size = malloc(pairs * sizeof(size_t));
- char** sha1sum = malloc(pairs * sizeof(char*));
+ 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);
- for (i = 0; i < pairs; ++i) {
- const char* size_str = strtok(NULL, ":");
- size[i] = strtol(size_str, NULL, 10);
+ for (size_t i = 0; i < pairs; ++i) {
+ size[i] = strtol(pieces[i*2+2].c_str(), NULL, 10);
if (size[i] == 0) {
printf("LoadPartitionContents called with bad size (%s)\n", filename);
return -1;
}
- sha1sum[i] = strtok(NULL, ":");
+ sha1sum[i] = pieces[i*2+3].c_str();
index[i] = i;
}
- // sort the index[] array so it indexes the pairs in order of
- // increasing size.
- size_array = size;
- qsort(index, pairs, sizeof(int), compare_size_indices);
+ // 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]);
+ }
+ );
MtdReadContext* ctx = NULL;
FILE* dev = NULL;
switch (type) {
- case MTD:
+ case MTD: {
if (!mtd_partitions_scanned) {
mtd_scan_partitions();
- mtd_partitions_scanned = 1;
+ mtd_partitions_scanned = true;
}
const MtdPartition* mtd = mtd_find_partition_by_name(partition);
if (mtd == NULL) {
- printf("mtd partition \"%s\" not found (loading %s)\n",
- partition, filename);
+ printf("mtd partition \"%s\" not found (loading %s)\n", partition, filename);
return -1;
}
ctx = mtd_read_partition(mtd);
if (ctx == NULL) {
- printf("failed to initialize read of mtd partition \"%s\"\n",
- partition);
+ printf("failed to initialize read of mtd partition \"%s\"\n", partition);
return -1;
}
break;
+ }
case EMMC:
- dev = fopen(partition, "rb");
+ dev = ota_fopen(partition, "rb");
if (dev == NULL) {
- printf("failed to open emmc partition \"%s\": %s\n",
- partition, strerror(errno));
+ printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno));
return -1;
}
}
SHA_CTX sha_ctx;
- SHA_init(&sha_ctx);
- uint8_t parsed_sha[SHA_DIGEST_SIZE];
-
- // allocate enough memory to hold the largest size.
- file->data = malloc(size[index[pairs-1]]);
- char* p = (char*)file->data;
- file->size = 0; // # bytes read so far
-
- for (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).
- size_t next = size[index[i]] - file->size;
- size_t read = 0;
+ SHA1_Init(&sha_ctx);
+ uint8_t parsed_sha[SHA_DIGEST_LENGTH];
+
+ // Allocate enough memory to hold the largest size.
+ 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
+ bool found = false;
+
+ 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).
+ size_t next = size[index[i]] - data_size;
if (next > 0) {
+ size_t read = 0;
switch (type) {
case MTD:
read = mtd_read_data(ctx, p, next);
break;
case EMMC:
- read = fread(p, 1, next, dev);
+ read = ota_fread(p, 1, next, dev);
break;
}
if (next != read) {
printf("short read (%zu bytes of %zu) for partition \"%s\"\n",
read, next, partition);
- free(file->data);
- file->data = NULL;
return -1;
}
- SHA_update(&sha_ctx, p, read);
- file->size += read;
+ SHA1_Update(&sha_ctx, p, read);
+ data_size += read;
+ p += read;
}
// 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));
- const uint8_t* sha_so_far = SHA_final(&temp_ctx);
+ uint8_t sha_so_far[SHA_DIGEST_LENGTH];
+ SHA1_Final(sha_so_far, &temp_ctx);
- if (ParseSha1(sha1sum[index[i]], parsed_sha) != 0) {
- printf("failed to parse sha1 %s in %s\n",
- sha1sum[index[i]], filename);
- free(file->data);
- file->data = NULL;
+ 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);
return -1;
}
- if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_SIZE) == 0) {
+ 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.
printf("partition read matched size %zu sha %s\n",
- size[index[i]], sha1sum[index[i]]);
+ size[index[i]], sha1sum[index[i]].c_str());
+ found = true;
break;
}
-
- p += read;
}
switch (type) {
@@ -284,36 +252,26 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
break;
case EMMC:
- fclose(dev);
+ ota_fclose(dev);
break;
}
- if (i == pairs) {
- // Ran off the end of the list of (size,sha1) pairs without
- // finding a match.
- printf("contents of partition \"%s\" didn't match %s\n",
- partition, filename);
- free(file->data);
- file->data = NULL;
+ if (!found) {
+ // Ran off the end of the list of (size,sha1) pairs without finding a match.
+ printf("contents of partition \"%s\" didn't match %s\n", partition, filename);
return -1;
}
- const uint8_t* sha_final = SHA_final(&sha_ctx);
- for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
- file->sha1[i] = sha_final[i];
- }
+ SHA1_Final(file->sha1, &sha_ctx);
+ data.resize(data_size);
+ file->data = std::move(data);
// Fake some stat() info.
file->st.st_mode = 0644;
file->st.st_uid = 0;
file->st.st_gid = 0;
- free(copy);
- free(index);
- free(size);
- free(sha1sum);
-
return 0;
}
@@ -321,26 +279,24 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
// Save the contents of the given FileContents object under the given
// filename. Return 0 on success.
int SaveFileContents(const char* filename, const FileContents* file) {
- int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR);
+ int fd = ota_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR);
if (fd < 0) {
- printf("failed to open \"%s\" for write: %s\n",
- filename, strerror(errno));
+ printf("failed to open \"%s\" for write: %s\n", filename, strerror(errno));
return -1;
}
- ssize_t bytes_written = FileSink(file->data, file->size, &fd);
- if (bytes_written != file->size) {
- printf("short write of \"%s\" (%ld bytes of %ld) (%s)\n",
- filename, (long)bytes_written, (long)file->size,
- strerror(errno));
- close(fd);
+ 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));
+ ota_close(fd);
return -1;
}
- if (fsync(fd) != 0) {
+ if (ota_fsync(fd) != 0) {
printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno));
return -1;
}
- if (close(fd) != 0) {
+ if (ota_close(fd) != 0) {
printf("close of \"%s\" failed: %s\n", filename, strerror(errno));
return -1;
}
@@ -358,17 +314,22 @@ int SaveFileContents(const char* filename, const FileContents* file) {
}
// Write a memory buffer to 'target' partition, a string of the form
-// "MTD:<partition>[:...]" or "EMMC:<partition_device>:". Return 0 on
-// success.
-int WriteToPartition(unsigned char* data, size_t len,
- const char* target) {
- char* copy = strdup(target);
- const char* magic = strtok(copy, ":");
+// "MTD:<partition>[:...]" or "EMMC:<partition_device>[:...]". The target name
+// might contain multiple colons, but WriteToPartition() only uses the first
+// two and ignores the rest. Return 0 on success.
+int WriteToPartition(const unsigned char* data, size_t len, const char* target) {
+ std::string copy(target);
+ std::vector<std::string> pieces = android::base::Split(copy, ":");
+
+ if (pieces.size() < 2) {
+ printf("WriteToPartition called with bad target (%s)\n", target);
+ return -1;
+ }
enum PartitionType type;
- if (strcmp(magic, "MTD") == 0) {
+ if (pieces[0] == "MTD") {
type = MTD;
- } else if (strcmp(magic, "EMMC") == 0) {
+ } else if (pieces[0] == "EMMC") {
type = EMMC;
} else if (strcmp(magic, "BML") == 0) {
type = EMMC;
@@ -376,7 +337,8 @@ int WriteToPartition(unsigned char* data, size_t len,
printf("WriteToPartition called with bad target (%s)\n", target);
return -1;
}
- const char* partition = strtok(NULL, ":");
+
+ const char* partition = pieces[1].c_str();
if (strcmp(magic, "BML") == 0) {
if (strcmp(partition, "boot") == 0) {
@@ -401,30 +363,27 @@ int WriteToPartition(unsigned char* data, size_t len,
}
switch (type) {
- case MTD:
+ case MTD: {
if (!mtd_partitions_scanned) {
mtd_scan_partitions();
- mtd_partitions_scanned = 1;
+ mtd_partitions_scanned = true;
}
const MtdPartition* mtd = mtd_find_partition_by_name(partition);
if (mtd == NULL) {
- printf("mtd partition \"%s\" not found for writing\n",
- partition);
+ printf("mtd partition \"%s\" not found for writing\n", partition);
return -1;
}
MtdWriteContext* ctx = mtd_write_partition(mtd);
if (ctx == NULL) {
- printf("failed to init mtd partition \"%s\" for writing\n",
- partition);
+ printf("failed to init mtd partition \"%s\" for writing\n", partition);
return -1;
}
- size_t written = mtd_write_data(ctx, (char*)data, len);
+ size_t written = mtd_write_data(ctx, reinterpret_cast<const char*>(data), len);
if (written != len) {
- printf("only wrote %zu of %zu bytes to MTD %s\n",
- written, len, partition);
+ printf("only wrote %zu of %zu bytes to MTD %s\n", written, len, partition);
mtd_write_close(ctx);
return -1;
}
@@ -440,62 +399,57 @@ int WriteToPartition(unsigned char* data, size_t len,
return -1;
}
break;
+ }
- case EMMC:
- {
+ case EMMC: {
size_t start = 0;
- int success = 0;
- int fd = open(partition, O_RDWR | O_SYNC);
+ 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;
}
- int attempt;
- for (attempt = 0; attempt < 2; ++attempt) {
+ 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));
+ 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;
- ssize_t written = TEMP_FAILURE_RETRY(write(fd, data+start, to_write));
+ 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));
return -1;
}
start += written;
}
- if (fsync(fd) != 0) {
- printf("failed to sync to %s (%s)\n",
- partition, strerror(errno));
+ if (ota_fsync(fd) != 0) {
+ printf("failed to sync to %s (%s)\n", partition, strerror(errno));
return -1;
}
- if (close(fd) != 0) {
- printf("failed to close %s (%s)\n",
- partition, strerror(errno));
+ if (ota_close(fd) != 0) {
+ printf("failed to close %s (%s)\n", partition, strerror(errno));
return -1;
}
- fd = open(partition, O_RDONLY);
+ fd = ota_open(partition, O_RDONLY);
if (fd < 0) {
- printf("failed to reopen %s for verify (%s)\n",
- partition, strerror(errno));
+ printf("failed to reopen %s for verify (%s)\n", partition, strerror(errno));
return -1;
}
- // drop caches so our subsequent verification read
+ // Drop caches so our subsequent verification read
// won't just be reading the cache.
sync();
- int dc = open("/proc/sys/vm/drop_caches", O_WRONLY);
- if (TEMP_FAILURE_RETRY(write(dc, "3\n", 2)) == -1) {
+ 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");
}
- close(dc);
+ ota_close(dc);
sleep(1);
// verify
@@ -506,28 +460,29 @@ int WriteToPartition(unsigned char* data, size_t len,
}
unsigned char buffer[4096];
start = len;
- size_t p;
- for (p = 0; p < len; p += sizeof(buffer)) {
+ 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);
+ 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(read(fd, buffer+so_far, to_read-so_far));
+ 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));
return -1;
}
- if ((size_t)read_count < to_read) {
+ 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));
}
so_far += read_count;
}
- if (memcmp(buffer, data+p, to_read)) {
+ if (memcmp(buffer, data+p, to_read) != 0) {
printf("verification failed starting at %zu\n", p);
start = p;
break;
@@ -535,7 +490,7 @@ int WriteToPartition(unsigned char* data, size_t len,
}
if (start == len) {
- printf("verification read succeeded (attempt %d)\n", attempt+1);
+ printf("verification read succeeded (attempt %zu)\n", attempt+1);
success = true;
break;
}
@@ -546,7 +501,7 @@ int WriteToPartition(unsigned char* data, size_t len,
return -1;
}
- if (close(fd) != 0) {
+ if (ota_close(fd) != 0) {
printf("error closing %s (%s)\n", partition, strerror(errno));
return -1;
}
@@ -555,7 +510,6 @@ int WriteToPartition(unsigned char* data, size_t len,
}
}
- free(copy);
return 0;
}
@@ -565,10 +519,9 @@ int WriteToPartition(unsigned char* data, size_t len,
// the form "<digest>:<anything>". Return 0 on success, -1 on any
// error.
int ParseSha1(const char* str, uint8_t* digest) {
- int i;
const char* ps = str;
uint8_t* pd = digest;
- for (i = 0; i < SHA_DIGEST_SIZE * 2; ++i, ++ps) {
+ for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
int digit;
if (*ps >= '0' && *ps <= '9') {
digit = *ps - '0';
@@ -595,11 +548,10 @@ int ParseSha1(const char* str, uint8_t* digest) {
// found.
int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str,
int num_patches) {
- int i;
- uint8_t patch_sha1[SHA_DIGEST_SIZE];
- for (i = 0; i < num_patches; ++i) {
+ uint8_t patch_sha1[SHA_DIGEST_LENGTH];
+ for (int i = 0; i < num_patches; ++i) {
if (ParseSha1(patch_sha1_str[i], patch_sha1) == 0 &&
- memcmp(patch_sha1, sha1, SHA_DIGEST_SIZE) == 0) {
+ memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
return i;
}
}
@@ -609,10 +561,9 @@ int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str,
// 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.
-int applypatch_check(const char* filename,
- int num_patches, char** const patch_sha1_str) {
+int applypatch_check(const char* filename, int num_patches,
+ char** const patch_sha1_str) {
FileContents file;
- file.data = NULL;
// It's okay to specify no sha1s; the check will pass if the
// LoadFileContents is successful. (Useful for reading
@@ -624,9 +575,6 @@ int applypatch_check(const char* filename,
printf("file \"%s\" doesn't have any of expected "
"sha1 sums; checking cache\n", filename);
- free(file.data);
- file.data = NULL;
-
// 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
@@ -640,12 +588,9 @@ int applypatch_check(const char* filename,
if (FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0) {
printf("cache bits don't match any sha1 for \"%s\"\n", filename);
- free(file.data);
return 1;
}
}
-
- free(file.data);
return 0;
}
@@ -655,13 +600,13 @@ int ShowLicenses() {
}
ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) {
- int fd = *(int *)token;
+ int fd = *static_cast<int*>(token);
ssize_t done = 0;
ssize_t wrote;
- while (done < (ssize_t) len) {
- wrote = TEMP_FAILURE_RETRY(write(fd, data+done, len-done));
+ while (done < len) {
+ wrote = TEMP_FAILURE_RETRY(ota_write(fd, data+done, len-done));
if (wrote == -1) {
- printf("error writing %d bytes: %s\n", (int)(len-done), strerror(errno));
+ printf("error writing %zd bytes: %s\n", (len-done), strerror(errno));
return done;
}
done += wrote;
@@ -669,19 +614,9 @@ ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) {
return done;
}
-typedef struct {
- unsigned char* buffer;
- ssize_t size;
- ssize_t pos;
-} MemorySinkInfo;
-
ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) {
- MemorySinkInfo* msi = (MemorySinkInfo*)token;
- if (msi->size - msi->pos < len) {
- return -1;
- }
- memcpy(msi->buffer + msi->pos, data, len);
- msi->pos += len;
+ std::string* s = static_cast<std::string*>(token);
+ s->append(reinterpret_cast<const char*>(data), len);
return len;
}
@@ -705,15 +640,6 @@ int CacheSizeCheck(size_t bytes) {
}
}
-static void print_short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) {
- int i;
- const char* hex = "0123456789abcdef";
- for (i = 0; i < 4; ++i) {
- putchar(hex[(sha1[i]>>4) & 0xf]);
- putchar(hex[sha1[i] & 0xf]);
- }
-}
-
// This function applies binary patches to files in a way that is safe
// (the original file is not touched until we have the desired
// replacement for it) and idempotent (it's okay to run this program
@@ -726,7 +652,7 @@ static void print_short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) {
// 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
-// blob daat). If that new file has sha1 hash <target_sha1_str>,
+// blob data). If that new file has sha1 hash <target_sha1_str>,
// 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.
@@ -737,7 +663,7 @@ static void print_short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) {
// status.
//
// <source_filename> may refer to a partition to read the source data.
-// See the comments for the LoadPartition Contents() function above
+// See the comments for the LoadPartitionContents() function above
// for the format of such a filename.
int applypatch(const char* source_filename,
@@ -750,12 +676,11 @@ int applypatch(const char* source_filename,
Value* bonus_data) {
printf("patch %s: ", source_filename);
- if (target_filename[0] == '-' &&
- target_filename[1] == '\0') {
+ if (target_filename[0] == '-' && target_filename[1] == '\0') {
target_filename = source_filename;
}
- uint8_t target_sha1[SHA_DIGEST_SIZE];
+ uint8_t target_sha1[SHA_DIGEST_LENGTH];
if (ParseSha1(target_sha1_str, target_sha1) != 0) {
printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
return 1;
@@ -763,45 +688,37 @@ int applypatch(const char* source_filename,
FileContents copy_file;
FileContents source_file;
- copy_file.data = NULL;
- source_file.data = NULL;
const Value* source_patch_value = NULL;
const Value* copy_patch_value = NULL;
// We try to load the target file into the source_file object.
if (LoadFileContents(target_filename, &source_file) == 0) {
- if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 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.
- printf("already ");
- print_short_sha1(target_sha1);
- putchar('\n');
- free(source_file.data);
+ printf("already %s\n", short_sha1(target_sha1).c_str());
return 0;
}
}
- if (source_file.data == NULL ||
+ 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 source file.
- free(source_file.data);
- source_file.data = NULL;
+ source_file.data.clear();
LoadFileContents(source_filename, &source_file);
}
- if (source_file.data != NULL) {
- int to_use = FindMatchingPatch(source_file.sha1,
- patch_sha1_str, num_patches);
+ if (!source_file.data.empty()) {
+ int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str, num_patches);
if (to_use >= 0) {
source_patch_value = patch_data[to_use];
}
}
if (source_patch_value == NULL) {
- free(source_file.data);
- source_file.data = NULL;
+ source_file.data.clear();
printf("source file is bad; trying copy\n");
if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file) < 0) {
@@ -810,8 +727,7 @@ int applypatch(const char* source_filename,
return 1;
}
- int to_use = FindMatchingPatch(copy_file.sha1,
- patch_sha1_str, num_patches);
+ int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str, num_patches);
if (to_use >= 0) {
copy_patch_value = patch_data[to_use];
}
@@ -819,19 +735,69 @@ int applypatch(const char* source_filename,
if (copy_patch_value == NULL) {
// fail.
printf("copy file doesn't match source SHA-1s either\n");
- free(copy_file.data);
return 1;
}
}
- int result = GenerateTarget(&source_file, source_patch_value,
- &copy_file, copy_patch_value,
- source_filename, target_filename,
- target_sha1, target_size, bonus_data);
- free(source_file.data);
- free(copy_file.data);
+ return GenerateTarget(&source_file, source_patch_value,
+ &copy_file, copy_patch_value,
+ source_filename, target_filename,
+ target_sha1, target_size, bonus_data);
+}
- return result;
+/*
+ * 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);
+
+ uint8_t target_sha1[SHA_DIGEST_LENGTH];
+ 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, ":");
+ if (pieces.size() != 2 || (pieces[0] != "MTD" && pieces[0] != "EMMC")) {
+ 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 &&
+ memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
+ // 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) {
+ if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
+ // 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;
+ }
+ }
+
+ if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
+ printf("write of copied data to %s failed\n", target_filename);
+ return 1;
+ }
+ return 0;
}
static int GenerateTarget(FileContents* source_file,
@@ -840,37 +806,58 @@ static int GenerateTarget(FileContents* source_file,
const Value* copy_patch_value,
const char* source_filename,
const char* target_filename,
- const uint8_t target_sha1[SHA_DIGEST_SIZE],
+ const uint8_t target_sha1[SHA_DIGEST_LENGTH],
size_t target_size,
const Value* bonus_data) {
int retry = 1;
SHA_CTX ctx;
- int output;
- MemorySinkInfo msi;
+ std::string memory_sink_str;
FileContents* source_to_use;
- char* outname;
int made_copy = 0;
+ bool target_is_partition = (strncmp(target_filename, "MTD:", 4) == 0 ||
+ strncmp(target_filename, "EMMC:", 5) == 0 ||
+ strncmp(target_filename, "BML:", 4) == 0);
+ const std::string tmp_target_filename = std::string(target_filename) + ".patch";
+
// 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().
- char target_fs[strlen(target_filename)+1];
- char* slash = strchr(target_filename+1, '/');
- if (slash != NULL) {
- int count = slash - target_filename;
- strncpy(target_fs, target_filename, count);
- target_fs[count] = '\0';
+ 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;
} else {
- strcpy(target_fs, target_filename);
+ 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;
}
do {
// Is there enough room in the target filesystem to hold the patched
// file?
- if (strncmp(target_filename, "MTD:", 4) == 0 ||
- strncmp(target_filename, "EMMC:", 5) == 0 ||
- strncmp(target_filename, "BML:", 4) == 0) {
+ if (target_is_partition) {
// 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
@@ -879,7 +866,7 @@ static int GenerateTarget(FileContents* source_file,
// We still write the original source to cache, in case
// the partition write is interrupted.
- if (MakeFreeSpaceOnCache(source_file->size) < 0) {
+ if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) {
printf("not enough free space on /cache\n");
return 1;
}
@@ -892,13 +879,13 @@ static int GenerateTarget(FileContents* source_file,
} else {
int enough_space = 0;
if (retry > 0) {
- size_t free_space = FreeSpaceForFile(target_fs);
+ size_t free_space = FreeSpaceForFile(target_fs.c_str());
enough_space =
(free_space > (256 << 10)) && // 256k (two-block) minimum
(free_space > (target_size * 3 / 2)); // 50% margin of error
if (!enough_space) {
- printf("target %ld bytes; free space %ld bytes; retry %d; enough %d\n",
- (long)target_size, (long)free_space, retry, enough_space);
+ printf("target %zu bytes; free space %zu bytes; retry %d; enough %d\n",
+ target_size, free_space, retry, enough_space);
}
}
@@ -917,12 +904,11 @@ static int GenerateTarget(FileContents* source_file,
// It's impossible to free space on the target filesystem by
// deleting the source if the source is a partition. If
// we're ever in a state where we need to do this, fail.
- printf("not enough free space for target but source "
- "is partition\n");
+ printf("not enough free space for target but source is partition\n");
return 1;
}
- if (MakeFreeSpaceOnCache(source_file->size) < 0) {
+ if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) {
printf("not enough free space on /cache\n");
return 1;
}
@@ -934,87 +920,54 @@ static int GenerateTarget(FileContents* source_file,
made_copy = 1;
unlink(source_filename);
- size_t free_space = FreeSpaceForFile(target_fs);
- printf("(now %ld bytes free for target) ", (long)free_space);
+ size_t free_space = FreeSpaceForFile(target_fs.c_str());
+ printf("(now %zu bytes free for target) ", free_space);
}
}
- const Value* patch;
- if (source_patch_value != NULL) {
- source_to_use = source_file;
- patch = source_patch_value;
- } else {
- source_to_use = copy_file;
- patch = copy_patch_value;
- }
-
- if (patch->type != VAL_BLOB) {
- printf("patch is not a blob\n");
- return 1;
- }
SinkFn sink = NULL;
void* token = NULL;
- output = -1;
- outname = NULL;
- if (strncmp(target_filename, "MTD:", 4) == 0 ||
- strncmp(target_filename, "EMMC:", 5) == 0 ||
- strncmp(target_filename, "BML:", 4) == 0) {
+
+ int output_fd = -1;
+ if (target_is_partition) {
// We store the decoded output in memory.
- msi.buffer = malloc(target_size);
- if (msi.buffer == NULL) {
- printf("failed to alloc %ld bytes for output\n",
- (long)target_size);
- return 1;
- }
- msi.pos = 0;
- msi.size = target_size;
sink = MemorySink;
- token = &msi;
+ token = &memory_sink_str;
} else {
// We write the decoded output to "<tgt-file>.patch".
- outname = (char*)malloc(strlen(target_filename) + 10);
- strcpy(outname, target_filename);
- strcat(outname, ".patch");
-
- output = open(outname, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
- S_IRUSR | S_IWUSR);
- if (output < 0) {
- printf("failed to open output file %s: %s\n",
- outname, strerror(errno));
+ output_fd = ota_open(tmp_target_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
+ S_IRUSR | S_IWUSR);
+ if (output_fd < 0) {
+ printf("failed to open output file %s: %s\n", tmp_target_filename.c_str(),
+ strerror(errno));
return 1;
}
sink = FileSink;
- token = &output;
+ token = &output_fd;
}
- char* header = patch->data;
- ssize_t header_bytes_read = patch->size;
- SHA_init(&ctx);
+ SHA1_Init(&ctx);
int result;
-
- if (header_bytes_read >= 8 &&
- memcmp(header, "BSDIFF40", 8) == 0) {
- result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size,
+ if (use_bsdiff) {
+ result = ApplyBSDiffPatch(source_to_use->data.data(), source_to_use->data.size(),
patch, 0, sink, token, &ctx);
- } else if (header_bytes_read >= 8 &&
- memcmp(header, "IMGDIFF2", 8) == 0) {
- result = ApplyImagePatch(source_to_use->data, source_to_use->size,
- patch, sink, token, &ctx, bonus_data);
} else {
- printf("Unknown patch file format\n");
- return 1;
+ result = ApplyImagePatch(source_to_use->data.data(), source_to_use->data.size(),
+ patch, sink, token, &ctx, bonus_data);
}
- if (output >= 0) {
- if (fsync(output) != 0) {
- printf("failed to fsync file \"%s\" (%s)\n", outname, strerror(errno));
+ if (!target_is_partition) {
+ if (ota_fsync(output_fd) != 0) {
+ printf("failed to fsync file \"%s\" (%s)\n", tmp_target_filename.c_str(),
+ strerror(errno));
result = 1;
}
- if (close(output) != 0) {
- printf("failed to close file \"%s\" (%s)\n", outname, strerror(errno));
+ if (ota_close(output_fd) != 0) {
+ printf("failed to close file \"%s\" (%s)\n", tmp_target_filename.c_str(),
+ strerror(errno));
result = 1;
}
}
@@ -1026,8 +979,8 @@ static int GenerateTarget(FileContents* source_file,
} else {
printf("applying patch failed; retrying\n");
}
- if (outname != NULL) {
- unlink(outname);
+ if (!target_is_partition) {
+ unlink(tmp_target_filename.c_str());
}
} else {
// succeeded; no need to retry
@@ -1035,47 +988,46 @@ static int GenerateTarget(FileContents* source_file,
}
} while (retry-- > 0);
- const uint8_t* current_target_sha1 = SHA_final(&ctx);
- if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) {
+ 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) {
printf("patch did not produce expected sha1\n");
return 1;
} else {
- printf("now ");
- print_short_sha1(target_sha1);
- putchar('\n');
+ printf("now %s\n", short_sha1(target_sha1).c_str());
}
- if (output < 0) {
+ if (target_is_partition) {
// Copy the temp file to the partition.
- if (WriteToPartition(msi.buffer, msi.pos, target_filename) != 0) {
+ if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
+ memory_sink_str.size(), target_filename) != 0) {
printf("write of patched data to %s failed\n", target_filename);
return 1;
}
- free(msi.buffer);
} else {
// Give the .patch file the same owner, group, and mode of the
// original source file.
- if (chmod(outname, source_to_use->st.st_mode) != 0) {
- printf("chmod of \"%s\" failed: %s\n", outname, strerror(errno));
+ 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));
return 1;
}
- if (chown(outname, source_to_use->st.st_uid,
- source_to_use->st.st_gid) != 0) {
- printf("chown of \"%s\" failed: %s\n", outname, strerror(errno));
+ 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));
return 1;
}
// Finally, rename the .patch file to replace the target file.
- if (rename(outname, target_filename) != 0) {
- printf("rename of .patch to \"%s\" failed: %s\n",
- target_filename, strerror(errno));
+ if (rename(tmp_target_filename.c_str(), target_filename) != 0) {
+ printf("rename of .patch to \"%s\" failed: %s\n", target_filename, strerror(errno));
return 1;
}
}
// If this run of applypatch created the copy, and we're here, we
// can delete it.
- if (made_copy) unlink(CACHE_TEMP_SOURCE);
+ if (made_copy) {
+ unlink(CACHE_TEMP_SOURCE);
+ }
// Success!
return 0;