blob: 2aae9f05fb4208851f95ad59f6f0adae20e7444a [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 Deymo1b03f9f2015-12-09 00:38:36 -080017#include "update_engine/hardware_chromeos.h"
Alex Deymo42432912013-07-12 20:21:15 -070018
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -080019#include <utility>
20
Alex Deymo46a9aae2016-05-04 20:20:11 -070021#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070022#include <base/files/file_util.h>
Alex Deymo42432912013-07-12 20:21:15 -070023#include <base/logging.h>
Alex Deymoebbe7ef2014-10-30 13:02:49 -070024#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070025#include <base/strings/string_util.h>
Alex Deymo46a9aae2016-05-04 20:20:11 -070026#include <brillo/key_value_store.h>
Sen Jiange67bb5b2016-06-20 15:53:56 -070027#include <debugd/dbus-constants.h>
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070028#include <vboot/crossystem.h>
Alex Deymo42432912013-07-12 20:21:15 -070029
Don Garrett83692e42013-11-08 10:11:30 -080030extern "C" {
31#include "vboot/vboot_host.h"
32}
33
Alex Deymo46a9aae2016-05-04 20:20:11 -070034#include "update_engine/common/constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/hardware.h"
36#include "update_engine/common/hwid_override.h"
Sen Jiang9c123462015-11-19 13:16:23 -080037#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080038#include "update_engine/common/subprocess.h"
39#include "update_engine/common/utils.h"
Sen Jiange67bb5b2016-06-20 15:53:56 -070040#include "update_engine/dbus_connection.h"
Matt Ziegelbaum91ba9be2020-06-10 16:56:40 -040041#if USE_CFM
42#include "update_engine/requisition_util.h"
43#endif
J. Richard Barnette522d36f2013-10-28 17:22:12 -070044
Alex Deymo42432912013-07-12 20:21:15 -070045using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070046using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070047
Alex Deymobccbc382014-04-03 13:38:55 -070048namespace {
49
Alex Deymodd132f32015-09-14 19:12:07 -070050const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
51
52// The stateful directory used by update_engine to store powerwash-safe files.
Andrewcc6ab9f2020-06-25 07:41:40 -070053// The files stored here must be added to the powerwash script allowlist.
Alex Deymodd132f32015-09-14 19:12:07 -070054const char kPowerwashSafeDirectory[] =
55 "/mnt/stateful_partition/unencrypted/preserve";
Alex Deymobccbc382014-04-03 13:38:55 -070056
Alex Deymoebbe7ef2014-10-30 13:02:49 -070057// The powerwash_count marker file contains the number of times the device was
58// powerwashed. This value is incremented by the clobber-state script when
59// a powerwash is performed.
Alex Deymodd132f32015-09-14 19:12:07 -070060const char kPowerwashCountMarker[] = "powerwash_count";
61
Alex Deymofb905d92016-06-03 19:26:58 -070062// The name of the marker file used to trigger powerwash when post-install
63// completes successfully so that the device is powerwashed on next reboot.
64const char kPowerwashMarkerFile[] =
65 "/mnt/stateful_partition/factory_install_reset";
66
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -080067// The name of the marker file used to trigger a save of rollback data
68// during the next shutdown.
69const char kRollbackSaveMarkerFile[] =
70 "/mnt/stateful_partition/.save_rollback_data";
71
Zentaro Kavanagh0418de32019-01-15 10:29:35 -080072// The contents of the powerwash marker file for the non-rollback case.
Alex Deymofb905d92016-06-03 19:26:58 -070073const char kPowerwashCommand[] = "safe fast keepimg reason=update_engine\n";
74
Zentaro Kavanagh0418de32019-01-15 10:29:35 -080075// The contents of the powerwas marker file for the rollback case.
76const char kRollbackPowerwashCommand[] =
77 "safe fast keepimg rollback reason=update_engine\n";
78
Alex Deymo46a9aae2016-05-04 20:20:11 -070079// UpdateManager config path.
80const char* kConfigFilePath = "/etc/update_manager.conf";
81
82// UpdateManager config options:
83const char* kConfigOptsIsOOBEEnabled = "is_oobe_enabled";
84
Amin Hassani1677e812017-06-21 13:36:36 -070085const char* kActivePingKey = "first_active_omaha_ping_sent";
86
Alex Deymobccbc382014-04-03 13:38:55 -070087} // namespace
88
Alex Deymo42432912013-07-12 20:21:15 -070089namespace chromeos_update_engine {
90
Alex Deymo40d86b22015-09-03 22:27:10 -070091namespace hardware {
92
93// Factory defined in hardware.h.
94std::unique_ptr<HardwareInterface> CreateHardware() {
Alex Deymo46a9aae2016-05-04 20:20:11 -070095 std::unique_ptr<HardwareChromeOS> hardware(new HardwareChromeOS());
96 hardware->Init();
97 return std::move(hardware);
Alex Deymo40d86b22015-09-03 22:27:10 -070098}
99
100} // namespace hardware
101
Alex Deymo46a9aae2016-05-04 20:20:11 -0700102void HardwareChromeOS::Init() {
103 LoadConfig("" /* root_prefix */, IsNormalBootMode());
Sen Jiange67bb5b2016-06-20 15:53:56 -0700104 debugd_proxy_.reset(
105 new org::chromium::debugdProxy(DBusConnection::Get()->GetDBus()));
Alex Deymo46a9aae2016-05-04 20:20:11 -0700106}
107
Alex Deymo40d86b22015-09-03 22:27:10 -0700108bool HardwareChromeOS::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700109 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700110}
111
Alex Deymo40d86b22015-09-03 22:27:10 -0700112bool HardwareChromeOS::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700113 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700114 return !dev_mode;
115}
116
Sen Jiange67bb5b2016-06-20 15:53:56 -0700117bool HardwareChromeOS::AreDevFeaturesEnabled() const {
118 // Even though the debugd tools are also gated on devmode, checking here can
119 // save us a D-Bus call so it's worth doing explicitly.
120 if (IsNormalBootMode())
121 return false;
122
123 int32_t dev_features = debugd::DEV_FEATURES_DISABLED;
124 brillo::ErrorPtr error;
125 // Some boards may not include debugd so it's expected that this may fail,
126 // in which case we treat it as disabled.
127 if (debugd_proxy_ && debugd_proxy_->QueryDevFeatures(&dev_features, &error) &&
128 !(dev_features & debugd::DEV_FEATURES_DISABLED)) {
129 LOG(INFO) << "Debugd dev tools enabled.";
130 return true;
131 }
132 return false;
133}
134
Alex Deymo46a9aae2016-05-04 20:20:11 -0700135bool HardwareChromeOS::IsOOBEEnabled() const {
136 return is_oobe_enabled_;
137}
138
Alex Deymo40d86b22015-09-03 22:27:10 -0700139bool HardwareChromeOS::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700140 if (!is_oobe_enabled_) {
141 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() was called";
142 }
Alex Deymobccbc382014-04-03 13:38:55 -0700143 struct stat statbuf;
144 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
145 if (errno != ENOENT) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800146 PLOG(ERROR) << "Error getting information about " << kOOBECompletedMarker;
Alex Deymobccbc382014-04-03 13:38:55 -0700147 }
148 return false;
149 }
150
151 if (out_time_of_oobe != nullptr)
152 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
153 return true;
154}
155
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700156static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700157 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700158
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800159 const char* rv = VbGetSystemPropertyString(
160 key.c_str(), value_buffer, sizeof(value_buffer));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700161 if (rv != nullptr) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700162 string return_value(value_buffer);
Ben Chan736fcb52014-05-21 18:28:22 -0700163 base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700164 return return_value;
165 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700166
167 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700168 return "";
169}
170
Alex Deymo40d86b22015-09-03 22:27:10 -0700171string HardwareChromeOS::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000172 if (USE_HWID_OVERRIDE) {
173 return HwidOverride::Read(base::FilePath("/"));
174 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700175 return ReadValueFromCrosSystem("hwid");
176}
177
Alex Deymo40d86b22015-09-03 22:27:10 -0700178string HardwareChromeOS::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700179 return ReadValueFromCrosSystem("fwid");
180}
181
Alex Deymo40d86b22015-09-03 22:27:10 -0700182string HardwareChromeOS::GetECVersion() const {
Amin Hassani3a4caa12019-11-06 11:12:28 -0800183 string input_line, error;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700184 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800185 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700186
Amin Hassani3a4caa12019-11-06 11:12:28 -0800187 if (!Subprocess::SynchronousExec(cmd, &exit_code, &input_line, &error) ||
188 exit_code != 0) {
189 LOG(ERROR) << "Unable to read EC info from mosys with exit code: "
190 << exit_code << " and error: " << error;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700191 return "";
192 }
193
194 return utils::ParseECVersion(input_line);
195}
196
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -0400197string HardwareChromeOS::GetDeviceRequisition() const {
Matt Ziegelbaum7cfadf72020-06-09 18:51:16 -0400198#if USE_CFM
Matt Ziegelbaum91ba9be2020-06-10 16:56:40 -0400199 const char* kLocalStatePath = "/home/chronos/Local State";
200 return ReadDeviceRequisition(base::FilePath(kLocalStatePath));
201#else
202 return "";
Matt Ziegelbaum7cfadf72020-06-09 18:51:16 -0400203#endif
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -0400204}
205
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800206int HardwareChromeOS::GetMinKernelKeyVersion() const {
207 return VbGetSystemPropertyInt("tpm_kernver");
208}
209
Zentaro Kavanagh8f6f2432018-05-16 13:48:12 -0700210int HardwareChromeOS::GetMaxFirmwareKeyRollforward() const {
211 return VbGetSystemPropertyInt("firmware_max_rollforward");
212}
213
214bool HardwareChromeOS::SetMaxFirmwareKeyRollforward(
215 int firmware_max_rollforward) {
216 // Not all devices have this field yet. So first try to read
217 // it and if there is an error just fail.
218 if (GetMaxFirmwareKeyRollforward() == -1)
219 return false;
220
221 return VbSetSystemPropertyInt("firmware_max_rollforward",
222 firmware_max_rollforward) == 0;
223}
224
Marton Hunyady99ced782018-05-08 12:59:50 +0200225int HardwareChromeOS::GetMinFirmwareKeyVersion() const {
226 return VbGetSystemPropertyInt("tpm_fwver");
227}
228
Zentaro Kavanagh5d956152018-05-15 09:40:33 -0700229bool HardwareChromeOS::SetMaxKernelKeyRollforward(int kernel_max_rollforward) {
230 return VbSetSystemPropertyInt("kernel_max_rollforward",
231 kernel_max_rollforward) == 0;
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800232}
233
Alex Deymo40d86b22015-09-03 22:27:10 -0700234int HardwareChromeOS::GetPowerwashCount() const {
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700235 int powerwash_count;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800236 base::FilePath marker_path =
237 base::FilePath(kPowerwashSafeDirectory).Append(kPowerwashCountMarker);
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700238 string contents;
Alex Deymodd132f32015-09-14 19:12:07 -0700239 if (!utils::ReadFile(marker_path.value(), &contents))
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700240 return -1;
241 base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents);
242 if (!base::StringToInt(contents, &powerwash_count))
243 return -1;
244 return powerwash_count;
245}
246
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800247bool HardwareChromeOS::SchedulePowerwash(bool save_rollback_data) {
248 if (save_rollback_data) {
249 if (!utils::WriteFile(kRollbackSaveMarkerFile, nullptr, 0)) {
250 PLOG(ERROR) << "Error in creating rollback save marker file: "
251 << kRollbackSaveMarkerFile << ". Rollback will not"
252 << " preserve any data.";
253 } else {
254 LOG(INFO) << "Rollback data save has been scheduled on next shutdown.";
255 }
256 }
257
Zentaro Kavanagh0418de32019-01-15 10:29:35 -0800258 const char* powerwash_command =
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800259 save_rollback_data ? kRollbackPowerwashCommand : kPowerwashCommand;
Alex Deymofb905d92016-06-03 19:26:58 -0700260 bool result = utils::WriteFile(
Zentaro Kavanagh0418de32019-01-15 10:29:35 -0800261 kPowerwashMarkerFile, powerwash_command, strlen(powerwash_command));
Alex Deymofb905d92016-06-03 19:26:58 -0700262 if (result) {
Alex Deymocaa46722016-06-09 12:08:29 -0700263 LOG(INFO) << "Created " << kPowerwashMarkerFile
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800264 << " to powerwash on next reboot ("
265 << "save_rollback_data=" << save_rollback_data << ")";
Alex Deymofb905d92016-06-03 19:26:58 -0700266 } else {
Alex Deymocaa46722016-06-09 12:08:29 -0700267 PLOG(ERROR) << "Error in creating powerwash marker file: "
268 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700269 }
270
271 return result;
272}
273
274bool HardwareChromeOS::CancelPowerwash() {
275 bool result = base::DeleteFile(base::FilePath(kPowerwashMarkerFile), false);
276
277 if (result) {
278 LOG(INFO) << "Successfully deleted the powerwash marker file : "
Alex Deymocaa46722016-06-09 12:08:29 -0700279 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700280 } else {
281 PLOG(ERROR) << "Could not delete the powerwash marker file : "
Alex Deymocaa46722016-06-09 12:08:29 -0700282 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700283 }
284
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800285 // Delete the rollback save marker file if it existed.
286 if (!base::DeleteFile(base::FilePath(kRollbackSaveMarkerFile), false)) {
287 PLOG(ERROR) << "Could not remove rollback save marker";
288 }
289
Alex Deymofb905d92016-06-03 19:26:58 -0700290 return result;
291}
292
Alex Deymodd132f32015-09-14 19:12:07 -0700293bool HardwareChromeOS::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800294 *path = base::FilePath(constants::kNonVolatileDirectory);
Alex Deymodd132f32015-09-14 19:12:07 -0700295 return true;
296}
297
298bool HardwareChromeOS::GetPowerwashSafeDirectory(base::FilePath* path) const {
299 *path = base::FilePath(kPowerwashSafeDirectory);
300 return true;
301}
302
Sen Jiang5011df62017-06-28 17:13:19 -0700303int64_t HardwareChromeOS::GetBuildTimestamp() const {
304 // TODO(senj): implement this in Chrome OS.
305 return 0;
306}
307
Alex Deymo46a9aae2016-05-04 20:20:11 -0700308void HardwareChromeOS::LoadConfig(const string& root_prefix, bool normal_mode) {
309 brillo::KeyValueStore store;
310
311 if (normal_mode) {
312 store.Load(base::FilePath(root_prefix + kConfigFilePath));
313 } else {
314 if (store.Load(base::FilePath(root_prefix + kStatefulPartition +
315 kConfigFilePath))) {
316 LOG(INFO) << "UpdateManager Config loaded from stateful partition.";
317 } else {
318 store.Load(base::FilePath(root_prefix + kConfigFilePath));
319 }
320 }
321
322 if (!store.GetBoolean(kConfigOptsIsOOBEEnabled, &is_oobe_enabled_))
323 is_oobe_enabled_ = true; // Default value.
324}
325
Amin Hassani1677e812017-06-21 13:36:36 -0700326bool HardwareChromeOS::GetFirstActiveOmahaPingSent() const {
Amin Hassani1677e812017-06-21 13:36:36 -0700327 string active_ping_str;
Matt Ziegelbaum91ba9be2020-06-10 16:56:40 -0400328 if (!utils::GetVpdValue(kActivePingKey, &active_ping_str)) {
Amin Hassani1677e812017-06-21 13:36:36 -0700329 return false;
330 }
331
Amin Hassani1677e812017-06-21 13:36:36 -0700332 int active_ping;
333 if (active_ping_str.empty() ||
334 !base::StringToInt(active_ping_str, &active_ping)) {
335 LOG(INFO) << "Failed to parse active_ping value: " << active_ping_str;
336 return false;
337 }
338 return static_cast<bool>(active_ping);
339}
340
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700341bool HardwareChromeOS::SetFirstActiveOmahaPingSent() {
Amin Hassani1677e812017-06-21 13:36:36 -0700342 int exit_code = 0;
Amin Hassani3a4caa12019-11-06 11:12:28 -0800343 string output, error;
Amin Hassani1677e812017-06-21 13:36:36 -0700344 vector<string> vpd_set_cmd = {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800345 "vpd", "-i", "RW_VPD", "-s", string(kActivePingKey) + "=1"};
Amin Hassani3a4caa12019-11-06 11:12:28 -0800346 if (!Subprocess::SynchronousExec(vpd_set_cmd, &exit_code, &output, &error) ||
Amin Hassani1677e812017-06-21 13:36:36 -0700347 exit_code) {
348 LOG(ERROR) << "Failed to set vpd key for " << kActivePingKey
Amin Hassani3a4caa12019-11-06 11:12:28 -0800349 << " with exit code: " << exit_code << " with output: " << output
350 << " and error: " << error;
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700351 return false;
Amin Hassani3a4caa12019-11-06 11:12:28 -0800352 } else if (!error.empty()) {
353 LOG(INFO) << "vpd succeeded but with error logs: " << error;
Amin Hassani1677e812017-06-21 13:36:36 -0700354 }
355
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800356 vector<string> vpd_dump_cmd = {"dump_vpd_log", "--force"};
Amin Hassani3a4caa12019-11-06 11:12:28 -0800357 if (!Subprocess::SynchronousExec(vpd_dump_cmd, &exit_code, &output, &error) ||
Amin Hassani1677e812017-06-21 13:36:36 -0700358 exit_code) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800359 LOG(ERROR) << "Failed to cache " << kActivePingKey << " using dump_vpd_log"
Amin Hassani3a4caa12019-11-06 11:12:28 -0800360 << " with exit code: " << exit_code << " with output: " << output
361 << " and error: " << error;
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700362 return false;
Amin Hassani3a4caa12019-11-06 11:12:28 -0800363 } else if (!error.empty()) {
364 LOG(INFO) << "dump_vpd_log succeeded but with error logs: " << error;
Amin Hassani1677e812017-06-21 13:36:36 -0700365 }
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700366 return true;
Amin Hassani1677e812017-06-21 13:36:36 -0700367}
368
Alex Deymo42432912013-07-12 20:21:15 -0700369} // namespace chromeos_update_engine