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.0-impl"
17 
18 #include <log/log.h>
19 
20 #include <hardware/hardware.h>
21 #include <hardware/boot_control.h>
22 #include "BootControl.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_0 {
32 namespace implementation {
33 
BootControl(boot_control_module_t * module)34 BootControl::BootControl(boot_control_module_t *module) : mModule(module){
35 }
36 
37 // Methods from ::android::hardware::boot::V1_0::IBootControl follow.
getNumberSlots()38 Return<uint32_t> BootControl::getNumberSlots()  {
39     return mModule->getNumberSlots(mModule);
40 }
41 
getCurrentSlot()42 Return<uint32_t> BootControl::getCurrentSlot()  {
43     return mModule->getCurrentSlot(mModule);
44 }
45 
markBootSuccessful(markBootSuccessful_cb _hidl_cb)46 Return<void> BootControl::markBootSuccessful(markBootSuccessful_cb _hidl_cb)  {
47     int ret = mModule->markBootSuccessful(mModule);
48     struct CommandResult cr;
49     cr.success = (ret == 0);
50     cr.errMsg = strerror(-ret);
51     _hidl_cb(cr);
52     return Void();
53 }
54 
setActiveBootSlot(uint32_t slot,setActiveBootSlot_cb _hidl_cb)55 Return<void> BootControl::setActiveBootSlot(uint32_t slot, setActiveBootSlot_cb _hidl_cb)  {
56     int ret = mModule->setActiveBootSlot(mModule, slot);
57     struct CommandResult cr;
58     cr.success = (ret == 0);
59     cr.errMsg = strerror(-ret);
60     _hidl_cb(cr);
61     return Void();
62 }
63 
setSlotAsUnbootable(uint32_t slot,setSlotAsUnbootable_cb _hidl_cb)64 Return<void> BootControl::setSlotAsUnbootable(uint32_t slot, setSlotAsUnbootable_cb _hidl_cb)  {
65     int ret = mModule->setSlotAsUnbootable(mModule, slot);
66     struct CommandResult cr;
67     cr.success = (ret == 0);
68     cr.errMsg = strerror(-ret);
69     _hidl_cb(cr);
70     return Void();
71 }
72 
isSlotBootable(uint32_t slot)73 Return<BoolResult> BootControl::isSlotBootable(uint32_t slot)  {
74     int32_t ret = mModule->isSlotBootable(mModule, slot);
75     if (ret < 0) {
76         return BoolResult::INVALID_SLOT;
77     }
78     return ret ? BoolResult::TRUE : BoolResult::FALSE;
79 }
80 
isSlotMarkedSuccessful(uint32_t slot)81 Return<BoolResult> BootControl::isSlotMarkedSuccessful(uint32_t slot)  {
82     int32_t ret = mModule->isSlotMarkedSuccessful(mModule, slot);
83     if (ret < 0) {
84         return BoolResult::INVALID_SLOT;
85     }
86     return ret ? BoolResult::TRUE : BoolResult::FALSE;
87 }
88 
getSuffix(uint32_t slot,getSuffix_cb _hidl_cb)89 Return<void> BootControl::getSuffix(uint32_t slot, getSuffix_cb _hidl_cb)  {
90     hidl_string ans;
91     const char *suffix = mModule->getSuffix(mModule, slot);
92     if (suffix) {
93         ans = suffix;
94     }
95     _hidl_cb(ans);
96     return Void();
97 }
98 
99 #ifdef BOOT_CONTROL_RECOVERY
HIDL_FETCH_IBootControl(const char *)100 IBootControl* HIDL_FETCH_IBootControl(const char * /* hal */) {
101     boot_control_module_t* module;
102 
103     // For devices that don't build a standalone libhardware bootctrl impl for recovery,
104     // we simulate the hw_get_module() by accessing it from the current process directly.
105     const hw_module_t* hw_module = &HAL_MODULE_INFO_SYM;
106     if (!hw_module ||
107         strcmp(BOOT_CONTROL_HARDWARE_MODULE_ID, hw_module->id) != 0) {
108         ALOGE("Error loading boot_control HAL implementation: %d.", -EINVAL);
109         return nullptr;
110     }
111     module = reinterpret_cast<boot_control_module_t*>(const_cast<hw_module_t*>(hw_module));
112     module->init(module);
113     return new BootControl(module);
114 }
115 #else
HIDL_FETCH_IBootControl(const char *)116 IBootControl* HIDL_FETCH_IBootControl(const char* /* hal */) {
117     int ret = 0;
118     boot_control_module_t* module = NULL;
119     hw_module_t **hwm = reinterpret_cast<hw_module_t**>(&module);
120     ret = hw_get_module(BOOT_CONTROL_HARDWARE_MODULE_ID, const_cast<const hw_module_t**>(hwm));
121     if (ret)
122     {
123         ALOGE("hw_get_module %s failed: %d", BOOT_CONTROL_HARDWARE_MODULE_ID, ret);
124         return nullptr;
125     }
126     module->init(module);
127     return new BootControl(module);
128 }
129 #endif
130 } // namespace implementation
131 }  // namespace V1_0
132 }  // namespace boot
133 }  // namespace hardware
134 }  // namespace android
135