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