1 /*
2  * Copyright (C) 2019 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.1-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_1 {
28 namespace implementation {
29 
30 using ::android::hardware::boot::V1_0::CommandResult;
31 
32 bool BootControl::Init() {
33     return impl_.Init();
34 }
35 
36 // Methods from ::android::hardware::boot::V1_0::IBootControl follow.
37 Return<uint32_t> BootControl::getNumberSlots() {
38     return impl_.GetNumberSlots();
39 }
40 
41 Return<uint32_t> BootControl::getCurrentSlot() {
42     return impl_.GetCurrentSlot();
43 }
44 
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 
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 
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 
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 
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 
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 Return<bool> BootControl::setSnapshotMergeStatus(MergeStatus status) {
109     return impl_.SetSnapshotMergeStatus(status);
110 }
111 
112 Return<MergeStatus> BootControl::getSnapshotMergeStatus() {
113     return impl_.GetSnapshotMergeStatus();
114 }
115 
116 IBootControl* HIDL_FETCH_IBootControl(const char* /* hal */) {
117     auto module = std::make_unique<BootControl>();
118     if (!module->Init()) {
119         ALOGE("Could not initialize BootControl module");
120         return nullptr;
121     }
122     return module.release();
123 }
124 
125 }  // namespace implementation
126 }  // namespace V1_1
127 }  // namespace boot
128 }  // namespace hardware
129 }  // namespace android
130