Skip to content

Commit

Permalink
recovery: Change install_package() to take std::string.
Browse files Browse the repository at this point in the history
Also change the parameter type for log_buffer from reference to pointer,
so the styles for parameters look consistent.

Test: mmma bootable/recovery
Test: sideload a package with the new recovery image
Change-Id: I8f25580ccf22977624648b3e2181cca44dd67c1b
  • Loading branch information
Tao Bao committed May 1, 2017
1 parent 54da112 commit 29ee69b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 75 deletions.
141 changes: 72 additions & 69 deletions install.cpp
Expand Up @@ -100,7 +100,7 @@ bool read_metadata_from_package(ZipArchiveHandle zip, std::string* metadata) {
}

// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::string>& log_buffer) {
static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::string>* log_buffer) {
std::string metadata;
if (!read_metadata_from_package(zip, &metadata)) {
return;
Expand All @@ -114,12 +114,12 @@ static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::stri
if (android::base::StartsWith(str, "pre-build-incremental")) {
int source_build = parse_build_number(str);
if (source_build != -1) {
log_buffer.push_back(android::base::StringPrintf("source_build: %d", source_build));
log_buffer->push_back(android::base::StringPrintf("source_build: %d", source_build));
}
} else if (android::base::StartsWith(str, "post-build-incremental")) {
int target_build = parse_build_number(str);
if (target_build != -1) {
log_buffer.push_back(android::base::StringPrintf("target_build: %d", target_build));
log_buffer->push_back(android::base::StringPrintf("target_build: %d", target_build));
}
}
}
Expand Down Expand Up @@ -308,8 +308,8 @@ static void log_max_temperature(int* max_temperature) {
}

// If the package contains an update binary, extract it and run it.
static int try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_cache,
std::vector<std::string>& log_buffer, int retry_count,
static int try_update_binary(const std::string& path, ZipArchiveHandle zip, bool* wipe_cache,
std::vector<std::string>* log_buffer, int retry_count,
int* max_temperature) {
read_source_target_build(zip, log_buffer);

Expand Down Expand Up @@ -452,7 +452,7 @@ static int try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_
} else if (command == "log") {
if (!args.empty()) {
// Save the logging request from updater and write to last_install later.
log_buffer.push_back(args);
log_buffer->push_back(args);
} else {
LOG(ERROR) << "invalid \"log\" parameters: " << line;
}
Expand Down Expand Up @@ -547,78 +547,81 @@ bool verify_package_compatibility(ZipArchiveHandle package_zip) {
return false;
}

static int
really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
std::vector<std::string>& log_buffer, int retry_count, int* max_temperature)
{
ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
ui->Print("Finding update package...\n");
// Give verification half the progress bar...
ui->SetProgressType(RecoveryUI::DETERMINATE);
ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
LOG(INFO) << "Update location: " << path;

// Map the update package into memory.
ui->Print("Opening update package...\n");

if (path && needs_mount) {
if (path[0] == '@') {
ensure_path_mounted(path+1);
} else {
ensure_path_mounted(path);
}
}

MemMapping map;
if (sysMapFile(path, &map) != 0) {
LOG(ERROR) << "failed to map file";
return INSTALL_CORRUPT;
}

// Verify package.
if (!verify_package(map.addr, map.length)) {
log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
sysReleaseMap(&map);
return INSTALL_CORRUPT;
static int really_install_package(const std::string& path, bool* wipe_cache, bool needs_mount,
std::vector<std::string>* log_buffer, int retry_count,
int* max_temperature) {
ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
ui->Print("Finding update package...\n");
// Give verification half the progress bar...
ui->SetProgressType(RecoveryUI::DETERMINATE);
ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
LOG(INFO) << "Update location: " << path;

// Map the update package into memory.
ui->Print("Opening update package...\n");

if (needs_mount) {
if (path[0] == '@') {
ensure_path_mounted(path.substr(1).c_str());
} else {
ensure_path_mounted(path.c_str());
}
}

// Try to open the package.
ZipArchiveHandle zip;
int err = OpenArchiveFromMemory(map.addr, map.length, path, &zip);
if (err != 0) {
LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err);
log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
MemMapping map;
if (sysMapFile(path.c_str(), &map) != 0) {
LOG(ERROR) << "failed to map file";
return INSTALL_CORRUPT;
}

sysReleaseMap(&map);
CloseArchive(zip);
return INSTALL_CORRUPT;
}
// Verify package.
if (!verify_package(map.addr, map.length)) {
log_buffer->push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
sysReleaseMap(&map);
return INSTALL_CORRUPT;
}

// Additionally verify the compatibility of the package.
if (!verify_package_compatibility(zip)) {
log_buffer.push_back(android::base::StringPrintf("error: %d", kPackageCompatibilityFailure));
sysReleaseMap(&map);
CloseArchive(zip);
return INSTALL_CORRUPT;
}
// Try to open the package.
ZipArchiveHandle zip;
int err = OpenArchiveFromMemory(map.addr, map.length, path.c_str(), &zip);
if (err != 0) {
LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err);
log_buffer->push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));

// Verify and install the contents of the package.
ui->Print("Installing update...\n");
if (retry_count > 0) {
ui->Print("Retry attempt: %d\n", retry_count);
}
ui->SetEnableReboot(false);
int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count, max_temperature);
ui->SetEnableReboot(true);
ui->Print("\n");
sysReleaseMap(&map);
CloseArchive(zip);
return INSTALL_CORRUPT;
}

