am cfd9d9c7: Merge "applypatch: Refactor strtok()."
* commit 'cfd9d9c7ce03b847946977a98efcbfca2f067fd3': applypatch: Refactor strtok().
This commit is contained in:
@@ -21,7 +21,7 @@ LOCAL_SRC_FILES := applypatch.cpp bspatch.cpp freecache.cpp imgpatch.cpp utils.c
|
|||||||
LOCAL_MODULE := libapplypatch
|
LOCAL_MODULE := libapplypatch
|
||||||
LOCAL_MODULE_TAGS := eng
|
LOCAL_MODULE_TAGS := eng
|
||||||
LOCAL_C_INCLUDES += external/bzip2 external/zlib bootable/recovery
|
LOCAL_C_INCLUDES += external/bzip2 external/zlib bootable/recovery
|
||||||
LOCAL_STATIC_LIBRARIES += libmtdutils libmincrypt libbz libz
|
LOCAL_STATIC_LIBRARIES += libbase libmtdutils libmincrypt libbz libz
|
||||||
|
|
||||||
include $(BUILD_STATIC_LIBRARY)
|
include $(BUILD_STATIC_LIBRARY)
|
||||||
|
|
||||||
@@ -31,7 +31,7 @@ LOCAL_CLANG := true
|
|||||||
LOCAL_SRC_FILES := main.cpp
|
LOCAL_SRC_FILES := main.cpp
|
||||||
LOCAL_MODULE := applypatch
|
LOCAL_MODULE := applypatch
|
||||||
LOCAL_C_INCLUDES += bootable/recovery
|
LOCAL_C_INCLUDES += bootable/recovery
|
||||||
LOCAL_STATIC_LIBRARIES += libapplypatch libmtdutils libmincrypt libbz
|
LOCAL_STATIC_LIBRARIES += libapplypatch libbase libmtdutils libmincrypt libbz
|
||||||
LOCAL_SHARED_LIBRARIES += libz libcutils libc
|
LOCAL_SHARED_LIBRARIES += libz libcutils libc
|
||||||
|
|
||||||
include $(BUILD_EXECUTABLE)
|
include $(BUILD_EXECUTABLE)
|
||||||
@@ -44,7 +44,7 @@ LOCAL_MODULE := applypatch_static
|
|||||||
LOCAL_FORCE_STATIC_EXECUTABLE := true
|
LOCAL_FORCE_STATIC_EXECUTABLE := true
|
||||||
LOCAL_MODULE_TAGS := eng
|
LOCAL_MODULE_TAGS := eng
|
||||||
LOCAL_C_INCLUDES += bootable/recovery
|
LOCAL_C_INCLUDES += bootable/recovery
|
||||||
LOCAL_STATIC_LIBRARIES += libapplypatch libmtdutils libmincrypt libbz
|
LOCAL_STATIC_LIBRARIES += libapplypatch libbase libmtdutils libmincrypt libbz
|
||||||
LOCAL_STATIC_LIBRARIES += libz libcutils libc
|
LOCAL_STATIC_LIBRARIES += libz libcutils libc
|
||||||
|
|
||||||
include $(BUILD_EXECUTABLE)
|
include $(BUILD_EXECUTABLE)
|
||||||
|
|||||||
+53
-83
@@ -25,6 +25,8 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <base/strings.h>
|
||||||
|
|
||||||
#include "mincrypt/sha.h"
|
#include "mincrypt/sha.h"
|
||||||
#include "applypatch.h"
|
#include "applypatch.h"
|
||||||
#include "mtdutils/mtdutils.h"
|
#include "mtdutils/mtdutils.h"
|
||||||
@@ -42,7 +44,7 @@ static int GenerateTarget(FileContents* source_file,
|
|||||||
size_t target_size,
|
size_t target_size,
|
||||||
const Value* bonus_data);
|
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
|
// Read a file into memory; store the file contents and associated
|
||||||
// metadata in *file.
|
// metadata in *file.
|
||||||
@@ -87,21 +89,6 @@ int LoadFileContents(const char* filename, FileContents* file) {
|
|||||||
return 0;
|
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) {
|
|
||||||
const int aa = *reinterpret_cast<const int*>(a);
|
|
||||||
const int bb = *reinterpret_cast<const 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
|
// Load the contents of an MTD or EMMC partition into the provided
|
||||||
// FileContents. filename should be a string of the form
|
// FileContents. filename should be a string of the form
|
||||||
// "MTD:<partition_name>:<size_1>:<sha1_1>:<size_2>:<sha1_2>:..." (or
|
// "MTD:<partition_name>:<size_1>:<sha1_1>:<size_2>:<sha1_2>:..." (or
|
||||||
@@ -120,53 +107,45 @@ static int compare_size_indices(const void* a, const void* b) {
|
|||||||
enum PartitionType { MTD, EMMC };
|
enum PartitionType { MTD, EMMC };
|
||||||
|
|
||||||
static int LoadPartitionContents(const char* filename, FileContents* file) {
|
static int LoadPartitionContents(const char* filename, FileContents* file) {
|
||||||
char* copy = strdup(filename);
|
std::string copy(filename);
|
||||||
const char* magic = strtok(copy, ":");
|
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;
|
enum PartitionType type;
|
||||||
|
if (pieces[0] == "MTD") {
|
||||||
if (strcmp(magic, "MTD") == 0) {
|
|
||||||
type = MTD;
|
type = MTD;
|
||||||
} else if (strcmp(magic, "EMMC") == 0) {
|
} else if (pieces[0] == "EMMC") {
|
||||||
type = EMMC;
|
type = EMMC;
|
||||||
} else {
|
} else {
|
||||||
printf("LoadPartitionContents called with bad filename (%s)\n", filename);
|
printf("LoadPartitionContents called with bad filename (%s)\n", filename);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
const char* partition = strtok(NULL, ":");
|
const char* partition = pieces[1].c_str();
|
||||||
|
|
||||||
int i;
|
size_t pairs = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename
|
||||||
int colons = 0;
|
std::vector<size_t> index(pairs);
|
||||||
for (i = 0; filename[i] != '\0'; ++i) {
|
std::vector<size_t> size(pairs);
|
||||||
if (filename[i] == ':') {
|
std::vector<std::string> sha1sum(pairs);
|
||||||
++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
|
for (size_t i = 0; i < pairs; ++i) {
|
||||||
int* index = reinterpret_cast<int*>(malloc(pairs * sizeof(int)));
|
size[i] = strtol(pieces[i*2+2].c_str(), NULL, 10);
|
||||||
size_t* size = reinterpret_cast<size_t*>(malloc(pairs * sizeof(size_t)));
|
|
||||||
char** sha1sum = reinterpret_cast<char**>(malloc(pairs * sizeof(char*)));
|
|
||||||
|
|
||||||
for (i = 0; i < pairs; ++i) {
|
|
||||||
const char* size_str = strtok(NULL, ":");
|
|
||||||
size[i] = strtol(size_str, NULL, 10);
|
|
||||||
if (size[i] == 0) {
|
if (size[i] == 0) {
|
||||||
printf("LoadPartitionContents called with bad size (%s)\n", filename);
|
printf("LoadPartitionContents called with bad size (%s)\n", filename);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
sha1sum[i] = strtok(NULL, ":");
|
sha1sum[i] = pieces[i*2+3].c_str();
|
||||||
index[i] = i;
|
index[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort the index[] array so it indexes the pairs in order of
|
// Sort the index[] array so it indexes the pairs in order of increasing size.
|
||||||
// increasing size.
|
sort(index.begin(), index.end(),
|
||||||
size_array = size;
|
[&](const size_t& i, const size_t& j) {
|
||||||
qsort(index, pairs, sizeof(int), compare_size_indices);
|
return (size[i] < size[j]);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
MtdReadContext* ctx = NULL;
|
MtdReadContext* ctx = NULL;
|
||||||
FILE* dev = NULL;
|
FILE* dev = NULL;
|
||||||
@@ -175,20 +154,18 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
|
|||||||
case MTD: {
|
case MTD: {
|
||||||
if (!mtd_partitions_scanned) {
|
if (!mtd_partitions_scanned) {
|
||||||
mtd_scan_partitions();
|
mtd_scan_partitions();
|
||||||
mtd_partitions_scanned = 1;
|
mtd_partitions_scanned = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const MtdPartition* mtd = mtd_find_partition_by_name(partition);
|
const MtdPartition* mtd = mtd_find_partition_by_name(partition);
|
||||||
if (mtd == NULL) {
|
if (mtd == NULL) {
|
||||||
printf("mtd partition \"%s\" not found (loading %s)\n",
|
printf("mtd partition \"%s\" not found (loading %s)\n", partition, filename);
|
||||||
partition, filename);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx = mtd_read_partition(mtd);
|
ctx = mtd_read_partition(mtd);
|
||||||
if (ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
printf("failed to initialize read of mtd partition \"%s\"\n",
|
printf("failed to initialize read of mtd partition \"%s\"\n", partition);
|
||||||
partition);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -197,8 +174,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
|
|||||||
case EMMC:
|
case EMMC:
|
||||||
dev = fopen(partition, "rb");
|
dev = fopen(partition, "rb");
|
||||||
if (dev == NULL) {
|
if (dev == NULL) {
|
||||||
printf("failed to open emmc partition \"%s\": %s\n",
|
printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno));
|
||||||
partition, strerror(errno));
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -207,15 +183,15 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
|
|||||||
SHA_init(&sha_ctx);
|
SHA_init(&sha_ctx);
|
||||||
uint8_t parsed_sha[SHA_DIGEST_SIZE];
|
uint8_t parsed_sha[SHA_DIGEST_SIZE];
|
||||||
|
|
||||||
// allocate enough memory to hold the largest size.
|
// Allocate enough memory to hold the largest size.
|
||||||
file->data = reinterpret_cast<unsigned char*>(malloc(size[index[pairs-1]]));
|
file->data = reinterpret_cast<unsigned char*>(malloc(size[index[pairs-1]]));
|
||||||
char* p = (char*)file->data;
|
char* p = (char*)file->data;
|
||||||
file->size = 0; // # bytes read so far
|
file->size = 0; // # bytes read so far
|
||||||
|
bool found = false;
|
||||||
|
|
||||||
for (i = 0; i < pairs; ++i) {
|
for (size_t i = 0; i < pairs; ++i) {
|
||||||
// Read enough additional bytes to get us up to the next size
|
// Read enough additional bytes to get us up to the next size. (Again,
|
||||||
// (again, we're trying the possibilities in order of increasing
|
// we're trying the possibilities in order of increasing size).
|
||||||
// size).
|
|
||||||
size_t next = size[index[i]] - file->size;
|
size_t next = size[index[i]] - file->size;
|
||||||
size_t read = 0;
|
size_t read = 0;
|
||||||
if (next > 0) {
|
if (next > 0) {
|
||||||
@@ -245,8 +221,8 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
|
|||||||
memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
|
memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
|
||||||
const uint8_t* sha_so_far = SHA_final(&temp_ctx);
|
const uint8_t* sha_so_far = SHA_final(&temp_ctx);
|
||||||
|
|
||||||
if (ParseSha1(sha1sum[index[i]], parsed_sha) != 0) {
|
if (ParseSha1(sha1sum[index[i]].c_str(), parsed_sha) != 0) {
|
||||||
printf("failed to parse sha1 %s in %s\n", sha1sum[index[i]], filename);
|
printf("failed to parse sha1 %s in %s\n", sha1sum[index[i]].c_str(), filename);
|
||||||
free(file->data);
|
free(file->data);
|
||||||
file->data = NULL;
|
file->data = NULL;
|
||||||
return -1;
|
return -1;
|
||||||
@@ -256,7 +232,8 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
|
|||||||
// we have a match. stop reading the partition; we'll return
|
// we have a match. stop reading the partition; we'll return
|
||||||
// the data we've read so far.
|
// the data we've read so far.
|
||||||
printf("partition read matched size %zu sha %s\n",
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,9 +251,8 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (i == pairs) {
|
if (!found) {
|
||||||
// Ran off the end of the list of (size,sha1) pairs without
|
// Ran off the end of the list of (size,sha1) pairs without finding a match.
|
||||||
// finding a match.
|
|
||||||
printf("contents of partition \"%s\" didn't match %s\n", partition, filename);
|
printf("contents of partition \"%s\" didn't match %s\n", partition, filename);
|
||||||
free(file->data);
|
free(file->data);
|
||||||
file->data = NULL;
|
file->data = NULL;
|
||||||
@@ -293,11 +269,6 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
|
|||||||
file->st.st_uid = 0;
|
file->st.st_uid = 0;
|
||||||
file->st.st_gid = 0;
|
file->st.st_gid = 0;
|
||||||
|
|
||||||
free(copy);
|
|
||||||
free(index);
|
|
||||||
free(size);
|
|
||||||
free(sha1sum);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,33 +311,33 @@ int SaveFileContents(const char* filename, const FileContents* file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write a memory buffer to 'target' partition, a string of the form
|
// Write a memory buffer to 'target' partition, a string of the form
|
||||||
// "MTD:<partition>[:...]" or "EMMC:<partition_device>:". Return 0 on
|
// "MTD:<partition>[:...]" or "EMMC:<partition_device>". Return 0 on
|
||||||
// success.
|
// success.
|
||||||
int WriteToPartition(unsigned char* data, size_t len, const char* target) {
|
int WriteToPartition(unsigned char* data, size_t len, const char* target) {
|
||||||
char* copy = strdup(target);
|
std::string copy(target);
|
||||||
const char* magic = strtok(copy, ":");
|
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;
|
enum PartitionType type;
|
||||||
if (strcmp(magic, "MTD") == 0) {
|
if (pieces[0] == "MTD") {
|
||||||
type = MTD;
|
type = MTD;
|
||||||
} else if (strcmp(magic, "EMMC") == 0) {
|
} else if (pieces[0] == "EMMC") {
|
||||||
type = EMMC;
|
type = EMMC;
|
||||||
} else {
|
} else {
|
||||||
printf("WriteToPartition called with bad target (%s)\n", target);
|
printf("WriteToPartition called with bad target (%s)\n", target);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
const char* partition = strtok(NULL, ":");
|
const char* partition = pieces[1].c_str();
|
||||||
|
|
||||||
if (partition == NULL) {
|
|
||||||
printf("bad partition target name \"%s\"\n", target);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case MTD: {
|
case MTD: {
|
||||||
if (!mtd_partitions_scanned) {
|
if (!mtd_partitions_scanned) {
|
||||||
mtd_scan_partitions();
|
mtd_scan_partitions();
|
||||||
mtd_partitions_scanned = 1;
|
mtd_partitions_scanned = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const MtdPartition* mtd = mtd_find_partition_by_name(partition);
|
const MtdPartition* mtd = mtd_find_partition_by_name(partition);
|
||||||
@@ -410,7 +381,7 @@ int WriteToPartition(unsigned char* data, size_t len, const char* target) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int attempt = 0; attempt < 2; ++attempt) {
|
for (size_t attempt = 0; attempt < 2; ++attempt) {
|
||||||
if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
|
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;
|
return -1;
|
||||||
@@ -510,7 +481,6 @@ int WriteToPartition(unsigned char* data, size_t len, const char* target) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(copy);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user