blob: 1c9be2d5810a4f22b73c8ef5a3ea89b3d747fd8b [file] [log] [blame]
Tao Bao7197ee02015-12-05 21:21:27 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * This program verifies the integrity of the partitions after an A/B OTA
19 * update. It gets invoked by init, and will only perform the verification if
20 * it's the first boot post an A/B OTA update.
21 *
22 * It relies on dm-verity to capture any corruption on the partitions being
23 * verified. dm-verity must be in enforcing mode, so that it will reboot the
24 * device on dm-verity failures. When that happens, the bootloader should
Tianjie Xud007cf22016-02-29 16:08:06 -080025 * mark the slot as unbootable and stops trying. Other dm-verity modes (
26 * for example, veritymode=EIO) are not accepted and simply lead to a
27 * verification failure.
Tao Bao7197ee02015-12-05 21:21:27 -080028 *
29 * The current slot will be marked as having booted successfully if the
30 * verifier reaches the end after the verification.
Tao Bao7197ee02015-12-05 21:21:27 -080031 */
32
Tianjie Xud007cf22016-02-29 16:08:06 -080033#include <errno.h>
34#include <fcntl.h>
35#include <stdio.h>
Tao Bao7197ee02015-12-05 21:21:27 -080036#include <string.h>
37
Tianjie Xud007cf22016-02-29 16:08:06 -080038#include <string>
39#include <vector>
40
41#include <android-base/file.h>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070042#include <android-base/logging.h>
Tianjie Xud007cf22016-02-29 16:08:06 -080043#include <android-base/parseint.h>
Tao Bao4f8d2172017-01-13 15:36:32 -080044#include <android-base/properties.h>
Tianjie Xud007cf22016-02-29 16:08:06 -080045#include <android-base/strings.h>
46#include <android-base/unique_fd.h>
Connor O'Brienad43d2d2016-11-21 12:47:33 -080047#include <android/hardware/boot/1.0/IBootControl.h>
48
49using android::sp;
50using android::hardware::boot::V1_0::IBootControl;
51using android::hardware::boot::V1_0::BoolResult;
52using android::hardware::boot::V1_0::CommandResult;
Tao Bao7197ee02015-12-05 21:21:27 -080053
Tianjie Xud007cf22016-02-29 16:08:06 -080054constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt";
55constexpr int BLOCKSIZE = 4096;
56
57static bool read_blocks(const std::string& blk_device_prefix, const std::string& range_str) {
Tao Bao4f8d2172017-01-13 15:36:32 -080058 std::string slot_suffix = android::base::GetProperty("ro.boot.slot_suffix", "");
59 std::string blk_device = blk_device_prefix + slot_suffix;
60 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY)));
61 if (fd.get() == -1) {
62 PLOG(ERROR) << "Error reading partition " << blk_device;
63 return false;
64 }
65
66 // For block range string, first integer 'count' equals 2 * total number of valid ranges,
67 // followed by 'count' number comma separated integers. Every two integers reprensent a
68 // block range with the first number included in range but second number not included.
69 // For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150).
70 std::vector<std::string> ranges = android::base::Split(range_str, ",");
71 size_t range_count;
72 bool status = android::base::ParseUint(ranges[0], &range_count);
73 if (!status || (range_count == 0) || (range_count % 2 != 0) ||
74 (range_count != ranges.size() - 1)) {
75 LOG(ERROR) << "Error in parsing range string.";
76 return false;
77 }
78
79 size_t blk_count = 0;
80 for (size_t i = 1; i < ranges.size(); i += 2) {
81 unsigned int range_start, range_end;
82 bool parse_status = android::base::ParseUint(ranges[i], &range_start);
83 parse_status = parse_status && android::base::ParseUint(ranges[i + 1], &range_end);
84 if (!parse_status || range_start >= range_end) {
85 LOG(ERROR) << "Invalid range pair " << ranges[i] << ", " << ranges[i + 1];
86 return false;
Tianjie Xud007cf22016-02-29 16:08:06 -080087 }
88
Tao Bao4f8d2172017-01-13 15:36:32 -080089 if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) {
90 PLOG(ERROR) << "lseek to " << range_start << " failed";
91 return false;
Tianjie Xud007cf22016-02-29 16:08:06 -080092 }
93
Tao Bao4f8d2172017-01-13 15:36:32 -080094 size_t size = (range_end - range_start) * BLOCKSIZE;
95 std::vector<uint8_t> buf(size);
96 if (!android::base::ReadFully(fd.get(), buf.data(), size)) {
97 PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end;
98 return false;
Tianjie Xud007cf22016-02-29 16:08:06 -080099 }
Tao Bao4f8d2172017-01-13 15:36:32 -0800100 blk_count += (range_end - range_start);
101 }
Tianjie Xud007cf22016-02-29 16:08:06 -0800102
Tao Bao4f8d2172017-01-13 15:36:32 -0800103 LOG(INFO) << "Finished reading " << blk_count << " blocks on " << blk_device;
104 return true;
Tianjie Xud007cf22016-02-29 16:08:06 -0800105}
106
107static bool verify_image(const std::string& care_map_name) {
108 android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY)));
109 // If the device is flashed before the current boot, it may not have care_map.txt
110 // in /data/ota_package. To allow the device to continue booting in this situation,
111 // we should print a warning and skip the block verification.
112 if (care_map_fd.get() == -1) {
113 LOG(WARNING) << "Warning: care map " << care_map_name << " not found.";
114 return true;
115 }
116 // Care map file has four lines (two lines if vendor partition is not present):
117 // First line has the block device name, e.g./dev/block/.../by-name/system.
118 // Second line holds all ranges of blocks to verify.
119 // The next two lines have the same format but for vendor partition.
120 std::string file_content;
121 if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) {
122 LOG(ERROR) << "Error reading care map contents to string.";
123 return false;
124 }
125
126 std::vector<std::string> lines;
127 lines = android::base::Split(android::base::Trim(file_content), "\n");
128 if (lines.size() != 2 && lines.size() != 4) {
129 LOG(ERROR) << "Invalid lines in care_map: found " << lines.size()
130 << " lines, expecting 2 or 4 lines.";
131 return false;
132 }
133
134 for (size_t i = 0; i < lines.size(); i += 2) {
135 if (!read_blocks(lines[i], lines[i+1])) {
136 return false;
137 }
138 }
139
140 return true;
141}
142
Tao Bao7197ee02015-12-05 21:21:27 -0800143int main(int argc, char** argv) {
Tao Bao7197ee02015-12-05 21:21:27 -0800144 for (int i = 1; i < argc; i++) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700145 LOG(INFO) << "Started with arg " << i << ": " << argv[i];
Tao Bao7197ee02015-12-05 21:21:27 -0800146 }
147
Chris Phoenix0157c782017-01-20 11:34:00 -0800148 sp<IBootControl> module = IBootControl::getService();
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800149 if (module == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700150 LOG(ERROR) << "Error getting bootctrl module.";
Tao Bao7197ee02015-12-05 21:21:27 -0800151 return -1;
152 }
153
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800154 uint32_t current_slot = module->getCurrentSlot();
155 BoolResult is_successful = module->isSlotMarkedSuccessful(current_slot);
156 LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful="
157 << static_cast<int32_t>(is_successful);
Tao Bao7197ee02015-12-05 21:21:27 -0800158
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800159 if (is_successful == BoolResult::FALSE) {
Tao Bao7197ee02015-12-05 21:21:27 -0800160 // The current slot has not booted successfully.
Tao Bao4f8d2172017-01-13 15:36:32 -0800161 std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", "");
162 if (verity_mode.empty()) {
Tianjie Xud007cf22016-02-29 16:08:06 -0800163 LOG(ERROR) << "Failed to get dm-verity mode.";
164 return -1;
Tao Bao4f8d2172017-01-13 15:36:32 -0800165 } else if (android::base::EqualsIgnoreCase(verity_mode, "eio")) {
Tianjie Xud007cf22016-02-29 16:08:06 -0800166 // We shouldn't see verity in EIO mode if the current slot hasn't booted
167 // successfully before. Therefore, fail the verification when veritymode=eio.
168 LOG(ERROR) << "Found dm-verity in EIO mode, skip verification.";
169 return -1;
Tao Bao4f8d2172017-01-13 15:36:32 -0800170 } else if (verity_mode != "enforcing") {
Tianjie Xud007cf22016-02-29 16:08:06 -0800171 LOG(ERROR) << "Unexpected dm-verity mode : " << verity_mode << ", expecting enforcing.";
172 return -1;
173 } else if (!verify_image(CARE_MAP_FILE)) {
174 LOG(ERROR) << "Failed to verify all blocks in care map file.";
175 return -1;
176 }
Tao Bao7197ee02015-12-05 21:21:27 -0800177
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800178 CommandResult cr;
179 module->markBootSuccessful([&cr](CommandResult result) { cr = result; });
180 if (!cr.success) {
181 LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg;
Tao Bao7197ee02015-12-05 21:21:27 -0800182 return -1;
183 }
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700184 LOG(INFO) << "Marked slot " << current_slot << " as booted successfully.";
Tao Bao7197ee02015-12-05 21:21:27 -0800185 }
186
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700187 LOG(INFO) << "Leaving update_verifier.";
Tao Bao7197ee02015-12-05 21:21:27 -0800188 return 0;
189}