blob: 95456f007edeb75b6447d683cda4c4f007ff7394 [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
Yifan Hongc96589a2018-09-24 16:53:24 -070019#include <memory>
Alex Deymo763e7db2015-08-27 21:08:08 -070020#include <string>
Yifan Hongc96589a2018-09-24 16:53:24 -070021#include <utility>
Amin Hassani73733a02019-03-18 14:20:46 -070022#include <vector>
Alex Deymo763e7db2015-08-27 21:08:08 -070023
Alex Deymoaa26f622015-09-16 18:21:27 -070024#include <base/bind.h>
Alex Deymo763e7db2015-08-27 21:08:08 -070025#include <base/files/file_path.h>
26#include <base/files/file_util.h>
Amin Hassani73733a02019-03-18 14:20:46 -070027#include <base/strings/string_split.h>
Alex Deymo763e7db2015-08-27 21:08:08 -070028#include <base/strings/string_util.h>
Xiaochu Liub9cf5012019-01-25 11:05:58 -080029#include <chromeos/constants/imageloader.h>
Alex Deymo763e7db2015-08-27 21:08:08 -070030#include <rootdev/rootdev.h>
31
32extern "C" {
33#include <vboot/vboot_host.h>
34}
35
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/boot_control.h"
Yifan Hongdaac7322019-11-07 10:48:26 -080037#include "update_engine/common/dynamic_partition_control_stub.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080038#include "update_engine/common/subprocess.h"
39#include "update_engine/common/utils.h"
Alex Deymo763e7db2015-08-27 21:08:08 -070040
41using std::string;
Amin Hassani73733a02019-03-18 14:20:46 -070042using std::vector;
Alex Deymo763e7db2015-08-27 21:08:08 -070043
44namespace {
45
46const char* kChromeOSPartitionNameKernel = "kernel";
47const char* kChromeOSPartitionNameRoot = "root";
48const char* kAndroidPartitionNameKernel = "boot";
49const char* kAndroidPartitionNameRoot = "system";
50
Amin Hassani73733a02019-03-18 14:20:46 -070051const char kPartitionNamePrefixDlc[] = "dlc";
Xiaochu Liu0d692202018-10-18 10:52:20 -070052const char kPartitionNameDlcA[] = "dlc_a";
53const char kPartitionNameDlcB[] = "dlc_b";
54const char kPartitionNameDlcImage[] = "dlc.img";
55
Alex Deymo763e7db2015-08-27 21:08:08 -070056// Returns the currently booted rootfs partition. "/dev/sda3", for example.
57string GetBootDevice() {
58 char boot_path[PATH_MAX];
59 // Resolve the boot device path fully, including dereferencing through
60 // dm-verity.
61 int ret = rootdev(boot_path, sizeof(boot_path), true, false);
62 if (ret < 0) {
63 LOG(ERROR) << "rootdev failed to find the root device";
64 return "";
65 }
66 LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node";
67
68 // This local variable is used to construct the return string and is not
69 // passed around after use.
70 return boot_path;
71}
72
Alex Deymoaa26f622015-09-16 18:21:27 -070073// ExecCallback called when the execution of setgoodkernel finishes. Notifies
74// the caller of MarkBootSuccessfullAsync() by calling |callback| with the
75// result.
76void OnMarkBootSuccessfulDone(base::Callback<void(bool)> callback,
77 int return_code,
78 const string& output) {
79 callback.Run(return_code == 0);
80}
81
Alex Deymo763e7db2015-08-27 21:08:08 -070082} // namespace
83
84namespace chromeos_update_engine {
85
Alex Deymob17327c2015-09-04 10:29:00 -070086namespace boot_control {
87
88// Factory defined in boot_control.h.
89std::unique_ptr<BootControlInterface> CreateBootControl() {
90 std::unique_ptr<BootControlChromeOS> boot_control_chromeos(
91 new BootControlChromeOS());
92 if (!boot_control_chromeos->Init()) {
93 LOG(ERROR) << "Ignoring BootControlChromeOS failure. We won't run updates.";
94 }
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070095 return std::move(boot_control_chromeos);
Alex Deymob17327c2015-09-04 10:29:00 -070096}
97
98} // namespace boot_control
99
Alex Deymo763e7db2015-08-27 21:08:08 -0700100bool BootControlChromeOS::Init() {
101 string boot_device = GetBootDevice();
102 if (boot_device.empty())
103 return false;
104
105 int partition_num;
106 if (!utils::SplitPartitionName(boot_device, &boot_disk_name_, &partition_num))
107 return false;
108
109 // All installed Chrome OS devices have two slots. We don't update removable
110 // devices, so we will pretend we have only one slot in that case.
111 if (IsRemovableDevice(boot_disk_name_)) {
112 LOG(INFO)
113 << "Booted from a removable device, pretending we have only one slot.";
114 num_slots_ = 1;
115 } else {
116 // TODO(deymo): Look at the actual number of slots reported in the GPT.
117 num_slots_ = 2;
118 }
119
120 // Search through the slots to see which slot has the partition_num we booted
121 // from. This should map to one of the existing slots, otherwise something is
122 // very wrong.
123 current_slot_ = 0;
124 while (current_slot_ < num_slots_ &&
125 partition_num !=
126 GetPartitionNumber(kChromeOSPartitionNameRoot, current_slot_)) {
127 current_slot_++;
128 }
129 if (current_slot_ >= num_slots_) {
130 LOG(ERROR) << "Couldn't find the slot number corresponding to the "
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800131 << "partition " << boot_device << ", number of slots: "
132 << num_slots_ << ". This device is not updateable.";
Alex Deymo763e7db2015-08-27 21:08:08 -0700133 num_slots_ = 1;
134 current_slot_ = BootControlInterface::kInvalidSlot;
135 return false;
136 }
137
Yifan Hongdaac7322019-11-07 10:48:26 -0800138 dynamic_partition_control_.reset(new DynamicPartitionControlStub());
139
Alex Deymo763e7db2015-08-27 21:08:08 -0700140 LOG(INFO) << "Booted from slot " << current_slot_ << " (slot "
Alex Deymo31d95ac2015-09-17 11:56:18 -0700141 << SlotName(current_slot_) << ") of " << num_slots_
142 << " slots present on disk " << boot_disk_name_;
Alex Deymo763e7db2015-08-27 21:08:08 -0700143 return true;
144}
145
146unsigned int BootControlChromeOS::GetNumSlots() const {
147 return num_slots_;
148}
149
150BootControlInterface::Slot BootControlChromeOS::GetCurrentSlot() const {
151 return current_slot_;
152}
153
Amin Hassani73733a02019-03-18 14:20:46 -0700154bool BootControlChromeOS::ParseDlcPartitionName(
155 const std::string partition_name,
156 std::string* dlc_id,
157 std::string* dlc_package) const {
158 CHECK_NE(dlc_id, nullptr);
159 CHECK_NE(dlc_package, nullptr);
160
161 vector<string> tokens = base::SplitString(
162 partition_name, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
163 if (tokens.size() != 3 || tokens[0] != kPartitionNamePrefixDlc) {
164 LOG(ERROR) << "DLC partition name (" << partition_name
165 << ") is not well formatted.";
166 return false;
167 }
168 if (tokens[1].empty() || tokens[2].empty()) {
169 LOG(ERROR) << " partition name does not contain valid DLC ID (" << tokens[1]
170 << ") or package (" << tokens[2] << ")";
171 return false;
172 }
173
174 *dlc_id = tokens[1];
175 *dlc_package = tokens[2];
176 return true;
177}
178
Tianjie51a5a392020-06-03 14:39:32 -0700179bool BootControlChromeOS::GetPartitionDevice(const std::string& partition_name,
180 BootControlInterface::Slot slot,
181 bool not_in_payload,
182 std::string* device,
183 bool* is_dynamic) const {
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800184 // Partition name prefixed with |kPartitionNamePrefixDlc| is a DLC module.
Xiaochu Liu88d90382018-08-29 16:09:11 -0700185 if (base::StartsWith(partition_name,
Xiaochu Liu0d692202018-10-18 10:52:20 -0700186 kPartitionNamePrefixDlc,
Xiaochu Liu88d90382018-08-29 16:09:11 -0700187 base::CompareCase::SENSITIVE)) {
Amin Hassani73733a02019-03-18 14:20:46 -0700188 string dlc_id, dlc_package;
189 if (!ParseDlcPartitionName(partition_name, &dlc_id, &dlc_package))
Xiaochu Liu88d90382018-08-29 16:09:11 -0700190 return false;
Amin Hassani73733a02019-03-18 14:20:46 -0700191
Xiaochu Liub9cf5012019-01-25 11:05:58 -0800192 *device = base::FilePath(imageloader::kDlcImageRootpath)
Amin Hassani73733a02019-03-18 14:20:46 -0700193 .Append(dlc_id)
194 .Append(dlc_package)
Xiaochu Liu0d692202018-10-18 10:52:20 -0700195 .Append(slot == 0 ? kPartitionNameDlcA : kPartitionNameDlcB)
196 .Append(kPartitionNameDlcImage)
197 .value();
Xiaochu Liu88d90382018-08-29 16:09:11 -0700198 return true;
199 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700200 int partition_num = GetPartitionNumber(partition_name, slot);
201 if (partition_num < 0)
202 return false;
203
204 string part_device = utils::MakePartitionName(boot_disk_name_, partition_num);
205 if (part_device.empty())
206 return false;
207
208 *device = part_device;
Tianjie51a5a392020-06-03 14:39:32 -0700209 if (is_dynamic) {
210 *is_dynamic = false;
211 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700212 return true;
213}
214
Tianjie51a5a392020-06-03 14:39:32 -0700215bool BootControlChromeOS::GetPartitionDevice(const string& partition_name,
216 BootControlInterface::Slot slot,
217 string* device) const {
218 return GetPartitionDevice(partition_name, slot, false, device, nullptr);
219}
220
Alex Deymo763e7db2015-08-27 21:08:08 -0700221bool BootControlChromeOS::IsSlotBootable(Slot slot) const {
222 int partition_num = GetPartitionNumber(kChromeOSPartitionNameKernel, slot);
223 if (partition_num < 0)
224 return false;
225
226 CgptAddParams params;
227 memset(&params, '\0', sizeof(params));
228 params.drive_name = const_cast<char*>(boot_disk_name_.c_str());
229 params.partition = partition_num;
230
231 int retval = CgptGetPartitionDetails(&params);
232 if (retval != CGPT_OK)
233 return false;
234
235 return params.successful || params.tries > 0;
236}
237
238bool BootControlChromeOS::MarkSlotUnbootable(Slot slot) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700239 LOG(INFO) << "Marking slot " << SlotName(slot) << " unbootable";
Alex Deymo763e7db2015-08-27 21:08:08 -0700240
241 if (slot == current_slot_) {
242 LOG(ERROR) << "Refusing to mark current slot as unbootable.";
243 return false;
244 }
245
246 int partition_num = GetPartitionNumber(kChromeOSPartitionNameKernel, slot);
247 if (partition_num < 0)
248 return false;
249
250 CgptAddParams params;
251 memset(&params, 0, sizeof(params));
252
253 params.drive_name = const_cast<char*>(boot_disk_name_.c_str());
254 params.partition = partition_num;
255
256 params.successful = false;
257 params.set_successful = true;
258
259 params.tries = 0;
260 params.set_tries = true;
261
262 int retval = CgptSetAttributes(&params);
263 if (retval != CGPT_OK) {
264 LOG(ERROR) << "Marking kernel unbootable failed.";
265 return false;
266 }
267
268 return true;
269}
270
Alex Deymo31d95ac2015-09-17 11:56:18 -0700271bool BootControlChromeOS::SetActiveBootSlot(Slot slot) {
272 LOG(INFO) << "Marking slot " << SlotName(slot) << " active.";
273
274 int partition_num = GetPartitionNumber(kChromeOSPartitionNameKernel, slot);
275 if (partition_num < 0)
276 return false;
277
278 CgptPrioritizeParams prio_params;
279 memset(&prio_params, 0, sizeof(prio_params));
280
281 prio_params.drive_name = const_cast<char*>(boot_disk_name_.c_str());
282 prio_params.set_partition = partition_num;
283
284 prio_params.max_priority = 0;
285
286 int retval = CgptPrioritize(&prio_params);
287 if (retval != CGPT_OK) {
288 LOG(ERROR) << "Unable to set highest priority for slot " << SlotName(slot)
289 << " (partition " << partition_num << ").";
290 return false;
291 }
292
293 CgptAddParams add_params;
294 memset(&add_params, 0, sizeof(add_params));
295
296 add_params.drive_name = const_cast<char*>(boot_disk_name_.c_str());
297 add_params.partition = partition_num;
298
299 add_params.tries = 6;
300 add_params.set_tries = true;
301
302 retval = CgptSetAttributes(&add_params);
303 if (retval != CGPT_OK) {
304 LOG(ERROR) << "Unable to set NumTriesLeft to " << add_params.tries
305 << " for slot " << SlotName(slot) << " (partition "
306 << partition_num << ").";
307 return false;
308 }
309
310 return true;
311}
312
Alex Deymoaa26f622015-09-16 18:21:27 -0700313bool BootControlChromeOS::MarkBootSuccessfulAsync(
314 base::Callback<void(bool)> callback) {
315 return Subprocess::Get().Exec(
316 {"/usr/sbin/chromeos-setgoodkernel"},
317 base::Bind(&OnMarkBootSuccessfulDone, callback)) != 0;
318}
319
Alex Deymo763e7db2015-08-27 21:08:08 -0700320// static
321string BootControlChromeOS::SysfsBlockDevice(const string& device) {
322 base::FilePath device_path(device);
323 if (device_path.DirName().value() != "/dev") {
324 return "";
325 }
326 return base::FilePath("/sys/block").Append(device_path.BaseName()).value();
327}
328
329// static
330bool BootControlChromeOS::IsRemovableDevice(const string& device) {
331 string sysfs_block = SysfsBlockDevice(device);
332 string removable;
333 if (sysfs_block.empty() ||
334 !base::ReadFileToString(base::FilePath(sysfs_block).Append("removable"),
335 &removable)) {
336 return false;
337 }
338 base::TrimWhitespaceASCII(removable, base::TRIM_ALL, &removable);
339 return removable == "1";
340}
341
342int BootControlChromeOS::GetPartitionNumber(
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800343 const string partition_name, BootControlInterface::Slot slot) const {
Alex Deymo763e7db2015-08-27 21:08:08 -0700344 if (slot >= num_slots_) {
345 LOG(ERROR) << "Invalid slot number: " << slot << ", we only have "
346 << num_slots_ << " slot(s)";
347 return -1;
348 }
349
350 // In Chrome OS, the partition numbers are hard-coded:
351 // KERNEL-A=2, ROOT-A=3, KERNEL-B=4, ROOT-B=4, ...
352 // To help compatibility between different we accept both lowercase and
353 // uppercase names in the ChromeOS or Brillo standard names.
354 // See http://www.chromium.org/chromium-os/chromiumos-design-docs/disk-format
Alex Vakulenko0103c362016-01-20 07:56:15 -0800355 string partition_lower = base::ToLowerASCII(partition_name);
Alex Deymo763e7db2015-08-27 21:08:08 -0700356 int base_part_num = 2 + 2 * slot;
357 if (partition_lower == kChromeOSPartitionNameKernel ||
358 partition_lower == kAndroidPartitionNameKernel)
359 return base_part_num + 0;
360 if (partition_lower == kChromeOSPartitionNameRoot ||
361 partition_lower == kAndroidPartitionNameRoot)
362 return base_part_num + 1;
363 LOG(ERROR) << "Unknown Chrome OS partition name \"" << partition_name << "\"";
364 return -1;
365}
366
Yifan Hongf1415942020-02-24 18:34:49 -0800367bool BootControlChromeOS::IsSlotMarkedSuccessful(Slot slot) const {
368 LOG(ERROR) << __func__ << " not supported.";
369 return false;
370}
371
Yifan Hongdaac7322019-11-07 10:48:26 -0800372DynamicPartitionControlInterface*
373BootControlChromeOS::GetDynamicPartitionControl() {
374 return dynamic_partition_control_.get();
375}
376
Alex Deymo763e7db2015-08-27 21:08:08 -0700377} // namespace chromeos_update_engine