blob: a4799cc31ee843923c51813c1a337c58bbbad0cd [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 Xub0ac8722017-01-18 18:41:53 -080033#include <dirent.h>
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>
Tao Bao4f8d2172017-01-13 15:36:32 -080045#include <android-base/properties.h>
Tianjie Xud007cf22016-02-29 16:08:06 -080046#include <android-base/strings.h>
47#include <android-base/unique_fd.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";
Tianjie Xub0ac8722017-01-18 18:41:53 -080056constexpr auto DM_PATH_PREFIX = "/sys/block/";
57constexpr auto DM_PATH_SUFFIX = "/dm/name";
58constexpr auto DEV_PATH = "/dev/block/";
Tianjie Xud007cf22016-02-29 16:08:06 -080059constexpr int BLOCKSIZE = 4096;
60
Tianjie Xub0ac8722017-01-18 18:41:53 -080061// Find directories in format of "/sys/block/dm-X".
62static int dm_name_filter(const dirent* de) {
63 if (android::base::StartsWith(de->d_name, "dm-")) {
64 return 1;
65 }
66 return 0;
67}
68
69static bool read_blocks(const std::string& blk_device, const std::string& range_str) {
70 // Parse the partition in the end of the block_device string.
71 // Here is one example: "/dev/block/bootdevice/by-name/system"
72 std::string partition;
73 if (android::base::EndsWith(blk_device, "system")) {
74 partition = "system";
75 } else if (android::base::EndsWith(blk_device, "vendor")) {
76 partition = "vendor";
77 } else {
78 LOG(ERROR) << "Failed to parse partition string in " << blk_device;
79 return false;
80 }
81
82 // Iterate the content of "/sys/block/dm-X/dm/name". If it matches "system"
83 // (or "vendor"), then dm-X is a dm-wrapped system/vendor partition.
84 // Afterwards, update_verifier will read every block on the care_map_file of
85 // "/dev/block/dm-X" to ensure the partition's integrity.
86 dirent** namelist;
87 int n = scandir(DM_PATH_PREFIX, &namelist, dm_name_filter, alphasort);
88 if (n == -1) {
89 PLOG(ERROR) << "Failed to scan dir " << DM_PATH_PREFIX;
90 return false;
91 }
92 if (n == 0) {
93 LOG(ERROR) << "dm block device not found for " << partition;
94 return false;
95 }
96
97 std::string dm_block_device;
98 while (n--) {
99 std::string path = DM_PATH_PREFIX + std::string(namelist[n]->d_name) + DM_PATH_SUFFIX;
100 std::string content;
101 if (!android::base::ReadFileToString(path, &content)) {
102 PLOG(WARNING) << "Failed to read " << path;
103 } else if (android::base::Trim(content) == partition) {
104 dm_block_device = DEV_PATH + std::string(namelist[n]->d_name);
105 while (n--) {
106 free(namelist[n]);
107 }
108 break;
109 }
110 free(namelist[n]);
111 }
112 free(namelist);
113
114 if (dm_block_device.empty()) {
115 LOG(ERROR) << "Failed to find dm block device for " << partition;
116 return false;
117 }
118 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dm_block_device.c_str(), O_RDONLY)));
Tao Bao4f8d2172017-01-13 15:36:32 -0800119 if (fd.get() == -1) {
Tianjie Xub0ac8722017-01-18 18:41:53 -0800120 PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition;
Tao Bao4f8d2172017-01-13 15:36:32 -0800121 return false;
122 }
123
124 // For block range string, first integer 'count' equals 2 * total number of valid ranges,
125 // followed by 'count' number comma separated integers. Every two integers reprensent a
126 // block range with the first number included in range but second number not included.
127 // For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150).
128 std::vector<std::string> ranges = android::base::Split(range_str, ",");
129 size_t range_count;
130 bool status = android::base::ParseUint(ranges[0], &range_count);
131 if (!status || (range_count == 0) || (range_count % 2 != 0) ||
132 (range_count != ranges.size() - 1)) {
133 LOG(ERROR) << "Error in parsing range string.";
134 return false;
135 }
136
137 size_t blk_count = 0;
138 for (size_t i = 1; i < ranges.size(); i += 2) {
139 unsigned int range_start, range_end;
140 bool parse_status = android::base::ParseUint(ranges[i], &range_start);
141 parse_status = parse_status && android::base::ParseUint(ranges[i + 1], &range_end);
142 if (!parse_status || range_start >= range_end) {
143 LOG(ERROR) << "Invalid range pair " << ranges[i] << ", " << ranges[i + 1];
144 return false;
Tianjie Xud007cf22016-02-29 16:08:06 -0800145 }
146
Tao Bao4f8d2172017-01-13 15:36:32 -0800147 if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) {
148 PLOG(ERROR) << "lseek to " << range_start << " failed";
149 return false;
Tianjie Xud007cf22016-02-29 16:08:06 -0800150 }
151
Tao Bao4f8d2172017-01-13 15:36:32 -0800152 size_t size = (range_end - range_start) * BLOCKSIZE;
153 std::vector<uint8_t> buf(size);
154 if (!android::base::ReadFully(fd.get(), buf.data(), size)) {
155 PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end;
156 return false;
Tianjie Xud007cf22016-02-29 16:08:06 -0800157 }
Tao Bao4f8d2172017-01-13 15:36:32 -0800158 blk_count += (range_end - range_start);
159 }
Tianjie Xud007cf22016-02-29 16:08:06 -0800160
Tianjie Xub0ac8722017-01-18 18:41:53 -0800161 LOG(INFO) << "Finished reading " << blk_count << " blocks on " << dm_block_device;
Tao Bao4f8d2172017-01-13 15:36:32 -0800162 return true;
Tianjie Xud007cf22016-02-29 16:08:06 -0800163}
164
165static bool verify_image(const std::string& care_map_name) {
166 android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY)));
167 // If the device is flashed before the current boot, it may not have care_map.txt
168 // in /data/ota_package. To allow the device to continue booting in this situation,
169 // we should print a warning and skip the block verification.
170 if (care_map_fd.get() == -1) {
171 LOG(WARNING) << "Warning: care map " << care_map_name << " not found.";
172 return true;
173 }
174 // Care map file has four lines (two lines if vendor partition is not present):
175 // First line has the block device name, e.g./dev/block/.../by-name/system.
176 // Second line holds all ranges of blocks to verify.
177 // The next two lines have the same format but for vendor partition.
178 std::string file_content;
179 if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) {
180 LOG(ERROR) << "Error reading care map contents to string.";
181 return false;
182 }
183
184 std::vector<std::string> lines;
185 lines = android::base::Split(android::base::Trim(file_content), "\n");
186 if (lines.size() != 2 && lines.size() != 4) {
187 LOG(ERROR) << "Invalid lines in care_map: found " << lines.size()
188 << " lines, expecting 2 or 4 lines.";
189 return false;
190 }
191
192 for (size_t i = 0; i < lines.size(); i += 2) {
193 if (!read_blocks(lines[i], lines[i+1])) {
194 return false;
195 }
196 }
197
198 return true;
199}
200
Tao Bao7197ee02015-12-05 21:21:27 -0800201int main(int argc, char** argv) {
Tao Bao7197ee02015-12-05 21:21:27 -0800202 for (int i = 1; i < argc; i++) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700203 LOG(INFO) << "Started with arg " << i << ": " << argv[i];
Tao Bao7197ee02015-12-05 21:21:27 -0800204 }
205
Chris Phoenix0157c782017-01-20 11:34:00 -0800206 sp<IBootControl> module = IBootControl::getService();
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800207 if (module == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700208 LOG(ERROR) << "Error getting bootctrl module.";
Tao Bao7197ee02015-12-05 21:21:27 -0800209 return -1;
210 }
211
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800212 uint32_t current_slot = module->getCurrentSlot();
213 BoolResult is_successful = module->isSlotMarkedSuccessful(current_slot);
214 LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful="
215 << static_cast<int32_t>(is_successful);
Tao Bao7197ee02015-12-05 21:21:27 -0800216
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800217 if (is_successful == BoolResult::FALSE) {
Tao Bao7197ee02015-12-05 21:21:27 -0800218 // The current slot has not booted successfully.
Tao Bao4f8d2172017-01-13 15:36:32 -0800219 std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", "");
220 if (verity_mode.empty()) {
Tianjie Xud007cf22016-02-29 16:08:06 -0800221 LOG(ERROR) << "Failed to get dm-verity mode.";
222 return -1;
Tao Bao4f8d2172017-01-13 15:36:32 -0800223 } else if (android::base::EqualsIgnoreCase(verity_mode, "eio")) {
Tianjie Xud007cf22016-02-29 16:08:06 -0800224 // We shouldn't see verity in EIO mode if the current slot hasn't booted
225 // successfully before. Therefore, fail the verification when veritymode=eio.
226 LOG(ERROR) << "Found dm-verity in EIO mode, skip verification.";
227 return -1;
Tao Bao4f8d2172017-01-13 15:36:32 -0800228 } else if (verity_mode != "enforcing") {
Tianjie Xud007cf22016-02-29 16:08:06 -0800229 LOG(ERROR) << "Unexpected dm-verity mode : " << verity_mode << ", expecting enforcing.";
230 return -1;
231 } else if (!verify_image(CARE_MAP_FILE)) {
232 LOG(ERROR) << "Failed to verify all blocks in care map file.";
233 return -1;
234 }
Tao Bao7197ee02015-12-05 21:21:27 -0800235
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800236 CommandResult cr;
237 module->markBootSuccessful([&cr](CommandResult result) { cr = result; });
238 if (!cr.success) {
239 LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg;
Tao Bao7197ee02015-12-05 21:21:27 -0800240 return -1;
241 }
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700242 LOG(INFO) << "Marked slot " << current_slot << " as booted successfully.";
Tao Bao7197ee02015-12-05 21:21:27 -0800243 }
244
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700245 LOG(INFO) << "Leaving update_verifier.";
Tao Bao7197ee02015-12-05 21:21:27 -0800246 return 0;
247}