Tao Bao | 1171d3a | 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 |
| 26 | * started in dm-verity logging mode but with isSlotBootable equals to 0. |
| 27 | * |
| 28 | * The current slot will be marked as having booted successfully if the |
| 29 | * verifier reaches the end after the verification. |
| 30 | * |
| 31 | * TODO: The actual verification part will be added later after we have the |
| 32 | * A/B OTA package format in place. |
| 33 | */ |
| 34 | |
Tao Bao | 1171d3a | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 35 | #include <string.h> |
| 36 | |
| 37 | #include <hardware/boot_control.h> |
| 38 | |
| 39 | #define LOG_TAG "update_verifier" |
Tao Bao | 45eac58 | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 40 | #include <log/log.h> |
Tao Bao | 1171d3a | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 41 | |
| 42 | int main(int argc, char** argv) { |
Tao Bao | 1171d3a | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 43 | for (int i = 1; i < argc; i++) { |
Tao Bao | 45eac58 | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 44 | SLOGI("Started with arg %d: %s\n", i, argv[i]); |
Tao Bao | 1171d3a | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | const hw_module_t* hw_module; |
| 48 | if (hw_get_module("bootctrl", &hw_module) != 0) { |
Tao Bao | 45eac58 | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 49 | SLOGE("Error getting bootctrl module.\n"); |
Tao Bao | 1171d3a | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | boot_control_module_t* module = reinterpret_cast<boot_control_module_t*>( |
| 54 | const_cast<hw_module_t*>(hw_module)); |
| 55 | module->init(module); |
| 56 | |
| 57 | unsigned current_slot = module->getCurrentSlot(module); |
| 58 | int bootable = module->isSlotBootable(module, current_slot); |
Tao Bao | 45eac58 | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 59 | SLOGI("Booting slot %u: isSlotBootable=%d\n", current_slot, bootable); |
Tao Bao | 1171d3a | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 60 | |
| 61 | if (bootable == 0) { |
| 62 | // The current slot has not booted successfully. |
| 63 | |
| 64 | // TODO: Add the actual verification after we have the A/B OTA package |
| 65 | // format in place. |
| 66 | |
| 67 | // TODO: Assert the dm-verity mode. Bootloader should never boot a newly |
| 68 | // flashed slot (isSlotBootable == 0) with dm-verity logging mode. |
| 69 | |
| 70 | int ret = module->markBootSuccessful(module); |
| 71 | if (ret != 0) { |
Tao Bao | 45eac58 | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 72 | SLOGE("Error marking booted successfully: %s\n", strerror(-ret)); |
Tao Bao | 1171d3a | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 73 | return -1; |
| 74 | } |
Tao Bao | 45eac58 | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 75 | SLOGI("Marked slot %u as booted successfully.\n", current_slot); |
Tao Bao | 1171d3a | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Tao Bao | 45eac58 | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 78 | SLOGI("Leaving update_verifier.\n"); |
Tao Bao | 1171d3a | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 79 | return 0; |
| 80 | } |