blob: 82382ffd8126fb45e5341f976b834bc0c9f00033 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2013 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//
Alex Deymo42432912013-07-12 20:21:15 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_COMMON_FAKE_HARDWARE_H_
18#define UPDATE_ENGINE_COMMON_FAKE_HARDWARE_H_
Alex Deymo42432912013-07-12 20:21:15 -070019
Don Garrett83692e42013-11-08 10:11:30 -080020#include <map>
Alex Deymodf632d12014-04-29 20:04:36 -070021#include <string>
Kelvin Zhangd7191032020-08-11 10:48:16 -040022#include <utility>
Don Garrett83692e42013-11-08 10:11:30 -080023
Alex Deymobccbc382014-04-03 13:38:55 -070024#include <base/time/time.h>
25
Yifan Hong87029332020-09-01 17:20:08 -070026#include "update_engine/common/error_code.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/hardware_interface.h"
Kelvin Zhangd7191032020-08-11 10:48:16 -040028#include "update_engine/common/utils.h"
Alex Deymo42432912013-07-12 20:21:15 -070029
Alex Deymo42432912013-07-12 20:21:15 -070030namespace chromeos_update_engine {
31
32// Implements a fake hardware interface used for testing.
33class FakeHardware : public HardwareInterface {
34 public:
Alex Deymoebbe7ef2014-10-30 13:02:49 -070035 // Value used to signal that the powerwash_count file is not present. When
36 // this value is used in SetPowerwashCount(), GetPowerwashCount() will return
37 // false.
38 static const int kPowerwashCountNotSet = -1;
39
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -080040 // Default value for crossystem tpm_kernver.
41 static const int kMinKernelKeyVersion = 3;
42
Marton Hunyady99ced782018-05-08 12:59:50 +020043 // Default value for crossystem tpm_fwver.
44 static const int kMinFirmwareKeyVersion = 13;
45
Zentaro Kavanagh5d956152018-05-15 09:40:33 -070046 // Default value for crossystem kernel_max_rollforward. This value is the
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -080047 // default for consumer devices and effectively means "unlimited rollforward
48 // is allowed", which is the same as the behavior prior to implementing
49 // roll forward prevention.
Zentaro Kavanagh8f6f2432018-05-16 13:48:12 -070050 static const int kKernelMaxRollforward = 0xfffffffe;
51
52 // Default value for crossystem firmware_max_rollforward. This value is the
53 // default for consumer devices and effectively means "unlimited rollforward
54 // is allowed", which is the same as the behavior prior to implementing
55 // roll forward prevention.
56 static const int kFirmwareMaxRollforward = 0xfffffffe;
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -080057
Alex Deymo46a9aae2016-05-04 20:20:11 -070058 FakeHardware() = default;
Alex Deymo42432912013-07-12 20:21:15 -070059
60 // HardwareInterface methods.
Alex Vakulenko157fe302014-08-11 15:59:58 -070061 bool IsOfficialBuild() const override { return is_official_build_; }
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080062
Alex Vakulenko157fe302014-08-11 15:59:58 -070063 bool IsNormalBootMode() const override { return is_normal_boot_mode_; }
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080064
Sen Jiange67bb5b2016-06-20 15:53:56 -070065 bool AreDevFeaturesEnabled() const override {
66 return are_dev_features_enabled_;
67 }
68
Alex Deymo46a9aae2016-05-04 20:20:11 -070069 bool IsOOBEEnabled() const override { return is_oobe_enabled_; }
70
Alex Vakulenko157fe302014-08-11 15:59:58 -070071 bool IsOOBEComplete(base::Time* out_time_of_oobe) const override {
Alex Deymobccbc382014-04-03 13:38:55 -070072 if (out_time_of_oobe != nullptr)
73 *out_time_of_oobe = oobe_timestamp_;
74 return is_oobe_complete_;
75 }
76
Alex Vakulenko157fe302014-08-11 15:59:58 -070077 std::string GetHardwareClass() const override { return hardware_class_; }
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080078
Alex Vakulenko157fe302014-08-11 15:59:58 -070079 std::string GetFirmwareVersion() const override { return firmware_version_; }
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080080
Alex Vakulenko157fe302014-08-11 15:59:58 -070081 std::string GetECVersion() const override { return ec_version_; }
Alex Deymo42432912013-07-12 20:21:15 -070082
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -040083 std::string GetDeviceRequisition() const override {
84 return device_requisition_;
85 }
86
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -080087 int GetMinKernelKeyVersion() const override {
88 return min_kernel_key_version_;
89 }
90
Marton Hunyady99ced782018-05-08 12:59:50 +020091 int GetMinFirmwareKeyVersion() const override {
92 return min_firmware_key_version_;
93 }
94
Zentaro Kavanagh8f6f2432018-05-16 13:48:12 -070095 int GetMaxFirmwareKeyRollforward() const override {
96 return firmware_max_rollforward_;
97 }
98
99 bool SetMaxFirmwareKeyRollforward(int firmware_max_rollforward) override {
100 if (GetMaxFirmwareKeyRollforward() == -1)
101 return false;
102
103 firmware_max_rollforward_ = firmware_max_rollforward;
104 return true;
105 }
106
Zentaro Kavanagh5d956152018-05-15 09:40:33 -0700107 bool SetMaxKernelKeyRollforward(int kernel_max_rollforward) override {
108 kernel_max_rollforward_ = kernel_max_rollforward;
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800109 return true;
110 }
111
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700112 int GetPowerwashCount() const override { return powerwash_count_; }
113
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800114 bool SchedulePowerwash(bool save_rollback_data) override {
Alex Deymofb905d92016-06-03 19:26:58 -0700115 powerwash_scheduled_ = true;
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800116 save_rollback_data_ = save_rollback_data;
Alex Deymofb905d92016-06-03 19:26:58 -0700117 return true;
118 }
119
120 bool CancelPowerwash() override {
121 powerwash_scheduled_ = false;
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800122 save_rollback_data_ = false;
Alex Deymofb905d92016-06-03 19:26:58 -0700123 return true;
124 }
125
126 bool IsPowerwashScheduled() { return powerwash_scheduled_; }
127
Alex Deymodd132f32015-09-14 19:12:07 -0700128 bool GetNonVolatileDirectory(base::FilePath* path) const override {
129 return false;
130 }
131
132 bool GetPowerwashSafeDirectory(base::FilePath* path) const override {
133 return false;
134 }
135
Sen Jiang5011df62017-06-28 17:13:19 -0700136 int64_t GetBuildTimestamp() const override { return build_timestamp_; }
137
Tianjie Xu4ad3af62019-10-30 11:59:45 -0700138 bool AllowDowngrade() const override { return false; }
139
Amin Hassani1677e812017-06-21 13:36:36 -0700140 bool GetFirstActiveOmahaPingSent() const override {
141 return first_active_omaha_ping_sent_;
142 }
143
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700144 bool SetFirstActiveOmahaPingSent() override {
Amin Hassani1677e812017-06-21 13:36:36 -0700145 first_active_omaha_ping_sent_ = true;
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700146 return true;
Amin Hassani1677e812017-06-21 13:36:36 -0700147 }
148
Alex Deymo42432912013-07-12 20:21:15 -0700149 // Setters
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700150 void SetIsOfficialBuild(bool is_official_build) {
151 is_official_build_ = is_official_build;
152 }
153
154 void SetIsNormalBootMode(bool is_normal_boot_mode) {
155 is_normal_boot_mode_ = is_normal_boot_mode;
156 }
157
Sen Jiange67bb5b2016-06-20 15:53:56 -0700158 void SetAreDevFeaturesEnabled(bool are_dev_features_enabled) {
159 are_dev_features_enabled_ = are_dev_features_enabled;
160 }
161
Alex Deymo46a9aae2016-05-04 20:20:11 -0700162 // Sets the SetIsOOBEEnabled to |is_oobe_enabled|.
163 void SetIsOOBEEnabled(bool is_oobe_enabled) {
164 is_oobe_enabled_ = is_oobe_enabled;
165 }
166
Alex Deymobccbc382014-04-03 13:38:55 -0700167 // Sets the IsOOBEComplete to True with the given timestamp.
168 void SetIsOOBEComplete(base::Time oobe_timestamp) {
169 is_oobe_complete_ = true;
170 oobe_timestamp_ = oobe_timestamp;
171 }
172
Amin Hassanib2689592019-01-13 17:04:28 -0800173 void UnsetIsOOBEComplete() { is_oobe_complete_ = false; }
Alex Deymobccbc382014-04-03 13:38:55 -0700174
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -0700175 void SetHardwareClass(const std::string& hardware_class) {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700176 hardware_class_ = hardware_class;
177 }
178
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -0700179 void SetFirmwareVersion(const std::string& firmware_version) {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700180 firmware_version_ = firmware_version;
181 }
182
Amin Hassanib2689592019-01-13 17:04:28 -0800183 void SetECVersion(const std::string& ec_version) { ec_version_ = ec_version; }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700184
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -0400185 void SetDeviceRequisition(const std::string& requisition) {
186 device_requisition_ = requisition;
187 }
188
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800189 void SetMinKernelKeyVersion(int min_kernel_key_version) {
190 min_kernel_key_version_ = min_kernel_key_version;
191 }
192
Marton Hunyady99ced782018-05-08 12:59:50 +0200193 void SetMinFirmwareKeyVersion(int min_firmware_key_version) {
194 min_firmware_key_version_ = min_firmware_key_version;
195 }
196
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700197 void SetPowerwashCount(int powerwash_count) {
198 powerwash_count_ = powerwash_count;
199 }
200
Sen Jiang5011df62017-06-28 17:13:19 -0700201 void SetBuildTimestamp(int64_t build_timestamp) {
202 build_timestamp_ = build_timestamp;
203 }
204
Amin Hassani99563202020-09-15 15:30:09 -0700205 void SetWarmReset(bool warm_reset) override { warm_reset_ = warm_reset; }
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800206
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800207 // Getters to verify state.
Zentaro Kavanagh5d956152018-05-15 09:40:33 -0700208 int GetMaxKernelKeyRollforward() const { return kernel_max_rollforward_; }
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800209
Zentaro Kavanagh0418de32019-01-15 10:29:35 -0800210 bool GetIsRollbackPowerwashScheduled() const {
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800211 return powerwash_scheduled_ && save_rollback_data_;
Zentaro Kavanagh0418de32019-01-15 10:29:35 -0800212 }
Kelvin Zhangd7191032020-08-11 10:48:16 -0400213 std::string GetVersionForLogging(
214 const std::string& partition_name) const override {
215 return partition_timestamps_[partition_name];
216 }
217 void SetVersion(const std::string& partition_name, std::string timestamp) {
218 partition_timestamps_[partition_name] = std::move(timestamp);
219 }
Yifan Hong87029332020-09-01 17:20:08 -0700220 ErrorCode IsPartitionUpdateValid(
221 const std::string& partition_name,
222 const std::string& new_version) const override {
Kelvin Zhangd7191032020-08-11 10:48:16 -0400223 const auto old_version = GetVersionForLogging(partition_name);
224 return utils::IsTimestampNewer(old_version, new_version);
225 }
Zentaro Kavanagh0418de32019-01-15 10:29:35 -0800226
Alex Deymo42432912013-07-12 20:21:15 -0700227 private:
Alex Deymo46a9aae2016-05-04 20:20:11 -0700228 bool is_official_build_{true};
229 bool is_normal_boot_mode_{true};
Sen Jiange67bb5b2016-06-20 15:53:56 -0700230 bool are_dev_features_enabled_{false};
Alex Deymo46a9aae2016-05-04 20:20:11 -0700231 bool is_oobe_enabled_{true};
232 bool is_oobe_complete_{true};
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800233 // Jan 20, 2007
234 base::Time oobe_timestamp_{base::Time::FromTimeT(1169280000)};
Alex Deymo46a9aae2016-05-04 20:20:11 -0700235 std::string hardware_class_{"Fake HWID BLAH-1234"};
236 std::string firmware_version_{"Fake Firmware v1.0.1"};
237 std::string ec_version_{"Fake EC v1.0a"};
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -0400238 std::string device_requisition_{"fake_requisition"};
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800239 int min_kernel_key_version_{kMinKernelKeyVersion};
Marton Hunyady99ced782018-05-08 12:59:50 +0200240 int min_firmware_key_version_{kMinFirmwareKeyVersion};
Zentaro Kavanagh8f6f2432018-05-16 13:48:12 -0700241 int kernel_max_rollforward_{kKernelMaxRollforward};
242 int firmware_max_rollforward_{kFirmwareMaxRollforward};
Alex Deymo46a9aae2016-05-04 20:20:11 -0700243 int powerwash_count_{kPowerwashCountNotSet};
Alex Deymofb905d92016-06-03 19:26:58 -0700244 bool powerwash_scheduled_{false};
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800245 bool save_rollback_data_{false};
Sen Jiang5011df62017-06-28 17:13:19 -0700246 int64_t build_timestamp_{0};
Amin Hassani1677e812017-06-21 13:36:36 -0700247 bool first_active_omaha_ping_sent_{false};
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800248 bool warm_reset_{false};
Kelvin Zhangd7191032020-08-11 10:48:16 -0400249 mutable std::map<std::string, std::string> partition_timestamps_;
Alex Deymo42432912013-07-12 20:21:15 -0700250
251 DISALLOW_COPY_AND_ASSIGN(FakeHardware);
252};
253
254} // namespace chromeos_update_engine
255
Alex Deymo39910dc2015-11-09 17:04:30 -0800256#endif // UPDATE_ENGINE_COMMON_FAKE_HARDWARE_H_