• 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 <map>
22 #include <string>
23 #include <vector>
24 
25 #include <base/callback.h>
26 #include <base/macros.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   struct PartitionMetadata {
39     struct Partition {
40       std::string name;
41       uint64_t size;
42     };
43     struct Group {
44       std::string name;
45       uint64_t size;
46       std::vector<Partition> partitions;
47     };
48     std::vector<Group> groups;
49   };
50 
51   static const Slot kInvalidSlot = UINT_MAX;
52 
53   virtual ~BootControlInterface() = default;
54 
55   // Return the number of update slots in the system. A system will normally
56   // have two slots, named "A" and "B" in the documentation, but sometimes
57   // images running from other media can have only one slot, like some USB
58   // image. Systems with only one slot won't be able to update.
59   virtual unsigned int GetNumSlots() const = 0;
60 
61   // Return the slot where we are running the system from. On success, the
62   // result is a number between 0 and GetNumSlots() - 1. Otherwise, log an error
63   // and return kInvalidSlot.
64   virtual Slot GetCurrentSlot() const = 0;
65 
66   // Determines the block device for the given partition name and slot number.
67   // The |slot| number must be between 0 and GetNumSlots() - 1 and the
68   // |partition_name| is a platform-specific name that identifies a partition on
69   // every slot. In order to access the dynamic partitions in the target slot,
70   // InitPartitionMetadata() must be called (once per payload) prior to calling
71   // this function. On success, returns true and stores the block device in
72   // |device|.
73   virtual bool GetPartitionDevice(const std::string& partition_name,
74                                   Slot slot,
75                                   std::string* device) const = 0;
76 
77   // Returns whether the passed |slot| is marked as bootable. Returns false if
78   // the slot is invalid.
79   virtual bool IsSlotBootable(Slot slot) const = 0;
80 
81   // Mark the specified slot unbootable. No other slot flags are modified.
82   // Returns true on success.
83   virtual bool MarkSlotUnbootable(Slot slot) = 0;
84 
85   // Set the passed |slot| as the preferred boot slot. Returns whether it
86   // succeeded setting the active slot. If succeeded, on next boot the
87   // bootloader will attempt to load the |slot| marked as active. Note that this
88   // method doesn't change the value of GetCurrentSlot() on the current boot.
89   virtual bool SetActiveBootSlot(Slot slot) = 0;
90 
91   // Mark the current slot as successfully booted asynchronously. No other slot
92   // flags are modified. Returns false if it was not able to schedule the
93   // operation, otherwise, returns true and calls the |callback| with the result
94   // of the operation.
95   virtual bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) = 0;
96 
97   // Initializes the metadata of the underlying partitions for a given |slot|
98   // and sets up the states for accessing dynamic partitions.
99   // |partition_metadata| will be written to the specified |slot| if
100   // |update_metadata| is set.
101   virtual bool InitPartitionMetadata(
102       Slot slot,
103       const PartitionMetadata& partition_metadata,
104       bool update_metadata) = 0;
105 
106   // Do necessary clean-up operations after the whole update.
107   virtual void Cleanup() = 0;
108 
109   // Return a human-readable slot name used for logging.
SlotName(Slot slot)110   static std::string SlotName(Slot slot) {
111     if (slot == kInvalidSlot)
112       return "INVALID";
113     if (slot < 26)
114       return std::string(1, 'A' + slot);
115     return "TOO_BIG";
116   }
117 
118  protected:
119   BootControlInterface() = default;
120 
121  private:
122   DISALLOW_COPY_AND_ASSIGN(BootControlInterface);
123 };
124 
125 }  // namespace chromeos_update_engine
126 
127 #endif  // UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_
128