xref: /aosp_15_r20/external/avb/boot_control/boot_control_avb.c (revision d289c2ba6de359471b23d594623b906876bc48a0)
1*d289c2baSAndroid Build Coastguard Worker /*
2*d289c2baSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*d289c2baSAndroid Build Coastguard Worker  *
4*d289c2baSAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person
5*d289c2baSAndroid Build Coastguard Worker  * obtaining a copy of this software and associated documentation
6*d289c2baSAndroid Build Coastguard Worker  * files (the "Software"), to deal in the Software without
7*d289c2baSAndroid Build Coastguard Worker  * restriction, including without limitation the rights to use, copy,
8*d289c2baSAndroid Build Coastguard Worker  * modify, merge, publish, distribute, sublicense, and/or sell copies
9*d289c2baSAndroid Build Coastguard Worker  * of the Software, and to permit persons to whom the Software is
10*d289c2baSAndroid Build Coastguard Worker  * furnished to do so, subject to the following conditions:
11*d289c2baSAndroid Build Coastguard Worker  *
12*d289c2baSAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be
13*d289c2baSAndroid Build Coastguard Worker  * included in all copies or substantial portions of the Software.
14*d289c2baSAndroid Build Coastguard Worker  *
15*d289c2baSAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*d289c2baSAndroid Build Coastguard Worker  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*d289c2baSAndroid Build Coastguard Worker  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*d289c2baSAndroid Build Coastguard Worker  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*d289c2baSAndroid Build Coastguard Worker  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*d289c2baSAndroid Build Coastguard Worker  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*d289c2baSAndroid Build Coastguard Worker  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*d289c2baSAndroid Build Coastguard Worker  * SOFTWARE.
23*d289c2baSAndroid Build Coastguard Worker  */
24*d289c2baSAndroid Build Coastguard Worker 
25*d289c2baSAndroid Build Coastguard Worker #include <errno.h>
26*d289c2baSAndroid Build Coastguard Worker #include <string.h>
27*d289c2baSAndroid Build Coastguard Worker 
28*d289c2baSAndroid Build Coastguard Worker #include <cutils/properties.h>
29*d289c2baSAndroid Build Coastguard Worker #include <hardware/boot_control.h>
30*d289c2baSAndroid Build Coastguard Worker #include <hardware/hardware.h>
31*d289c2baSAndroid Build Coastguard Worker 
32*d289c2baSAndroid Build Coastguard Worker #include <libavb_ab/libavb_ab.h>
33*d289c2baSAndroid Build Coastguard Worker #include <libavb_user/libavb_user.h>
34*d289c2baSAndroid Build Coastguard Worker 
35*d289c2baSAndroid Build Coastguard Worker static AvbOps* ops = NULL;
36*d289c2baSAndroid Build Coastguard Worker 
module_init(boot_control_module_t * module)37*d289c2baSAndroid Build Coastguard Worker static void module_init(boot_control_module_t* module) {
38*d289c2baSAndroid Build Coastguard Worker   if (ops != NULL) {
39*d289c2baSAndroid Build Coastguard Worker     return;
40*d289c2baSAndroid Build Coastguard Worker   }
41*d289c2baSAndroid Build Coastguard Worker 
42*d289c2baSAndroid Build Coastguard Worker   ops = avb_ops_user_new();
43*d289c2baSAndroid Build Coastguard Worker   if (ops == NULL) {
44*d289c2baSAndroid Build Coastguard Worker     avb_error("Unable to allocate AvbOps instance.\n");
45*d289c2baSAndroid Build Coastguard Worker   }
46*d289c2baSAndroid Build Coastguard Worker }
47*d289c2baSAndroid Build Coastguard Worker 
module_getNumberSlots(boot_control_module_t * module)48*d289c2baSAndroid Build Coastguard Worker static unsigned int module_getNumberSlots(boot_control_module_t* module) {
49*d289c2baSAndroid Build Coastguard Worker   return 2;
50*d289c2baSAndroid Build Coastguard Worker }
51*d289c2baSAndroid Build Coastguard Worker 
module_getCurrentSlot(boot_control_module_t * module)52*d289c2baSAndroid Build Coastguard Worker static unsigned int module_getCurrentSlot(boot_control_module_t* module) {
53*d289c2baSAndroid Build Coastguard Worker   char propbuf[PROPERTY_VALUE_MAX];
54*d289c2baSAndroid Build Coastguard Worker 
55*d289c2baSAndroid Build Coastguard Worker   property_get("ro.boot.slot_suffix", propbuf, "");
56*d289c2baSAndroid Build Coastguard Worker   if (strcmp(propbuf, "_a") == 0) {
57*d289c2baSAndroid Build Coastguard Worker     return 0;
58*d289c2baSAndroid Build Coastguard Worker   } else if (strcmp(propbuf, "_b") == 0) {
59*d289c2baSAndroid Build Coastguard Worker     return 1;
60*d289c2baSAndroid Build Coastguard Worker   } else {
61*d289c2baSAndroid Build Coastguard Worker     avb_error("Unexpected slot suffix '", propbuf, "'.\n");
62*d289c2baSAndroid Build Coastguard Worker     return 0;
63*d289c2baSAndroid Build Coastguard Worker   }
64*d289c2baSAndroid Build Coastguard Worker   return 0;
65*d289c2baSAndroid Build Coastguard Worker }
66*d289c2baSAndroid Build Coastguard Worker 
module_markBootSuccessful(boot_control_module_t * module)67*d289c2baSAndroid Build Coastguard Worker static int module_markBootSuccessful(boot_control_module_t* module) {
68*d289c2baSAndroid Build Coastguard Worker   if (avb_ab_mark_slot_successful(ops->ab_ops, module_getCurrentSlot(module)) ==
69*d289c2baSAndroid Build Coastguard Worker       AVB_IO_RESULT_OK) {
70*d289c2baSAndroid Build Coastguard Worker     return 0;
71*d289c2baSAndroid Build Coastguard Worker   } else {
72*d289c2baSAndroid Build Coastguard Worker     return -EIO;
73*d289c2baSAndroid Build Coastguard Worker   }
74*d289c2baSAndroid Build Coastguard Worker }
75*d289c2baSAndroid Build Coastguard Worker 
module_setActiveBootSlot(boot_control_module_t * module,unsigned int slot)76*d289c2baSAndroid Build Coastguard Worker static int module_setActiveBootSlot(boot_control_module_t* module,
77*d289c2baSAndroid Build Coastguard Worker                                     unsigned int slot) {
78*d289c2baSAndroid Build Coastguard Worker   if (slot >= module_getNumberSlots(module)) {
79*d289c2baSAndroid Build Coastguard Worker     return -EINVAL;
80*d289c2baSAndroid Build Coastguard Worker   } else if (avb_ab_mark_slot_active(ops->ab_ops, slot) == AVB_IO_RESULT_OK) {
81*d289c2baSAndroid Build Coastguard Worker     return 0;
82*d289c2baSAndroid Build Coastguard Worker   } else {
83*d289c2baSAndroid Build Coastguard Worker     return -EIO;
84*d289c2baSAndroid Build Coastguard Worker   }
85*d289c2baSAndroid Build Coastguard Worker }
86*d289c2baSAndroid Build Coastguard Worker 
module_setSlotAsUnbootable(struct boot_control_module * module,unsigned int slot)87*d289c2baSAndroid Build Coastguard Worker static int module_setSlotAsUnbootable(struct boot_control_module* module,
88*d289c2baSAndroid Build Coastguard Worker                                       unsigned int slot) {
89*d289c2baSAndroid Build Coastguard Worker   if (slot >= module_getNumberSlots(module)) {
90*d289c2baSAndroid Build Coastguard Worker     return -EINVAL;
91*d289c2baSAndroid Build Coastguard Worker   } else if (avb_ab_mark_slot_unbootable(ops->ab_ops, slot) ==
92*d289c2baSAndroid Build Coastguard Worker              AVB_IO_RESULT_OK) {
93*d289c2baSAndroid Build Coastguard Worker     return 0;
94*d289c2baSAndroid Build Coastguard Worker   } else {
95*d289c2baSAndroid Build Coastguard Worker     return -EIO;
96*d289c2baSAndroid Build Coastguard Worker   }
97*d289c2baSAndroid Build Coastguard Worker }
98*d289c2baSAndroid Build Coastguard Worker 
module_isSlotBootable(struct boot_control_module * module,unsigned int slot)99*d289c2baSAndroid Build Coastguard Worker static int module_isSlotBootable(struct boot_control_module* module,
100*d289c2baSAndroid Build Coastguard Worker                                  unsigned int slot) {
101*d289c2baSAndroid Build Coastguard Worker   AvbABData ab_data;
102*d289c2baSAndroid Build Coastguard Worker   bool is_bootable;
103*d289c2baSAndroid Build Coastguard Worker 
104*d289c2baSAndroid Build Coastguard Worker   avb_assert(slot < 2);
105*d289c2baSAndroid Build Coastguard Worker 
106*d289c2baSAndroid Build Coastguard Worker   if (slot >= module_getNumberSlots(module)) {
107*d289c2baSAndroid Build Coastguard Worker     return -EINVAL;
108*d289c2baSAndroid Build Coastguard Worker   } else if (avb_ab_data_read(ops->ab_ops, &ab_data) != AVB_IO_RESULT_OK) {
109*d289c2baSAndroid Build Coastguard Worker     return -EIO;
110*d289c2baSAndroid Build Coastguard Worker   }
111*d289c2baSAndroid Build Coastguard Worker 
112*d289c2baSAndroid Build Coastguard Worker   is_bootable = (ab_data.slots[slot].priority > 0) &&
113*d289c2baSAndroid Build Coastguard Worker                 (ab_data.slots[slot].successful_boot ||
114*d289c2baSAndroid Build Coastguard Worker                  (ab_data.slots[slot].tries_remaining > 0));
115*d289c2baSAndroid Build Coastguard Worker 
116*d289c2baSAndroid Build Coastguard Worker   return is_bootable ? 1 : 0;
117*d289c2baSAndroid Build Coastguard Worker }
118*d289c2baSAndroid Build Coastguard Worker 
module_isSlotMarkedSuccessful(struct boot_control_module * module,unsigned int slot)119*d289c2baSAndroid Build Coastguard Worker static int module_isSlotMarkedSuccessful(struct boot_control_module* module,
120*d289c2baSAndroid Build Coastguard Worker                                          unsigned int slot) {
121*d289c2baSAndroid Build Coastguard Worker   AvbABData ab_data;
122*d289c2baSAndroid Build Coastguard Worker   bool is_marked_successful;
123*d289c2baSAndroid Build Coastguard Worker 
124*d289c2baSAndroid Build Coastguard Worker   avb_assert(slot < 2);
125*d289c2baSAndroid Build Coastguard Worker 
126*d289c2baSAndroid Build Coastguard Worker   if (slot >= module_getNumberSlots(module)) {
127*d289c2baSAndroid Build Coastguard Worker     return -EINVAL;
128*d289c2baSAndroid Build Coastguard Worker   } else if (avb_ab_data_read(ops->ab_ops, &ab_data) != AVB_IO_RESULT_OK) {
129*d289c2baSAndroid Build Coastguard Worker     return -EIO;
130*d289c2baSAndroid Build Coastguard Worker   }
131*d289c2baSAndroid Build Coastguard Worker 
132*d289c2baSAndroid Build Coastguard Worker   is_marked_successful = ab_data.slots[slot].successful_boot;
133*d289c2baSAndroid Build Coastguard Worker 
134*d289c2baSAndroid Build Coastguard Worker   return is_marked_successful ? 1 : 0;
135*d289c2baSAndroid Build Coastguard Worker }
136*d289c2baSAndroid Build Coastguard Worker 
module_getSuffix(boot_control_module_t * module,unsigned int slot)137*d289c2baSAndroid Build Coastguard Worker static const char* module_getSuffix(boot_control_module_t* module,
138*d289c2baSAndroid Build Coastguard Worker                                     unsigned int slot) {
139*d289c2baSAndroid Build Coastguard Worker   static const char* suffix[2] = {"_a", "_b"};
140*d289c2baSAndroid Build Coastguard Worker   if (slot >= 2) {
141*d289c2baSAndroid Build Coastguard Worker     return NULL;
142*d289c2baSAndroid Build Coastguard Worker   }
143*d289c2baSAndroid Build Coastguard Worker   return suffix[slot];
144*d289c2baSAndroid Build Coastguard Worker }
145*d289c2baSAndroid Build Coastguard Worker 
146*d289c2baSAndroid Build Coastguard Worker static struct hw_module_methods_t module_methods = {
147*d289c2baSAndroid Build Coastguard Worker     .open = NULL,
148*d289c2baSAndroid Build Coastguard Worker };
149*d289c2baSAndroid Build Coastguard Worker 
150*d289c2baSAndroid Build Coastguard Worker boot_control_module_t HAL_MODULE_INFO_SYM = {
151*d289c2baSAndroid Build Coastguard Worker     .common =
152*d289c2baSAndroid Build Coastguard Worker         {
153*d289c2baSAndroid Build Coastguard Worker             .tag = HARDWARE_MODULE_TAG,
154*d289c2baSAndroid Build Coastguard Worker             .module_api_version = BOOT_CONTROL_MODULE_API_VERSION_0_1,
155*d289c2baSAndroid Build Coastguard Worker             .hal_api_version = HARDWARE_HAL_API_VERSION,
156*d289c2baSAndroid Build Coastguard Worker             .id = BOOT_CONTROL_HARDWARE_MODULE_ID,
157*d289c2baSAndroid Build Coastguard Worker             .name = "AVB implementation of boot_control HAL",
158*d289c2baSAndroid Build Coastguard Worker             .author = "The Android Open Source Project",
159*d289c2baSAndroid Build Coastguard Worker             .methods = &module_methods,
160*d289c2baSAndroid Build Coastguard Worker         },
161*d289c2baSAndroid Build Coastguard Worker     .init = module_init,
162*d289c2baSAndroid Build Coastguard Worker     .getNumberSlots = module_getNumberSlots,
163*d289c2baSAndroid Build Coastguard Worker     .getCurrentSlot = module_getCurrentSlot,
164*d289c2baSAndroid Build Coastguard Worker     .markBootSuccessful = module_markBootSuccessful,
165*d289c2baSAndroid Build Coastguard Worker     .setActiveBootSlot = module_setActiveBootSlot,
166*d289c2baSAndroid Build Coastguard Worker     .setSlotAsUnbootable = module_setSlotAsUnbootable,
167*d289c2baSAndroid Build Coastguard Worker     .isSlotBootable = module_isSlotBootable,
168*d289c2baSAndroid Build Coastguard Worker     .getSuffix = module_getSuffix,
169*d289c2baSAndroid Build Coastguard Worker     .isSlotMarkedSuccessful = module_isSlotMarkedSuccessful,
170*d289c2baSAndroid Build Coastguard Worker };
171