utility.h revision 518d4f39b4ab5fa7d516de397795bc146250c51b
11d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
21d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * Use of this source code is governed by a BSD-style license that can be
31d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * found in the LICENSE file.
41d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong */
51d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
61d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong/* Helper functions/wrappers for memory allocations, manipulation and
71d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * comparison.
81d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong */
91d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
101d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#ifndef VBOOT_REFERENCE_UTILITY_H_
111d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#define VBOOT_REFERENCE_UTILITY_H_
121d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
131d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#include "sysincludes.h"
141d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
151d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong/* Debug and error output */
161d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#ifdef VBOOT_DEBUG
171d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#define VBDEBUG(params) VbExDebug params
181d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#else
191d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#define VBDEBUG(params)
201d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#endif
211d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
221d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#ifndef VBOOT_PERFORMANCE
231d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong/* Define performance macros as nothing.  If you enable VBOOT_PERFORMANCE,
241d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * you must define these macros in your platform's biosincludes.h.
251d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong *
261d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * Intended usage for using a performance counter called 'foo':
272a7e0a1eb29306982fd77bdc64d324464a48a2b9James Dong *
280b42f253d15a190e230df43a4b45a3c483e5869aJames Dong * VBPERFSTART("foo")
29c0a84782589eececdfa7e723e8aa0e572d0d79f5Nipun Kwatra * ...code to be tested...
30c0a84782589eececdfa7e723e8aa0e572d0d79f5Nipun Kwatra * VBPERFEND("foo")
319783ed8f85af3d7c72030098acfed7e1d6638349Nipun Kwatra *
329783ed8f85af3d7c72030098acfed7e1d6638349Nipun Kwatra * Names should be <= 8 characters to be compatible with all platforms.
339783ed8f85af3d7c72030098acfed7e1d6638349Nipun Kwatra */
349783ed8f85af3d7c72030098acfed7e1d6638349Nipun Kwatra#define VBPERFSTART(name)
352a7e0a1eb29306982fd77bdc64d324464a48a2b9James Dong#define VBPERFEND(name)
36c0a84782589eececdfa7e723e8aa0e572d0d79f5Nipun Kwatra#endif
372a7e0a1eb29306982fd77bdc64d324464a48a2b9James Dong
38c0a84782589eececdfa7e723e8aa0e572d0d79f5Nipun Kwatra#ifdef VBOOT_DEBUG
39c0a84782589eececdfa7e723e8aa0e572d0d79f5Nipun Kwatra#define VbAssert(expr) do { if (!(expr)) { \
40c0a84782589eececdfa7e723e8aa0e572d0d79f5Nipun Kwatra    VbExError("assert fail: %s at %s:%d\n", \
419783ed8f85af3d7c72030098acfed7e1d6638349Nipun Kwatra              #expr, __FILE__, __LINE__); }} while(0)
429783ed8f85af3d7c72030098acfed7e1d6638349Nipun Kwatra#else
439783ed8f85af3d7c72030098acfed7e1d6638349Nipun Kwatra#define VbAssert(expr)
442a7e0a1eb29306982fd77bdc64d324464a48a2b9James Dong#endif
452a7e0a1eb29306982fd77bdc64d324464a48a2b9James Dong
461d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong/* Optional, up to the BIOS */
471d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#ifdef VBOOT_EASTER_EGG
48cabd5f867ec69c0dfe8333cba46323cb58917402Hong Teng#define VBEASTEREGG(A,B) VbExEasterEgg(A,B)
49cabd5f867ec69c0dfe8333cba46323cb58917402Hong Teng#else
50cabd5f867ec69c0dfe8333cba46323cb58917402Hong Teng#define VBEASTEREGG(A,B)
51cabd5f867ec69c0dfe8333cba46323cb58917402Hong Teng#endif
52cabd5f867ec69c0dfe8333cba46323cb58917402Hong Teng
53cabd5f867ec69c0dfe8333cba46323cb58917402Hong Teng/* Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and
54cabd5f867ec69c0dfe8333cba46323cb58917402Hong Teng * [lsw] forming the most and least signficant 16-bit words.
55cabd5f867ec69c0dfe8333cba46323cb58917402Hong Teng */
56cabd5f867ec69c0dfe8333cba46323cb58917402Hong Teng#define CombineUint16Pair(msw,lsw) (((uint32_t)(msw) << 16) |   \
57cabd5f867ec69c0dfe8333cba46323cb58917402Hong Teng                                    (((lsw)) & 0xFFFF))
58cabd5f867ec69c0dfe8333cba46323cb58917402Hong Teng/* Return the minimum of (a) or (b). */
59cabd5f867ec69c0dfe8333cba46323cb58917402Hong Teng#define Min(a, b) (((a) < (b)) ? (a) : (b))
601d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
611d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong/* Compare [n] bytes in [src1] and [src2]
621d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * Returns an integer less than, equal to, or greater than zero if the first [n]
631d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * bytes of [src1] is found, respectively, to be less than, to match, or be
641d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * greater than the first n bytes of [src2]. */
651d7491b19516505e0754c66a3c8cd61811c9b6a6James Dongint Memcmp(const void* src1, const void* src2, size_t n);
661d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
671d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong/* Copy [n] bytes from [src] to [dest]. */
681d7491b19516505e0754c66a3c8cd61811c9b6a6James Dongvoid* Memcpy(void* dest, const void* src, uint64_t n);
691d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
701d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
711d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong/* Implementations of the functions below must be built as part of the firmware
721d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * and defined in lib/utility.c */
731d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
741d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong/* Set [n] bytes starting at [s] to [c].  Returns dest. */
751d7491b19516505e0754c66a3c8cd61811c9b6a6James Dongvoid* Memset(void* dest, const uint8_t c, uint64_t n);
761d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
771d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong/* Compare [n] bytes starting at [s1] with [s2] and return 0 if they
781d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * match, 1 if they don't.  Returns 0 if n=0, since no bytes mismatched.
791d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * Time taken to perform the comparison is only dependent on [n] and
803eaa4e92dbebb5b27cb89f329f31fac6fb6fe1f0Chih-Chung Chang * not on the relationship of the match between [s1] and [s2].
813eaa4e92dbebb5b27cb89f329f31fac6fb6fe1f0Chih-Chung Chang *
821d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * Note that unlike Memcmp(), this only indicates inequality, not
831d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * whether s1 is less than or greater than s2.
84f5a8385649204213dc19e1a64f4693931e048d85James Dong */
851d7491b19516505e0754c66a3c8cd61811c9b6a6James Dongint SafeMemcmp(const void* s1, const void* s2, size_t n);
8613896b94336085d4e6ff555fa1a420312e8dea79James Dong
8713896b94336085d4e6ff555fa1a420312e8dea79James Dong/* Buffer size required to hold the longest possible output of
881d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * Uint64ToString() - that is, Uint64ToString(~0, 2). */
891d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#define UINT64_TO_STRING_MAX 65
901d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
911d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong/* Convert a value to a string in the specified radix (2=binary, 10=decimal,
921d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * 16=hex) and store it in <buf>, which is <bufsize> chars long.  If
931d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * <zero_pad_width>, left-pads the string to at least that width with '0'.
941d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * Returns the length of the stored string, not counting the terminating
951d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong * null. */
963eaa4e92dbebb5b27cb89f329f31fac6fb6fe1f0Chih-Chung Changuint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value,
973eaa4e92dbebb5b27cb89f329f31fac6fb6fe1f0Chih-Chung Chang                        uint32_t radix, uint32_t zero_pad_width);
981d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
991d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong/* Concatenate <src> onto <dest>, which has space for <destlen> characters
1008bb5603cc735315c8691dc9b7a81b4ad4d6e253eNipun Kwatra * including the terminating null.  Note that <dest> will always be
1018bb5603cc735315c8691dc9b7a81b4ad4d6e253eNipun Kwatra * null-terminated if <destlen> > 0.  Returns the number of characters
1028bb5603cc735315c8691dc9b7a81b4ad4d6e253eNipun Kwatra * used in <dest>, not counting the terminating null. */
1038bb5603cc735315c8691dc9b7a81b4ad4d6e253eNipun Kwatrauint32_t Strncat(char *dest, const char *src, uint32_t destlen);
1048bb5603cc735315c8691dc9b7a81b4ad4d6e253eNipun Kwatra
1058bb5603cc735315c8691dc9b7a81b4ad4d6e253eNipun Kwatra/* Ensure that only our stub implementations are used, not standard C */
1061d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#ifndef _STUB_IMPLEMENTATION_
1071d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#define malloc _do_not_use_standard_malloc
1081d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#define free _do_not_use_standard_free
1091d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#define memcmp _do_not_use_standard_memcmp
1101d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#define memcpy _do_not_use_standard_memcpy
1111d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#define memset _do_not_use_standard_memset
1121d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#endif
1131d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong
1141d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong#endif  /* VBOOT_REFERENCE_UTILITY_H_ */
1151d7491b19516505e0754c66a3c8cd61811c9b6a6James Dong