]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Calculate Crc32 value and Verify Crc32 value for input data.\r | |
3 | \r | |
4 | Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r | |
5 | This program and the accompanying materials\r | |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include <stdio.h>\r | |
16 | #include <stdlib.h>\r | |
17 | #include <string.h>\r | |
18 | \r | |
19 | #include "ParseInf.h"\r | |
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 | |
26 | #define UTILITY_MINOR_VERSION 2\r | |
27 | \r | |
28 | #define CRC32_NULL 0\r | |
29 | #define CRC32_ENCODE 1\r | |
30 | #define CRC32_DECODE 2\r | |
31 | \r | |
32 | VOID\r | |
33 | Version (\r | |
34 | VOID\r | |
35 | )\r | |
36 | /*++\r | |
37 | \r | |
38 | Routine Description:\r | |
39 | \r | |
40 | Displays the standard utility information to SDTOUT\r | |
41 | \r | |
42 | Arguments:\r | |
43 | \r | |
44 | None\r | |
45 | \r | |
46 | Returns:\r | |
47 | \r | |
48 | None\r | |
49 | \r | |
50 | --*/\r | |
51 | {\r | |
52 | fprintf (stdout, "%s Version %d.%d %s \n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);\r | |
53 | }\r | |
54 | \r | |
55 | VOID\r | |
56 | Usage (\r | |
57 | VOID\r | |
58 | )\r | |
59 | /*++\r | |
60 | \r | |
61 | Routine Description:\r | |
62 | \r | |
63 | Displays the utility usage syntax to STDOUT\r | |
64 | \r | |
65 | Arguments:\r | |
66 | \r | |
67 | None\r | |
68 | \r | |
69 | Returns:\r | |
70 | \r | |
71 | None\r | |
72 | \r | |
73 | --*/\r | |
74 | {\r | |
75 | //\r | |
76 | // Summary usage\r | |
77 | //\r | |
78 | fprintf (stdout, "Usage: GenCrc32 -e|-d [options] <input_file>\n\n");\r | |
79 | \r | |
80 | //\r | |
81 | // Copyright declaration\r | |
82 | //\r | |
83 | fprintf (stdout, "Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.\n\n");\r | |
84 | \r | |
85 | //\r | |
86 | // Details Option\r | |
87 | //\r | |
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 | |
104 | }\r | |
105 | \r | |
106 | int\r | |
107 | main (\r | |
108 | int argc,\r | |
109 | CHAR8 *argv[]\r | |
110 | )\r | |
111 | /*++\r | |
112 | \r | |
113 | Routine Description:\r | |
114 | \r | |
115 | Main function.\r | |
116 | \r | |
117 | Arguments:\r | |
118 | \r | |
119 | argc - Number of command line parameters.\r | |
120 | argv - Array of pointers to parameter strings.\r | |
121 | \r | |
122 | Returns:\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 | |
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", (int) 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 parameters\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 (LongFilePath (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 | fclose (InFile);\r | |
293 | goto Finish;\r | |
294 | }\r | |
295 | \r | |
296 | fread (FileBuffer, 1, FileSize, InFile);\r | |
297 | fclose (InFile);\r | |
298 | VerboseMsg ("the size of the input file is %u bytes", (unsigned) FileSize);\r | |
299 | \r | |
300 | //\r | |
301 | // Open output file\r | |
302 | //\r | |
303 | OutFile = fopen (LongFilePath (OutputFileName), "wb");\r | |
304 | if (OutFile == NULL) {\r | |
305 | Error (NULL, 0, 0001, "Error opening file", OutputFileName);\r | |
306 | goto Finish;\r | |
307 | }\r | |
308 | \r | |
309 | //\r | |
310 | // Calculate Crc32 value\r | |
311 | //\r | |
312 | if (FileAction == CRC32_ENCODE) {\r | |
313 | Status = CalculateCrc32 (FileBuffer, FileSize, &Crc32Value);\r | |
314 | if (Status != EFI_SUCCESS) {\r | |
315 | Error (NULL, 0, 3000, "Invalid", "Calculate CRC32 value failed!");\r | |
316 | goto Finish;\r | |
317 | }\r | |
318 | //\r | |
319 | // Done, write output file.\r | |
320 | //\r | |
321 | fwrite (&Crc32Value, 1, sizeof (Crc32Value), OutFile);\r | |
322 | VerboseMsg ("The calculated CRC32 value is 0x%08x", (unsigned) Crc32Value);\r | |
323 | fwrite (FileBuffer, 1, FileSize, OutFile);\r | |
324 | VerboseMsg ("the size of the encoded file is %u bytes", (unsigned) FileSize + sizeof (UINT32));\r | |
325 | } else {\r | |
326 | //\r | |
327 | // Verify Crc32 Value\r | |
328 | //\r | |
329 | if (FileSize < sizeof (UINT32)) {\r | |
330 | Error (NULL, 0, 3000, "Invalid", "Input file is invalid!");\r | |
331 | goto Finish;\r | |
332 | }\r | |
333 | Status = CalculateCrc32 (FileBuffer + sizeof (UINT32), FileSize - sizeof (UINT32), &Crc32Value);\r | |
334 | if (Status != EFI_SUCCESS) {\r | |
335 | Error (NULL, 0, 3000, "Invalid", "Calculate CRC32 value failed!");\r | |
336 | goto Finish;\r | |
337 | }\r | |
338 | VerboseMsg ("The calculated CRC32 value is 0x%08x and File Crc32 value is 0x%08x", (unsigned) Crc32Value, (unsigned) (*(UINT32 *)FileBuffer));\r | |
339 | if (Crc32Value != *(UINT32 *)FileBuffer) {\r | |
340 | Error (NULL, 0, 3000, "Invalid", "CRC32 value of input file is not correct!");\r | |
341 | Status = STATUS_ERROR;\r | |
342 | goto Finish;\r | |
343 | }\r | |
344 | //\r | |
345 | // Done, write output file.\r | |
346 | //\r | |
347 | fwrite (FileBuffer + sizeof (UINT32), 1, FileSize - sizeof (UINT32), OutFile);\r | |
348 | VerboseMsg ("the size of the decoded file is %u bytes", (unsigned) FileSize - sizeof (UINT32));\r | |
349 | }\r | |
350 | \r | |
351 | Finish:\r | |
352 | if (FileBuffer != NULL) {\r | |
353 | free (FileBuffer);\r | |
354 | }\r | |
355 | \r | |
356 | if (OutFile != NULL) {\r | |
357 | fclose (OutFile);\r | |
358 | }\r | |
359 | \r | |
360 | VerboseMsg ("%s tool done with return code is 0x%x.", UTILITY_NAME, GetUtilityStatus ());\r | |
361 | \r | |
362 | return GetUtilityStatus ();\r | |
363 | }\r | |
364 | \r | |
365 | \r | |
366 | \r | |
367 | \r |