Merge "recovery: Move property_get() to android::base::GetProperty()."

This commit is contained in:
Tao Bao
2017-01-14 15:37:00 +00:00
committed by Gerrit Code Review
2 changed files with 79 additions and 83 deletions

View File

@@ -36,9 +36,9 @@
#include <android-base/logging.h> #include <android-base/logging.h>
#include <android-base/parsedouble.h> #include <android-base/parsedouble.h>
#include <android-base/parseint.h> #include <android-base/parseint.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
#include <android-base/strings.h> #include <android-base/strings.h>
#include <cutils/properties.h>
#include <ziparchive/zip_archive.h> #include <ziparchive/zip_archive.h>
#include "common.h" #include "common.h"
@@ -139,81 +139,79 @@ update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
// Parses the metadata of the OTA package in |zip| and checks whether we are // Parses the metadata of the OTA package in |zip| and checks whether we are
// allowed to accept this A/B package. Downgrading is not allowed unless // allowed to accept this A/B package. Downgrading is not allowed unless
// explicitly enabled in the package and only for incremental packages. // explicitly enabled in the package and only for incremental packages.
static int check_newer_ab_build(ZipArchiveHandle zip) static int check_newer_ab_build(ZipArchiveHandle zip) {
{ std::string metadata_str;
std::string metadata_str; if (!read_metadata_from_package(zip, &metadata_str)) {
if (!read_metadata_from_package(zip, &metadata_str)) { return INSTALL_CORRUPT;
return INSTALL_CORRUPT; }
std::map<std::string, std::string> metadata;
for (const std::string& line : android::base::Split(metadata_str, "\n")) {
size_t eq = line.find('=');
if (eq != std::string::npos) {
metadata[line.substr(0, eq)] = line.substr(eq + 1);
} }
std::map<std::string, std::string> metadata; }
for (const std::string& line : android::base::Split(metadata_str, "\n")) {
size_t eq = line.find('=');
if (eq != std::string::npos) {
metadata[line.substr(0, eq)] = line.substr(eq + 1);
}
}
char value[PROPERTY_VALUE_MAX];
property_get("ro.product.device", value, ""); std::string value = android::base::GetProperty("ro.product.device", "");
const std::string& pkg_device = metadata["pre-device"]; const std::string& pkg_device = metadata["pre-device"];
if (pkg_device != value || pkg_device.empty()) { if (pkg_device != value || pkg_device.empty()) {
LOG(ERROR) << "Package is for product " << pkg_device << " but expected " << value; LOG(ERROR) << "Package is for product " << pkg_device << " but expected " << value;
return INSTALL_ERROR; return INSTALL_ERROR;
} }
// We allow the package to not have any serialno, but if it has a non-empty // We allow the package to not have any serialno, but if it has a non-empty
// value it should match. // value it should match.
property_get("ro.serialno", value, ""); value = android::base::GetProperty("ro.serialno", "");
const std::string& pkg_serial_no = metadata["serialno"]; const std::string& pkg_serial_no = metadata["serialno"];
if (!pkg_serial_no.empty() && pkg_serial_no != value) { if (!pkg_serial_no.empty() && pkg_serial_no != value) {
LOG(ERROR) << "Package is for serial " << pkg_serial_no; LOG(ERROR) << "Package is for serial " << pkg_serial_no;
return INSTALL_ERROR; return INSTALL_ERROR;
} }
if (metadata["ota-type"] != "AB") { if (metadata["ota-type"] != "AB") {
LOG(ERROR) << "Package is not A/B"; LOG(ERROR) << "Package is not A/B";
return INSTALL_ERROR; return INSTALL_ERROR;
} }
// Incremental updates should match the current build. // Incremental updates should match the current build.
property_get("ro.build.version.incremental", value, ""); value = android::base::GetProperty("ro.build.version.incremental", "");
const std::string& pkg_pre_build = metadata["pre-build-incremental"]; const std::string& pkg_pre_build = metadata["pre-build-incremental"];
if (!pkg_pre_build.empty() && pkg_pre_build != value) { if (!pkg_pre_build.empty() && pkg_pre_build != value) {
LOG(ERROR) << "Package is for source build " << pkg_pre_build << " but expected " << value; LOG(ERROR) << "Package is for source build " << pkg_pre_build << " but expected " << value;
return INSTALL_ERROR; return INSTALL_ERROR;
} }
property_get("ro.build.fingerprint", value, "");
const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
if (!pkg_pre_build_fingerprint.empty() &&
pkg_pre_build_fingerprint != value) {
LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint
<< " but expected " << value;
return INSTALL_ERROR;
}
// Check for downgrade version. value = android::base::GetProperty("ro.build.fingerprint", "");
int64_t build_timestamp = property_get_int64( const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
"ro.build.date.utc", std::numeric_limits<int64_t>::max()); if (!pkg_pre_build_fingerprint.empty() && pkg_pre_build_fingerprint != value) {
int64_t pkg_post_timestamp = 0; LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint << " but expected "
// We allow to full update to the same version we are running, in case there << value;
// is a problem with the current copy of that version. return INSTALL_ERROR;
if (metadata["post-timestamp"].empty() || }
!android::base::ParseInt(metadata["post-timestamp"].c_str(),
&pkg_post_timestamp) ||
pkg_post_timestamp < build_timestamp) {
if (metadata["ota-downgrade"] != "yes") {
LOG(ERROR) << "Update package is older than the current build, expected a build "
"newer than timestamp " << build_timestamp << " but package has "
"timestamp " << pkg_post_timestamp << " and downgrade not allowed.";
return INSTALL_ERROR;
}
if (pkg_pre_build_fingerprint.empty()) {
LOG(ERROR) << "Downgrade package must have a pre-build version set, not allowed.";
return INSTALL_ERROR;
}
}
return 0; // Check for downgrade version.
int64_t build_timestamp =
android::base::GetIntProperty("ro.build.date.utc", std::numeric_limits<int64_t>::max());
int64_t pkg_post_timestamp = 0;
// We allow to full update to the same version we are running, in case there
// is a problem with the current copy of that version.
if (metadata["post-timestamp"].empty() ||
!android::base::ParseInt(metadata["post-timestamp"].c_str(), &pkg_post_timestamp) ||
pkg_post_timestamp < build_timestamp) {
if (metadata["ota-downgrade"] != "yes") {
LOG(ERROR) << "Update package is older than the current build, expected a build "
"newer than timestamp "
<< build_timestamp << " but package has timestamp " << pkg_post_timestamp
<< " and downgrade not allowed.";
return INSTALL_ERROR;
}
if (pkg_pre_build_fingerprint.empty()) {
LOG(ERROR) << "Downgrade package must have a pre-build version set, not allowed.";
return INSTALL_ERROR;
}
}
return 0;
} }
static int static int

