• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2015 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 UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_
18 #define UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_
19 
20 #include <climits>
21 #include <string>
22 
23 #include <base/callback.h>
24 #include <android-base/macros.h>
25 
26 #include "update_engine/common/dynamic_partition_control_interface.h"
27 
28 namespace chromeos_update_engine {
29 
30 // The abstract boot control interface defines the interaction with the
31 // platform's bootloader hiding vendor-specific details from the rest of
32 // update_engine. This interface is used for controlling where the device should
33 // boot from.
34 class BootControlInterface {
35  public:
36   using Slot = unsigned int;
37 
38   static const Slot kInvalidSlot = UINT_MAX;
39 
40   virtual ~BootControlInterface() = default;
41 
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   virtual unsigned int 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   virtual Slot GetCurrentSlot() const = 0;
52 
53   // Determines the block device for the given partition name and slot number.
54   // The |slot| number must be between 0 and GetNumSlots() - 1 and the
55   // |partition_name| is a platform-specific name that identifies a partition on
56   // every slot. In order to access the dynamic partitions in the target slot,
57   // GetDynamicPartitionControl()->PreparePartitionsForUpdate() must be called
58   // (with |update| == true for the first time for a payload, and |false| for
59   // for the rest of the times) prior to calling this function.
60   // The handling may be different based on whether the partition is included
61   // in the update payload. On success, returns true; and stores the block
62   // device in |device|, if the partition is dynamic in |is_dynamic|.
63   virtual bool GetPartitionDevice(const std::string& partition_name,
64                                   Slot slot,
65                                   bool not_in_payload,
66                                   std::string* device,
67                                   bool* is_dynamic) const = 0;
68 
69   // Overload of the above function. We assume the partition is always included
70   // in the payload.
71   virtual bool GetPartitionDevice(const std::string& partition_name,
72                                   Slot slot,
73                                   std::string* device) const = 0;
74 
75   virtual std::optional<PartitionDevice> GetPartitionDevice(
76       const std::string& partition_name,
77       uint32_t slot,
78       uint32_t current_slot,
79       bool not_in_payload = false) const = 0;
80   // Returns whether the passed |slot| is marked as bootable. Returns false if
81   // the slot is invalid.
82   virtual bool IsSlotBootable(Slot slot) const = 0;
83 
84   // Mark the specified slot unbootable. No other slot flags are modified.
85   // Returns true on success.
86   virtual bool MarkSlotUnbootable(Slot slot) = 0;
87 
88   // Set the passed |slot| as the preferred boot slot. Returns whether it
89   // succeeded setting the active slot. If succeeded, on next boot the
90   // bootloader will attempt to load the |slot| marked as active. Note that this
91   // method doesn't change the value of GetCurrentSlot() on the current boot.
92   virtual bool SetActiveBootSlot(Slot slot) = 0;
93   // Get the active slot. In other words, the slot which will be used on
94   // next system reboot. This should match the |slot| parameter of last
95   // successful call to |SetActiveBootSlot|.
96   // Return 0xFFFFFFFF if underlying HAL doesn't support this operation.
97   virtual Slot GetActiveBootSlot() = 0;
98 
99   // Mark the current slot as successfully booted asynchronously. No other slot
100   // flags are modified. Returns false if it was not able to schedule the
101   // operation, otherwise, returns true and calls the |callback| with the result
102   // of the operation.
103   virtual bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) = 0;
104 
105   // Check if |slot| is marked boot successfully.
106   virtual bool IsSlotMarkedSuccessful(Slot slot) const = 0;
107 
108   // Return the dynamic partition control interface. Never null.
109   virtual DynamicPartitionControlInterface* GetDynamicPartitionControl() = 0;
110 
111   // Return a human-readable slot name used for logging.
SlotName(Slot slot)112   static std::string SlotName(Slot slot) {
113     if (slot == kInvalidSlot)
114       return "INVALID";
115     if (slot < 26)
116       return std::string(1, 'A' + slot);
117     return "TOO_BIG";
118   }
119 
120  protected:
121   BootControlInterface() = default;
122 
123  private:
124   DISALLOW_COPY_AND_ASSIGN(BootControlInterface);
125 };
126 
127 }  // namespace chromeos_update_engine
128 
129 #endif  // UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_
130