blob: 9bf639a61043a8c30a9d396172b0ae98c5babe6a [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 Hong13d41cb2019-09-16 13:18:22 -070028#include "update_engine/update_metadata.pb.h"
29
Alex Deymo763e7db2015-08-27 21:08:08 -070030namespace chromeos_update_engine {
31
32// The abstract boot control interface defines the interaction with the
33// platform's bootloader hiding vendor-specific details from the rest of
34// update_engine. This interface is used for controlling where the device should
35// boot from.
36class BootControlInterface {
37 public:
38 using Slot = unsigned int;
Yifan Hongd4db07e2018-10-18 17:46:27 -070039
Alex Deymo763e7db2015-08-27 21:08:08 -070040 static const Slot kInvalidSlot = UINT_MAX;
41
42 virtual ~BootControlInterface() = default;
43
44 // Return the number of update slots in the system. A system will normally
45 // have two slots, named "A" and "B" in the documentation, but sometimes
46 // images running from other media can have only one slot, like some USB
47 // image. Systems with only one slot won't be able to update.
48 virtual unsigned int GetNumSlots() const = 0;
49
50 // Return the slot where we are running the system from. On success, the
51 // result is a number between 0 and GetNumSlots() - 1. Otherwise, log an error
52 // and return kInvalidSlot.
53 virtual Slot GetCurrentSlot() const = 0;
54
55 // Determines the block device for the given partition name and slot number.
56 // The |slot| number must be between 0 and GetNumSlots() - 1 and the
57 // |partition_name| is a platform-specific name that identifies a partition on
Tao Bao3406c772019-01-02 15:34:35 -080058 // every slot. In order to access the dynamic partitions in the target slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -070059 // PreparePartitionsForUpdate() must be called (once per payload) prior to
60 // calling this function. On success, returns true and stores the block device
61 // in |device|.
Alex Deymo763e7db2015-08-27 21:08:08 -070062 virtual bool GetPartitionDevice(const std::string& partition_name,
63 Slot slot,
64 std::string* device) const = 0;
65
66 // Returns whether the passed |slot| is marked as bootable. Returns false if
67 // the slot is invalid.
68 virtual bool IsSlotBootable(Slot slot) const = 0;
69
70 // Mark the specified slot unbootable. No other slot flags are modified.
71 // Returns true on success.
72 virtual bool MarkSlotUnbootable(Slot slot) = 0;
73
Alex Deymo31d95ac2015-09-17 11:56:18 -070074 // Set the passed |slot| as the preferred boot slot. Returns whether it
75 // succeeded setting the active slot. If succeeded, on next boot the
76 // bootloader will attempt to load the |slot| marked as active. Note that this
77 // method doesn't change the value of GetCurrentSlot() on the current boot.
78 virtual bool SetActiveBootSlot(Slot slot) = 0;
79
Alex Deymoaa26f622015-09-16 18:21:27 -070080 // Mark the current slot as successfully booted asynchronously. No other slot
81 // flags are modified. Returns false if it was not able to schedule the
82 // operation, otherwise, returns true and calls the |callback| with the result
83 // of the operation.
84 virtual bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) = 0;
85
Tao Bao3406c772019-01-02 15:34:35 -080086 // Initializes the metadata of the underlying partitions for a given |slot|
87 // and sets up the states for accessing dynamic partitions.
Yifan Hong13d41cb2019-09-16 13:18:22 -070088 // Metadata will be written to the specified |slot| if
Tao Bao3406c772019-01-02 15:34:35 -080089 // |update_metadata| is set.
Yifan Hong13d41cb2019-09-16 13:18:22 -070090 virtual bool PreparePartitionsForUpdate(Slot slot,
91 const DeltaArchiveManifest& manifest,
92 bool update_metadata) = 0;
Yifan Hong537802d2018-08-15 13:15:42 -070093
94 // Do necessary clean-up operations after the whole update.
95 virtual void Cleanup() = 0;
96
Alex Deymo763e7db2015-08-27 21:08:08 -070097 // Return a human-readable slot name used for logging.
98 static std::string SlotName(Slot slot) {
99 if (slot == kInvalidSlot)
100 return "INVALID";
101 if (slot < 26)
102 return std::string(1, 'A' + slot);
103 return "TOO_BIG";
104 }
105
106 protected:
107 BootControlInterface() = default;
108
109 private:
110 DISALLOW_COPY_AND_ASSIGN(BootControlInterface);
111};
112
113} // namespace chromeos_update_engine
114
Alex Deymo39910dc2015-11-09 17:04:30 -0800115#endif // UPDATE_ENGINE_COMMON_BOOT_CONTROL_INTERFACE_H_