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