Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | */ |
| 5 | |
Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 6 | /* |
| 7 | * Helper functions/wrappers for memory allocations, manipulation and |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 8 | * comparison. |
| 9 | */ |
| 10 | |
| 11 | #ifndef VBOOT_REFERENCE_UTILITY_H_ |
| 12 | #define VBOOT_REFERENCE_UTILITY_H_ |
| 13 | |
Randall Spangler | f302905 | 2010-06-16 13:42:58 -0700 | [diff] [blame] | 14 | #include "sysincludes.h" |
Mike Frysinger | 05f5944 | 2013-06-21 20:32:16 -0400 | [diff] [blame] | 15 | #include "vboot_api.h" |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 16 | |
Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 17 | /* Debug and error output */ |
| 18 | #ifdef VBOOT_DEBUG |
Randall Spangler | e49e8af | 2011-07-08 13:03:32 -0700 | [diff] [blame] | 19 | #define VBDEBUG(params) VbExDebug params |
Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 20 | #else |
| 21 | #define VBDEBUG(params) |
| 22 | #endif |
| 23 | |
Randall Spangler | c93347b | 2010-10-04 12:45:49 -0700 | [diff] [blame] | 24 | #ifdef VBOOT_DEBUG |
Randall Spangler | 32a6526 | 2011-06-27 10:49:11 -0700 | [diff] [blame] | 25 | #define VbAssert(expr) do { if (!(expr)) { \ |
Randall Spangler | e49e8af | 2011-07-08 13:03:32 -0700 | [diff] [blame] | 26 | VbExError("assert fail: %s at %s:%d\n", \ |
| 27 | #expr, __FILE__, __LINE__); }} while(0) |
Randall Spangler | c93347b | 2010-10-04 12:45:49 -0700 | [diff] [blame] | 28 | #else |
Randall Spangler | 32a6526 | 2011-06-27 10:49:11 -0700 | [diff] [blame] | 29 | #define VbAssert(expr) |
Randall Spangler | c93347b | 2010-10-04 12:45:49 -0700 | [diff] [blame] | 30 | #endif |
Gaurav Shah | ed9c96a | 2010-03-30 18:56:07 -0700 | [diff] [blame] | 31 | |
Bill Richardson | 518d4f3 | 2011-09-13 15:28:55 -0700 | [diff] [blame] | 32 | /* Optional, up to the BIOS */ |
| 33 | #ifdef VBOOT_EASTER_EGG |
Bill Richardson | 0600e41 | 2011-10-11 15:00:17 -0700 | [diff] [blame] | 34 | #define VBEASTEREGG VbExEasterEgg() |
Bill Richardson | 518d4f3 | 2011-09-13 15:28:55 -0700 | [diff] [blame] | 35 | #else |
Bill Richardson | 0600e41 | 2011-10-11 15:00:17 -0700 | [diff] [blame] | 36 | #define VBEASTEREGG 0 |
Bill Richardson | 518d4f3 | 2011-09-13 15:28:55 -0700 | [diff] [blame] | 37 | #endif |
| 38 | |
Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 39 | /* |
| 40 | * Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and [lsw] |
| 41 | * forming the most and least signficant 16-bit words. |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 42 | */ |
Randall Spangler | f02bbb4 | 2011-08-24 14:16:01 -0700 | [diff] [blame] | 43 | #define CombineUint16Pair(msw,lsw) (((uint32_t)(msw) << 16) | \ |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 44 | (((lsw)) & 0xFFFF)) |
Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 45 | |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 46 | /* Return the minimum of (a) or (b). */ |
| 47 | #define Min(a, b) (((a) < (b)) ? (a) : (b)) |
| 48 | |
Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 49 | /** |
Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 50 | * Compare [n] bytes starting at [s1] with [s2] and return 0 if they |
Randall Spangler | a3454fc | 2011-08-23 14:41:18 -0700 | [diff] [blame] | 51 | * match, 1 if they don't. Returns 0 if n=0, since no bytes mismatched. |
Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 52 | * |
Randall Spangler | a3454fc | 2011-08-23 14:41:18 -0700 | [diff] [blame] | 53 | * Time taken to perform the comparison is only dependent on [n] and |
| 54 | * not on the relationship of the match between [s1] and [s2]. |
| 55 | * |
Randall Spangler | 664096b | 2016-10-13 16:16:41 -0700 | [diff] [blame] | 56 | * Note that unlike memcmp(), this only indicates inequality, not |
Randall Spangler | a3454fc | 2011-08-23 14:41:18 -0700 | [diff] [blame] | 57 | * whether s1 is less than or greater than s2. |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 58 | */ |
Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 59 | int SafeMemcmp(const void *s1, const void *s2, size_t n); |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 60 | |
Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 61 | /* |
| 62 | * Buffer size required to hold the longest possible output of Uint64ToString() |
| 63 | * - that is, Uint64ToString(~0, 2). |
| 64 | */ |
Randall Spangler | bebe53c | 2011-07-07 10:30:25 -0700 | [diff] [blame] | 65 | #define UINT64_TO_STRING_MAX 65 |
| 66 | |
Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Convert a value to a string in the specified radix (2=binary, 10=decimal, |
Randall Spangler | bebe53c | 2011-07-07 10:30:25 -0700 | [diff] [blame] | 69 | * 16=hex) and store it in <buf>, which is <bufsize> chars long. If |
| 70 | * <zero_pad_width>, left-pads the string to at least that width with '0'. |
Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 71 | * Returns the length of the stored string, not counting the terminating null. |
| 72 | */ |
Randall Spangler | bebe53c | 2011-07-07 10:30:25 -0700 | [diff] [blame] | 73 | uint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value, |
| 74 | uint32_t radix, uint32_t zero_pad_width); |
| 75 | |
Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Concatenate <src> onto <dest>, which has space for <destlen> characters |
Randall Spangler | bebe53c | 2011-07-07 10:30:25 -0700 | [diff] [blame] | 78 | * including the terminating null. Note that <dest> will always be |
Randall Spangler | a2db67d | 2013-01-24 16:15:35 -0800 | [diff] [blame] | 79 | * null-terminated if <destlen> > 0. Returns the number of characters used in |
| 80 | * <dest>, not counting the terminating null. |
| 81 | */ |
Bill Richardson | 5fed2a6 | 2013-03-04 15:11:38 -0800 | [diff] [blame] | 82 | uint32_t StrnAppend(char *dest, const char *src, uint32_t destlen); |
Randall Spangler | bebe53c | 2011-07-07 10:30:25 -0700 | [diff] [blame] | 83 | |
Bill Richardson | d6ff721 | 2010-05-27 12:27:32 -0700 | [diff] [blame] | 84 | /* Ensure that only our stub implementations are used, not standard C */ |
| 85 | #ifndef _STUB_IMPLEMENTATION_ |
| 86 | #define malloc _do_not_use_standard_malloc |
| 87 | #define free _do_not_use_standard_free |
Bill Richardson | d6ff721 | 2010-05-27 12:27:32 -0700 | [diff] [blame] | 88 | #endif |
Bill Richardson | f5db4b8 | 2010-05-27 11:15:14 -0700 | [diff] [blame] | 89 | |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 90 | #endif /* VBOOT_REFERENCE_UTILITY_H_ */ |