Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 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 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_ |
| 18 | #define UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_ |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 19 | |
| 20 | #include <climits> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 21 | #include <map> |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 22 | #include <string> |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 23 | #include <vector> |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 24 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 25 | #include <base/callback.h> |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 26 | #include <base/macros.h> |
| 27 | |
Yifan Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame^] | 28 | #include "update_engine/common/dynamic_partition_control_interface.h" |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 29 | #include "update_engine/update_metadata.pb.h" |
| 30 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 31 | namespace chromeos_update_engine { |
| 32 | |
| 33 | // The abstract boot control interface defines the interaction with the |
| 34 | // platform's bootloader hiding vendor-specific details from the rest of |
| 35 | // update_engine. This interface is used for controlling where the device should |
| 36 | // boot from. |
| 37 | class BootControlInterface { |
| 38 | public: |
| 39 | using Slot = unsigned int; |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 40 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 41 | static const Slot kInvalidSlot = UINT_MAX; |
| 42 | |
| 43 | virtual ~BootControlInterface() = default; |
| 44 | |
| 45 | // Return the number of update slots in the system. A system will normally |
| 46 | // have two slots, named "A" and "B" in the documentation, but sometimes |
| 47 | // images running from other media can have only one slot, like some USB |
| 48 | // image. Systems with only one slot won't be able to update. |
| 49 | virtual unsigned int GetNumSlots() const = 0; |
| 50 | |
| 51 | // Return the slot where we are running the system from. On success, the |
| 52 | // result is a number between 0 and GetNumSlots() - 1. Otherwise, log an error |
| 53 | // and return kInvalidSlot. |
| 54 | virtual Slot GetCurrentSlot() const = 0; |
| 55 | |
| 56 | // Determines the block device for the given partition name and slot number. |
| 57 | // The |slot| number must be between 0 and GetNumSlots() - 1 and the |
| 58 | // |partition_name| is a platform-specific name that identifies a partition on |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 59 | // every slot. In order to access the dynamic partitions in the target slot, |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 60 | // PreparePartitionsForUpdate() must be called (once per payload) prior to |
| 61 | // calling this function. On success, returns true and stores the block device |
| 62 | // in |device|. |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 63 | virtual bool GetPartitionDevice(const std::string& partition_name, |
| 64 | Slot slot, |
| 65 | std::string* device) const = 0; |
| 66 | |
| 67 | // Returns whether the passed |slot| is marked as bootable. Returns false if |
| 68 | // the slot is invalid. |
| 69 | virtual bool IsSlotBootable(Slot slot) const = 0; |
| 70 | |
| 71 | // Mark the specified slot unbootable. No other slot flags are modified. |
| 72 | // Returns true on success. |
| 73 | virtual bool MarkSlotUnbootable(Slot slot) = 0; |
| 74 | |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 75 | // Set the passed |slot| as the preferred boot slot. Returns whether it |
| 76 | // succeeded setting the active slot. If succeeded, on next boot the |
| 77 | // bootloader will attempt to load the |slot| marked as active. Note that this |
| 78 | // method doesn't change the value of GetCurrentSlot() on the current boot. |
| 79 | virtual bool SetActiveBootSlot(Slot slot) = 0; |
| 80 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 81 | // Mark the current slot as successfully booted asynchronously. No other slot |
| 82 | // flags are modified. Returns false if it was not able to schedule the |
| 83 | // operation, otherwise, returns true and calls the |callback| with the result |
| 84 | // of the operation. |
| 85 | virtual bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) = 0; |
| 86 | |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 87 | // Initializes the metadata of the underlying partitions for a given |slot| |
| 88 | // and sets up the states for accessing dynamic partitions. |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 89 | // Metadata will be written to the specified |slot| if |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 90 | // |update_metadata| is set. |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 91 | virtual bool PreparePartitionsForUpdate(Slot slot, |
| 92 | const DeltaArchiveManifest& manifest, |
| 93 | bool update_metadata) = 0; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 94 | |
| 95 | // Do necessary clean-up operations after the whole update. |
| 96 | virtual void Cleanup() = 0; |
| 97 | |
Yifan Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame^] | 98 | // Return the dynamic partition control interface. |
| 99 | virtual DynamicPartitionControlInterface* GetDynamicPartitionControl() = 0; |
| 100 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 101 | // Return a human-readable slot name used for logging. |
| 102 | static std::string SlotName(Slot slot) { |
| 103 | if (slot == kInvalidSlot) |
| 104 | return "INVALID"; |
| 105 | if (slot < 26) |
| 106 | return std::string(1, 'A' + slot); |
| 107 | return "TOO_BIG"; |
| 108 | } |
| 109 | |
| 110 | protected: |
| 111 | BootControlInterface() = default; |
| 112 | |
| 113 | private: |
| 114 | DISALLOW_COPY_AND_ASSIGN(BootControlInterface); |
| 115 | }; |
| 116 | |
| 117 | } // namespace chromeos_update_engine |
| 118 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 119 | #endif // UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_ |