blob: e97a3adbad974cadc40dfa1bca89f032a315a071 [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.
31 *
Tao Bao7197ee02015-12-05 21:21:27 -080032 */
33
Tianjie Xud007cf22016-02-29 16:08:06 -080034#include <errno.h>
35#include <fcntl.h>
36#include <stdio.h>
Tao Bao7197ee02015-12-05 21:21:27 -080037#include <string.h>
38
Tianjie Xud007cf22016-02-29 16:08:06 -080039#include <string>
40#include <vector>
41
42#include <android-base/file.h>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070043#include <android-base/logging.h>
Tianjie Xud007cf22016-02-29 16:08:06 -080044#include <android-base/parseint.h>
45#include <android-base/strings.h>
46#include <android-base/unique_fd.h>
47#include <cutils/properties.h>
Connor O'Brienad43d2d2016-11-21 12:47:33 -080048#include <android/hardware/boot/1.0/IBootControl.h>
49
50using android::sp;
51using android::hardware::boot::V1_0::IBootControl;
52using android::hardware::boot::V1_0::BoolResult;
53using android::hardware::boot::V1_0::CommandResult;
Tao Bao7197ee02015-12-05 21:21:27 -080054
Tianjie Xud007cf22016-02-29 16:08:06 -080055constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt";
56constexpr int BLOCKSIZE = 4096;
57
58static bool read_blocks(const std::string& blk_device_prefix, const std::string& range_str) {
59 char slot_suffix[PROPERTY_VALUE_MAX];
60 property_get("ro.boot.slot_suffix", slot_suffix, "");
61 std::string blk_device = blk_device_prefix + std::string(slot_suffix);
62 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY)));
63 if (fd.get() == -1) {
64 PLOG(ERROR) << "Error reading partition " << blk_device;
65 return false;
66 }
67
68 // For block range string, first integer 'count' equals 2 * total number of valid ranges,
69 // followed by 'count' number comma separated integers. Every two integers reprensent a
70 // block range with the first number included in range but second number not included.
71 // For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150).
72 std::vector<std::string> ranges = android::base::Split(range_str, ",");
73 size_t range_count;
74 bool status = android::base::ParseUint(ranges[0].c_str(), &range_count);
75 if (!status || (range_count == 0) || (range_count % 2 != 0) ||
76 (range_count != ranges.size()-1)) {
77 LOG(ERROR) << "Error in parsing range string.";
78 return false;
79 }
80
81 size_t blk_count = 0;
82 for (size_t i = 1; i < ranges.size(); i += 2) {
83 unsigned int range_start, range_end;
84 bool parse_status = android::base::ParseUint(ranges[i].c_str(), &range_start);
85 parse_status = parse_status && android::base::ParseUint(ranges[i+1].c_str(), &range_end);
86 if (!parse_status || range_start >= range_end) {
87 LOG(ERROR) << "Invalid range pair " << ranges[i] << ", " << ranges[i+1];
88 return false;
89 }
90
91 if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) {
92 PLOG(ERROR) << "lseek to " << range_start << " failed";
93 return false;
94 }
95
96 size_t size = (range_end - range_start) * BLOCKSIZE;
97 std::vector<uint8_t> buf(size);
98 if (!android::base::ReadFully(fd.get(), buf.data(), size)) {
99 PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end;
100 return false;
101 }
102 blk_count += (range_end - range_start);
103 }
104
105 LOG(INFO) << "Finished reading " << blk_count << " blocks on " << blk_device;
106 return true;
107}
108
109static bool verify_image(const std::string& care_map_name) {
110 android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY)));
111 // If the device is flashed before the current boot, it may not have care_map.txt
112 // in /data/ota_package. To allow the device to continue booting in this situation,
113 // we should print a warning and skip the block verification.
114 if (care_map_fd.get() == -1) {
115 LOG(WARNING) << "Warning: care map " << care_map_name << " not found.";
116 return true;
117 }
118 // Care map file has four lines (two lines if vendor partition is not present):
119 // First line has the block device name, e.g./dev/block/.../by-name/system.
120 // Second line holds all ranges of blocks to verify.
121 // The next two lines have the same format but for vendor partition.
122 std::string file_content;
123 if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) {
124 LOG(ERROR) << "Error reading care map contents to string.";
125 return false;
126 }
127
128 std::vector<std::string> lines;
129 lines = android::base::Split(android::base::Trim(file_content), "\n");
130 if (lines.size() != 2 && lines.size() != 4) {
131 LOG(ERROR) << "Invalid lines in care_map: found " << lines.size()
132 << " lines, expecting 2 or 4 lines.";
133 return false;
134 }
135
136 for (size_t i = 0; i < lines.size(); i += 2) {
137 if (!read_blocks(lines[i], lines[i+1])) {
138 return false;
139 }
140 }
141
142 return true;
143}
144
Tao Bao7197ee02015-12-05 21:21:27 -0800145int main(int argc, char** argv) {
Tao Bao7197ee02015-12-05 21:21:27 -0800146 for (int i = 1; i < argc; i++) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700147 LOG(INFO) << "Started with arg " << i << ": " << argv[i];
Tao Bao7197ee02015-12-05 21:21:27 -0800148 }
149
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800150 sp<IBootControl> module = IBootControl::getService("bootctrl");
151 if (module == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700152 LOG(ERROR) << "Error getting bootctrl module.";
Tao Bao7197ee02015-12-05 21:21:27 -0800153 return -1;
154 }
155
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800156 uint32_t current_slot = module->getCurrentSlot();
157 BoolResult is_successful = module->isSlotMarkedSuccessful(current_slot);
158 LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful="
159 << static_cast<int32_t>(is_successful);
Tao Bao7197ee02015-12-05 21:21:27 -0800160
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800161 if (is_successful == BoolResult::FALSE) {
Tao Bao7197ee02015-12-05 21:21:27 -0800162 // The current slot has not booted successfully.
Tianjie Xud007cf22016-02-29 16:08:06 -0800163 char verity_mode[PROPERTY_VALUE_MAX];
164 if (property_get("ro.boot.veritymode", verity_mode, "") == -1) {
165 LOG(ERROR) << "Failed to get dm-verity mode.";
166 return -1;
167 } else if (strcasecmp(verity_mode, "eio") == 0) {
168 // We shouldn't see verity in EIO mode if the current slot hasn't booted
169 // successfully before. Therefore, fail the verification when veritymode=eio.
170 LOG(ERROR) << "Found dm-verity in EIO mode, skip verification.";
171 return -1;
172 } else if (strcmp(verity_mode, "enforcing") != 0) {
173 LOG(ERROR) << "Unexpected dm-verity mode : " << verity_mode << ", expecting enforcing.";
174 return -1;
175 } else if (!verify_image(CARE_MAP_FILE)) {
176 LOG(ERROR) << "Failed to verify all blocks in care map file.";
177 return -1;
178 }
Tao Bao7197ee02015-12-05 21:21:27 -0800179
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800180 CommandResult cr;
181 module->markBootSuccessful([&cr](CommandResult result) { cr = result; });
182 if (!cr.success) {
183 LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg;
Tao Bao7197ee02015-12-05 21:21:27 -0800184 return -1;
185 }
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700186 LOG(INFO) << "Marked slot " << current_slot << " as booted successfully.";
Tao Bao7197ee02015-12-05 21:21:27 -0800187 }
188
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700189 LOG(INFO) << "Leaving update_verifier.";
Tao Bao7197ee02015-12-05 21:21:27 -0800190 return 0;
191}