blob: 2de21a16c85953cea30d507c8e79fff4d3e02bc0 [file] [log] [blame]
Alex Deymo763e7db2015-08-27 21:08:08 -07001//
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 Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_
18#define UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_
Alex Deymo763e7db2015-08-27 21:08:08 -070019
20#include <climits>
Yifan Hong537802d2018-08-15 13:15:42 -070021#include <map>
Alex Deymo763e7db2015-08-27 21:08:08 -070022#include <string>
Yifan Hongd4db07e2018-10-18 17:46:27 -070023#include <vector>
Alex Deymo763e7db2015-08-27 21:08:08 -070024
Alex Deymoaa26f622015-09-16 18:21:27 -070025#include <base/callback.h>
Alex Deymo763e7db2015-08-27 21:08:08 -070026#include <base/macros.h>
27
Yifan Hongdaac7322019-11-07 10:48:26 -080028#include "update_engine/common/dynamic_partition_control_interface.h"
Yifan Hong13d41cb2019-09-16 13:18:22 -070029#include "update_engine/update_metadata.pb.h"
30
Alex Deymo763e7db2015-08-27 21:08:08 -070031namespace 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.
37class BootControlInterface {
38 public:
39 using Slot = unsigned int;
Yifan Hongd4db07e2018-10-18 17:46:27 -070040
Alex Deymo763e7db2015-08-27 21:08:08 -070041 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 Bao3406c772019-01-02 15:34:35 -080059 // every slot. In order to access the dynamic partitions in the target slot,
Yifan Hong02e2b6b2019-11-07 11:00:39 -080060 // GetDynamicPartitionControl()->PreparePartitionsForUpdate() must be called
61 // (with |update| == true for the first time for a payload, and |false| for
Tianjie51a5a392020-06-03 14:39:32 -070062 // for the rest of the times) prior to calling this function.
63 // The handling may be different based on whether the partition is included
64 // in the update payload. On success, returns true; and stores the block
65 // device in |device|, if the partition is dynamic in |is_dynamic|.
66 virtual bool GetPartitionDevice(const std::string& partition_name,
67 Slot slot,
68 bool not_in_payload,
69 std::string* device,
70 bool* is_dynamic) const = 0;
71
72 // Overload of the above function. We assume the partition is always included
73 // in the payload.
Alex Deymo763e7db2015-08-27 21:08:08 -070074 virtual bool GetPartitionDevice(const std::string& partition_name,
75 Slot slot,
76 std::string* device) const = 0;
77
Kelvin Zhang91d95fa2020-11-05 13:52:00 -050078 virtual std::optional<PartitionDevice> GetPartitionDevice(
79 const std::string& partition_name,
80 uint32_t slot,
81 uint32_t current_slot,
82 bool not_in_payload = false) const = 0;
Alex Deymo763e7db2015-08-27 21:08:08 -070083 // Returns whether the passed |slot| is marked as bootable. Returns false if
84 // the slot is invalid.
85 virtual bool IsSlotBootable(Slot slot) const = 0;
86
87 // Mark the specified slot unbootable. No other slot flags are modified.
88 // Returns true on success.
89 virtual bool MarkSlotUnbootable(Slot slot) = 0;
90
Alex Deymo31d95ac2015-09-17 11:56:18 -070091 // Set the passed |slot| as the preferred boot slot. Returns whether it
92 // succeeded setting the active slot. If succeeded, on next boot the
93 // bootloader will attempt to load the |slot| marked as active. Note that this
94 // method doesn't change the value of GetCurrentSlot() on the current boot.
95 virtual bool SetActiveBootSlot(Slot slot) = 0;
Kelvin Zhangcb419e62021-06-16 13:56:47 -040096 // Get the active slot. In other words, the slot which will be used on
97 // next system reboot. This should match the |slot| parameter of last
98 // successful call to |SetActiveBootSlot|.
99 // Return 0xFFFFFFFF if underlying HAL doesn't support this operation.
100 virtual Slot GetActiveBootSlot() = 0;
Alex Deymo31d95ac2015-09-17 11:56:18 -0700101
Alex Deymoaa26f622015-09-16 18:21:27 -0700102 // Mark the current slot as successfully booted asynchronously. No other slot
103 // flags are modified. Returns false if it was not able to schedule the
104 // operation, otherwise, returns true and calls the |callback| with the result
105 // of the operation.
106 virtual bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) = 0;
107
Yifan Hongf1415942020-02-24 18:34:49 -0800108 // Check if |slot| is marked boot successfully.
109 virtual bool IsSlotMarkedSuccessful(Slot slot) const = 0;
110
Yifan Hongc514f662021-02-04 11:18:43 -0800111 // Return the dynamic partition control interface. Never null.
Yifan Hongdaac7322019-11-07 10:48:26 -0800112 virtual DynamicPartitionControlInterface* GetDynamicPartitionControl() = 0;
113
Alex Deymo763e7db2015-08-27 21:08:08 -0700114 // Return a human-readable slot name used for logging.
115 static std::string SlotName(Slot slot) {
116 if (slot == kInvalidSlot)
117 return "INVALID";
118 if (slot < 26)
119 return std::string(1, 'A' + slot);
120 return "TOO_BIG";
121 }
122
123 protected:
124 BootControlInterface() = default;
125
126 private:
127 DISALLOW_COPY_AND_ASSIGN(BootControlInterface);
128};
129
130} // namespace chromeos_update_engine
131
Alex Deymo39910dc2015-11-09 17:04:30 -0800132#endif // UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_