1*5225e6b1SAndroid Build Coastguard Worker // Copyright 2024, The Android Open Source Project
2*5225e6b1SAndroid Build Coastguard Worker //
3*5225e6b1SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*5225e6b1SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*5225e6b1SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*5225e6b1SAndroid Build Coastguard Worker //
7*5225e6b1SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
8*5225e6b1SAndroid Build Coastguard Worker //
9*5225e6b1SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*5225e6b1SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*5225e6b1SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*5225e6b1SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*5225e6b1SAndroid Build Coastguard Worker // limitations under the License.
14*5225e6b1SAndroid Build Coastguard Worker
15*5225e6b1SAndroid Build Coastguard Worker //! This file provides one possible implementation of the sysdeps functions for libavb.
16*5225e6b1SAndroid Build Coastguard Worker //! Global allocator is required.
17*5225e6b1SAndroid Build Coastguard Worker
18*5225e6b1SAndroid Build Coastguard Worker #![cfg_attr(not(test), no_std)]
19*5225e6b1SAndroid Build Coastguard Worker // These are implementations of required C functions, see libavb sysdeps for docs.
20*5225e6b1SAndroid Build Coastguard Worker #![allow(missing_docs)]
21*5225e6b1SAndroid Build Coastguard Worker
22*5225e6b1SAndroid Build Coastguard Worker use core::ffi::{c_char, c_int, c_void};
23*5225e6b1SAndroid Build Coastguard Worker use libc::{gbl_free, gbl_malloc, memcmp, memcpy, memset, strcmp, strlen, strncmp};
24*5225e6b1SAndroid Build Coastguard Worker
25*5225e6b1SAndroid Build Coastguard Worker const AVB_MALLOC_ALIGNMENT: usize = avb_bindgen::AVB_ALIGNMENT_SIZE as usize;
26*5225e6b1SAndroid Build Coastguard Worker
27*5225e6b1SAndroid Build Coastguard Worker #[no_mangle]
avb_abort() -> !28*5225e6b1SAndroid Build Coastguard Worker pub extern "C" fn avb_abort() -> ! {
29*5225e6b1SAndroid Build Coastguard Worker panic!("avb_abort");
30*5225e6b1SAndroid Build Coastguard Worker }
31*5225e6b1SAndroid Build Coastguard Worker
32*5225e6b1SAndroid Build Coastguard Worker #[no_mangle]
avb_malloc_(size: usize) -> *mut c_void33*5225e6b1SAndroid Build Coastguard Worker pub extern "C" fn avb_malloc_(size: usize) -> *mut c_void {
34*5225e6b1SAndroid Build Coastguard Worker // SAFETY: libavb calls are compatible with libc counterparts, alignment the same as
35*5225e6b1SAndroid Build Coastguard Worker // avb_free
36*5225e6b1SAndroid Build Coastguard Worker unsafe { gbl_malloc(size, AVB_MALLOC_ALIGNMENT) }
37*5225e6b1SAndroid Build Coastguard Worker }
38*5225e6b1SAndroid Build Coastguard Worker
39*5225e6b1SAndroid Build Coastguard Worker #[no_mangle]
avb_free(ptr: *mut c_void)40*5225e6b1SAndroid Build Coastguard Worker pub extern "C" fn avb_free(ptr: *mut c_void) {
41*5225e6b1SAndroid Build Coastguard Worker // SAFETY: libavb calls are compatible with libc counterparts, alignment the same as
42*5225e6b1SAndroid Build Coastguard Worker // avb_malloc_
43*5225e6b1SAndroid Build Coastguard Worker unsafe { gbl_free(ptr, AVB_MALLOC_ALIGNMENT) }
44*5225e6b1SAndroid Build Coastguard Worker }
45*5225e6b1SAndroid Build Coastguard Worker
46*5225e6b1SAndroid Build Coastguard Worker #[no_mangle]
avb_strlen(s: *const c_char) -> usize47*5225e6b1SAndroid Build Coastguard Worker pub extern "C" fn avb_strlen(s: *const c_char) -> usize {
48*5225e6b1SAndroid Build Coastguard Worker // SAFETY: libavb calls are compatible with libc counterparts
49*5225e6b1SAndroid Build Coastguard Worker unsafe { strlen(s) }
50*5225e6b1SAndroid Build Coastguard Worker }
51*5225e6b1SAndroid Build Coastguard Worker
52*5225e6b1SAndroid Build Coastguard Worker #[no_mangle]
avb_div_by_10(dividend: *mut u64) -> u3253*5225e6b1SAndroid Build Coastguard Worker pub extern "C" fn avb_div_by_10(dividend: *mut u64) -> u32 {
54*5225e6b1SAndroid Build Coastguard Worker // SAFETY: libavb guarantees to pass valid pointer to u64 integer here
55*5225e6b1SAndroid Build Coastguard Worker let val = unsafe { &mut *dividend };
56*5225e6b1SAndroid Build Coastguard Worker let rem = *val % 10;
57*5225e6b1SAndroid Build Coastguard Worker *val /= 10;
58*5225e6b1SAndroid Build Coastguard Worker rem.try_into().unwrap()
59*5225e6b1SAndroid Build Coastguard Worker }
60*5225e6b1SAndroid Build Coastguard Worker
61*5225e6b1SAndroid Build Coastguard Worker #[no_mangle]
avb_memcpy(dest: *mut c_void, src: *const c_void, n: usize) -> *mut c_void62*5225e6b1SAndroid Build Coastguard Worker pub extern "C" fn avb_memcpy(dest: *mut c_void, src: *const c_void, n: usize) -> *mut c_void {
63*5225e6b1SAndroid Build Coastguard Worker // SAFETY: libavb calls are compatible with libc counterparts
64*5225e6b1SAndroid Build Coastguard Worker unsafe { memcpy(dest, src, n) }
65*5225e6b1SAndroid Build Coastguard Worker }
66*5225e6b1SAndroid Build Coastguard Worker
67*5225e6b1SAndroid Build Coastguard Worker #[no_mangle]
avb_memcmp(src1: *const c_void, src2: *const c_void, n: usize) -> c_int68*5225e6b1SAndroid Build Coastguard Worker pub extern "C" fn avb_memcmp(src1: *const c_void, src2: *const c_void, n: usize) -> c_int {
69*5225e6b1SAndroid Build Coastguard Worker // SAFETY: libavb calls are compatible with libc counterparts
70*5225e6b1SAndroid Build Coastguard Worker unsafe { memcmp(src1, src2, n) }
71*5225e6b1SAndroid Build Coastguard Worker }
72*5225e6b1SAndroid Build Coastguard Worker
73*5225e6b1SAndroid Build Coastguard Worker #[no_mangle]
avb_memset(dest: *mut c_void, c: c_int, n: usize) -> *mut c_void74*5225e6b1SAndroid Build Coastguard Worker pub extern "C" fn avb_memset(dest: *mut c_void, c: c_int, n: usize) -> *mut c_void {
75*5225e6b1SAndroid Build Coastguard Worker // SAFETY: libavb calls are compatible with libc counterparts
76*5225e6b1SAndroid Build Coastguard Worker unsafe { memset(dest, c, n) }
77*5225e6b1SAndroid Build Coastguard Worker }
78*5225e6b1SAndroid Build Coastguard Worker
79*5225e6b1SAndroid Build Coastguard Worker #[no_mangle]
avb_strcmp(s1: *const c_char, s2: *const c_char) -> c_int80*5225e6b1SAndroid Build Coastguard Worker pub extern "C" fn avb_strcmp(s1: *const c_char, s2: *const c_char) -> c_int {
81*5225e6b1SAndroid Build Coastguard Worker // SAFETY: libavb calls are compatible with libc counterparts
82*5225e6b1SAndroid Build Coastguard Worker unsafe { strcmp(s1, s2) }
83*5225e6b1SAndroid Build Coastguard Worker }
84*5225e6b1SAndroid Build Coastguard Worker
85*5225e6b1SAndroid Build Coastguard Worker #[no_mangle]
avb_strncmp(s1: *const c_char, s2: *const c_char, n: usize) -> c_int86*5225e6b1SAndroid Build Coastguard Worker pub extern "C" fn avb_strncmp(s1: *const c_char, s2: *const c_char, n: usize) -> c_int {
87*5225e6b1SAndroid Build Coastguard Worker // SAFETY: libavb calls are compatible with libc counterparts
88*5225e6b1SAndroid Build Coastguard Worker unsafe { strncmp(s1, s2, n) }
89*5225e6b1SAndroid Build Coastguard Worker }
90