xref: /aosp_15_r20/bootable/libbootloader/gbl/libavb/src/lib.rs (revision 5225e6b173e52d2efc6bcf950c27374fd72adabc)
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 //! This file provides one possible implementation of the sysdeps functions for libavb.
16 //! Global allocator is required.
17 
18 #![cfg_attr(not(test), no_std)]
19 // These are implementations of required C functions, see libavb sysdeps for docs.
20 #![allow(missing_docs)]
21 
22 use core::ffi::{c_char, c_int, c_void};
23 use libc::{gbl_free, gbl_malloc, memcmp, memcpy, memset, strcmp, strlen, strncmp};
24 
25 const AVB_MALLOC_ALIGNMENT: usize = avb_bindgen::AVB_ALIGNMENT_SIZE as usize;
26 
27 #[no_mangle]
avb_abort() -> !28 pub extern "C" fn avb_abort() -> ! {
29     panic!("avb_abort");
30 }
31 
32 #[no_mangle]
avb_malloc_(size: usize) -> *mut c_void33 pub extern "C" fn avb_malloc_(size: usize) -> *mut c_void {
34     // SAFETY: libavb calls are compatible with libc counterparts, alignment the same as
35     // avb_free
36     unsafe { gbl_malloc(size, AVB_MALLOC_ALIGNMENT) }
37 }
38 
39 #[no_mangle]
avb_free(ptr: *mut c_void)40 pub extern "C" fn avb_free(ptr: *mut c_void) {
41     // SAFETY: libavb calls are compatible with libc counterparts, alignment the same as
42     // avb_malloc_
43     unsafe { gbl_free(ptr, AVB_MALLOC_ALIGNMENT) }
44 }
45 
46 #[no_mangle]
avb_strlen(s: *const c_char) -> usize47 pub extern "C" fn avb_strlen(s: *const c_char) -> usize {
48     // SAFETY: libavb calls are compatible with libc counterparts
49     unsafe { strlen(s) }
50 }
51 
52 #[no_mangle]
avb_div_by_10(dividend: *mut u64) -> u3253 pub extern "C" fn avb_div_by_10(dividend: *mut u64) -> u32 {
54     // SAFETY: libavb guarantees to pass valid pointer to u64 integer here
55     let val = unsafe { &mut *dividend };
56     let rem = *val % 10;
57     *val /= 10;
58     rem.try_into().unwrap()
59 }
60 
61 #[no_mangle]
avb_memcpy(dest: *mut c_void, src: *const c_void, n: usize) -> *mut c_void62 pub extern "C" fn avb_memcpy(dest: *mut c_void, src: *const c_void, n: usize) -> *mut c_void {
63     // SAFETY: libavb calls are compatible with libc counterparts
64     unsafe { memcpy(dest, src, n) }
65 }
66 
67 #[no_mangle]
avb_memcmp(src1: *const c_void, src2: *const c_void, n: usize) -> c_int68 pub extern "C" fn avb_memcmp(src1: *const c_void, src2: *const c_void, n: usize) -> c_int {
69     // SAFETY: libavb calls are compatible with libc counterparts
70     unsafe { memcmp(src1, src2, n) }
71 }
72 
73 #[no_mangle]
avb_memset(dest: *mut c_void, c: c_int, n: usize) -> *mut c_void74 pub extern "C" fn avb_memset(dest: *mut c_void, c: c_int, n: usize) -> *mut c_void {
75     // SAFETY: libavb calls are compatible with libc counterparts
76     unsafe { memset(dest, c, n) }
77 }
78 
79 #[no_mangle]
avb_strcmp(s1: *const c_char, s2: *const c_char) -> c_int80 pub extern "C" fn avb_strcmp(s1: *const c_char, s2: *const c_char) -> c_int {
81     // SAFETY: libavb calls are compatible with libc counterparts
82     unsafe { strcmp(s1, s2) }
83 }
84 
85 #[no_mangle]
avb_strncmp(s1: *const c_char, s2: *const c_char, n: usize) -> c_int86 pub extern "C" fn avb_strncmp(s1: *const c_char, s2: *const c_char, n: usize) -> c_int {
87     // SAFETY: libavb calls are compatible with libc counterparts
88     unsafe { strncmp(s1, s2, n) }
89 }
90