blob: f226a6599338128f08404b65a7e93fe44a59cc96 [file] [log] [blame]
Alex Deymo763e7db2015-08-27 21:08:08 -07001//
2// Copyright (C) 2015 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//
16
Alex Deymo1b03f9f2015-12-09 00:38:36 -080017#include "update_engine/boot_control_chromeos.h"
Alex Deymo763e7db2015-08-27 21:08:08 -070018
Xiaochu Liu88d90382018-08-29 16:09:11 -070019#include <memory>
Alex Deymo763e7db2015-08-27 21:08:08 -070020#include <string>
Xiaochu Liu88d90382018-08-29 16:09:11 -070021#include <utility>
Alex Deymo763e7db2015-08-27 21:08:08 -070022
Alex Deymoaa26f622015-09-16 18:21:27 -070023#include <base/bind.h>
Alex Deymo763e7db2015-08-27 21:08:08 -070024#include <base/files/file_path.h>
25#include <base/files/file_util.h>
26#include <base/strings/string_util.h>
Xiaochu Liu88d90382018-08-29 16:09:11 -070027#include <brillo/update_engine/utils.h>
Alex Deymo763e7db2015-08-27 21:08:08 -070028#include <rootdev/rootdev.h>
29
30extern "C" {
31#include <vboot/vboot_host.h>
32}
33
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/common/boot_control.h"
35#include "update_engine/common/subprocess.h"
36#include "update_engine/common/utils.h"
Alex Deymo763e7db2015-08-27 21:08:08 -070037
38using std::string;
39
40namespace {
41
42const char* kChromeOSPartitionNameKernel = "kernel";
43const char* kChromeOSPartitionNameRoot = "root";
44const char* kAndroidPartitionNameKernel = "boot";
45const char* kAndroidPartitionNameRoot = "system";
46
47// Returns the currently booted rootfs partition. "/dev/sda3", for example.
48string GetBootDevice() {
49 char boot_path[PATH_MAX];
50 // Resolve the boot device path fully, including dereferencing through
51 // dm-verity.
52 int ret = rootdev(boot_path, sizeof(boot_path), true, false);
53 if (ret < 0) {
54 LOG(ERROR) << "rootdev failed to find the root device";
55 return "";
56 }
57 LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node";
58
59 // This local variable is used to construct the return string and is not
60 // passed around after use.
61 return boot_path;
62}
63
Alex Deymoaa26f622015-09-16 18:21:27 -070064// ExecCallback called when the execution of setgoodkernel finishes. Notifies
65// the caller of MarkBootSuccessfullAsync() by calling |callback| with the
66// result.
67void OnMarkBootSuccessfulDone(base::Callback<void(bool)> callback,
68 int return_code,
69 const string& output) {
70 callback.Run(return_code == 0);
71}
72
Alex Deymo763e7db2015-08-27 21:08:08 -070073} // namespace
74
75namespace chromeos_update_engine {
76
Alex Deymob17327c2015-09-04 10:29:00 -070077namespace boot_control {
78
79// Factory defined in boot_control.h.
80std::unique_ptr<BootControlInterface> CreateBootControl() {
81 std::unique_ptr<BootControlChromeOS> boot_control_chromeos(
82 new BootControlChromeOS());
83 if (!boot_control_chromeos->Init()) {
84 LOG(ERROR) << "Ignoring BootControlChromeOS failure. We won't run updates.";
85 }
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070086 return std::move(boot_control_chromeos);
Alex Deymob17327c2015-09-04 10:29:00 -070087}
88
89} // namespace boot_control
90
Alex Deymo763e7db2015-08-27 21:08:08 -070091bool BootControlChromeOS::Init() {
92 string boot_device = GetBootDevice();
93 if (boot_device.empty())
94 return false;
95
96 int partition_num;
97 if (!utils::SplitPartitionName(boot_device, &boot_disk_name_, &partition_num))
98 return false;
99
100 // All installed Chrome OS devices have two slots. We don't update removable
101 // devices, so we will pretend we have only one slot in that case.
102 if (IsRemovableDevice(boot_disk_name_)) {
103 LOG(INFO)
104 << "Booted from a removable device, pretending we have only one slot.";
105 num_slots_ = 1;
106 } else {
107 // TODO(deymo): Look at the actual number of slots reported in the GPT.
108 num_slots_ = 2;
109 }
110
111 // Search through the slots to see which slot has the partition_num we booted
112 // from. This should map to one of the existing slots, otherwise something is
113 // very wrong.
114 current_slot_ = 0;
115 while (current_slot_ < num_slots_ &&
116 partition_num !=
117 GetPartitionNumber(kChromeOSPartitionNameRoot, current_slot_)) {
118 current_slot_++;
119 }
120 if (current_slot_ >= num_slots_) {
121 LOG(ERROR) << "Couldn't find the slot number corresponding to the "
122 "partition " << boot_device
123 << ", number of slots: " << num_slots_
124 << ". This device is not updateable.";
125 num_slots_ = 1;
126 current_slot_ = BootControlInterface::kInvalidSlot;
127 return false;
128 }
129
130 LOG(INFO) << "Booted from slot " << current_slot_ << " (slot "
Alex Deymo31d95ac2015-09-17 11:56:18 -0700131 << SlotName(current_slot_) << ") of " << num_slots_
132 << " slots present on disk " << boot_disk_name_;
Alex Deymo763e7db2015-08-27 21:08:08 -0700133 return true;
134}
135
136unsigned int BootControlChromeOS::GetNumSlots() const {
137 return num_slots_;
138}
139
140BootControlInterface::Slot BootControlChromeOS::GetCurrentSlot() const {
141 return current_slot_;
142}
143
144bool BootControlChromeOS::GetPartitionDevice(const string& partition_name,
145 unsigned int slot,
146 string* device) const {
Xiaochu Liu88d90382018-08-29 16:09:11 -0700147 // Partition name prefixed with |kPartitionNamePrefixDlc| is a DLC.
148 if (base::StartsWith(partition_name,
149 brillo::chromeos_update_engine::kPartitionNamePrefixDlc,
150 base::CompareCase::SENSITIVE)) {
151 // Extract DLC ID from partition_name (DLC ID is the string after
152 // |kPartitionNamePrefixDlc| in partition_name).
153 const auto dlc_id = partition_name.substr(
154 strlen(brillo::chromeos_update_engine::kPartitionNamePrefixDlc));
155 if (dlc_id.empty()) {
156 LOG(ERROR) << " partition name does not contain dlc id:"
157 << partition_name;
158 return false;
159 }
160 *device =
161 base::FilePath(
162 brillo::chromeos_update_engine::kDlcInstallRootDirectoryEncrypted)
163 .Append(dlc_id)
164 .Append(slot == 0
165 ? brillo::chromeos_update_engine::kPartitionNameDlcA
166 : brillo::chromeos_update_engine::kPartitionNameDlcB)
167 .Append(brillo::chromeos_update_engine::kPartitionNameDlcImage)
168 .value();
169 return true;
170 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700171 int partition_num = GetPartitionNumber(partition_name, slot);
172 if (partition_num < 0)
173 return false;
174
175 string part_device = utils::MakePartitionName(boot_disk_name_, partition_num);
176 if (part_device.empty())
177 return false;
178
179 *device = part_device;
180 return true;
181}
182
183bool BootControlChromeOS::IsSlotBootable(Slot slot) const {
184 int partition_num = GetPartitionNumber(kChromeOSPartitionNameKernel, slot);
185 if (partition_num < 0)
186 return false;
187
188 CgptAddParams params;
189 memset(&params, '\0', sizeof(params));
190 params.drive_name = const_cast<char*>(boot_disk_name_.c_str());
191 params.partition = partition_num;
192
193 int retval = CgptGetPartitionDetails(&params);
194 if (retval != CGPT_OK)
195 return false;
196
197 return params.successful || params.tries > 0;
198}
199
200bool BootControlChromeOS::MarkSlotUnbootable(Slot slot) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700201 LOG(INFO) << "Marking slot " << SlotName(slot) << " unbootable";
Alex Deymo763e7db2015-08-27 21:08:08 -0700202
203 if (slot == current_slot_) {
204 LOG(ERROR) << "Refusing to mark current slot as unbootable.";
205 return false;
206 }
207
208 int partition_num = GetPartitionNumber(kChromeOSPartitionNameKernel, slot);
209 if (partition_num < 0)
210 return false;
211
212 CgptAddParams params;
213 memset(&params, 0, sizeof(params));
214
215 params.drive_name = const_cast<char*>(boot_disk_name_.c_str());
216 params.partition = partition_num;
217
218 params.successful = false;
219 params.set_successful = true;
220
221 params.tries = 0;
222 params.set_tries = true;
223
224 int retval = CgptSetAttributes(&params);
225 if (retval != CGPT_OK) {
226 LOG(ERROR) << "Marking kernel unbootable failed.";
227 return false;
228 }
229
230 return true;
231}
232
Alex Deymo31d95ac2015-09-17 11:56:18 -0700233bool BootControlChromeOS::SetActiveBootSlot(Slot slot) {
234 LOG(INFO) << "Marking slot " << SlotName(slot) << " active.";
235
236 int partition_num = GetPartitionNumber(kChromeOSPartitionNameKernel, slot);
237 if (partition_num < 0)
238 return false;
239
240 CgptPrioritizeParams prio_params;
241 memset(&prio_params, 0, sizeof(prio_params));
242
243 prio_params.drive_name = const_cast<char*>(boot_disk_name_.c_str());
244 prio_params.set_partition = partition_num;
245
246 prio_params.max_priority = 0;
247
248 int retval = CgptPrioritize(&prio_params);
249 if (retval != CGPT_OK) {
250 LOG(ERROR) << "Unable to set highest priority for slot " << SlotName(slot)
251 << " (partition " << partition_num << ").";
252 return false;
253 }
254
255 CgptAddParams add_params;
256 memset(&add_params, 0, sizeof(add_params));
257
258 add_params.drive_name = const_cast<char*>(boot_disk_name_.c_str());
259 add_params.partition = partition_num;
260
261 add_params.tries = 6;
262 add_params.set_tries = true;
263
264 retval = CgptSetAttributes(&add_params);
265 if (retval != CGPT_OK) {
266 LOG(ERROR) << "Unable to set NumTriesLeft to " << add_params.tries
267 << " for slot " << SlotName(slot) << " (partition "
268 << partition_num << ").";
269 return false;
270 }
271
272 return true;
273}
274
Alex Deymoaa26f622015-09-16 18:21:27 -0700275bool BootControlChromeOS::MarkBootSuccessfulAsync(
276 base::Callback<void(bool)> callback) {
277 return Subprocess::Get().Exec(
278 {"/usr/sbin/chromeos-setgoodkernel"},
279 base::Bind(&OnMarkBootSuccessfulDone, callback)) != 0;
280}
281
Alex Deymo763e7db2015-08-27 21:08:08 -0700282// static
283string BootControlChromeOS::SysfsBlockDevice(const string& device) {
284 base::FilePath device_path(device);
285 if (device_path.DirName().value() != "/dev") {
286 return "";
287 }
288 return base::FilePath("/sys/block").Append(device_path.BaseName()).value();
289}
290
291// static
292bool BootControlChromeOS::IsRemovableDevice(const string& device) {
293 string sysfs_block = SysfsBlockDevice(device);
294 string removable;
295 if (sysfs_block.empty() ||
296 !base::ReadFileToString(base::FilePath(sysfs_block).Append("removable"),
297 &removable)) {
298 return false;
299 }
300 base::TrimWhitespaceASCII(removable, base::TRIM_ALL, &removable);
301 return removable == "1";
302}
303
304int BootControlChromeOS::GetPartitionNumber(
305 const string partition_name,
306 BootControlInterface::Slot slot) const {
307 if (slot >= num_slots_) {
308 LOG(ERROR) << "Invalid slot number: " << slot << ", we only have "
309 << num_slots_ << " slot(s)";
310 return -1;
311 }
312
313 // In Chrome OS, the partition numbers are hard-coded:
314 // KERNEL-A=2, ROOT-A=3, KERNEL-B=4, ROOT-B=4, ...
315 // To help compatibility between different we accept both lowercase and
316 // uppercase names in the ChromeOS or Brillo standard names.
317 // See http://www.chromium.org/chromium-os/chromiumos-design-docs/disk-format
Alex Vakulenko0103c362016-01-20 07:56:15 -0800318 string partition_lower = base::ToLowerASCII(partition_name);
Alex Deymo763e7db2015-08-27 21:08:08 -0700319 int base_part_num = 2 + 2 * slot;
320 if (partition_lower == kChromeOSPartitionNameKernel ||
321 partition_lower == kAndroidPartitionNameKernel)
322 return base_part_num + 0;
323 if (partition_lower == kChromeOSPartitionNameRoot ||
324 partition_lower == kAndroidPartitionNameRoot)
325 return base_part_num + 1;
326 LOG(ERROR) << "Unknown Chrome OS partition name \"" << partition_name << "\"";
327 return -1;
328}
329
330} // namespace chromeos_update_engine