2 Helper functions for SecureBoot configuration module.
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "SecureBootConfigImpl.h"
18 Read file content into BufferPtr, the size of the allocate buffer
19 is *FileSize plus AddtionAllocateSize.
21 @param[in] FileHandle The file to be read.
22 @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.
23 @param[out] FileSize Size of input file
24 @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated.
25 In case the buffer need to contain others besides the file content.
27 @retval EFI_SUCCESS The file was read into the buffer.
28 @retval EFI_INVALID_PARAMETER A parameter was invalid.
29 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
30 @retval others Unexpected error.
35 IN EFI_FILE_HANDLE FileHandle
,
36 IN OUT VOID
**BufferPtr
,
38 IN UINTN AddtionAllocateSize
43 UINT64 SourceFileSize
;
47 if ((FileHandle
== NULL
) || (FileSize
== NULL
)) {
48 return EFI_INVALID_PARAMETER
;
56 Status
= FileHandle
->SetPosition (FileHandle
, (UINT64
) -1);
57 if (EFI_ERROR (Status
)) {
61 Status
= FileHandle
->GetPosition (FileHandle
, &SourceFileSize
);
62 if (EFI_ERROR (Status
)) {
66 Status
= FileHandle
->SetPosition (FileHandle
, 0);
67 if (EFI_ERROR (Status
)) {
71 BufferSize
= (UINTN
) SourceFileSize
+ AddtionAllocateSize
;
72 Buffer
= AllocateZeroPool(BufferSize
);
74 return EFI_OUT_OF_RESOURCES
;
77 BufferSize
= (UINTN
) SourceFileSize
;
78 *FileSize
= BufferSize
;
80 Status
= FileHandle
->Read (FileHandle
, &BufferSize
, Buffer
);
81 if (EFI_ERROR (Status
) || BufferSize
!= *FileSize
) {
84 Status
= EFI_BAD_BUFFER_SIZE
;
95 Close an open file handle.
97 @param[in] FileHandle The file handle to close.
102 IN EFI_FILE_HANDLE FileHandle
105 if (FileHandle
!= NULL
) {
106 FileHandle
->Close (FileHandle
);
111 Convert a nonnegative integer to an octet string of a specified length.
113 @param[in] Integer Pointer to the nonnegative integer to be converted
114 @param[in] IntSizeInWords Length of integer buffer in words
115 @param[out] OctetString Converted octet string of the specified length
116 @param[in] OSSizeInBytes Intended length of resulting octet string in bytes
120 @retval EFI_SUCCESS Data conversion successfully
121 @retval EFI_BUFFER_TOOL_SMALL Buffer is too small for output string
127 IN CONST UINTN
*Integer
,
128 IN UINTN IntSizeInWords
,
129 OUT UINT8
*OctetString
,
130 IN UINTN OSSizeInBytes
136 for (Ptr1
= (CONST UINT8
*)Integer
, Ptr2
= OctetString
+ OSSizeInBytes
- 1;
137 Ptr1
< (UINT8
*)(Integer
+ IntSizeInWords
) && Ptr2
>= OctetString
;
142 for (; Ptr1
< (CONST UINT8
*)(Integer
+ IntSizeInWords
) && *Ptr1
== 0; Ptr1
++);
144 if (Ptr1
< (CONST UINT8
*)(Integer
+ IntSizeInWords
)) {
145 return EFI_BUFFER_TOO_SMALL
;
148 if (Ptr2
>= OctetString
) {
149 ZeroMem (OctetString
, Ptr2
- OctetString
+ 1);
156 Worker function that prints an EFI_GUID into specified Buffer.
158 @param[in] Guid Pointer to GUID to print.
159 @param[in] Buffer Buffer to print Guid into.
160 @param[in] BufferSize Size of Buffer.
162 @retval Number of characters printed.
174 Size
= UnicodeSPrint (
177 L
"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
181 (UINTN
)Guid
->Data4
[0],
182 (UINTN
)Guid
->Data4
[1],
183 (UINTN
)Guid
->Data4
[2],
184 (UINTN
)Guid
->Data4
[3],
185 (UINTN
)Guid
->Data4
[4],
186 (UINTN
)Guid
->Data4
[5],
187 (UINTN
)Guid
->Data4
[6],
188 (UINTN
)Guid
->Data4
[7]
192 // SPrint will null terminate the string. The -1 skips the null