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