xref: /OK3568_Linux_fs/u-boot/include/android_avb/avb_sysdeps.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2016 The Android Open Source Project
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person
5*4882a593Smuzhiyun  * obtaining a copy of this software and associated documentation
6*4882a593Smuzhiyun  * files (the "Software"), to deal in the Software without
7*4882a593Smuzhiyun  * restriction, including without limitation the rights to use, copy,
8*4882a593Smuzhiyun  * modify, merge, publish, distribute, sublicense, and/or sell copies
9*4882a593Smuzhiyun  * of the Software, and to permit persons to whom the Software is
10*4882a593Smuzhiyun  * furnished to do so, subject to the following conditions:
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * The above copyright notice and this permission notice shall be
13*4882a593Smuzhiyun  * included in all copies or substantial portions of the Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*4882a593Smuzhiyun  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*4882a593Smuzhiyun  * SOFTWARE.
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
27*4882a593Smuzhiyun #error "Never include this file directly, include libavb.h instead."
28*4882a593Smuzhiyun #endif
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #ifndef AVB_SYSDEPS_H_
32*4882a593Smuzhiyun #define AVB_SYSDEPS_H_
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #ifdef __cplusplus
35*4882a593Smuzhiyun extern "C" {
36*4882a593Smuzhiyun #endif
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /* Change these includes to match your platform to bring in the
39*4882a593Smuzhiyun  * equivalent types available in a normal C runtime. At least things
40*4882a593Smuzhiyun  * like uint8_t, uint64_t, and bool (with |false|, |true| keywords)
41*4882a593Smuzhiyun  * must be present.
42*4882a593Smuzhiyun  */
43*4882a593Smuzhiyun #include <inttypes.h>
44*4882a593Smuzhiyun #include <stdbool.h>
45*4882a593Smuzhiyun #include <stddef.h>
46*4882a593Smuzhiyun #ifdef CONFIG_USE_STDINT
47*4882a593Smuzhiyun /* Provided by gcc. */
48*4882a593Smuzhiyun #include <stdint.h>
49*4882a593Smuzhiyun #else
50*4882a593Smuzhiyun /* Type for `void *' pointers. */
51*4882a593Smuzhiyun typedef unsigned long int uintptr_t;
52*4882a593Smuzhiyun #endif
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /* If you don't have gcc or clang, these attribute macros may need to
55*4882a593Smuzhiyun  * be adjusted.
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun #define AVB_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
58*4882a593Smuzhiyun #define AVB_ATTR_PACKED __attribute__((packed))
59*4882a593Smuzhiyun #define AVB_ATTR_NO_RETURN __attribute__((noreturn))
60*4882a593Smuzhiyun #define AVB_ATTR_SENTINEL __attribute__((__sentinel__))
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /* Size in bytes used for alignment. */
63*4882a593Smuzhiyun #ifdef __LP64__
64*4882a593Smuzhiyun #define AVB_ALIGNMENT_SIZE 8
65*4882a593Smuzhiyun #else
66*4882a593Smuzhiyun #define AVB_ALIGNMENT_SIZE 4
67*4882a593Smuzhiyun #endif
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /* Compare |n| bytes in |src1| and |src2|.
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * Returns an integer less than, equal to, or greater than zero if the
72*4882a593Smuzhiyun  * first |n| bytes of |src1| is found, respectively, to be less than,
73*4882a593Smuzhiyun  * to match, or be greater than the first |n| bytes of |src2|. */
74*4882a593Smuzhiyun int avb_memcmp(const void* src1,
75*4882a593Smuzhiyun                const void* src2,
76*4882a593Smuzhiyun                size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /* Compare two strings.
79*4882a593Smuzhiyun  *
80*4882a593Smuzhiyun  * Return an integer less than, equal to, or greater than zero if |s1|
81*4882a593Smuzhiyun  * is found, respectively, to be less than, to match, or be greater
82*4882a593Smuzhiyun  * than |s2|.
83*4882a593Smuzhiyun  */
84*4882a593Smuzhiyun int avb_strcmp(const char* s1, const char* s2);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /* Compare |n| bytes in two strings.
87*4882a593Smuzhiyun  *
88*4882a593Smuzhiyun  * Return an integer less than, equal to, or greater than zero if the
89*4882a593Smuzhiyun  * first |n| bytes of |s1| is found, respectively, to be less than,
90*4882a593Smuzhiyun  * to match, or be greater than the first |n| bytes of |s2|.
91*4882a593Smuzhiyun  */
92*4882a593Smuzhiyun int avb_strncmp(const char* s1, const char* s2, size_t n);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /* Copy |n| bytes from |src| to |dest|. */
95*4882a593Smuzhiyun void* avb_memcpy(void* dest, const void* src, size_t n);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /* Set |n| bytes starting at |s| to |c|.  Returns |dest|. */
98*4882a593Smuzhiyun void* avb_memset(void* dest, const int c, size_t n);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun /* Prints out a message. The string passed must be a NUL-terminated
101*4882a593Smuzhiyun  * UTF-8 string.
102*4882a593Smuzhiyun  */
103*4882a593Smuzhiyun void avb_print(const char* message);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /* Prints out a vector of strings. Each argument must point to a
106*4882a593Smuzhiyun  * NUL-terminated UTF-8 string and NULL should be the last argument.
107*4882a593Smuzhiyun  */
108*4882a593Smuzhiyun void avb_printv(const char* message, ...) AVB_ATTR_SENTINEL;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun /* Aborts the program or reboots the device. */
111*4882a593Smuzhiyun void avb_abort(void);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /* Allocates |size| bytes. Returns NULL if no memory is available,
114*4882a593Smuzhiyun  * otherwise a pointer to the allocated memory.
115*4882a593Smuzhiyun  *
116*4882a593Smuzhiyun  * The memory is not initialized.
117*4882a593Smuzhiyun  *
118*4882a593Smuzhiyun  * The pointer returned is guaranteed to be word-aligned.
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * The memory should be freed with avb_free() when you are done with it.
121*4882a593Smuzhiyun  */
122*4882a593Smuzhiyun void* avb_malloc_(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun /* Frees memory previously allocated with avb_malloc(). */
125*4882a593Smuzhiyun void avb_free(void* ptr);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /* Returns the lenght of |str|, excluding the terminating NUL-byte. */
128*4882a593Smuzhiyun size_t avb_strlen(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /* Divide the |dividend| by 10 and saves back to the pointer. Return the
131*4882a593Smuzhiyun  * remainder. */
132*4882a593Smuzhiyun uint32_t avb_div_by_10(uint64_t* dividend);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun #ifdef __cplusplus
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun #endif
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun #endif /* AVB_SYSDEPS_H_ */
139