blob: 93ac605b15a8ffd5994ab25c11211f6eba4a42af [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 Xu03ca8532016-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 Xu03ca8532016-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 Xu03ca8532016-02-29 16:08:06 -080039#include <string>
40#include <vector>
Tao Bao7197ee02015-12-05 21:21:27 -080041
Tianjie Xu03ca8532016-02-29 16:08:06 -080042#include <android-base/file.h>
Tianjie Xuc21edd42016-08-05 18:00:04 -070043#include <android-base/logging.h>
Tianjie Xu03ca8532016-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>
48#include <hardware/boot_control.h>
Tao Bao7197ee02015-12-05 21:21:27 -080049
Tianjie Xu03ca8532016-02-29 16:08:06 -080050constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt";
51constexpr int BLOCKSIZE = 4096;
52
53static bool read_blocks(const std::string& blk_device_prefix, const std::string& range_str) {
54 char slot_suffix[PROPERTY_VALUE_MAX];
55 property_get("ro.boot.slot_suffix", slot_suffix, "");
56 std::string blk_device = blk_device_prefix + std::string(slot_suffix);
57 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY)));
58 if (fd.get() == -1) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070059 PLOG(ERROR) << "Error reading partition " << blk_device;
Tianjie Xu03ca8532016-02-29 16:08:06 -080060 return false;
61 }
62
63 // For block range string, first integer 'count' equals 2 * total number of valid ranges,
64 // followed by 'count' number comma separated integers. Every two integers reprensent a
65 // block range with the first number included in range but second number not included.
66 // For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150).
67 std::vector<std::string> ranges = android::base::Split(range_str, ",");
68 size_t range_count;
69 bool status = android::base::ParseUint(ranges[0].c_str(), &range_count);
70 if (!status || (range_count == 0) || (range_count % 2 != 0) ||
71 (range_count != ranges.size()-1)) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070072 LOG(ERROR) << "Error in parsing range string.";
Tianjie Xu03ca8532016-02-29 16:08:06 -080073 return false;
74 }
75
76 size_t blk_count = 0;
77 for (size_t i = 1; i < ranges.size(); i += 2) {
78 unsigned int range_start, range_end;
79 bool parse_status = android::base::ParseUint(ranges[i].c_str(), &range_start);
80 parse_status = parse_status && android::base::ParseUint(ranges[i+1].c_str(), &range_end);
81 if (!parse_status || range_start >= range_end) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070082 LOG(ERROR) << "Invalid range pair " << ranges[i] << ", " << ranges[i+1];
Tianjie Xu03ca8532016-02-29 16:08:06 -080083 return false;
84 }
85
86 if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070087 PLOG(ERROR) << "lseek to " << range_start << " failed";
Tianjie Xu03ca8532016-02-29 16:08:06 -080088 return false;
89 }
90
91 size_t size = (range_end - range_start) * BLOCKSIZE;
92 std::vector<uint8_t> buf(size);
93 if (!android::base::ReadFully(fd.get(), buf.data(), size)) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070094 PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end;
Tianjie Xu03ca8532016-02-29 16:08:06 -080095 return false;
96 }
97 blk_count += (range_end - range_start);
98 }
99
Tianjie Xuc21edd42016-08-05 18:00:04 -0700100 LOG(INFO) << "Finished reading " << blk_count << " blocks on " << blk_device;
Tianjie Xu03ca8532016-02-29 16:08:06 -0800101 return true;
102}
103
104static bool verify_image(const std::string& care_map_name) {
105 android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY)));
Tianjie Xuda654af2016-07-15 16:15:42 -0700106 // If the device is flashed before the current boot, it may not have care_map.txt
107 // in /data/ota_package. To allow the device to continue booting in this situation,
108 // we should print a warning and skip the block verification.
Tianjie Xu03ca8532016-02-29 16:08:06 -0800109 if (care_map_fd.get() == -1) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700110 LOG(WARNING) << "Warning: care map " << care_map_name << " not found.";
Tianjie Xuda654af2016-07-15 16:15:42 -0700111 return true;
Tianjie Xu03ca8532016-02-29 16:08:06 -0800112 }
113 // Care map file has four lines (two lines if vendor partition is not present):
114 // First line has the block device name, e.g./dev/block/.../by-name/system.
115 // Second line holds all ranges of blocks to verify.
116 // The next two lines have the same format but for vendor partition.
117 std::string file_content;
118 if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700119 LOG(ERROR) << "Error reading care map contents to string.";
Tianjie Xu03ca8532016-02-29 16:08:06 -0800120 return false;
121 }
122
123 std::vector<std::string> lines;
124 lines = android::base::Split(android::base::Trim(file_content), "\n");
125 if (lines.size() != 2 && lines.size() != 4) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700126 LOG(ERROR) << "Invalid lines in care_map: found " << lines.size()
127 << " lines, expecting 2 or 4 lines.";
Tianjie Xu03ca8532016-02-29 16:08:06 -0800128 return false;
129 }
130
131 for (size_t i = 0; i < lines.size(); i += 2) {
132 if (!read_blocks(lines[i], lines[i+1])) {
133 return false;
134 }
135 }
136
137 return true;
138}
139
Tao Bao7197ee02015-12-05 21:21:27 -0800140int main(int argc, char** argv) {
Tao Bao7197ee02015-12-05 21:21:27 -0800141 for (int i = 1; i < argc; i++) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700142 LOG(INFO) << "Started with arg " << i << ": " << argv[i];
Tao Bao7197ee02015-12-05 21:21:27 -0800143 }
144
145 const hw_module_t* hw_module;
146 if (hw_get_module("bootctrl", &hw_module) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700147 LOG(ERROR) << "Error getting bootctrl module.";
Tao Bao7197ee02015-12-05 21:21:27 -0800148 return -1;
149 }
150
151 boot_control_module_t* module = reinterpret_cast<boot_control_module_t*>(
152 const_cast<hw_module_t*>(hw_module));
153 module->init(module);
154
155 unsigned current_slot = module->getCurrentSlot(module);
Tao Bao612161e2015-12-09 14:41:40 -0800156 int is_successful= module->isSlotMarkedSuccessful(module, current_slot);
Tianjie Xuc21edd42016-08-05 18:00:04 -0700157 LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful=" << is_successful;
158
Tao Bao612161e2015-12-09 14:41:40 -0800159 if (is_successful == 0) {
Tao Bao7197ee02015-12-05 21:21:27 -0800160 // The current slot has not booted successfully.
Tianjie Xu03ca8532016-02-29 16:08:06 -0800161 char verity_mode[PROPERTY_VALUE_MAX];
162 if (property_get("ro.boot.veritymode", verity_mode, "") == -1) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700163 LOG(ERROR) << "Failed to get dm-verity mode.";
Tianjie Xu03ca8532016-02-29 16:08:06 -0800164 return -1;
Tianjie Xu4bbe0c92016-07-14 16:00:36 -0700165 } else if (strcasecmp(verity_mode, "eio") == 0) {
Tianjie Xu03ca8532016-02-29 16:08:06 -0800166 // We shouldn't see verity in EIO mode if the current slot hasn't booted
Tianjie Xu4bbe0c92016-07-14 16:00:36 -0700167 // successfully before. Therefore, fail the verification when veritymode=eio.
Tianjie Xuc21edd42016-08-05 18:00:04 -0700168 LOG(ERROR) << "Found dm-verity in EIO mode, skip verification.";
Tianjie Xu03ca8532016-02-29 16:08:06 -0800169 return -1;
170 } else if (strcmp(verity_mode, "enforcing") != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700171 LOG(ERROR) << "Unexpected dm-verity mode : " << verity_mode << ", expecting enforcing.";
Tianjie Xu03ca8532016-02-29 16:08:06 -0800172 return -1;
173 } else if (!verify_image(CARE_MAP_FILE)) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700174 LOG(ERROR) << "Failed to verify all blocks in care map file.";
Tianjie Xu03ca8532016-02-29 16:08:06 -0800175 return -1;
176 }
Tao Bao7197ee02015-12-05 21:21:27 -0800177
178 int ret = module->markBootSuccessful(module);
179 if (ret != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700180 LOG(ERROR) << "Error marking booted successfully: " << strerror(-ret);
Tao Bao7197ee02015-12-05 21:21:27 -0800181 return -1;
182 }
Tianjie Xuc21edd42016-08-05 18:00:04 -0700183 LOG(INFO) << "Marked slot " << current_slot << " as booted successfully.";
Tao Bao7197ee02015-12-05 21:21:27 -0800184 }
185
Tianjie Xuc21edd42016-08-05 18:00:04 -0700186 LOG(INFO) << "Leaving update_verifier.";
Tao Bao7197ee02015-12-05 21:21:27 -0800187 return 0;
188}