blob: f1d7b9c2918c8c3777cd00c987a4ed6793f3a9c9 [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 Hong6e706b12018-11-09 16:50:51 -0800122 auto builder = dynamic_control->LoadMetadataBuilder(
123 super_device, slot, BootControlInterface::kInvalidSlot);
Yifan Hongae04e192018-10-29 11:00:28 -0700124
125 if (builder == nullptr) {
126 if (!dynamic_control->IsDynamicPartitionsEnabled()) {
127 return DynamicPartitionDeviceStatus::TRY_STATIC;
128 }
129 LOG(ERROR) << "No metadata in slot "
130 << BootControlInterface::SlotName(slot);
131 return DynamicPartitionDeviceStatus::ERROR;
132 }
133
134 if (builder->FindPartition(partition_name_suffix) == nullptr) {
135 LOG(INFO) << partition_name_suffix
136 << " is not in super partition metadata.";
137 return DynamicPartitionDeviceStatus::TRY_STATIC;
138 }
139
140 DmDeviceState state = dynamic_control->GetState(partition_name_suffix);
141
142 if (state == DmDeviceState::ACTIVE) {
143 if (dynamic_control->GetDmDevicePathByName(partition_name_suffix, device)) {
144 LOG(INFO) << partition_name_suffix
145 << " is mapped on device mapper: " << *device;
146 return DynamicPartitionDeviceStatus::SUCCESS;
147 }
148 LOG(ERROR) << partition_name_suffix << " is mapped but path is unknown.";
149 return DynamicPartitionDeviceStatus::ERROR;
150 }
151
152 // DeltaPerformer calls InitPartitionMetadata before calling
153 // InstallPlan::LoadPartitionsFromSlots. After InitPartitionMetadata,
154 // the target partition must be re-mapped with force_writable == true.
155 // Hence, if it is not mapped, we assume it is a source partition and
156 // map it without force_writable.
157 if (state == DmDeviceState::INVALID) {
158 if (dynamic_control->MapPartitionOnDeviceMapper(super_device,
159 partition_name_suffix,
160 slot,
161 false /* force_writable */,
162 device)) {
163 return DynamicPartitionDeviceStatus::SUCCESS;
164 }
165 return DynamicPartitionDeviceStatus::ERROR;
166 }
167
168 LOG(ERROR) << partition_name_suffix
169 << " is mapped on device mapper but state is unknown: "
170 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
171 return DynamicPartitionDeviceStatus::ERROR;
172}
173} // namespace
174
Yifan Hong537802d2018-08-15 13:15:42 -0700175bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
176 Slot slot,
177 string* device) const {
178 string suffix;
179 if (!GetSuffix(slot, &suffix)) {
180 return false;
181 }
Yifan Hongae04e192018-10-29 11:00:28 -0700182 const string partition_name_suffix = partition_name + suffix;
Yifan Hong537802d2018-08-15 13:15:42 -0700183
184 string device_dir_str;
185 if (!dynamic_control_->GetDeviceDir(&device_dir_str)) {
186 return false;
187 }
Yifan Hongae04e192018-10-29 11:00:28 -0700188 base::FilePath device_dir(device_dir_str);
David Zeuthen753fadc2015-09-15 16:34:09 -0400189
Yifan Hongae04e192018-10-29 11:00:28 -0700190 string super_device =
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800191 device_dir.Append(fs_mgr_get_super_partition_name(slot)).value();
Yifan Hongae04e192018-10-29 11:00:28 -0700192 switch (GetDynamicPartitionDevice(dynamic_control_.get(),
193 super_device,
194 partition_name_suffix,
195 slot,
196 device)) {
197 case DynamicPartitionDeviceStatus::SUCCESS:
198 return true;
199 case DynamicPartitionDeviceStatus::TRY_STATIC:
200 break;
201 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
202 default:
203 return false;
204 }
205
206 base::FilePath path = device_dir.Append(partition_name_suffix);
Yifan Hong537802d2018-08-15 13:15:42 -0700207 if (!dynamic_control_->DeviceExists(path.value())) {
David Zeuthen753fadc2015-09-15 16:34:09 -0400208 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
209 return false;
210 }
211
212 *device = path.value();
213 return true;
Alex Deymob17327c2015-09-04 10:29:00 -0700214}
215
216bool BootControlAndroid::IsSlotBootable(Slot slot) const {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800217 Return<BoolResult> ret = module_->isSlotBootable(slot);
Yifan Hong7b514b42016-12-21 13:02:00 -0800218 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700219 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Connor O'Briencee6ad92016-11-21 13:53:52 -0800220 << " is bootable: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800221 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400222 return false;
223 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800224 if (ret == BoolResult::INVALID_SLOT) {
225 LOG(ERROR) << "Invalid slot: " << SlotName(slot);
226 return false;
227 }
228 return ret == BoolResult::TRUE;
Alex Deymob17327c2015-09-04 10:29:00 -0700229}
230
231bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800232 CommandResult result;
233 auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800234 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800235 LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
236 << SlotName(slot) << ": "
Yifan Hong7b514b42016-12-21 13:02:00 -0800237 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400238 return false;
239 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800240 if (!result.success) {
241 LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
242 << " as unbootable: " << result.errMsg.c_str();
243 }
244 return result.success;
Alex Deymob17327c2015-09-04 10:29:00 -0700245}
246
Alex Deymo31d95ac2015-09-17 11:56:18 -0700247bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800248 CommandResult result;
249 auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800250 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800251 LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
Yifan Hong7b514b42016-12-21 13:02:00 -0800252 << ": " << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800253 return false;
Alex Deymo29dcbf32016-10-06 13:33:20 -0700254 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800255 if (!result.success) {
256 LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
257 << ": " << result.errMsg.c_str();
258 }
259 return result.success;
Alex Deymo31d95ac2015-09-17 11:56:18 -0700260}
261
Alex Deymoaa26f622015-09-16 18:21:27 -0700262bool BootControlAndroid::MarkBootSuccessfulAsync(
263 base::Callback<void(bool)> callback) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800264 CommandResult result;
265 auto ret = module_->markBootSuccessful(StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800266 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800267 LOG(ERROR) << "Unable to call MarkBootSuccessful: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800268 << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800269 return false;
270 }
271 if (!result.success) {
272 LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str();
Alex Deymoaa26f622015-09-16 18:21:27 -0700273 }
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700274 return brillo::MessageLoop::current()->PostTask(
Connor O'Briencee6ad92016-11-21 13:53:52 -0800275 FROM_HERE, base::Bind(callback, result.success)) !=
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700276 brillo::MessageLoop::kTaskIdNull;
Alex Deymoaa26f622015-09-16 18:21:27 -0700277}
278
Yifan Hong537802d2018-08-15 13:15:42 -0700279namespace {
280
Yifan Hongd4db07e2018-10-18 17:46:27 -0700281bool InitPartitionMetadataInternal(
282 DynamicPartitionControlInterface* dynamic_control,
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800283 const string& source_device,
284 const string& target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700285 Slot source_slot,
286 Slot target_slot,
287 const string& target_suffix,
288 const PartitionMetadata& partition_metadata) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800289 auto builder = dynamic_control->LoadMetadataBuilder(
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800290 source_device, source_slot, target_slot);
Yifan Hongd4db07e2018-10-18 17:46:27 -0700291 if (builder == nullptr) {
292 // TODO(elsk): allow reconstructing metadata from partition_metadata
293 // in recovery sideload.
294 LOG(ERROR) << "No metadata at "
295 << BootControlInterface::SlotName(source_slot);
Yifan Hong537802d2018-08-15 13:15:42 -0700296 return false;
297 }
298
Yifan Hongd4db07e2018-10-18 17:46:27 -0700299 std::vector<string> groups = builder->ListGroups();
300 for (const auto& group_name : groups) {
301 if (base::EndsWith(
302 group_name, target_suffix, base::CompareCase::SENSITIVE)) {
303 LOG(INFO) << "Removing group " << group_name;
304 builder->RemoveGroupAndPartitions(group_name);
305 }
306 }
307
308 uint64_t total_size = 0;
309 for (const auto& group : partition_metadata.groups) {
310 total_size += group.size;
311 }
312
313 if (total_size > (builder->AllocatableSpace() / 2)) {
314 LOG(ERROR)
315 << "The maximum size of all groups with suffix " << target_suffix
316 << " (" << total_size
317 << ") has exceeded half of allocatable space for dynamic partitions "
318 << (builder->AllocatableSpace() / 2) << ".";
Yifan Hong537802d2018-08-15 13:15:42 -0700319 return false;
320 }
321
Yifan Hongd4db07e2018-10-18 17:46:27 -0700322 for (const auto& group : partition_metadata.groups) {
323 auto group_name_suffix = group.name + target_suffix;
324 if (!builder->AddGroup(group_name_suffix, group.size)) {
325 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
326 << group.size;
327 return false;
328 }
329 LOG(INFO) << "Added group " << group_name_suffix << " with size "
330 << group.size;
331
332 for (const auto& partition : group.partitions) {
333 auto parition_name_suffix = partition.name + target_suffix;
334 Partition* p = builder->AddPartition(
335 parition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
336 if (!p) {
337 LOG(ERROR) << "Cannot add partition " << parition_name_suffix
338 << " to group " << group_name_suffix;
339 return false;
340 }
341 if (!builder->ResizePartition(p, partition.size)) {
342 LOG(ERROR) << "Cannot resize partition " << parition_name_suffix
343 << " to size " << partition.size << ". Not enough space?";
344 return false;
345 }
346 LOG(INFO) << "Added partition " << parition_name_suffix << " to group "
347 << group_name_suffix << " with size " << partition.size;
348 }
Yifan Hong537802d2018-08-15 13:15:42 -0700349 }
350
Yifan Hongd4db07e2018-10-18 17:46:27 -0700351 return dynamic_control->StoreMetadata(
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800352 target_device, builder.get(), target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -0700353}
354
Yifan Hongaf65ef12018-10-29 11:09:06 -0700355// Unmap all partitions, and remap partitions as writable.
Yifan Hongd4db07e2018-10-18 17:46:27 -0700356bool Remap(DynamicPartitionControlInterface* dynamic_control,
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800357 const string& target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700358 Slot target_slot,
359 const string& target_suffix,
360 const PartitionMetadata& partition_metadata) {
361 for (const auto& group : partition_metadata.groups) {
362 for (const auto& partition : group.partitions) {
363 if (!dynamic_control->UnmapPartitionOnDeviceMapper(
364 partition.name + target_suffix, true /* wait */)) {
365 return false;
366 }
367 if (partition.size == 0) {
368 continue;
369 }
370 string map_path;
371 if (!dynamic_control->MapPartitionOnDeviceMapper(
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800372 target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700373 partition.name + target_suffix,
374 target_slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700375 true /* force writable */,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700376 &map_path)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700377 return false;
378 }
379 }
Yifan Hong537802d2018-08-15 13:15:42 -0700380 }
381 return true;
382}
383
Yifan Hong537802d2018-08-15 13:15:42 -0700384} // namespace
385
386bool BootControlAndroid::InitPartitionMetadata(
Yifan Hong6b0a9842018-10-16 16:54:18 -0700387 Slot target_slot, const PartitionMetadata& partition_metadata) {
Yifan Hong537802d2018-08-15 13:15:42 -0700388 if (!dynamic_control_->IsDynamicPartitionsEnabled()) {
389 return true;
390 }
391
392 string device_dir_str;
393 if (!dynamic_control_->GetDeviceDir(&device_dir_str)) {
394 return false;
395 }
396 base::FilePath device_dir(device_dir_str);
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800397 string target_device =
398 device_dir.Append(fs_mgr_get_super_partition_name(target_slot)).value();
Yifan Hong537802d2018-08-15 13:15:42 -0700399
400 Slot current_slot = GetCurrentSlot();
401 if (target_slot == current_slot) {
402 LOG(ERROR) << "Cannot call InitPartitionMetadata on current slot.";
403 return false;
404 }
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800405 string source_device =
406 device_dir.Append(fs_mgr_get_super_partition_name(current_slot)).value();
Yifan Hong537802d2018-08-15 13:15:42 -0700407
Yifan Hong537802d2018-08-15 13:15:42 -0700408 string target_suffix;
409 if (!GetSuffix(target_slot, &target_suffix)) {
410 return false;
411 }
412
Yifan Hongd4db07e2018-10-18 17:46:27 -0700413 if (!InitPartitionMetadataInternal(dynamic_control_.get(),
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800414 source_device,
415 target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700416 current_slot,
417 target_slot,
418 target_suffix,
419 partition_metadata)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700420 return false;
421 }
422
Yifan Hongd4db07e2018-10-18 17:46:27 -0700423 if (!Remap(dynamic_control_.get(),
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800424 target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700425 target_slot,
426 target_suffix,
427 partition_metadata)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700428 return false;
429 }
430
Yifan Hong537802d2018-08-15 13:15:42 -0700431 return true;
432}
433
Alex Deymob17327c2015-09-04 10:29:00 -0700434} // namespace chromeos_update_engine