1 /*
2  * Copyright (C) 2020 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 #define LOG_TAG "android.hardware.boot@1.2-impl"
17 
18 #include <memory>
19 
20 #include <log/log.h>
21 
22 #include "BootControl.h"
23 
24 namespace android {
25 namespace hardware {
26 namespace boot {
27 namespace V1_2 {
28 namespace implementation {
29 
30 using ::android::hardware::boot::V1_0::CommandResult;
31 
Init()32 bool BootControl::Init() {
33     return impl_.Init();
34 }
35 
36 // Methods from ::android::hardware::boot::V1_0::IBootControl.
getNumberSlots()37 Return<uint32_t> BootControl::getNumberSlots() {
38     return impl_.GetNumberSlots();
39 }
40 
getCurrentSlot()41 Return<uint32_t> BootControl::getCurrentSlot() {
42     return impl_.GetCurrentSlot();
43 }
44 
markBootSuccessful(markBootSuccessful_cb _hidl_cb)45 Return<void> BootControl::markBootSuccessful(markBootSuccessful_cb _hidl_cb) {
46     struct CommandResult cr;
47     if (impl_.MarkBootSuccessful()) {
48         cr.success = true;
49         cr.errMsg = "Success";
50     } else {
51         cr.success = false;
52         cr.errMsg = "Operation failed";
53     }
54     _hidl_cb(cr);
55     return Void();
56 }
57 
setActiveBootSlot(uint32_t slot,setActiveBootSlot_cb _hidl_cb)58 Return<void> BootControl::setActiveBootSlot(uint32_t slot, setActiveBootSlot_cb _hidl_cb) {
59     struct CommandResult cr;
60     if (impl_.SetActiveBootSlot(slot)) {
61         cr.success = true;
62         cr.errMsg = "Success";
63     } else {
64         cr.success = false;
65         cr.errMsg = "Operation failed";
66     }
67     _hidl_cb(cr);
68     return Void();
69 }
70 
setSlotAsUnbootable(uint32_t slot,setSlotAsUnbootable_cb _hidl_cb)71 Return<void> BootControl::setSlotAsUnbootable(uint32_t slot, setSlotAsUnbootable_cb _hidl_cb) {
72     struct CommandResult cr;
73     if (impl_.SetSlotAsUnbootable(slot)) {
74         cr.success = true;
75         cr.errMsg = "Success";
76     } else {
77         cr.success = false;
78         cr.errMsg = "Operation failed";
79     }
80     _hidl_cb(cr);
81     return Void();
82 }
83 
isSlotBootable(uint32_t slot)84 Return<BoolResult> BootControl::isSlotBootable(uint32_t slot) {
85     if (!impl_.IsValidSlot(slot)) {
86         return BoolResult::INVALID_SLOT;
87     }
88     return impl_.IsSlotBootable(slot) ? BoolResult::TRUE : BoolResult::FALSE;
89 }
90 
isSlotMarkedSuccessful(uint32_t slot)91 Return<BoolResult> BootControl::isSlotMarkedSuccessful(uint32_t slot) {
92     if (!impl_.IsValidSlot(slot)) {
93         return BoolResult::INVALID_SLOT;
94     }
95     return impl_.IsSlotMarkedSuccessful(slot) ? BoolResult::TRUE : BoolResult::FALSE;
96 }
97 
getSuffix(uint32_t slot,getSuffix_cb _hidl_cb)98 Return<void> BootControl::getSuffix(uint32_t slot, getSuffix_cb _hidl_cb) {
99     hidl_string ans;
100     const char* suffix = impl_.GetSuffix(slot);
101     if (suffix) {
102         ans = suffix;
103     }
104     _hidl_cb(ans);
105     return Void();
106 }
107 
108 // Methods from ::android::hardware::boot::V1_1::IBootControl.
setSnapshotMergeStatus(MergeStatus status)109 Return<bool> BootControl::setSnapshotMergeStatus(MergeStatus status) {
110     return impl_.SetSnapshotMergeStatus(status);
111 }
112 
getSnapshotMergeStatus()113 Return<MergeStatus> BootControl::getSnapshotMergeStatus() {
114     return impl_.GetSnapshotMergeStatus();
115 }
116 
117 // Methods from ::android::hardware::boot::V1_2::IBootControl.
getActiveBootSlot()118 Return<uint32_t> BootControl::getActiveBootSlot() {
119     return impl_.GetActiveBootSlot();
120 }
121 
HIDL_FETCH_IBootControl(const char *)122 IBootControl* HIDL_FETCH_IBootControl(const char* /* hal */) {
123     auto module = std::make_unique<BootControl>();
124     if (!module->Init()) {
125         ALOGE("Could not initialize BootControl module");
126         return nullptr;
127     }
128     return module.release();
129 }
130 
131 }  // namespace implementation
132 }  // namespace V1_2
133 }  // namespace boot
134 }  // namespace hardware
135 }  // namespace android
136