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