Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit efc35594 authored by Tao Bao's avatar Tao Bao
Browse files

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

Test: Apply two A/B incremental OTAs with the new recovery image. The
      one with incorrect pre-build should be blocked, while the other
      works fine.

Change-Id: I94d97eb8798599da1630f66343fb603e87464187
parent 8031c2d2
Loading
Loading
Loading
Loading
+66 −68
Original line number Original line Diff line number Diff line
@@ -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,8 +139,7 @@ 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;
@@ -152,9 +151,8 @@ static int check_newer_ab_build(ZipArchiveHandle zip)
      metadata[line.substr(0, eq)] = line.substr(eq + 1);
      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;
@@ -163,7 +161,7 @@ static int check_newer_ab_build(ZipArchiveHandle zip)


  // 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;
@@ -176,35 +174,35 @@ static int check_newer_ab_build(ZipArchiveHandle zip)
  }
  }


  // 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, "");

  value = android::base::GetProperty("ro.build.fingerprint", "");
  const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
  const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
    if (!pkg_pre_build_fingerprint.empty() &&
  if (!pkg_pre_build_fingerprint.empty() && pkg_pre_build_fingerprint != value) {
        pkg_pre_build_fingerprint != value) {
    LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint << " but expected "
        LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint
               << value;
                   << " but expected " << value;
    return INSTALL_ERROR;
    return INSTALL_ERROR;
  }
  }


  // Check for downgrade version.
  // Check for downgrade version.
    int64_t build_timestamp = property_get_int64(
  int64_t build_timestamp =
            "ro.build.date.utc", std::numeric_limits<int64_t>::max());
      android::base::GetIntProperty("ro.build.date.utc", std::numeric_limits<int64_t>::max());
  int64_t pkg_post_timestamp = 0;
  int64_t pkg_post_timestamp = 0;
  // We allow to full update to the same version we are running, in case there
  // 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.
  // is a problem with the current copy of that version.
  if (metadata["post-timestamp"].empty() ||
  if (metadata["post-timestamp"].empty() ||
        !android::base::ParseInt(metadata["post-timestamp"].c_str(),
      !android::base::ParseInt(metadata["post-timestamp"].c_str(), &pkg_post_timestamp) ||
                                 &pkg_post_timestamp) ||
      pkg_post_timestamp < build_timestamp) {
      pkg_post_timestamp < build_timestamp) {
    if (metadata["ota-downgrade"] != "yes") {
    if (metadata["ota-downgrade"] != "yes") {
      LOG(ERROR) << "Update package is older than the current build, expected a build "
      LOG(ERROR) << "Update package is older than the current build, expected a build "
                       "newer than timestamp " << build_timestamp << " but package has "
                    "newer than timestamp "
                       "timestamp " << pkg_post_timestamp << " and downgrade not allowed.";
                 << build_timestamp << " but package has timestamp " << pkg_post_timestamp
                 << " and downgrade not allowed.";
      return INSTALL_ERROR;
      return INSTALL_ERROR;
    }
    }
    if (pkg_pre_build_fingerprint.empty()) {
    if (pkg_pre_build_fingerprint.empty()) {
+12 −14
Original line number Original line Diff line number Diff line
@@ -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,10 +1359,10 @@ 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;
      }
      }
    }
    }