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