Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 25 | * 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 Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 28 | * |
| 29 | * The current slot will be marked as having booted successfully if the |
| 30 | * verifier reaches the end after the verification. |
| 31 | * |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 32 | */ |
| 33 | |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 34 | #include <errno.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <stdio.h> |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 37 | #include <string.h> |
| 38 | |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 39 | #include <string> |
| 40 | #include <vector> |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 41 | |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 42 | #include <android-base/file.h> |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 43 | #include <android-base/logging.h> |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 44 | #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 Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 49 | |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 50 | constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt"; |
| 51 | constexpr int BLOCKSIZE = 4096; |
| 52 | |
| 53 | static 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 Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 59 | PLOG(ERROR) << "Error reading partition " << blk_device; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 60 | 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 Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 72 | LOG(ERROR) << "Error in parsing range string."; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 73 | 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 Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 82 | LOG(ERROR) << "Invalid range pair " << ranges[i] << ", " << ranges[i+1]; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 87 | PLOG(ERROR) << "lseek to " << range_start << " failed"; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 88 | 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 Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 94 | PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | blk_count += (range_end - range_start); |
| 98 | } |
| 99 | |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 100 | LOG(INFO) << "Finished reading " << blk_count << " blocks on " << blk_device; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 101 | return true; |
| 102 | } |
| 103 | |
| 104 | static 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 Xu | da654af | 2016-07-15 16:15:42 -0700 | [diff] [blame] | 106 | // 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 Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 109 | if (care_map_fd.get() == -1) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 110 | LOG(WARNING) << "Warning: care map " << care_map_name << " not found."; |
Tianjie Xu | da654af | 2016-07-15 16:15:42 -0700 | [diff] [blame] | 111 | return true; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 112 | } |
| 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 Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 119 | LOG(ERROR) << "Error reading care map contents to string."; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 120 | 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 Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 126 | LOG(ERROR) << "Invalid lines in care_map: found " << lines.size() |
| 127 | << " lines, expecting 2 or 4 lines."; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 128 | 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 Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 140 | int main(int argc, char** argv) { |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 141 | for (int i = 1; i < argc; i++) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 142 | LOG(INFO) << "Started with arg " << i << ": " << argv[i]; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | const hw_module_t* hw_module; |
| 146 | if (hw_get_module("bootctrl", &hw_module) != 0) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 147 | LOG(ERROR) << "Error getting bootctrl module."; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 148 | 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 Bao | 612161e | 2015-12-09 14:41:40 -0800 | [diff] [blame] | 156 | int is_successful= module->isSlotMarkedSuccessful(module, current_slot); |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 157 | LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful=" << is_successful; |
| 158 | |
Tao Bao | 612161e | 2015-12-09 14:41:40 -0800 | [diff] [blame] | 159 | if (is_successful == 0) { |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 160 | // The current slot has not booted successfully. |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 161 | char verity_mode[PROPERTY_VALUE_MAX]; |
| 162 | if (property_get("ro.boot.veritymode", verity_mode, "") == -1) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 163 | LOG(ERROR) << "Failed to get dm-verity mode."; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 164 | return -1; |
Tianjie Xu | 4bbe0c9 | 2016-07-14 16:00:36 -0700 | [diff] [blame] | 165 | } else if (strcasecmp(verity_mode, "eio") == 0) { |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 166 | // We shouldn't see verity in EIO mode if the current slot hasn't booted |
Tianjie Xu | 4bbe0c9 | 2016-07-14 16:00:36 -0700 | [diff] [blame] | 167 | // successfully before. Therefore, fail the verification when veritymode=eio. |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 168 | LOG(ERROR) << "Found dm-verity in EIO mode, skip verification."; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 169 | return -1; |
| 170 | } else if (strcmp(verity_mode, "enforcing") != 0) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 171 | LOG(ERROR) << "Unexpected dm-verity mode : " << verity_mode << ", expecting enforcing."; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 172 | return -1; |
| 173 | } else if (!verify_image(CARE_MAP_FILE)) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 174 | LOG(ERROR) << "Failed to verify all blocks in care map file."; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 175 | return -1; |
| 176 | } |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 177 | |
| 178 | int ret = module->markBootSuccessful(module); |
| 179 | if (ret != 0) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 180 | LOG(ERROR) << "Error marking booted successfully: " << strerror(-ret); |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 181 | return -1; |
| 182 | } |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 183 | LOG(INFO) << "Marked slot " << current_slot << " as booted successfully."; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 186 | LOG(INFO) << "Leaving update_verifier."; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 187 | return 0; |
| 188 | } |