• Home
  • History
  • Annotate
  • 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 #include <libfiemap/image_manager.h>
21 
22 namespace android {
23 namespace snapshot {
24 
25 #ifdef LIBSNAPSHOT_USE_HAL
26 using android::hardware::boot::V1_0::BoolResult;
27 using android::hardware::boot::V1_0::CommandResult;
28 #endif
29 
30 using namespace std::chrono_literals;
31 using namespace std::string_literals;
32 
33 #ifdef __ANDROID_RECOVERY__
34 constexpr bool kIsRecovery = true;
35 #else
36 constexpr bool kIsRecovery = false;
37 #endif
38 
GetMetadataDir() const39 std::string DeviceInfo::GetMetadataDir() const {
40     return "/metadata/ota"s;
41 }
42 
GetSlotSuffix() const43 std::string DeviceInfo::GetSlotSuffix() const {
44     return fs_mgr_get_slot_suffix();
45 }
46 
GetOtherSlotSuffix() const47 std::string DeviceInfo::GetOtherSlotSuffix() const {
48     return fs_mgr_get_other_slot_suffix();
49 }
50 
GetPartitionOpener() const51 const android::fs_mgr::IPartitionOpener& DeviceInfo::GetPartitionOpener() const {
52     return opener_;
53 }
54 
GetSuperDevice(uint32_t slot) const55 std::string DeviceInfo::GetSuperDevice(uint32_t slot) const {
56     return fs_mgr_get_super_partition_name(slot);
57 }
58 
IsOverlayfsSetup() const59 bool DeviceInfo::IsOverlayfsSetup() const {
60     return fs_mgr_overlayfs_is_setup();
61 }
62 
63 #ifdef LIBSNAPSHOT_USE_HAL
EnsureBootHal()64 bool DeviceInfo::EnsureBootHal() {
65     if (!boot_control_) {
66         auto hal = android::hardware::boot::V1_0::IBootControl::getService();
67         if (!hal) {
68             LOG(ERROR) << "Could not find IBootControl HAL";
69             return false;
70         }
71         boot_control_ = android::hardware::boot::V1_1::IBootControl::castFrom(hal);
72         if (!boot_control_) {
73             LOG(ERROR) << "Could not find IBootControl 1.1 HAL";
74             return false;
75         }
76     }
77     return true;
78 }
79 #endif
80 
SetBootControlMergeStatus(MergeStatus status)81 bool DeviceInfo::SetBootControlMergeStatus([[maybe_unused]] MergeStatus status) {
82 #ifdef LIBSNAPSHOT_USE_HAL
83     if (!EnsureBootHal()) {
84         return false;
85     }
86     if (!boot_control_->setSnapshotMergeStatus(status)) {
87         LOG(ERROR) << "Unable to set the snapshot merge status";
88         return false;
89     }
90     return true;
91 #else
92     LOG(ERROR) << "HAL support not enabled.";
93     return false;
94 #endif
95 }
96 
IsRecovery() const97 bool DeviceInfo::IsRecovery() const {
98     return kIsRecovery;
99 }
100 
IsFirstStageInit() const101 bool DeviceInfo::IsFirstStageInit() const {
102     return first_stage_init_;
103 }
104 
SetSlotAsUnbootable(unsigned int slot)105 bool DeviceInfo::SetSlotAsUnbootable([[maybe_unused]] unsigned int slot) {
106 #ifdef LIBSNAPSHOT_USE_HAL
107     if (!EnsureBootHal()) {
108         return false;
109     }
110 
111     CommandResult result = {};
112     auto cb = [&](CommandResult r) -> void { result = r; };
113     boot_control_->setSlotAsUnbootable(slot, cb);
114     if (!result.success) {
115         LOG(ERROR) << "Error setting slot " << slot << " unbootable: " << result.errMsg;
116         return false;
117     }
118     return true;
119 #else
120     LOG(ERROR) << "HAL support not enabled.";
121     return false;
122 #endif
123 }
124 
OpenImageManager() const125 std::unique_ptr<android::fiemap::IImageManager> DeviceInfo::OpenImageManager() const {
126     return IDeviceInfo::OpenImageManager("ota");
127 }
128 
OpenImageManager(const std::string & gsid_dir) const129 std::unique_ptr<android::fiemap::IImageManager> ISnapshotManager::IDeviceInfo::OpenImageManager(
130         const std::string& gsid_dir) const {
131     if (IsRecovery() || IsFirstStageInit()) {
132         android::fiemap::ImageManager::DeviceInfo device_info = {
133                 .is_recovery = {IsRecovery()},
134         };
135         return android::fiemap::ImageManager::Open(gsid_dir, device_info);
136     } else {
137         // For now, use a preset timeout.
138         return android::fiemap::IImageManager::Open(gsid_dir, 15000ms);
139     }
140 }
141 
142 }  // namespace snapshot
143 }  // namespace android
144