• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "device_info.h"
16 
17 #include <android-base/logging.h>
18 #include <fs_mgr.h>
19 #include <fs_mgr_overlayfs.h>
20 
21 namespace android {
22 namespace snapshot {
23 
24 #ifdef LIBSNAPSHOT_USE_HAL
25 using android::hardware::boot::V1_0::BoolResult;
26 using android::hardware::boot::V1_0::CommandResult;
27 #endif
28 
29 using namespace std::string_literals;
30 
31 #ifdef __ANDROID_RECOVERY__
32 constexpr bool kIsRecovery = true;
33 #else
34 constexpr bool kIsRecovery = false;
35 #endif
36 
GetGsidDir() const37 std::string DeviceInfo::GetGsidDir() const {
38     return "ota"s;
39 }
40 
GetMetadataDir() const41 std::string DeviceInfo::GetMetadataDir() const {
42     return "/metadata/ota"s;
43 }
44 
GetSlotSuffix() const45 std::string DeviceInfo::GetSlotSuffix() const {
46     return fs_mgr_get_slot_suffix();
47 }
48 
GetOtherSlotSuffix() const49 std::string DeviceInfo::GetOtherSlotSuffix() const {
50     return fs_mgr_get_other_slot_suffix();
51 }
52 
GetPartitionOpener() const53 const android::fs_mgr::IPartitionOpener& DeviceInfo::GetPartitionOpener() const {
54     return opener_;
55 }
56 
GetSuperDevice(uint32_t slot) const57 std::string DeviceInfo::GetSuperDevice(uint32_t slot) const {
58     return fs_mgr_get_super_partition_name(slot);
59 }
60 
IsOverlayfsSetup() const61 bool DeviceInfo::IsOverlayfsSetup() const {
62     return fs_mgr_overlayfs_is_setup();
63 }
64 
65 #ifdef LIBSNAPSHOT_USE_HAL
EnsureBootHal()66 bool DeviceInfo::EnsureBootHal() {
67     if (!boot_control_) {
68         auto hal = android::hardware::boot::V1_0::IBootControl::getService();
69         if (!hal) {
70             LOG(ERROR) << "Could not find IBootControl HAL";
71             return false;
72         }
73         boot_control_ = android::hardware::boot::V1_1::IBootControl::castFrom(hal);
74         if (!boot_control_) {
75             LOG(ERROR) << "Could not find IBootControl 1.1 HAL";
76             return false;
77         }
78     }
79     return true;
80 }
81 #endif
82 
SetBootControlMergeStatus(MergeStatus status)83 bool DeviceInfo::SetBootControlMergeStatus([[maybe_unused]] MergeStatus status) {
84 #ifdef LIBSNAPSHOT_USE_HAL
85     if (!EnsureBootHal()) {
86         return false;
87     }
88     if (!boot_control_->setSnapshotMergeStatus(status)) {
89         LOG(ERROR) << "Unable to set the snapshot merge status";
90         return false;
91     }
92     return true;
93 #else
94     LOG(ERROR) << "HAL support not enabled.";
95     return false;
96 #endif
97 }
98 
IsRecovery() const99 bool DeviceInfo::IsRecovery() const {
100     return kIsRecovery;
101 }
102 
SetSlotAsUnbootable(unsigned int slot)103 bool DeviceInfo::SetSlotAsUnbootable([[maybe_unused]] unsigned int slot) {
104 #ifdef LIBSNAPSHOT_USE_HAL
105     if (!EnsureBootHal()) {
106         return false;
107     }
108 
109     CommandResult result = {};
110     auto cb = [&](CommandResult r) -> void { result = r; };
111     boot_control_->setSlotAsUnbootable(slot, cb);
112     if (!result.success) {
113         LOG(ERROR) << "Error setting slot " << slot << " unbootable: " << result.errMsg;
114         return false;
115     }
116     return true;
117 #else
118     LOG(ERROR) << "HAL support not enabled.";
119     return false;
120 #endif
121 }
122 
123 }  // namespace snapshot
124 }  // namespace android
125