xref: /aosp_15_r20/system/update_engine/common/boot_control_interface.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2015 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker 
17*5a923131SAndroid Build Coastguard Worker #ifndef UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_
18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_
19*5a923131SAndroid Build Coastguard Worker 
20*5a923131SAndroid Build Coastguard Worker #include <climits>
21*5a923131SAndroid Build Coastguard Worker #include <map>
22*5a923131SAndroid Build Coastguard Worker #include <string>
23*5a923131SAndroid Build Coastguard Worker #include <vector>
24*5a923131SAndroid Build Coastguard Worker 
25*5a923131SAndroid Build Coastguard Worker #include <base/callback.h>
26*5a923131SAndroid Build Coastguard Worker #include <android-base/macros.h>
27*5a923131SAndroid Build Coastguard Worker 
28*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/dynamic_partition_control_interface.h"
29*5a923131SAndroid Build Coastguard Worker #include "update_engine/update_metadata.pb.h"
30*5a923131SAndroid Build Coastguard Worker 
31*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
32*5a923131SAndroid Build Coastguard Worker 
33*5a923131SAndroid Build Coastguard Worker // The abstract boot control interface defines the interaction with the
34*5a923131SAndroid Build Coastguard Worker // platform's bootloader hiding vendor-specific details from the rest of
35*5a923131SAndroid Build Coastguard Worker // update_engine. This interface is used for controlling where the device should
36*5a923131SAndroid Build Coastguard Worker // boot from.
37*5a923131SAndroid Build Coastguard Worker class BootControlInterface {
38*5a923131SAndroid Build Coastguard Worker  public:
39*5a923131SAndroid Build Coastguard Worker   using Slot = unsigned int;
40*5a923131SAndroid Build Coastguard Worker 
41*5a923131SAndroid Build Coastguard Worker   static const Slot kInvalidSlot = UINT_MAX;
42*5a923131SAndroid Build Coastguard Worker 
43*5a923131SAndroid Build Coastguard Worker   virtual ~BootControlInterface() = default;
44*5a923131SAndroid Build Coastguard Worker 
45*5a923131SAndroid Build Coastguard Worker   // Return the number of update slots in the system. A system will normally
46*5a923131SAndroid Build Coastguard Worker   // have two slots, named "A" and "B" in the documentation, but sometimes
47*5a923131SAndroid Build Coastguard Worker   // images running from other media can have only one slot, like some USB
48*5a923131SAndroid Build Coastguard Worker   // image. Systems with only one slot won't be able to update.
49*5a923131SAndroid Build Coastguard Worker   virtual unsigned int GetNumSlots() const = 0;
50*5a923131SAndroid Build Coastguard Worker 
51*5a923131SAndroid Build Coastguard Worker   // Return the slot where we are running the system from. On success, the
52*5a923131SAndroid Build Coastguard Worker   // result is a number between 0 and GetNumSlots() - 1. Otherwise, log an error
53*5a923131SAndroid Build Coastguard Worker   // and return kInvalidSlot.
54*5a923131SAndroid Build Coastguard Worker   virtual Slot GetCurrentSlot() const = 0;
55*5a923131SAndroid Build Coastguard Worker 
56*5a923131SAndroid Build Coastguard Worker   // Determines the block device for the given partition name and slot number.
57*5a923131SAndroid Build Coastguard Worker   // The |slot| number must be between 0 and GetNumSlots() - 1 and the
58*5a923131SAndroid Build Coastguard Worker   // |partition_name| is a platform-specific name that identifies a partition on
59*5a923131SAndroid Build Coastguard Worker   // every slot. In order to access the dynamic partitions in the target slot,
60*5a923131SAndroid Build Coastguard Worker   // GetDynamicPartitionControl()->PreparePartitionsForUpdate() must be called
61*5a923131SAndroid Build Coastguard Worker   // (with |update| == true for the first time for a payload, and |false| for
62*5a923131SAndroid Build Coastguard Worker   // for the rest of the times) prior to calling this function.
63*5a923131SAndroid Build Coastguard Worker   // The handling may be different based on whether the partition is included
64*5a923131SAndroid Build Coastguard Worker   // in the update payload. On success, returns true; and stores the block
65*5a923131SAndroid Build Coastguard Worker   // device in |device|, if the partition is dynamic in |is_dynamic|.
66*5a923131SAndroid Build Coastguard Worker   virtual bool GetPartitionDevice(const std::string& partition_name,
67*5a923131SAndroid Build Coastguard Worker                                   Slot slot,
68*5a923131SAndroid Build Coastguard Worker                                   bool not_in_payload,
69*5a923131SAndroid Build Coastguard Worker                                   std::string* device,
70*5a923131SAndroid Build Coastguard Worker                                   bool* is_dynamic) const = 0;
71*5a923131SAndroid Build Coastguard Worker 
72*5a923131SAndroid Build Coastguard Worker   // Overload of the above function. We assume the partition is always included
73*5a923131SAndroid Build Coastguard Worker   // in the payload.
74*5a923131SAndroid Build Coastguard Worker   virtual bool GetPartitionDevice(const std::string& partition_name,
75*5a923131SAndroid Build Coastguard Worker                                   Slot slot,
76*5a923131SAndroid Build Coastguard Worker                                   std::string* device) const = 0;
77*5a923131SAndroid Build Coastguard Worker 
78*5a923131SAndroid Build Coastguard Worker   virtual std::optional<PartitionDevice> GetPartitionDevice(
79*5a923131SAndroid Build Coastguard Worker       const std::string& partition_name,
80*5a923131SAndroid Build Coastguard Worker       uint32_t slot,
81*5a923131SAndroid Build Coastguard Worker       uint32_t current_slot,
82*5a923131SAndroid Build Coastguard Worker       bool not_in_payload = false) const = 0;
83*5a923131SAndroid Build Coastguard Worker   // Returns whether the passed |slot| is marked as bootable. Returns false if
84*5a923131SAndroid Build Coastguard Worker   // the slot is invalid.
85*5a923131SAndroid Build Coastguard Worker   virtual bool IsSlotBootable(Slot slot) const = 0;
86*5a923131SAndroid Build Coastguard Worker 
87*5a923131SAndroid Build Coastguard Worker   // Mark the specified slot unbootable. No other slot flags are modified.
88*5a923131SAndroid Build Coastguard Worker   // Returns true on success.
89*5a923131SAndroid Build Coastguard Worker   virtual bool MarkSlotUnbootable(Slot slot) = 0;
90*5a923131SAndroid Build Coastguard Worker 
91*5a923131SAndroid Build Coastguard Worker   // Set the passed |slot| as the preferred boot slot. Returns whether it
92*5a923131SAndroid Build Coastguard Worker   // succeeded setting the active slot. If succeeded, on next boot the
93*5a923131SAndroid Build Coastguard Worker   // bootloader will attempt to load the |slot| marked as active. Note that this
94*5a923131SAndroid Build Coastguard Worker   // method doesn't change the value of GetCurrentSlot() on the current boot.
95*5a923131SAndroid Build Coastguard Worker   virtual bool SetActiveBootSlot(Slot slot) = 0;
96*5a923131SAndroid Build Coastguard Worker   // Get the active slot. In other words, the slot which will be used on
97*5a923131SAndroid Build Coastguard Worker   // next system reboot. This should match the |slot| parameter of last
98*5a923131SAndroid Build Coastguard Worker   // successful call to |SetActiveBootSlot|.
99*5a923131SAndroid Build Coastguard Worker   // Return 0xFFFFFFFF if underlying HAL doesn't support this operation.
100*5a923131SAndroid Build Coastguard Worker   virtual Slot GetActiveBootSlot() = 0;
101*5a923131SAndroid Build Coastguard Worker 
102*5a923131SAndroid Build Coastguard Worker   // Mark the current slot as successfully booted asynchronously. No other slot
103*5a923131SAndroid Build Coastguard Worker   // flags are modified. Returns false if it was not able to schedule the
104*5a923131SAndroid Build Coastguard Worker   // operation, otherwise, returns true and calls the |callback| with the result
105*5a923131SAndroid Build Coastguard Worker   // of the operation.
106*5a923131SAndroid Build Coastguard Worker   virtual bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) = 0;
107*5a923131SAndroid Build Coastguard Worker 
108*5a923131SAndroid Build Coastguard Worker   // Check if |slot| is marked boot successfully.
109*5a923131SAndroid Build Coastguard Worker   virtual bool IsSlotMarkedSuccessful(Slot slot) const = 0;
110*5a923131SAndroid Build Coastguard Worker 
111*5a923131SAndroid Build Coastguard Worker   // Return the dynamic partition control interface. Never null.
112*5a923131SAndroid Build Coastguard Worker   virtual DynamicPartitionControlInterface* GetDynamicPartitionControl() = 0;
113*5a923131SAndroid Build Coastguard Worker 
114*5a923131SAndroid Build Coastguard Worker   // Return a human-readable slot name used for logging.
SlotName(Slot slot)115*5a923131SAndroid Build Coastguard Worker   static std::string SlotName(Slot slot) {
116*5a923131SAndroid Build Coastguard Worker     if (slot == kInvalidSlot)
117*5a923131SAndroid Build Coastguard Worker       return "INVALID";
118*5a923131SAndroid Build Coastguard Worker     if (slot < 26)
119*5a923131SAndroid Build Coastguard Worker       return std::string(1, 'A' + slot);
120*5a923131SAndroid Build Coastguard Worker     return "TOO_BIG";
121*5a923131SAndroid Build Coastguard Worker   }
122*5a923131SAndroid Build Coastguard Worker 
123*5a923131SAndroid Build Coastguard Worker  protected:
124*5a923131SAndroid Build Coastguard Worker   BootControlInterface() = default;
125*5a923131SAndroid Build Coastguard Worker 
126*5a923131SAndroid Build Coastguard Worker  private:
127*5a923131SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(BootControlInterface);
128*5a923131SAndroid Build Coastguard Worker };
129*5a923131SAndroid Build Coastguard Worker 
130*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
131*5a923131SAndroid Build Coastguard Worker 
132*5a923131SAndroid Build Coastguard Worker #endif  // UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_
133