1*4d7e907cSAndroid Build Coastguard Worker /* 2*4d7e907cSAndroid Build Coastguard Worker * Copyright (C) 2022 The Android Open Source Project 3*4d7e907cSAndroid Build Coastguard Worker * 4*4d7e907cSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*4d7e907cSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*4d7e907cSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*4d7e907cSAndroid Build Coastguard Worker * 8*4d7e907cSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*4d7e907cSAndroid Build Coastguard Worker * 10*4d7e907cSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*4d7e907cSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*4d7e907cSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*4d7e907cSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*4d7e907cSAndroid Build Coastguard Worker * limitations under the License. 15*4d7e907cSAndroid Build Coastguard Worker */ 16*4d7e907cSAndroid Build Coastguard Worker 17*4d7e907cSAndroid Build Coastguard Worker #ifndef __BOOT_CONTROL_CLIENT_H_ 18*4d7e907cSAndroid Build Coastguard Worker #define __BOOT_CONTROL_CLIENT_H_ 19*4d7e907cSAndroid Build Coastguard Worker 20*4d7e907cSAndroid Build Coastguard Worker #include <aidl/android/hardware/boot/MergeStatus.h> 21*4d7e907cSAndroid Build Coastguard Worker 22*4d7e907cSAndroid Build Coastguard Worker #include <stdint.h> 23*4d7e907cSAndroid Build Coastguard Worker 24*4d7e907cSAndroid Build Coastguard Worker #include <memory> 25*4d7e907cSAndroid Build Coastguard Worker #include <optional> 26*4d7e907cSAndroid Build Coastguard Worker 27*4d7e907cSAndroid Build Coastguard Worker namespace android::hal { 28*4d7e907cSAndroid Build Coastguard Worker 29*4d7e907cSAndroid Build Coastguard Worker struct CommandResult { 30*4d7e907cSAndroid Build Coastguard Worker bool success; 31*4d7e907cSAndroid Build Coastguard Worker std::string errMsg; IsOkCommandResult32*4d7e907cSAndroid Build Coastguard Worker constexpr bool IsOk() const { return success; } 33*4d7e907cSAndroid Build Coastguard Worker }; 34*4d7e907cSAndroid Build Coastguard Worker 35*4d7e907cSAndroid Build Coastguard Worker enum class BootControlVersion { BOOTCTL_V1_0, BOOTCTL_V1_1, BOOTCTL_V1_2, BOOTCTL_AIDL }; 36*4d7e907cSAndroid Build Coastguard Worker 37*4d7e907cSAndroid Build Coastguard Worker class BootControlClient { 38*4d7e907cSAndroid Build Coastguard Worker public: 39*4d7e907cSAndroid Build Coastguard Worker using MergeStatus = aidl::android::hardware::boot::MergeStatus; 40*4d7e907cSAndroid Build Coastguard Worker virtual ~BootControlClient() = default; 41*4d7e907cSAndroid Build Coastguard Worker virtual BootControlVersion GetVersion() const = 0; 42*4d7e907cSAndroid Build Coastguard Worker // Return the number of update slots in the system. A system will normally 43*4d7e907cSAndroid Build Coastguard Worker // have two slots, named "A" and "B" in the documentation, but sometimes 44*4d7e907cSAndroid Build Coastguard Worker // images running from other media can have only one slot, like some USB 45*4d7e907cSAndroid Build Coastguard Worker // image. Systems with only one slot won't be able to update. 46*4d7e907cSAndroid Build Coastguard Worker [[nodiscard]] virtual int32_t GetNumSlots() const = 0; 47*4d7e907cSAndroid Build Coastguard Worker 48*4d7e907cSAndroid Build Coastguard Worker // Return the slot where we are running the system from. On success, the 49*4d7e907cSAndroid Build Coastguard Worker // result is a number between 0 and GetNumSlots() - 1. Otherwise, log an error 50*4d7e907cSAndroid Build Coastguard Worker // and return kInvalidSlot. 51*4d7e907cSAndroid Build Coastguard Worker [[nodiscard]] virtual int32_t GetCurrentSlot() const = 0; 52*4d7e907cSAndroid Build Coastguard Worker 53*4d7e907cSAndroid Build Coastguard Worker // Return string suffix for input slot. Usually, for slot 0 the suffix is _a, and for slot 1 the 54*4d7e907cSAndroid Build Coastguard Worker // suffix is _b. 55*4d7e907cSAndroid Build Coastguard Worker [[nodiscard]] virtual std::string GetSuffix(int32_t slot) const = 0; 56*4d7e907cSAndroid Build Coastguard Worker 57*4d7e907cSAndroid Build Coastguard Worker // Returns whether the passed |slot| is marked as bootable. Returns false if 58*4d7e907cSAndroid Build Coastguard Worker // the slot is invalid. 59*4d7e907cSAndroid Build Coastguard Worker [[nodiscard]] virtual std::optional<bool> IsSlotBootable(int32_t slot) const = 0; 60*4d7e907cSAndroid Build Coastguard Worker 61*4d7e907cSAndroid Build Coastguard Worker // Mark the specified slot unbootable. No other slot flags are modified. 62*4d7e907cSAndroid Build Coastguard Worker // Returns true on success. 63*4d7e907cSAndroid Build Coastguard Worker [[nodiscard]] virtual CommandResult MarkSlotUnbootable(int32_t slot) = 0; 64*4d7e907cSAndroid Build Coastguard Worker 65*4d7e907cSAndroid Build Coastguard Worker // Set the passed |slot| as the preferred boot slot. Returns whether it 66*4d7e907cSAndroid Build Coastguard Worker // succeeded setting the active slot. If succeeded, on next boot the 67*4d7e907cSAndroid Build Coastguard Worker // bootloader will attempt to load the |slot| marked as active. Note that this 68*4d7e907cSAndroid Build Coastguard Worker // method doesn't change the value of GetCurrentSlot() on the current boot. 69*4d7e907cSAndroid Build Coastguard Worker // Return true if operation succeeded. 70*4d7e907cSAndroid Build Coastguard Worker [[nodiscard]] virtual CommandResult SetActiveBootSlot(int32_t slot) = 0; 71*4d7e907cSAndroid Build Coastguard Worker 72*4d7e907cSAndroid Build Coastguard Worker // Check if |slot| is marked boot successfully. Return empty optional if the RPC call failed. 73*4d7e907cSAndroid Build Coastguard Worker [[nodiscard]] virtual std::optional<bool> IsSlotMarkedSuccessful(int32_t slot) const = 0; 74*4d7e907cSAndroid Build Coastguard Worker 75*4d7e907cSAndroid Build Coastguard Worker // Mark boot as successful. Return an error message if operation failed. 76*4d7e907cSAndroid Build Coastguard Worker [[nodiscard]] virtual CommandResult MarkBootSuccessful() = 0; 77*4d7e907cSAndroid Build Coastguard Worker 78*4d7e907cSAndroid Build Coastguard Worker // Added in IBootControl v1.1 79*4d7e907cSAndroid Build Coastguard Worker // Return the current merge status. 80*4d7e907cSAndroid Build Coastguard Worker [[nodiscard]] virtual MergeStatus getSnapshotMergeStatus() const = 0; 81*4d7e907cSAndroid Build Coastguard Worker 82*4d7e907cSAndroid Build Coastguard Worker // Set snapshot merge status, return true if succeeded. 83*4d7e907cSAndroid Build Coastguard Worker [[nodiscard]] virtual CommandResult SetSnapshotMergeStatus(MergeStatus status) = 0; 84*4d7e907cSAndroid Build Coastguard Worker 85*4d7e907cSAndroid Build Coastguard Worker // Added in IBootControl v1.2 86*4d7e907cSAndroid Build Coastguard Worker // Get the active slot. In other words, the slot which will be used on 87*4d7e907cSAndroid Build Coastguard Worker // next system reboot. This should match the |slot| parameter of last 88*4d7e907cSAndroid Build Coastguard Worker // successful call to |SetActiveBootSlot|. 89*4d7e907cSAndroid Build Coastguard Worker // Return 0xFFFFFFFF if underlying HAL doesn't support this operation. 90*4d7e907cSAndroid Build Coastguard Worker [[nodiscard]] virtual int32_t GetActiveBootSlot() const = 0; 91*4d7e907cSAndroid Build Coastguard Worker 92*4d7e907cSAndroid Build Coastguard Worker [[nodiscard]] static std::unique_ptr<BootControlClient> WaitForService(); 93*4d7e907cSAndroid Build Coastguard Worker }; 94*4d7e907cSAndroid Build Coastguard Worker 95*4d7e907cSAndroid Build Coastguard Worker } // namespace android::hal 96*4d7e907cSAndroid Build Coastguard Worker 97*4d7e907cSAndroid Build Coastguard Worker #endif 98