]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigMisc.c
SecurityPkg: Apply uncrustify changes
[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
d6b926e7 13 is *FileSize plus AdditionAllocateSize.\r
ecc722ad 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
d6b926e7 18 @param[in] AdditionAllocateSize Addition 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
c411b485
MK
29 IN EFI_FILE_HANDLE FileHandle,\r
30 IN OUT VOID **BufferPtr,\r
31 OUT UINTN *FileSize,\r
32 IN UINTN AdditionAllocateSize\r
ecc722ad 33 )\r
34\r
35{\r
c411b485
MK
36 UINTN BufferSize;\r
37 UINT64 SourceFileSize;\r
38 VOID *Buffer;\r
39 EFI_STATUS Status;\r
ecc722ad 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
c411b485 50 Status = FileHandle->SetPosition (FileHandle, (UINT64)-1);\r
ecc722ad 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
c411b485
MK
65 BufferSize = (UINTN)SourceFileSize + AdditionAllocateSize;\r
66 Buffer = AllocateZeroPool (BufferSize);\r
ecc722ad 67 if (Buffer == NULL) {\r
68 return EFI_OUT_OF_RESOURCES;\r
69 }\r
70\r
c411b485 71 BufferSize = (UINTN)SourceFileSize;\r
ecc722ad 72 *FileSize = BufferSize;\r
73\r
74 Status = FileHandle->Read (FileHandle, &BufferSize, Buffer);\r
c411b485 75 if (EFI_ERROR (Status) || (BufferSize != *FileSize)) {\r
ecc722ad 76 FreePool (Buffer);\r
77 Buffer = NULL;\r
c411b485 78 Status = EFI_BAD_BUFFER_SIZE;\r
ecc722ad 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
c411b485 96 IN EFI_FILE_HANDLE FileHandle\r
ecc722ad 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
c411b485
MK
121 IN CONST UINTN *Integer,\r
122 IN UINTN IntSizeInWords,\r
123 OUT UINT8 *OctetString,\r
124 IN UINTN OSSizeInBytes\r
ecc722ad 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
c411b485
MK
132 Ptr1++, Ptr2--)\r
133 {\r
ecc722ad 134 *Ptr2 = *Ptr1;\r
135 }\r
b3548d32 136\r
c411b485
MK
137 for ( ; Ptr1 < (CONST UINT8 *)(Integer + IntSizeInWords) && *Ptr1 == 0; Ptr1++) {\r
138 }\r
b3548d32 139\r
ecc722ad 140 if (Ptr1 < (CONST UINT8 *)(Integer + IntSizeInWords)) {\r
141 return EFI_BUFFER_TOO_SMALL;\r
142 }\r
b3548d32 143\r
ecc722ad 144 if (Ptr2 >= OctetString) {\r
145 ZeroMem (OctetString, Ptr2 - OctetString + 1);\r
146 }\r
b3548d32 147\r
ecc722ad 148 return EFI_SUCCESS;\r
149}\r
150\r
ecc722ad 151/**\r
152 Worker function that prints an EFI_GUID into specified Buffer.\r
153\r
154 @param[in] Guid Pointer to GUID to print.\r
155 @param[in] Buffer Buffer to print Guid into.\r
156 @param[in] BufferSize Size of Buffer.\r
b3548d32 157\r
ecc722ad 158 @retval Number of characters printed.\r
159\r
160**/\r
161UINTN\r
162GuidToString (\r
163 IN EFI_GUID *Guid,\r
164 IN CHAR16 *Buffer,\r
165 IN UINTN BufferSize\r
166 )\r
167{\r
c411b485 168 UINTN Size;\r
ecc722ad 169\r
170 Size = UnicodeSPrint (\r
c411b485
MK
171 Buffer,\r
172 BufferSize,\r
173 L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
174 (UINTN)Guid->Data1,\r
175 (UINTN)Guid->Data2,\r
176 (UINTN)Guid->Data3,\r
177 (UINTN)Guid->Data4[0],\r
178 (UINTN)Guid->Data4[1],\r
179 (UINTN)Guid->Data4[2],\r
180 (UINTN)Guid->Data4[3],\r
181 (UINTN)Guid->Data4[4],\r
182 (UINTN)Guid->Data4[5],\r
183 (UINTN)Guid->Data4[6],\r
184 (UINTN)Guid->Data4[7]\r
185 );\r
ecc722ad 186\r
187 //\r
188 // SPrint will null terminate the string. The -1 skips the null\r
189 //\r
190 return Size - 1;\r
191}\r