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