blob: 2d2b77eae8fff8720b0dab0b758dbd98d3a5db05 [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 Hong124a4602018-11-14 13:17:01 -0800108bool BootControlAndroid::IsSuperBlockDevice(
109 const base::FilePath& device_dir,
110 Slot slot,
111 const string& partition_name_suffix) const {
112 string source_device =
113 device_dir.Append(fs_mgr_get_super_partition_name(slot)).value();
114 auto source_metadata = dynamic_control_->LoadMetadataBuilder(
115 source_device, slot, BootControlInterface::kInvalidSlot);
116 return source_metadata->HasBlockDevice(partition_name_suffix);
117}
Yifan Hongae04e192018-10-29 11:00:28 -0700118
Yifan Hong124a4602018-11-14 13:17:01 -0800119BootControlAndroid::DynamicPartitionDeviceStatus
120BootControlAndroid::GetDynamicPartitionDevice(
121 const base::FilePath& device_dir,
Yifan Hongae04e192018-10-29 11:00:28 -0700122 const string& partition_name_suffix,
123 Slot slot,
Yifan Hong124a4602018-11-14 13:17:01 -0800124 string* device) const {
125 if (!dynamic_control_->IsDynamicPartitionsEnabled()) {
Yifan Hong8cc1e9e2018-11-14 13:52:12 -0800126 return DynamicPartitionDeviceStatus::TRY_STATIC;
127 }
128
Yifan Hong124a4602018-11-14 13:17:01 -0800129 string super_device =
130 device_dir.Append(fs_mgr_get_super_partition_name(slot)).value();
131
132 auto builder = dynamic_control_->LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800133 super_device, slot, BootControlInterface::kInvalidSlot);
Yifan Hongae04e192018-10-29 11:00:28 -0700134
135 if (builder == nullptr) {
Yifan Hongae04e192018-10-29 11:00:28 -0700136 LOG(ERROR) << "No metadata in slot "
137 << BootControlInterface::SlotName(slot);
138 return DynamicPartitionDeviceStatus::ERROR;
139 }
140
141 if (builder->FindPartition(partition_name_suffix) == nullptr) {
142 LOG(INFO) << partition_name_suffix
143 << " is not in super partition metadata.";
Yifan Hong124a4602018-11-14 13:17:01 -0800144
145 Slot current_slot = GetCurrentSlot();
146 if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) {
147 LOG(ERROR) << "The static partition " << partition_name_suffix
148 << " is a block device for current metadata ("
149 << fs_mgr_get_super_partition_name(current_slot) << ", slot "
150 << BootControlInterface::SlotName(current_slot)
151 << "). It cannot be used as a logical partition.";
152 return DynamicPartitionDeviceStatus::ERROR;
153 }
154
Yifan Hongae04e192018-10-29 11:00:28 -0700155 return DynamicPartitionDeviceStatus::TRY_STATIC;
156 }
157
Yifan Hong124a4602018-11-14 13:17:01 -0800158 DmDeviceState state = dynamic_control_->GetState(partition_name_suffix);
Yifan Hongae04e192018-10-29 11:00:28 -0700159
160 if (state == DmDeviceState::ACTIVE) {
Yifan Hong124a4602018-11-14 13:17:01 -0800161 if (dynamic_control_->GetDmDevicePathByName(partition_name_suffix,
162 device)) {
Yifan Hongae04e192018-10-29 11:00:28 -0700163 LOG(INFO) << partition_name_suffix
164 << " is mapped on device mapper: " << *device;
165 return DynamicPartitionDeviceStatus::SUCCESS;
166 }
167 LOG(ERROR) << partition_name_suffix << " is mapped but path is unknown.";
168 return DynamicPartitionDeviceStatus::ERROR;
169 }
170
171 // DeltaPerformer calls InitPartitionMetadata before calling
172 // InstallPlan::LoadPartitionsFromSlots. After InitPartitionMetadata,
173 // the target partition must be re-mapped with force_writable == true.
174 // Hence, if it is not mapped, we assume it is a source partition and
175 // map it without force_writable.
176 if (state == DmDeviceState::INVALID) {
Yifan Hong124a4602018-11-14 13:17:01 -0800177 if (dynamic_control_->MapPartitionOnDeviceMapper(super_device,
178 partition_name_suffix,
179 slot,
180 false /* force_writable */,
181 device)) {
Yifan Hongae04e192018-10-29 11:00:28 -0700182 return DynamicPartitionDeviceStatus::SUCCESS;
183 }
184 return DynamicPartitionDeviceStatus::ERROR;
185 }
186
187 LOG(ERROR) << partition_name_suffix
188 << " is mapped on device mapper but state is unknown: "
189 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
190 return DynamicPartitionDeviceStatus::ERROR;
191}
Yifan Hongae04e192018-10-29 11:00:28 -0700192
Yifan Hong537802d2018-08-15 13:15:42 -0700193bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
194 Slot slot,
195 string* device) const {
196 string suffix;
197 if (!GetSuffix(slot, &suffix)) {
198 return false;
199 }
Yifan Hongae04e192018-10-29 11:00:28 -0700200 const string partition_name_suffix = partition_name + suffix;
Yifan Hong537802d2018-08-15 13:15:42 -0700201
202 string device_dir_str;
203 if (!dynamic_control_->GetDeviceDir(&device_dir_str)) {
204 return false;
205 }
Yifan Hongae04e192018-10-29 11:00:28 -0700206 base::FilePath device_dir(device_dir_str);
David Zeuthen753fadc2015-09-15 16:34:09 -0400207
Yifan Hong124a4602018-11-14 13:17:01 -0800208 switch (GetDynamicPartitionDevice(
209 device_dir, partition_name_suffix, slot, device)) {
Yifan Hongae04e192018-10-29 11:00:28 -0700210 case DynamicPartitionDeviceStatus::SUCCESS:
211 return true;
212 case DynamicPartitionDeviceStatus::TRY_STATIC:
213 break;
214 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
215 default:
216 return false;
217 }
218
219 base::FilePath path = device_dir.Append(partition_name_suffix);
Yifan Hong537802d2018-08-15 13:15:42 -0700220 if (!dynamic_control_->DeviceExists(path.value())) {
David Zeuthen753fadc2015-09-15 16:34:09 -0400221 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
222 return false;
223 }
224
225 *device = path.value();
226 return true;
Alex Deymob17327c2015-09-04 10:29:00 -0700227}
228
229bool BootControlAndroid::IsSlotBootable(Slot slot) const {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800230 Return<BoolResult> ret = module_->isSlotBootable(slot);
Yifan Hong7b514b42016-12-21 13:02:00 -0800231 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700232 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Connor O'Briencee6ad92016-11-21 13:53:52 -0800233 << " is bootable: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800234 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400235 return false;
236 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800237 if (ret == BoolResult::INVALID_SLOT) {
238 LOG(ERROR) << "Invalid slot: " << SlotName(slot);
239 return false;
240 }
241 return ret == BoolResult::TRUE;
Alex Deymob17327c2015-09-04 10:29:00 -0700242}
243
244bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800245 CommandResult result;
246 auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800247 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800248 LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
249 << SlotName(slot) << ": "
Yifan Hong7b514b42016-12-21 13:02:00 -0800250 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400251 return false;
252 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800253 if (!result.success) {
254 LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
255 << " as unbootable: " << result.errMsg.c_str();
256 }
257 return result.success;
Alex Deymob17327c2015-09-04 10:29:00 -0700258}
259
Alex Deymo31d95ac2015-09-17 11:56:18 -0700260bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800261 CommandResult result;
262 auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800263 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800264 LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
Yifan Hong7b514b42016-12-21 13:02:00 -0800265 << ": " << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800266 return false;
Alex Deymo29dcbf32016-10-06 13:33:20 -0700267 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800268 if (!result.success) {
269 LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
270 << ": " << result.errMsg.c_str();
271 }
272 return result.success;
Alex Deymo31d95ac2015-09-17 11:56:18 -0700273}
274
Alex Deymoaa26f622015-09-16 18:21:27 -0700275bool BootControlAndroid::MarkBootSuccessfulAsync(
276 base::Callback<void(bool)> callback) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800277 CommandResult result;
278 auto ret = module_->markBootSuccessful(StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800279 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800280 LOG(ERROR) << "Unable to call MarkBootSuccessful: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800281 << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800282 return false;
283 }
284 if (!result.success) {
285 LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str();
Alex Deymoaa26f622015-09-16 18:21:27 -0700286 }
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700287 return brillo::MessageLoop::current()->PostTask(
Connor O'Briencee6ad92016-11-21 13:53:52 -0800288 FROM_HERE, base::Bind(callback, result.success)) !=
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700289 brillo::MessageLoop::kTaskIdNull;
Alex Deymoaa26f622015-09-16 18:21:27 -0700290}
291
Yifan Hong537802d2018-08-15 13:15:42 -0700292namespace {
293
Yifan Hongd4db07e2018-10-18 17:46:27 -0700294bool InitPartitionMetadataInternal(
295 DynamicPartitionControlInterface* dynamic_control,
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800296 const string& source_device,
297 const string& target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700298 Slot source_slot,
299 Slot target_slot,
300 const string& target_suffix,
301 const PartitionMetadata& partition_metadata) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800302 auto builder = dynamic_control->LoadMetadataBuilder(
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800303 source_device, source_slot, target_slot);
Yifan Hongd4db07e2018-10-18 17:46:27 -0700304 if (builder == nullptr) {
305 // TODO(elsk): allow reconstructing metadata from partition_metadata
306 // in recovery sideload.
307 LOG(ERROR) << "No metadata at "
308 << BootControlInterface::SlotName(source_slot);
Yifan Hong537802d2018-08-15 13:15:42 -0700309 return false;
310 }
311
Yifan Hongd4db07e2018-10-18 17:46:27 -0700312 std::vector<string> groups = builder->ListGroups();
313 for (const auto& group_name : groups) {
314 if (base::EndsWith(
315 group_name, target_suffix, base::CompareCase::SENSITIVE)) {
316 LOG(INFO) << "Removing group " << group_name;
317 builder->RemoveGroupAndPartitions(group_name);
318 }
319 }
320
321 uint64_t total_size = 0;
322 for (const auto& group : partition_metadata.groups) {
323 total_size += group.size;
324 }
325
Yifan Hong6e38b352018-11-19 14:12:37 -0800326 string expr;
327 uint64_t allocatable_space = builder->AllocatableSpace();
328 if (!dynamic_control->IsDynamicPartitionsRetrofit()) {
329 allocatable_space /= 2;
330 expr = "half of ";
331 }
332 if (total_size > allocatable_space) {
333 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
334 << " (" << total_size << ") has exceeded " << expr
335 << "allocatable space for dynamic partitions "
336 << allocatable_space << ".";
Yifan Hong537802d2018-08-15 13:15:42 -0700337 return false;
338 }
339
Yifan Hongd4db07e2018-10-18 17:46:27 -0700340 for (const auto& group : partition_metadata.groups) {
341 auto group_name_suffix = group.name + target_suffix;
342 if (!builder->AddGroup(group_name_suffix, group.size)) {
343 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
344 << group.size;
345 return false;
346 }
347 LOG(INFO) << "Added group " << group_name_suffix << " with size "
348 << group.size;
349
350 for (const auto& partition : group.partitions) {
Yifan Honga7897f32018-11-15 14:28:50 -0800351 auto partition_name_suffix = partition.name + target_suffix;
Yifan Hongd4db07e2018-10-18 17:46:27 -0700352 Partition* p = builder->AddPartition(
Yifan Honga7897f32018-11-15 14:28:50 -0800353 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
Yifan Hongd4db07e2018-10-18 17:46:27 -0700354 if (!p) {
Yifan Honga7897f32018-11-15 14:28:50 -0800355 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
Yifan Hongd4db07e2018-10-18 17:46:27 -0700356 << " to group " << group_name_suffix;
357 return false;
358 }
359 if (!builder->ResizePartition(p, partition.size)) {
Yifan Honga7897f32018-11-15 14:28:50 -0800360 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hongd4db07e2018-10-18 17:46:27 -0700361 << " to size " << partition.size << ". Not enough space?";
362 return false;
363 }
Yifan Honga7897f32018-11-15 14:28:50 -0800364 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hongd4db07e2018-10-18 17:46:27 -0700365 << group_name_suffix << " with size " << partition.size;
366 }
Yifan Hong537802d2018-08-15 13:15:42 -0700367 }
368
Yifan Hongd4db07e2018-10-18 17:46:27 -0700369 return dynamic_control->StoreMetadata(
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800370 target_device, builder.get(), target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -0700371}
372
Yifan Hongaf65ef12018-10-29 11:09:06 -0700373// Unmap all partitions, and remap partitions as writable.
Yifan Hongd4db07e2018-10-18 17:46:27 -0700374bool Remap(DynamicPartitionControlInterface* dynamic_control,
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800375 const string& target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700376 Slot target_slot,
377 const string& target_suffix,
378 const PartitionMetadata& partition_metadata) {
379 for (const auto& group : partition_metadata.groups) {
380 for (const auto& partition : group.partitions) {
381 if (!dynamic_control->UnmapPartitionOnDeviceMapper(
382 partition.name + target_suffix, true /* wait */)) {
383 return false;
384 }
385 if (partition.size == 0) {
386 continue;
387 }
388 string map_path;
389 if (!dynamic_control->MapPartitionOnDeviceMapper(
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800390 target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700391 partition.name + target_suffix,
392 target_slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700393 true /* force writable */,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700394 &map_path)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700395 return false;
396 }
397 }
Yifan Hong537802d2018-08-15 13:15:42 -0700398 }
399 return true;
400}
401
Yifan Hong537802d2018-08-15 13:15:42 -0700402} // namespace
403
404bool BootControlAndroid::InitPartitionMetadata(
Yifan Hong6b0a9842018-10-16 16:54:18 -0700405 Slot target_slot, const PartitionMetadata& partition_metadata) {
Yifan Hong537802d2018-08-15 13:15:42 -0700406 if (!dynamic_control_->IsDynamicPartitionsEnabled()) {
407 return true;
408 }
409
410 string device_dir_str;
411 if (!dynamic_control_->GetDeviceDir(&device_dir_str)) {
412 return false;
413 }
414 base::FilePath device_dir(device_dir_str);
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800415 string target_device =
416 device_dir.Append(fs_mgr_get_super_partition_name(target_slot)).value();
Yifan Hong537802d2018-08-15 13:15:42 -0700417
418 Slot current_slot = GetCurrentSlot();
419 if (target_slot == current_slot) {
420 LOG(ERROR) << "Cannot call InitPartitionMetadata on current slot.";
421 return false;
422 }
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800423 string source_device =
424 device_dir.Append(fs_mgr_get_super_partition_name(current_slot)).value();
Yifan Hong537802d2018-08-15 13:15:42 -0700425
Yifan Hong537802d2018-08-15 13:15:42 -0700426 string target_suffix;
427 if (!GetSuffix(target_slot, &target_suffix)) {
428 return false;
429 }
430
Yifan Hongd4db07e2018-10-18 17:46:27 -0700431 if (!InitPartitionMetadataInternal(dynamic_control_.get(),
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800432 source_device,
433 target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700434 current_slot,
435 target_slot,
436 target_suffix,
437 partition_metadata)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700438 return false;
439 }
440
Yifan Hongd4db07e2018-10-18 17:46:27 -0700441 if (!Remap(dynamic_control_.get(),
Yifan Hongb97e3ab2018-11-14 13:50:39 -0800442 target_device,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700443 target_slot,
444 target_suffix,
445 partition_metadata)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700446 return false;
447 }
448
Yifan Hong537802d2018-08-15 13:15:42 -0700449 return true;
450}
451
Alex Deymob17327c2015-09-04 10:29:00 -0700452} // namespace chromeos_update_engine