2 Helper functions for SecureBoot configuration module.
4 Copyright (c) 2012, 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);
158 Convert a String to Guid Value.
160 @param[in] Str Specifies the String to be converted.
161 @param[in] StrLen Number of Unicode Characters of String (exclusive \0)
162 @param[out] Guid Return the result Guid value.
164 @retval EFI_SUCCESS The operation is finished successfully.
165 @retval EFI_NOT_FOUND Invalid string.
182 Buffer
= (CHAR16
*) AllocateZeroPool (sizeof (CHAR16
) * (StrLen
+ 1));
183 if (Buffer
== NULL
) {
184 return EFI_OUT_OF_RESOURCES
;
187 StrCpy (Buffer
, Str
);
193 PtrPosition
= PtrBuffer
;
194 while (*PtrBuffer
!= L
'\0') {
195 if (*PtrBuffer
== L
'-') {
200 if (*PtrBuffer
== L
'\0') {
202 return EFI_NOT_FOUND
;
206 Data
= StrHexToUintn (PtrPosition
);
207 Guid
->Data1
= (UINT32
)Data
;
213 PtrPosition
= PtrBuffer
;
214 while (*PtrBuffer
!= L
'\0') {
215 if (*PtrBuffer
== L
'-') {
220 if (*PtrBuffer
== L
'\0') {
222 return EFI_NOT_FOUND
;
225 Data
= StrHexToUintn (PtrPosition
);
226 Guid
->Data2
= (UINT16
)Data
;
232 PtrPosition
= PtrBuffer
;
233 while (*PtrBuffer
!= L
'\0') {
234 if (*PtrBuffer
== L
'-') {
239 if (*PtrBuffer
== L
'\0') {
241 return EFI_NOT_FOUND
;
244 Data
= StrHexToUintn (PtrPosition
);
245 Guid
->Data3
= (UINT16
)Data
;
250 for ( Index
= 0 ; Index
< 2 ; Index
++) {
252 if ((*PtrBuffer
== L
'\0') || ( *(PtrBuffer
+ 1) == L
'\0')) {
254 return EFI_NOT_FOUND
;
256 Digits
[0] = *PtrBuffer
;
258 Digits
[1] = *PtrBuffer
;
260 Data
= StrHexToUintn (Digits
);
261 Guid
->Data4
[Index
] = (UINT8
)Data
;
268 if ((*PtrBuffer
!= L
'-' ) || ( *PtrBuffer
== L
'\0')) {
269 return EFI_NOT_FOUND
;
275 for ( ; Index
< 8; Index
++) {
277 if ((*PtrBuffer
== L
'\0') || ( *(PtrBuffer
+ 1) == L
'\0')) {
279 return EFI_NOT_FOUND
;
281 Digits
[0] = *PtrBuffer
;
283 Digits
[1] = *PtrBuffer
;
285 Data
= StrHexToUintn (Digits
);
286 Guid
->Data4
[Index
] = (UINT8
)Data
;
295 Worker function that prints an EFI_GUID into specified Buffer.
297 @param[in] Guid Pointer to GUID to print.
298 @param[in] Buffer Buffer to print Guid into.
299 @param[in] BufferSize Size of Buffer.
301 @retval Number of characters printed.
313 Size
= UnicodeSPrint (
316 L
"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
320 (UINTN
)Guid
->Data4
[0],
321 (UINTN
)Guid
->Data4
[1],
322 (UINTN
)Guid
->Data4
[2],
323 (UINTN
)Guid
->Data4
[3],
324 (UINTN
)Guid
->Data4
[4],
325 (UINTN
)Guid
->Data4
[5],
326 (UINTN
)Guid
->Data4
[6],
327 (UINTN
)Guid
->Data4
[7]
331 // SPrint will null terminate the string. The -1 skips the null