xref: /rk3399_rockchip-uboot/include/android_avb/avb_util.h (revision 5b69db0720b90f33ecb7fb666b196bfc90404185)
1*5b69db07SJason Zhu /*
2*5b69db07SJason Zhu  * Copyright (C) 2016 The Android Open Source Project
3*5b69db07SJason Zhu  *
4*5b69db07SJason Zhu  * Permission is hereby granted, free of charge, to any person
5*5b69db07SJason Zhu  * obtaining a copy of this software and associated documentation
6*5b69db07SJason Zhu  * files (the "Software"), to deal in the Software without
7*5b69db07SJason Zhu  * restriction, including without limitation the rights to use, copy,
8*5b69db07SJason Zhu  * modify, merge, publish, distribute, sublicense, and/or sell copies
9*5b69db07SJason Zhu  * of the Software, and to permit persons to whom the Software is
10*5b69db07SJason Zhu  * furnished to do so, subject to the following conditions:
11*5b69db07SJason Zhu  *
12*5b69db07SJason Zhu  * The above copyright notice and this permission notice shall be
13*5b69db07SJason Zhu  * included in all copies or substantial portions of the Software.
14*5b69db07SJason Zhu  *
15*5b69db07SJason Zhu  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*5b69db07SJason Zhu  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*5b69db07SJason Zhu  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*5b69db07SJason Zhu  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*5b69db07SJason Zhu  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*5b69db07SJason Zhu  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*5b69db07SJason Zhu  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*5b69db07SJason Zhu  * SOFTWARE.
23*5b69db07SJason Zhu  */
24*5b69db07SJason Zhu 
25*5b69db07SJason Zhu /*
26*5b69db07SJason Zhu #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
27*5b69db07SJason Zhu #error "Never include this file directly, include libavb.h instead."
28*5b69db07SJason Zhu #endif
29*5b69db07SJason Zhu */
30*5b69db07SJason Zhu 
31*5b69db07SJason Zhu #ifndef AVB_UTIL_H_
32*5b69db07SJason Zhu #define AVB_UTIL_H_
33*5b69db07SJason Zhu 
34*5b69db07SJason Zhu #include <android_avb/avb_sysdeps.h>
35*5b69db07SJason Zhu 
36*5b69db07SJason Zhu /* Converts a 32-bit unsigned integer from big-endian to host byte order. */
37*5b69db07SJason Zhu uint32_t avb_be32toh(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
38*5b69db07SJason Zhu 
39*5b69db07SJason Zhu /* Converts a 64-bit unsigned integer from big-endian to host byte order. */
40*5b69db07SJason Zhu uint64_t avb_be64toh(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
41*5b69db07SJason Zhu 
42*5b69db07SJason Zhu /* Converts a 32-bit unsigned integer from host to big-endian byte order. */
43*5b69db07SJason Zhu uint32_t avb_htobe32(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
44*5b69db07SJason Zhu 
45*5b69db07SJason Zhu /* Converts a 64-bit unsigned integer from host to big-endian byte order. */
46*5b69db07SJason Zhu uint64_t avb_htobe64(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
47*5b69db07SJason Zhu 
48*5b69db07SJason Zhu /* Compare |n| bytes starting at |s1| with |s2| and return 0 if they
49*5b69db07SJason Zhu  * match, 1 if they don't.  Returns 0 if |n|==0, since no bytes
50*5b69db07SJason Zhu  * mismatched.
51*5b69db07SJason Zhu  *
52*5b69db07SJason Zhu  * Time taken to perform the comparison is only dependent on |n| and
53*5b69db07SJason Zhu  * not on the relationship of the match between |s1| and |s2|.
54*5b69db07SJason Zhu  *
55*5b69db07SJason Zhu  * Note that unlike avb_memcmp(), this only indicates inequality, not
56*5b69db07SJason Zhu  * whether |s1| is less than or greater than |s2|.
57*5b69db07SJason Zhu  */
58*5b69db07SJason Zhu int avb_safe_memcmp(const void* s1,
59*5b69db07SJason Zhu                     const void* s2,
60*5b69db07SJason Zhu                     size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
61*5b69db07SJason Zhu 
62*5b69db07SJason Zhu /* Adds |value_to_add| to |value| with overflow protection.
63*5b69db07SJason Zhu  *
64*5b69db07SJason Zhu  * Returns false if the addition overflows, true otherwise. In either
65*5b69db07SJason Zhu  * case, |value| is always modified.
66*5b69db07SJason Zhu  */
67*5b69db07SJason Zhu bool avb_safe_add_to(uint64_t* value,
68*5b69db07SJason Zhu                      uint64_t value_to_add) AVB_ATTR_WARN_UNUSED_RESULT;
69*5b69db07SJason Zhu 
70*5b69db07SJason Zhu /* Adds |a| and |b| with overflow protection, returning the value in
71*5b69db07SJason Zhu  * |out_result|.
72*5b69db07SJason Zhu  *
73*5b69db07SJason Zhu  * It's permissible to pass NULL for |out_result| if you just want to
74*5b69db07SJason Zhu  * check that the addition would not overflow.
75*5b69db07SJason Zhu  *
76*5b69db07SJason Zhu  * Returns false if the addition overflows, true otherwise.
77*5b69db07SJason Zhu  */
78*5b69db07SJason Zhu bool avb_safe_add(uint64_t* out_result,
79*5b69db07SJason Zhu                   uint64_t a,
80*5b69db07SJason Zhu                   uint64_t b) AVB_ATTR_WARN_UNUSED_RESULT;
81*5b69db07SJason Zhu 
82*5b69db07SJason Zhu /* Checks if |num_bytes| data at |data| is a valid UTF-8
83*5b69db07SJason Zhu  * string. Returns true if valid UTF-8, false otherwise.
84*5b69db07SJason Zhu  */
85*5b69db07SJason Zhu bool avb_validate_utf8(const uint8_t* data,
86*5b69db07SJason Zhu                        size_t num_bytes) AVB_ATTR_WARN_UNUSED_RESULT;
87*5b69db07SJason Zhu 
88*5b69db07SJason Zhu /* Concatenates |str1| (of |str1_len| bytes) and |str2| (of |str2_len|
89*5b69db07SJason Zhu  * bytes) and puts the result in |buf| which holds |buf_size|
90*5b69db07SJason Zhu  * bytes. The result is also guaranteed to be NUL terminated. Fail if
91*5b69db07SJason Zhu  * there is not enough room in |buf| for the resulting string plus
92*5b69db07SJason Zhu  * terminating NUL byte.
93*5b69db07SJason Zhu  *
94*5b69db07SJason Zhu  * Returns true if the operation succeeds, false otherwise.
95*5b69db07SJason Zhu  */
96*5b69db07SJason Zhu bool avb_str_concat(char* buf,
97*5b69db07SJason Zhu                     size_t buf_size,
98*5b69db07SJason Zhu                     const char* str1,
99*5b69db07SJason Zhu                     size_t str1_len,
100*5b69db07SJason Zhu                     const char* str2,
101*5b69db07SJason Zhu                     size_t str2_len);
102*5b69db07SJason Zhu 
103*5b69db07SJason Zhu /* Like avb_malloc_() but prints a error using avb_error() if memory
104*5b69db07SJason Zhu  * allocation fails.
105*5b69db07SJason Zhu  */
106*5b69db07SJason Zhu void* avb_malloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
107*5b69db07SJason Zhu 
108*5b69db07SJason Zhu /* Like avb_malloc() but sets the memory with zeroes. */
109*5b69db07SJason Zhu void* avb_calloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
110*5b69db07SJason Zhu 
111*5b69db07SJason Zhu /* Duplicates a NUL-terminated string. Returns NULL on OOM. */
112*5b69db07SJason Zhu char* avb_strdup(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
113*5b69db07SJason Zhu 
114*5b69db07SJason Zhu /* Duplicates a NULL-terminated array of NUL-terminated strings by
115*5b69db07SJason Zhu  * concatenating them. The returned string will be
116*5b69db07SJason Zhu  * NUL-terminated. Returns NULL on OOM.
117*5b69db07SJason Zhu  */
118*5b69db07SJason Zhu char* avb_strdupv(const char* str,
119*5b69db07SJason Zhu                   ...) AVB_ATTR_WARN_UNUSED_RESULT AVB_ATTR_SENTINEL;
120*5b69db07SJason Zhu 
121*5b69db07SJason Zhu /* Finds the first occurrence of |needle| in the string |haystack|
122*5b69db07SJason Zhu  * where both strings are NUL-terminated strings. The terminating NUL
123*5b69db07SJason Zhu  * bytes are not compared.
124*5b69db07SJason Zhu  *
125*5b69db07SJason Zhu  * Returns NULL if not found, otherwise points into |haystack| for the
126*5b69db07SJason Zhu  * first occurrence of |needle|.
127*5b69db07SJason Zhu  */
128*5b69db07SJason Zhu const char* avb_strstr(const char* haystack,
129*5b69db07SJason Zhu                        const char* needle) AVB_ATTR_WARN_UNUSED_RESULT;
130*5b69db07SJason Zhu 
131*5b69db07SJason Zhu /* Finds the first occurrence of |str| in the NULL-terminated string
132*5b69db07SJason Zhu  * array |strings|. Each element in |strings| must be
133*5b69db07SJason Zhu  * NUL-terminated. The string given by |str| need not be
134*5b69db07SJason Zhu  * NUL-terminated but its size must be given in |str_size|.
135*5b69db07SJason Zhu  *
136*5b69db07SJason Zhu  * Returns NULL if not found, otherwise points into |strings| for the
137*5b69db07SJason Zhu  * first occurrence of |str|.
138*5b69db07SJason Zhu  */
139*5b69db07SJason Zhu const char* avb_strv_find_str(const char* const* strings,
140*5b69db07SJason Zhu                               const char* str,
141*5b69db07SJason Zhu                               size_t str_size);
142*5b69db07SJason Zhu 
143*5b69db07SJason Zhu /* Replaces all occurrences of |search| with |replace| in |str|.
144*5b69db07SJason Zhu  *
145*5b69db07SJason Zhu  * Returns a newly allocated string or NULL if out of memory.
146*5b69db07SJason Zhu  */
147*5b69db07SJason Zhu char* avb_replace(const char* str,
148*5b69db07SJason Zhu                   const char* search,
149*5b69db07SJason Zhu                   const char* replace) AVB_ATTR_WARN_UNUSED_RESULT;
150*5b69db07SJason Zhu 
151*5b69db07SJason Zhu /* Calculates the CRC-32 for data in |buf| of size |buf_size|. */
152*5b69db07SJason Zhu uint32_t avb_crc32(const uint8_t* buf, size_t buf_size);
153*5b69db07SJason Zhu 
154*5b69db07SJason Zhu /* Returns the basename of |str|. This is defined as the last path
155*5b69db07SJason Zhu  * component, assuming the normal POSIX separator '/'. If there are no
156*5b69db07SJason Zhu  * separators, returns |str|.
157*5b69db07SJason Zhu  */
158*5b69db07SJason Zhu const char *avb_basename(const char* str);
159*5b69db07SJason Zhu 
160*5b69db07SJason Zhu 
161*5b69db07SJason Zhu 
162*5b69db07SJason Zhu #define AVB_STRINGIFY(x) #x
163*5b69db07SJason Zhu #define AVB_TO_STRING(x) AVB_STRINGIFY(x)
164*5b69db07SJason Zhu 
165*5b69db07SJason Zhu #ifdef AVB_ENABLE_DEBUG
166*5b69db07SJason Zhu /* Aborts the program if |expr| is false.
167*5b69db07SJason Zhu  *
168*5b69db07SJason Zhu  * This has no effect unless AVB_ENABLE_DEBUG is defined.
169*5b69db07SJason Zhu  */
170*5b69db07SJason Zhu #define avb_assert(expr)                     \
171*5b69db07SJason Zhu   do {                                       \
172*5b69db07SJason Zhu     if (!(expr)) {                           \
173*5b69db07SJason Zhu       avb_fatal("assert fail: " #expr "\n"); \
174*5b69db07SJason Zhu     }                                        \
175*5b69db07SJason Zhu   } while (0)
176*5b69db07SJason Zhu #else
177*5b69db07SJason Zhu #define avb_assert(expr)
178*5b69db07SJason Zhu #endif
179*5b69db07SJason Zhu 
180*5b69db07SJason Zhu /* Aborts the program if reached.
181*5b69db07SJason Zhu  *
182*5b69db07SJason Zhu  * This has no effect unless AVB_ENABLE_DEBUG is defined.
183*5b69db07SJason Zhu  */
184*5b69db07SJason Zhu #ifdef AVB_ENABLE_DEBUG
185*5b69db07SJason Zhu #define avb_assert_not_reached()         \
186*5b69db07SJason Zhu   do {                                   \
187*5b69db07SJason Zhu     avb_fatal("assert_not_reached()\n"); \
188*5b69db07SJason Zhu   } while (0)
189*5b69db07SJason Zhu #else
190*5b69db07SJason Zhu #define avb_assert_not_reached()
191*5b69db07SJason Zhu #endif
192*5b69db07SJason Zhu 
193*5b69db07SJason Zhu /* Aborts the program if |addr| is not word-aligned.
194*5b69db07SJason Zhu  *
195*5b69db07SJason Zhu  * This has no effect unless AVB_ENABLE_DEBUG is defined.
196*5b69db07SJason Zhu  */
197*5b69db07SJason Zhu #define avb_assert_aligned(addr) \
198*5b69db07SJason Zhu   avb_assert((((uintptr_t)addr) & (AVB_ALIGNMENT_SIZE - 1)) == 0)
199*5b69db07SJason Zhu 
200*5b69db07SJason Zhu #ifdef AVB_ENABLE_DEBUG
201*5b69db07SJason Zhu /* Print functions, used for diagnostics.
202*5b69db07SJason Zhu  *
203*5b69db07SJason Zhu  * These have no effect unless AVB_ENABLE_DEBUG is defined.
204*5b69db07SJason Zhu  */
205*5b69db07SJason Zhu #define avb_debug(message)              \
206*5b69db07SJason Zhu   do {                                  \
207*5b69db07SJason Zhu     avb_printv(avb_basename(__FILE__),  \
208*5b69db07SJason Zhu                ":",                     \
209*5b69db07SJason Zhu                AVB_TO_STRING(__LINE__), \
210*5b69db07SJason Zhu                ": DEBUG: ",             \
211*5b69db07SJason Zhu                message,                 \
212*5b69db07SJason Zhu                NULL);                   \
213*5b69db07SJason Zhu   } while (0)
214*5b69db07SJason Zhu #define avb_debugv(message, ...)        \
215*5b69db07SJason Zhu   do {                                  \
216*5b69db07SJason Zhu     avb_printv(avb_basename(__FILE__),  \
217*5b69db07SJason Zhu                ":",                     \
218*5b69db07SJason Zhu                AVB_TO_STRING(__LINE__), \
219*5b69db07SJason Zhu                ": DEBUG: ",             \
220*5b69db07SJason Zhu                message,                 \
221*5b69db07SJason Zhu                ##__VA_ARGS__);          \
222*5b69db07SJason Zhu   } while (0)
223*5b69db07SJason Zhu #else
224*5b69db07SJason Zhu #define avb_debug(message)
225*5b69db07SJason Zhu #define avb_debugv(message, ...)
226*5b69db07SJason Zhu #endif
227*5b69db07SJason Zhu 
228*5b69db07SJason Zhu /* Prints out a message. This is typically used if a runtime-error
229*5b69db07SJason Zhu  * occurs.
230*5b69db07SJason Zhu  */
231*5b69db07SJason Zhu #define avb_error(message)              \
232*5b69db07SJason Zhu   do {                                  \
233*5b69db07SJason Zhu     avb_printv(avb_basename(__FILE__),  \
234*5b69db07SJason Zhu                ":",                     \
235*5b69db07SJason Zhu                AVB_TO_STRING(__LINE__), \
236*5b69db07SJason Zhu                ": ERROR: ",             \
237*5b69db07SJason Zhu                message,                 \
238*5b69db07SJason Zhu                NULL);                   \
239*5b69db07SJason Zhu   } while (0)
240*5b69db07SJason Zhu #define avb_errorv(message, ...)        \
241*5b69db07SJason Zhu   do {                                  \
242*5b69db07SJason Zhu     avb_printv(avb_basename(__FILE__),  \
243*5b69db07SJason Zhu                ":",                     \
244*5b69db07SJason Zhu                AVB_TO_STRING(__LINE__), \
245*5b69db07SJason Zhu                ": ERROR: ",             \
246*5b69db07SJason Zhu                message,                 \
247*5b69db07SJason Zhu                ##__VA_ARGS__);          \
248*5b69db07SJason Zhu   } while (0)
249*5b69db07SJason Zhu 
250*5b69db07SJason Zhu /* Prints out a message and calls avb_abort().
251*5b69db07SJason Zhu  */
252*5b69db07SJason Zhu #define avb_fatal(message)              \
253*5b69db07SJason Zhu   do {                                  \
254*5b69db07SJason Zhu     avb_printv(avb_basename(__FILE__),  \
255*5b69db07SJason Zhu                ":",                     \
256*5b69db07SJason Zhu                AVB_TO_STRING(__LINE__), \
257*5b69db07SJason Zhu                ": FATAL: ",             \
258*5b69db07SJason Zhu                message,                 \
259*5b69db07SJason Zhu                NULL);                   \
260*5b69db07SJason Zhu     avb_abort();                        \
261*5b69db07SJason Zhu   } while (0)
262*5b69db07SJason Zhu #define avb_fatalv(message, ...)        \
263*5b69db07SJason Zhu   do {                                  \
264*5b69db07SJason Zhu     avb_printv(avb_basename(__FILE__),  \
265*5b69db07SJason Zhu                ":",                     \
266*5b69db07SJason Zhu                AVB_TO_STRING(__LINE__), \
267*5b69db07SJason Zhu                ": FATAL: ",             \
268*5b69db07SJason Zhu                message,                 \
269*5b69db07SJason Zhu                ##__VA_ARGS__);          \
270*5b69db07SJason Zhu     avb_abort();                        \
271*5b69db07SJason Zhu   } while (0)
272*5b69db07SJason Zhu 
273*5b69db07SJason Zhu #endif /* AVB_UTIL_H_ */
274