1 /* 2 * Copyright (C) 2022 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 #ifndef __BOOT_CONTROL_CLIENT_H_ 18 #define __BOOT_CONTROL_CLIENT_H_ 19 20 #include <aidl/android/hardware/boot/MergeStatus.h> 21 22 #include <stdint.h> 23 24 #include <memory> 25 #include <optional> 26 27 namespace android::hal { 28 29 struct CommandResult { 30 bool success; 31 std::string errMsg; IsOkCommandResult32 constexpr bool IsOk() const { return success; } 33 }; 34 35 enum class BootControlVersion { BOOTCTL_V1_0, BOOTCTL_V1_1, BOOTCTL_V1_2, BOOTCTL_AIDL }; 36 37 class BootControlClient { 38 public: 39 using MergeStatus = aidl::android::hardware::boot::MergeStatus; 40 virtual ~BootControlClient() = default; 41 virtual BootControlVersion GetVersion() const = 0; 42 // Return the number of update slots in the system. A system will normally 43 // have two slots, named "A" and "B" in the documentation, but sometimes 44 // images running from other media can have only one slot, like some USB 45 // image. Systems with only one slot won't be able to update. 46 [[nodiscard]] virtual int32_t GetNumSlots() const = 0; 47 48 // Return the slot where we are running the system from. On success, the 49 // result is a number between 0 and GetNumSlots() - 1. Otherwise, log an error 50 // and return kInvalidSlot. 51 [[nodiscard]] virtual int32_t GetCurrentSlot() const = 0; 52 53 // Return string suffix for input slot. Usually, for slot 0 the suffix is _a, and for slot 1 the 54 // suffix is _b. 55 [[nodiscard]] virtual std::string GetSuffix(int32_t slot) const = 0; 56 57 // Returns whether the passed |slot| is marked as bootable. Returns false if 58 // the slot is invalid. 59 [[nodiscard]] virtual std::optional<bool> IsSlotBootable(int32_t slot) const = 0; 60 61 // Mark the specified slot unbootable. No other slot flags are modified. 62 // Returns true on success. 63 [[nodiscard]] virtual CommandResult MarkSlotUnbootable(int32_t slot) = 0; 64 65 // Set the passed |slot| as the preferred boot slot. Returns whether it 66 // succeeded setting the active slot. If succeeded, on next boot the 67 // bootloader will attempt to load the |slot| marked as active. Note that this 68 // method doesn't change the value of GetCurrentSlot() on the current boot. 69 // Return true if operation succeeded. 70 [[nodiscard]] virtual CommandResult SetActiveBootSlot(int32_t slot) = 0; 71 72 // Check if |slot| is marked boot successfully. Return empty optional if the RPC call failed. 73 [[nodiscard]] virtual std::optional<bool> IsSlotMarkedSuccessful(int32_t slot) const = 0; 74 75 // Mark boot as successful. Return an error message if operation failed. 76 [[nodiscard]] virtual CommandResult MarkBootSuccessful() = 0; 77 78 // Added in IBootControl v1.1 79 // Return the current merge status. 80 [[nodiscard]] virtual MergeStatus getSnapshotMergeStatus() const = 0; 81 82 // Set snapshot merge status, return true if succeeded. 83 [[nodiscard]] virtual CommandResult SetSnapshotMergeStatus(MergeStatus status) = 0; 84 85 // Added in IBootControl v1.2 86 // Get the active slot. In other words, the slot which will be used on 87 // next system reboot. This should match the |slot| parameter of last 88 // successful call to |SetActiveBootSlot|. 89 // Return 0xFFFFFFFF if underlying HAL doesn't support this operation. 90 [[nodiscard]] virtual int32_t GetActiveBootSlot() const = 0; 91 92 [[nodiscard]] static std::unique_ptr<BootControlClient> WaitForService(); 93 }; 94 95 } // namespace android::hal 96 97 #endif 98