summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorandroid-build-team Robot <android-build-team-robot@google.com>2018-09-23 05:01:58 +0200
committerandroid-build-team Robot <android-build-team-robot@google.com>2018-09-23 05:01:58 +0200
commitce5781640feb1b819258f6eb37dfa4640364b588 (patch)
tree953295f16c621146f623963000f345f9307814af
parentSnap for 5019810 from 6fcfbb46f8e32385ef156e84543912ed0fa758b8 to qt-release (diff)
parentMerge "Refactor update_verifier into a class" am: ff29eb7f12 am: 4c65ab54c1 (diff)
downloadandroid_bootable_recovery-ce5781640feb1b819258f6eb37dfa4640364b588.tar
android_bootable_recovery-ce5781640feb1b819258f6eb37dfa4640364b588.tar.gz
android_bootable_recovery-ce5781640feb1b819258f6eb37dfa4640364b588.tar.bz2
android_bootable_recovery-ce5781640feb1b819258f6eb37dfa4640364b588.tar.lz
android_bootable_recovery-ce5781640feb1b819258f6eb37dfa4640364b588.tar.xz
android_bootable_recovery-ce5781640feb1b819258f6eb37dfa4640364b588.tar.zst
android_bootable_recovery-ce5781640feb1b819258f6eb37dfa4640364b588.zip
-rw-r--r--Android.bp1
-rw-r--r--Android.mk9
-rw-r--r--logging.cpp1
-rw-r--r--otautil/Android.bp1
-rw-r--r--otautil/include/otautil/parse_install_logs.h33
-rw-r--r--otautil/parse_install_logs.cpp114
-rw-r--r--recovery-persist.cpp56
-rw-r--r--tests/component/update_verifier_test.cpp63
-rw-r--r--tests/unit/parse_install_logs_test.cpp75
-rw-r--r--update_verifier/Android.bp5
-rw-r--r--update_verifier/care_map.proto2
-rw-r--r--update_verifier/include/update_verifier/update_verifier.h42
-rw-r--r--update_verifier/update_verifier.cpp158
13 files changed, 449 insertions, 111 deletions
diff --git a/Android.bp b/Android.bp
index afab76c20..76e69859c 100644
--- a/Android.bp
+++ b/Android.bp
@@ -256,6 +256,7 @@ cc_binary {
shared_libs: [
"libbase",
"liblog",
+ "libmetricslogger",
],
static_libs: [
diff --git a/Android.mk b/Android.mk
index 80d107dc4..7be123069 100644
--- a/Android.mk
+++ b/Android.mk
@@ -71,10 +71,13 @@ LOCAL_REQUIRED_MODULES += \
endif
endif
+# On A/B devices recovery-persist reads the recovery related file from the persist storage and
+# copies them into /data/misc/recovery. Then, for both A/B and non-A/B devices, recovery-persist
+# parses the last_install file and reports the embedded update metrics. Also, the last_install file
+# will be deteleted after the report.
+LOCAL_REQUIRED_MODULES += recovery-persist
ifeq ($(BOARD_CACHEIMAGE_PARTITION_SIZE),)
-LOCAL_REQUIRED_MODULES += \
- recovery-persist \
- recovery-refresh
+LOCAL_REQUIRED_MODULES += recovery-refresh
endif
include $(BUILD_PHONY_PACKAGE)
diff --git a/logging.cpp b/logging.cpp
index d5af72aad..50642a26b 100644
--- a/logging.cpp
+++ b/logging.cpp
@@ -221,6 +221,7 @@ void copy_logs(bool modified_flash, bool has_cache) {
chown(LAST_KMSG_FILE, AID_SYSTEM, AID_SYSTEM);
chmod(LAST_LOG_FILE, 0640);
chmod(LAST_INSTALL_FILE, 0644);
+ chown(LAST_INSTALL_FILE, AID_SYSTEM, AID_SYSTEM);
sync();
}
diff --git a/otautil/Android.bp b/otautil/Android.bp
index 56c7c9e89..41018dd2f 100644
--- a/otautil/Android.bp
+++ b/otautil/Android.bp
@@ -41,6 +41,7 @@ cc_library_static {
srcs: [
"dirutil.cpp",
"mounts.cpp",
+ "parse_install_logs.cpp",
"sysutil.cpp",
"thermalutil.cpp",
],
diff --git a/otautil/include/otautil/parse_install_logs.h b/otautil/include/otautil/parse_install_logs.h
new file mode 100644
index 000000000..135d29ccf
--- /dev/null
+++ b/otautil/include/otautil/parse_install_logs.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <stdint.h>
+
+#include <map>
+#include <string>
+#include <vector>
+
+constexpr const char* LAST_INSTALL_FILE = "/data/misc/recovery/last_install";
+constexpr const char* LAST_INSTALL_FILE_IN_CACHE = "/cache/recovery/last_install";
+
+// Parses the metrics of update applied under recovery mode in |lines|, and returns a map with
+// "name: value".
+std::map<std::string, int64_t> ParseRecoveryUpdateMetrics(const std::vector<std::string>& lines);
+// Parses the sideload history and update metrics in the last_install file. Returns a map with
+// entries as "metrics_name: value". If no such file exists, returns an empty map.
+std::map<std::string, int64_t> ParseLastInstall(const std::string& file_name);
diff --git a/otautil/parse_install_logs.cpp b/otautil/parse_install_logs.cpp
new file mode 100644
index 000000000..13a729921
--- /dev/null
+++ b/otautil/parse_install_logs.cpp
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "otautil/parse_install_logs.h"
+
+#include <unistd.h>
+
+#include <optional>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/parseint.h>
+#include <android-base/properties.h>
+#include <android-base/strings.h>
+
+constexpr const char* OTA_SIDELOAD_METRICS = "ota_sideload";
+
+// Here is an example of lines in last_install:
+// ...
+// time_total: 101
+// bytes_written_vendor: 51074
+// bytes_stashed_vendor: 200
+std::map<std::string, int64_t> ParseRecoveryUpdateMetrics(const std::vector<std::string>& lines) {
+ constexpr unsigned int kMiB = 1024 * 1024;
+ std::optional<int64_t> bytes_written_in_mib;
+ std::optional<int64_t> bytes_stashed_in_mib;
+ std::map<std::string, int64_t> metrics;
+ for (const auto& line : lines) {
+ size_t num_index = line.find(':');
+ if (num_index == std::string::npos) {
+ LOG(WARNING) << "Skip parsing " << line;
+ continue;
+ }
+
+ std::string num_string = android::base::Trim(line.substr(num_index + 1));
+ int64_t parsed_num;
+ if (!android::base::ParseInt(num_string, &parsed_num)) {
+ LOG(ERROR) << "Failed to parse numbers in " << line;
+ continue;
+ }
+
+ if (android::base::StartsWith(line, "bytes_written")) {
+ bytes_written_in_mib = bytes_written_in_mib.value_or(0) + parsed_num / kMiB;
+ } else if (android::base::StartsWith(line, "bytes_stashed")) {
+ bytes_stashed_in_mib = bytes_stashed_in_mib.value_or(0) + parsed_num / kMiB;
+ } else if (android::base::StartsWith(line, "time")) {
+ metrics.emplace("ota_time_total", parsed_num);
+ } else if (android::base::StartsWith(line, "uncrypt_time")) {
+ metrics.emplace("ota_uncrypt_time", parsed_num);
+ } else if (android::base::StartsWith(line, "source_build")) {
+ metrics.emplace("ota_source_version", parsed_num);
+ } else if (android::base::StartsWith(line, "temperature_start")) {
+ metrics.emplace("ota_temperature_start", parsed_num);
+ } else if (android::base::StartsWith(line, "temperature_end")) {
+ metrics.emplace("ota_temperature_end", parsed_num);
+ } else if (android::base::StartsWith(line, "temperature_max")) {
+ metrics.emplace("ota_temperature_max", parsed_num);
+ } else if (android::base::StartsWith(line, "error")) {
+ metrics.emplace("ota_non_ab_error_code", parsed_num);
+ } else if (android::base::StartsWith(line, "cause")) {
+ metrics.emplace("ota_non_ab_cause_code", parsed_num);
+ }
+ }
+
+ if (bytes_written_in_mib) {
+ metrics.emplace("ota_written_in_MiBs", bytes_written_in_mib.value());
+ }
+ if (bytes_stashed_in_mib) {
+ metrics.emplace("ota_stashed_in_MiBs", bytes_stashed_in_mib.value());
+ }
+
+ return metrics;
+}
+
+std::map<std::string, int64_t> ParseLastInstall(const std::string& file_name) {
+ if (access(file_name.c_str(), F_OK) != 0) {
+ return {};
+ }
+
+ std::string content;
+ if (!android::base::ReadFileToString(file_name, &content)) {
+ PLOG(ERROR) << "Failed to read " << file_name;
+ return {};
+ }
+
+ if (content.empty()) {
+ LOG(INFO) << "Empty last_install file";
+ return {};
+ }
+
+ std::vector<std::string> lines = android::base::Split(content, "\n");
+ auto metrics = ParseRecoveryUpdateMetrics(lines);
+
+ // LAST_INSTALL starts with "/sideload/package.zip" after a sideload.
+ if (android::base::Trim(lines[0]) == "/sideload/package.zip") {
+ int type = (android::base::GetProperty("ro.build.type", "") == "user") ? 1 : 0;
+ metrics.emplace(OTA_SIDELOAD_METRICS, type);
+ }
+
+ return metrics;
+}
diff --git a/recovery-persist.cpp b/recovery-persist.cpp
index d3ade6260..ebb42d223 100644
--- a/recovery-persist.cpp
+++ b/recovery-persist.cpp
@@ -35,19 +35,22 @@
#include <string.h>
#include <unistd.h>
+#include <limits>
#include <string>
#include <android-base/file.h>
#include <android-base/logging.h>
+#include <metricslogger/metrics_logger.h>
#include <private/android_logger.h> /* private pmsg functions */
#include "logging.h"
+#include "otautil/parse_install_logs.h"
-static const char *LAST_LOG_FILE = "/data/misc/recovery/last_log";
-static const char *LAST_PMSG_FILE = "/sys/fs/pstore/pmsg-ramoops-0";
-static const char *LAST_KMSG_FILE = "/data/misc/recovery/last_kmsg";
-static const char *LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops-0";
-static const char *ALT_LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops";
+constexpr const char* LAST_LOG_FILE = "/data/misc/recovery/last_log";
+constexpr const char* LAST_PMSG_FILE = "/sys/fs/pstore/pmsg-ramoops-0";
+constexpr const char* LAST_KMSG_FILE = "/data/misc/recovery/last_kmsg";
+constexpr const char* LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops-0";
+constexpr const char* ALT_LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops";
// close a file, log an error if the error indicator is set
static void check_and_fclose(FILE *fp, const char *name) {
@@ -109,6 +112,20 @@ ssize_t logsave(
return android::base::WriteStringToFile(buffer, destination.c_str());
}
+// Parses the LAST_INSTALL file and reports the update metrics saved under recovery mode.
+static void report_metrics_from_last_install(const std::string& file_name) {
+ auto metrics = ParseLastInstall(file_name);
+ // TODO(xunchang) report the installation result.
+ for (const auto& [event, value] : metrics) {
+ if (value > std::numeric_limits<int>::max()) {
+ LOG(WARNING) << event << " (" << value << ") exceeds integer max.";
+ } else {
+ LOG(INFO) << "Uploading " << value << " to " << event;
+ android::metricslogger::LogHistogram(event, value);
+ }
+ }
+}
+
int main(int argc, char **argv) {
/* Is /cache a mount?, we have been delivered where we are not wanted */
@@ -138,14 +155,18 @@ int main(int argc, char **argv) {
}
if (has_cache) {
- /*
- * TBD: Future location to move content from
- * /cache/recovery to /data/misc/recovery/
- */
- /* if --force-persist flag, then transfer pmsg data anyways */
- if ((argc <= 1) || !argv[1] || strcmp(argv[1], "--force-persist")) {
- return 0;
- }
+ // Collects and reports the non-a/b update metrics from last_install; and removes the file
+ // to avoid duplicate report.
+ report_metrics_from_last_install(LAST_INSTALL_FILE_IN_CACHE);
+ if (unlink(LAST_INSTALL_FILE_IN_CACHE) == -1) {
+ PLOG(ERROR) << "Failed to unlink " << LAST_INSTALL_FILE_IN_CACHE;
+ }
+
+ // TBD: Future location to move content from /cache/recovery to /data/misc/recovery/
+ // if --force-persist flag, then transfer pmsg data anyways
+ if ((argc <= 1) || !argv[1] || strcmp(argv[1], "--force-persist")) {
+ return 0;
+ }
}
/* Is there something in pmsg? */
@@ -157,6 +178,15 @@ int main(int argc, char **argv) {
__android_log_pmsg_file_read(
LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", logsave, NULL);
+ // For those device without /cache, the last_install file has been copied to
+ // /data/misc/recovery from pmsg. Looks for the sideload history only.
+ if (!has_cache) {
+ report_metrics_from_last_install(LAST_INSTALL_FILE);
+ if (unlink(LAST_INSTALL_FILE) == -1) {
+ PLOG(ERROR) << "Failed to unlink " << LAST_INSTALL_FILE;
+ }
+ }
+
/* Is there a last console log too? */
if (rotated) {
if (!access(LAST_CONSOLE_FILE, R_OK)) {
diff --git a/tests/component/update_verifier_test.cpp b/tests/component/update_verifier_test.cpp
index a97071635..543e4278e 100644
--- a/tests/component/update_verifier_test.cpp
+++ b/tests/component/update_verifier_test.cpp
@@ -29,19 +29,29 @@
#include "care_map.pb.h"
+using namespace std::string_literals;
+
class UpdateVerifierTest : public ::testing::Test {
protected:
void SetUp() override {
std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", "");
verity_supported = android::base::EqualsIgnoreCase(verity_mode, "enforcing");
+
+ care_map_pb_ = care_map_dir_.path + "/care_map.pb"s;
+ care_map_txt_ = care_map_dir_.path + "/care_map.txt"s;
+ }
+
+ void TearDown() override {
+ unlink(care_map_pb_.c_str());
+ unlink(care_map_txt_.c_str());
}
// Returns a serialized string of the proto3 message according to the given partition info.
std::string ConstructProto(
std::vector<std::unordered_map<std::string, std::string>>& partitions) {
- UpdateVerifier::CareMap result;
+ recovery_update_verifier::CareMap result;
for (const auto& partition : partitions) {
- UpdateVerifier::CareMap::PartitionInfo info;
+ recovery_update_verifier::CareMap::PartitionInfo info;
if (partition.find("name") != partition.end()) {
info.set_name(partition.at("name"));
}
@@ -59,12 +69,16 @@ class UpdateVerifierTest : public ::testing::Test {
}
bool verity_supported;
- TemporaryFile care_map_file;
+ UpdateVerifier verifier_;
+
+ TemporaryDir care_map_dir_;
+ std::string care_map_pb_;
+ std::string care_map_txt_;
};
TEST_F(UpdateVerifierTest, verify_image_no_care_map) {
// Non-existing care_map is allowed.
- ASSERT_TRUE(verify_image("/doesntexist"));
+ ASSERT_FALSE(verifier_.ParseCareMap("/doesntexist"));
}
TEST_F(UpdateVerifierTest, verify_image_smoke) {
@@ -75,25 +89,27 @@ TEST_F(UpdateVerifierTest, verify_image_smoke) {
}
std::string content = "system\n2,0,1";
- ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_file.path));
- ASSERT_TRUE(verify_image(care_map_file.path));
+ ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_));
+ ASSERT_TRUE(verifier_.ParseCareMap(care_map_txt_));
+ ASSERT_TRUE(verifier_.VerifyPartitions());
// Leading and trailing newlines should be accepted.
- ASSERT_TRUE(android::base::WriteStringToFile("\n" + content + "\n\n", care_map_file.path));
- ASSERT_TRUE(verify_image(care_map_file.path));
+ ASSERT_TRUE(android::base::WriteStringToFile("\n" + content + "\n\n", care_map_txt_));
+ ASSERT_TRUE(verifier_.ParseCareMap(care_map_txt_));
+ ASSERT_TRUE(verifier_.VerifyPartitions());
}
TEST_F(UpdateVerifierTest, verify_image_empty_care_map) {
- ASSERT_FALSE(verify_image(care_map_file.path));
+ ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
}
TEST_F(UpdateVerifierTest, verify_image_wrong_lines) {
// The care map file can have only 2 / 4 / 6 lines.
- ASSERT_TRUE(android::base::WriteStringToFile("line1", care_map_file.path));
- ASSERT_FALSE(verify_image(care_map_file.path));
+ ASSERT_TRUE(android::base::WriteStringToFile("line1", care_map_txt_));
+ ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
- ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2\nline3", care_map_file.path));
- ASSERT_FALSE(verify_image(care_map_file.path));
+ ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2\nline3", care_map_txt_));
+ ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
}
TEST_F(UpdateVerifierTest, verify_image_malformed_care_map) {
@@ -104,8 +120,8 @@ TEST_F(UpdateVerifierTest, verify_image_malformed_care_map) {
}
std::string content = "system\n2,1,0";
- ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_file.path));
- ASSERT_FALSE(verify_image(care_map_file.path));
+ ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_));
+ ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
}
TEST_F(UpdateVerifierTest, verify_image_legacy_care_map) {
@@ -116,8 +132,8 @@ TEST_F(UpdateVerifierTest, verify_image_legacy_care_map) {
}
std::string content = "/dev/block/bootdevice/by-name/system\n2,1,0";
- ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_file.path));
- ASSERT_TRUE(verify_image(care_map_file.path));
+ ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_));
+ ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
}
TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_smoke) {
@@ -132,8 +148,9 @@ TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_smoke) {
};
std::string proto = ConstructProto(partitions);
- ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_file.path));
- ASSERT_TRUE(verify_image(care_map_file.path));
+ ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_));
+ ASSERT_TRUE(verifier_.ParseCareMap(care_map_pb_));
+ ASSERT_TRUE(verifier_.VerifyPartitions());
}
TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_missing_name) {
@@ -148,8 +165,8 @@ TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_missing_name) {
};
std::string proto = ConstructProto(partitions);
- ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_file.path));
- ASSERT_FALSE(verify_image(care_map_file.path));
+ ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_));
+ ASSERT_FALSE(verifier_.ParseCareMap(care_map_pb_));
}
TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_bad_ranges) {
@@ -164,6 +181,6 @@ TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_bad_ranges) {
};
std::string proto = ConstructProto(partitions);
- ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_file.path));
- ASSERT_FALSE(verify_image(care_map_file.path));
+ ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_));
+ ASSERT_FALSE(verifier_.ParseCareMap(care_map_pb_));
}
diff --git a/tests/unit/parse_install_logs_test.cpp b/tests/unit/parse_install_logs_test.cpp
new file mode 100644
index 000000000..8061f3be1
--- /dev/null
+++ b/tests/unit/parse_install_logs_test.cpp
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <map>
+#include <string>
+#include <vector>
+
+#include <android-base/file.h>
+#include <android-base/strings.h>
+#include <android-base/test_utils.h>
+#include <gtest/gtest.h>
+
+#include "otautil/parse_install_logs.h"
+
+TEST(ParseInstallLogsTest, EmptyFile) {
+ TemporaryFile last_install;
+
+ auto metrics = ParseLastInstall(last_install.path);
+ ASSERT_TRUE(metrics.empty());
+}
+
+TEST(ParseInstallLogsTest, SideloadSmoke) {
+ TemporaryFile last_install;
+ ASSERT_TRUE(android::base::WriteStringToFile("/cache/recovery/ota.zip\n0\n", last_install.path));
+ auto metrics = ParseLastInstall(last_install.path);
+ ASSERT_EQ(metrics.end(), metrics.find("ota_sideload"));
+
+ ASSERT_TRUE(android::base::WriteStringToFile("/sideload/package.zip\n0\n", last_install.path));
+ metrics = ParseLastInstall(last_install.path);
+ ASSERT_NE(metrics.end(), metrics.find("ota_sideload"));
+}
+
+TEST(ParseInstallLogsTest, ParseRecoveryUpdateMetrics) {
+ std::vector<std::string> lines = {
+ "/sideload/package.zip",
+ "0",
+ "time_total: 300",
+ "uncrypt_time: 40",
+ "source_build: 4973410",
+ "bytes_written_system: " + std::to_string(1200 * 1024 * 1024),
+ "bytes_stashed_system: " + std::to_string(300 * 1024 * 1024),
+ "bytes_written_vendor: " + std::to_string(40 * 1024 * 1024),
+ "bytes_stashed_vendor: " + std::to_string(50 * 1024 * 1024),
+ "temperature_start: 37000",
+ "temperature_end: 38000",
+ "temperature_max: 39000",
+ "error: 22",
+ "cause: 55",
+ };
+
+ auto metrics = ParseRecoveryUpdateMetrics(lines);
+
+ std::map<std::string, int64_t> expected_result = {
+ { "ota_time_total", 300 }, { "ota_uncrypt_time", 40 },
+ { "ota_source_version", 4973410 }, { "ota_written_in_MiBs", 1240 },
+ { "ota_stashed_in_MiBs", 350 }, { "ota_temperature_start", 37000 },
+ { "ota_temperature_end", 38000 }, { "ota_temperature_max", 39000 },
+ { "ota_non_ab_error_code", 22 }, { "ota_non_ab_cause_code", 55 },
+ };
+
+ ASSERT_EQ(expected_result, metrics);
+}
diff --git a/update_verifier/Android.bp b/update_verifier/Android.bp
index 7a860a149..1b84619af 100644
--- a/update_verifier/Android.bp
+++ b/update_verifier/Android.bp
@@ -15,9 +15,8 @@
cc_defaults {
name: "update_verifier_defaults",
- cflags: [
- "-Wall",
- "-Werror",
+ defaults: [
+ "recovery_defaults",
],
local_include_dirs: [
diff --git a/update_verifier/care_map.proto b/update_verifier/care_map.proto
index 442ddd4a9..15d3afa83 100644
--- a/update_verifier/care_map.proto
+++ b/update_verifier/care_map.proto
@@ -16,7 +16,7 @@
syntax = "proto3";
-package UpdateVerifier;
+package recovery_update_verifier;
option optimize_for = LITE_RUNTIME;
message CareMap {
diff --git a/update_verifier/include/update_verifier/update_verifier.h b/update_verifier/include/update_verifier/update_verifier.h
index 534384e1d..bdfabed0c 100644
--- a/update_verifier/include/update_verifier/update_verifier.h
+++ b/update_verifier/include/update_verifier/update_verifier.h
@@ -16,17 +16,45 @@
#pragma once
+#include <map>
#include <string>
+#include <vector>
-int update_verifier(int argc, char** argv);
+#include "otautil/rangeset.h"
+
+// The update verifier performs verification upon the first boot to a new slot on A/B devices.
+// During the verification, it reads all the blocks in the care_map. And if a failure happens,
+// it rejects the current boot and triggers a fallback.
-// Returns true to indicate a passing verification (or the error should be ignored); Otherwise
-// returns false on fatal errors, where we should reject the current boot and trigger a fallback.
-// This function tries to process the care_map.txt as protobuf message; and falls back to use the
-// plain text format if the parse failed.
-//
// Note that update_verifier should be backward compatible to not reject care_map.txt from old
// releases, which could otherwise fail to boot into the new release. For example, we've changed
// the care_map format between N and O. An O update_verifier would fail to work with N care_map.txt.
// This could be a result of sideloading an O OTA while the device having a pending N update.
-bool verify_image(const std::string& care_map_name);
+int update_verifier(int argc, char** argv);
+
+// The UpdateVerifier parses the content in the care map, and continues to verify the
+// partitions by reading the cared blocks if there's no format error in the file. Otherwise,
+// it should skip the verification to avoid bricking the device.
+class UpdateVerifier {
+ public:
+ // This function tries to process the care_map.pb as protobuf message; and falls back to use
+ // care_map.txt if the pb format file doesn't exist. If the parsing succeeds, put the result of
+ // the pair <partition_name, ranges> into the |partition_map_|.
+ bool ParseCareMap(const std::string& file_name = "");
+
+ // Verifies the new boot by reading all the cared blocks for partitions in |partition_map_|.
+ bool VerifyPartitions();
+
+ private:
+ // Parses the legacy care_map.txt in plain text format.
+ bool ParseCareMapPlainText(const std::string& content);
+
+ // Finds all the dm-enabled partitions, and returns a map of <partition_name, block_device>.
+ std::map<std::string, std::string> FindDmPartitions();
+
+ // Returns true if we successfully read the blocks in |ranges| of the |dm_block_device|.
+ bool ReadBlocks(const std::string partition_name, const std::string& dm_block_device,
+ const RangeSet& ranges);
+
+ std::map<std::string, RangeSet> partition_map_;
+};
diff --git a/update_verifier/update_verifier.cpp b/update_verifier/update_verifier.cpp
index 5e5aa1819..6e86ac9d3 100644
--- a/update_verifier/update_verifier.cpp
+++ b/update_verifier/update_verifier.cpp
@@ -48,8 +48,6 @@
#include <algorithm>
#include <future>
-#include <string>
-#include <vector>
#include <android-base/file.h>
#include <android-base/logging.h>
@@ -61,7 +59,6 @@
#include <cutils/android_reboot.h>
#include "care_map.pb.h"
-#include "otautil/rangeset.h"
using android::sp;
using android::hardware::boot::V1_0::IBootControl;
@@ -76,29 +73,25 @@ static int dm_name_filter(const dirent* de) {
return 0;
}
-static bool read_blocks(const std::string& partition, const std::string& range_str) {
- if (partition != "system" && partition != "vendor" && partition != "product") {
- LOG(ERROR) << "Invalid partition name \"" << partition << "\"";
- return false;
- }
- // Iterate the content of "/sys/block/dm-X/dm/name". If it matches one of "system", "vendor" or
- // "product", then dm-X is a dm-wrapped device for that target. We will later read all the
- // ("cared") blocks from "/dev/block/dm-X" to ensure the target partition's integrity.
+// Iterate the content of "/sys/block/dm-X/dm/name" and find all the dm-wrapped block devices.
+// We will later read all the ("cared") blocks from "/dev/block/dm-X" to ensure the target
+// partition's integrity.
+std::map<std::string, std::string> UpdateVerifier::FindDmPartitions() {
static constexpr auto DM_PATH_PREFIX = "/sys/block/";
dirent** namelist;
int n = scandir(DM_PATH_PREFIX, &namelist, dm_name_filter, alphasort);
if (n == -1) {
PLOG(ERROR) << "Failed to scan dir " << DM_PATH_PREFIX;
- return false;
+ return {};
}
if (n == 0) {
- LOG(ERROR) << "dm block device not found for " << partition;
- return false;
+ LOG(ERROR) << "No dm block device found.";
+ return {};
}
static constexpr auto DM_PATH_SUFFIX = "/dm/name";
static constexpr auto DEV_PATH = "/dev/block/";
- std::string dm_block_device;
+ std::map<std::string, std::string> dm_block_devices;
while (n--) {
std::string path = DM_PATH_PREFIX + std::string(namelist[n]->d_name) + DM_PATH_SUFFIX;
std::string content;
@@ -110,33 +103,18 @@ static bool read_blocks(const std::string& partition, const std::string& range_s
if (dm_block_name == "vroot") {
dm_block_name = "system";
}
- if (dm_block_name == partition) {
- dm_block_device = DEV_PATH + std::string(namelist[n]->d_name);
- while (n--) {
- free(namelist[n]);
- }
- break;
- }
+
+ dm_block_devices.emplace(dm_block_name, DEV_PATH + std::string(namelist[n]->d_name));
}
free(namelist[n]);
}
free(namelist);
- if (dm_block_device.empty()) {
- LOG(ERROR) << "Failed to find dm block device for " << partition;
- return false;
- }
-
- // For block range string, first integer 'count' equals 2 * total number of valid ranges,
- // followed by 'count' number comma separated integers. Every two integers reprensent a
- // block range with the first number included in range but second number not included.
- // For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150).
- RangeSet ranges = RangeSet::Parse(range_str);
- if (!ranges) {
- LOG(ERROR) << "Error parsing RangeSet string " << range_str;
- return false;
- }
+ return dm_block_devices;
+}
+bool UpdateVerifier::ReadBlocks(const std::string partition_name,
+ const std::string& dm_block_device, const RangeSet& ranges) {
// RangeSet::Split() splits the ranges into multiple groups with same number of blocks (except for
// the last group).
size_t thread_num = std::thread::hardware_concurrency() ?: 4;
@@ -144,10 +122,10 @@ static bool read_blocks(const std::string& partition, const std::string& range_s
std::vector<std::future<bool>> threads;
for (const auto& group : groups) {
- auto thread_func = [&group, &dm_block_device, &partition]() {
+ auto thread_func = [&group, &dm_block_device, &partition_name]() {
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dm_block_device.c_str(), O_RDONLY)));
if (fd.get() == -1) {
- PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition;
+ PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition_name;
return false;
}
@@ -155,9 +133,7 @@ static bool read_blocks(const std::string& partition, const std::string& range_s
std::vector<uint8_t> buf(1024 * kBlockSize);
size_t block_count = 0;
- for (const auto& range : group) {
- size_t range_start = range.first;
- size_t range_end = range.second;
+ for (const auto& [range_start, range_end] : group) {
if (lseek64(fd.get(), static_cast<off64_t>(range_start) * kBlockSize, SEEK_SET) == -1) {
PLOG(ERROR) << "lseek to " << range_start << " failed";
return false;
@@ -190,72 +166,130 @@ static bool read_blocks(const std::string& partition, const std::string& range_s
return ret;
}
-static bool process_care_map_plain_text(const std::string& care_map_contents) {
+bool UpdateVerifier::VerifyPartitions() {
+ auto dm_block_devices = FindDmPartitions();
+ if (dm_block_devices.empty()) {
+ LOG(ERROR) << "No dm-enabled block device is found.";
+ return false;
+ }
+
+ for (const auto& [partition_name, ranges] : partition_map_) {
+ if (dm_block_devices.find(partition_name) == dm_block_devices.end()) {
+ LOG(ERROR) << "Failed to find dm block device for " << partition_name;
+ return false;
+ }
+
+ if (!ReadBlocks(partition_name, dm_block_devices.at(partition_name), ranges)) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+bool UpdateVerifier::ParseCareMapPlainText(const std::string& content) {
// care_map file has up to six lines, where every two lines make a pair. Within each pair, the
// first line has the partition name (e.g. "system"), while the second line holds the ranges of
// all the blocks to verify.
- std::vector<std::string> lines =
- android::base::Split(android::base::Trim(care_map_contents), "\n");
+ auto lines = android::base::Split(android::base::Trim(content), "\n");
if (lines.size() != 2 && lines.size() != 4 && lines.size() != 6) {
- LOG(ERROR) << "Invalid lines in care_map: found " << lines.size()
- << " lines, expecting 2 or 4 or 6 lines.";
+ LOG(WARNING) << "Invalid lines in care_map: found " << lines.size()
+ << " lines, expecting 2 or 4 or 6 lines.";
return false;
}
for (size_t i = 0; i < lines.size(); i += 2) {
+ const std::string& partition_name = lines[i];
+ const std::string& range_str = lines[i + 1];
// We're seeing an N care_map.txt. Skip the verification since it's not compatible with O
// update_verifier (the last few metadata blocks can't be read via device mapper).
- if (android::base::StartsWith(lines[i], "/dev/block/")) {
+ if (android::base::StartsWith(partition_name, "/dev/block/")) {
LOG(WARNING) << "Found legacy care_map.txt; skipped.";
- return true;
+ return false;
}
- if (!read_blocks(lines[i], lines[i+1])) {
+
+ // For block range string, first integer 'count' equals 2 * total number of valid ranges,
+ // followed by 'count' number comma separated integers. Every two integers reprensent a
+ // block range with the first number included in range but second number not included.
+ // For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150).
+ RangeSet ranges = RangeSet::Parse(range_str);
+ if (!ranges) {
+ LOG(WARNING) << "Error parsing RangeSet string " << range_str;
return false;
}
+
+ partition_map_.emplace(partition_name, ranges);
}
return true;
}
-bool verify_image(const std::string& care_map_name) {
+bool UpdateVerifier::ParseCareMap(const std::string& file_name) {
+ partition_map_.clear();
+
+ std::string care_map_name = file_name;
+ if (care_map_name.empty()) {
+ std::string care_map_prefix = "/data/ota_package/care_map";
+ care_map_name = care_map_prefix + ".pb";
+ if (access(care_map_name.c_str(), R_OK) == -1) {
+ LOG(WARNING) << care_map_name
+ << " doesn't exist, falling back to read the care_map in plain text format.";
+ care_map_name = care_map_prefix + ".txt";
+ }
+ }
+
android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY)));
// If the device is flashed before the current boot, it may not have care_map.txt in
// /data/ota_package. To allow the device to continue booting in this situation, we should
// print a warning and skip the block verification.
if (care_map_fd.get() == -1) {
PLOG(WARNING) << "Failed to open " << care_map_name;
- return true;
+ return false;
}
std::string file_content;
if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) {
- PLOG(ERROR) << "Failed to read " << care_map_name;
+ PLOG(WARNING) << "Failed to read " << care_map_name;
return false;
}
if (file_content.empty()) {
- LOG(ERROR) << "Unexpected empty care map";
+ LOG(WARNING) << "Unexpected empty care map";
return false;
}
- UpdateVerifier::CareMap care_map;
- // Falls back to use the plain text version if we cannot parse the file as protobuf message.
+ if (android::base::EndsWith(care_map_name, ".txt")) {
+ return ParseCareMapPlainText(file_content);
+ }
+
+ recovery_update_verifier::CareMap care_map;
if (!care_map.ParseFromString(file_content)) {
- return process_care_map_plain_text(file_content);
+ LOG(WARNING) << "Failed to parse " << care_map_name << " in protobuf format.";
+ return false;
}
for (const auto& partition : care_map.partitions()) {
if (partition.name().empty()) {
- LOG(ERROR) << "Unexpected empty partition name.";
+ LOG(WARNING) << "Unexpected empty partition name.";
return false;
}
if (partition.ranges().empty()) {
- LOG(ERROR) << "Unexpected block ranges for partition " << partition.name();
+ LOG(WARNING) << "Unexpected block ranges for partition " << partition.name();
return false;
}
- if (!read_blocks(partition.name(), partition.ranges())) {
+ RangeSet ranges = RangeSet::Parse(partition.ranges());
+ if (!ranges) {
+ LOG(WARNING) << "Error parsing RangeSet string " << partition.ranges();
return false;
}
+
+ // TODO(xunchang) compare the fingerprint of the partition.
+ partition_map_.emplace(partition.name(), ranges);
+ }
+
+ if (partition_map_.empty()) {
+ LOG(WARNING) << "No partition to verify";
+ return false;
}
return true;
@@ -308,8 +342,10 @@ int update_verifier(int argc, char** argv) {
}
if (!skip_verification) {
- static constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt";
- if (!verify_image(CARE_MAP_FILE)) {
+ UpdateVerifier verifier;
+ if (!verifier.ParseCareMap()) {
+ LOG(WARNING) << "Failed to parse the care map file, skipping verification";
+ } else if (!verifier.VerifyPartitions()) {
LOG(ERROR) << "Failed to verify all blocks in care map file.";
return reboot_device();
}