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