2 Helper functions for SecureBoot configuration module.
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "SecureBootConfigImpl.h"
12 Read file content into BufferPtr, the size of the allocate buffer
13 is *FileSize plus AdditionAllocateSize.
15 @param[in] FileHandle The file to be read.
16 @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.
17 @param[out] FileSize Size of input file
18 @param[in] AdditionAllocateSize Addition size the buffer need to be allocated.
19 In case the buffer need to contain others besides the file content.
21 @retval EFI_SUCCESS The file was read into the buffer.
22 @retval EFI_INVALID_PARAMETER A parameter was invalid.
23 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
24 @retval others Unexpected error.
29 IN EFI_FILE_HANDLE FileHandle
,
30 IN OUT VOID
**BufferPtr
,
32 IN UINTN AdditionAllocateSize
37 UINT64 SourceFileSize
;
41 if ((FileHandle
== NULL
) || (FileSize
== NULL
)) {
42 return EFI_INVALID_PARAMETER
;
50 Status
= FileHandle
->SetPosition (FileHandle
, (UINT64
) -1);
51 if (EFI_ERROR (Status
)) {
55 Status
= FileHandle
->GetPosition (FileHandle
, &SourceFileSize
);
56 if (EFI_ERROR (Status
)) {
60 Status
= FileHandle
->SetPosition (FileHandle
, 0);
61 if (EFI_ERROR (Status
)) {
65 BufferSize
= (UINTN
) SourceFileSize
+ AdditionAllocateSize
;
66 Buffer
= AllocateZeroPool(BufferSize
);
68 return EFI_OUT_OF_RESOURCES
;
71 BufferSize
= (UINTN
) SourceFileSize
;
72 *FileSize
= BufferSize
;
74 Status
= FileHandle
->Read (FileHandle
, &BufferSize
, Buffer
);
75 if (EFI_ERROR (Status
) || BufferSize
!= *FileSize
) {
78 Status
= EFI_BAD_BUFFER_SIZE
;
89 Close an open file handle.
91 @param[in] FileHandle The file handle to close.
96 IN EFI_FILE_HANDLE FileHandle
99 if (FileHandle
!= NULL
) {
100 FileHandle
->Close (FileHandle
);
105 Convert a nonnegative integer to an octet string of a specified length.
107 @param[in] Integer Pointer to the nonnegative integer to be converted
108 @param[in] IntSizeInWords Length of integer buffer in words
109 @param[out] OctetString Converted octet string of the specified length
110 @param[in] OSSizeInBytes Intended length of resulting octet string in bytes
114 @retval EFI_SUCCESS Data conversion successfully
115 @retval EFI_BUFFER_TOOL_SMALL Buffer is too small for output string
121 IN CONST UINTN
*Integer
,
122 IN UINTN IntSizeInWords
,
123 OUT UINT8
*OctetString
,
124 IN UINTN OSSizeInBytes
130 for (Ptr1
= (CONST UINT8
*)Integer
, Ptr2
= OctetString
+ OSSizeInBytes
- 1;
131 Ptr1
< (UINT8
*)(Integer
+ IntSizeInWords
) && Ptr2
>= OctetString
;
136 for (; Ptr1
< (CONST UINT8
*)(Integer
+ IntSizeInWords
) && *Ptr1
== 0; Ptr1
++);
138 if (Ptr1
< (CONST UINT8
*)(Integer
+ IntSizeInWords
)) {
139 return EFI_BUFFER_TOO_SMALL
;
142 if (Ptr2
>= OctetString
) {
143 ZeroMem (OctetString
, Ptr2
- OctetString
+ 1);
150 Worker function that prints an EFI_GUID into specified Buffer.
152 @param[in] Guid Pointer to GUID to print.
153 @param[in] Buffer Buffer to print Guid into.
154 @param[in] BufferSize Size of Buffer.
156 @retval Number of characters printed.
168 Size
= UnicodeSPrint (
171 L
"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
175 (UINTN
)Guid
->Data4
[0],
176 (UINTN
)Guid
->Data4
[1],
177 (UINTN
)Guid
->Data4
[2],
178 (UINTN
)Guid
->Data4
[3],
179 (UINTN
)Guid
->Data4
[4],
180 (UINTN
)Guid
->Data4
[5],
181 (UINTN
)Guid
->Data4
[6],
182 (UINTN
)Guid
->Data4
[7]
186 // SPrint will null terminate the string. The -1 skips the null