blob: aaddb8c87722ac05a6ac483988a172fd6736d02d [file] [log] [blame]
Randall Spanglera2db67d2013-01-24 16:15:35 -08001/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
Gaurav Shah322536d2010-01-28 15:01:23 -08002 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
Randall Spanglera2db67d2013-01-24 16:15:35 -08006/*
7 * Helper functions/wrappers for memory allocations, manipulation and
Gaurav Shah322536d2010-01-28 15:01:23 -08008 * comparison.
9 */
10
11#ifndef VBOOT_REFERENCE_UTILITY_H_
12#define VBOOT_REFERENCE_UTILITY_H_
13
Randall Spanglerf3029052010-06-16 13:42:58 -070014#include "sysincludes.h"
Mike Frysinger05f59442013-06-21 20:32:16 -040015#include "vboot_api.h"
Gaurav Shah322536d2010-01-28 15:01:23 -080016
Randall Spanglere2ec9842010-06-23 21:17:07 -070017/* Debug and error output */
18#ifdef VBOOT_DEBUG
Randall Spanglere49e8af2011-07-08 13:03:32 -070019#define VBDEBUG(params) VbExDebug params
Randall Spanglere2ec9842010-06-23 21:17:07 -070020#else
21#define VBDEBUG(params)
22#endif
23
Randall Spanglerc93347b2010-10-04 12:45:49 -070024#ifdef VBOOT_DEBUG
Randall Spangler32a65262011-06-27 10:49:11 -070025#define VbAssert(expr) do { if (!(expr)) { \
Randall Spanglere49e8af2011-07-08 13:03:32 -070026 VbExError("assert fail: %s at %s:%d\n", \
27 #expr, __FILE__, __LINE__); }} while(0)
Randall Spanglerc93347b2010-10-04 12:45:49 -070028#else
Randall Spangler32a65262011-06-27 10:49:11 -070029#define VbAssert(expr)
Randall Spanglerc93347b2010-10-04 12:45:49 -070030#endif
Gaurav Shahed9c96a2010-03-30 18:56:07 -070031
Bill Richardson518d4f32011-09-13 15:28:55 -070032/* Optional, up to the BIOS */
33#ifdef VBOOT_EASTER_EGG
Bill Richardson0600e412011-10-11 15:00:17 -070034#define VBEASTEREGG VbExEasterEgg()
Bill Richardson518d4f32011-09-13 15:28:55 -070035#else
Bill Richardson0600e412011-10-11 15:00:17 -070036#define VBEASTEREGG 0
Bill Richardson518d4f32011-09-13 15:28:55 -070037#endif
38
Randall Spanglera2db67d2013-01-24 16:15:35 -080039/*
40 * Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and [lsw]
41 * forming the most and least signficant 16-bit words.
Gaurav Shahce0cc302010-03-24 13:48:55 -070042 */
Randall Spanglerf02bbb42011-08-24 14:16:01 -070043#define CombineUint16Pair(msw,lsw) (((uint32_t)(msw) << 16) | \
Gaurav Shahce0cc302010-03-24 13:48:55 -070044 (((lsw)) & 0xFFFF))
Randall Spanglera2db67d2013-01-24 16:15:35 -080045
Gaurav Shahce0cc302010-03-24 13:48:55 -070046/* Return the minimum of (a) or (b). */
47#define Min(a, b) (((a) < (b)) ? (a) : (b))
48
Randall Spanglera2db67d2013-01-24 16:15:35 -080049/**
Randall Spanglera2db67d2013-01-24 16:15:35 -080050 * Compare [n] bytes starting at [s1] with [s2] and return 0 if they
Randall Spanglera3454fc2011-08-23 14:41:18 -070051 * match, 1 if they don't. Returns 0 if n=0, since no bytes mismatched.
Randall Spanglera2db67d2013-01-24 16:15:35 -080052 *
Randall Spanglera3454fc2011-08-23 14:41:18 -070053 * Time taken to perform the comparison is only dependent on [n] and
54 * not on the relationship of the match between [s1] and [s2].
55 *
Randall Spangler664096b2016-10-13 16:16:41 -070056 * Note that unlike memcmp(), this only indicates inequality, not
Randall Spanglera3454fc2011-08-23 14:41:18 -070057 * whether s1 is less than or greater than s2.
Gaurav Shah322536d2010-01-28 15:01:23 -080058 */
Randall Spanglera2db67d2013-01-24 16:15:35 -080059int SafeMemcmp(const void *s1, const void *s2, size_t n);
Gaurav Shah322536d2010-01-28 15:01:23 -080060
Randall Spanglera2db67d2013-01-24 16:15:35 -080061/*
62 * Buffer size required to hold the longest possible output of Uint64ToString()
63 * - that is, Uint64ToString(~0, 2).
64 */
Randall Spanglerbebe53c2011-07-07 10:30:25 -070065#define UINT64_TO_STRING_MAX 65
66
Randall Spanglera2db67d2013-01-24 16:15:35 -080067/**
68 * Convert a value to a string in the specified radix (2=binary, 10=decimal,
Randall Spanglerbebe53c2011-07-07 10:30:25 -070069 * 16=hex) and store it in <buf>, which is <bufsize> chars long. If
70 * <zero_pad_width>, left-pads the string to at least that width with '0'.
Randall Spanglera2db67d2013-01-24 16:15:35 -080071 * Returns the length of the stored string, not counting the terminating null.
72 */
Randall Spanglerbebe53c2011-07-07 10:30:25 -070073uint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value,
74 uint32_t radix, uint32_t zero_pad_width);
75
Randall Spanglera2db67d2013-01-24 16:15:35 -080076/**
77 * Concatenate <src> onto <dest>, which has space for <destlen> characters
Randall Spanglerbebe53c2011-07-07 10:30:25 -070078 * including the terminating null. Note that <dest> will always be
Randall Spanglera2db67d2013-01-24 16:15:35 -080079 * null-terminated if <destlen> > 0. Returns the number of characters used in
80 * <dest>, not counting the terminating null.
81 */
Bill Richardson5fed2a62013-03-04 15:11:38 -080082uint32_t StrnAppend(char *dest, const char *src, uint32_t destlen);
Randall Spanglerbebe53c2011-07-07 10:30:25 -070083
Bill Richardsond6ff7212010-05-27 12:27:32 -070084/* Ensure that only our stub implementations are used, not standard C */
85#ifndef _STUB_IMPLEMENTATION_
86#define malloc _do_not_use_standard_malloc
87#define free _do_not_use_standard_free
Bill Richardsond6ff7212010-05-27 12:27:32 -070088#endif
Bill Richardsonf5db4b82010-05-27 11:15:14 -070089
Gaurav Shah322536d2010-01-28 15:01:23 -080090#endif /* VBOOT_REFERENCE_UTILITY_H_ */