blob: 36f2caf19883af85774e22c69e62f39152e9eda2 [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
17#ifndef UPDATE_ENGINE_BOOT_CONTROL_INTERFACE_H_
18#define UPDATE_ENGINE_BOOT_CONTROL_INTERFACE_H_
19
20#include <climits>
21#include <string>
22
Alex Deymoaa26f622015-09-16 18:21:27 -070023#include <base/callback.h>
Alex Deymo763e7db2015-08-27 21:08:08 -070024#include <base/macros.h>
25
26namespace chromeos_update_engine {
27
28// The abstract boot control interface defines the interaction with the
29// platform's bootloader hiding vendor-specific details from the rest of
30// update_engine. This interface is used for controlling where the device should
31// boot from.
32class BootControlInterface {
33 public:
34 using Slot = unsigned int;
35
36 static const Slot kInvalidSlot = UINT_MAX;
37
38 virtual ~BootControlInterface() = default;
39
40 // Return the number of update slots in the system. A system will normally
41 // have two slots, named "A" and "B" in the documentation, but sometimes
42 // images running from other media can have only one slot, like some USB
43 // image. Systems with only one slot won't be able to update.
44 virtual unsigned int GetNumSlots() const = 0;
45
46 // Return the slot where we are running the system from. On success, the
47 // result is a number between 0 and GetNumSlots() - 1. Otherwise, log an error
48 // and return kInvalidSlot.
49 virtual Slot GetCurrentSlot() const = 0;
50
51 // Determines the block device for the given partition name and slot number.
52 // The |slot| number must be between 0 and GetNumSlots() - 1 and the
53 // |partition_name| is a platform-specific name that identifies a partition on
54 // every slot. On success, returns true and stores the block device in
55 // |device|.
56 virtual bool GetPartitionDevice(const std::string& partition_name,
57 Slot slot,
58 std::string* device) const = 0;
59
60 // Returns whether the passed |slot| is marked as bootable. Returns false if
61 // the slot is invalid.
62 virtual bool IsSlotBootable(Slot slot) const = 0;
63
64 // Mark the specified slot unbootable. No other slot flags are modified.
65 // Returns true on success.
66 virtual bool MarkSlotUnbootable(Slot slot) = 0;
67
Alex Deymoaa26f622015-09-16 18:21:27 -070068 // Mark the current slot as successfully booted asynchronously. No other slot
69 // flags are modified. Returns false if it was not able to schedule the
70 // operation, otherwise, returns true and calls the |callback| with the result
71 // of the operation.
72 virtual bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) = 0;
73
Alex Deymo763e7db2015-08-27 21:08:08 -070074 // Return a human-readable slot name used for logging.
75 static std::string SlotName(Slot slot) {
76 if (slot == kInvalidSlot)
77 return "INVALID";
78 if (slot < 26)
79 return std::string(1, 'A' + slot);
80 return "TOO_BIG";
81 }
82
83 protected:
84 BootControlInterface() = default;
85
86 private:
87 DISALLOW_COPY_AND_ASSIGN(BootControlInterface);
88};
89
90} // namespace chromeos_update_engine
91
92#endif // UPDATE_ENGINE_BOOT_CONTROL_INTERFACE_H_