]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/C/GenCrc32/GenCrc32.c
License header updated to match correct format.
[mirror_edk2.git] / BaseTools / Source / C / GenCrc32 / GenCrc32.c
CommitLineData
30fdf114 1/** @file\r
97fa0ee9 2Calculate Crc32 value and Verify Crc32 value for input data.\r
30fdf114 3\r
1be2ed90 4Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
40d841f6 5This program and the accompanying materials \r
30fdf114
LG
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php \r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
30fdf114
LG
13**/\r
14\r
15#include <stdio.h>\r
16#include <stdlib.h>\r
17#include <string.h>\r
18\r
fd171542 19#include "ParseInf.h"\r
30fdf114
LG
20#include "EfiUtilityMsgs.h"\r
21#include "CommonLib.h"\r
22#include "Crc32.h"\r
23\r
24#define UTILITY_NAME "GenCrc32"\r
25#define UTILITY_MAJOR_VERSION 0\r
f4260465 26#define UTILITY_MINOR_VERSION 2\r
30fdf114
LG
27\r
28#define CRC32_NULL 0\r
29#define CRC32_ENCODE 1\r
30#define CRC32_DECODE 2 \r
31\r
32VOID\r
33Version (\r
34 VOID\r
35 )\r
36/*++\r
37\r
38Routine Description:\r
39\r
40 Displays the standard utility information to SDTOUT\r
41\r
42Arguments:\r
43\r
44 None\r
45\r
46Returns:\r
47\r
48 None\r
49\r
50--*/\r
51{\r
f4260465 52 fprintf (stdout, "%s Version %d.%d %s \n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);\r
30fdf114
LG
53}\r
54\r
55VOID\r
56Usage (\r
57 VOID\r
58 )\r
59/*++\r
60\r
61Routine Description:\r
62\r
63 Displays the utility usage syntax to STDOUT\r
64\r
65Arguments:\r
66\r
67 None\r
68\r
69Returns:\r
70\r
71 None\r
72\r
73--*/\r
74{\r
75 //\r
76 // Summary usage\r
77 //\r
4afd3d04 78 fprintf (stdout, "Usage: GenCrc32 -e|-d [options] <input_file>\n\n");\r
30fdf114
LG
79 \r
80 //\r
81 // Copyright declaration\r
82 // \r
f4260465 83 fprintf (stdout, "Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.\n\n");\r
30fdf114
LG
84\r
85 //\r
86 // Details Option\r
87 //\r
4afd3d04
LG
88 fprintf (stdout, "optional arguments:\n");\r
89 fprintf (stdout, " -h, --help Show this help message and exit\n");\r
90 fprintf (stdout, " --version Show program's version number and exit\n");\r
91 fprintf (stdout, " --debug [DEBUG] Output DEBUG statements, where DEBUG_LEVEL is 0 (min)\n\\r
92 - 9 (max)\n");\r
93 fprintf (stdout, " -v, --verbose Print informational statements\n");\r
94 fprintf (stdout, " -q, --quiet Returns the exit code, error messages will be\n\\r
95 displayed\n");\r
96 fprintf (stdout, " -s, --silent Returns only the exit code; informational and error\n\\r
97 messages are not displayed\n");\r
98 fprintf (stdout, " -e, --encode Calculate CRC32 value for the input file\n");\r
99 fprintf (stdout, " -d, --decode Verify CRC32 value for the input file\n");\r
100 fprintf (stdout, " -o OUTPUT_FILENAME, --output OUTPUT_FILENAME\n\\r
101 Output file name\n");\r
102 fprintf (stdout, " --sfo Reserved for future use\n");\r
103 \r
30fdf114
LG
104}\r
105\r
106int\r
107main (\r
fd171542 108 int argc,\r
30fdf114
LG
109 CHAR8 *argv[]\r
110 )\r
111/*++\r
112\r
113Routine Description:\r
114\r
115 Main function.\r
116\r
117Arguments:\r
118\r
119 argc - Number of command line parameters.\r
120 argv - Array of pointers to parameter strings.\r
121\r
122Returns:\r
123 STATUS_SUCCESS - Utility exits successfully.\r
124 STATUS_ERROR - Some error occurred during execution.\r
125\r
126--*/\r
127{\r
128 EFI_STATUS Status;\r
129 CHAR8 *OutputFileName;\r
130 CHAR8 *InputFileName;\r
131 UINT8 *FileBuffer;\r
132 UINT32 FileSize;\r
133 UINT64 LogLevel;\r
134 UINT8 FileAction;\r
135 UINT32 Crc32Value;\r
136 FILE *InFile;\r
137 FILE *OutFile;\r
138 \r
139 //\r
140 // Init local variables\r
141 //\r
142 LogLevel = 0;\r
143 Status = EFI_SUCCESS;\r
144 InputFileName = NULL;\r
145 OutputFileName = NULL;\r
146 FileAction = CRC32_NULL;\r
147 InFile = NULL;\r
148 OutFile = NULL;\r
149 Crc32Value = 0;\r
150 FileBuffer = NULL;\r
151\r
152 SetUtilityName (UTILITY_NAME);\r
153\r
154 if (argc == 1) {\r
155 Error (NULL, 0, 1001, "Missing options", "no options input");\r
156 Usage ();\r
157 return STATUS_ERROR;\r
158 }\r
159\r
160 //\r
161 // Parse command line\r
162 //\r
163 argc --;\r
164 argv ++;\r
165\r
166 if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) {\r
30fdf114
LG
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
fd171542 225 Error (NULL, 0, 1003, "Invalid option value", "Debug Level range is 0-9, current input level is %d", (int) LogLevel);\r
30fdf114
LG
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
1be2ed90 279 InFile = fopen (LongFilePath (InputFileName), "rb");\r
30fdf114
LG
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
fd171542 297 VerboseMsg ("the size of the input file is %u bytes", (unsigned) FileSize);\r
30fdf114
LG
298 \r
299 //\r
300 // Open output file\r
301 //\r
1be2ed90 302 OutFile = fopen (LongFilePath (OutputFileName), "wb");\r
30fdf114
LG
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
fd171542 321 VerboseMsg ("The calculated CRC32 value is 0x%08x", (unsigned) Crc32Value);\r
30fdf114 322 fwrite (FileBuffer, 1, FileSize, OutFile);\r
fd171542 323 VerboseMsg ("the size of the encoded file is %u bytes", (unsigned) FileSize + sizeof (UINT32));\r
30fdf114
LG
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
fd171542 333 VerboseMsg ("The calculated CRC32 value is 0x%08x and File Crc32 value is 0x%08x", (unsigned) Crc32Value, (unsigned) (*(UINT32 *)FileBuffer));\r
30fdf114
LG
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
fd171542 343 VerboseMsg ("the size of the decoded file is %u bytes", (unsigned) FileSize - sizeof (UINT32));\r
30fdf114
LG
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