]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/GenCrc32/GenCrc32.c
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / C / GenCrc32 / GenCrc32.c
1 /** @file
2 Calculate Crc32 value and Verify Crc32 value for input data.
3
4 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "ParseInf.h"
14 #include "EfiUtilityMsgs.h"
15 #include "CommonLib.h"
16 #include "Crc32.h"
17
18 #define UTILITY_NAME "GenCrc32"
19 #define UTILITY_MAJOR_VERSION 0
20 #define UTILITY_MINOR_VERSION 2
21
22 #define CRC32_NULL 0
23 #define CRC32_ENCODE 1
24 #define CRC32_DECODE 2
25
26 VOID
27 Version (
28 VOID
29 )
30 /*++
31
32 Routine Description:
33
34 Displays the standard utility information to SDTOUT
35
36 Arguments:
37
38 None
39
40 Returns:
41
42 None
43
44 --*/
45 {
46 fprintf (stdout, "%s Version %d.%d %s \n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);
47 }
48
49 VOID
50 Usage (
51 VOID
52 )
53 /*++
54
55 Routine Description:
56
57 Displays the utility usage syntax to STDOUT
58
59 Arguments:
60
61 None
62
63 Returns:
64
65 None
66
67 --*/
68 {
69 //
70 // Summary usage
71 //
72 fprintf (stdout, "Usage: GenCrc32 -e|-d [options] <input_file>\n\n");
73
74 //
75 // Copyright declaration
76 //
77 fprintf (stdout, "Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.\n\n");
78
79 //
80 // Details Option
81 //
82 fprintf (stdout, "optional arguments:\n");
83 fprintf (stdout, " -h, --help Show this help message and exit\n");
84 fprintf (stdout, " --version Show program's version number and exit\n");
85 fprintf (stdout, " --debug [DEBUG] Output DEBUG statements, where DEBUG_LEVEL is 0 (min)\n\
86 - 9 (max)\n");
87 fprintf (stdout, " -v, --verbose Print informational statements\n");
88 fprintf (stdout, " -q, --quiet Returns the exit code, error messages will be\n\
89 displayed\n");
90 fprintf (stdout, " -s, --silent Returns only the exit code; informational and error\n\
91 messages are not displayed\n");
92 fprintf (stdout, " -e, --encode Calculate CRC32 value for the input file\n");
93 fprintf (stdout, " -d, --decode Verify CRC32 value for the input file\n");
94 fprintf (stdout, " -o OUTPUT_FILENAME, --output OUTPUT_FILENAME\n\
95 Output file name\n");
96 fprintf (stdout, " --sfo Reserved for future use\n");
97
98 }
99
100 int
101 main (
102 int argc,
103 CHAR8 *argv[]
104 )
105 /*++
106
107 Routine Description:
108
109 Main function.
110
111 Arguments:
112
113 argc - Number of command line parameters.
114 argv - Array of pointers to parameter strings.
115
116 Returns:
117 STATUS_SUCCESS - Utility exits successfully.
118 STATUS_ERROR - Some error occurred during execution.
119
120 --*/
121 {
122 EFI_STATUS Status;
123 CHAR8 *OutputFileName;
124 CHAR8 *InputFileName;
125 UINT8 *FileBuffer;
126 UINT32 FileSize;
127 UINT64 LogLevel;
128 UINT8 FileAction;
129 UINT32 Crc32Value;
130 FILE *InFile;
131 FILE *OutFile;
132
133 //
134 // Init local variables
135 //
136 LogLevel = 0;
137 Status = EFI_SUCCESS;
138 InputFileName = NULL;
139 OutputFileName = NULL;
140 FileAction = CRC32_NULL;
141 InFile = NULL;
142 OutFile = NULL;
143 Crc32Value = 0;
144 FileBuffer = NULL;
145
146 SetUtilityName (UTILITY_NAME);
147
148 if (argc == 1) {
149 Error (NULL, 0, 1001, "Missing options", "no options input");
150 Usage ();
151 return STATUS_ERROR;
152 }
153
154 //
155 // Parse command line
156 //
157 argc --;
158 argv ++;
159
160 if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) {
161 Usage ();
162 return STATUS_SUCCESS;
163 }
164
165 if (stricmp (argv[0], "--version") == 0) {
166 Version ();
167 return STATUS_SUCCESS;
168 }
169
170 while (argc > 0) {
171 if ((stricmp (argv[0], "-o") == 0) || (stricmp (argv[0], "--output") == 0)) {
172 if (argv[1] == NULL || argv[1][0] == '-') {
173 Error (NULL, 0, 1003, "Invalid option value", "Output File name is missing for -o option");
174 goto Finish;
175 }
176 OutputFileName = argv[1];
177 argc -= 2;
178 argv += 2;
179 continue;
180 }
181
182 if ((stricmp (argv[0], "-e") == 0) || (stricmp (argv[0], "--encode") == 0)) {
183 FileAction = CRC32_ENCODE;
184 argc --;
185 argv ++;
186 continue;
187 }
188
189 if ((stricmp (argv[0], "-d") == 0) || (stricmp (argv[0], "--decode") == 0)) {
190 FileAction = CRC32_DECODE;
191 argc --;
192 argv ++;
193 continue;
194 }
195
196 if ((stricmp (argv[0], "-v") == 0) || (stricmp (argv[0], "--verbose") == 0)) {
197 SetPrintLevel (VERBOSE_LOG_LEVEL);
198 VerboseMsg ("Verbose output Mode Set!");
199 argc --;
200 argv ++;
201 continue;
202 }
203
204 if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) {
205 SetPrintLevel (KEY_LOG_LEVEL);
206 KeyMsg ("Quiet output Mode Set!");
207 argc --;
208 argv ++;
209 continue;
210 }
211
212 if (stricmp (argv[0], "--debug") == 0) {
213 Status = AsciiStringToUint64 (argv[1], FALSE, &LogLevel);
214 if (EFI_ERROR (Status)) {
215 Error (NULL, 0, 1003, "Invalid option value", "%s = %s", argv[0], argv[1]);
216 goto Finish;
217 }
218 if (LogLevel > 9) {
219 Error (NULL, 0, 1003, "Invalid option value", "Debug Level range is 0-9, current input level is %d", (int) LogLevel);
220 goto Finish;
221 }
222 SetPrintLevel (LogLevel);
223 DebugMsg (NULL, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", argv[1]);
224 argc -= 2;
225 argv += 2;
226 continue;
227 }
228
229 if (argv[0][0] == '-') {
230 Error (NULL, 0, 1000, "Unknown option", argv[0]);
231 goto Finish;
232 }
233
234 //
235 // Get Input file file name.
236 //
237 InputFileName = argv[0];
238 argc --;
239 argv ++;
240 }
241
242 VerboseMsg ("%s tool start.", UTILITY_NAME);
243
244 //
245 // Check Input parameters
246 //
247 if (FileAction == CRC32_NULL) {
248 Error (NULL, 0, 1001, "Missing option", "either the encode or the decode option must be specified!");
249 return STATUS_ERROR;
250 } else if (FileAction == CRC32_ENCODE) {
251 VerboseMsg ("File will be encoded by Crc32");
252 } else if (FileAction == CRC32_DECODE) {
253 VerboseMsg ("File will be decoded by Crc32");
254 }
255
256 if (InputFileName == NULL) {
257 Error (NULL, 0, 1001, "Missing option", "Input files are not specified");
258 goto Finish;
259 } else {
260 VerboseMsg ("Input file name is %s", InputFileName);
261 }
262
263 if (OutputFileName == NULL) {
264 Error (NULL, 0, 1001, "Missing option", "Output file are not specified");
265 goto Finish;
266 } else {
267 VerboseMsg ("Output file name is %s", OutputFileName);
268 }
269
270 //
271 // Open Input file and read file data.
272 //
273 InFile = fopen (LongFilePath (InputFileName), "rb");
274 if (InFile == NULL) {
275 Error (NULL, 0, 0001, "Error opening file", InputFileName);
276 return STATUS_ERROR;
277 }
278
279 fseek (InFile, 0, SEEK_END);
280 FileSize = ftell (InFile);
281 fseek (InFile, 0, SEEK_SET);
282
283 FileBuffer = (UINT8 *) malloc (FileSize);
284 if (FileBuffer == NULL) {
285 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
286 fclose (InFile);
287 goto Finish;
288 }
289
290 fread (FileBuffer, 1, FileSize, InFile);
291 fclose (InFile);
292 VerboseMsg ("the size of the input file is %u bytes", (unsigned) FileSize);
293
294 //
295 // Open output file
296 //
297 OutFile = fopen (LongFilePath (OutputFileName), "wb");
298 if (OutFile == NULL) {
299 Error (NULL, 0, 0001, "Error opening file", OutputFileName);
300 goto Finish;
301 }
302
303 //
304 // Calculate Crc32 value
305 //
306 if (FileAction == CRC32_ENCODE) {
307 Status = CalculateCrc32 (FileBuffer, FileSize, &Crc32Value);
308 if (Status != EFI_SUCCESS) {
309 Error (NULL, 0, 3000, "Invalid", "Calculate CRC32 value failed!");
310 goto Finish;
311 }
312 //
313 // Done, write output file.
314 //
315 fwrite (&Crc32Value, 1, sizeof (Crc32Value), OutFile);
316 VerboseMsg ("The calculated CRC32 value is 0x%08x", (unsigned) Crc32Value);
317 fwrite (FileBuffer, 1, FileSize, OutFile);
318 VerboseMsg ("the size of the encoded file is %u bytes", (unsigned) FileSize + sizeof (UINT32));
319 } else {
320 //
321 // Verify Crc32 Value
322 //
323 if (FileSize < sizeof (UINT32)) {
324 Error (NULL, 0, 3000, "Invalid", "Input file is invalid!");
325 goto Finish;
326 }
327 Status = CalculateCrc32 (FileBuffer + sizeof (UINT32), FileSize - sizeof (UINT32), &Crc32Value);
328 if (Status != EFI_SUCCESS) {
329 Error (NULL, 0, 3000, "Invalid", "Calculate CRC32 value failed!");
330 goto Finish;
331 }
332 VerboseMsg ("The calculated CRC32 value is 0x%08x and File Crc32 value is 0x%08x", (unsigned) Crc32Value, (unsigned) (*(UINT32 *)FileBuffer));
333 if (Crc32Value != *(UINT32 *)FileBuffer) {
334 Error (NULL, 0, 3000, "Invalid", "CRC32 value of input file is not correct!");
335 Status = STATUS_ERROR;
336 goto Finish;
337 }
338 //
339 // Done, write output file.
340 //
341 fwrite (FileBuffer + sizeof (UINT32), 1, FileSize - sizeof (UINT32), OutFile);
342 VerboseMsg ("the size of the decoded file is %u bytes", (unsigned) FileSize - sizeof (UINT32));
343 }
344
345 Finish:
346 if (FileBuffer != NULL) {
347 free (FileBuffer);
348 }
349
350 if (OutFile != NULL) {
351 fclose (OutFile);
352 }
353
354 VerboseMsg ("%s tool done with return code is 0x%x.", UTILITY_NAME, GetUtilityStatus ());
355
356 return GetUtilityStatus ();
357 }
358
359
360
361