diff options
author | David Anderson <dvander@google.com> | 2019-11-04 23:08:11 +0100 |
---|---|---|
committer | David Anderson <dvander@google.com> | 2019-11-09 00:22:21 +0100 |
commit | cf8427af89f47075953dad10d6ea9b848736527f (patch) | |
tree | ff5b71f4a30251ad26692cfe706e19d850c339a3 /boot_control | |
parent | Merge "bootloader_message: Carve out space reserved for system." (diff) | |
download | android_bootable_recovery-cf8427af89f47075953dad10d6ea9b848736527f.tar android_bootable_recovery-cf8427af89f47075953dad10d6ea9b848736527f.tar.gz android_bootable_recovery-cf8427af89f47075953dad10d6ea9b848736527f.tar.bz2 android_bootable_recovery-cf8427af89f47075953dad10d6ea9b848736527f.tar.lz android_bootable_recovery-cf8427af89f47075953dad10d6ea9b848736527f.tar.xz android_bootable_recovery-cf8427af89f47075953dad10d6ea9b848736527f.tar.zst android_bootable_recovery-cf8427af89f47075953dad10d6ea9b848736527f.zip |
Diffstat (limited to 'boot_control')
-rw-r--r-- | boot_control/include/libboot_control/libboot_control.h | 23 | ||||
-rw-r--r-- | boot_control/libboot_control.cpp | 80 |
2 files changed, 94 insertions, 9 deletions
diff --git a/boot_control/include/libboot_control/libboot_control.h b/boot_control/include/libboot_control/libboot_control.h index 34a9affe1..546865887 100644 --- a/boot_control/include/libboot_control/libboot_control.h +++ b/boot_control/include/libboot_control/libboot_control.h @@ -62,5 +62,28 @@ class BootControl { unsigned int current_slot_ = 0; }; +// Helper functions to write the Virtual A/B merge status message. These are +// separate because BootControl uses bootloader_control_ab in vendor space, +// whereas the Virtual A/B merge status is in system space. A HAL might not +// use bootloader_control_ab, but may want to use the AOSP method of maintaining +// the merge status. + +// If the Virtual A/B message has not yet been initialized, then initialize it. +// This should be called when the BootControl HAL first loads. +// +// If the Virtual A/B message in misc was already initialized, true is returned. +// If initialization was attempted, but failed, false is returned, and the HAL +// should fail to load. +bool InitMiscVirtualAbMessageIfNeeded(); + +// Save the current merge status as well as the current slot. +bool SetMiscVirtualAbMergeStatus(unsigned int current_slot, + android::hardware::boot::V1_1::MergeStatus status); + +// Return the current merge status. If the saved status is SNAPSHOTTED but the +// slot hasn't changed, the status returned will be NONE. +bool GetMiscVirtualAbMergeStatus(unsigned int current_slot, + android::hardware::boot::V1_1::MergeStatus* status); + } // namespace bootable } // namespace android diff --git a/boot_control/libboot_control.cpp b/boot_control/libboot_control.cpp index ff4eaabfa..702183979 100644 --- a/boot_control/libboot_control.cpp +++ b/boot_control/libboot_control.cpp @@ -232,6 +232,10 @@ bool BootControl::Init() { UpdateAndSaveBootloaderControl(device.c_str(), &boot_ctrl); } + if (!InitMiscVirtualAbMessageIfNeeded()) { + return false; + } + num_slots_ = boot_ctrl.nb_slot; return true; } @@ -335,18 +339,15 @@ bool BootControl::IsValidSlot(unsigned int slot) { } bool BootControl::SetSnapshotMergeStatus(MergeStatus status) { - bootloader_control bootctrl; - if (!LoadBootloaderControl(misc_device_, &bootctrl)) return false; - - bootctrl.merge_status = (unsigned int)status; - return UpdateAndSaveBootloaderControl(misc_device_, &bootctrl); + return SetMiscVirtualAbMergeStatus(current_slot_, status); } MergeStatus BootControl::GetSnapshotMergeStatus() { - bootloader_control bootctrl; - if (!LoadBootloaderControl(misc_device_, &bootctrl)) return MergeStatus::UNKNOWN; - - return (MergeStatus)bootctrl.merge_status; + MergeStatus status; + if (!GetMiscVirtualAbMergeStatus(current_slot_, &status)) { + return MergeStatus::UNKNOWN; + } + return status; } const char* BootControl::GetSuffix(unsigned int slot) { @@ -356,5 +357,66 @@ const char* BootControl::GetSuffix(unsigned int slot) { return kSlotSuffixes[slot]; } +bool InitMiscVirtualAbMessageIfNeeded() { + std::string err; + misc_virtual_ab_message message; + if (!ReadMiscVirtualAbMessage(&message, &err)) { + LOG(ERROR) << "Could not read merge status: " << err; + return false; + } + + if (message.version == MISC_VIRTUAL_AB_MESSAGE_VERSION) { + // Already initialized. + return true; + } + + message = {}; + message.version = MISC_VIRTUAL_AB_MESSAGE_VERSION; + if (!WriteMiscVirtualAbMessage(message, &err)) { + LOG(ERROR) << "Could not write merge status: " << err; + return false; + } + return true; +} + +bool SetMiscVirtualAbMergeStatus(unsigned int current_slot, + android::hardware::boot::V1_1::MergeStatus status) { + std::string err; + misc_virtual_ab_message message; + + if (!ReadMiscVirtualAbMessage(&message, &err)) { + LOG(ERROR) << "Could not read merge status: " << err; + return false; + } + + message.merge_status = static_cast<uint8_t>(status); + message.source_slot = current_slot; + if (!WriteMiscVirtualAbMessage(message, &err)) { + LOG(ERROR) << "Could not write merge status: " << err; + return false; + } + return true; +} + +bool GetMiscVirtualAbMergeStatus(unsigned int current_slot, + android::hardware::boot::V1_1::MergeStatus* status) { + std::string err; + misc_virtual_ab_message message; + + if (!ReadMiscVirtualAbMessage(&message, &err)) { + LOG(ERROR) << "Could not read merge status: " << err; + return false; + } + + // If the slot reverted after having created a snapshot, then the snapshot will + // be thrown away at boot. Thus we don't count this as being in a snapshotted + // state. + *status = static_cast<MergeStatus>(message.merge_status); + if (*status == MergeStatus::SNAPSHOTTED && current_slot == message.source_slot) { + *status = MergeStatus::NONE; + } + return true; +} + } // namespace bootable } // namespace android |