]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/GenCrc32/GenCrc32.c
3 Copyright (c) 2007 - 2008, 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
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.
17 Calculate Crc32 value and Verify Crc32 value for input data.
25 #include "EfiUtilityMsgs.h"
26 #include "CommonLib.h"
29 #define UTILITY_NAME "GenCrc32"
30 #define UTILITY_MAJOR_VERSION 0
31 #define UTILITY_MINOR_VERSION 1
34 #define CRC32_ENCODE 1
35 #define CRC32_DECODE 2
45 Displays the standard utility information to SDTOUT
57 fprintf (stdout
, "%s Version %d.%d\n", UTILITY_NAME
, UTILITY_MAJOR_VERSION
, UTILITY_MINOR_VERSION
);
68 Displays the utility usage syntax to STDOUT
83 fprintf (stdout
, "\nUsage: %s -e|-d [options] <input_file>\n\n", UTILITY_NAME
);
86 // Copyright declaration
88 fprintf (stdout
, "Copyright (c) 2007 - 2009, Intel Corporation. All rights reserved.\n\n");
93 fprintf (stdout
, "Options:\n");
94 fprintf (stdout
, " -o FileName, --output FileName\n\
95 File will be created to store the ouput content.\n");
96 fprintf (stdout
, " -e, --encode Calculate CRC32 value for the input file.\n");
97 fprintf (stdout
, " -d, --decode Verify CRC32 value for the input file.\n");
98 fprintf (stdout
, " -v, --verbose Turn on verbose output with informational messages.\n");
99 fprintf (stdout
, " -q, --quiet Disable all messages except key message and fatal error\n");
100 fprintf (stdout
, " --debug level Enable debug messages, at input debug level.\n");
101 fprintf (stdout
, " --version Show program's version number and exit.\n");
102 fprintf (stdout
, " -h, --help Show this help message and exit.\n");
118 argc - Number of command line parameters.
119 argv - Array of pointers to parameter strings.
122 STATUS_SUCCESS - Utility exits successfully.
123 STATUS_ERROR - Some error occurred during execution.
128 CHAR8
*OutputFileName
;
129 CHAR8
*InputFileName
;
139 // Init local variables
142 Status
= EFI_SUCCESS
;
143 InputFileName
= NULL
;
144 OutputFileName
= NULL
;
145 FileAction
= CRC32_NULL
;
151 SetUtilityName (UTILITY_NAME
);
154 Error (NULL
, 0, 1001, "Missing options", "no options input");
160 // Parse command line
165 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
168 return STATUS_SUCCESS
;
171 if (stricmp (argv
[0], "--version") == 0) {
173 return STATUS_SUCCESS
;
177 if ((stricmp (argv
[0], "-o") == 0) || (stricmp (argv
[0], "--output") == 0)) {
178 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
179 Error (NULL
, 0, 1003, "Invalid option value", "Output File name is missing for -o option");
182 OutputFileName
= argv
[1];
188 if ((stricmp (argv
[0], "-e") == 0) || (stricmp (argv
[0], "--encode") == 0)) {
189 FileAction
= CRC32_ENCODE
;
195 if ((stricmp (argv
[0], "-d") == 0) || (stricmp (argv
[0], "--decode") == 0)) {
196 FileAction
= CRC32_DECODE
;
202 if ((stricmp (argv
[0], "-v") == 0) || (stricmp (argv
[0], "--verbose") == 0)) {
203 SetPrintLevel (VERBOSE_LOG_LEVEL
);
204 VerboseMsg ("Verbose output Mode Set!");
210 if ((stricmp (argv
[0], "-q") == 0) || (stricmp (argv
[0], "--quiet") == 0)) {
211 SetPrintLevel (KEY_LOG_LEVEL
);
212 KeyMsg ("Quiet output Mode Set!");
218 if (stricmp (argv
[0], "--debug") == 0) {
219 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &LogLevel
);
220 if (EFI_ERROR (Status
)) {
221 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
225 Error (NULL
, 0, 1003, "Invalid option value", "Debug Level range is 0-9, current input level is %d", LogLevel
);
228 SetPrintLevel (LogLevel
);
229 DebugMsg (NULL
, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", argv
[1]);
235 if (argv
[0][0] == '-') {
236 Error (NULL
, 0, 1000, "Unknown option", argv
[0]);
241 // Get Input file file name.
243 InputFileName
= argv
[0];
248 VerboseMsg ("%s tool start.", UTILITY_NAME
);
251 // Check Input paramters
253 if (FileAction
== CRC32_NULL
) {
254 Error (NULL
, 0, 1001, "Missing option", "either the encode or the decode option must be specified!");
256 } else if (FileAction
== CRC32_ENCODE
) {
257 VerboseMsg ("File will be encoded by Crc32");
258 } else if (FileAction
== CRC32_DECODE
) {
259 VerboseMsg ("File will be decoded by Crc32");
262 if (InputFileName
== NULL
) {
263 Error (NULL
, 0, 1001, "Missing option", "Input files are not specified");
266 VerboseMsg ("Input file name is %s", InputFileName
);
269 if (OutputFileName
== NULL
) {
270 Error (NULL
, 0, 1001, "Missing option", "Output file are not specified");
273 VerboseMsg ("Output file name is %s", OutputFileName
);
277 // Open Input file and read file data.
279 InFile
= fopen (InputFileName
, "rb");
280 if (InFile
== NULL
) {
281 Error (NULL
, 0, 0001, "Error opening file", InputFileName
);
285 fseek (InFile
, 0, SEEK_END
);
286 FileSize
= ftell (InFile
);
287 fseek (InFile
, 0, SEEK_SET
);
289 FileBuffer
= (UINT8
*) malloc (FileSize
);
290 if (FileBuffer
== NULL
) {
291 Error (NULL
, 0, 4001, "Resource", "memory cannot be allcoated!");
295 fread (FileBuffer
, 1, FileSize
, InFile
);
297 VerboseMsg ("the size of the input file is %d bytes", FileSize
);
302 OutFile
= fopen (OutputFileName
, "wb");
303 if (OutFile
== NULL
) {
304 Error (NULL
, 0, 0001, "Error opening file", OutputFileName
);
309 // Calculate Crc32 value
311 if (FileAction
== CRC32_ENCODE
) {
312 Status
= CalculateCrc32 (FileBuffer
, FileSize
, &Crc32Value
);
313 if (Status
!= EFI_SUCCESS
) {
314 Error (NULL
, 0, 3000, "Invalid", "Calculate CRC32 value failed!");
318 // Done, write output file.
320 fwrite (&Crc32Value
, 1, sizeof (Crc32Value
), OutFile
);
321 VerboseMsg ("The calculated CRC32 value is 0x%08x", Crc32Value
);
322 fwrite (FileBuffer
, 1, FileSize
, OutFile
);
323 VerboseMsg ("the size of the encoded file is %d bytes", FileSize
+ sizeof (UINT32
));
326 // Verify Crc32 Value
328 Status
= CalculateCrc32 (FileBuffer
+ sizeof (UINT32
), FileSize
- sizeof (UINT32
), &Crc32Value
);
329 if (Status
!= EFI_SUCCESS
) {
330 Error (NULL
, 0, 3000, "Invalid", "Calculate CRC32 value failed!");
333 VerboseMsg ("The calculated CRC32 value is 0x%08x and File Crc32 value is 0x%08x", Crc32Value
, *(UINT32
*)FileBuffer
);
334 if (Crc32Value
!= *(UINT32
*)FileBuffer
) {
335 Error (NULL
, 0, 3000, "Invalid", "CRC32 value of input file is not correct!");
336 Status
= STATUS_ERROR
;
340 // Done, write output file.
342 fwrite (FileBuffer
+ sizeof (UINT32
), 1, FileSize
- sizeof (UINT32
), OutFile
);
343 VerboseMsg ("the size of the decoded file is %d bytes", FileSize
- sizeof (UINT32
));
347 if (FileBuffer
!= NULL
) {
351 if (OutFile
!= NULL
) {
355 VerboseMsg ("%s tool done with return code is 0x%x.", UTILITY_NAME
, GetUtilityStatus ());
357 return GetUtilityStatus ();