utility.h revision 0600e41e0fd2912486832a150f75765ab6c90a2b
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
46518d4f39b4ab5fa7d516de397795bc146250c51bBill Richardson/* Optional, up to the BIOS */
47518d4f39b4ab5fa7d516de397795bc146250c51bBill Richardson#ifdef VBOOT_EASTER_EGG
480600e41e0fd2912486832a150f75765ab6c90a2bBill Richardson#define VBEASTEREGG VbExEasterEgg()
49518d4f39b4ab5fa7d516de397795bc146250c51bBill Richardson#else
500600e41e0fd2912486832a150f75765ab6c90a2bBill Richardson#define VBEASTEREGG 0
51518d4f39b4ab5fa7d516de397795bc146250c51bBill Richardson#endif
52518d4f39b4ab5fa7d516de397795bc146250c51bBill Richardson
53ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and
54ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah * [lsw] forming the most and least signficant 16-bit words.
55ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah */
56f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler#define CombineUint16Pair(msw,lsw) (((uint32_t)(msw) << 16) |   \
57ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah                                    (((lsw)) & 0xFFFF))
58ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Return the minimum of (a) or (b). */
59ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define Min(a, b) (((a) < (b)) ? (a) : (b))
60ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah
6137f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo/* Compare [n] bytes in [src1] and [src2]
6237f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * Returns an integer less than, equal to, or greater than zero if the first [n]
6337f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * bytes of [src1] is found, respectively, to be less than, to match, or be
6437f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * greater than the first n bytes of [src2]. */
6537f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Loint Memcmp(const void* src1, const void* src2, size_t n);
6637f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo
67322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Copy [n] bytes from [src] to [dest]. */
683ecaf776d82d29573be083b2e5c6ddc5b9f49c70vbendebvoid* Memcpy(void* dest, const void* src, uint64_t n);
69322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
7037dff84dbbe7fc683fce04382e68eca7aa19ea83Gaurav Shah
7137dff84dbbe7fc683fce04382e68eca7aa19ea83Gaurav Shah/* Implementations of the functions below must be built as part of the firmware
7237dff84dbbe7fc683fce04382e68eca7aa19ea83Gaurav Shah * and defined in lib/utility.c */
7337dff84dbbe7fc683fce04382e68eca7aa19ea83Gaurav Shah
74a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler/* Set [n] bytes starting at [s] to [c].  Returns dest. */
75a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spanglervoid* Memset(void* dest, const uint8_t c, uint64_t n);
76d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah
77a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler/* Compare [n] bytes starting at [s1] with [s2] and return 0 if they
78a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * match, 1 if they don't.  Returns 0 if n=0, since no bytes mismatched.
79a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * Time taken to perform the comparison is only dependent on [n] and
80a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * not on the relationship of the match between [s1] and [s2].
81a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler *
82a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * Note that unlike Memcmp(), this only indicates inequality, not
83a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * whether s1 is less than or greater than s2.
84322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
85322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahint SafeMemcmp(const void* s1, const void* s2, size_t n);
86322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
87bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler/* Buffer size required to hold the longest possible output of
88bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * Uint64ToString() - that is, Uint64ToString(~0, 2). */
89bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler#define UINT64_TO_STRING_MAX 65
90bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler
91bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler/* Convert a value to a string in the specified radix (2=binary, 10=decimal,
92bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * 16=hex) and store it in <buf>, which is <bufsize> chars long.  If
93bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * <zero_pad_width>, left-pads the string to at least that width with '0'.
94bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * Returns the length of the stored string, not counting the terminating
95bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * null. */
96bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangleruint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value,
97bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler                        uint32_t radix, uint32_t zero_pad_width);
98bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler
99bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler/* Concatenate <src> onto <dest>, which has space for <destlen> characters
100bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * including the terminating null.  Note that <dest> will always be
101bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * null-terminated if <destlen> > 0.  Returns the number of characters
102bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * used in <dest>, not counting the terminating null. */
103bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangleruint32_t Strncat(char *dest, const char *src, uint32_t destlen);
104bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler
105d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson/* Ensure that only our stub implementations are used, not standard C */
106d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#ifndef _STUB_IMPLEMENTATION_
107d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define malloc _do_not_use_standard_malloc
108d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define free _do_not_use_standard_free
109d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memcmp _do_not_use_standard_memcmp
110d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memcpy _do_not_use_standard_memcpy
111d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memset _do_not_use_standard_memset
112d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#endif
113f5db4b86fa5569285dfc8373fd8977e1998b8066Bill Richardson
114322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#endif  /* VBOOT_REFERENCE_UTILITY_H_ */
115