blob: b820dedacabf4b898a66d5beb71213a57fcb5a23 [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>
Sen Jiangd944faa2018-08-22 18:46:39 -070025#include <bootloader_message/bootloader_message.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070026#include <brillo/message_loops/message_loop.h>
David Andersond63cb3c2018-10-01 14:15:00 -070027#include <fs_mgr.h>
Mark Salyzyn62b42c82018-12-05 13:33:17 -080028#include <fs_mgr_overlayfs.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::hardware::hidl_string;
Connor O'Briencee6ad92016-11-21 13:53:52 -080037using android::hardware::Return;
38using android::hardware::boot::V1_0::BoolResult;
39using android::hardware::boot::V1_0::CommandResult;
40using android::hardware::boot::V1_0::IBootControl;
Yifan Hong537802d2018-08-15 13:15:42 -070041using Slot = chromeos_update_engine::BootControlInterface::Slot;
Yifan Hong6b0a9842018-10-16 16:54:18 -070042using PartitionMetadata =
43 chromeos_update_engine::BootControlInterface::PartitionMetadata;
Connor O'Briencee6ad92016-11-21 13:53:52 -080044
45namespace {
Yifan Hong537802d2018-08-15 13:15:42 -070046
Connor O'Briencee6ad92016-11-21 13:53:52 -080047auto StoreResultCallback(CommandResult* dest) {
48 return [dest](const CommandResult& result) { *dest = result; };
49}
50} // namespace
Alex Deymo44348e02016-07-29 16:22:26 -070051
Alex Deymob17327c2015-09-04 10:29:00 -070052namespace chromeos_update_engine {
53
54namespace boot_control {
55
56// Factory defined in boot_control.h.
57std::unique_ptr<BootControlInterface> CreateBootControl() {
Yifan Hong537802d2018-08-15 13:15:42 -070058 auto boot_control = std::make_unique<BootControlAndroid>();
David Zeuthen753fadc2015-09-15 16:34:09 -040059 if (!boot_control->Init()) {
60 return nullptr;
61 }
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070062 return std::move(boot_control);
Alex Deymob17327c2015-09-04 10:29:00 -070063}
64
65} // namespace boot_control
66
David Zeuthen753fadc2015-09-15 16:34:09 -040067bool BootControlAndroid::Init() {
Chris Phoenixafde8e82017-01-17 23:14:58 -080068 module_ = IBootControl::getService();
Connor O'Briencee6ad92016-11-21 13:53:52 -080069 if (module_ == nullptr) {
Steven Moreland927e00d2017-01-04 12:58:40 -080070 LOG(ERROR) << "Error getting bootctrl HIDL module.";
David Zeuthen753fadc2015-09-15 16:34:09 -040071 return false;
72 }
73
Steven Moreland927e00d2017-01-04 12:58:40 -080074 LOG(INFO) << "Loaded boot control hidl hal.";
David Zeuthen753fadc2015-09-15 16:34:09 -040075
Yifan Hong537802d2018-08-15 13:15:42 -070076 dynamic_control_ = std::make_unique<DynamicPartitionControlAndroid>();
77
David Zeuthen753fadc2015-09-15 16:34:09 -040078 return true;
79}
Alex Deymob17327c2015-09-04 10:29:00 -070080
Yifan Hong537802d2018-08-15 13:15:42 -070081void BootControlAndroid::Cleanup() {
82 dynamic_control_->Cleanup();
83}
84
Alex Deymob17327c2015-09-04 10:29:00 -070085unsigned int BootControlAndroid::GetNumSlots() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080086 return module_->getNumberSlots();
Alex Deymob17327c2015-09-04 10:29:00 -070087}
88
89BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080090 return module_->getCurrentSlot();
Alex Deymob17327c2015-09-04 10:29:00 -070091}
92
Yifan Hong537802d2018-08-15 13:15:42 -070093bool BootControlAndroid::GetSuffix(Slot slot, string* suffix) const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080094 auto store_suffix_cb = [&suffix](hidl_string cb_suffix) {
Yifan Hong537802d2018-08-15 13:15:42 -070095 *suffix = cb_suffix.c_str();
Connor O'Briencee6ad92016-11-21 13:53:52 -080096 };
97 Return<void> ret = module_->getSuffix(slot, store_suffix_cb);
98
Yifan Hong7b514b42016-12-21 13:02:00 -080099 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700100 LOG(ERROR) << "boot_control impl returned no suffix for slot "
101 << SlotName(slot);
David Zeuthen753fadc2015-09-15 16:34:09 -0400102 return false;
103 }
Yifan Hong537802d2018-08-15 13:15:42 -0700104 return true;
105}
106
Yifan Hong124a4602018-11-14 13:17:01 -0800107bool BootControlAndroid::IsSuperBlockDevice(
108 const base::FilePath& device_dir,
109 Slot slot,
110 const string& partition_name_suffix) const {
111 string source_device =
112 device_dir.Append(fs_mgr_get_super_partition_name(slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700113 auto source_metadata =
114 dynamic_control_->LoadMetadataBuilder(source_device, slot);
Yifan Hong124a4602018-11-14 13:17:01 -0800115 return source_metadata->HasBlockDevice(partition_name_suffix);
116}
Yifan Hongae04e192018-10-29 11:00:28 -0700117
Yifan Hong124a4602018-11-14 13:17:01 -0800118BootControlAndroid::DynamicPartitionDeviceStatus
119BootControlAndroid::GetDynamicPartitionDevice(
120 const base::FilePath& device_dir,
Yifan Hongae04e192018-10-29 11:00:28 -0700121 const string& partition_name_suffix,
122 Slot slot,
Yifan Hong124a4602018-11-14 13:17:01 -0800123 string* device) const {
Yifan Hong124a4602018-11-14 13:17:01 -0800124 string super_device =
125 device_dir.Append(fs_mgr_get_super_partition_name(slot)).value();
126
Yifan Hong012508e2019-07-22 18:30:40 -0700127 auto builder = dynamic_control_->LoadMetadataBuilder(super_device, slot);
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
Yifan Hong8546a712019-03-28 14:42:53 -0700135 Slot current_slot = GetCurrentSlot();
Yifan Hongae04e192018-10-29 11:00:28 -0700136 if (builder->FindPartition(partition_name_suffix) == nullptr) {
137 LOG(INFO) << partition_name_suffix
138 << " is not in super partition metadata.";
Yifan Hong124a4602018-11-14 13:17:01 -0800139
Yifan Hong124a4602018-11-14 13:17:01 -0800140 if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) {
141 LOG(ERROR) << "The static partition " << partition_name_suffix
142 << " is a block device for current metadata ("
143 << fs_mgr_get_super_partition_name(current_slot) << ", slot "
144 << BootControlInterface::SlotName(current_slot)
145 << "). It cannot be used as a logical partition.";
146 return DynamicPartitionDeviceStatus::ERROR;
147 }
148
Yifan Hongae04e192018-10-29 11:00:28 -0700149 return DynamicPartitionDeviceStatus::TRY_STATIC;
150 }
151
Yifan Hong8546a712019-03-28 14:42:53 -0700152 if (slot == current_slot) {
153 if (dynamic_control_->GetState(partition_name_suffix) !=
154 DmDeviceState::ACTIVE) {
155 LOG(WARNING) << partition_name_suffix << " is at current slot but it is "
156 << "not mapped. Now try to map it.";
157 } else {
158 if (dynamic_control_->GetDmDevicePathByName(partition_name_suffix,
159 device)) {
160 LOG(INFO) << partition_name_suffix
161 << " is mapped on device mapper: " << *device;
162 return DynamicPartitionDeviceStatus::SUCCESS;
163 }
164 LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown.";
165 return DynamicPartitionDeviceStatus::ERROR;
Yifan Hongae04e192018-10-29 11:00:28 -0700166 }
Yifan Hongae04e192018-10-29 11:00:28 -0700167 }
168
Yifan Hong8546a712019-03-28 14:42:53 -0700169 bool force_writable = slot != current_slot;
170 if (dynamic_control_->MapPartitionOnDeviceMapper(
171 super_device, partition_name_suffix, slot, force_writable, device)) {
172 return DynamicPartitionDeviceStatus::SUCCESS;
Yifan Hongae04e192018-10-29 11:00:28 -0700173 }
Yifan Hongae04e192018-10-29 11:00:28 -0700174 return DynamicPartitionDeviceStatus::ERROR;
175}
Yifan Hongae04e192018-10-29 11:00:28 -0700176
Yifan Hong537802d2018-08-15 13:15:42 -0700177bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
178 Slot slot,
179 string* device) const {
180 string suffix;
181 if (!GetSuffix(slot, &suffix)) {
182 return false;
183 }
Yifan Hongae04e192018-10-29 11:00:28 -0700184 const string partition_name_suffix = partition_name + suffix;
Yifan Hong537802d2018-08-15 13:15:42 -0700185
186 string device_dir_str;
187 if (!dynamic_control_->GetDeviceDir(&device_dir_str)) {
188 return false;
189 }
Yifan Hongae04e192018-10-29 11:00:28 -0700190 base::FilePath device_dir(device_dir_str);
David Zeuthen753fadc2015-09-15 16:34:09 -0400191
Yifan Hong1d9077f2018-12-07 12:09:37 -0800192 // When looking up target partition devices, treat them as static if the
193 // current payload doesn't encode them as dynamic partitions. This may happen
194 // when applying a retrofit update on top of a dynamic-partitions-enabled
195 // build.
196 if (dynamic_control_->IsDynamicPartitionsEnabled() &&
197 (slot == GetCurrentSlot() || is_target_dynamic_)) {
198 switch (GetDynamicPartitionDevice(
199 device_dir, partition_name_suffix, slot, device)) {
200 case DynamicPartitionDeviceStatus::SUCCESS:
201 return true;
202 case DynamicPartitionDeviceStatus::TRY_STATIC:
203 break;
204 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
205 default:
206 return false;
207 }
Yifan Hongae04e192018-10-29 11:00:28 -0700208 }
209
210 base::FilePath path = device_dir.Append(partition_name_suffix);
Yifan Hong537802d2018-08-15 13:15:42 -0700211 if (!dynamic_control_->DeviceExists(path.value())) {
David Zeuthen753fadc2015-09-15 16:34:09 -0400212 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
213 return false;
214 }
215
216 *device = path.value();
217 return true;
Alex Deymob17327c2015-09-04 10:29:00 -0700218}
219
220bool BootControlAndroid::IsSlotBootable(Slot slot) const {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800221 Return<BoolResult> ret = module_->isSlotBootable(slot);
Yifan Hong7b514b42016-12-21 13:02:00 -0800222 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700223 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800224 << " is bootable: " << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400225 return false;
226 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800227 if (ret == BoolResult::INVALID_SLOT) {
228 LOG(ERROR) << "Invalid slot: " << SlotName(slot);
229 return false;
230 }
231 return ret == BoolResult::TRUE;
Alex Deymob17327c2015-09-04 10:29:00 -0700232}
233
234bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800235 CommandResult result;
236 auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800237 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800238 LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800239 << SlotName(slot) << ": " << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400240 return false;
241 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800242 if (!result.success) {
243 LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
244 << " as unbootable: " << result.errMsg.c_str();
245 }
246 return result.success;
Alex Deymob17327c2015-09-04 10:29:00 -0700247}
248
Alex Deymo31d95ac2015-09-17 11:56:18 -0700249bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800250 CommandResult result;
251 auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800252 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800253 LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
Yifan Hong7b514b42016-12-21 13:02:00 -0800254 << ": " << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800255 return false;
Alex Deymo29dcbf32016-10-06 13:33:20 -0700256 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800257 if (!result.success) {
258 LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
259 << ": " << result.errMsg.c_str();
260 }
261 return result.success;
Alex Deymo31d95ac2015-09-17 11:56:18 -0700262}
263
Alex Deymoaa26f622015-09-16 18:21:27 -0700264bool BootControlAndroid::MarkBootSuccessfulAsync(
265 base::Callback<void(bool)> callback) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800266 CommandResult result;
267 auto ret = module_->markBootSuccessful(StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800268 if (!ret.isOk()) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800269 LOG(ERROR) << "Unable to call MarkBootSuccessful: " << 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 -0700280bool BootControlAndroid::InitPartitionMetadata(
Tao Bao3406c772019-01-02 15:34:35 -0800281 Slot target_slot,
282 const PartitionMetadata& partition_metadata,
283 bool update_metadata) {
Mark Salyzyn62b42c82018-12-05 13:33:17 -0800284 if (fs_mgr_overlayfs_is_setup()) {
285 // Non DAP devices can use overlayfs as well.
286 LOG(WARNING)
287 << "overlayfs overrides are active and can interfere with our "
288 "resources.\n"
289 << "run adb enable-verity to deactivate if required and try again.";
290 }
Yifan Hong537802d2018-08-15 13:15:42 -0700291 if (!dynamic_control_->IsDynamicPartitionsEnabled()) {
292 return true;
293 }
294
Yifan Hong1d9077f2018-12-07 12:09:37 -0800295 auto source_slot = GetCurrentSlot();
296 if (target_slot == source_slot) {
Yifan Hong537802d2018-08-15 13:15:42 -0700297 LOG(ERROR) << "Cannot call InitPartitionMetadata on current slot.";
298 return false;
299 }
300
Yifan Hong1d9077f2018-12-07 12:09:37 -0800301 // Although the current build supports dynamic partitions, the given payload
302 // doesn't use it for target partitions. This could happen when applying a
303 // retrofit update. Skip updating the partition metadata for the target slot.
304 is_target_dynamic_ = !partition_metadata.groups.empty();
305 if (!is_target_dynamic_) {
306 return true;
307 }
308
Tao Bao3406c772019-01-02 15:34:35 -0800309 if (!update_metadata) {
310 return true;
311 }
312
Yifan Hong012508e2019-07-22 18:30:40 -0700313 return dynamic_control_->PreparePartitionsForUpdate(
314 source_slot, target_slot, partition_metadata);
Yifan Hong537802d2018-08-15 13:15:42 -0700315}
316
Alex Deymob17327c2015-09-04 10:29:00 -0700317} // namespace chromeos_update_engine