2015-12-06 06:21:27 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2018-04-19 02:10:49 +02:00
|
|
|
* update_verifier verifies the integrity of the partitions after an A/B OTA update. It gets invoked
|
|
|
|
* by init, and will only perform the verification if it's the first boot post an A/B OTA update
|
|
|
|
* (https://source.android.com/devices/tech/ota/ab/#after_reboot).
|
2015-12-06 06:21:27 +01:00
|
|
|
*
|
2018-04-19 02:10:49 +02:00
|
|
|
* update_verifier relies on device-mapper-verity (dm-verity) to capture any corruption on the
|
|
|
|
* partitions being verified (https://source.android.com/security/verifiedboot). The verification
|
|
|
|
* will be skipped, if dm-verity is not enabled on the device.
|
|
|
|
*
|
|
|
|
* Upon detecting verification failures, the device will be rebooted, although the trigger of the
|
|
|
|
* reboot depends on the dm-verity mode.
|
2017-03-02 00:31:25 +01:00
|
|
|
* enforcing mode: dm-verity reboots the device
|
|
|
|
* eio mode: dm-verity fails the read and update_verifier reboots the device
|
|
|
|
* other mode: not supported and update_verifier reboots the device
|
|
|
|
*
|
2018-04-19 02:10:49 +02:00
|
|
|
* All these reboots prevent the device from booting into a known corrupt state. If the device
|
|
|
|
* continuously fails to boot into the new slot, the bootloader should mark the slot as unbootable
|
|
|
|
* and trigger a fallback to the old slot.
|
2015-12-06 06:21:27 +01:00
|
|
|
*
|
2018-04-19 02:10:49 +02:00
|
|
|
* The current slot will be marked as having booted successfully if the verifier reaches the end
|
|
|
|
* after the verification.
|
2015-12-06 06:21:27 +01:00
|
|
|
*/
|
|
|
|
|
2017-04-26 23:30:56 +02:00
|
|
|
#include "update_verifier/update_verifier.h"
|
|
|
|
|
2017-01-19 03:41:53 +01:00
|
|
|
#include <dirent.h>
|
2016-03-01 01:08:06 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2019-03-13 23:30:13 +01:00
|
|
|
#include <stdint.h>
|
2016-03-01 01:08:06 +01:00
|
|
|
#include <stdio.h>
|
2019-03-13 23:30:13 +01:00
|
|
|
#include <stdlib.h>
|
2015-12-06 06:21:27 +01:00
|
|
|
#include <string.h>
|
2017-03-02 00:31:25 +01:00
|
|
|
#include <unistd.h>
|
2015-12-06 06:21:27 +01:00
|
|
|
|
2017-04-27 20:47:35 +02:00
|
|
|
#include <algorithm>
|
update_verifier: verify blocks in parallel
This CL is to change update_verifier to verify blocks in parallel to
maximize storage bandwidth, it also preallocate the buffer to avoid
vector allocation within reading loop.
Test:
care_map.txt:
system
16,0,517,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,483544,524288,524296
vendor
8,0,119,135,32770,32831,96150,98304,98306
With CL:
init: Service 'update_verifier_nonencrypted' (pid 711) exited with status 0 waiting took 2.978424 seconds
Without CL:
init: Service 'update_verifier_nonencrypted' (pid 695) exited with status 0 waiting took 4.466320 seconds
Bug: 63686531
Test: reboot with manual insert care_map.txt
Change-Id: Idf791865f15f6ff6cad89bf7ff230ee46c6adccc
(cherry picked from commit bd9664b5a01c8941949212973ca12be4df1b5d54)
2017-08-02 19:27:31 +02:00
|
|
|
#include <future>
|
2019-03-13 23:30:13 +01:00
|
|
|
#include <thread>
|
2016-03-01 01:08:06 +01:00
|
|
|
|
|
|
|
#include <android-base/file.h>
|
2016-08-06 03:00:04 +02:00
|
|
|
#include <android-base/logging.h>
|
2016-03-01 01:08:06 +01:00
|
|
|
#include <android-base/parseint.h>
|
2017-01-14 00:36:32 +01:00
|
|
|
#include <android-base/properties.h>
|
2016-03-01 01:08:06 +01:00
|
|
|
#include <android-base/strings.h>
|
|
|
|
#include <android-base/unique_fd.h>
|
2016-11-21 21:47:33 +01:00
|
|
|
#include <android/hardware/boot/1.0/IBootControl.h>
|
2019-03-13 23:30:13 +01:00
|
|
|
#include <android/os/IVold.h>
|
2019-01-23 04:57:28 +01:00
|
|
|
#include <binder/BinderService.h>
|
|
|
|
#include <binder/Status.h>
|
2017-03-02 00:31:25 +01:00
|
|
|
#include <cutils/android_reboot.h>
|
2016-11-21 21:47:33 +01:00
|
|
|
|
2018-05-11 19:41:44 +02:00
|
|
|
#include "care_map.pb.h"
|
Load-balancing update_verifier worker threads.
Prior to this CL, the block verification works were assigned based on
the pattern of the ranges, which could lead to unbalanced workloads. This
CL adds RangeSet::Split() and moves update_verifier over.
a) For the following care_map.txt on walleye:
system
20,0,347,348,540,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,524289,524291,524292,524348,529059
vendor
8,0,120,135,32770,32831,94564,98304,98306
Measured the time costs prior to and with this CL with the following
script.
$ cat test_update_verifier.sh
#!/bin/sh
adb shell stop
adb shell "cp /data/local/tmp/care_map.txt /data/ota_package/"
for i in $(seq 1 50)
do
echo "Iteration: $i"
adb shell "bootctl set-active-boot-slot 0"
adb shell "echo 3 > /proc/sys/vm/drop_caches"
adb shell "time /data/local/tmp/update_verifier"
sleep 3
done
Without this CL, the average time cost is 5.66s, while with the CL it's
reduced to 3.2s.
b) For the following care_map.txt, measured the performance on marlin:
system
18,0,271,286,457,8350,32770,33022,98306,98558,163842,164094,196609,204800,229378,229630,294914,295166,501547
vendor
10,0,42,44,85,2408,32770,32806,32807,36902,74242
It takes 12.9s and 5.6s without and with the CL respectively.
Fixes: 68553827
Test: recovery_unit_test
Test: Flash new build and trigger update_verifier. Check the balanced
block verification.
Change-Id: I5fa4bf09a84e6b9b0975ee5f522724464181333f
2017-11-04 08:08:08 +01:00
|
|
|
|
2016-11-21 21:47:33 +01:00
|
|
|
using android::sp;
|
|
|
|
using android::hardware::boot::V1_0::IBootControl;
|
|
|
|
using android::hardware::boot::V1_0::BoolResult;
|
|
|
|
using android::hardware::boot::V1_0::CommandResult;
|
2015-12-06 06:21:27 +01:00
|
|
|
|
2019-03-13 22:21:48 +01:00
|
|
|
// TODO(xunchang) remove the prefix and use a default path instead.
|
2018-09-14 21:52:02 +02:00
|
|
|
constexpr const char* kDefaultCareMapPrefix = "/data/ota_package/care_map";
|
|
|
|
|
2017-01-19 03:41:53 +01:00
|
|
|
// Find directories in format of "/sys/block/dm-X".
|
|
|
|
static int dm_name_filter(const dirent* de) {
|
|
|
|
if (android::base::StartsWith(de->d_name, "dm-")) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-14 21:52:02 +02:00
|
|
|
UpdateVerifier::UpdateVerifier()
|
|
|
|
: care_map_prefix_(kDefaultCareMapPrefix),
|
|
|
|
property_reader_([](const std::string& id) { return android::base::GetProperty(id, ""); }) {}
|
|
|
|
|
2018-09-20 00:45:28 +02:00
|
|
|
// 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() {
|
2017-04-26 23:30:56 +02:00
|
|
|
static constexpr auto DM_PATH_PREFIX = "/sys/block/";
|
2017-01-19 03:41:53 +01:00
|
|
|
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;
|
2018-09-20 00:45:28 +02:00
|
|
|
return {};
|
2017-01-19 03:41:53 +01:00
|
|
|
}
|
|
|
|
if (n == 0) {
|
2018-09-20 00:45:28 +02:00
|
|
|
LOG(ERROR) << "No dm block device found.";
|
|
|
|
return {};
|
2017-01-19 03:41:53 +01:00
|
|
|
}
|
|
|
|
|
2017-04-26 23:30:56 +02:00
|
|
|
static constexpr auto DM_PATH_SUFFIX = "/dm/name";
|
|
|
|
static constexpr auto DEV_PATH = "/dev/block/";
|
2018-09-20 00:45:28 +02:00
|
|
|
std::map<std::string, std::string> dm_block_devices;
|
2017-01-19 03:41:53 +01:00
|
|
|
while (n--) {
|
|
|
|
std::string path = DM_PATH_PREFIX + std::string(namelist[n]->d_name) + DM_PATH_SUFFIX;
|
|
|
|
std::string content;
|
|
|
|
if (!android::base::ReadFileToString(path, &content)) {
|
|
|
|
PLOG(WARNING) << "Failed to read " << path;
|
2017-05-08 19:41:28 +02:00
|
|
|
} else {
|
|
|
|
std::string dm_block_name = android::base::Trim(content);
|
|
|
|
// AVB is using 'vroot' for the root block device but we're expecting 'system'.
|
|
|
|
if (dm_block_name == "vroot") {
|
|
|
|
dm_block_name = "system";
|
2019-04-01 23:37:33 +02:00
|
|
|
} else if (android::base::EndsWith(dm_block_name, "-verity")) {
|
|
|
|
auto npos = dm_block_name.rfind("-verity");
|
|
|
|
dm_block_name = dm_block_name.substr(0, npos);
|
|
|
|
} else if (!android::base::GetProperty("ro.boot.avb_version", "").empty()) {
|
|
|
|
// Verified Boot 1.0 doesn't add a -verity suffix. On AVB 2 devices,
|
|
|
|
// if DAP is enabled, then a -verity suffix must be used to
|
|
|
|
// differentiate between dm-linear and dm-verity devices. If we get
|
|
|
|
// here, we're AVB 2 and looking at a non-verity partition.
|
|
|
|
continue;
|
2017-05-08 19:41:28 +02:00
|
|
|
}
|
2018-09-20 00:45:28 +02:00
|
|
|
|
|
|
|
dm_block_devices.emplace(dm_block_name, DEV_PATH + std::string(namelist[n]->d_name));
|
2017-01-19 03:41:53 +01:00
|
|
|
}
|
|
|
|
free(namelist[n]);
|
|
|
|
}
|
|
|
|
free(namelist);
|
|
|
|
|
2018-09-20 00:45:28 +02:00
|
|
|
return dm_block_devices;
|
|
|
|
}
|
2016-03-01 01:08:06 +01:00
|
|
|
|
2018-09-20 00:45:28 +02:00
|
|
|
bool UpdateVerifier::ReadBlocks(const std::string partition_name,
|
|
|
|
const std::string& dm_block_device, const RangeSet& ranges) {
|
Load-balancing update_verifier worker threads.
Prior to this CL, the block verification works were assigned based on
the pattern of the ranges, which could lead to unbalanced workloads. This
CL adds RangeSet::Split() and moves update_verifier over.
a) For the following care_map.txt on walleye:
system
20,0,347,348,540,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,524289,524291,524292,524348,529059
vendor
8,0,120,135,32770,32831,94564,98304,98306
Measured the time costs prior to and with this CL with the following
script.
$ cat test_update_verifier.sh
#!/bin/sh
adb shell stop
adb shell "cp /data/local/tmp/care_map.txt /data/ota_package/"
for i in $(seq 1 50)
do
echo "Iteration: $i"
adb shell "bootctl set-active-boot-slot 0"
adb shell "echo 3 > /proc/sys/vm/drop_caches"
adb shell "time /data/local/tmp/update_verifier"
sleep 3
done
Without this CL, the average time cost is 5.66s, while with the CL it's
reduced to 3.2s.
b) For the following care_map.txt, measured the performance on marlin:
system
18,0,271,286,457,8350,32770,33022,98306,98558,163842,164094,196609,204800,229378,229630,294914,295166,501547
vendor
10,0,42,44,85,2408,32770,32806,32807,36902,74242
It takes 12.9s and 5.6s without and with the CL respectively.
Fixes: 68553827
Test: recovery_unit_test
Test: Flash new build and trigger update_verifier. Check the balanced
block verification.
Change-Id: I5fa4bf09a84e6b9b0975ee5f522724464181333f
2017-11-04 08:08:08 +01:00
|
|
|
// RangeSet::Split() splits the ranges into multiple groups with same number of blocks (except for
|
|
|
|
// the last group).
|
update_verifier: verify blocks in parallel
This CL is to change update_verifier to verify blocks in parallel to
maximize storage bandwidth, it also preallocate the buffer to avoid
vector allocation within reading loop.
Test:
care_map.txt:
system
16,0,517,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,483544,524288,524296
vendor
8,0,119,135,32770,32831,96150,98304,98306
With CL:
init: Service 'update_verifier_nonencrypted' (pid 711) exited with status 0 waiting took 2.978424 seconds
Without CL:
init: Service 'update_verifier_nonencrypted' (pid 695) exited with status 0 waiting took 4.466320 seconds
Bug: 63686531
Test: reboot with manual insert care_map.txt
Change-Id: Idf791865f15f6ff6cad89bf7ff230ee46c6adccc
(cherry picked from commit bd9664b5a01c8941949212973ca12be4df1b5d54)
2017-08-02 19:27:31 +02:00
|
|
|
size_t thread_num = std::thread::hardware_concurrency() ?: 4;
|
Load-balancing update_verifier worker threads.
Prior to this CL, the block verification works were assigned based on
the pattern of the ranges, which could lead to unbalanced workloads. This
CL adds RangeSet::Split() and moves update_verifier over.
a) For the following care_map.txt on walleye:
system
20,0,347,348,540,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,524289,524291,524292,524348,529059
vendor
8,0,120,135,32770,32831,94564,98304,98306
Measured the time costs prior to and with this CL with the following
script.
$ cat test_update_verifier.sh
#!/bin/sh
adb shell stop
adb shell "cp /data/local/tmp/care_map.txt /data/ota_package/"
for i in $(seq 1 50)
do
echo "Iteration: $i"
adb shell "bootctl set-active-boot-slot 0"
adb shell "echo 3 > /proc/sys/vm/drop_caches"
adb shell "time /data/local/tmp/update_verifier"
sleep 3
done
Without this CL, the average time cost is 5.66s, while with the CL it's
reduced to 3.2s.
b) For the following care_map.txt, measured the performance on marlin:
system
18,0,271,286,457,8350,32770,33022,98306,98558,163842,164094,196609,204800,229378,229630,294914,295166,501547
vendor
10,0,42,44,85,2408,32770,32806,32807,36902,74242
It takes 12.9s and 5.6s without and with the CL respectively.
Fixes: 68553827
Test: recovery_unit_test
Test: Flash new build and trigger update_verifier. Check the balanced
block verification.
Change-Id: I5fa4bf09a84e6b9b0975ee5f522724464181333f
2017-11-04 08:08:08 +01:00
|
|
|
std::vector<RangeSet> groups = ranges.Split(thread_num);
|
update_verifier: verify blocks in parallel
This CL is to change update_verifier to verify blocks in parallel to
maximize storage bandwidth, it also preallocate the buffer to avoid
vector allocation within reading loop.
Test:
care_map.txt:
system
16,0,517,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,483544,524288,524296
vendor
8,0,119,135,32770,32831,96150,98304,98306
With CL:
init: Service 'update_verifier_nonencrypted' (pid 711) exited with status 0 waiting took 2.978424 seconds
Without CL:
init: Service 'update_verifier_nonencrypted' (pid 695) exited with status 0 waiting took 4.466320 seconds
Bug: 63686531
Test: reboot with manual insert care_map.txt
Change-Id: Idf791865f15f6ff6cad89bf7ff230ee46c6adccc
(cherry picked from commit bd9664b5a01c8941949212973ca12be4df1b5d54)
2017-08-02 19:27:31 +02:00
|
|
|
|
Load-balancing update_verifier worker threads.
Prior to this CL, the block verification works were assigned based on
the pattern of the ranges, which could lead to unbalanced workloads. This
CL adds RangeSet::Split() and moves update_verifier over.
a) For the following care_map.txt on walleye:
system
20,0,347,348,540,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,524289,524291,524292,524348,529059
vendor
8,0,120,135,32770,32831,94564,98304,98306
Measured the time costs prior to and with this CL with the following
script.
$ cat test_update_verifier.sh
#!/bin/sh
adb shell stop
adb shell "cp /data/local/tmp/care_map.txt /data/ota_package/"
for i in $(seq 1 50)
do
echo "Iteration: $i"
adb shell "bootctl set-active-boot-slot 0"
adb shell "echo 3 > /proc/sys/vm/drop_caches"
adb shell "time /data/local/tmp/update_verifier"
sleep 3
done
Without this CL, the average time cost is 5.66s, while with the CL it's
reduced to 3.2s.
b) For the following care_map.txt, measured the performance on marlin:
system
18,0,271,286,457,8350,32770,33022,98306,98558,163842,164094,196609,204800,229378,229630,294914,295166,501547
vendor
10,0,42,44,85,2408,32770,32806,32807,36902,74242
It takes 12.9s and 5.6s without and with the CL respectively.
Fixes: 68553827
Test: recovery_unit_test
Test: Flash new build and trigger update_verifier. Check the balanced
block verification.
Change-Id: I5fa4bf09a84e6b9b0975ee5f522724464181333f
2017-11-04 08:08:08 +01:00
|
|
|
std::vector<std::future<bool>> threads;
|
|
|
|
for (const auto& group : groups) {
|
2018-09-20 00:45:28 +02:00
|
|
|
auto thread_func = [&group, &dm_block_device, &partition_name]() {
|
update_verifier: verify blocks in parallel
This CL is to change update_verifier to verify blocks in parallel to
maximize storage bandwidth, it also preallocate the buffer to avoid
vector allocation within reading loop.
Test:
care_map.txt:
system
16,0,517,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,483544,524288,524296
vendor
8,0,119,135,32770,32831,96150,98304,98306
With CL:
init: Service 'update_verifier_nonencrypted' (pid 711) exited with status 0 waiting took 2.978424 seconds
Without CL:
init: Service 'update_verifier_nonencrypted' (pid 695) exited with status 0 waiting took 4.466320 seconds
Bug: 63686531
Test: reboot with manual insert care_map.txt
Change-Id: Idf791865f15f6ff6cad89bf7ff230ee46c6adccc
(cherry picked from commit bd9664b5a01c8941949212973ca12be4df1b5d54)
2017-08-02 19:27:31 +02:00
|
|
|
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dm_block_device.c_str(), O_RDONLY)));
|
|
|
|
if (fd.get() == -1) {
|
2018-09-20 00:45:28 +02:00
|
|
|
PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition_name;
|
update_verifier: verify blocks in parallel
This CL is to change update_verifier to verify blocks in parallel to
maximize storage bandwidth, it also preallocate the buffer to avoid
vector allocation within reading loop.
Test:
care_map.txt:
system
16,0,517,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,483544,524288,524296
vendor
8,0,119,135,32770,32831,96150,98304,98306
With CL:
init: Service 'update_verifier_nonencrypted' (pid 711) exited with status 0 waiting took 2.978424 seconds
Without CL:
init: Service 'update_verifier_nonencrypted' (pid 695) exited with status 0 waiting took 4.466320 seconds
Bug: 63686531
Test: reboot with manual insert care_map.txt
Change-Id: Idf791865f15f6ff6cad89bf7ff230ee46c6adccc
(cherry picked from commit bd9664b5a01c8941949212973ca12be4df1b5d54)
2017-08-02 19:27:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-01 01:08:06 +01:00
|
|
|
|
Load-balancing update_verifier worker threads.
Prior to this CL, the block verification works were assigned based on
the pattern of the ranges, which could lead to unbalanced workloads. This
CL adds RangeSet::Split() and moves update_verifier over.
a) For the following care_map.txt on walleye:
system
20,0,347,348,540,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,524289,524291,524292,524348,529059
vendor
8,0,120,135,32770,32831,94564,98304,98306
Measured the time costs prior to and with this CL with the following
script.
$ cat test_update_verifier.sh
#!/bin/sh
adb shell stop
adb shell "cp /data/local/tmp/care_map.txt /data/ota_package/"
for i in $(seq 1 50)
do
echo "Iteration: $i"
adb shell "bootctl set-active-boot-slot 0"
adb shell "echo 3 > /proc/sys/vm/drop_caches"
adb shell "time /data/local/tmp/update_verifier"
sleep 3
done
Without this CL, the average time cost is 5.66s, while with the CL it's
reduced to 3.2s.
b) For the following care_map.txt, measured the performance on marlin:
system
18,0,271,286,457,8350,32770,33022,98306,98558,163842,164094,196609,204800,229378,229630,294914,295166,501547
vendor
10,0,42,44,85,2408,32770,32806,32807,36902,74242
It takes 12.9s and 5.6s without and with the CL respectively.
Fixes: 68553827
Test: recovery_unit_test
Test: Flash new build and trigger update_verifier. Check the balanced
block verification.
Change-Id: I5fa4bf09a84e6b9b0975ee5f522724464181333f
2017-11-04 08:08:08 +01:00
|
|
|
static constexpr size_t kBlockSize = 4096;
|
|
|
|
std::vector<uint8_t> buf(1024 * kBlockSize);
|
2016-03-01 01:08:06 +01:00
|
|
|
|
Load-balancing update_verifier worker threads.
Prior to this CL, the block verification works were assigned based on
the pattern of the ranges, which could lead to unbalanced workloads. This
CL adds RangeSet::Split() and moves update_verifier over.
a) For the following care_map.txt on walleye:
system
20,0,347,348,540,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,524289,524291,524292,524348,529059
vendor
8,0,120,135,32770,32831,94564,98304,98306
Measured the time costs prior to and with this CL with the following
script.
$ cat test_update_verifier.sh
#!/bin/sh
adb shell stop
adb shell "cp /data/local/tmp/care_map.txt /data/ota_package/"
for i in $(seq 1 50)
do
echo "Iteration: $i"
adb shell "bootctl set-active-boot-slot 0"
adb shell "echo 3 > /proc/sys/vm/drop_caches"
adb shell "time /data/local/tmp/update_verifier"
sleep 3
done
Without this CL, the average time cost is 5.66s, while with the CL it's
reduced to 3.2s.
b) For the following care_map.txt, measured the performance on marlin:
system
18,0,271,286,457,8350,32770,33022,98306,98558,163842,164094,196609,204800,229378,229630,294914,295166,501547
vendor
10,0,42,44,85,2408,32770,32806,32807,36902,74242
It takes 12.9s and 5.6s without and with the CL respectively.
Fixes: 68553827
Test: recovery_unit_test
Test: Flash new build and trigger update_verifier. Check the balanced
block verification.
Change-Id: I5fa4bf09a84e6b9b0975ee5f522724464181333f
2017-11-04 08:08:08 +01:00
|
|
|
size_t block_count = 0;
|
2018-09-20 00:45:28 +02:00
|
|
|
for (const auto& [range_start, range_end] : group) {
|
update_verifier: verify blocks in parallel
This CL is to change update_verifier to verify blocks in parallel to
maximize storage bandwidth, it also preallocate the buffer to avoid
vector allocation within reading loop.
Test:
care_map.txt:
system
16,0,517,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,483544,524288,524296
vendor
8,0,119,135,32770,32831,96150,98304,98306
With CL:
init: Service 'update_verifier_nonencrypted' (pid 711) exited with status 0 waiting took 2.978424 seconds
Without CL:
init: Service 'update_verifier_nonencrypted' (pid 695) exited with status 0 waiting took 4.466320 seconds
Bug: 63686531
Test: reboot with manual insert care_map.txt
Change-Id: Idf791865f15f6ff6cad89bf7ff230ee46c6adccc
(cherry picked from commit bd9664b5a01c8941949212973ca12be4df1b5d54)
2017-08-02 19:27:31 +02:00
|
|
|
if (lseek64(fd.get(), static_cast<off64_t>(range_start) * kBlockSize, SEEK_SET) == -1) {
|
|
|
|
PLOG(ERROR) << "lseek to " << range_start << " failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t remain = (range_end - range_start) * kBlockSize;
|
|
|
|
while (remain > 0) {
|
|
|
|
size_t to_read = std::min(remain, 1024 * kBlockSize);
|
|
|
|
if (!android::base::ReadFully(fd.get(), buf.data(), to_read)) {
|
|
|
|
PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
remain -= to_read;
|
|
|
|
}
|
Load-balancing update_verifier worker threads.
Prior to this CL, the block verification works were assigned based on
the pattern of the ranges, which could lead to unbalanced workloads. This
CL adds RangeSet::Split() and moves update_verifier over.
a) For the following care_map.txt on walleye:
system
20,0,347,348,540,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,524289,524291,524292,524348,529059
vendor
8,0,120,135,32770,32831,94564,98304,98306
Measured the time costs prior to and with this CL with the following
script.
$ cat test_update_verifier.sh
#!/bin/sh
adb shell stop
adb shell "cp /data/local/tmp/care_map.txt /data/ota_package/"
for i in $(seq 1 50)
do
echo "Iteration: $i"
adb shell "bootctl set-active-boot-slot 0"
adb shell "echo 3 > /proc/sys/vm/drop_caches"
adb shell "time /data/local/tmp/update_verifier"
sleep 3
done
Without this CL, the average time cost is 5.66s, while with the CL it's
reduced to 3.2s.
b) For the following care_map.txt, measured the performance on marlin:
system
18,0,271,286,457,8350,32770,33022,98306,98558,163842,164094,196609,204800,229378,229630,294914,295166,501547
vendor
10,0,42,44,85,2408,32770,32806,32807,36902,74242
It takes 12.9s and 5.6s without and with the CL respectively.
Fixes: 68553827
Test: recovery_unit_test
Test: Flash new build and trigger update_verifier. Check the balanced
block verification.
Change-Id: I5fa4bf09a84e6b9b0975ee5f522724464181333f
2017-11-04 08:08:08 +01:00
|
|
|
block_count += (range_end - range_start);
|
2017-04-27 20:47:35 +02:00
|
|
|
}
|
Load-balancing update_verifier worker threads.
Prior to this CL, the block verification works were assigned based on
the pattern of the ranges, which could lead to unbalanced workloads. This
CL adds RangeSet::Split() and moves update_verifier over.
a) For the following care_map.txt on walleye:
system
20,0,347,348,540,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,524289,524291,524292,524348,529059
vendor
8,0,120,135,32770,32831,94564,98304,98306
Measured the time costs prior to and with this CL with the following
script.
$ cat test_update_verifier.sh
#!/bin/sh
adb shell stop
adb shell "cp /data/local/tmp/care_map.txt /data/ota_package/"
for i in $(seq 1 50)
do
echo "Iteration: $i"
adb shell "bootctl set-active-boot-slot 0"
adb shell "echo 3 > /proc/sys/vm/drop_caches"
adb shell "time /data/local/tmp/update_verifier"
sleep 3
done
Without this CL, the average time cost is 5.66s, while with the CL it's
reduced to 3.2s.
b) For the following care_map.txt, measured the performance on marlin:
system
18,0,271,286,457,8350,32770,33022,98306,98558,163842,164094,196609,204800,229378,229630,294914,295166,501547
vendor
10,0,42,44,85,2408,32770,32806,32807,36902,74242
It takes 12.9s and 5.6s without and with the CL respectively.
Fixes: 68553827
Test: recovery_unit_test
Test: Flash new build and trigger update_verifier. Check the balanced
block verification.
Change-Id: I5fa4bf09a84e6b9b0975ee5f522724464181333f
2017-11-04 08:08:08 +01:00
|
|
|
LOG(INFO) << "Finished reading " << block_count << " blocks on " << dm_block_device;
|
update_verifier: verify blocks in parallel
This CL is to change update_verifier to verify blocks in parallel to
maximize storage bandwidth, it also preallocate the buffer to avoid
vector allocation within reading loop.
Test:
care_map.txt:
system
16,0,517,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,483544,524288,524296
vendor
8,0,119,135,32770,32831,96150,98304,98306
With CL:
init: Service 'update_verifier_nonencrypted' (pid 711) exited with status 0 waiting took 2.978424 seconds
Without CL:
init: Service 'update_verifier_nonencrypted' (pid 695) exited with status 0 waiting took 4.466320 seconds
Bug: 63686531
Test: reboot with manual insert care_map.txt
Change-Id: Idf791865f15f6ff6cad89bf7ff230ee46c6adccc
(cherry picked from commit bd9664b5a01c8941949212973ca12be4df1b5d54)
2017-08-02 19:27:31 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
threads.emplace_back(std::async(std::launch::async, thread_func));
|
2017-01-14 00:36:32 +01:00
|
|
|
}
|
2016-03-01 01:08:06 +01:00
|
|
|
|
update_verifier: verify blocks in parallel
This CL is to change update_verifier to verify blocks in parallel to
maximize storage bandwidth, it also preallocate the buffer to avoid
vector allocation within reading loop.
Test:
care_map.txt:
system
16,0,517,556,32770,33084,98306,98620,163842,164156,229378,229692,294914,295228,483544,524288,524296
vendor
8,0,119,135,32770,32831,96150,98304,98306
With CL:
init: Service 'update_verifier_nonencrypted' (pid 711) exited with status 0 waiting took 2.978424 seconds
Without CL:
init: Service 'update_verifier_nonencrypted' (pid 695) exited with status 0 waiting took 4.466320 seconds
Bug: 63686531
Test: reboot with manual insert care_map.txt
Change-Id: Idf791865f15f6ff6cad89bf7ff230ee46c6adccc
(cherry picked from commit bd9664b5a01c8941949212973ca12be4df1b5d54)
2017-08-02 19:27:31 +02:00
|
|
|
bool ret = true;
|
|
|
|
for (auto& t : threads) {
|
|
|
|
ret = t.get() && ret;
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Finished reading blocks on " << dm_block_device << " with " << thread_num
|
|
|
|
<< " threads.";
|
|
|
|
return ret;
|
2016-03-01 01:08:06 +01:00
|
|
|
}
|
|
|
|
|
2018-09-20 00:45:28 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-14 21:52:02 +02:00
|
|
|
bool UpdateVerifier::ParseCareMap() {
|
2018-09-20 00:45:28 +02:00
|
|
|
partition_map_.clear();
|
|
|
|
|
2018-09-14 21:52:02 +02:00
|
|
|
std::string care_map_name = care_map_prefix_ + ".pb";
|
|
|
|
if (access(care_map_name.c_str(), R_OK) == -1) {
|
2019-03-13 22:21:48 +01:00
|
|
|
LOG(ERROR) << care_map_name << " doesn't exist";
|
|
|
|
return false;
|
2018-09-20 00:45:28 +02:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:41:44 +02:00
|
|
|
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;
|
2018-09-20 00:45:28 +02:00
|
|
|
return false;
|
2018-05-11 19:41:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string file_content;
|
|
|
|
if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) {
|
2018-09-20 00:45:28 +02:00
|
|
|
PLOG(WARNING) << "Failed to read " << care_map_name;
|
2018-05-11 19:41:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_content.empty()) {
|
2018-09-20 00:45:28 +02:00
|
|
|
LOG(WARNING) << "Unexpected empty care map";
|
2018-05-11 19:41:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-20 00:45:28 +02:00
|
|
|
recovery_update_verifier::CareMap care_map;
|
2018-05-11 19:41:44 +02:00
|
|
|
if (!care_map.ParseFromString(file_content)) {
|
2018-09-20 00:45:28 +02:00
|
|
|
LOG(WARNING) << "Failed to parse " << care_map_name << " in protobuf format.";
|
|
|
|
return false;
|
2018-05-11 19:41:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& partition : care_map.partitions()) {
|
|
|
|
if (partition.name().empty()) {
|
2018-09-20 00:45:28 +02:00
|
|
|
LOG(WARNING) << "Unexpected empty partition name.";
|
2018-05-11 19:41:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (partition.ranges().empty()) {
|
2018-09-20 00:45:28 +02:00
|
|
|
LOG(WARNING) << "Unexpected block ranges for partition " << partition.name();
|
2018-05-11 19:41:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-20 00:45:28 +02:00
|
|
|
RangeSet ranges = RangeSet::Parse(partition.ranges());
|
|
|
|
if (!ranges) {
|
|
|
|
LOG(WARNING) << "Error parsing RangeSet string " << partition.ranges();
|
2018-05-11 19:41:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-20 00:45:28 +02:00
|
|
|
|
2018-09-14 21:52:02 +02:00
|
|
|
// Continues to check other partitions if there is a fingerprint mismatch.
|
|
|
|
if (partition.id().empty() || partition.id() == "unknown") {
|
|
|
|
LOG(WARNING) << "Skip reading partition " << partition.name()
|
|
|
|
<< ": property_id is not provided to get fingerprint.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string fingerprint = property_reader_(partition.id());
|
|
|
|
if (fingerprint != partition.fingerprint()) {
|
|
|
|
LOG(WARNING) << "Skip reading partition " << partition.name() << ": fingerprint "
|
|
|
|
<< fingerprint << " doesn't match the expected value "
|
|
|
|
<< partition.fingerprint();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-09-20 00:45:28 +02:00
|
|
|
partition_map_.emplace(partition.name(), ranges);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (partition_map_.empty()) {
|
|
|
|
LOG(WARNING) << "No partition to verify";
|
|
|
|
return false;
|
2018-05-11 19:41:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-14 21:52:02 +02:00
|
|
|
void UpdateVerifier::set_care_map_prefix(const std::string& prefix) {
|
|
|
|
care_map_prefix_ = prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateVerifier::set_property_reader(
|
|
|
|
const std::function<std::string(const std::string&)>& property_reader) {
|
|
|
|
property_reader_ = property_reader;
|
|
|
|
}
|
|
|
|
|
2017-03-02 00:31:25 +01:00
|
|
|
static int reboot_device() {
|
|
|
|
if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) == -1) {
|
|
|
|
LOG(ERROR) << "Failed to reboot.";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (true) pause();
|
|
|
|
}
|
|
|
|
|
2017-04-26 23:30:56 +02:00
|
|
|
int update_verifier(int argc, char** argv) {
|
2015-12-06 06:21:27 +01:00
|
|
|
for (int i = 1; i < argc; i++) {
|
2016-08-06 03:00:04 +02:00
|
|
|
LOG(INFO) << "Started with arg " << i << ": " << argv[i];
|
2015-12-06 06:21:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-20 20:34:00 +01:00
|
|
|
sp<IBootControl> module = IBootControl::getService();
|
2016-11-21 21:47:33 +01:00
|
|
|
if (module == nullptr) {
|
2016-08-06 03:00:04 +02:00
|
|
|
LOG(ERROR) << "Error getting bootctrl module.";
|
2017-03-02 00:31:25 +01:00
|
|
|
return reboot_device();
|
2015-12-06 06:21:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-21 21:47:33 +01:00
|
|
|
uint32_t current_slot = module->getCurrentSlot();
|
|
|
|
BoolResult is_successful = module->isSlotMarkedSuccessful(current_slot);
|
|
|
|
LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful="
|
|
|
|
<< static_cast<int32_t>(is_successful);
|
2015-12-06 06:21:27 +01:00
|
|
|
|
2016-11-21 21:47:33 +01:00
|
|
|
if (is_successful == BoolResult::FALSE) {
|
2015-12-06 06:21:27 +01:00
|
|
|
// The current slot has not booted successfully.
|
2017-03-10 23:21:25 +01:00
|
|
|
|
2017-08-08 00:47:27 +02:00
|
|
|
bool skip_verification = false;
|
2017-01-14 00:36:32 +01:00
|
|
|
std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", "");
|
|
|
|
if (verity_mode.empty()) {
|
2018-04-19 02:10:49 +02:00
|
|
|
// Skip the verification if ro.boot.veritymode property is not set. This could be a result
|
|
|
|
// that device doesn't support dm-verity, or has disabled that.
|
|
|
|
LOG(WARNING) << "dm-verity not enabled; marking without verification.";
|
2017-08-08 00:47:27 +02:00
|
|
|
skip_verification = true;
|
2017-01-14 00:36:32 +01:00
|
|
|
} else if (android::base::EqualsIgnoreCase(verity_mode, "eio")) {
|
2017-03-02 00:31:25 +01:00
|
|
|
// We shouldn't see verity in EIO mode if the current slot hasn't booted successfully before.
|
|
|
|
// Continue the verification until we fail to read some blocks.
|
|
|
|
LOG(WARNING) << "Found dm-verity in EIO mode.";
|
2017-08-08 00:47:27 +02:00
|
|
|
} else if (android::base::EqualsIgnoreCase(verity_mode, "disabled")) {
|
|
|
|
LOG(WARNING) << "dm-verity in disabled mode; marking without verification.";
|
|
|
|
skip_verification = true;
|
2017-01-14 00:36:32 +01:00
|
|
|
} else if (verity_mode != "enforcing") {
|
2018-04-19 02:10:49 +02:00
|
|
|
LOG(ERROR) << "Unexpected dm-verity mode: " << verity_mode << ", expecting enforcing.";
|
2017-03-02 00:31:25 +01:00
|
|
|
return reboot_device();
|
|
|
|
}
|
|
|
|
|
2017-08-08 00:47:27 +02:00
|
|
|
if (!skip_verification) {
|
2018-09-20 00:45:28 +02:00
|
|
|
UpdateVerifier verifier;
|
|
|
|
if (!verifier.ParseCareMap()) {
|
|
|
|
LOG(WARNING) << "Failed to parse the care map file, skipping verification";
|
|
|
|
} else if (!verifier.VerifyPartitions()) {
|
2017-08-08 00:47:27 +02:00
|
|
|
LOG(ERROR) << "Failed to verify all blocks in care map file.";
|
|
|
|
return reboot_device();
|
|
|
|
}
|
2016-03-01 01:08:06 +01:00
|
|
|
}
|
2015-12-06 06:21:27 +01:00
|
|
|
|
2019-01-23 04:57:28 +01:00
|
|
|
bool supports_checkpoint = false;
|
|
|
|
auto sm = android::defaultServiceManager();
|
|
|
|
android::sp<android::IBinder> binder = sm->getService(android::String16("vold"));
|
|
|
|
if (binder) {
|
|
|
|
auto vold = android::interface_cast<android::os::IVold>(binder);
|
|
|
|
android::binder::Status status = vold->supportsCheckpoint(&supports_checkpoint);
|
|
|
|
if (!status.isOk()) {
|
|
|
|
LOG(ERROR) << "Failed to check if checkpoints supported. Continuing";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Failed to obtain vold Binder. Continuing";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!supports_checkpoint) {
|
|
|
|
CommandResult cr;
|
|
|
|
module->markBootSuccessful([&cr](CommandResult result) { cr = result; });
|
|
|
|
if (!cr.success) {
|
|
|
|
LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg;
|
|
|
|
return reboot_device();
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Marked slot " << current_slot << " as booted successfully.";
|
2019-11-15 00:07:25 +01:00
|
|
|
// Clears the warm reset flag for next reboot.
|
|
|
|
if (!android::base::SetProperty("ota.warm_reset", "0")) {
|
|
|
|
LOG(WARNING) << "Failed to reset the warm reset flag";
|
|
|
|
}
|
2019-01-23 04:57:28 +01:00
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Deferred marking slot " << current_slot << " as booted successfully.";
|
2015-12-06 06:21:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-06 03:00:04 +02:00
|
|
|
LOG(INFO) << "Leaving update_verifier.";
|
2015-12-06 06:21:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|