blob: 43517ce25a779c10ccaf1bcf56578d0cd064a6f9 [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
28namespace 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.
34class BootControlInterface {
35 public:
36 using Slot = unsigned int;
Yifan Hongd4db07e2018-10-18 17:46:27 -070037
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 };
Alex Deymo763e7db2015-08-27 21:08:08 -070050
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. On success, returns true and stores the block device in
70 // |device|.
71 virtual bool GetPartitionDevice(const std::string& partition_name,
72 Slot slot,
73 std::string* device) const = 0;
74
75 // Returns whether the passed |slot| is marked as bootable. Returns false if
76 // the slot is invalid.
77 virtual bool IsSlotBootable(Slot slot) const = 0;
78
79 // Mark the specified slot unbootable. No other slot flags are modified.
80 // Returns true on success.
81 virtual bool MarkSlotUnbootable(Slot slot) = 0;
82
Alex Deymo31d95ac2015-09-17 11:56:18 -070083 // Set the passed |slot| as the preferred boot slot. Returns whether it
84 // succeeded setting the active slot. If succeeded, on next boot the
85 // bootloader will attempt to load the |slot| marked as active. Note that this
86 // method doesn't change the value of GetCurrentSlot() on the current boot.
87 virtual bool SetActiveBootSlot(Slot slot) = 0;
88
Alex Deymoaa26f622015-09-16 18:21:27 -070089 // Mark the current slot as successfully booted asynchronously. No other slot
90 // flags are modified. Returns false if it was not able to schedule the
91 // operation, otherwise, returns true and calls the |callback| with the result
92 // of the operation.
93 virtual bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) = 0;
94
Yifan Hong537802d2018-08-15 13:15:42 -070095 // Initialize metadata of underlying partitions for a given |slot|.
Yifan Hongd4db07e2018-10-18 17:46:27 -070096 // Ensure that all updateable groups with the suffix GetSuffix(|slot|) exactly
97 // matches the layout specified in |partition_metadata|. Ensure that
98 // partitions at the specified |slot| has a given size and updateable group,
99 // as specified by |partition_metadata|. Sizes must be aligned to the logical
100 // block size of the super partition.
Yifan Hong6b0a9842018-10-16 16:54:18 -0700101 virtual bool InitPartitionMetadata(
102 Slot slot, const PartitionMetadata& partition_metadata) = 0;
Yifan Hong537802d2018-08-15 13:15:42 -0700103
104 // Do necessary clean-up operations after the whole update.
105 virtual void Cleanup() = 0;
106
Alex Deymo763e7db2015-08-27 21:08:08 -0700107 // Return a human-readable slot name used for logging.
108 static std::string SlotName(Slot slot) {
109 if (slot == kInvalidSlot)
110 return "INVALID";
111 if (slot < 26)
112 return std::string(1, 'A' + slot);
113 return "TOO_BIG";
114 }
115
116 protected:
117 BootControlInterface() = default;
118
119 private:
120 DISALLOW_COPY_AND_ASSIGN(BootControlInterface);
121};
122
123} // namespace chromeos_update_engine
124
Alex Deymo39910dc2015-11-09 17:04:30 -0800125#endif // UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_