xref: /openbmc/u-boot/lib/libavb/avb_sysdeps.h (revision 8e51c0f2)
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * SPDX-License-Identifier:	MIT
5  */
6 
7 #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
8 #error "Never include this file directly, include libavb.h instead."
9 #endif
10 
11 #ifndef AVB_SYSDEPS_H_
12 #define AVB_SYSDEPS_H_
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /* Change these includes to match your platform to bring in the
19  * equivalent types available in a normal C runtime. At least things
20  * like uint8_t, uint64_t, and bool (with |false|, |true| keywords)
21  * must be present.
22  */
23 #include <common.h>
24 
25 /* If you don't have gcc or clang, these attribute macros may need to
26  * be adjusted.
27  */
28 #define AVB_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
29 #define AVB_ATTR_PACKED __attribute__((packed))
30 #define AVB_ATTR_NO_RETURN __attribute__((noreturn))
31 #define AVB_ATTR_SENTINEL __attribute__((__sentinel__))
32 
33 /* Size in bytes used for alignment. */
34 #ifdef __LP64__
35 #define AVB_ALIGNMENT_SIZE 8
36 #else
37 #define AVB_ALIGNMENT_SIZE 4
38 #endif
39 
40 /* Compare |n| bytes in |src1| and |src2|.
41  *
42  * Returns an integer less than, equal to, or greater than zero if the
43  * first |n| bytes of |src1| is found, respectively, to be less than,
44  * to match, or be greater than the first |n| bytes of |src2|. */
45 int avb_memcmp(const void* src1,
46                const void* src2,
47                size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
48 
49 /* Compare two strings.
50  *
51  * Return an integer less than, equal to, or greater than zero if |s1|
52  * is found, respectively, to be less than, to match, or be greater
53  * than |s2|.
54  */
55 int avb_strcmp(const char* s1, const char* s2);
56 
57 /* Copy |n| bytes from |src| to |dest|. */
58 void* avb_memcpy(void* dest, const void* src, size_t n);
59 
60 /* Set |n| bytes starting at |s| to |c|.  Returns |dest|. */
61 void* avb_memset(void* dest, const int c, size_t n);
62 
63 /* Prints out a message. The string passed must be a NUL-terminated
64  * UTF-8 string.
65  */
66 void avb_print(const char* message);
67 
68 /* Prints out a vector of strings. Each argument must point to a
69  * NUL-terminated UTF-8 string and NULL should be the last argument.
70  */
71 void avb_printv(const char* message, ...) AVB_ATTR_SENTINEL;
72 
73 /* Aborts the program or reboots the device. */
74 void avb_abort(void) AVB_ATTR_NO_RETURN;
75 
76 /* Allocates |size| bytes. Returns NULL if no memory is available,
77  * otherwise a pointer to the allocated memory.
78  *
79  * The memory is not initialized.
80  *
81  * The pointer returned is guaranteed to be word-aligned.
82  *
83  * The memory should be freed with avb_free() when you are done with it.
84  */
85 void* avb_malloc_(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
86 
87 /* Frees memory previously allocated with avb_malloc(). */
88 void avb_free(void* ptr);
89 
90 /* Returns the lenght of |str|, excluding the terminating NUL-byte. */
91 size_t avb_strlen(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
92 
93 /* Divide the |dividend| by 10 and saves back to the pointer. Return the
94  * remainder. */
95 uint32_t avb_div_by_10(uint64_t* dividend);
96 
97 #ifdef __cplusplus
98 }
99 #endif
100 
101 #endif /* AVB_SYSDEPS_H_ */
102