blob: 35ff8290d6391fac887a768b6dbaf857283e40f8 [file] [log] [blame]
Alex Deymob17327c2015-09-04 10:29:00 -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_android.h"
Alex Deymob17327c2015-09-04 10:29:00 -070018
Sen Jiangd944faa2018-08-22 18:46:39 -070019#include <memory>
20#include <utility>
Yifan Hongd4db07e2018-10-18 17:46:27 -070021#include <vector>
Sen Jiangd944faa2018-08-22 18:46:39 -070022
Alex Deymoaa26f622015-09-16 18:21:27 -070023#include <base/bind.h>
Alex Deymoaa26f622015-09-16 18:21:27 -070024#include <base/logging.h>
Yifan Hongd4db07e2018-10-18 17:46:27 -070025#include <base/strings/string_util.h>
Sen Jiangd944faa2018-08-22 18:46:39 -070026#include <bootloader_message/bootloader_message.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070027#include <brillo/message_loops/message_loop.h>
David Andersond63cb3c2018-10-01 14:15:00 -070028#include <fs_mgr.h>
Alex Deymob17327c2015-09-04 10:29:00 -070029
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/utils.h"
Yifan Hong537802d2018-08-15 13:15:42 -070031#include "update_engine/dynamic_partition_control_android.h"
Alex Deymob17327c2015-09-04 10:29:00 -070032
33using std::string;
34
Yifan Hong537802d2018-08-15 13:15:42 -070035using android::dm::DmDeviceState;
Yifan Hong537802d2018-08-15 13:15:42 -070036using android::fs_mgr::Partition;
Yifan Hong537802d2018-08-15 13:15:42 -070037using android::hardware::hidl_string;
Connor O'Briencee6ad92016-11-21 13:53:52 -080038using android::hardware::Return;
39using android::hardware::boot::V1_0::BoolResult;
40using android::hardware::boot::V1_0::CommandResult;
41using android::hardware::boot::V1_0::IBootControl;
Yifan Hong537802d2018-08-15 13:15:42 -070042using Slot = chromeos_update_engine::BootControlInterface::Slot;
Yifan Hong6b0a9842018-10-16 16:54:18 -070043using PartitionMetadata =
44 chromeos_update_engine::BootControlInterface::PartitionMetadata;
Connor O'Briencee6ad92016-11-21 13:53:52 -080045
46namespace {
Yifan Hong537802d2018-08-15 13:15:42 -070047
Connor O'Briencee6ad92016-11-21 13:53:52 -080048auto StoreResultCallback(CommandResult* dest) {
49 return [dest](const CommandResult& result) { *dest = result; };
50}
51} // namespace
Alex Deymo44348e02016-07-29 16:22:26 -070052
Alex Deymob17327c2015-09-04 10:29:00 -070053namespace chromeos_update_engine {
54
55namespace boot_control {
56
57// Factory defined in boot_control.h.
58std::unique_ptr<BootControlInterface> CreateBootControl() {
Yifan Hong537802d2018-08-15 13:15:42 -070059 auto boot_control = std::make_unique<BootControlAndroid>();
David Zeuthen753fadc2015-09-15 16:34:09 -040060 if (!boot_control->Init()) {
61 return nullptr;
62 }
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070063 return std::move(boot_control);
Alex Deymob17327c2015-09-04 10:29:00 -070064}
65
66} // namespace boot_control
67
David Zeuthen753fadc2015-09-15 16:34:09 -040068bool BootControlAndroid::Init() {
Chris Phoenixafde8e82017-01-17 23:14:58 -080069 module_ = IBootControl::getService();
Connor O'Briencee6ad92016-11-21 13:53:52 -080070 if (module_ == nullptr) {
Steven Moreland927e00d2017-01-04 12:58:40 -080071 LOG(ERROR) << "Error getting bootctrl HIDL module.";
David Zeuthen753fadc2015-09-15 16:34:09 -040072 return false;
73 }
74
Steven Moreland927e00d2017-01-04 12:58:40 -080075 LOG(INFO) << "Loaded boot control hidl hal.";
David Zeuthen753fadc2015-09-15 16:34:09 -040076
Yifan Hong537802d2018-08-15 13:15:42 -070077 dynamic_control_ = std::make_unique<DynamicPartitionControlAndroid>();
78
David Zeuthen753fadc2015-09-15 16:34:09 -040079 return true;
80}
Alex Deymob17327c2015-09-04 10:29:00 -070081
Yifan Hong537802d2018-08-15 13:15:42 -070082void BootControlAndroid::Cleanup() {
83 dynamic_control_->Cleanup();
84}
85
Alex Deymob17327c2015-09-04 10:29:00 -070086unsigned int BootControlAndroid::GetNumSlots() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080087 return module_->getNumberSlots();
Alex Deymob17327c2015-09-04 10:29:00 -070088}
89
90BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080091 return module_->getCurrentSlot();
Alex Deymob17327c2015-09-04 10:29:00 -070092}
93
Yifan Hong537802d2018-08-15 13:15:42 -070094bool BootControlAndroid::GetSuffix(Slot slot, string* suffix) const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080095 auto store_suffix_cb = [&suffix](hidl_string cb_suffix) {
Yifan Hong537802d2018-08-15 13:15:42 -070096 *suffix = cb_suffix.c_str();
Connor O'Briencee6ad92016-11-21 13:53:52 -080097 };
98 Return<void> ret = module_->getSuffix(slot, store_suffix_cb);
99
Yifan Hong7b514b42016-12-21 13:02:00 -0800100 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700101 LOG(ERROR) << "boot_control impl returned no suffix for slot "
102 << SlotName(slot);
David Zeuthen753fadc2015-09-15 16:34:09 -0400103 return false;
104 }
Yifan Hong537802d2018-08-15 13:15:42 -0700105 return true;
106}
107
Yifan Hongae04e192018-10-29 11:00:28 -0700108namespace {
109
110enum class DynamicPartitionDeviceStatus {
111 SUCCESS,
112 ERROR,
113 TRY_STATIC,
114};
115
116DynamicPartitionDeviceStatus GetDynamicPartitionDevice(
117 DynamicPartitionControlInterface* dynamic_control,
118 const string& super_device,
119 const string& partition_name_suffix,
120 Slot slot,
121 string* device) {
Yifan Hong8cc1e9e2018-11-14 13:52:12 -0800122 if (!dynamic_control->IsDynamicPartitionsEnabled()) {
123 return DynamicPartitionDeviceStatus::TRY_STATIC;
124 }
125
Yifan Hong6e706b12018-11-09 16:50:51 -0800126 auto builder = dynamic_control->LoadMetadataBuilder(
127 super_device, slot, BootControlInterface::kInvalidSlot);
Yifan Hongae04e192018-10-29 11:00:28 -0700128
129 if (builder == nullptr) {
Yifan Hongae04e192018-10-29 11:00:28 -0700130 LOG(ERROR) << "No metadata in slot "
131 << BootControlInterface::SlotName(slot);
132 return DynamicPartitionDeviceStatus::ERROR;
133 }
134
135 if (builder->FindPartition(partition_name_suffix) == nullptr) {
136 LOG(INFO) << partition_name_suffix
137 << " is not in super partition metadata.";
138 return DynamicPartitionDeviceStatus::TRY_STATIC;
139 }
140
141 DmDeviceState state = dynamic_control->GetState(partition_name_suffix);
142
143 if (state == DmDeviceState::ACTIVE) {
144 if (dynamic_control->GetDmDevicePathByName(partition_name_suffix, device)) {
145 LOG(INFO) << partition_name_suffix
146 << " is mapped on device mapper: " << *device;
147 return DynamicPartitionDeviceStatus::SUCCESS;
148 }
149 LOG(ERROR) << partition_name_suffix << " is mapped but path is unknown.";
150 return DynamicPartitionDeviceStatus::ERROR;
151 }
152
153 // DeltaPerformer calls InitPartitionMetadata before calling
154 // InstallPlan::LoadPartitionsFromSlots. After InitPartitionMetadata,
155 // the target partition must be re-mapped with force_writable == true.
156 // Hence, if it is not mapped, we assume it is a source partition and
157 // map it without force_writable.
158 if (state == DmDeviceState::INVALID) {
159 if (dynamic_control->MapPartitionOnDeviceMapper(super_device,
160 partition_name_suffix,
161 slot,
162 false /* force_writable */,
163 device)) {
164 return DynamicPartitionDeviceStatus::SUCCESS;
165 }
166 return DynamicPartitionDeviceStatus::ERROR;
167 }
168
169 LOG(ERROR) << partition_name_suffix
170 << " is mapped on device mapper but state is unknown: "
171 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
172 return DynamicPartitionDeviceStatus::ERROR;
173}
174} // namespace
175
Yifan Hong537802d2018-08-15 13:15:42 -0700176bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
177 Slot slot,
178 string* device) const {
179 string suffix;
180 if (!GetSuffix(slot, &suffix)) {
181 return false;
182 }
Yifan Hongae04e192018-10-29 11:00:28 -0700183 const string partition_name_suffix = partition_name + suffix;
Yifan Hong537802d2018-08-15 13:15:42 -0700184
185 string device_dir_str;
186 if (!dynamic_control_->GetDeviceDir(&device_dir_str)) {
187 return false;
188 }
Yifan Hongae04e192018-10-29 11:00:28 -0700189 base::FilePath device_dir(device_dir_str);
David Zeuthen753fadc2015-09-15 16:34:09 -0400190
Yifan Hongae04e192018-10-29 11:00:28 -0700191 string super_device =
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800192 device_dir.Append(fs_mgr_get_super_partition_name(slot)).value();
Yifan Hongae04e192018-10-29 11:00:28 -0700193 switch (GetDynamicPartitionDevice(dynamic_control_.get(),
194 super_device,
195 partition_name_suffix,
196 slot,
197 device)) {
198 case DynamicPartitionDeviceStatus::SUCCESS:
199 return true;
200 case DynamicPartitionDeviceStatus::TRY_STATIC:
201 break;
202 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
203 default:
204 return false;
205 }
206
207 base::FilePath path = device_dir.Append(partition_name_suffix);
Yifan Hong537802d2018-08-15 13:15:42 -0700208 if (!dynamic_control_->DeviceExists(path.value())) {
David Zeuthen753fadc2015-09-15 16:34:09 -0400209 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
210 return false;
211 }
212
213 *device = path.value();
214 return true;
Alex Deymob17327c2015-09-04 10:29:00 -0700215}
216
217bool BootControlAndroid::IsSlotBootable(Slot slot) const {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800218 Return<BoolResult> ret = module_->isSlotBootable(slot);
Yifan Hong7b514b42016-12-21 13:02:00 -0800219 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700220 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Connor O'Briencee6ad92016-11-21 13:53:52 -0800221 << " is bootable: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800222 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400223 return false;
224 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800225 if (ret == BoolResult::INVALID_SLOT) {
226 LOG(ERROR) << "Invalid slot: " << SlotName(slot);
227 return false;
228 }
229 return ret == BoolResult::TRUE;
Alex Deymob17327c2015-09-04 10:29:00 -0700230}
231
232bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800233 CommandResult result;
234 auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800235 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800236 LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
237 << SlotName(slot) << ": "
Yifan Hong7b514b42016-12-21 13:02:00 -0800238 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400239 return false;
240 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800241 if (!result.success) {
242 LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
243 << " as unbootable: " << result.errMsg.c_str();
244 }
245 return result.success;
Alex Deymob17327c2015-09-04 10:29:00 -0700246}
247
Alex Deymo31d95ac2015-09-17 11:56:18 -0700248bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800249 CommandResult result;
250 auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800251 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800252 LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
Yifan Hong7b514b42016-12-21 13:02:00 -0800253 << ": " << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800254 return false;
Alex Deymo29dcbf32016-10-06 13:33:20 -0700255 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800256 if (!result.success) {
257 LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
258 << ": " << result.errMsg.c_str();
259 }
260 return result.success;
Alex Deymo31d95ac2015-09-17 11:56:18 -0700261}
262
Alex Deymoaa26f622015-09-16 18:21:27 -0700263bool BootControlAndroid::MarkBootSuccessfulAsync(
264 base::Callback<void(bool)> callback) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800265 CommandResult result;
266 auto ret = module_->markBootSuccessful(StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800267 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800268 LOG(ERROR) << "Unable to call MarkBootSuccessful: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800269 << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800270 return false;
271 }
272 if (!result.success) {
273 LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str();
Alex Deymoaa26f622015-09-16 18:21:27 -0700274 }
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700275 return brillo::MessageLoop::current()->PostTask(
Connor O'Briencee6ad92016-11-21 13:53:52 -0800276 FROM_HERE, base::Bind(callback, result.success)) !=
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700277 brillo::MessageLoop::kTaskIdNull;
Alex Deymoaa26f622015-09-16 18:21:27 -0700278}
279
Yifan Hong537802d2018-08-15 13:15:42 -0700280namespace {
281
Yifan Hongd4db07e2018-10-18 17:46:27 -0700282bool InitPartitionMetadataInternal(
283 DynamicPartitionControlInterface* dynamic_control,
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800284 const string& source_device,
285 const string& target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700286 Slot source_slot,
287 Slot target_slot,
288 const string& target_suffix,
289 const PartitionMetadata& partition_metadata) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800290 auto builder = dynamic_control->LoadMetadataBuilder(
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800291 source_device, source_slot, target_slot);
Yifan Hongd4db07e2018-10-18 17:46:27 -0700292 if (builder == nullptr) {
293 // TODO(elsk): allow reconstructing metadata from partition_metadata
294 // in recovery sideload.
295 LOG(ERROR) << "No metadata at "
296 << BootControlInterface::SlotName(source_slot);
Yifan Hong537802d2018-08-15 13:15:42 -0700297 return false;
298 }
299
Yifan Hongd4db07e2018-10-18 17:46:27 -0700300 std::vector<string> groups = builder->ListGroups();
301 for (const auto& group_name : groups) {
302 if (base::EndsWith(
303 group_name, target_suffix, base::CompareCase::SENSITIVE)) {
304 LOG(INFO) << "Removing group " << group_name;
305 builder->RemoveGroupAndPartitions(group_name);
306 }
307 }
308
309 uint64_t total_size = 0;
310 for (const auto& group : partition_metadata.groups) {
311 total_size += group.size;
312 }
313
314 if (total_size > (builder->AllocatableSpace() / 2)) {
315 LOG(ERROR)
316 << "The maximum size of all groups with suffix " << target_suffix
317 << " (" << total_size
318 << ") has exceeded half of allocatable space for dynamic partitions "
319 << (builder->AllocatableSpace() / 2) << ".";
Yifan Hong537802d2018-08-15 13:15:42 -0700320 return false;
321 }
322
Yifan Hongd4db07e2018-10-18 17:46:27 -0700323 for (const auto& group : partition_metadata.groups) {
324 auto group_name_suffix = group.name + target_suffix;
325 if (!builder->AddGroup(group_name_suffix, group.size)) {
326 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
327 << group.size;
328 return false;
329 }
330 LOG(INFO) << "Added group " << group_name_suffix << " with size "
331 << group.size;
332
333 for (const auto& partition : group.partitions) {
334 auto parition_name_suffix = partition.name + target_suffix;
335 Partition* p = builder->AddPartition(
336 parition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
337 if (!p) {
338 LOG(ERROR) << "Cannot add partition " << parition_name_suffix
339 << " to group " << group_name_suffix;
340 return false;
341 }
342 if (!builder->ResizePartition(p, partition.size)) {
343 LOG(ERROR) << "Cannot resize partition " << parition_name_suffix
344 << " to size " << partition.size << ". Not enough space?";
345 return false;
346 }
347 LOG(INFO) << "Added partition " << parition_name_suffix << " to group "
348 << group_name_suffix << " with size " << partition.size;
349 }
Yifan Hong537802d2018-08-15 13:15:42 -0700350 }
351
Yifan Hongd4db07e2018-10-18 17:46:27 -0700352 return dynamic_control->StoreMetadata(
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800353 target_device, builder.get(), target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -0700354}
355
Yifan Hongaf65ef12018-10-29 11:09:06 -0700356// Unmap all partitions, and remap partitions as writable.
Yifan Hongd4db07e2018-10-18 17:46:27 -0700357bool Remap(DynamicPartitionControlInterface* dynamic_control,
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800358 const string& target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700359 Slot target_slot,
360 const string& target_suffix,
361 const PartitionMetadata& partition_metadata) {
362 for (const auto& group : partition_metadata.groups) {
363 for (const auto& partition : group.partitions) {
364 if (!dynamic_control->UnmapPartitionOnDeviceMapper(
365 partition.name + target_suffix, true /* wait */)) {
366 return false;
367 }
368 if (partition.size == 0) {
369 continue;
370 }
371 string map_path;
372 if (!dynamic_control->MapPartitionOnDeviceMapper(
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800373 target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700374 partition.name + target_suffix,
375 target_slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700376 true /* force writable */,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700377 &map_path)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700378 return false;
379 }
380 }
Yifan Hong537802d2018-08-15 13:15:42 -0700381 }
382 return true;
383}
384
Yifan Hong537802d2018-08-15 13:15:42 -0700385} // namespace
386
387bool BootControlAndroid::InitPartitionMetadata(
Yifan Hong6b0a9842018-10-16 16:54:18 -0700388 Slot target_slot, const PartitionMetadata& partition_metadata) {
Yifan Hong537802d2018-08-15 13:15:42 -0700389 if (!dynamic_control_->IsDynamicPartitionsEnabled()) {
390 return true;
391 }
392
393 string device_dir_str;
394 if (!dynamic_control_->GetDeviceDir(&device_dir_str)) {
395 return false;
396 }
397 base::FilePath device_dir(device_dir_str);
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800398 string target_device =
399 device_dir.Append(fs_mgr_get_super_partition_name(target_slot)).value();
Yifan Hong537802d2018-08-15 13:15:42 -0700400
401 Slot current_slot = GetCurrentSlot();
402 if (target_slot == current_slot) {
403 LOG(ERROR) << "Cannot call InitPartitionMetadata on current slot.";
404 return false;
405 }
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800406 string source_device =
407 device_dir.Append(fs_mgr_get_super_partition_name(current_slot)).value();
Yifan Hong537802d2018-08-15 13:15:42 -0700408
Yifan Hong537802d2018-08-15 13:15:42 -0700409 string target_suffix;
410 if (!GetSuffix(target_slot, &target_suffix)) {
411 return false;
412 }
413
Yifan Hongd4db07e2018-10-18 17:46:27 -0700414 if (!InitPartitionMetadataInternal(dynamic_control_.get(),
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800415 source_device,
416 target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700417 current_slot,
418 target_slot,
419 target_suffix,
420 partition_metadata)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700421 return false;
422 }
423
Yifan Hongd4db07e2018-10-18 17:46:27 -0700424 if (!Remap(dynamic_control_.get(),
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800425 target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700426 target_slot,
427 target_suffix,
428 partition_metadata)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700429 return false;
430 }
431
Yifan Hong537802d2018-08-15 13:15:42 -0700432 return true;
433}
434
Alex Deymob17327c2015-09-04 10:29:00 -0700435} // namespace chromeos_update_engine