View File

@@ -892,8 +892,8 @@ static bool check_wipe_package(size_t wipe_package_size) {
// Extract metadata // Extract metadata
ZipArchiveHandle zip; ZipArchiveHandle zip;
int err = OpenArchiveFromMemory(reinterpret_cast<void*>(&wipe_package[0]), int err = OpenArchiveFromMemory(static_cast<void*>(&wipe_package[0]), wipe_package.size(),
wipe_package.size(), "wipe_package", &zip); "wipe_package", &zip);
if (err != 0) { if (err != 0) {
LOG(ERROR) << "Can't open wipe package : " << ErrorCodeString(err); LOG(ERROR) << "Can't open wipe package : " << ErrorCodeString(err);
return false; return false;
@@ -916,13 +916,11 @@ static bool check_wipe_package(size_t wipe_package_size) {
ota_type_matched = true; ota_type_matched = true;
} else if (android::base::StartsWith(line, "pre-device=")) { } else if (android::base::StartsWith(line, "pre-device=")) {
std::string device_type = line.substr(strlen("pre-device=")); std::string device_type = line.substr(strlen("pre-device="));
char real_device_type[PROPERTY_VALUE_MAX]; std::string real_device_type = android::base::GetProperty("ro.build.product", "");
property_get("ro.build.product", real_device_type, "");
device_type_matched = (device_type == real_device_type); device_type_matched = (device_type == real_device_type);
} else if (android::base::StartsWith(line, "serialno=")) { } else if (android::base::StartsWith(line, "serialno=")) {
std::string serial_no = line.substr(strlen("serialno=")); std::string serial_no = line.substr(strlen("serialno="));
char real_serial_no[PROPERTY_VALUE_MAX]; std::string real_serial_no = android::base::GetProperty("ro.serialno", "");
property_get("ro.serialno", real_serial_no, "");
has_serial_number = true; has_serial_number = true;
serial_number_matched = (serial_no == real_serial_no); serial_number_matched = (serial_no == real_serial_no);
} }
@@ -1361,15 +1359,15 @@ static void set_retry_bootloader_message(int retry_count, int argc, char** argv)
} }
static bool bootreason_in_blacklist() { static bool bootreason_in_blacklist() {
char bootreason[PROPERTY_VALUE_MAX]; std::string bootreason = android::base::GetProperty("ro.boot.bootreason", "");
if (property_get("ro.boot.bootreason", bootreason, nullptr) > 0) { if (!bootreason.empty()) {
for (const auto& str : bootreason_blacklist) { for (const auto& str : bootreason_blacklist) {
if (strcasecmp(str.c_str(), bootreason) == 0) { if (strcasecmp(str.c_str(), bootreason.c_str()) == 0) {
return true; return true;
} }
}
} }
return false; }
return false;
} }
static void log_failure_code(ErrorCode code, const char *update_package) { static void log_failure_code(ErrorCode code, const char *update_package) {