blob: 8efb9030eaca732f04461f8ec7f39b4af1277590 [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
Alex Deymo94c06162014-03-21 20:34:46 -070017#include "update_engine/real_system_state.h"
18
Ben Chan06c76a42014-09-05 08:21:06 -070019#include <base/files/file_util.h>
Gilad Arnoldb2271992014-06-19 12:35:24 -070020#include <base/time/time.h>
David Zeuthen6eddf262015-10-16 15:23:53 -040021#include <brillo/make_unique_ptr.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070022
Alex Deymob17327c2015-09-04 10:29:00 -070023#include "update_engine/boot_control.h"
David Zeuthen6eddf262015-10-16 15:23:53 -040024#include "update_engine/boot_control_stub.h"
David Zeuthen639aa362014-02-03 16:23:44 -080025#include "update_engine/constants.h"
Alex Deymo40d86b22015-09-03 22:27:10 -070026#include "update_engine/hardware.h"
Alex Deymo63784a52014-05-28 10:46:14 -070027#include "update_engine/update_manager/state_factory.h"
Chris Sosabe45bef2013-04-09 18:25:12 -070028#include "update_engine/utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070029
30namespace chromeos_update_engine {
31
Alex Deymo30534502015-07-20 15:06:33 -070032RealSystemState::RealSystemState(const scoped_refptr<dbus::Bus>& bus)
Alex Deymod6deb1d2015-08-28 15:54:37 -070033 : debugd_proxy_(bus),
34 power_manager_proxy_(bus),
35 session_manager_proxy_(bus),
Alex Deymo30534502015-07-20 15:06:33 -070036 shill_proxy_(bus),
37 libcros_proxy_(bus) {
38}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080039
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -070040bool RealSystemState::Initialize() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080041 metrics_lib_.Init();
42
Alex Deymob17327c2015-09-04 10:29:00 -070043 boot_control_ = boot_control::CreateBootControl();
David Zeuthen6eddf262015-10-16 15:23:53 -040044 if (!boot_control_) {
45 LOG(WARNING) << "Unable to create BootControl instance, using stub "
46 << "instead. All update attempts will fail.";
47 boot_control_ = brillo::make_unique_ptr(new BootControlStub());
48 }
Alex Deymo763e7db2015-08-27 21:08:08 -070049
Alex Deymo40d86b22015-09-03 22:27:10 -070050 hardware_ = hardware::CreateHardware();
51 if (!hardware_) {
52 LOG(ERROR) << "Error intializing the HardwareInterface.";
53 return false;
54 }
55
Alex Deymo1c4e84a2015-09-22 16:58:10 -070056 LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
57 LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
58
Alex Deymo30534502015-07-20 15:06:33 -070059 if (!shill_proxy_.Init()) {
60 LOG(ERROR) << "Failed to initialize shill proxy.";
61 return false;
62 }
63
Alex Deymodd132f32015-09-14 19:12:07 -070064 // Initialize standard and powerwash-safe prefs.
65 base::FilePath non_volatile_path;
66 // TODO(deymo): Fall back to in-memory prefs if there's no physical directory
67 // available.
68 if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
69 LOG(ERROR) << "Failed to get a non-volatile directory.";
70 return false;
71 }
72 Prefs* prefs;
73 prefs_.reset(prefs = new Prefs());
74 if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080075 LOG(ERROR) << "Failed to initialize preferences.";
76 return false;
77 }
78
Alex Deymodd132f32015-09-14 19:12:07 -070079 base::FilePath powerwash_safe_path;
80 if (!hardware_->GetPowerwashSafeDirectory(&powerwash_safe_path)) {
81 // TODO(deymo): Fall-back to in-memory prefs if there's no powerwash-safe
82 // directory, or disable powerwash feature.
83 powerwash_safe_path = non_volatile_path.Append("powerwash-safe");
84 LOG(WARNING) << "No powerwash-safe directory, using non-volatile one.";
85 }
86 powerwash_safe_prefs_.reset(prefs = new Prefs());
87 if (!prefs->Init(
88 powerwash_safe_path.Append(kPowerwashSafePrefsSubDirectory))) {
Chris Sosaaa18e162013-06-20 13:20:30 -070089 LOG(ERROR) << "Failed to initialize powerwash preferences.";
90 return false;
91 }
92
Alex Deymodd132f32015-09-14 19:12:07 -070093 // Check the system rebooted marker file.
94 std::string boot_id;
95 if (utils::GetBootId(&boot_id)) {
96 std::string prev_boot_id;
97 system_rebooted_ = (!prefs_->GetString(kPrefsBootId, &prev_boot_id) ||
98 prev_boot_id != boot_id);
99 prefs_->SetString(kPrefsBootId, boot_id);
100 } else {
101 LOG(WARNING) << "Couldn't detect the bootid, assuming system was rebooted.";
Chris Sosabe45bef2013-04-09 18:25:12 -0700102 system_rebooted_ = true;
103 }
104
Alex Deymo5378f5e2015-11-10 15:02:50 -0800105 // Initialize the OmahaRequestParams with the default settings. These settings
106 // will be re-initialized before every request using the actual request
107 // options. This initialization here pre-loads current channel and version, so
108 // the DBus service can access it.
109 if (!request_params_.Init("", "", false)) {
110 LOG(WARNING) << "Ignoring OmahaRequestParams initialization error. Some "
111 "features might not work properly.";
112 }
113
Alex Deymo63784a52014-05-28 10:46:14 -0700114 // Initialize the Update Manager using the default state factory.
115 chromeos_update_manager::State* um_state =
116 chromeos_update_manager::DefaultStateFactory(
Alex Deymo30534502015-07-20 15:06:33 -0700117 &policy_provider_, &shill_proxy_, &session_manager_proxy_, this);
Alex Deymo63784a52014-05-28 10:46:14 -0700118 if (!um_state) {
119 LOG(ERROR) << "Failed to initialize the Update Manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800120 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -0700121 }
Alex Deymo63784a52014-05-28 10:46:14 -0700122 update_manager_.reset(
Gilad Arnoldb2271992014-06-19 12:35:24 -0700123 new chromeos_update_manager::UpdateManager(
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700124 &clock_, base::TimeDelta::FromSeconds(5),
125 base::TimeDelta::FromHours(12), um_state));
Gilad Arnold1f847232014-04-07 12:07:49 -0700126
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700127 // The P2P Manager depends on the Update Manager for its initialization.
128 p2p_manager_.reset(P2PManager::Construct(
129 nullptr, &clock_, update_manager_.get(), "cros_au",
130 kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays)));
131
Gilad Arnold1f847232014-04-07 12:07:49 -0700132 if (!payload_state_.Initialize(this)) {
133 LOG(ERROR) << "Failed to initialize the payload state object.";
134 return false;
135 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800136
Gilad Arnold1f847232014-04-07 12:07:49 -0700137 // Initialize the update attempter.
138 update_attempter_.Init();
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800139
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800140 // All is well. Initialization successful.
141 return true;
142}
Jay Srinivasan43488792012-06-19 00:25:31 -0700143
Jay Srinivasan43488792012-06-19 00:25:31 -0700144} // namespace chromeos_update_engine