1a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler/* Copyright (c) 2013 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 6a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler/* 7a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * Helper functions/wrappers for memory allocations, manipulation and 8322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * comparison. 9322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */ 10322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 11322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#ifndef VBOOT_REFERENCE_UTILITY_H_ 12322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#define VBOOT_REFERENCE_UTILITY_H_ 13322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 14f302905224a346718910e56f5f1593d4b19253f1Randall Spangler#include "sysincludes.h" 1505f5944a40dca2a92070f26dad0bfbb57f59aca1Mike Frysinger#include "vboot_api.h" 16322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 17e2ec98412e3a6e24620f8d8730c50c79b7ce7d25Randall Spangler/* Debug and error output */ 18e2ec98412e3a6e24620f8d8730c50c79b7ce7d25Randall Spangler#ifdef VBOOT_DEBUG 19e49e8af65fce38da7a308305566f8a14f102254aRandall Spangler#define VBDEBUG(params) VbExDebug params 20e2ec98412e3a6e24620f8d8730c50c79b7ce7d25Randall Spangler#else 21e2ec98412e3a6e24620f8d8730c50c79b7ce7d25Randall Spangler#define VBDEBUG(params) 22e2ec98412e3a6e24620f8d8730c50c79b7ce7d25Randall Spangler#endif 23e2ec98412e3a6e24620f8d8730c50c79b7ce7d25Randall Spangler 24c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler#ifdef VBOOT_DEBUG 2532a6526d25d4bf9a1c137fc3d275d1c68935d184Randall Spangler#define VbAssert(expr) do { if (!(expr)) { \ 26e49e8af65fce38da7a308305566f8a14f102254aRandall Spangler VbExError("assert fail: %s at %s:%d\n", \ 27e49e8af65fce38da7a308305566f8a14f102254aRandall Spangler #expr, __FILE__, __LINE__); }} while(0) 28c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler#else 2932a6526d25d4bf9a1c137fc3d275d1c68935d184Randall Spangler#define VbAssert(expr) 30c93347b3c2d07f8f6022f205a1558a3442cfab35Randall Spangler#endif 31ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah 32518d4f39b4ab5fa7d516de397795bc146250c51bBill Richardson/* Optional, up to the BIOS */ 33518d4f39b4ab5fa7d516de397795bc146250c51bBill Richardson#ifdef VBOOT_EASTER_EGG 340600e41e0fd2912486832a150f75765ab6c90a2bBill Richardson#define VBEASTEREGG VbExEasterEgg() 35518d4f39b4ab5fa7d516de397795bc146250c51bBill Richardson#else 360600e41e0fd2912486832a150f75765ab6c90a2bBill Richardson#define VBEASTEREGG 0 37518d4f39b4ab5fa7d516de397795bc146250c51bBill Richardson#endif 38518d4f39b4ab5fa7d516de397795bc146250c51bBill Richardson 39a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler/* 40a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and [lsw] 41a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * forming the most and least signficant 16-bit words. 42ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah */ 43f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler#define CombineUint16Pair(msw,lsw) (((uint32_t)(msw) << 16) | \ 44ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah (((lsw)) & 0xFFFF)) 45a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler 46ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Return the minimum of (a) or (b). */ 47ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define Min(a, b) (((a) < (b)) ? (a) : (b)) 48ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah 49a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler/** 50a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * Compare [n] bytes in [src1] and [src2]. 51a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * 52a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * Returns an integer less than, equal to, or greater than zero if the first 53a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * [n] bytes of [src1] is found, respectively, to be less than, to match, or be 5437f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * greater than the first n bytes of [src2]. */ 55a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spanglerint Memcmp(const void *src1, const void *src2, size_t n); 56322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 57a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler/** 58a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * Copy [n] bytes from [src] to [dest]. 59a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler */ 60a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spanglervoid *Memcpy(void *dest, const void *src, uint64_t n); 6137dff84dbbe7fc683fce04382e68eca7aa19ea83Gaurav Shah 62a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler/* 63a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * Implementations of the functions below must be built as part of the firmware 64a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * and defined in lib/utility.c. 65a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler */ 6637dff84dbbe7fc683fce04382e68eca7aa19ea83Gaurav Shah 67a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler/** 68a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * Set [n] bytes starting at [s] to [c]. Returns dest. 69a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler */ 70a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spanglervoid *Memset(void *dest, const uint8_t c, uint64_t n); 71d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 72a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler/** 73a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * Compare [n] bytes starting at [s1] with [s2] and return 0 if they 74a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * match, 1 if they don't. Returns 0 if n=0, since no bytes mismatched. 75a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * 76a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * Time taken to perform the comparison is only dependent on [n] and 77a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * not on the relationship of the match between [s1] and [s2]. 78a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * 79a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * Note that unlike Memcmp(), this only indicates inequality, not 80a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * whether s1 is less than or greater than s2. 81322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */ 82a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spanglerint SafeMemcmp(const void *s1, const void *s2, size_t n); 83322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 84a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler/* 85a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * Buffer size required to hold the longest possible output of Uint64ToString() 86a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * - that is, Uint64ToString(~0, 2). 87a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler */ 88bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler#define UINT64_TO_STRING_MAX 65 89bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler 90a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler/** 91a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall 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'. 94a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * Returns the length of the stored string, not counting the terminating null. 95a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler */ 96bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangleruint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value, 97bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler uint32_t radix, uint32_t zero_pad_width); 98bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler 99a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler/** 100a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * Concatenate <src> onto <dest>, which has space for <destlen> characters 101bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler * including the terminating null. Note that <dest> will always be 102a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * null-terminated if <destlen> > 0. Returns the number of characters used in 103a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler * <dest>, not counting the terminating null. 104a2db67d204c0dd3d152ff54958bf42c5dbe394ffRandall Spangler */ 1055fed2a667096341160db8643a4a057e328953a1dBill Richardsonuint32_t StrnAppend(char *dest, const char *src, uint32_t destlen); 106bebe53c9193dbe875d793c7cb124ed66e610e1bdRandall Spangler 107d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson/* Ensure that only our stub implementations are used, not standard C */ 108d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#ifndef _STUB_IMPLEMENTATION_ 109d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define malloc _do_not_use_standard_malloc 110d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define free _do_not_use_standard_free 111d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memcmp _do_not_use_standard_memcmp 112d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memcpy _do_not_use_standard_memcpy 113d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memset _do_not_use_standard_memset 114d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#endif 115f5db4b86fa5569285dfc8373fd8977e1998b8066Bill Richardson 116322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#endif /* VBOOT_REFERENCE_UTILITY_H_ */ 117