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