blob: f094d97dabba45f5f2f512a33b8a6a1009bb1825 [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>
Alex Vakulenko75039d72014-03-25 12:36:28 -07009#include <base/strings/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
Chris Masonef8d037f2014-02-19 01:53:00 +000023#include "update_engine/hwid_override.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070024#include "update_engine/subprocess.h"
25#include "update_engine/utils.h"
26
Alex Deymo42432912013-07-12 20:21:15 -070027using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070028using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070029
30namespace chromeos_update_engine {
31
Chris Masonef8d037f2014-02-19 01:53:00 +000032Hardware::Hardware() {}
33
34Hardware::~Hardware() {}
35
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080036string Hardware::BootKernelDevice() const {
Don Garrett83692e42013-11-08 10:11:30 -080037 return utils::KernelDeviceOfBootDevice(Hardware::BootDevice());
38}
39
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080040string Hardware::BootDevice() const {
Alex Deymo42432912013-07-12 20:21:15 -070041 char boot_path[PATH_MAX];
42 // Resolve the boot device path fully, including dereferencing
43 // through dm-verity.
44 int ret = rootdev(boot_path, sizeof(boot_path), true, false);
45
46 if (ret < 0) {
47 LOG(ERROR) << "rootdev failed to find the root device";
48 return "";
49 }
50 LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node";
51
52 // This local variable is used to construct the return string and is not
53 // passed around after use.
54 return boot_path;
55}
56
Don Garrett83692e42013-11-08 10:11:30 -080057bool Hardware::IsKernelBootable(const std::string& kernel_device,
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080058 bool* bootable) const {
Don Garrett83692e42013-11-08 10:11:30 -080059 CgptAddParams params;
60 memset(&params, '\0', sizeof(params));
61
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080062 std::string disk_name;
63 int partition_num = 0;
Don Garrett83692e42013-11-08 10:11:30 -080064
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080065 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
66 return false;
67
68 params.drive_name = const_cast<char *>(disk_name.c_str());
69 params.partition = partition_num;
Don Garrett83692e42013-11-08 10:11:30 -080070
71 int retval = CgptGetPartitionDetails(&params);
72 if (retval != CGPT_OK)
73 return false;
74
75 *bootable = params.successful || (params.tries > 0);
76 return true;
77}
78
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080079std::vector<std::string> Hardware::GetKernelDevices() const {
Alex Vakulenko59e253e2014-02-24 10:40:21 -080080 LOG(INFO) << "GetAllKernelDevices";
81
82 std::string disk_name = utils::GetDiskName(Hardware::BootKernelDevice());
83 if(disk_name.empty()) {
84 LOG(ERROR) << "Failed to get the cuurent kernel boot disk name";
85 return std::vector<std::string>();
86 }
87
88 std::vector<std::string> devices;
Alex Vakulenko2bddadd2014-03-27 13:23:46 -070089 for (int partition_num : {2, 4}) { // for now, only #2, #4 for slot A & B
Alex Vakulenko59e253e2014-02-24 10:40:21 -080090 std::string device = utils::MakePartitionName(disk_name, partition_num);
91 if(!device.empty()) {
92 devices.push_back(std::move(device));
93 } else {
94 LOG(ERROR) << "Cannot make a partition name for disk: "
95 << disk_name << ", partition: " << partition_num;
96 }
97 }
98
99 return devices;
100}
101
102
Don Garrett83692e42013-11-08 10:11:30 -0800103bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) {
104 LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device;
105
Don Garrett6646b442013-11-13 15:29:11 -0800106 if (kernel_device == BootKernelDevice()) {
107 LOG(ERROR) << "Refusing to mark current kernel as unbootable.";
108 return false;
109 }
110
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800111 std::string disk_name;
112 int partition_num = 0;
113
114 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
115 return false;
Don Garrett83692e42013-11-08 10:11:30 -0800116
117 CgptAddParams params;
118 memset(&params, 0, sizeof(params));
119
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800120 params.drive_name = const_cast<char *>(disk_name.c_str());
121 params.partition = partition_num;
Don Garrett83692e42013-11-08 10:11:30 -0800122
123 params.successful = false;
124 params.set_successful = true;
125
126 params.tries = 0;
127 params.set_tries = true;
128
129 int retval = CgptSetAttributes(&params);
130 if (retval != CGPT_OK) {
131 LOG(ERROR) << "Marking kernel unbootable failed.";
132 return false;
133 }
134
135 return true;
136}
137
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800138bool Hardware::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700139 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700140}
141
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800142bool Hardware::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700143 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700144 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
145 return !dev_mode;
146}
147
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700148static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700149 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700150
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700151 const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
152 sizeof(value_buffer));
153 if (rv != NULL) {
154 string return_value(value_buffer);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700155 TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value);
156 return return_value;
157 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700158
159 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700160 return "";
161}
162
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800163string Hardware::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000164 if (USE_HWID_OVERRIDE) {
165 return HwidOverride::Read(base::FilePath("/"));
166 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700167 return ReadValueFromCrosSystem("hwid");
168}
169
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800170string Hardware::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700171 return ReadValueFromCrosSystem("fwid");
172}
173
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800174string Hardware::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700175 string input_line;
176 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800177 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700178
179 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
180 if (!success || exit_code) {
181 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
182 return "";
183 }
184
185 return utils::ParseECVersion(input_line);
186}
187
Alex Deymo42432912013-07-12 20:21:15 -0700188} // namespace chromeos_update_engine