blob: d9d08181f0e090680905770b63837caefe960860 [file] [log] [blame]
Jay Srinivasan43488792012-06-19 00:25:31 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <base/file_util.h>
6
Jay Srinivasan55f50c22013-01-10 19:24:35 -08007#include "update_engine/real_system_state.h"
Chris Sosabe45bef2013-04-09 18:25:12 -07008#include "update_engine/utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -07009
10namespace chromeos_update_engine {
11
12static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
13
Jay Srinivasan34b5d862012-07-23 11:43:22 -070014RealSystemState::RealSystemState()
15 : device_policy_(NULL),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070016 connection_manager_(this),
Chris Sosabe45bef2013-04-09 18:25:12 -070017 request_params_(this),
18 system_rebooted_(false) {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080019
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080020bool RealSystemState::Initialize(bool enable_gpio) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080021 metrics_lib_.Init();
22
23 if (!prefs_.Init(FilePath(kPrefsDirectory))) {
24 LOG(ERROR) << "Failed to initialize preferences.";
25 return false;
26 }
27
Chris Sosaaa18e162013-06-20 13:20:30 -070028 if (!powerwash_safe_prefs_.Init(FilePath(kPowerwashSafePrefsDir))) {
29 LOG(ERROR) << "Failed to initialize powerwash preferences.";
30 return false;
31 }
32
Chris Sosabe45bef2013-04-09 18:25:12 -070033 if (!utils::FileExists(kSystemRebootedMarkerFile)) {
34 if (!utils::WriteFile(kSystemRebootedMarkerFile, "", 0)) {
35 LOG(ERROR) << "Could not create reboot marker file";
36 return false;
37 }
38 system_rebooted_ = true;
39 }
40
Jay Srinivasan19409b72013-04-12 19:23:36 -070041 if (!payload_state_.Initialize(this))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080042 return false;
43
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080044 // Initialize the GPIO handler as instructed.
45 if (enable_gpio) {
46 // A real GPIO handler. Defer GPIO discovery to ensure the udev has ample
47 // time to export the devices. Also require that test mode is physically
48 // queried at most once and the result cached, for a more consistent update
49 // behavior.
50 udev_iface_.reset(new StandardUdevInterface());
51 file_descriptor_.reset(new EintrSafeFileDescriptor());
52 gpio_handler_.reset(new StandardGpioHandler(udev_iface_.get(),
53 file_descriptor_.get(),
54 true, true));
55 } else {
56 // A no-op GPIO handler, always indicating a non-test mode.
57 gpio_handler_.reset(new NoopGpioHandler(false));
58 }
59
Jay Srinivasan55f50c22013-01-10 19:24:35 -080060 // Create the update attempter.
61 update_attempter_.reset(new UpdateAttempter(this, &dbus_));
62
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080063 // All is well. Initialization successful.
64 return true;
65}
Jay Srinivasan43488792012-06-19 00:25:31 -070066
67bool RealSystemState::IsOOBEComplete() {
68 return file_util::PathExists(FilePath(kOOBECompletedMarker));
69}
70
Jay Srinivasan53173b92013-05-17 17:13:01 -070071bool RealSystemState::IsOfficialBuild() {
72 return utils::IsOfficialBuild();
73}
74
Jay Srinivasan43488792012-06-19 00:25:31 -070075} // namespace chromeos_update_engine