1 // Copyright 2024, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use abr::{AbrData, ONE_SHOT_BOOTLOADER, ONE_SHOT_RECOVERY};
16
17 /// Converts big endian order to host order.
18 #[no_mangle]
19 #[allow(non_snake_case)]
AbrBigEndianToHost(val: u32) -> u3220 pub extern "C" fn AbrBigEndianToHost(val: u32) -> u32 {
21 u32::from_be(val)
22 }
23
24 /// Converts host order to big endian.
25 #[no_mangle]
26 #[allow(non_snake_case)]
AbrHostToBigEndian(val: u32) -> u3227 pub extern "C" fn AbrHostToBigEndian(val: u32) -> u32 {
28 val.to_be()
29 }
30
31 /// Checks if one-shot recovery boot is set in the given one-shot flags
32 #[no_mangle]
33 #[allow(non_snake_case)]
AbrIsOneShotRecoveryBootSet(flags: u8) -> bool34 pub extern "C" fn AbrIsOneShotRecoveryBootSet(flags: u8) -> bool {
35 (flags & ONE_SHOT_RECOVERY) != 0
36 }
37
38 /// Checks if one-shot recovery boot is set in the given AbrData
39 ///
40 /// # Safety
41 ///
42 /// Caller must make sure to pass a valid pointer for `abr_data`.
43 #[no_mangle]
44 #[allow(non_snake_case)]
AbrIsOneShotRecoveryBoot(abr_data: *const AbrData) -> bool45 pub unsafe extern "C" fn AbrIsOneShotRecoveryBoot(abr_data: *const AbrData) -> bool {
46 // SAFETY: function safety requires `abr_data` to be a valid pointer.
47 AbrIsOneShotRecoveryBootSet(unsafe { abr_data.as_ref() }.unwrap().one_shot_flags)
48 }
49
50 /// Checks if one-shot bootloader boot is set in the given one-shot flags
51 #[no_mangle]
52 #[allow(non_snake_case)]
AbrIsOneShotBootloaderBootSet(flags: u8) -> bool53 pub extern "C" fn AbrIsOneShotBootloaderBootSet(flags: u8) -> bool {
54 (flags & ONE_SHOT_BOOTLOADER) != 0
55 }
56
57 /// Checks if one-shot bootloader boot is set in the given AbrData
58 ///
59 /// # Safety
60 ///
61 /// Caller must make sure to pass a valid pointer for `abr_data`.
62 #[no_mangle]
63 #[allow(non_snake_case)]
AbrIsOneShotBootloaderBoot(abr_data: *const AbrData) -> bool64 pub unsafe extern "C" fn AbrIsOneShotBootloaderBoot(abr_data: *const AbrData) -> bool {
65 // SAFETY: function safety requires `abr_data` to be a valid pointer.
66 AbrIsOneShotBootloaderBootSet(unsafe { abr_data.as_ref() }.unwrap().one_shot_flags)
67 }
68
69 /// Sets the one-shot recovery flag in the given AbrData.
70 ///
71 /// # Safety
72 ///
73 /// Caller must make sure to pass a valid pointer for `abr_data`.
74 #[no_mangle]
75 #[allow(non_snake_case)]
AbrSetOneShotRecoveryBoot(abr_data: *mut AbrData, enable: bool)76 pub unsafe extern "C" fn AbrSetOneShotRecoveryBoot(abr_data: *mut AbrData, enable: bool) {
77 // SAFETY: function safety requires `abr_data` to be a valid pointer.
78 unsafe { abr_data.as_mut() }.unwrap().set_one_shot_recovery(enable);
79 }
80
81 /// Sets the one-shot bootloader flag in the given AbrData.
82 ///
83 /// # Safety
84 ///
85 /// Caller must make sure to pass a valid pointer for `abr_data`.
86 #[no_mangle]
87 #[allow(non_snake_case)]
AbrSetOneShotBootloaderBoot(abr_data: *mut AbrData, enable: bool)88 pub unsafe extern "C" fn AbrSetOneShotBootloaderBoot(abr_data: *mut AbrData, enable: bool) {
89 // SAFETY: function safety requires `abr_data` to be a valid pointer.
90 unsafe { abr_data.as_mut() }.unwrap().set_one_shot_bootloader(enable);
91 }
92