utility.h revision a3454fcaa415b2c99514c44eebee7325fe0d1f9f
1322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * Use of this source code is governed by a BSD-style license that can be
3322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * found in the LICENSE file.
4322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
5322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
6322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Helper functions/wrappers for memory allocations, manipulation and
7322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * comparison.
8322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
9322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
10322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#ifndef VBOOT_REFERENCE_UTILITY_H_
11322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#define VBOOT_REFERENCE_UTILITY_H_
12322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
13f302905224a346718910e56f5f1593d4b19253f1Randall Spangler#include "sysincludes.h"
14322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
15e2ec98412e3a6e24620f8d8730c50c79b7ce7d25Randall Spangler/* Debug and error output */
16e2ec98412e3a6e24620f8d8730c50c79b7ce7d25Randall Spangler#ifdef VBOOT_DEBUG
17e49e8af65fce38da7a308305566f8a14f102254aRandall Spangler#define VBDEBUG(params) VbExDebug params
18e2ec98412e3a6e24620f8d8730c50c79b7ce7d25Randall Spangler#else
19e2ec98412e3a6e24620f8d8730c50c79b7ce7d25Randall Spangler#define VBDEBUG(params)
20e2ec98412e3a6e24620f8d8730c50c79b7ce7d25Randall Spangler#endif
21e2ec98412e3a6e24620f8d8730c50c79b7ce7d25Randall Spangler
22c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler#ifndef VBOOT_PERFORMANCE
23c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler/* Define performance macros as nothing.  If you enable VBOOT_PERFORMANCE,
24c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler * you must define these macros in your platform's biosincludes.h.
25c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler *
26c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler * Intended usage for using a performance counter called 'foo':
27c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler *
28c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler * VBPERFSTART("foo")
29c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler * ...code to be tested...
30c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler * VBPERFEND("foo")
31c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler *
32c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler * Names should be <= 8 characters to be compatible with all platforms.
33c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler */
34c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler#define VBPERFSTART(name)
35c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler#define VBPERFEND(name)
36c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler#endif
37c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler
38c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler#ifdef VBOOT_DEBUG
3932a6526d25d4bf9a1c137fc3d275d1c68935d184Randall Spangler#define VbAssert(expr) do { if (!(expr)) { \
40e49e8af65fce38da7a308305566f8a14f102254aRandall Spangler    VbExError("assert fail: %s at %s:%d\n", \
41e49e8af65fce38da7a308305566f8a14f102254aRandall Spangler              #expr, __FILE__, __LINE__); }} while(0)
42c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler#else
4332a6526d25d4bf9a1c137fc3d275d1c68935d184Randall Spangler#define VbAssert(expr)
44c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler#endif
45ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah
46ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and
47ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah * [lsw] forming the most and least signficant 16-bit words.
48ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah */
49ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define CombineUint16Pair(msw,lsw) (((msw) << 16) |     \
50ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah                                    (((lsw)) & 0xFFFF))
51ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Return the minimum of (a) or (b). */
52ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define Min(a, b) (((a) < (b)) ? (a) : (b))
53ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah
5437f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo/* Compare [n] bytes in [src1] and [src2]
5537f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * Returns an integer less than, equal to, or greater than zero if the first [n]
5637f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * bytes of [src1] is found, respectively, to be less than, to match, or be
5737f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * greater than the first n bytes of [src2]. */
5837f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Loint Memcmp(const void* src1, const void* src2, size_t n);
5937f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo
60322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Copy [n] bytes from [src] to [dest]. */
613ecaf776d82d29573be083b2e5c6ddc5b9f49c70vbendebvoid* Memcpy(void* dest, const void* src, uint64_t n);
62322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
6337dff84dbbe7fc683fce04382e68eca7aa19ea83Gaurav Shah
6437dff84dbbe7fc683fce04382e68eca7aa19ea83Gaurav Shah/* Implementations of the functions below must be built as part of the firmware
6537dff84dbbe7fc683fce04382e68eca7aa19ea83Gaurav Shah * and defined in lib/utility.c */
6637dff84dbbe7fc683fce04382e68eca7aa19ea83Gaurav Shah
67a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler/* Set [n] bytes starting at [s] to [c].  Returns dest. */
68a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spanglervoid* Memset(void* dest, const uint8_t c, uint64_t n);
69d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah
70a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler/* Compare [n] bytes starting at [s1] with [s2] and return 0 if they
71a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * match, 1 if they don't.  Returns 0 if n=0, since no bytes mismatched.
72a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * Time taken to perform the comparison is only dependent on [n] and
73a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * not on the relationship of the match between [s1] and [s2].
74a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler *
75a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * Note that unlike Memcmp(), this only indicates inequality, not
76a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * whether s1 is less than or greater than s2.
77322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
78322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahint SafeMemcmp(const void* s1, const void* s2, size_t n);
79322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
80bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler/* Buffer size required to hold the longest possible output of
81bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * Uint64ToString() - that is, Uint64ToString(~0, 2). */
82bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler#define UINT64_TO_STRING_MAX 65
83bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler
84bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler/* Convert a value to a string in the specified radix (2=binary, 10=decimal,
85bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * 16=hex) and store it in <buf>, which is <bufsize> chars long.  If
86bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * <zero_pad_width>, left-pads the string to at least that width with '0'.
87bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * Returns the length of the stored string, not counting the terminating
88bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * null. */
89bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangleruint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value,
90bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler                        uint32_t radix, uint32_t zero_pad_width);
91bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler
92bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler/* Concatenate <src> onto <dest>, which has space for <destlen> characters
93bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * including the terminating null.  Note that <dest> will always be
94bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * null-terminated if <destlen> > 0.  Returns the number of characters
95bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * used in <dest>, not counting the terminating null. */
96bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangleruint32_t Strncat(char *dest, const char *src, uint32_t destlen);
97bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler
98d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson/* Ensure that only our stub implementations are used, not standard C */
99d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#ifndef _STUB_IMPLEMENTATION_
100d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define malloc _do_not_use_standard_malloc
101d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define free _do_not_use_standard_free
102d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memcmp _do_not_use_standard_memcmp
103d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memcpy _do_not_use_standard_memcpy
104d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memset _do_not_use_standard_memset
105d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#endif
106f5db4b86fa5569285dfc8373fd8977e1998b8066Bill Richardson
107322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#endif  /* VBOOT_REFERENCE_UTILITY_H_ */
108