1 /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 */ 5 6 /* 7 * Helper functions/wrappers for memory allocations, manipulation and 8 * comparison. 9 */ 10 11 #ifndef VBOOT_REFERENCE_UTILITY_H_ 12 #define VBOOT_REFERENCE_UTILITY_H_ 13 14 #include "sysincludes.h" 15 #include "vboot_api.h" 16 17 /* Debug and error output */ 18 #ifdef VBOOT_DEBUG 19 #define VBDEBUG(params) VbExDebug params 20 #else 21 #define VBDEBUG(params) 22 #endif 23 24 #ifdef VBOOT_DEBUG 25 #define VbAssert(expr) do { if (!(expr)) { \ 26 VbExError("assert fail: %s at %s:%d\n", \ 27 #expr, __FILE__, __LINE__); }} while(0) 28 #else 29 #define VbAssert(expr) 30 #endif 31 32 /* Optional, up to the BIOS */ 33 #ifdef VBOOT_EASTER_EGG 34 #define VBEASTEREGG VbExEasterEgg() 35 #else 36 #define VBEASTEREGG 0 37 #endif 38 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. 42 */ 43 #define CombineUint16Pair(msw,lsw) (((uint32_t)(msw) << 16) | \ 44 (((lsw)) & 0xFFFF)) 45 46 /* Return the minimum of (a) or (b). */ 47 #define Min(a, b) (((a) < (b)) ? (a) : (b)) 48 49 /** 50 * Compare [n] bytes in [src1] and [src2]. 51 * 52 * Returns an integer less than, equal to, or greater than zero if the first 53 * [n] bytes of [src1] is found, respectively, to be less than, to match, or be 54 * greater than the first n bytes of [src2]. */ 55 int Memcmp(const void *src1, const void *src2, size_t n); 56 57 /** 58 * Copy [n] bytes from [src] to [dest]. 59 */ 60 void *Memcpy(void *dest, const void *src, uint64_t n); 61 62 /* 63 * Implementations of the functions below must be built as part of the firmware 64 * and defined in lib/utility.c. 65 */ 66 67 /** 68 * Set [n] bytes starting at [s] to [c]. Returns dest. 69 */ 70 void *Memset(void *dest, const uint8_t c, uint64_t n); 71 72 /** 73 * Compare [n] bytes starting at [s1] with [s2] and return 0 if they 74 * match, 1 if they don't. Returns 0 if n=0, since no bytes mismatched. 75 * 76 * Time taken to perform the comparison is only dependent on [n] and 77 * not on the relationship of the match between [s1] and [s2]. 78 * 79 * Note that unlike Memcmp(), this only indicates inequality, not 80 * whether s1 is less than or greater than s2. 81 */ 82 int SafeMemcmp(const void *s1, const void *s2, size_t n); 83 84 /* 85 * Buffer size required to hold the longest possible output of Uint64ToString() 86 * - that is, Uint64ToString(~0, 2). 87 */ 88 #define UINT64_TO_STRING_MAX 65 89 90 /** 91 * Convert a value to a string in the specified radix (2=binary, 10=decimal, 92 * 16=hex) and store it in <buf>, which is <bufsize> chars long. If 93 * <zero_pad_width>, left-pads the string to at least that width with '0'. 94 * Returns the length of the stored string, not counting the terminating null. 95 */ 96 uint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value, 97 uint32_t radix, uint32_t zero_pad_width); 98 99 /** 100 * Concatenate <src> onto <dest>, which has space for <destlen> characters 101 * including the terminating null. Note that <dest> will always be 102 * null-terminated if <destlen> > 0. Returns the number of characters used in 103 * <dest>, not counting the terminating null. 104 */ 105 uint32_t StrnAppend(char *dest, const char *src, uint32_t destlen); 106 107 /* Ensure that only our stub implementations are used, not standard C */ 108 #ifndef _STUB_IMPLEMENTATION_ 109 #define malloc _do_not_use_standard_malloc 110 #define free _do_not_use_standard_free 111 #define memcmp _do_not_use_standard_memcmp 112 #define memcpy _do_not_use_standard_memcpy 113 #define memset _do_not_use_standard_memset 114 #endif 115 116 #endif /* VBOOT_REFERENCE_UTILITY_H_ */ 117