blob: 546865887f60ce3a93f1348f28401729a3ee02d7 [file] [log] [blame]
David Anderson8108e252019-08-28 15:24:07 -07001//
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
David Anderson46f38e42019-08-29 15:09:22 -070021#include <android/hardware/boot/1.1/IBootControl.h>
22
David Anderson8108e252019-08-28 15:24:07 -070023namespace android {
24namespace bootable {
25
26// Helper library to implement the IBootControl HAL using the misc partition.
27class BootControl {
David Anderson46f38e42019-08-29 15:09:22 -070028 using MergeStatus = ::android::hardware::boot::V1_1::MergeStatus;
29
David Anderson8108e252019-08-28 15:24:07 -070030 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);
David Anderson46f38e42019-08-29 15:09:22 -070041 bool SetSnapshotMergeStatus(MergeStatus status);
42 MergeStatus GetSnapshotMergeStatus();
43
44 bool IsValidSlot(unsigned int slot);
David Anderson8108e252019-08-28 15:24:07 -070045
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
David Andersoncf8427a2019-11-04 14:08:11 -080065// 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.
77bool InitMiscVirtualAbMessageIfNeeded();
78
79// Save the current merge status as well as the current slot.
80bool 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.
85bool GetMiscVirtualAbMergeStatus(unsigned int current_slot,
86 android::hardware::boot::V1_1::MergeStatus* status);
87
David Anderson8108e252019-08-28 15:24:07 -070088} // namespace bootable
89} // namespace android