blob: bd9d9ca861e699fb204bd3fe2f4eb37c3c98044d [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,
51 std::string* device) const override {
52 if (slot >= num_slots_)
53 return false;
54 auto part_it = devices_[slot].find(partition_name);
55 if (part_it == devices_[slot].end())
56 return false;
57 *device = part_it->second;
58 return true;
59 }
60
61 bool IsSlotBootable(BootControlInterface::Slot slot) const override {
62 return slot < num_slots_ && is_bootable_[slot];
63 }
64
65 bool MarkSlotUnbootable(BootControlInterface::Slot slot) override {
66 if (slot >= num_slots_)
67 return false;
68 is_bootable_[slot] = false;
69 return true;
70 }
71
Alex Deymo31d95ac2015-09-17 11:56:18 -070072 bool SetActiveBootSlot(Slot slot) override { return true; }
73
Alex Deymoaa26f622015-09-16 18:21:27 -070074 bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) override {
75 // We run the callback directly from here to avoid having to setup a message
76 // loop in the test environment.
Yifan Hongf1415942020-02-24 18:34:49 -080077 is_marked_successful_[GetCurrentSlot()] = true;
Alex Deymoaa26f622015-09-16 18:21:27 -070078 callback.Run(true);
79 return true;
80 }
81
Yifan Hongf1415942020-02-24 18:34:49 -080082 bool IsSlotMarkedSuccessful(Slot slot) const override {
83 return slot < num_slots_ && is_marked_successful_[slot];
84 }
85
Alex Deymo763e7db2015-08-27 21:08:08 -070086 // Setters
87 void SetNumSlots(unsigned int num_slots) {
88 num_slots_ = num_slots;
89 is_bootable_.resize(num_slots_, false);
Yifan Hongf1415942020-02-24 18:34:49 -080090 is_marked_successful_.resize(num_slots_, false);
Alex Deymo763e7db2015-08-27 21:08:08 -070091 devices_.resize(num_slots_);
92 }
93
Amin Hassanib2689592019-01-13 17:04:28 -080094 void SetCurrentSlot(BootControlInterface::Slot slot) { current_slot_ = slot; }
Alex Deymo763e7db2015-08-27 21:08:08 -070095
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -070096 void SetPartitionDevice(const std::string& partition_name,
Alex Deymo763e7db2015-08-27 21:08:08 -070097 BootControlInterface::Slot slot,
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -070098 const std::string& device) {
Alex Deymo763e7db2015-08-27 21:08:08 -070099 DCHECK(slot < num_slots_);
100 devices_[slot][partition_name] = device;
101 }
102
103 void SetSlotBootable(BootControlInterface::Slot slot, bool bootable) {
104 DCHECK(slot < num_slots_);
105 is_bootable_[slot] = bootable;
106 }
107
Yifan Hongdaac7322019-11-07 10:48:26 -0800108 DynamicPartitionControlInterface* GetDynamicPartitionControl() {
109 return dynamic_partition_control_.get();
110 }
111
Alex Deymo763e7db2015-08-27 21:08:08 -0700112 private:
113 BootControlInterface::Slot num_slots_{2};
114 BootControlInterface::Slot current_slot_{0};
115
116 std::vector<bool> is_bootable_;
Yifan Hongf1415942020-02-24 18:34:49 -0800117 std::vector<bool> is_marked_successful_;
Alex Deymo763e7db2015-08-27 21:08:08 -0700118 std::vector<std::map<std::string, std::string>> devices_;
119
Yifan Hongdaac7322019-11-07 10:48:26 -0800120 std::unique_ptr<DynamicPartitionControlInterface> dynamic_partition_control_;
121
Alex Deymo763e7db2015-08-27 21:08:08 -0700122 DISALLOW_COPY_AND_ASSIGN(FakeBootControl);
123};
124
125} // namespace chromeos_update_engine
126
Alex Deymo39910dc2015-11-09 17:04:30 -0800127#endif // UPDATE_ENGINE_COMMON_FAKE_BOOT_CONTROL_H_