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