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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program 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.
|
|
|
|
*
|
2017-03-02 00:31:25 +01:00
|
|
|
* Update_verifier relies on dm-verity to capture any corruption on the partitions
|
|
|
|
* being verified. And its behavior varies depending on the dm-verity mode.
|
|
|
|
* Upon detection of failures:
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* After a predefined number of failing boot attempts, the bootloader should
|
2016-03-01 01:08:06 +01:00
|
|
|
* mark the slot as unbootable and stops trying. Other dm-verity modes (
|
|
|
|
* for example, veritymode=EIO) are not accepted and simply lead to a
|
|
|
|
* verification failure.
|
2015-12-06 06:21:27 +01:00
|
|
|
*
|
|
|
|
* The current slot will be marked as having booted successfully if the
|
|
|
|
* verifier reaches the end after the verification.
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
#include <stdio.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>
|
2016-03-01 01:08:06 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#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>
|
2017-03-02 00:31:25 +01:00
|
|
|
#include <cutils/android_reboot.h>
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-02 00:31:25 +01:00
|
|
|
static bool read_blocks(const std::string& partition, const std::string& range_str) {
|
2017-04-01 01:36:12 +02:00
|
|
|
if (partition != "system" && partition != "vendor") {
|
|
|
|
LOG(ERROR) << "partition name must be system or vendor: " << partition;
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-19 03:41:53 +01:00
|
|
|
// Iterate the content of "/sys/block/dm-X/dm/name". If it matches "system"
|
|
|
|
// (or "vendor"), then dm-X is a dm-wrapped system/vendor partition.
|
|
|
|
// Afterwards, update_verifier will read every block on the care_map_file of
|
|
|
|
// "/dev/block/dm-X" to ensure the partition's integrity.
|
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;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (n == 0) {
|
|
|
|
LOG(ERROR) << "dm block device not found for " << partition;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-26 23:30:56 +02:00
|
|
|
static constexpr auto DM_PATH_SUFFIX = "/dm/name";
|
|
|
|
static constexpr auto DEV_PATH = "/dev/block/";
|
2017-01-19 03:41:53 +01:00
|
|
|
std::string dm_block_device;
|
|
|
|
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);
|
|
|
|
#ifdef BOARD_AVB_ENABLE
|
|
|
|
// AVB is using 'vroot' for the root block device but we're expecting 'system'.
|
|
|
|
if (dm_block_name == "vroot") {
|
|
|
|
dm_block_name = "system";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (dm_block_name == partition) {
|
|
|
|
dm_block_device = DEV_PATH + std::string(namelist[n]->d_name);
|
|
|
|
while (n--) {
|
|
|
|
free(namelist[n]);
|
|
|
|
}
|
|
|
|
break;
|
2017-01-19 03:41:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(namelist[n]);
|
|
|
|
}
|
|
|
|
free(namelist);
|
|
|
|
|
|
|
|
if (dm_block_device.empty()) {
|
|
|
|
LOG(ERROR) << "Failed to find dm block device for " << partition;
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-01 01:08:06 +01:00
|
|
|
|
2017-01-14 00:36:32 +01:00
|
|
|
// 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).
|
|
|
|
std::vector<std::string> ranges = android::base::Split(range_str, ",");
|
|
|
|
size_t range_count;
|
|
|
|
bool status = android::base::ParseUint(ranges[0], &range_count);
|
|
|
|
if (!status || (range_count == 0) || (range_count % 2 != 0) ||
|
|
|
|
(range_count != ranges.size() - 1)) {
|
|
|
|
LOG(ERROR) << "Error in parsing range string.";
|
|
|
|
return false;
|
|
|
|
}
|
update_verifier: Fix the wrong computation with group_range_count.
'group_range_count' doesn't properly consider the pair-wise range
structure. It may split the ranges into wrong pairs if it evaluates to
an odd number.
For example, for an input range string of "6,0,2,10,12,20,22" with 4
threads, group_range_count becomes 1. It would then try to verify (0,2),
(2,10), (10,12) and (12,20). Note that (2,10) and (12,20) are not valid
ranges to be verified, and with (20,22) uncovered.
Bug: 68343761
Test: Trigger update_verifier verification. Check the number of verified
blocks against the one in care_map.txt.
Change-Id: I7c5769325d9866be06c45e7dbcc0c8ea266de714
2017-10-28 08:39:45 +02:00
|
|
|
range_count /= 2;
|
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
|
|
|
std::vector<std::future<bool>> threads;
|
|
|
|
size_t thread_num = std::thread::hardware_concurrency() ?: 4;
|
update_verifier: Fix the wrong computation with group_range_count.
'group_range_count' doesn't properly consider the pair-wise range
structure. It may split the ranges into wrong pairs if it evaluates to
an odd number.
For example, for an input range string of "6,0,2,10,12,20,22" with 4
threads, group_range_count becomes 1. It would then try to verify (0,2),
(2,10), (10,12) and (12,20). Note that (2,10) and (12,20) are not valid
ranges to be verified, and with (20,22) uncovered.
Bug: 68343761
Test: Trigger update_verifier verification. Check the number of verified
blocks against the one in care_map.txt.
Change-Id: I7c5769325d9866be06c45e7dbcc0c8ea266de714
2017-10-28 08:39:45 +02:00
|
|
|
thread_num = std::min(thread_num, range_count);
|
|
|
|
size_t group_range_count = (range_count + thread_num - 1) / 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
|
|
|
|
|
|
|
for (size_t t = 0; t < thread_num; t++) {
|
|
|
|
auto thread_func = [t, group_range_count, &dm_block_device, &ranges, &partition]() {
|
|
|
|
size_t blk_count = 0;
|
|
|
|
static constexpr size_t kBlockSize = 4096;
|
|
|
|
std::vector<uint8_t> buf(1024 * kBlockSize);
|
|
|
|
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;
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-01 01:08:06 +01:00
|
|
|
|
update_verifier: Fix the wrong computation with group_range_count.
'group_range_count' doesn't properly consider the pair-wise range
structure. It may split the ranges into wrong pairs if it evaluates to
an odd number.
For example, for an input range string of "6,0,2,10,12,20,22" with 4
threads, group_range_count becomes 1. It would then try to verify (0,2),
(2,10), (10,12) and (12,20). Note that (2,10) and (12,20) are not valid
ranges to be verified, and with (20,22) uncovered.
Bug: 68343761
Test: Trigger update_verifier verification. Check the number of verified
blocks against the one in care_map.txt.
Change-Id: I7c5769325d9866be06c45e7dbcc0c8ea266de714
2017-10-28 08:39:45 +02:00
|
|
|
for (size_t i = group_range_count * 2 * t + 1;
|
|
|
|
i < std::min(group_range_count * 2 * (t + 1) + 1, ranges.size()); i += 2) {
|
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
|
|
|
unsigned int range_start, range_end;
|
|
|
|
bool parse_status = android::base::ParseUint(ranges[i], &range_start);
|
|
|
|
parse_status = parse_status && android::base::ParseUint(ranges[i + 1], &range_end);
|
|
|
|
if (!parse_status || range_start >= range_end) {
|
|
|
|
LOG(ERROR) << "Invalid range pair " << ranges[i] << ", " << ranges[i + 1];
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
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;
|
|
|
|
}
|
|
|
|
blk_count += (range_end - range_start);
|
2017-04-27 20:47:35 +02: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
|
|
|
LOG(INFO) << "Finished reading " << blk_count << " blocks on " << dm_block_device;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-22 00:15:31 +02:00
|
|
|
// 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.
|
|
|
|
// 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.
|
2017-04-26 23:30:56 +02:00
|
|
|
bool verify_image(const std::string& care_map_name) {
|
2017-07-22 00:15:31 +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;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Care map file has four lines (two lines if vendor partition is not present):
|
|
|
|
// First line has the block partition name (system/vendor).
|
|
|
|
// Second line holds all ranges of blocks to verify.
|
|
|
|
// The next two lines have the same format but for vendor partition.
|
|
|
|
std::string file_content;
|
|
|
|
if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) {
|
|
|
|
LOG(ERROR) << "Error reading care map contents to string.";
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-01 01:08:06 +01:00
|
|
|
|
2017-07-22 00:15:31 +02:00
|
|
|
std::vector<std::string> lines;
|
|
|
|
lines = android::base::Split(android::base::Trim(file_content), "\n");
|
|
|
|
if (lines.size() != 2 && lines.size() != 4) {
|
|
|
|
LOG(ERROR) << "Invalid lines in care_map: found " << lines.size()
|
|
|
|
<< " lines, expecting 2 or 4 lines.";
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-01 01:08:06 +01:00
|
|
|
|
2017-07-22 00:15:31 +02:00
|
|
|
for (size_t i = 0; i < lines.size(); i += 2) {
|
|
|
|
// 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/")) {
|
|
|
|
LOG(WARNING) << "Found legacy care_map.txt; skipped.";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!read_blocks(lines[i], lines[i+1])) {
|
|
|
|
return false;
|
2016-03-01 01:08:06 +01:00
|
|
|
}
|
2017-07-22 00:15:31 +02:00
|
|
|
}
|
2016-03-01 01:08:06 +01:00
|
|
|
|
2017-07-22 00:15:31 +02:00
|
|
|
return true;
|
2016-03-01 01:08:06 +01:00
|
|
|
}
|
|
|
|
|
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-05-08 19:41:28 +02:00
|
|
|
#if defined(PRODUCT_SUPPORTS_VERITY) || defined(BOARD_AVB_ENABLE)
|
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()) {
|
2017-08-08 00:47:27 +02:00
|
|
|
// With AVB it's possible to disable verification entirely and
|
|
|
|
// in this case ro.boot.veritymode is empty.
|
|
|
|
#if defined(BOARD_AVB_ENABLE)
|
|
|
|
LOG(WARNING) << "verification has been disabled; marking without verification.";
|
|
|
|
skip_verification = true;
|
|
|
|
#else
|
2016-03-01 01:08:06 +01:00
|
|
|
LOG(ERROR) << "Failed to get dm-verity mode.";
|
2017-03-02 00:31:25 +01:00
|
|
|
return reboot_device();
|
2017-08-08 00:47:27 +02:00
|
|
|
#endif
|
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") {
|
2016-03-01 01:08:06 +01: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) {
|
|
|
|
static constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt";
|
|
|
|
if (!verify_image(CARE_MAP_FILE)) {
|
|
|
|
LOG(ERROR) << "Failed to verify all blocks in care map file.";
|
|
|
|
return reboot_device();
|
|
|
|
}
|
2016-03-01 01:08:06 +01:00
|
|
|
}
|
2017-03-10 23:21:25 +01:00
|
|
|
#else
|
|
|
|
LOG(WARNING) << "dm-verity not enabled; marking without verification.";
|
|
|
|
#endif
|
2015-12-06 06:21:27 +01:00
|
|
|
|
2016-11-21 21:47:33 +01:00
|
|
|
CommandResult cr;
|
|
|
|
module->markBootSuccessful([&cr](CommandResult result) { cr = result; });
|
|
|
|
if (!cr.success) {
|
|
|
|
LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg;
|
2017-03-02 00:31:25 +01:00
|
|
|
return reboot_device();
|
2015-12-06 06:21:27 +01:00
|
|
|
}
|
2016-08-06 03:00:04 +02:00
|
|
|
LOG(INFO) << "Marked 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;
|
|
|
|
}
|