| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 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 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_COMMON_FAKE_BOOT_CONTROL_H_ | 
 | 18 | #define UPDATE_ENGINE_COMMON_FAKE_BOOT_CONTROL_H_ | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 19 |  | 
 | 20 | #include <map> | 
| Yifan Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame] | 21 | #include <memory> | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 22 | #include <string> | 
 | 23 | #include <vector> | 
 | 24 |  | 
 | 25 | #include <base/time/time.h> | 
 | 26 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/common/boot_control_interface.h" | 
| Yifan Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame] | 28 | #include "update_engine/common/dynamic_partition_control_stub.h" | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 29 |  | 
 | 30 | namespace chromeos_update_engine { | 
 | 31 |  | 
 | 32 | // Implements a fake bootloader control interface used for testing. | 
 | 33 | class 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 Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame] | 39 |  | 
 | 40 |     dynamic_partition_control_.reset(new DynamicPartitionControlStub()); | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 41 |   } | 
 | 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, | 
| Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 51 |                           bool not_in_payload, | 
 | 52 |                           std::string* device, | 
 | 53 |                           bool* is_dynamic) const override { | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 54 |     if (slot >= num_slots_) | 
 | 55 |       return false; | 
 | 56 |     auto part_it = devices_[slot].find(partition_name); | 
 | 57 |     if (part_it == devices_[slot].end()) | 
 | 58 |       return false; | 
 | 59 |     *device = part_it->second; | 
| Kelvin Zhang | 3b323cf | 2020-08-05 10:56:02 -0400 | [diff] [blame] | 60 |     if (is_dynamic != nullptr) { | 
 | 61 |       *is_dynamic = false; | 
 | 62 |     } | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 63 |     return true; | 
 | 64 |   } | 
 | 65 |  | 
| Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 66 |   bool GetPartitionDevice(const std::string& partition_name, | 
 | 67 |                           BootControlInterface::Slot slot, | 
 | 68 |                           std::string* device) const override { | 
 | 69 |     return GetPartitionDevice(partition_name, slot, false, device, nullptr); | 
 | 70 |   } | 
 | 71 |  | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 72 |   bool IsSlotBootable(BootControlInterface::Slot slot) const override { | 
 | 73 |     return slot < num_slots_ && is_bootable_[slot]; | 
 | 74 |   } | 
 | 75 |  | 
 | 76 |   bool MarkSlotUnbootable(BootControlInterface::Slot slot) override { | 
 | 77 |     if (slot >= num_slots_) | 
 | 78 |       return false; | 
 | 79 |     is_bootable_[slot] = false; | 
 | 80 |     return true; | 
 | 81 |   } | 
 | 82 |  | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 83 |   bool SetActiveBootSlot(Slot slot) override { return true; } | 
 | 84 |  | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 85 |   bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) override { | 
 | 86 |     // We run the callback directly from here to avoid having to setup a message | 
 | 87 |     // loop in the test environment. | 
| Yifan Hong | f141594 | 2020-02-24 18:34:49 -0800 | [diff] [blame] | 88 |     is_marked_successful_[GetCurrentSlot()] = true; | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 89 |     callback.Run(true); | 
 | 90 |     return true; | 
 | 91 |   } | 
 | 92 |  | 
| Yifan Hong | f141594 | 2020-02-24 18:34:49 -0800 | [diff] [blame] | 93 |   bool IsSlotMarkedSuccessful(Slot slot) const override { | 
 | 94 |     return slot < num_slots_ && is_marked_successful_[slot]; | 
 | 95 |   } | 
 | 96 |  | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 97 |   // Setters | 
 | 98 |   void SetNumSlots(unsigned int num_slots) { | 
 | 99 |     num_slots_ = num_slots; | 
 | 100 |     is_bootable_.resize(num_slots_, false); | 
| Yifan Hong | f141594 | 2020-02-24 18:34:49 -0800 | [diff] [blame] | 101 |     is_marked_successful_.resize(num_slots_, false); | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 102 |     devices_.resize(num_slots_); | 
 | 103 |   } | 
 | 104 |  | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 105 |   void SetCurrentSlot(BootControlInterface::Slot slot) { current_slot_ = slot; } | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 106 |  | 
| Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 107 |   void SetPartitionDevice(const std::string& partition_name, | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 108 |                           BootControlInterface::Slot slot, | 
| Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 109 |                           const std::string& device) { | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 110 |     DCHECK(slot < num_slots_); | 
 | 111 |     devices_[slot][partition_name] = device; | 
 | 112 |   } | 
 | 113 |  | 
 | 114 |   void SetSlotBootable(BootControlInterface::Slot slot, bool bootable) { | 
 | 115 |     DCHECK(slot < num_slots_); | 
 | 116 |     is_bootable_[slot] = bootable; | 
 | 117 |   } | 
 | 118 |  | 
| Yifan Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame] | 119 |   DynamicPartitionControlInterface* GetDynamicPartitionControl() { | 
 | 120 |     return dynamic_partition_control_.get(); | 
 | 121 |   } | 
 | 122 |  | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 123 |  private: | 
 | 124 |   BootControlInterface::Slot num_slots_{2}; | 
 | 125 |   BootControlInterface::Slot current_slot_{0}; | 
 | 126 |  | 
 | 127 |   std::vector<bool> is_bootable_; | 
| Yifan Hong | f141594 | 2020-02-24 18:34:49 -0800 | [diff] [blame] | 128 |   std::vector<bool> is_marked_successful_; | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 129 |   std::vector<std::map<std::string, std::string>> devices_; | 
 | 130 |  | 
| Yifan Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame] | 131 |   std::unique_ptr<DynamicPartitionControlInterface> dynamic_partition_control_; | 
 | 132 |  | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 133 |   DISALLOW_COPY_AND_ASSIGN(FakeBootControl); | 
 | 134 | }; | 
 | 135 |  | 
 | 136 | }  // namespace chromeos_update_engine | 
 | 137 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 138 | #endif  // UPDATE_ENGINE_COMMON_FAKE_BOOT_CONTROL_H_ |