blob: 79e2139045dbf0c984749c576009f03af3d0e33b [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_FAKE_BOOT_CONTROL_H_
18#define UPDATE_ENGINE_COMMON_FAKE_BOOT_CONTROL_H_
Alex Deymo763e7db2015-08-27 21:08:08 -070019
20#include <map>
Yifan Hongdaac7322019-11-07 10:48:26 -080021#include <memory>
Alex Deymo763e7db2015-08-27 21:08:08 -070022#include <string>
23#include <vector>
24
25#include <base/time/time.h>
26
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/boot_control_interface.h"
Yifan Hongdaac7322019-11-07 10:48:26 -080028#include "update_engine/common/dynamic_partition_control_stub.h"
Alex Deymo763e7db2015-08-27 21:08:08 -070029
30namespace chromeos_update_engine {
31
32// Implements a fake bootloader control interface used for testing.
33class FakeBootControl : public BootControlInterface {
34 public:
35 FakeBootControl() {
36 SetNumSlots(num_slots_);
37 // The current slot should be bootable.
38 is_bootable_[current_slot_] = true;
Yifan Hongdaac7322019-11-07 10:48:26 -080039
40 dynamic_partition_control_.reset(new DynamicPartitionControlStub());
Alex Deymo763e7db2015-08-27 21:08:08 -070041 }
42
43 // BootControlInterface overrides.
44 unsigned int GetNumSlots() const override { return num_slots_; }
45 BootControlInterface::Slot GetCurrentSlot() const override {
46 return current_slot_;
47 }
48
49 bool GetPartitionDevice(const std::string& partition_name,
50 BootControlInterface::Slot slot,
Tianjie51a5a392020-06-03 14:39:32 -070051 bool not_in_payload,
52 std::string* device,
53 bool* is_dynamic) const override {
Kelvin Zhang91d95fa2020-11-05 13:52:00 -050054 auto dev =
55 GetPartitionDevice(partition_name, slot, current_slot_, not_in_payload);
56 if (!dev.has_value()) {
Alex Deymo763e7db2015-08-27 21:08:08 -070057 return false;
Kelvin Zhang91d95fa2020-11-05 13:52:00 -050058 }
59 if (is_dynamic) {
60 *is_dynamic = dev->is_dynamic;
61 }
62 if (device) {
63 *device = dev->rw_device_path;
Kelvin Zhang3b323cf2020-08-05 10:56:02 -040064 }
Alex Deymo763e7db2015-08-27 21:08:08 -070065 return true;
66 }
67
Tianjie51a5a392020-06-03 14:39:32 -070068 bool GetPartitionDevice(const std::string& partition_name,
69 BootControlInterface::Slot slot,
70 std::string* device) const override {
71 return GetPartitionDevice(partition_name, slot, false, device, nullptr);
72 }
73
Alex Deymo763e7db2015-08-27 21:08:08 -070074 bool IsSlotBootable(BootControlInterface::Slot slot) const override {
75 return slot < num_slots_ && is_bootable_[slot];
76 }
77
78 bool MarkSlotUnbootable(BootControlInterface::Slot slot) override {
79 if (slot >= num_slots_)
80 return false;
81 is_bootable_[slot] = false;
82 return true;
83 }
84
Alex Deymo31d95ac2015-09-17 11:56:18 -070085 bool SetActiveBootSlot(Slot slot) override { return true; }
86
Alex Deymoaa26f622015-09-16 18:21:27 -070087 bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) override {
88 // We run the callback directly from here to avoid having to setup a message
89 // loop in the test environment.
Yifan Hongf1415942020-02-24 18:34:49 -080090 is_marked_successful_[GetCurrentSlot()] = true;
Alex Deymoaa26f622015-09-16 18:21:27 -070091 callback.Run(true);
92 return true;
93 }
94
Yifan Hongf1415942020-02-24 18:34:49 -080095 bool IsSlotMarkedSuccessful(Slot slot) const override {
96 return slot < num_slots_ && is_marked_successful_[slot];
Yifan Hong537802d2018-08-15 13:15:42 -070097 }
98
Alex Deymo763e7db2015-08-27 21:08:08 -070099 // Setters
100 void SetNumSlots(unsigned int num_slots) {
101 num_slots_ = num_slots;
102 is_bootable_.resize(num_slots_, false);
Yifan Hongf1415942020-02-24 18:34:49 -0800103 is_marked_successful_.resize(num_slots_, false);
Alex Deymo763e7db2015-08-27 21:08:08 -0700104 devices_.resize(num_slots_);
105 }
106
Amin Hassanib2689592019-01-13 17:04:28 -0800107 void SetCurrentSlot(BootControlInterface::Slot slot) { current_slot_ = slot; }
Alex Deymo763e7db2015-08-27 21:08:08 -0700108
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -0700109 void SetPartitionDevice(const std::string& partition_name,
Alex Deymo763e7db2015-08-27 21:08:08 -0700110 BootControlInterface::Slot slot,
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -0700111 const std::string& device) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700112 DCHECK(slot < num_slots_);
113 devices_[slot][partition_name] = device;
114 }
115
116 void SetSlotBootable(BootControlInterface::Slot slot, bool bootable) {
117 DCHECK(slot < num_slots_);
118 is_bootable_[slot] = bootable;
119 }
120
Amin Hassanie53b39b2020-09-16 11:19:28 -0700121 DynamicPartitionControlInterface* GetDynamicPartitionControl() override {
Yifan Hongdaac7322019-11-07 10:48:26 -0800122 return dynamic_partition_control_.get();
123 }
124
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500125 std::optional<PartitionDevice> GetPartitionDevice(
126 const std::string& partition_name,
127 uint32_t slot,
128 uint32_t current_slot,
129 bool not_in_payload = false) const override {
130 if (slot >= devices_.size()) {
131 return {};
132 }
133 auto device_path = devices_[slot].find(partition_name);
134 if (device_path == devices_[slot].end()) {
135 return {};
136 }
137 PartitionDevice device;
138 device.is_dynamic = false;
139 device.rw_device_path = device_path->second;
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400140 device.readonly_device_path = device.rw_device_path;
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500141 return device;
142 }
143
Alex Deymo763e7db2015-08-27 21:08:08 -0700144 private:
145 BootControlInterface::Slot num_slots_{2};
146 BootControlInterface::Slot current_slot_{0};
147
148 std::vector<bool> is_bootable_;
Yifan Hongf1415942020-02-24 18:34:49 -0800149 std::vector<bool> is_marked_successful_;
Alex Deymo763e7db2015-08-27 21:08:08 -0700150 std::vector<std::map<std::string, std::string>> devices_;
151
Yifan Hongdaac7322019-11-07 10:48:26 -0800152 std::unique_ptr<DynamicPartitionControlInterface> dynamic_partition_control_;
153
Alex Deymo763e7db2015-08-27 21:08:08 -0700154 DISALLOW_COPY_AND_ASSIGN(FakeBootControl);
155};
156
157} // namespace chromeos_update_engine
158
Alex Deymo39910dc2015-11-09 17:04:30 -0800159#endif // UPDATE_ENGINE_COMMON_FAKE_BOOT_CONTROL_H_