]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/CodeTools/Source/GenCRC32Section/GenCRC32Section.c
More renames for Tool Packages
[mirror_edk2.git] / Tools / CodeTools / Source / GenCRC32Section / GenCRC32Section.c
CommitLineData
878ddf1f 1/*++\r
2\r
3Copyright (c) 2004, 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 "GenCRC32Section.h"\r
24\r
25#define TOOLVERSION "0.2"\r
26\r
27#define UTILITY_NAME "GenCrc32Section"\r
28\r
29EFI_GUID gEfiCrc32SectionGuid = EFI_CRC32_GUIDED_SECTION_EXTRACTION_PROTOCOL_GUID;\r
30\r
31EFI_STATUS\r
32SignSectionWithCrc32 (\r
33 IN OUT UINT8 *FileBuffer,\r
34 IN OUT UINT32 *BufferSize,\r
35 IN UINT32 DataSize\r
36 )\r
37/*++\r
38 \r
39Routine Description:\r
40 \r
41 Signs the section with CRC32 and add GUIDed section header for the \r
42 signed data. data stays in same location (overwrites source data).\r
43 \r
44Arguments:\r
45 \r
46 FileBuffer - Buffer containing data to sign\r
47 \r
48 BufferSize - On input, the size of FileBuffer. On output, the size of \r
49 actual section data (including added section header). \r
50\r
51 DataSize - Length of data to Sign\r
52\r
53 Key - Key to use when signing. Currently only CRC32 is supported.\r
54 \r
55Returns:\r
56 \r
57 EFI_SUCCESS - Successful\r
58 EFI_OUT_OF_RESOURCES - Not enough resource to complete the operation.\r
59 \r
60--*/\r
61{\r
62\r
63 UINT32 Crc32Checksum;\r
64 EFI_STATUS Status;\r
65 UINT32 TotalSize;\r
66 CRC32_SECTION_HEADER Crc32Header;\r
67 UINT8 *SwapBuffer;\r
68\r
69 Crc32Checksum = 0;\r
70 SwapBuffer = NULL;\r
71\r
72 if (DataSize == 0) {\r
73 *BufferSize = 0;\r
74\r
75 return EFI_SUCCESS;\r
76 }\r
77\r
78 Status = CalculateCrc32 (FileBuffer, DataSize, &Crc32Checksum);\r
79 if (EFI_ERROR (Status)) {\r
80 return Status;\r
81 }\r
82\r
83 TotalSize = DataSize + CRC32_SECTION_HEADER_SIZE;\r
84 Crc32Header.GuidSectionHeader.CommonHeader.Type = EFI_SECTION_GUID_DEFINED;\r
85 Crc32Header.GuidSectionHeader.CommonHeader.Size[0] = (UINT8) (TotalSize & 0xff);\r
86 Crc32Header.GuidSectionHeader.CommonHeader.Size[1] = (UINT8) ((TotalSize & 0xff00) >> 8);\r
87 Crc32Header.GuidSectionHeader.CommonHeader.Size[2] = (UINT8) ((TotalSize & 0xff0000) >> 16);\r
88 memcpy (&(Crc32Header.GuidSectionHeader.SectionDefinitionGuid), &gEfiCrc32SectionGuid, sizeof (EFI_GUID));\r
89 Crc32Header.GuidSectionHeader.Attributes = EFI_GUIDED_SECTION_AUTH_STATUS_VALID;\r
90 Crc32Header.GuidSectionHeader.DataOffset = CRC32_SECTION_HEADER_SIZE;\r
91 Crc32Header.CRC32Checksum = Crc32Checksum;\r
92\r
93 SwapBuffer = (UINT8 *) malloc (DataSize);\r
94 if (SwapBuffer == NULL) {\r
95 return EFI_OUT_OF_RESOURCES;\r
96 }\r
97\r
98 memcpy (SwapBuffer, FileBuffer, DataSize);\r
99 memcpy (FileBuffer, &Crc32Header, CRC32_SECTION_HEADER_SIZE);\r
100 memcpy (FileBuffer + CRC32_SECTION_HEADER_SIZE, SwapBuffer, DataSize);\r
101\r
102 //\r
103 // Make sure section ends on a DWORD boundary\r
104 //\r
105 while ((TotalSize & 0x03) != 0) {\r
106 FileBuffer[TotalSize] = 0;\r
107 TotalSize++;\r
108 }\r
109\r
110 *BufferSize = TotalSize;\r
111\r
112 if (SwapBuffer != NULL) {\r
113 free (SwapBuffer);\r
114 }\r
115\r
116 return EFI_SUCCESS;\r
117}\r
118\r
119VOID\r
120PrintUsage (\r
121 VOID\r
122 )\r
123{\r
124 printf ("Usage:\n");\r
125 printf (UTILITY_NAME " -i \"inputfile1\" \"inputfile2\" -o \"outputfile\" \n");\r
126 printf (" -i \"inputfile\":\n ");\r
127 printf (" specifies the input files that would be signed to CRC32 Guided section.\n");\r
128 printf (" -o \"outputfile\":\n");\r
129 printf (" specifies the output file that is a CRC32 Guided section.\n");\r
130}\r
131\r
132INT32\r
133ReadFilesContentsIntoBuffer (\r
134 IN CHAR8 *argv[],\r
135 IN INT32 Start,\r
136 IN OUT UINT8 **FileBuffer,\r
137 IN OUT UINT32 *BufferSize,\r
138 OUT UINT32 *ContentSize,\r
139 IN INT32 MaximumArguments\r
140 )\r
141{\r
142 INT32 Index;\r
143 CHAR8 *FileName;\r
144 FILE *InputFile;\r
145 UINT8 Temp;\r
146 UINT32 Size;\r
147\r
148 FileName = NULL;\r
149 InputFile = NULL;\r
150 Size = 0;\r
151 Index = 0;\r
152\r
153 //\r
154 // read all input files into one file buffer\r
155 //\r
156 while (argv[Start + Index][0] != '-') {\r
157\r
158 FileName = argv[Start + Index];\r
159 InputFile = fopen (FileName, "rb");\r
160 if (InputFile == NULL) {\r
161 Error (NULL, 0, 0, FileName, "failed to open input binary file");\r
162 return -1;\r
163 }\r
164\r
165 fread (&Temp, sizeof (UINT8), 1, InputFile);\r
166 while (!feof (InputFile)) {\r
167 (*FileBuffer)[Size++] = Temp;\r
168 fread (&Temp, sizeof (UINT8), 1, InputFile);\r
169 }\r
170\r
171 fclose (InputFile);\r
172 InputFile = NULL;\r
173\r
174 //\r
175 // Make sure section ends on a DWORD boundary\r
176 //\r
177 while ((Size & 0x03) != 0) {\r
178 (*FileBuffer)[Size] = 0;\r
179 Size++;\r
180 }\r
181\r
182 Index++;\r
183 if (Index == MaximumArguments) {\r
184 break;\r
185 }\r
186 }\r
187\r
188 *ContentSize = Size;\r
189 return Index;\r
190}\r
191\r
192int\r
193main (\r
194 INT32 argc,\r
195 CHAR8 *argv[]\r
196 )\r
197{\r
198 FILE *OutputFile;\r
199 UINT8 *FileBuffer;\r
200 UINT32 BufferSize;\r
201 EFI_STATUS Status;\r
202 UINT32 ContentSize;\r
203 CHAR8 *OutputFileName;\r
204 INT32 ReturnValue;\r
205 INT32 Index;\r
206\r
207 OutputFile = NULL;\r
208 FileBuffer = NULL;\r
209 ContentSize = 0;\r
210 OutputFileName = NULL;\r
211\r
212 SetUtilityName (UTILITY_NAME);\r
213\r
214 if (argc == 1) {\r
215 PrintUsage ();\r
216 return -1;\r
217 }\r
218\r
219 BufferSize = 1024 * 1024 * 16;\r
220 FileBuffer = (UINT8 *) malloc (BufferSize * sizeof (UINT8));\r
221 if (FileBuffer == NULL) {\r
222 Error (NULL, 0, 0, "memory allocation failed", NULL);\r
223 return -1;\r
224 }\r
225\r
226 ZeroMem (FileBuffer, BufferSize);\r
227\r
228 for (Index = 0; Index < argc; Index++) {\r
229 if (strcmpi (argv[Index], "-i") == 0) {\r
230 ReturnValue = ReadFilesContentsIntoBuffer (\r
231 argv,\r
232 (Index + 1),\r
233 &FileBuffer,\r
234 &BufferSize,\r
235 &ContentSize,\r
236 (argc - (Index + 1))\r
237 );\r
238 if (ReturnValue == -1) {\r
239 Error (NULL, 0, 0, "failed to read file contents", NULL);\r
240 return -1;\r
241 }\r
242\r
243 Index += ReturnValue;\r
244 }\r
245\r
246 if (strcmpi (argv[Index], "-o") == 0) {\r
247 OutputFileName = argv[Index + 1];\r
248 }\r
249 }\r
250\r
251 OutputFile = fopen (OutputFileName, "wb");\r
252 if (OutputFile == NULL) {\r
253 Error (NULL, 0, 0, OutputFileName, "failed to open output binary file");\r
254 free (FileBuffer);\r
255 return -1;\r
256 }\r
257\r
258 /* \r
259 //\r
260 // make sure section ends on a DWORD boundary ??\r
261 //\r
262 while ( (Size & 0x03) != 0 ) {\r
263 FileBuffer[Size] = 0;\r
264 Size ++;\r
265 }\r
266*/\r
267 Status = SignSectionWithCrc32 (FileBuffer, &BufferSize, ContentSize);\r
268 if (EFI_ERROR (Status)) {\r
269 Error (NULL, 0, 0, "failed to sign section", NULL);\r
270 free (FileBuffer);\r
271 fclose (OutputFile);\r
272 return -1;\r
273 }\r
274\r
275 ContentSize = fwrite (FileBuffer, sizeof (UINT8), BufferSize, OutputFile);\r
276 if (ContentSize != BufferSize) {\r
277 Error (NULL, 0, 0, "failed to write output buffer", NULL);\r
278 ReturnValue = -1;\r
279 } else {\r
280 ReturnValue = 0;\r
281 }\r
282\r
283 free (FileBuffer);\r
284 fclose (OutputFile);\r
285 return ReturnValue;\r
286}\r