blob: e36407fe26e3b799e57777176066d3501e2b2bbe [file] [log] [blame]
Steven Moreland9f8b5c72016-11-29 15:03:38 -08001/*
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 */
Connor O'Brien74f82922016-10-10 12:31:37 -070016#define LOG_TAG "android.hardware.boot@1.0-impl"
Mark Salyzyna4842ac2017-01-10 10:16:48 -080017
18#include <log/log.h>
Connor O'Brien74f82922016-10-10 12:31:37 -070019
20#include <hardware/hardware.h>
21#include <hardware/boot_control.h>
22#include "BootControl.h"
23
Connor O'Brien3847ffc2018-09-28 12:53:02 -070024#ifdef BOOT_CONTROL_RECOVERY
25extern const hw_module_t HAL_MODULE_INFO_SYM;
26#endif
27
Connor O'Brien74f82922016-10-10 12:31:37 -070028namespace android {
29namespace hardware {
30namespace boot {
31namespace V1_0 {
32namespace implementation {
33
34BootControl::BootControl(boot_control_module_t *module) : mModule(module){
35}
36
37// Methods from ::android::hardware::boot::V1_0::IBootControl follow.
38Return<uint32_t> BootControl::getNumberSlots() {
39 return mModule->getNumberSlots(mModule);
40}
41
42Return<uint32_t> BootControl::getCurrentSlot() {
43 return mModule->getCurrentSlot(mModule);
44}
45
46Return<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
55Return<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
64Return<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
73Return<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
81Return<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
89Return<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
Connor O'Brien3847ffc2018-09-28 12:53:02 -070099#ifdef BOOT_CONTROL_RECOVERY
100IBootControl* HIDL_FETCH_IBootControl(const char * /* hal */) {
101 boot_control_module_t* module;
Connor O'Brien74f82922016-10-10 12:31:37 -0700102
Connor O'Brien3847ffc2018-09-28 12:53:02 -0700103 // 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
Chris Phoenixa79b3b62017-01-20 13:46:36 -0800116IBootControl* HIDL_FETCH_IBootControl(const char* /* hal */) {
Connor O'Brien74f82922016-10-10 12:31:37 -0700117 int ret = 0;
118 boot_control_module_t* module = NULL;
119 hw_module_t **hwm = reinterpret_cast<hw_module_t**>(&module);
Chris Phoenixa79b3b62017-01-20 13:46:36 -0800120 ret = hw_get_module(BOOT_CONTROL_HARDWARE_MODULE_ID, const_cast<const hw_module_t**>(hwm));
Connor O'Brien74f82922016-10-10 12:31:37 -0700121 if (ret)
122 {
Chris Phoenixa79b3b62017-01-20 13:46:36 -0800123 ALOGE("hw_get_module %s failed: %d", BOOT_CONTROL_HARDWARE_MODULE_ID, ret);
Connor O'Brien74f82922016-10-10 12:31:37 -0700124 return nullptr;
125 }
126 module->init(module);
127 return new BootControl(module);
128}
Connor O'Brien3847ffc2018-09-28 12:53:02 -0700129#endif
Connor O'Brien74f82922016-10-10 12:31:37 -0700130} // namespace implementation
131} // namespace V1_0
132} // namespace boot
133} // namespace hardware
134} // namespace android