Skip to content

Commit

Permalink
OvmfPkg: Add C runtime apis referenced by Boringssl
Browse files Browse the repository at this point in the history
As part of the effort to get Boringssl to work with EDK2, there are
many C runtime apis that get referenced and are currently not
implemented in EDK2. This change adds those apis to edk2.
In particular, this change adds the following :

Stubs for file functions. These have been implemented to return -1
rather than 0 since there is no file system support. This will ensure
that any calls into these apis will return failure.

Implementations of the following api which are referenced and used
by Boringssl code :
strdup : string duplication function
bsearch : binary search
getentropy : return entropy of requested length

Signed-off-by: Leena Soman <[email protected]>
  • Loading branch information
leenasoman committed Dec 13, 2024
1 parent c1eb477 commit 38679ef
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 27 deletions.
163 changes: 159 additions & 4 deletions CryptoPkg/Library/BaseCryptLib/SysCall/CrtWrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <CrtLibSupport.h>
#include <Uefi/UefiBaseType.h>
#include <Library/RngLib.h>

int errno = 0;

Expand Down Expand Up @@ -580,15 +582,168 @@ fopen (

size_t
fread (
void *b,
size_t c,
size_t i,
FILE *f
void *ptr,
size_t size,
size_t nmemb,
FILE *stream
)
{
return 0;
}

int
fputs (
const char *s,
FILE *stream
)
{
return -1;
}

int
fflush (
FILE *stream
)
{
return -1;
}

int
ferror (
FILE *stream
)
{
return -1;
}

int
fseek (
FILE *stream,
long offset,
int whence
)
{
return -1;
}

int
feof (
FILE *stream
)
{
return -1;
}

int
ftell (
FILE *stream
)
{
return -1;
}

char *
fgets (
char *s,
int size,
FILE *stream
)
{
return NULL;
}

char *
strdup (
char *s
)
{
UINTN Length;
VOID *Buffer;

if (!s) {
return NULL;
}

Length = strlen (s);

Buffer = malloc (Length);
if (Buffer == NULL) {
return NULL;
}

strncpy (Buffer, s, Length);
return Buffer;
}

/* Performs a binary search */
void *
bsearch (
const void *key,
const void *base,
size_t nmemb,
size_t size,
int ( *compar )(const void *, const void *)
)
{
void *Mid;
int Sign;

if (!key || !base || !nmemb || !size) {
return NULL;
}

while (nmemb > 0) {
Mid = (char *)base + size * (nmemb/2);
Sign = compar (key, Mid);
if (Sign < 0) {
nmemb /= 2;
} else if (Sign > 0) {
base = (char *)Mid + size;
nmemb -= nmemb/2 + 1;
} else {
return Mid;
}
}

return NULL;
}

/* Returns entropy of requested length in provided buffer */
int
getentropy (
void *buffer,
size_t length
)
{
UINT8 *EntropyBuffer = (UINT8 *)buffer;
UINTN Index;
UINT64 RandNum;
UINTN CopyLength;

if (length > GETENTROPY_MAX) {
errno = EIO;
return -1;
}

if (EntropyBuffer == NULL) {
errno = EFAULT;
return -1;
}

for (Index = 0; Index < length; Index += sizeof (UINT64)) {
if (!GetRandomNumber64 (&RandNum)) {
errno = ENOSYS;
return -1;
}

CopyLength =
(length - Index >= sizeof (UINT64)) ? sizeof (UINT64) : (length - Index);
CopyMem (EntropyBuffer + Index, &RandNum, CopyLength);
}

return 0;
}

uid_t
getuid (
void
Expand Down
142 changes: 119 additions & 23 deletions CryptoPkg/Library/Include/CrtLibSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,22 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
//
// Definitions for global constants used by CRT library routines
//
#define EINVAL 22 /* Invalid argument */
#define EAFNOSUPPORT 47 /* Address family not supported by protocol family */
#define INT_MAX 0x7FFFFFFF /* Maximum (signed) int value */
#define INT_MIN (-INT_MAX-1) /* Minimum (signed) int value */
#define LONG_MAX 0X7FFFFFFFL /* max value for a long */
#define LONG_MIN (-LONG_MAX-1) /* min value for a long */
#define UINT_MAX 0xFFFFFFFF /* Maximum unsigned int value */
#define ULONG_MAX 0xFFFFFFFF /* Maximum unsigned long value */
#define CHAR_BIT 8 /* Number of bits in a char */
#define SIZE_MAX 0xFFFFFFFF /* Maximum unsigned size_t */
#define ENOENT 2
#define EIO 5 /* I/O error */
#define ENOMEM 11 /* Out of memory */
#define EFAULT 14 /* Bad address */
#define EINVAL 22 /* Invalid argument */
#define ENOSYS 38 /* Function not implemented */
#define EAFNOSUPPORT 47 /* Address family not supported */
#define GETENTROPY_MAX 256
#define INT_MAX 0x7FFFFFFF /* Maximum (signed) int value */
#define INT_MIN (-INT_MAX-1) /* Minimum (signed) int value */
#define LONG_MAX 0X7FFFFFFFL /* max value for a long */
#define LONG_MIN (-LONG_MAX-1) /* min value for a long */
#define UINT_MAX 0xFFFFFFFF /* Maximum unsigned int value */
#define ULONG_MAX 0xFFFFFFFF /* Maximum unsigned long value */
#define CHAR_BIT 8 /* Number of bits in a char */
#define SIZE_MAX 0xFFFFFFFF /* Maximum unsigned size_t */

//
// Address families.
Expand All @@ -103,22 +109,52 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#define NS_INADDRSZ 4 /*%< IPv4 T_A */
#define NS_IN6ADDRSZ 16 /*%< IPv6 T_AAAA */

/* Defines for Boringssl build */
#define PRIx8 "x"
#define PRIx16 "x"
#define PRIx32 "x"
#define PRIx64 __PRI64_PREFIX "x"
#define PRIX32 "X"
#define __PRI64_PREFIX "l"
#define PRIu64 __PRI64_PREFIX "u"

#define INT32_MIN -0x80000000
#define INT32_MAX 0x7FFFFFFF
#define PTRDIFF_MAX 0x7FFFFFFFFFFFFFFF
#define INT64_C(c) c ## L

#ifndef __INT64_C
#define __INT64_C(c) c ## L
#endif

#define INT64_MIN (-__INT64_C(0x7FFFFFFFFFFFFFFF)-1)
#define INT64_MAX (__INT64_C(0x7FFFFFFFFFFFFFFF))
typedef int int32_t;

#ifndef UINT64_C
#define UINT64_C(c) c ## UL
#endif

#define UINT64_MAX (__UINT64_C(18446744073709551615))
#define UINT32_MAX (4294967295U)

//
// Basic types mapping
//
typedef UINTN size_t;
typedef UINTN off_t;
typedef UINTN u_int;
typedef UINTN intptr_t;
typedef INTN ptrdiff_t;
typedef INTN ssize_t;
typedef INT64 time_t;
typedef UINT8 __uint8_t;
typedef UINT8 sa_family_t;
typedef UINT8 u_char;
typedef UINT32 uid_t;
typedef UINT32 gid_t;
typedef CHAR16 wchar_t;
typedef UINTN size_t;
typedef UINTN off_t;
typedef UINTN u_int;
typedef UINTN intptr_t;
typedef INTN ptrdiff_t;
typedef INTN ssize_t;
typedef INT64 time_t;
typedef UINT8 __uint8_t;
typedef UINT8 sa_family_t;
typedef UINT8 u_char;
typedef UINT32 uid_t;
typedef UINT32 gid_t;
typedef CHAR16 wchar_t;
typedef short int16_t;

//
// File operations are not required for EFI building,
Expand Down Expand Up @@ -437,6 +473,66 @@ strcat (
const char *strSource
);

int
fflush (
FILE *stream
);

int
ferror (
FILE *stream
);

int
fseek (
FILE *stream,
long offset,
int whence
);

int
feof (
FILE *stream
);

char *
fgets (
char *s,
int size,
FILE *stream
);

int
ftell (
FILE *stream
);

int
fputs (
const char *s,
FILE *stream
);

char *
strdup (
char *s
);

void *
bsearch (
const void *key,
const void *base,
size_t nmemb,
size_t size,
int ( *compar )(const void *, const void *)
);

int
getentropy (
void *buffer,
size_t length
);

//
// Macros that directly map functions to BaseLib, BaseMemoryLib, and DebugLib functions
//
Expand Down

0 comments on commit 38679ef

Please sign in to comment.