]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/GenCRC32Section.c
Add in the 1st version of ECP.
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Tools / Source / GenCRC32Section / GenCRC32Section.c
CommitLineData
3eb9473e 1/*++\r
2\r
3Copyright (c) 2004 - 2006, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13 \r
14 GenCRC32Section.c\r
15\r
16Abstract:\r
17\r
18 This file contains functions required to generate a Firmware File System \r
19 file. The code is compliant with the Tiano C Coding standards.\r
20\r
21--*/\r
22\r
23#include "TianoCommon.h"\r
24#include "EfiFirmwareFileSystem.h"\r
25#include "EfiFirmwareVolumeHeader.h"\r
26#include "ParseInf.h"\r
27#include "crc32.h"\r
28#include "EfiUtilityMsgs.h"\r
29#include "GenCRC32Section.h"\r
30#include <stdio.h>\r
31#include <stdlib.h>\r
32#include <string.h>\r
33#include <assert.h>\r
34#include "CommonLib.h"\r
35\r
36#include EFI_PROTOCOL_DEFINITION (GuidedSectionExtraction)\r
37\r
38#define TOOLVERSION "0.2"\r
39\r
40#define UTILITY_NAME "GenCrc32Section"\r
41\r
42EFI_GUID gEfiCrc32SectionGuid = EFI_CRC32_GUIDED_SECTION_EXTRACTION_PROTOCOL_GUID;\r
43\r
44EFI_STATUS\r
45SignSectionWithCrc32 (\r
46 IN OUT UINT8 *FileBuffer,\r
47 IN OUT UINT32 *BufferSize,\r
48 IN UINT32 DataSize\r
49 )\r
50/*++\r
51 \r
52Routine Description:\r
53 \r
54 Signs the section with CRC32 and add GUIDed section header for the \r
55 signed data. data stays in same location (overwrites source data).\r
56 \r
57Arguments:\r
58 \r
59 FileBuffer - Buffer containing data to sign\r
60 \r
61 BufferSize - On input, the size of FileBuffer. On output, the size of \r
62 actual section data (including added section header). \r
63\r
64 DataSize - Length of data to Sign\r
65\r
66 Key - Key to use when signing. Currently only CRC32 is supported.\r
67 \r
68Returns:\r
69 \r
70 EFI_SUCCESS - Successful\r
71 EFI_OUT_OF_RESOURCES - Not enough resource to complete the operation.\r
72 \r
73--*/\r
74{\r
75\r
76 UINT32 Crc32Checksum;\r
77 EFI_STATUS Status;\r
78 UINT32 TotalSize;\r
79 CRC32_SECTION_HEADER Crc32Header;\r
80 UINT8 *SwapBuffer;\r
81\r
82 Crc32Checksum = 0;\r
83 SwapBuffer = NULL;\r
84\r
85 if (DataSize == 0) {\r
86 *BufferSize = 0;\r
87\r
88 return EFI_SUCCESS;\r
89 }\r
90\r
91 Status = CalculateCrc32 (FileBuffer, DataSize, &Crc32Checksum);\r
92 if (EFI_ERROR (Status)) {\r
93 return Status;\r
94 }\r
95\r
96 TotalSize = DataSize + CRC32_SECTION_HEADER_SIZE;\r
97 Crc32Header.GuidSectionHeader.CommonHeader.Type = EFI_SECTION_GUID_DEFINED;\r
98 Crc32Header.GuidSectionHeader.CommonHeader.Size[0] = (UINT8) (TotalSize & 0xff);\r
99 Crc32Header.GuidSectionHeader.CommonHeader.Size[1] = (UINT8) ((TotalSize & 0xff00) >> 8);\r
100 Crc32Header.GuidSectionHeader.CommonHeader.Size[2] = (UINT8) ((TotalSize & 0xff0000) >> 16);\r
101 memcpy (&(Crc32Header.GuidSectionHeader.SectionDefinitionGuid), &gEfiCrc32SectionGuid, sizeof (EFI_GUID));\r
102 Crc32Header.GuidSectionHeader.Attributes = EFI_GUIDED_SECTION_AUTH_STATUS_VALID;\r
103 Crc32Header.GuidSectionHeader.DataOffset = CRC32_SECTION_HEADER_SIZE;\r
104 Crc32Header.CRC32Checksum = Crc32Checksum;\r
105\r
106 SwapBuffer = (UINT8 *) malloc (DataSize);\r
107 if (SwapBuffer == NULL) {\r
108 return EFI_OUT_OF_RESOURCES;\r
109 }\r
110\r
111 memcpy (SwapBuffer, FileBuffer, DataSize);\r
112 memcpy (FileBuffer, &Crc32Header, CRC32_SECTION_HEADER_SIZE);\r
113 memcpy (FileBuffer + CRC32_SECTION_HEADER_SIZE, SwapBuffer, DataSize);\r
114\r
115 //\r
116 // Make sure section ends on a DWORD boundary\r
117 //\r
118 while ((TotalSize & 0x03) != 0) {\r
119 FileBuffer[TotalSize] = 0;\r
120 TotalSize++;\r
121 }\r
122\r
123 *BufferSize = TotalSize;\r
124\r
125 if (SwapBuffer != NULL) {\r
126 free (SwapBuffer);\r
127 }\r
128\r
129 return EFI_SUCCESS;\r
130}\r
131\r
132VOID\r
133PrintUsage (\r
134 VOID\r
135 )\r
136{\r
137 printf ("Usage:\n");\r
138 printf (UTILITY_NAME " -i \"inputfile1\" \"inputfile2\" -o \"outputfile\" \n");\r
139 printf (" -i \"inputfile\":\n ");\r
140 printf (" specifies the input files that would be signed to CRC32 Guided section.\n");\r
141 printf (" -o \"outputfile\":\n");\r
142 printf (" specifies the output file that is a CRC32 Guided section.\n");\r
143}\r
144\r
145INT32\r
146ReadFilesContentsIntoBuffer (\r
147 IN CHAR8 *argv[],\r
148 IN INT32 Start,\r
149 IN OUT UINT8 **FileBuffer,\r
150 IN OUT UINT32 *BufferSize,\r
151 OUT UINT32 *ContentSize,\r
152 IN INT32 MaximumArguments\r
153 )\r
154{\r
155 INT32 Index;\r
156 CHAR8 *FileName;\r
157 FILE *InputFile;\r
158 UINT8 Temp;\r
159 UINT32 Size;\r
160\r
161 FileName = NULL;\r
162 InputFile = NULL;\r
163 Size = 0;\r
164 Index = 0;\r
165\r
166 //\r
167 // read all input files into one file buffer\r
168 //\r
169 while (argv[Start + Index][0] != '-') {\r
170\r
171 FileName = argv[Start + Index];\r
172 InputFile = fopen (FileName, "rb");\r
173 if (InputFile == NULL) {\r
174 Error (NULL, 0, 0, FileName, "failed to open input binary file");\r
175 return -1;\r
176 }\r
177\r
178 fread (&Temp, sizeof (UINT8), 1, InputFile);\r
179 while (!feof (InputFile)) {\r
180 (*FileBuffer)[Size++] = Temp;\r
181 fread (&Temp, sizeof (UINT8), 1, InputFile);\r
182 }\r
183\r
184 fclose (InputFile);\r
185 InputFile = NULL;\r
186\r
187 //\r
188 // Make sure section ends on a DWORD boundary\r
189 //\r
190 while ((Size & 0x03) != 0) {\r
191 (*FileBuffer)[Size] = 0;\r
192 Size++;\r
193 }\r
194\r
195 Index++;\r
196 if (Index == MaximumArguments) {\r
197 break;\r
198 }\r
199 }\r
200\r
201 *ContentSize = Size;\r
202 return Index;\r
203}\r
204\r
205INT32\r
206main (\r
207 INT32 argc,\r
208 CHAR8 *argv[]\r
209 )\r
210{\r
211 FILE *OutputFile;\r
212 UINT8 *FileBuffer;\r
213 UINT32 BufferSize;\r
214 EFI_STATUS Status;\r
215 UINT32 ContentSize;\r
216 CHAR8 *OutputFileName;\r
217 INT32 ReturnValue;\r
218 INT32 Index;\r
219\r
220 OutputFile = NULL;\r
221 FileBuffer = NULL;\r
222 ContentSize = 0;\r
223 OutputFileName = NULL;\r
224\r
225 SetUtilityName (UTILITY_NAME);\r
226\r
227 if (argc == 1) {\r
228 PrintUsage ();\r
229 return -1;\r
230 }\r
231\r
232 BufferSize = 1024 * 1024 * 16;\r
233 FileBuffer = (UINT8 *) malloc (BufferSize * sizeof (UINT8));\r
234 if (FileBuffer == NULL) {\r
235 Error (NULL, 0, 0, "memory allocation failed", NULL);\r
236 return -1;\r
237 }\r
238\r
239 ZeroMem (FileBuffer, BufferSize);\r
240\r
241 for (Index = 0; Index < argc; Index++) {\r
242 if (_strcmpi (argv[Index], "-i") == 0) {\r
243 ReturnValue = ReadFilesContentsIntoBuffer (\r
244 argv,\r
245 (Index + 1),\r
246 &FileBuffer,\r
247 &BufferSize,\r
248 &ContentSize,\r
249 (argc - (Index + 1))\r
250 );\r
251 if (ReturnValue == -1) {\r
252 Error (NULL, 0, 0, "failed to read file contents", NULL);\r
253 return -1;\r
254 }\r
255\r
256 Index += ReturnValue;\r
257 }\r
258\r
259 if (_strcmpi (argv[Index], "-o") == 0) {\r
260 OutputFileName = argv[Index + 1];\r
261 }\r
262 }\r
263\r
264 OutputFile = fopen (OutputFileName, "wb");\r
265 if (OutputFile == NULL) {\r
266 Error (NULL, 0, 0, OutputFileName, "failed to open output binary file");\r
267 free (FileBuffer);\r
268 return -1;\r
269 }\r
270\r
271 /* \r
272 //\r
273 // make sure section ends on a DWORD boundary ??\r
274 //\r
275 while ( (Size & 0x03) != 0 ) {\r
276 FileBuffer[Size] = 0;\r
277 Size ++;\r
278 }\r
279*/\r
280 Status = SignSectionWithCrc32 (FileBuffer, &BufferSize, ContentSize);\r
281 if (EFI_ERROR (Status)) {\r
282 Error (NULL, 0, 0, "failed to sign section", NULL);\r
283 free (FileBuffer);\r
284 fclose (OutputFile);\r
285 return -1;\r
286 }\r
287\r
288 ContentSize = fwrite (FileBuffer, sizeof (UINT8), BufferSize, OutputFile);\r
289 if (ContentSize != BufferSize) {\r
290 Error (NULL, 0, 0, "failed to write output buffer", NULL);\r
291 ReturnValue = -1;\r
292 } else {\r
293 ReturnValue = 0;\r
294 }\r
295\r
296 free (FileBuffer);\r
297 fclose (OutputFile);\r
298 return ReturnValue;\r
299}\r