]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/GenCrc32/GenCrc32.c
2 Calculate Crc32 value and Verify Crc32 value for input data.
4 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include "EfiUtilityMsgs.h"
15 #include "CommonLib.h"
18 #define UTILITY_NAME "GenCrc32"
19 #define UTILITY_MAJOR_VERSION 0
20 #define UTILITY_MINOR_VERSION 2
23 #define CRC32_ENCODE 1
24 #define CRC32_DECODE 2
34 Displays the standard utility information to SDTOUT
46 fprintf (stdout
, "%s Version %d.%d %s \n", UTILITY_NAME
, UTILITY_MAJOR_VERSION
, UTILITY_MINOR_VERSION
, __BUILD_VERSION
);
57 Displays the utility usage syntax to STDOUT
72 fprintf (stdout
, "Usage: GenCrc32 -e|-d [options] <input_file>\n\n");
75 // Copyright declaration
77 fprintf (stdout
, "Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.\n\n");
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\
87 fprintf (stdout
, " -v, --verbose Print informational statements\n");
88 fprintf (stdout
, " -q, --quiet Returns the exit code, error messages will be\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\
96 fprintf (stdout
, " --sfo Reserved for future use\n");
113 argc - Number of command line parameters.
114 argv - Array of pointers to parameter strings.
117 STATUS_SUCCESS - Utility exits successfully.
118 STATUS_ERROR - Some error occurred during execution.
123 CHAR8
*OutputFileName
;
124 CHAR8
*InputFileName
;
134 // Init local variables
137 Status
= EFI_SUCCESS
;
138 InputFileName
= NULL
;
139 OutputFileName
= NULL
;
140 FileAction
= CRC32_NULL
;
146 SetUtilityName (UTILITY_NAME
);
149 Error (NULL
, 0, 1001, "Missing options", "no options input");
155 // Parse command line
160 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
162 return STATUS_SUCCESS
;
165 if (stricmp (argv
[0], "--version") == 0) {
167 return STATUS_SUCCESS
;
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");
176 OutputFileName
= argv
[1];
182 if ((stricmp (argv
[0], "-e") == 0) || (stricmp (argv
[0], "--encode") == 0)) {
183 FileAction
= CRC32_ENCODE
;
189 if ((stricmp (argv
[0], "-d") == 0) || (stricmp (argv
[0], "--decode") == 0)) {
190 FileAction
= CRC32_DECODE
;
196 if ((stricmp (argv
[0], "-v") == 0) || (stricmp (argv
[0], "--verbose") == 0)) {
197 SetPrintLevel (VERBOSE_LOG_LEVEL
);
198 VerboseMsg ("Verbose output Mode Set!");
204 if ((stricmp (argv
[0], "-q") == 0) || (stricmp (argv
[0], "--quiet") == 0)) {
205 SetPrintLevel (KEY_LOG_LEVEL
);
206 KeyMsg ("Quiet output Mode Set!");
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]);
219 Error (NULL
, 0, 1003, "Invalid option value", "Debug Level range is 0-9, current input level is %d", (int) LogLevel
);
222 SetPrintLevel (LogLevel
);
223 DebugMsg (NULL
, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", argv
[1]);
229 if (argv
[0][0] == '-') {
230 Error (NULL
, 0, 1000, "Unknown option", argv
[0]);
235 // Get Input file file name.
237 InputFileName
= argv
[0];
242 VerboseMsg ("%s tool start.", UTILITY_NAME
);
245 // Check Input parameters
247 if (FileAction
== CRC32_NULL
) {
248 Error (NULL
, 0, 1001, "Missing option", "either the encode or the decode option must be specified!");
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");
256 if (InputFileName
== NULL
) {
257 Error (NULL
, 0, 1001, "Missing option", "Input files are not specified");
260 VerboseMsg ("Input file name is %s", InputFileName
);
263 if (OutputFileName
== NULL
) {
264 Error (NULL
, 0, 1001, "Missing option", "Output file are not specified");
267 VerboseMsg ("Output file name is %s", OutputFileName
);
271 // Open Input file and read file data.
273 InFile
= fopen (LongFilePath (InputFileName
), "rb");
274 if (InFile
== NULL
) {
275 Error (NULL
, 0, 0001, "Error opening file", InputFileName
);
279 fseek (InFile
, 0, SEEK_END
);
280 FileSize
= ftell (InFile
);
281 fseek (InFile
, 0, SEEK_SET
);
283 FileBuffer
= (UINT8
*) malloc (FileSize
);
284 if (FileBuffer
== NULL
) {
285 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
290 fread (FileBuffer
, 1, FileSize
, InFile
);
292 VerboseMsg ("the size of the input file is %u bytes", (unsigned) FileSize
);
297 OutFile
= fopen (LongFilePath (OutputFileName
), "wb");
298 if (OutFile
== NULL
) {
299 Error (NULL
, 0, 0001, "Error opening file", OutputFileName
);
304 // Calculate Crc32 value
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!");
313 // Done, write output file.
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
));
321 // Verify Crc32 Value
323 if (FileSize
< sizeof (UINT32
)) {
324 Error (NULL
, 0, 3000, "Invalid", "Input file is invalid!");
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!");
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
;
339 // Done, write output file.
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
));
346 if (FileBuffer
!= NULL
) {
350 if (OutFile
!= NULL
) {
354 VerboseMsg ("%s tool done with return code is 0x%x.", UTILITY_NAME
, GetUtilityStatus ());
356 return GetUtilityStatus ();