GenCrc32.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /** @file
  2. Calculate Crc32 value and Verify Crc32 value for input data.
  3. Copyright (c) 2007 - 2018, 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. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  10. **/
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "ParseInf.h"
  15. #include "EfiUtilityMsgs.h"
  16. #include "CommonLib.h"
  17. #include "Crc32.h"
  18. #define UTILITY_NAME "GenCrc32"
  19. #define UTILITY_MAJOR_VERSION 0
  20. #define UTILITY_MINOR_VERSION 2
  21. #define CRC32_NULL 0
  22. #define CRC32_ENCODE 1
  23. #define CRC32_DECODE 2
  24. VOID
  25. Version (
  26. VOID
  27. )
  28. /*++
  29. Routine Description:
  30. Displays the standard utility information to SDTOUT
  31. Arguments:
  32. None
  33. Returns:
  34. None
  35. --*/
  36. {
  37. fprintf (stdout, "%s Version %d.%d %s \n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);
  38. }
  39. VOID
  40. Usage (
  41. VOID
  42. )
  43. /*++
  44. Routine Description:
  45. Displays the utility usage syntax to STDOUT
  46. Arguments:
  47. None
  48. Returns:
  49. None
  50. --*/
  51. {
  52. //
  53. // Summary usage
  54. //
  55. fprintf (stdout, "Usage: GenCrc32 -e|-d [options] <input_file>\n\n");
  56. //
  57. // Copyright declaration
  58. //
  59. fprintf (stdout, "Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.\n\n");
  60. //
  61. // Details Option
  62. //
  63. fprintf (stdout, "optional arguments:\n");
  64. fprintf (stdout, " -h, --help Show this help message and exit\n");
  65. fprintf (stdout, " --version Show program's version number and exit\n");
  66. fprintf (stdout, " --debug [DEBUG] Output DEBUG statements, where DEBUG_LEVEL is 0 (min)\n\
  67. - 9 (max)\n");
  68. fprintf (stdout, " -v, --verbose Print informational statements\n");
  69. fprintf (stdout, " -q, --quiet Returns the exit code, error messages will be\n\
  70. displayed\n");
  71. fprintf (stdout, " -s, --silent Returns only the exit code; informational and error\n\
  72. messages are not displayed\n");
  73. fprintf (stdout, " -e, --encode Calculate CRC32 value for the input file\n");
  74. fprintf (stdout, " -d, --decode Verify CRC32 value for the input file\n");
  75. fprintf (stdout, " -o OUTPUT_FILENAME, --output OUTPUT_FILENAME\n\
  76. Output file name\n");
  77. fprintf (stdout, " --sfo Reserved for future use\n");
  78. }
  79. int
  80. main (
  81. int argc,
  82. CHAR8 *argv[]
  83. )
  84. /*++
  85. Routine Description:
  86. Main function.
  87. Arguments:
  88. argc - Number of command line parameters.
  89. argv - Array of pointers to parameter strings.
  90. Returns:
  91. STATUS_SUCCESS - Utility exits successfully.
  92. STATUS_ERROR - Some error occurred during execution.
  93. --*/
  94. {
  95. EFI_STATUS Status;
  96. CHAR8 *OutputFileName;
  97. CHAR8 *InputFileName;
  98. UINT8 *FileBuffer;
  99. UINT32 FileSize;
  100. UINT64 LogLevel;
  101. UINT8 FileAction;
  102. UINT32 Crc32Value;
  103. FILE *InFile;
  104. FILE *OutFile;
  105. //
  106. // Init local variables
  107. //
  108. LogLevel = 0;
  109. Status = EFI_SUCCESS;
  110. InputFileName = NULL;
  111. OutputFileName = NULL;
  112. FileAction = CRC32_NULL;
  113. InFile = NULL;
  114. OutFile = NULL;
  115. Crc32Value = 0;
  116. FileBuffer = NULL;
  117. SetUtilityName (UTILITY_NAME);
  118. if (argc == 1) {
  119. Error (NULL, 0, 1001, "Missing options", "no options input");
  120. Usage ();
  121. return STATUS_ERROR;
  122. }
  123. //
  124. // Parse command line
  125. //
  126. argc --;
  127. argv ++;
  128. if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) {
  129. Usage ();
  130. return STATUS_SUCCESS;
  131. }
  132. if (stricmp (argv[0], "--version") == 0) {
  133. Version ();
  134. return STATUS_SUCCESS;
  135. }
  136. while (argc > 0) {
  137. if ((stricmp (argv[0], "-o") == 0) || (stricmp (argv[0], "--output") == 0)) {
  138. if (argv[1] == NULL || argv[1][0] == '-') {
  139. Error (NULL, 0, 1003, "Invalid option value", "Output File name is missing for -o option");
  140. goto Finish;
  141. }
  142. OutputFileName = argv[1];
  143. argc -= 2;
  144. argv += 2;
  145. continue;
  146. }
  147. if ((stricmp (argv[0], "-e") == 0) || (stricmp (argv[0], "--encode") == 0)) {
  148. FileAction = CRC32_ENCODE;
  149. argc --;
  150. argv ++;
  151. continue;
  152. }
  153. if ((stricmp (argv[0], "-d") == 0) || (stricmp (argv[0], "--decode") == 0)) {
  154. FileAction = CRC32_DECODE;
  155. argc --;
  156. argv ++;
  157. continue;
  158. }
  159. if ((stricmp (argv[0], "-v") == 0) || (stricmp (argv[0], "--verbose") == 0)) {
  160. SetPrintLevel (VERBOSE_LOG_LEVEL);
  161. VerboseMsg ("Verbose output Mode Set!");
  162. argc --;
  163. argv ++;
  164. continue;
  165. }
  166. if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) {
  167. SetPrintLevel (KEY_LOG_LEVEL);
  168. KeyMsg ("Quiet output Mode Set!");
  169. argc --;
  170. argv ++;
  171. continue;
  172. }
  173. if (stricmp (argv[0], "--debug") == 0) {
  174. Status = AsciiStringToUint64 (argv[1], FALSE, &LogLevel);
  175. if (EFI_ERROR (Status)) {
  176. Error (NULL, 0, 1003, "Invalid option value", "%s = %s", argv[0], argv[1]);
  177. goto Finish;
  178. }
  179. if (LogLevel > 9) {
  180. Error (NULL, 0, 1003, "Invalid option value", "Debug Level range is 0-9, current input level is %d", (int) LogLevel);
  181. goto Finish;
  182. }
  183. SetPrintLevel (LogLevel);
  184. DebugMsg (NULL, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", argv[1]);
  185. argc -= 2;
  186. argv += 2;
  187. continue;
  188. }
  189. if (argv[0][0] == '-') {
  190. Error (NULL, 0, 1000, "Unknown option", argv[0]);
  191. goto Finish;
  192. }
  193. //
  194. // Get Input file file name.
  195. //
  196. InputFileName = argv[0];
  197. argc --;
  198. argv ++;
  199. }
  200. VerboseMsg ("%s tool start.", UTILITY_NAME);
  201. //
  202. // Check Input parameters
  203. //
  204. if (FileAction == CRC32_NULL) {
  205. Error (NULL, 0, 1001, "Missing option", "either the encode or the decode option must be specified!");
  206. return STATUS_ERROR;
  207. } else if (FileAction == CRC32_ENCODE) {
  208. VerboseMsg ("File will be encoded by Crc32");
  209. } else if (FileAction == CRC32_DECODE) {
  210. VerboseMsg ("File will be decoded by Crc32");
  211. }
  212. if (InputFileName == NULL) {
  213. Error (NULL, 0, 1001, "Missing option", "Input files are not specified");
  214. goto Finish;
  215. } else {
  216. VerboseMsg ("Input file name is %s", InputFileName);
  217. }
  218. if (OutputFileName == NULL) {
  219. Error (NULL, 0, 1001, "Missing option", "Output file are not specified");
  220. goto Finish;
  221. } else {
  222. VerboseMsg ("Output file name is %s", OutputFileName);
  223. }
  224. //
  225. // Open Input file and read file data.
  226. //
  227. InFile = fopen (LongFilePath (InputFileName), "rb");
  228. if (InFile == NULL) {
  229. Error (NULL, 0, 0001, "Error opening file", InputFileName);
  230. return STATUS_ERROR;
  231. }
  232. fseek (InFile, 0, SEEK_END);
  233. FileSize = ftell (InFile);
  234. fseek (InFile, 0, SEEK_SET);
  235. FileBuffer = (UINT8 *) malloc (FileSize);
  236. if (FileBuffer == NULL) {
  237. Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
  238. fclose (InFile);
  239. goto Finish;
  240. }
  241. fread (FileBuffer, 1, FileSize, InFile);
  242. fclose (InFile);
  243. VerboseMsg ("the size of the input file is %u bytes", (unsigned) FileSize);
  244. //
  245. // Open output file
  246. //
  247. OutFile = fopen (LongFilePath (OutputFileName), "wb");
  248. if (OutFile == NULL) {
  249. Error (NULL, 0, 0001, "Error opening file", OutputFileName);
  250. goto Finish;
  251. }
  252. //
  253. // Calculate Crc32 value
  254. //
  255. if (FileAction == CRC32_ENCODE) {
  256. Status = CalculateCrc32 (FileBuffer, FileSize, &Crc32Value);
  257. if (Status != EFI_SUCCESS) {
  258. Error (NULL, 0, 3000, "Invalid", "Calculate CRC32 value failed!");
  259. goto Finish;
  260. }
  261. //
  262. // Done, write output file.
  263. //
  264. fwrite (&Crc32Value, 1, sizeof (Crc32Value), OutFile);
  265. VerboseMsg ("The calculated CRC32 value is 0x%08x", (unsigned) Crc32Value);
  266. fwrite (FileBuffer, 1, FileSize, OutFile);
  267. VerboseMsg ("the size of the encoded file is %u bytes", (unsigned) FileSize + sizeof (UINT32));
  268. } else {
  269. //
  270. // Verify Crc32 Value
  271. //
  272. if (FileSize < sizeof (UINT32)) {
  273. Error (NULL, 0, 3000, "Invalid", "Input file is invalid!");
  274. goto Finish;
  275. }
  276. Status = CalculateCrc32 (FileBuffer + sizeof (UINT32), FileSize - sizeof (UINT32), &Crc32Value);
  277. if (Status != EFI_SUCCESS) {
  278. Error (NULL, 0, 3000, "Invalid", "Calculate CRC32 value failed!");
  279. goto Finish;
  280. }
  281. VerboseMsg ("The calculated CRC32 value is 0x%08x and File Crc32 value is 0x%08x", (unsigned) Crc32Value, (unsigned) (*(UINT32 *)FileBuffer));
  282. if (Crc32Value != *(UINT32 *)FileBuffer) {
  283. Error (NULL, 0, 3000, "Invalid", "CRC32 value of input file is not correct!");
  284. Status = STATUS_ERROR;
  285. goto Finish;
  286. }
  287. //
  288. // Done, write output file.
  289. //
  290. fwrite (FileBuffer + sizeof (UINT32), 1, FileSize - sizeof (UINT32), OutFile);
  291. VerboseMsg ("the size of the decoded file is %u bytes", (unsigned) FileSize - sizeof (UINT32));
  292. }
  293. Finish:
  294. if (FileBuffer != NULL) {
  295. free (FileBuffer);
  296. }
  297. if (OutFile != NULL) {
  298. fclose (OutFile);
  299. }
  300. VerboseMsg ("%s tool done with return code is 0x%x.", UTILITY_NAME, GetUtilityStatus ());
  301. return GetUtilityStatus ();
  302. }