blob: 776333c84bef594b0132d658a53dead3adf0f635 [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>
23
Alex Deymoaa26f622015-09-16 18:21:27 -070024#include <base/callback.h>
Alex Deymo763e7db2015-08-27 21:08:08 -070025#include <base/macros.h>
26
27namespace chromeos_update_engine {
28
29// The abstract boot control interface defines the interaction with the
30// platform's bootloader hiding vendor-specific details from the rest of
31// update_engine. This interface is used for controlling where the device should
32// boot from.
33class BootControlInterface {
34 public:
35 using Slot = unsigned int;
Yifan Hong537802d2018-08-15 13:15:42 -070036 using PartitionSizes = std::map<std::string, uint64_t>;
Alex Deymo763e7db2015-08-27 21:08:08 -070037
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. On success, returns true and stores the block device in
57 // |device|.
58 virtual bool GetPartitionDevice(const std::string& partition_name,
59 Slot slot,
60 std::string* device) const = 0;
61
62 // Returns whether the passed |slot| is marked as bootable. Returns false if
63 // the slot is invalid.
64 virtual bool IsSlotBootable(Slot slot) const = 0;
65
66 // Mark the specified slot unbootable. No other slot flags are modified.
67 // Returns true on success.
68 virtual bool MarkSlotUnbootable(Slot slot) = 0;
69
Alex Deymo31d95ac2015-09-17 11:56:18 -070070 // Set the passed |slot| as the preferred boot slot. Returns whether it
71 // succeeded setting the active slot. If succeeded, on next boot the
72 // bootloader will attempt to load the |slot| marked as active. Note that this
73 // method doesn't change the value of GetCurrentSlot() on the current boot.
74 virtual bool SetActiveBootSlot(Slot slot) = 0;
75
Alex Deymoaa26f622015-09-16 18:21:27 -070076 // Mark the current slot as successfully booted asynchronously. No other slot
77 // flags are modified. Returns false if it was not able to schedule the
78 // operation, otherwise, returns true and calls the |callback| with the result
79 // of the operation.
80 virtual bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) = 0;
81
Yifan Hong537802d2018-08-15 13:15:42 -070082 // Initialize metadata of underlying partitions for a given |slot|.
83 // Ensure that partitions at the specified |slot| has a given size, as
84 // specified by |partition_sizes|. |partition_sizes| has the format:
85 // {"vendor": 524288000, "system": 2097152000, ...}; values must be
86 // aligned to the logical block size of the super partition.
87 virtual bool InitPartitionMetadata(Slot slot,
88 const PartitionSizes& partition_sizes) = 0;
89
90 // Do necessary clean-up operations after the whole update.
91 virtual void Cleanup() = 0;
92
Alex Deymo763e7db2015-08-27 21:08:08 -070093 // Return a human-readable slot name used for logging.
94 static std::string SlotName(Slot slot) {
95 if (slot == kInvalidSlot)
96 return "INVALID";
97 if (slot < 26)
98 return std::string(1, 'A' + slot);
99 return "TOO_BIG";
100 }
101
102 protected:
103 BootControlInterface() = default;
104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(BootControlInterface);
107};
108
109} // namespace chromeos_update_engine
110
Alex Deymo39910dc2015-11-09 17:04:30 -0800111#endif // UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_