PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/edk2/EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/GenCRC32Section.c

https://gitlab.com/envieidoc/Clover
C | 313 lines | 208 code | 42 blank | 63 comment | 38 complexity | 555cdf3f999ef69d58d8bd1f7a0f1ceb MD5 | raw file
  1. /*++
  2. Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
  3. This program and the accompanying materials
  4. are licensed and made available under the terms and conditions of the BSD License
  5. which accompanies this distribution. The full text of the license may be found at
  6. http://opensource.org/licenses/bsd-license.php
  7. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  9. Module Name:
  10. GenCRC32Section.c
  11. Abstract:
  12. This file contains functions required to generate a Firmware File System
  13. file. The code is compliant with the Tiano C Coding standards.
  14. --*/
  15. #include "TianoCommon.h"
  16. #include "EfiFirmwareFileSystem.h"
  17. #include "EfiFirmwareVolumeHeader.h"
  18. #include "ParseInf.h"
  19. #include "crc32.h"
  20. #include "EfiUtilityMsgs.h"
  21. #include "GenCRC32Section.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include "CommonLib.h"
  27. #include EFI_PROTOCOL_DEFINITION (GuidedSectionExtraction)
  28. #define UTILITY_VERSION "v1.0"
  29. #define UTILITY_NAME "GenCrc32Section"
  30. EFI_GUID gEfiCrc32SectionGuid = EFI_CRC32_GUIDED_SECTION_EXTRACTION_PROTOCOL_GUID;
  31. EFI_STATUS
  32. SignSectionWithCrc32 (
  33. IN OUT UINT8 *FileBuffer,
  34. IN OUT UINT32 *BufferSize,
  35. IN UINT32 DataSize
  36. )
  37. /*++
  38. Routine Description:
  39. Signs the section with CRC32 and add GUIDed section header for the
  40. signed data. data stays in same location (overwrites source data).
  41. Arguments:
  42. FileBuffer - Buffer containing data to sign
  43. BufferSize - On input, the size of FileBuffer. On output, the size of
  44. actual section data (including added section header).
  45. DataSize - Length of data to Sign
  46. Key - Key to use when signing. Currently only CRC32 is supported.
  47. Returns:
  48. EFI_SUCCESS - Successful
  49. EFI_OUT_OF_RESOURCES - Not enough resource to complete the operation.
  50. --*/
  51. {
  52. UINT32 Crc32Checksum;
  53. EFI_STATUS Status;
  54. UINT32 TotalSize;
  55. CRC32_SECTION_HEADER Crc32Header;
  56. UINT8 *SwapBuffer;
  57. Crc32Checksum = 0;
  58. SwapBuffer = NULL;
  59. if (DataSize == 0) {
  60. *BufferSize = 0;
  61. return EFI_SUCCESS;
  62. }
  63. Status = CalculateCrc32 (FileBuffer, DataSize, &Crc32Checksum);
  64. if (EFI_ERROR (Status)) {
  65. return Status;
  66. }
  67. TotalSize = DataSize + CRC32_SECTION_HEADER_SIZE;
  68. Crc32Header.GuidSectionHeader.CommonHeader.Type = EFI_SECTION_GUID_DEFINED;
  69. Crc32Header.GuidSectionHeader.CommonHeader.Size[0] = (UINT8) (TotalSize & 0xff);
  70. Crc32Header.GuidSectionHeader.CommonHeader.Size[1] = (UINT8) ((TotalSize & 0xff00) >> 8);
  71. Crc32Header.GuidSectionHeader.CommonHeader.Size[2] = (UINT8) ((TotalSize & 0xff0000) >> 16);
  72. memcpy (&(Crc32Header.GuidSectionHeader.SectionDefinitionGuid), &gEfiCrc32SectionGuid, sizeof (EFI_GUID));
  73. Crc32Header.GuidSectionHeader.Attributes = EFI_GUIDED_SECTION_AUTH_STATUS_VALID;
  74. Crc32Header.GuidSectionHeader.DataOffset = CRC32_SECTION_HEADER_SIZE;
  75. Crc32Header.CRC32Checksum = Crc32Checksum;
  76. SwapBuffer = (UINT8 *) malloc (DataSize);
  77. if (SwapBuffer == NULL) {
  78. return EFI_OUT_OF_RESOURCES;
  79. }
  80. memcpy (SwapBuffer, FileBuffer, DataSize);
  81. memcpy (FileBuffer, &Crc32Header, CRC32_SECTION_HEADER_SIZE);
  82. memcpy (FileBuffer + CRC32_SECTION_HEADER_SIZE, SwapBuffer, DataSize);
  83. //
  84. // Make sure section ends on a DWORD boundary
  85. //
  86. while ((TotalSize & 0x03) != 0) {
  87. FileBuffer[TotalSize] = 0;
  88. TotalSize++;
  89. }
  90. *BufferSize = TotalSize;
  91. if (SwapBuffer != NULL) {
  92. free (SwapBuffer);
  93. }
  94. return EFI_SUCCESS;
  95. }
  96. VOID
  97. PrintUsage (
  98. VOID
  99. )
  100. {
  101. int Index;
  102. const char *Str[] = {
  103. UTILITY_NAME" "UTILITY_VERSION" - Intel Generate CRC32 Section Utility",
  104. " Copyright (C), 2004 - 2008 Intel Corporation",
  105. #if ( defined(UTILITY_BUILD) && defined(UTILITY_VENDOR) )
  106. " Built from "UTILITY_BUILD", project of "UTILITY_VENDOR,
  107. #endif
  108. "",
  109. "Usage:",
  110. " "UTILITY_NAME" [OPTION]",
  111. "Options:",
  112. " -i Input1 ... specifies the input file(s) that would be signed to CRC32",
  113. " Guided section.",
  114. " -o Output specifies the output file that is a CRC32 Guided section",
  115. NULL
  116. };
  117. for (Index = 0; Str[Index] != NULL; Index++) {
  118. fprintf (stdout, "%s\n", Str[Index]);
  119. }
  120. }
  121. INT32
  122. ReadFilesContentsIntoBuffer (
  123. IN CHAR8 *argv[],
  124. IN INT32 Start,
  125. IN OUT UINT8 **FileBuffer,
  126. IN OUT UINT32 *BufferSize,
  127. OUT UINT32 *ContentSize,
  128. IN INT32 MaximumArguments
  129. )
  130. {
  131. INT32 Index;
  132. CHAR8 *FileName;
  133. FILE *InputFile;
  134. UINT8 Temp;
  135. UINT32 Size;
  136. FileName = NULL;
  137. InputFile = NULL;
  138. Size = 0;
  139. Index = 0;
  140. //
  141. // read all input files into one file buffer
  142. //
  143. while (argv[Start + Index][0] != '-') {
  144. FileName = argv[Start + Index];
  145. InputFile = fopen (FileName, "rb");
  146. if (InputFile == NULL) {
  147. Error (NULL, 0, 0, FileName, "failed to open input binary file");
  148. return -1;
  149. }
  150. fread (&Temp, sizeof (UINT8), 1, InputFile);
  151. while (!feof (InputFile)) {
  152. (*FileBuffer)[Size++] = Temp;
  153. fread (&Temp, sizeof (UINT8), 1, InputFile);
  154. }
  155. fclose (InputFile);
  156. InputFile = NULL;
  157. //
  158. // Make sure section ends on a DWORD boundary
  159. //
  160. while ((Size & 0x03) != 0) {
  161. (*FileBuffer)[Size] = 0;
  162. Size++;
  163. }
  164. Index++;
  165. if (Index == MaximumArguments) {
  166. break;
  167. }
  168. }
  169. *ContentSize = Size;
  170. return Index;
  171. }
  172. INT32
  173. main (
  174. INT32 argc,
  175. CHAR8 *argv[]
  176. )
  177. {
  178. FILE *OutputFile;
  179. UINT8 *FileBuffer;
  180. UINT32 BufferSize;
  181. EFI_STATUS Status;
  182. UINT32 ContentSize;
  183. CHAR8 *OutputFileName;
  184. INT32 ReturnValue;
  185. INT32 Index;
  186. OutputFile = NULL;
  187. FileBuffer = NULL;
  188. ContentSize = 0;
  189. OutputFileName = NULL;
  190. SetUtilityName (UTILITY_NAME);
  191. if (argc == 1) {
  192. PrintUsage ();
  193. return -1;
  194. }
  195. BufferSize = 1024 * 1024 * 16;
  196. FileBuffer = (UINT8 *) malloc (BufferSize * sizeof (UINT8));
  197. if (FileBuffer == NULL) {
  198. Error (NULL, 0, 0, "memory allocation failed", NULL);
  199. return -1;
  200. }
  201. ZeroMem (FileBuffer, BufferSize);
  202. for (Index = 0; Index < argc; Index++) {
  203. if (_strcmpi (argv[Index], "-i") == 0) {
  204. ReturnValue = ReadFilesContentsIntoBuffer (
  205. argv,
  206. (Index + 1),
  207. &FileBuffer,
  208. &BufferSize,
  209. &ContentSize,
  210. (argc - (Index + 1))
  211. );
  212. if (ReturnValue == -1) {
  213. Error (NULL, 0, 0, "failed to read file contents", NULL);
  214. return -1;
  215. }
  216. Index += ReturnValue;
  217. }
  218. if (_strcmpi (argv[Index], "-o") == 0) {
  219. OutputFileName = argv[Index + 1];
  220. }
  221. }
  222. OutputFile = fopen (OutputFileName, "wb");
  223. if (OutputFile == NULL) {
  224. Error (NULL, 0, 0, OutputFileName, "failed to open output binary file");
  225. free (FileBuffer);
  226. return -1;
  227. }
  228. /*
  229. //
  230. // make sure section ends on a DWORD boundary ??
  231. //
  232. while ( (Size & 0x03) != 0 ) {
  233. FileBuffer[Size] = 0;
  234. Size ++;
  235. }
  236. */
  237. Status = SignSectionWithCrc32 (FileBuffer, &BufferSize, ContentSize);
  238. if (EFI_ERROR (Status)) {
  239. Error (NULL, 0, 0, "failed to sign section", NULL);
  240. free (FileBuffer);
  241. fclose (OutputFile);
  242. return -1;
  243. }
  244. ContentSize = fwrite (FileBuffer, sizeof (UINT8), BufferSize, OutputFile);
  245. if (ContentSize != BufferSize) {
  246. Error (NULL, 0, 0, "failed to write output buffer", NULL);
  247. ReturnValue = -1;
  248. } else {
  249. ReturnValue = 0;
  250. }
  251. free (FileBuffer);
  252. fclose (OutputFile);
  253. return ReturnValue;
  254. }