1 /*
2  * Copyright (C) 2016 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 <log/log.h>
19 
20 #include <hardware/boot_control.h>
21 #include <hardware/hardware.h>
22 #include "LegacyBootControl.h"
23 
24 #ifdef BOOT_CONTROL_RECOVERY
25 extern const hw_module_t HAL_MODULE_INFO_SYM;
26 #endif
27 
28 namespace android {
29 namespace hardware {
30 namespace boot {
31 namespace V1_2 {
32 namespace implementation {
33 
34 using ::android::hardware::boot::V1_0::CommandResult;
35 
BootControl(boot_control_module_t * module)36 BootControl::BootControl(boot_control_module_t *module) : mModule(module) {}
37 
38 // Methods from ::android::hardware::boot::V1_0::IBootControl follow.
getNumberSlots()39 Return<uint32_t> BootControl::getNumberSlots() {
40     return mModule->getNumberSlots(mModule);
41 }
42 
getCurrentSlot()43 Return<uint32_t> BootControl::getCurrentSlot() {
44     return mModule->getCurrentSlot(mModule);
45 }
46 
markBootSuccessful(markBootSuccessful_cb _hidl_cb)47 Return<void> BootControl::markBootSuccessful(markBootSuccessful_cb _hidl_cb) {
48     int ret = mModule->markBootSuccessful(mModule);
49     struct CommandResult cr;
50     cr.success = (ret == 0);
51     cr.errMsg = strerror(-ret);
52     _hidl_cb(cr);
53     return Void();
54 }
55 
setActiveBootSlot(uint32_t slot,setActiveBootSlot_cb _hidl_cb)56 Return<void> BootControl::setActiveBootSlot(uint32_t slot, setActiveBootSlot_cb _hidl_cb) {
57     int ret = mModule->setActiveBootSlot(mModule, slot);
58     struct CommandResult cr;
59     cr.success = (ret == 0);
60     cr.errMsg = strerror(-ret);
61     _hidl_cb(cr);
62     return Void();
63 }
64 
setSlotAsUnbootable(uint32_t slot,setSlotAsUnbootable_cb _hidl_cb)65 Return<void> BootControl::setSlotAsUnbootable(uint32_t slot, setSlotAsUnbootable_cb _hidl_cb) {
66     int ret = mModule->setSlotAsUnbootable(mModule, slot);
67     struct CommandResult cr;
68     cr.success = (ret == 0);
69     cr.errMsg = strerror(-ret);
70     _hidl_cb(cr);
71     return Void();
72 }
73 
isSlotBootable(uint32_t slot)74 Return<BoolResult> BootControl::isSlotBootable(uint32_t slot) {
75     int32_t ret = mModule->isSlotBootable(mModule, slot);
76     if (ret < 0) {
77         return BoolResult::INVALID_SLOT;
78     }
79     return ret ? BoolResult::TRUE : BoolResult::FALSE;
80 }
81 
isSlotMarkedSuccessful(uint32_t slot)82 Return<BoolResult> BootControl::isSlotMarkedSuccessful(uint32_t slot) {
83     int32_t ret = mModule->isSlotMarkedSuccessful(mModule, slot);
84     if (ret < 0) {
85         return BoolResult::INVALID_SLOT;
86     }
87     return ret ? BoolResult::TRUE : BoolResult::FALSE;
88 }
89 
getSuffix(uint32_t slot,getSuffix_cb _hidl_cb)90 Return<void> BootControl::getSuffix(uint32_t slot, getSuffix_cb _hidl_cb) {
91     hidl_string ans;
92     const char *suffix = mModule->getSuffix(mModule, slot);
93     if (suffix) {
94         ans = suffix;
95     }
96     _hidl_cb(ans);
97     return Void();
98 }
99 
100 // Methods from ::android::hardware::boot::V1_2::IBootControl follow.
getActiveBootSlot()101 Return<uint32_t> BootControl::getActiveBootSlot() {
102     auto get_active_slot = mModule->getActiveBootSlot;
103     if (!get_active_slot) {
104         ALOGE("Failed to find the implementation of getActiveBootSlot in boot"
105               " control HAL.");
106         return 0;
107     }
108     return get_active_slot(mModule);
109 }
110 
HIDL_FETCH_IBootControl(const char *)111 IBootControl *HIDL_FETCH_IBootControl(const char * /* hal */) {
112     int ret = 0;
113     boot_control_module_t *module = NULL;
114     hw_module_t **hwm = reinterpret_cast<hw_module_t **>(&module);
115     ret = hw_get_module(BOOT_CONTROL_HARDWARE_MODULE_ID, const_cast<const hw_module_t **>(hwm));
116     if (ret) {
117         ALOGE("hw_get_module %s failed: %d", BOOT_CONTROL_HARDWARE_MODULE_ID, ret);
118         return nullptr;
119     }
120     module->init(module);
121 
122     auto hal = new BootControl(module);
123     if (!hal->Init()) {
124         ALOGE("Failed to initialize boot control HAL");
125     }
126     return hal;
127 }
128 
129 }  // namespace implementation
130 }  // namespace V1_2
131 }  // namespace boot
132 }  // namespace hardware
133 }  // namespace android
134