1 /*
2  * Copyright (C) 2015 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 
17 #include <string>
18 
19 #include <hardware/boot_control.h>
20 #include <hardware/hardware.h>
21 
22 #include <libboot_control/libboot_control.h>
23 
24 using android::bootable::BootControl;
25 
26 struct boot_control_private_t {
27   // The base struct needs to be first in the list.
28   boot_control_module_t base;
29 
30   BootControl impl;
31 };
32 
33 namespace {
34 
BootControl_init(boot_control_module_t * module)35 void BootControl_init(boot_control_module_t* module) {
36   auto& impl = reinterpret_cast<boot_control_private_t*>(module)->impl;
37   impl.Init();
38 }
39 
BootControl_getNumberSlots(boot_control_module_t * module)40 unsigned int BootControl_getNumberSlots(boot_control_module_t* module) {
41   auto& impl = reinterpret_cast<boot_control_private_t*>(module)->impl;
42   return impl.GetNumberSlots();
43 }
44 
BootControl_getCurrentSlot(boot_control_module_t * module)45 unsigned int BootControl_getCurrentSlot(boot_control_module_t* module) {
46   auto& impl = reinterpret_cast<boot_control_private_t*>(module)->impl;
47   return impl.GetCurrentSlot();
48 }
49 
BootControl_markBootSuccessful(boot_control_module_t * module)50 int BootControl_markBootSuccessful(boot_control_module_t* module) {
51   auto& impl = reinterpret_cast<boot_control_private_t*>(module)->impl;
52   return impl.MarkBootSuccessful() ? 0 : -1;
53 }
54 
BootControl_setActiveBootSlot(boot_control_module_t * module,unsigned int slot)55 int BootControl_setActiveBootSlot(boot_control_module_t* module, unsigned int slot) {
56   auto& impl = reinterpret_cast<boot_control_private_t*>(module)->impl;
57   return impl.SetActiveBootSlot(slot) ? 0 : -1;
58 }
59 
BootControl_setSlotAsUnbootable(struct boot_control_module * module,unsigned int slot)60 int BootControl_setSlotAsUnbootable(struct boot_control_module* module, unsigned int slot) {
61   auto& impl = reinterpret_cast<boot_control_private_t*>(module)->impl;
62   return impl.SetSlotAsUnbootable(slot) ? 0 : -1;
63 }
64 
BootControl_isSlotBootable(struct boot_control_module * module,unsigned int slot)65 int BootControl_isSlotBootable(struct boot_control_module* module, unsigned int slot) {
66   auto& impl = reinterpret_cast<boot_control_private_t*>(module)->impl;
67   return impl.IsSlotBootable(slot) ? 0 : -1;
68 }
69 
BootControl_isSlotMarkedSuccessful(struct boot_control_module * module,unsigned int slot)70 int BootControl_isSlotMarkedSuccessful(struct boot_control_module* module, unsigned int slot) {
71   auto& impl = reinterpret_cast<boot_control_private_t*>(module)->impl;
72   return impl.IsSlotMarkedSuccessful(slot) ? 0 : -1;
73 }
74 
BootControl_getSuffix(boot_control_module_t * module,unsigned int slot)75 const char* BootControl_getSuffix(boot_control_module_t* module, unsigned int slot) {
76   auto& impl = reinterpret_cast<boot_control_private_t*>(module)->impl;
77   return impl.GetSuffix(slot);
78 }
79 
BootControl_open(const hw_module_t * module __unused,const char * id __unused,hw_device_t ** device __unused)80 static int BootControl_open(const hw_module_t* module __unused, const char* id __unused,
81                             hw_device_t** device __unused) {
82   /* Nothing to do currently. */
83   return 0;
84 }
85 
86 struct hw_module_methods_t BootControl_methods = {
87   .open = BootControl_open,
88 };
89 
90 }  // namespace
91 
92 boot_control_private_t HAL_MODULE_INFO_SYM = {
93   .base =
94       {
95           .common =
96               {
97                   .tag = HARDWARE_MODULE_TAG,
98                   .module_api_version = BOOT_CONTROL_MODULE_API_VERSION_0_1,
99                   .hal_api_version = HARDWARE_HAL_API_VERSION,
100                   .id = BOOT_CONTROL_HARDWARE_MODULE_ID,
101                   .name = "AOSP reference bootctrl HAL",
102                   .author = "The Android Open Source Project",
103                   .methods = &BootControl_methods,
104               },
105           .init = BootControl_init,
106           .getNumberSlots = BootControl_getNumberSlots,
107           .getCurrentSlot = BootControl_getCurrentSlot,
108           .markBootSuccessful = BootControl_markBootSuccessful,
109           .setActiveBootSlot = BootControl_setActiveBootSlot,
110           .setSlotAsUnbootable = BootControl_setSlotAsUnbootable,
111           .isSlotBootable = BootControl_isSlotBootable,
112           .getSuffix = BootControl_getSuffix,
113           .isSlotMarkedSuccessful = BootControl_isSlotMarkedSuccessful,
114       },
115 };
116