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
17#ifndef UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_
18#define UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_
19
20#include <climits>
21#include <string>
22
23#include <base/callback.h>
24#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
68  // Set the passed |slot| as the preferred boot slot. Returns whether it
69  // succeeded setting the active slot. If succeeded, on next boot the
70  // bootloader will attempt to load the |slot| marked as active. Note that this
71  // method doesn't change the value of GetCurrentSlot() on the current boot.
72  virtual bool SetActiveBootSlot(Slot slot) = 0;
73
74  // Mark the current slot as successfully booted asynchronously. No other slot
75  // flags are modified. Returns false if it was not able to schedule the
76  // operation, otherwise, returns true and calls the |callback| with the result
77  // of the operation.
78  virtual bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) = 0;
79
80  // Return a human-readable slot name used for logging.
81  static std::string SlotName(Slot slot) {
82    if (slot == kInvalidSlot)
83      return "INVALID";
84    if (slot < 26)
85      return std::string(1, 'A' + slot);
86    return "TOO_BIG";
87  }
88
89 protected:
90  BootControlInterface() = default;
91
92 private:
93  DISALLOW_COPY_AND_ASSIGN(BootControlInterface);
94};
95
96}  // namespace chromeos_update_engine
97
98#endif  // UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_
99