blob: 621373993dc0b3335d7816ab7bcfd90a24c3d769 [file] [log] [blame]
Alex Deymo42432912013-07-12 20:21:15 -07001// Copyright (c) 2013 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 "update_engine/hardware.h"
6
J. Richard Barnette056b0ab2013-10-29 15:24:56 -07007#include <base/file_util.h>
Alex Deymo42432912013-07-12 20:21:15 -07008#include <base/logging.h>
J. Richard Barnette522d36f2013-10-28 17:22:12 -07009#include <base/string_util.h>
Alex Deymo42432912013-07-12 20:21:15 -070010#include <rootdev/rootdev.h>
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070011#include <vboot/crossystem.h>
Alex Deymo42432912013-07-12 20:21:15 -070012
Don Garrett83692e42013-11-08 10:11:30 -080013extern "C" {
14#include "vboot/vboot_host.h"
15}
16
17// We don't use these variables, but libcgpt needs them defined to link.
18// TODO(dgarrett) chromium:318536
19const char* progname = "";
20const char* command = "";
21void (*uuid_generator)(uint8_t* buffer) = NULL;
22
J. Richard Barnette522d36f2013-10-28 17:22:12 -070023#include "update_engine/subprocess.h"
24#include "update_engine/utils.h"
25
Alex Deymo42432912013-07-12 20:21:15 -070026using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070027using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070028
29namespace chromeos_update_engine {
30
Don Garrett83692e42013-11-08 10:11:30 -080031const string Hardware::BootKernelDevice() {
32 return utils::KernelDeviceOfBootDevice(Hardware::BootDevice());
33}
34
Alex Deymo42432912013-07-12 20:21:15 -070035const string Hardware::BootDevice() {
36 char boot_path[PATH_MAX];
37 // Resolve the boot device path fully, including dereferencing
38 // through dm-verity.
39 int ret = rootdev(boot_path, sizeof(boot_path), true, false);
40
41 if (ret < 0) {
42 LOG(ERROR) << "rootdev failed to find the root device";
43 return "";
44 }
45 LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node";
46
47 // This local variable is used to construct the return string and is not
48 // passed around after use.
49 return boot_path;
50}
51
Don Garrett83692e42013-11-08 10:11:30 -080052bool Hardware::IsKernelBootable(const std::string& kernel_device,
53 bool* bootable) {
Don Garrett83692e42013-11-08 10:11:30 -080054 CgptAddParams params;
55 memset(&params, '\0', sizeof(params));
56
57 string root_dev = utils::RootDevice(kernel_device);
58 string partition_number_str = utils::PartitionNumber(kernel_device);
59 uint32_t partition_number = atoi(partition_number_str.c_str());
60
61 params.drive_name = const_cast<char *>(root_dev.c_str());
62 params.partition = partition_number;
63
64 int retval = CgptGetPartitionDetails(&params);
65 if (retval != CGPT_OK)
66 return false;
67
68 *bootable = params.successful || (params.tries > 0);
69 return true;
70}
71
72bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) {
73 LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device;
74
Don Garrett6646b442013-11-13 15:29:11 -080075 if (kernel_device == BootKernelDevice()) {
76 LOG(ERROR) << "Refusing to mark current kernel as unbootable.";
77 return false;
78 }
79
Don Garrett83692e42013-11-08 10:11:30 -080080 string root_dev = utils::RootDevice(kernel_device);
81 string partition_number_str = utils::PartitionNumber(kernel_device);
82 uint32_t partition_number = atoi(partition_number_str.c_str());
83
84 CgptAddParams params;
85 memset(&params, 0, sizeof(params));
86
87 params.drive_name = const_cast<char *>(root_dev.c_str());
88 params.partition = partition_number;
89
90 params.successful = false;
91 params.set_successful = true;
92
93 params.tries = 0;
94 params.set_tries = true;
95
96 int retval = CgptSetAttributes(&params);
97 if (retval != CGPT_OK) {
98 LOG(ERROR) << "Marking kernel unbootable failed.";
99 return false;
100 }
101
102 return true;
103}
104
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700105bool Hardware::IsOfficialBuild() {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700106 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700107}
108
109bool Hardware::IsNormalBootMode() {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700110 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700111 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
112 return !dev_mode;
113}
114
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700115static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700116 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700117
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700118 const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
119 sizeof(value_buffer));
120 if (rv != NULL) {
121 string return_value(value_buffer);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700122 TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value);
123 return return_value;
124 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700125
126 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700127 return "";
128}
129
130string Hardware::GetHardwareClass() {
131 return ReadValueFromCrosSystem("hwid");
132}
133
134string Hardware::GetFirmwareVersion() {
135 return ReadValueFromCrosSystem("fwid");
136}
137
138string Hardware::GetECVersion() {
139 string input_line;
140 int exit_code = 0;
141 vector<string> cmd(1, "/usr/sbin/mosys");
142 cmd.push_back("-k");
143 cmd.push_back("ec");
144 cmd.push_back("info");
145
146 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
147 if (!success || exit_code) {
148 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
149 return "";
150 }
151
152 return utils::ParseECVersion(input_line);
153}
154
Alex Deymo42432912013-07-12 20:21:15 -0700155} // namespace chromeos_update_engine