1 //
2 // Copyright (C) 2019 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 #pragma once
18 
19 #include <string>
20 
21 #include <android/hardware/boot/1.1/IBootControl.h>
22 
23 namespace android {
24 namespace bootable {
25 
26 // Helper library to implement the IBootControl HAL using the misc partition.
27 class BootControl {
28   using MergeStatus = ::android::hardware::boot::V1_1::MergeStatus;
29 
30  public:
31   bool Init();
32   unsigned int GetNumberSlots();
33   unsigned int GetCurrentSlot();
34   bool MarkBootSuccessful();
35   unsigned int GetActiveBootSlot();
36   bool SetActiveBootSlot(unsigned int slot);
37   bool SetSlotAsUnbootable(unsigned int slot);
38   bool SetSlotBootable(unsigned int slot);
39   bool IsSlotBootable(unsigned int slot);
40   const char* GetSuffix(unsigned int slot);
41   bool IsSlotMarkedSuccessful(unsigned int slot);
42   bool SetSnapshotMergeStatus(MergeStatus status);
43   MergeStatus GetSnapshotMergeStatus();
44 
45   bool IsValidSlot(unsigned int slot);
46 
misc_device()47   const std::string& misc_device() const {
48     return misc_device_;
49   }
50 
51  private:
52   // Whether this object was initialized with data from the bootloader message
53   // that doesn't change until next reboot.
54   bool initialized_ = false;
55 
56   // The path to the misc_device as reported in the fstab.
57   std::string misc_device_;
58 
59   // The number of slots present on the device.
60   unsigned int num_slots_ = 0;
61 
62   // The slot where we are running from.
63   unsigned int current_slot_ = 0;
64 };
65 
66 // Helper functions to write the Virtual A/B merge status message. These are
67 // separate because BootControl uses bootloader_control_ab in vendor space,
68 // whereas the Virtual A/B merge status is in system space. A HAL might not
69 // use bootloader_control_ab, but may want to use the AOSP method of maintaining
70 // the merge status.
71 
72 // If the Virtual A/B message has not yet been initialized, then initialize it.
73 // This should be called when the BootControl HAL first loads.
74 //
75 // If the Virtual A/B message in misc was already initialized, true is returned.
76 // If initialization was attempted, but failed, false is returned, and the HAL
77 // should fail to load.
78 bool InitMiscVirtualAbMessageIfNeeded();
79 
80 // Save the current merge status as well as the current slot.
81 bool SetMiscVirtualAbMergeStatus(unsigned int current_slot,
82                                  android::hardware::boot::V1_1::MergeStatus status);
83 
84 // Return the current merge status. If the saved status is SNAPSHOTTED but the
85 // slot hasn't changed, the status returned will be NONE.
86 bool GetMiscVirtualAbMergeStatus(unsigned int current_slot,
87                                  android::hardware::boot::V1_1::MergeStatus* status);
88 
89 }  // namespace bootable
90 }  // namespace android
91