summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--otafault/Android.mk3
-rw-r--r--otafault/config.cpp31
-rw-r--r--updater/blockimg.cpp54
3 files changed, 63 insertions, 25 deletions
diff --git a/otafault/Android.mk b/otafault/Android.mk
index 7468de6c4..ba7add855 100644
--- a/otafault/Android.mk
+++ b/otafault/Android.mk
@@ -17,9 +17,10 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
otafault_static_libs := \
+ libbase \
libminzip \
libz \
- libselinux \
+ libselinux
LOCAL_SRC_FILES := config.cpp ota_io.cpp
LOCAL_MODULE_TAGS := eng
diff --git a/otafault/config.cpp b/otafault/config.cpp
index aa4b4d5ce..b4567392d 100644
--- a/otafault/config.cpp
+++ b/otafault/config.cpp
@@ -20,6 +20,8 @@
#include <stdio.h>
#include <unistd.h>
+#include <android-base/stringprintf.h>
+
#include "minzip/Zip.h"
#include "config.h"
#include "ota_io.h"
@@ -27,12 +29,10 @@
#define OTAIO_MAX_FNAME_SIZE 128
static ZipArchive* archive;
-static std::map<const char*, bool> should_inject_cache;
+static std::map<std::string, bool> should_inject_cache;
-static const char* get_type_path(const char* io_type) {
- char* path = (char*)calloc(strlen(io_type) + strlen(OTAIO_BASE_DIR) + 2, sizeof(char));
- sprintf(path, "%s/%s", OTAIO_BASE_DIR, io_type);
- return path;
+static std::string get_type_path(const char* io_type) {
+ return android::base::StringPrintf("%s/%s", OTAIO_BASE_DIR, io_type);
}
void ota_io_init(ZipArchive* za) {
@@ -46,13 +46,12 @@ bool should_fault_inject(const char* io_type) {
if (archive == NULL) {
return false;
}
- if (should_inject_cache.find(io_type) != should_inject_cache.end()) {
- return should_inject_cache[io_type];
+ const std::string type_path = get_type_path(io_type);
+ if (should_inject_cache.find(type_path) != should_inject_cache.end()) {
+ return should_inject_cache[type_path];
}
- const char* type_path = get_type_path(io_type);
- const ZipEntry* entry = mzFindZipEntry(archive, type_path);
+ const ZipEntry* entry = mzFindZipEntry(archive, type_path.c_str());
should_inject_cache[type_path] = entry != nullptr;
- free((void*)type_path);
return entry != NULL;
}
@@ -61,10 +60,10 @@ bool should_hit_cache() {
}
std::string fault_fname(const char* io_type) {
- const char* type_path = get_type_path(io_type);
- char* fname = (char*) calloc(OTAIO_MAX_FNAME_SIZE, sizeof(char));
- const ZipEntry* entry = mzFindZipEntry(archive, type_path);
- mzReadZipEntry(archive, entry, fname, OTAIO_MAX_FNAME_SIZE);
- free((void*)type_path);
- return std::string(fname);
+ std::string type_path = get_type_path(io_type);
+ std::string fname;
+ fname.resize(OTAIO_MAX_FNAME_SIZE);
+ const ZipEntry* entry = mzFindZipEntry(archive, type_path.c_str());
+ mzReadZipEntry(archive, entry, &fname[0], OTAIO_MAX_FNAME_SIZE);
+ return fname;
}
diff --git a/updater/blockimg.cpp b/updater/blockimg.cpp
index faa7008c5..c20bad904 100644
--- a/updater/blockimg.cpp
+++ b/updater/blockimg.cpp
@@ -33,6 +33,7 @@
#include <unistd.h>
#include <fec/io.h>
+#include <map>
#include <memory>
#include <string>
#include <vector>
@@ -67,6 +68,8 @@ struct RangeSet {
std::vector<size_t> pos; // Actual limit is INT_MAX.
};
+static std::map<std::string, RangeSet> stash_map;
+
static void parse_range(const std::string& range_text, RangeSet& rs) {
std::vector<std::string> pieces = android::base::Split(range_text, ",");
@@ -522,8 +525,28 @@ static void DeleteStash(const std::string& base) {
}
}
-static int LoadStash(const std::string& base, const std::string& id, bool verify, size_t* blocks,
- std::vector<uint8_t>& buffer, bool printnoent) {
+static int LoadStash(CommandParameters& params, const std::string& base, const std::string& id,
+ bool verify, size_t* blocks, std::vector<uint8_t>& buffer, bool printnoent) {
+ // In verify mode, if source range_set was saved for the given hash,
+ // check contents in the source blocks first. If the check fails,
+ // search for the stashed files on /cache as usual.
+ if (!params.canwrite) {
+ if (stash_map.find(id) != stash_map.end()) {
+ const RangeSet& src = stash_map[id];
+ allocate(src.size * BLOCKSIZE, buffer);
+
+ if (ReadBlocks(src, buffer, params.fd) == -1) {
+ fprintf(stderr, "failed to read source blocks in stash map.\n");
+ return -1;
+ }
+ if (VerifyBlocks(id, buffer, src.size, true) != 0) {
+ fprintf(stderr, "failed to verify loaded source blocks in stash map.\n");
+ return -1;
+ }
+ return 0;
+ }
+ }
+
if (base.empty()) {
return -1;
}
@@ -722,7 +745,7 @@ static int SaveStash(CommandParameters& params, const std::string& base,
const std::string& id = params.tokens[params.cpos++];
size_t blocks = 0;
- if (usehash && LoadStash(base, id, true, &blocks, buffer, false) == 0) {
+ if (usehash && LoadStash(params, base, id, true, &blocks, buffer, false) == 0) {
// Stash file already exists and has expected contents. Do not
// read from source again, as the source may have been already
// overwritten during a previous attempt.
@@ -747,6 +770,12 @@ static int SaveStash(CommandParameters& params, const std::string& base,
return 0;
}
+ // In verify mode, save source range_set instead of stashing blocks.
+ if (!params.canwrite && usehash) {
+ stash_map[id] = src;
+ return 0;
+ }
+
fprintf(stderr, "stashing %zu blocks to %s\n", blocks, id.c_str());
return WriteStash(base, id, blocks, buffer, false, nullptr);
}
@@ -857,7 +886,7 @@ static int LoadSrcTgtVersion2(CommandParameters& params, RangeSet& tgt, size_t&
}
std::vector<uint8_t> stash;
- int res = LoadStash(stashbase, tokens[0], false, nullptr, stash, true);
+ int res = LoadStash(params, stashbase, tokens[0], false, nullptr, stash, true);
if (res == -1) {
// These source blocks will fail verification if used later, but we
@@ -931,8 +960,9 @@ static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t&
if (VerifyBlocks(srchash, params.buffer, src_blocks, true) == 0) {
// If source and target blocks overlap, stash the source blocks so we can
- // resume from possible write errors
- if (overlap) {
+ // resume from possible write errors. In verify mode, we can skip stashing
+ // because the source blocks won't be overwritten.
+ if (overlap && params.canwrite) {
fprintf(stderr, "stashing %zu overlapping blocks to %s\n", src_blocks,
srchash.c_str());
@@ -953,7 +983,8 @@ static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t&
return 0;
}
- if (overlap && LoadStash(params.stashbase, srchash, true, nullptr, params.buffer, true) == 0) {
+ if (overlap && LoadStash(params, params.stashbase, srchash, true, nullptr, params.buffer,
+ true) == 0) {
// Overlapping source blocks were previously stashed, command can proceed.
// We are recovering from an interrupted command, so we don't know if the
// stash can safely be deleted after this command.
@@ -1028,8 +1059,15 @@ static int PerformCommandFree(CommandParameters& params) {
return -1;
}
+ const std::string& id = params.tokens[params.cpos++];
+
+ if (!params.canwrite && stash_map.find(id) != stash_map.end()) {
+ stash_map.erase(id);
+ return 0;
+ }
+
if (params.createdstash || params.canwrite) {
- return FreeStash(params.stashbase, params.tokens[params.cpos++]);
+ return FreeStash(params.stashbase, id);
}
return 0;