| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2012 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 | // | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 16 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/cros/real_system_state.h" | 
| Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 18 |  | 
| Ben Chan | ab5a0af | 2017-10-12 14:57:50 -0700 | [diff] [blame] | 19 | #include <memory> | 
| Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 20 | #include <string> | 
| Amin Hassani | 0882a51 | 2018-04-05 16:25:44 -0700 | [diff] [blame] | 21 | #include <utility> | 
| Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 22 |  | 
| Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 23 | #include <base/bind.h> | 
| Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 24 | #include <base/files/file_util.h> | 
| Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 25 | #include <base/location.h> | 
| Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 26 | #include <base/time/time.h> | 
| Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 27 | #include <brillo/message_loops/message_loop.h> | 
| Daniel Erat | e5f6f25 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 28 | #include <chromeos/dbus/service_constants.h> | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 29 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include "update_engine/common/boot_control.h" | 
|  | 31 | #include "update_engine/common/boot_control_stub.h" | 
|  | 32 | #include "update_engine/common/constants.h" | 
| Jae Hoon Kim | 50f2673 | 2020-05-14 10:46:15 -0700 | [diff] [blame] | 33 | #include "update_engine/common/dlcservice_interface.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 34 | #include "update_engine/common/hardware.h" | 
|  | 35 | #include "update_engine/common/utils.h" | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 36 | #include "update_engine/cros/metrics_reporter_omaha.h" | 
| Daniel Erat | e5f6f25 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 37 | #if USE_DBUS | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 38 | #include "update_engine/cros/dbus_connection.h" | 
| Daniel Erat | e5f6f25 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 39 | #endif  // USE_DBUS | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 40 | #include "update_engine/update_manager/state_factory.h" | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | namespace chromeos_update_engine { | 
|  | 43 |  | 
| Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 44 | bool RealSystemState::Initialize() { | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 45 | boot_control_ = boot_control::CreateBootControl(); | 
| David Zeuthen | 6eddf26 | 2015-10-16 15:23:53 -0400 | [diff] [blame] | 46 | if (!boot_control_) { | 
|  | 47 | LOG(WARNING) << "Unable to create BootControl instance, using stub " | 
|  | 48 | << "instead. All update attempts will fail."; | 
| Ben Chan | ab5a0af | 2017-10-12 14:57:50 -0700 | [diff] [blame] | 49 | boot_control_ = std::make_unique<BootControlStub>(); | 
| David Zeuthen | 6eddf26 | 2015-10-16 15:23:53 -0400 | [diff] [blame] | 50 | } | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 51 |  | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 52 | hardware_ = hardware::CreateHardware(); | 
|  | 53 | if (!hardware_) { | 
| Sen Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 54 | LOG(ERROR) << "Error initializing the HardwareInterface."; | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 55 | return false; | 
|  | 56 | } | 
|  | 57 |  | 
| Daniel Erat | 04df23a | 2018-03-29 17:55:35 -0700 | [diff] [blame] | 58 | kiosk_app_proxy_.reset(new org::chromium::KioskAppServiceInterfaceProxy( | 
|  | 59 | DBusConnection::Get()->GetDBus(), chromeos::kKioskAppServiceName)); | 
| Daniel Erat | e5f6f25 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 60 |  | 
| Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 61 | LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode."; | 
|  | 62 | LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build."; | 
|  | 63 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 64 | connection_manager_ = connection_manager::CreateConnectionManager(); | 
| Sen Jiang | f5bebae | 2016-06-03 15:36:54 -0700 | [diff] [blame] | 65 | if (!connection_manager_) { | 
| Sen Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 66 | LOG(ERROR) << "Error initializing the ConnectionManagerInterface."; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 67 | return false; | 
|  | 68 | } | 
|  | 69 |  | 
| Sen Jiang | b8c6a8f | 2016-06-07 17:33:17 -0700 | [diff] [blame] | 70 | power_manager_ = power_manager::CreatePowerManager(); | 
|  | 71 | if (!power_manager_) { | 
| Sen Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 72 | LOG(ERROR) << "Error initializing the PowerManagerInterface."; | 
| Sen Jiang | b8c6a8f | 2016-06-07 17:33:17 -0700 | [diff] [blame] | 73 | return false; | 
|  | 74 | } | 
|  | 75 |  | 
| Xiaochu Liu | 8ba486f | 2018-11-06 11:14:10 -0800 | [diff] [blame] | 76 | dlcservice_ = CreateDlcService(); | 
|  | 77 | if (!dlcservice_) { | 
|  | 78 | LOG(ERROR) << "Error initializing the DlcServiceInterface."; | 
|  | 79 | return false; | 
|  | 80 | } | 
|  | 81 |  | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 82 | // Initialize standard and powerwash-safe prefs. | 
|  | 83 | base::FilePath non_volatile_path; | 
|  | 84 | // TODO(deymo): Fall back to in-memory prefs if there's no physical directory | 
|  | 85 | // available. | 
|  | 86 | if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) { | 
|  | 87 | LOG(ERROR) << "Failed to get a non-volatile directory."; | 
|  | 88 | return false; | 
|  | 89 | } | 
|  | 90 | Prefs* prefs; | 
|  | 91 | prefs_.reset(prefs = new Prefs()); | 
|  | 92 | if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) { | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 93 | LOG(ERROR) << "Failed to initialize preferences."; | 
|  | 94 | return false; | 
|  | 95 | } | 
|  | 96 |  | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 97 | base::FilePath powerwash_safe_path; | 
|  | 98 | if (!hardware_->GetPowerwashSafeDirectory(&powerwash_safe_path)) { | 
|  | 99 | // TODO(deymo): Fall-back to in-memory prefs if there's no powerwash-safe | 
|  | 100 | // directory, or disable powerwash feature. | 
|  | 101 | powerwash_safe_path = non_volatile_path.Append("powerwash-safe"); | 
|  | 102 | LOG(WARNING) << "No powerwash-safe directory, using non-volatile one."; | 
|  | 103 | } | 
|  | 104 | powerwash_safe_prefs_.reset(prefs = new Prefs()); | 
|  | 105 | if (!prefs->Init( | 
|  | 106 | powerwash_safe_path.Append(kPowerwashSafePrefsSubDirectory))) { | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 107 | LOG(ERROR) << "Failed to initialize powerwash preferences."; | 
|  | 108 | return false; | 
|  | 109 | } | 
|  | 110 |  | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 111 | // Check the system rebooted marker file. | 
|  | 112 | std::string boot_id; | 
|  | 113 | if (utils::GetBootId(&boot_id)) { | 
|  | 114 | std::string prev_boot_id; | 
|  | 115 | system_rebooted_ = (!prefs_->GetString(kPrefsBootId, &prev_boot_id) || | 
|  | 116 | prev_boot_id != boot_id); | 
|  | 117 | prefs_->SetString(kPrefsBootId, boot_id); | 
|  | 118 | } else { | 
|  | 119 | LOG(WARNING) << "Couldn't detect the bootid, assuming system was rebooted."; | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 120 | system_rebooted_ = true; | 
|  | 121 | } | 
|  | 122 |  | 
| Alex Deymo | 5378f5e | 2015-11-10 15:02:50 -0800 | [diff] [blame] | 123 | // Initialize the OmahaRequestParams with the default settings. These settings | 
|  | 124 | // will be re-initialized before every request using the actual request | 
|  | 125 | // options. This initialization here pre-loads current channel and version, so | 
|  | 126 | // the DBus service can access it. | 
| Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 127 | if (!request_params_.Init("", "", {})) { | 
| Alex Deymo | 5378f5e | 2015-11-10 15:02:50 -0800 | [diff] [blame] | 128 | LOG(WARNING) << "Ignoring OmahaRequestParams initialization error. Some " | 
|  | 129 | "features might not work properly."; | 
|  | 130 | } | 
|  | 131 |  | 
| Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 132 | certificate_checker_.reset( | 
|  | 133 | new CertificateChecker(prefs_.get(), &openssl_wrapper_)); | 
|  | 134 | certificate_checker_->Init(); | 
|  | 135 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 136 | update_attempter_.reset(new UpdateAttempter(certificate_checker_.get())); | 
| Sen Jiang | c92195c | 2016-06-13 15:48:44 -0700 | [diff] [blame] | 137 |  | 
| Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 138 | // Initialize the UpdateAttempter before the UpdateManager. | 
| Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 139 | update_attempter_->Init(); | 
|  | 140 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 141 | // Initialize the Update Manager using the default state factory. | 
|  | 142 | chromeos_update_manager::State* um_state = | 
| Amin Hassani | e94ece5 | 2017-08-01 15:03:08 -0700 | [diff] [blame] | 143 | chromeos_update_manager::DefaultStateFactory(&policy_provider_, | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 144 | kiosk_app_proxy_.get()); | 
| Amin Hassani | e94ece5 | 2017-08-01 15:03:08 -0700 | [diff] [blame] | 145 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 146 | if (!um_state) { | 
|  | 147 | LOG(ERROR) << "Failed to initialize the Update Manager."; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 148 | return false; | 
| Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 149 | } | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 150 | update_manager_.reset(new chromeos_update_manager::UpdateManager( | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 151 | base::TimeDelta::FromSeconds(5), | 
|  | 152 | base::TimeDelta::FromHours(12), | 
|  | 153 | um_state)); | 
| Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 154 |  | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 155 | // The P2P Manager depends on the Update Manager for its initialization. | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 156 | p2p_manager_.reset( | 
|  | 157 | P2PManager::Construct(nullptr, | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 158 | update_manager_.get(), | 
|  | 159 | "cros_au", | 
|  | 160 | kMaxP2PFilesToKeep, | 
|  | 161 | base::TimeDelta::FromDays(kMaxP2PFileAgeDays))); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 162 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 163 | if (!payload_state_.Initialize()) { | 
| Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 164 | LOG(ERROR) << "Failed to initialize the payload state object."; | 
|  | 165 | return false; | 
|  | 166 | } | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 167 |  | 
| Amin Hassani | 000c762 | 2019-05-01 07:02:27 -0700 | [diff] [blame] | 168 | // For images that are build for debugging purposes like test images | 
|  | 169 | // initialize max kernel key version to 0xfffffffe, which is logical infinity. | 
|  | 170 | if (!hardware_->IsOfficialBuild()) { | 
| Amin Hassani | ef13c92 | 2019-01-10 17:30:38 -0800 | [diff] [blame] | 171 | if (!hardware()->SetMaxKernelKeyRollforward( | 
|  | 172 | chromeos_update_manager::kRollforwardInfinity)) { | 
|  | 173 | LOG(ERROR) << "Failed to set kernel_max_rollforward to infinity for" | 
| Amin Hassani | 000c762 | 2019-05-01 07:02:27 -0700 | [diff] [blame] | 174 | << " device with test/dev image."; | 
| Amin Hassani | ef13c92 | 2019-01-10 17:30:38 -0800 | [diff] [blame] | 175 | } | 
|  | 176 | } | 
|  | 177 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 178 | // All is well. Initialization successful. | 
|  | 179 | return true; | 
|  | 180 | } | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 181 |  | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 182 | }  // namespace chromeos_update_engine |