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 |
| 25 | * mark the slot as unbootable and stops trying. We should never see a device |
Tao Bao | 612161e | 2015-12-09 14:41:40 -0800 | [diff] [blame] | 26 | * started in dm-verity logging mode but with isSlotMarkedSuccessful equals to |
| 27 | * 0. |
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 | * |
| 32 | * TODO: The actual verification part will be added later after we have the |
| 33 | * A/B OTA package format in place. |
| 34 | */ |
| 35 | |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 38 | #include <android-base/logging.h> |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 39 | #include <hardware/boot_control.h> |
| 40 | |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 41 | int main(int argc, char** argv) { |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 42 | for (int i = 1; i < argc; i++) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 43 | LOG(INFO) << "Started with arg " << i << ": " << argv[i]; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | const hw_module_t* hw_module; |
| 47 | if (hw_get_module("bootctrl", &hw_module) != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 48 | LOG(ERROR) << "Error getting bootctrl module."; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | boot_control_module_t* module = reinterpret_cast<boot_control_module_t*>( |
| 53 | const_cast<hw_module_t*>(hw_module)); |
| 54 | module->init(module); |
| 55 | |
| 56 | unsigned current_slot = module->getCurrentSlot(module); |
Tao Bao | 612161e | 2015-12-09 14:41:40 -0800 | [diff] [blame] | 57 | int is_successful= module->isSlotMarkedSuccessful(module, current_slot); |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 58 | LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful=" << is_successful; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 59 | |
Tao Bao | 612161e | 2015-12-09 14:41:40 -0800 | [diff] [blame] | 60 | if (is_successful == 0) { |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 61 | // The current slot has not booted successfully. |
| 62 | |
| 63 | // TODO: Add the actual verification after we have the A/B OTA package |
| 64 | // format in place. |
| 65 | |
| 66 | // TODO: Assert the dm-verity mode. Bootloader should never boot a newly |
Tao Bao | 612161e | 2015-12-09 14:41:40 -0800 | [diff] [blame] | 67 | // flashed slot (isSlotMarkedSuccessful == 0) with dm-verity logging mode. |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 68 | |
| 69 | int ret = module->markBootSuccessful(module); |
| 70 | if (ret != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 71 | LOG(ERROR) << "Error marking booted successfully: " << strerror(-ret); |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 72 | return -1; |
| 73 | } |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 74 | LOG(INFO) << "Marked slot " << current_slot << " as booted successfully."; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 77 | LOG(INFO) << "Leaving update_verifier."; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 78 | return 0; |
| 79 | } |