1 
2 /*
3  * Copyright (C) 2019 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <android-base/logging.h>
19 #include <android-base/properties.h>
20 #include <libsnapshot/snapshot.h>
21 
22 #include "recovery_ui/device.h"
23 #include "recovery_ui/ui.h"
24 #include "recovery_utils/roots.h"
25 
26 using android::snapshot::CreateResult;
27 using android::snapshot::SnapshotManager;
28 
FinishPendingSnapshotMerges(Device * device)29 bool FinishPendingSnapshotMerges(Device* device) {
30   if (!android::base::GetBoolProperty("ro.virtual_ab.enabled", false)) {
31     return true;
32   }
33 
34   RecoveryUI* ui = device->GetUI();
35   auto sm = SnapshotManager::NewForFirstStageMount();
36   if (!sm) {
37     ui->Print("Could not create SnapshotManager.\n");
38     return false;
39   }
40 
41   auto callback = [&]() -> void {
42     double progress;
43     sm->GetUpdateState(&progress);
44     ui->Print("Waiting for merge to complete: %.2f\n", progress);
45   };
46   if (!sm->HandleImminentDataWipe(callback)) {
47     ui->Print("Unable to check merge status and/or complete update merge.\n");
48     return false;
49   }
50   return true;
51 }
52 
CreateSnapshotPartitions()53 bool CreateSnapshotPartitions() {
54   if (!android::base::GetBoolProperty("ro.virtual_ab.enabled", false)) {
55     // If the device does not support Virtual A/B, there's no need to create
56     // snapshot devices.
57     return true;
58   }
59 
60   auto sm = SnapshotManager::NewForFirstStageMount();
61   if (!sm) {
62     // SnapshotManager could not be created. The device is still in a
63     // consistent state and can continue with the mounting of the existing
64     // devices, but cannot initialize snapshot devices.
65     LOG(WARNING) << "Could not create SnapshotManager";
66     return true;
67   }
68 
69   auto ret = sm->RecoveryCreateSnapshotDevices();
70   if (ret == CreateResult::ERROR) {
71     return false;
72   }
73   return true;
74 }
75