]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigMisc.c
SecurityPkg: Fix spelling errors
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / SecureBootConfigDxe / SecureBootConfigMisc.c
1 /** @file
2 Helper functions for SecureBoot configuration module.
3
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "SecureBootConfigImpl.h"
10
11 /**
12 Read file content into BufferPtr, the size of the allocate buffer
13 is *FileSize plus AdditionAllocateSize.
14
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.
20
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.
25
26 **/
27 EFI_STATUS
28 ReadFileContent (
29 IN EFI_FILE_HANDLE FileHandle,
30 IN OUT VOID **BufferPtr,
31 OUT UINTN *FileSize,
32 IN UINTN AdditionAllocateSize
33 )
34
35 {
36 UINTN BufferSize;
37 UINT64 SourceFileSize;
38 VOID *Buffer;
39 EFI_STATUS Status;
40
41 if ((FileHandle == NULL) || (FileSize == NULL)) {
42 return EFI_INVALID_PARAMETER;
43 }
44
45 Buffer = NULL;
46
47 //
48 // Get the file size
49 //
50 Status = FileHandle->SetPosition (FileHandle, (UINT64) -1);
51 if (EFI_ERROR (Status)) {
52 goto ON_EXIT;
53 }
54
55 Status = FileHandle->GetPosition (FileHandle, &SourceFileSize);
56 if (EFI_ERROR (Status)) {
57 goto ON_EXIT;
58 }
59
60 Status = FileHandle->SetPosition (FileHandle, 0);
61 if (EFI_ERROR (Status)) {
62 goto ON_EXIT;
63 }
64
65 BufferSize = (UINTN) SourceFileSize + AdditionAllocateSize;
66 Buffer = AllocateZeroPool(BufferSize);
67 if (Buffer == NULL) {
68 return EFI_OUT_OF_RESOURCES;
69 }
70
71 BufferSize = (UINTN) SourceFileSize;
72 *FileSize = BufferSize;
73
74 Status = FileHandle->Read (FileHandle, &BufferSize, Buffer);
75 if (EFI_ERROR (Status) || BufferSize != *FileSize) {
76 FreePool (Buffer);
77 Buffer = NULL;
78 Status = EFI_BAD_BUFFER_SIZE;
79 goto ON_EXIT;
80 }
81
82 ON_EXIT:
83
84 *BufferPtr = Buffer;
85 return Status;
86 }
87
88 /**
89 Close an open file handle.
90
91 @param[in] FileHandle The file handle to close.
92
93 **/
94 VOID
95 CloseFile (
96 IN EFI_FILE_HANDLE FileHandle
97 )
98 {
99 if (FileHandle != NULL) {
100 FileHandle->Close (FileHandle);
101 }
102 }
103
104 /**
105 Convert a nonnegative integer to an octet string of a specified length.
106
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
111
112 Returns:
113
114 @retval EFI_SUCCESS Data conversion successfully
115 @retval EFI_BUFFER_TOOL_SMALL Buffer is too small for output string
116
117 **/
118 EFI_STATUS
119 EFIAPI
120 Int2OctStr (
121 IN CONST UINTN *Integer,
122 IN UINTN IntSizeInWords,
123 OUT UINT8 *OctetString,
124 IN UINTN OSSizeInBytes
125 )
126 {
127 CONST UINT8 *Ptr1;
128 UINT8 *Ptr2;
129
130 for (Ptr1 = (CONST UINT8 *)Integer, Ptr2 = OctetString + OSSizeInBytes - 1;
131 Ptr1 < (UINT8 *)(Integer + IntSizeInWords) && Ptr2 >= OctetString;
132 Ptr1++, Ptr2--) {
133 *Ptr2 = *Ptr1;
134 }
135
136 for (; Ptr1 < (CONST UINT8 *)(Integer + IntSizeInWords) && *Ptr1 == 0; Ptr1++);
137
138 if (Ptr1 < (CONST UINT8 *)(Integer + IntSizeInWords)) {
139 return EFI_BUFFER_TOO_SMALL;
140 }
141
142 if (Ptr2 >= OctetString) {
143 ZeroMem (OctetString, Ptr2 - OctetString + 1);
144 }
145
146 return EFI_SUCCESS;
147 }
148
149 /**
150 Worker function that prints an EFI_GUID into specified Buffer.
151
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.
155
156 @retval Number of characters printed.
157
158 **/
159 UINTN
160 GuidToString (
161 IN EFI_GUID *Guid,
162 IN CHAR16 *Buffer,
163 IN UINTN BufferSize
164 )
165 {
166 UINTN Size;
167
168 Size = UnicodeSPrint (
169 Buffer,
170 BufferSize,
171 L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
172 (UINTN)Guid->Data1,
173 (UINTN)Guid->Data2,
174 (UINTN)Guid->Data3,
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]
183 );
184
185 //
186 // SPrint will null terminate the string. The -1 skips the null
187 //
188 return Size - 1;
189 }