// Additionally verify the compatibility of the package.
if (!verify_package_compatibility(zip)) {
log_buffer->push_back(android::base::StringPrintf("error: %d", kPackageCompatibilityFailure));
sysReleaseMap(&map);
CloseArchive(zip);
return result;
return INSTALL_CORRUPT;
}

// Verify and install the contents of the package.
ui->Print("Installing update...\n");
if (retry_count > 0) {
ui->Print("Retry attempt: %d\n", retry_count);
}
ui->SetEnableReboot(false);
int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count, max_temperature);
ui->SetEnableReboot(true);
ui->Print("\n");

sysReleaseMap(&map);
CloseArchive(zip);
return result;
}

int install_package(const char* path, bool* wipe_cache, const char* install_file, bool needs_mount,
int retry_count) {
int install_package(const std::string& path, bool* wipe_cache, const std::string& install_file,
bool needs_mount, int retry_count) {
CHECK(!path.empty());
CHECK(!install_file.empty());
CHECK(wipe_cache != nullptr);

modified_flash = true;
auto start = std::chrono::system_clock::now();

Expand All @@ -631,7 +634,7 @@ int install_package(const char* path, bool* wipe_cache, const char* install_file
LOG(ERROR) << "failed to set up expected mounts for install; aborting";
result = INSTALL_ERROR;
} else {
result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count,
result = really_install_package(path, wipe_cache, needs_mount, &log_buffer, retry_count,
&max_temperature);
}

Expand Down
11 changes: 5 additions & 6 deletions install.h
Expand Up @@ -23,10 +23,9 @@
enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT, INSTALL_NONE, INSTALL_SKIPPED,
INSTALL_RETRY };

// Install the package specified by root_path. If INSTALL_SUCCESS is
// returned and *wipe_cache is true on exit, caller should wipe the
// cache partition.
int install_package(const char* root_path, bool* wipe_cache, const char* install_file,
// Installs the given update package. If INSTALL_SUCCESS is returned and *wipe_cache is true on
// exit, caller should wipe the cache partition.
int install_package(const std::string& package, bool* wipe_cache, const std::string& install_file,
bool needs_mount, int retry_count);

// Verify the package by ota keys. Return true if the package is verified successfully,
Expand All @@ -35,9 +34,9 @@ bool verify_package(const unsigned char* package_data, size_t package_size);

// Read meta data file of the package, write its content in the string pointed by meta_data.
// Return true if succeed, otherwise return false.
bool read_metadata_from_package(ZipArchiveHandle zip, std::string* meta_data);
bool read_metadata_from_package(ZipArchiveHandle zip, std::string* metadata);

// Verifes the compatibility info in a Treble-compatible package. Returns true directly if the
// Verifies the compatibility info in a Treble-compatible package. Returns true directly if the
// entry doesn't exist.
bool verify_package_compatibility(ZipArchiveHandle package_zip);

Expand Down

0 comments on commit 29ee69b

Please sign in to comment.