blob: 5715a39db5ce6e9fda48c0fdcd486b7bfc663dd2 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Srinivasan43488792012-06-19 00:25:31 -070016
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/cros/real_system_state.h"
Alex Deymo94c06162014-03-21 20:34:46 -070018
Ben Chanab5a0af2017-10-12 14:57:50 -070019#include <memory>
Alex Deymof7ead812015-10-23 17:37:27 -070020#include <string>
Amin Hassani0882a512018-04-05 16:25:44 -070021#include <utility>
Alex Deymof7ead812015-10-23 17:37:27 -070022
Alex Deymoe97b39c2016-01-20 13:22:17 -080023#include <base/bind.h>
Ben Chan06c76a42014-09-05 08:21:06 -070024#include <base/files/file_util.h>
Alex Deymoe97b39c2016-01-20 13:22:17 -080025#include <base/location.h>
Gilad Arnoldb2271992014-06-19 12:35:24 -070026#include <base/time/time.h>
Alex Deymoe97b39c2016-01-20 13:22:17 -080027#include <brillo/message_loops/message_loop.h>
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070028#if USE_CHROME_KIOSK_APP
Daniel Erate5f6f252017-04-20 12:09:58 -060029#include <chromeos/dbus/service_constants.h>
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070030#endif // USE_CHROME_KIOSK_APP
Jay Srinivasan43488792012-06-19 00:25:31 -070031
Alex Deymo39910dc2015-11-09 17:04:30 -080032#include "update_engine/common/boot_control.h"
33#include "update_engine/common/boot_control_stub.h"
34#include "update_engine/common/constants.h"
Jae Hoon Kim50f26732020-05-14 10:46:15 -070035#include "update_engine/common/dlcservice_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/hardware.h"
37#include "update_engine/common/utils.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070038#include "update_engine/cros/metrics_reporter_omaha.h"
Daniel Erate5f6f252017-04-20 12:09:58 -060039#if USE_DBUS
Amin Hassaniec7bc112020-10-29 16:47:58 -070040#include "update_engine/cros/dbus_connection.h"
Daniel Erate5f6f252017-04-20 12:09:58 -060041#endif // USE_DBUS
Alex Deymo63784a52014-05-28 10:46:14 -070042#include "update_engine/update_manager/state_factory.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070043
44namespace chromeos_update_engine {
45
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -070046bool RealSystemState::Initialize() {
Alex Deymob17327c2015-09-04 10:29:00 -070047 boot_control_ = boot_control::CreateBootControl();
David Zeuthen6eddf262015-10-16 15:23:53 -040048 if (!boot_control_) {
49 LOG(WARNING) << "Unable to create BootControl instance, using stub "
50 << "instead. All update attempts will fail.";
Ben Chanab5a0af2017-10-12 14:57:50 -070051 boot_control_ = std::make_unique<BootControlStub>();
David Zeuthen6eddf262015-10-16 15:23:53 -040052 }
Alex Deymo763e7db2015-08-27 21:08:08 -070053
Alex Deymo40d86b22015-09-03 22:27:10 -070054 hardware_ = hardware::CreateHardware();
55 if (!hardware_) {
Sen Jiang771f6482018-04-04 17:59:10 -070056 LOG(ERROR) << "Error initializing the HardwareInterface.";
Alex Deymo40d86b22015-09-03 22:27:10 -070057 return false;
58 }
59
Amin Hassanie94ece52017-08-01 15:03:08 -070060#if USE_CHROME_KIOSK_APP
Daniel Erat04df23a2018-03-29 17:55:35 -070061 kiosk_app_proxy_.reset(new org::chromium::KioskAppServiceInterfaceProxy(
62 DBusConnection::Get()->GetDBus(), chromeos::kKioskAppServiceName));
Amin Hassanie94ece52017-08-01 15:03:08 -070063#endif // USE_CHROME_KIOSK_APP
Daniel Erate5f6f252017-04-20 12:09:58 -060064
Alex Deymo1c4e84a2015-09-22 16:58:10 -070065 LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
66 LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
67
Sen Jiangf5bebae2016-06-03 15:36:54 -070068 connection_manager_ = connection_manager::CreateConnectionManager(this);
69 if (!connection_manager_) {
Sen Jiang771f6482018-04-04 17:59:10 -070070 LOG(ERROR) << "Error initializing the ConnectionManagerInterface.";
Alex Deymo30534502015-07-20 15:06:33 -070071 return false;
72 }
73
Sen Jiangb8c6a8f2016-06-07 17:33:17 -070074 power_manager_ = power_manager::CreatePowerManager();
75 if (!power_manager_) {
Sen Jiang771f6482018-04-04 17:59:10 -070076 LOG(ERROR) << "Error initializing the PowerManagerInterface.";
Sen Jiangb8c6a8f2016-06-07 17:33:17 -070077 return false;
78 }
79
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080080 dlcservice_ = CreateDlcService();
81 if (!dlcservice_) {
82 LOG(ERROR) << "Error initializing the DlcServiceInterface.";
83 return false;
84 }
85
Alex Deymodd132f32015-09-14 19:12:07 -070086 // Initialize standard and powerwash-safe prefs.
87 base::FilePath non_volatile_path;
88 // TODO(deymo): Fall back to in-memory prefs if there's no physical directory
89 // available.
90 if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
91 LOG(ERROR) << "Failed to get a non-volatile directory.";
92 return false;
93 }
94 Prefs* prefs;
95 prefs_.reset(prefs = new Prefs());
96 if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080097 LOG(ERROR) << "Failed to initialize preferences.";
98 return false;
99 }
100
Alex Deymodd132f32015-09-14 19:12:07 -0700101 base::FilePath powerwash_safe_path;
102 if (!hardware_->GetPowerwashSafeDirectory(&powerwash_safe_path)) {
103 // TODO(deymo): Fall-back to in-memory prefs if there's no powerwash-safe
104 // directory, or disable powerwash feature.
105 powerwash_safe_path = non_volatile_path.Append("powerwash-safe");
106 LOG(WARNING) << "No powerwash-safe directory, using non-volatile one.";
107 }
108 powerwash_safe_prefs_.reset(prefs = new Prefs());
109 if (!prefs->Init(
110 powerwash_safe_path.Append(kPowerwashSafePrefsSubDirectory))) {
Chris Sosaaa18e162013-06-20 13:20:30 -0700111 LOG(ERROR) << "Failed to initialize powerwash preferences.";
112 return false;
113 }
114
Alex Deymodd132f32015-09-14 19:12:07 -0700115 // Check the system rebooted marker file.
116 std::string boot_id;
117 if (utils::GetBootId(&boot_id)) {
118 std::string prev_boot_id;
119 system_rebooted_ = (!prefs_->GetString(kPrefsBootId, &prev_boot_id) ||
120 prev_boot_id != boot_id);
121 prefs_->SetString(kPrefsBootId, boot_id);
122 } else {
123 LOG(WARNING) << "Couldn't detect the bootid, assuming system was rebooted.";
Chris Sosabe45bef2013-04-09 18:25:12 -0700124 system_rebooted_ = true;
125 }
126
Alex Deymo5378f5e2015-11-10 15:02:50 -0800127 // Initialize the OmahaRequestParams with the default settings. These settings
128 // will be re-initialized before every request using the actual request
129 // options. This initialization here pre-loads current channel and version, so
130 // the DBus service can access it.
Amin Hassanie8153632020-10-27 15:11:28 -0700131 if (!request_params_.Init("", "", {})) {
Alex Deymo5378f5e2015-11-10 15:02:50 -0800132 LOG(WARNING) << "Ignoring OmahaRequestParams initialization error. Some "
133 "features might not work properly.";
134 }
135
Alex Deymo33e91e72015-12-01 18:26:08 -0300136 certificate_checker_.reset(
137 new CertificateChecker(prefs_.get(), &openssl_wrapper_));
138 certificate_checker_->Init();
139
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800140 update_attempter_.reset(
141 new UpdateAttempter(this, certificate_checker_.get()));
Sen Jiangc92195c2016-06-13 15:48:44 -0700142
Alex Deymo33e91e72015-12-01 18:26:08 -0300143 // Initialize the UpdateAttempter before the UpdateManager.
Alex Deymo33e91e72015-12-01 18:26:08 -0300144 update_attempter_->Init();
145
Alex Deymo63784a52014-05-28 10:46:14 -0700146 // Initialize the Update Manager using the default state factory.
147 chromeos_update_manager::State* um_state =
Amin Hassanie94ece52017-08-01 15:03:08 -0700148 chromeos_update_manager::DefaultStateFactory(&policy_provider_,
149#if USE_CHROME_KIOSK_APP
Daniel Erat04df23a2018-03-29 17:55:35 -0700150 kiosk_app_proxy_.get(),
Amin Hassanie94ece52017-08-01 15:03:08 -0700151#else
152 nullptr,
153#endif // USE_CHROME_KIOSK_APP
154 this);
155
Alex Deymo63784a52014-05-28 10:46:14 -0700156 if (!um_state) {
157 LOG(ERROR) << "Failed to initialize the Update Manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800158 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -0700159 }
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800160 update_manager_.reset(new chromeos_update_manager::UpdateManager(
161 &clock_,
162 base::TimeDelta::FromSeconds(5),
163 base::TimeDelta::FromHours(12),
164 um_state));
Gilad Arnold1f847232014-04-07 12:07:49 -0700165
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700166 // The P2P Manager depends on the Update Manager for its initialization.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800167 p2p_manager_.reset(
168 P2PManager::Construct(nullptr,
169 &clock_,
170 update_manager_.get(),
171 "cros_au",
172 kMaxP2PFilesToKeep,
173 base::TimeDelta::FromDays(kMaxP2PFileAgeDays)));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700174
Gilad Arnold1f847232014-04-07 12:07:49 -0700175 if (!payload_state_.Initialize(this)) {
176 LOG(ERROR) << "Failed to initialize the payload state object.";
177 return false;
178 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800179
Amin Hassani000c7622019-05-01 07:02:27 -0700180 // For images that are build for debugging purposes like test images
181 // initialize max kernel key version to 0xfffffffe, which is logical infinity.
182 if (!hardware_->IsOfficialBuild()) {
Amin Hassanief13c922019-01-10 17:30:38 -0800183 if (!hardware()->SetMaxKernelKeyRollforward(
184 chromeos_update_manager::kRollforwardInfinity)) {
185 LOG(ERROR) << "Failed to set kernel_max_rollforward to infinity for"
Amin Hassani000c7622019-05-01 07:02:27 -0700186 << " device with test/dev image.";
Amin Hassanief13c922019-01-10 17:30:38 -0800187 }
188 }
189
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800190 // All is well. Initialization successful.
191 return true;
192}
Jay Srinivasan43488792012-06-19 00:25:31 -0700193
Jay Srinivasan43488792012-06-19 00:25:31 -0700194} // namespace chromeos_update_engine