blob: 44bc9decd70dcfd40bf0b0b8826227214a1715ec [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;
89 const int slot_count = 2; // Use only partition slots A and B
90 devices.reserve(slot_count);
91 for(int slot = 0; slot < slot_count; slot++) {
92 int partition_num = (slot + 1) * 2; // for now, only #2, #4
93 std::string device = utils::MakePartitionName(disk_name, partition_num);
94 if(!device.empty()) {
95 devices.push_back(std::move(device));
96 } else {
97 LOG(ERROR) << "Cannot make a partition name for disk: "
98 << disk_name << ", partition: " << partition_num;
99 }
100 }
101
102 return devices;
103}
104
105
Don Garrett83692e42013-11-08 10:11:30 -0800106bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) {
107 LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device;
108
Don Garrett6646b442013-11-13 15:29:11 -0800109 if (kernel_device == BootKernelDevice()) {
110 LOG(ERROR) << "Refusing to mark current kernel as unbootable.";
111 return false;
112 }
113
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800114 std::string disk_name;
115 int partition_num = 0;
116
117 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
118 return false;
Don Garrett83692e42013-11-08 10:11:30 -0800119
120 CgptAddParams params;
121 memset(&params, 0, sizeof(params));
122
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800123 params.drive_name = const_cast<char *>(disk_name.c_str());
124 params.partition = partition_num;
Don Garrett83692e42013-11-08 10:11:30 -0800125
126 params.successful = false;
127 params.set_successful = true;
128
129 params.tries = 0;
130 params.set_tries = true;
131
132 int retval = CgptSetAttributes(&params);
133 if (retval != CGPT_OK) {
134 LOG(ERROR) << "Marking kernel unbootable failed.";
135 return false;
136 }
137
138 return true;
139}
140
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800141bool Hardware::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700142 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700143}
144
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800145bool Hardware::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700146 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700147 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
148 return !dev_mode;
149}
150
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700151static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700152 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700153
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700154 const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
155 sizeof(value_buffer));
156 if (rv != NULL) {
157 string return_value(value_buffer);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700158 TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value);
159 return return_value;
160 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700161
162 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700163 return "";
164}
165
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800166string Hardware::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000167 if (USE_HWID_OVERRIDE) {
168 return HwidOverride::Read(base::FilePath("/"));
169 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700170 return ReadValueFromCrosSystem("hwid");
171}
172
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800173string Hardware::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700174 return ReadValueFromCrosSystem("fwid");
175}
176
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800177string Hardware::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700178 string input_line;
179 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800180 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700181
182 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
183 if (!success || exit_code) {
184 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
185 return "";
186 }
187
188 return utils::ParseECVersion(input_line);
189}
190
Alex Deymo42432912013-07-12 20:21:15 -0700191} // namespace chromeos_update_engine