3 Copyright (c) 2004-2007, Intel Corporation
4 All rights reserved. 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
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 This file contains functions required to generate a Firmware File System
19 file. The code is compliant with the Tiano C Coding standards.
23 #include "GenCRC32Section.h"
25 #define UTILITY_NAME "GenCrc32Section"
26 #define UTILITY_MAJOR_VERSION 0
27 #define UTILITY_MINOR_VERSION 2
29 EFI_GUID gEfiCrc32SectionGuid
= EFI_CRC32_GUIDED_SECTION_EXTRACTION_PROTOCOL_GUID
;
32 SignSectionWithCrc32 (
33 IN OUT UINT8
*FileBuffer
,
34 IN OUT UINT32
*BufferSize
,
41 Signs the section with CRC32 and add GUIDed section header for the
42 signed data. data stays in same location (overwrites source data).
46 FileBuffer - Buffer containing data to sign
48 BufferSize - On input, the size of FileBuffer. On output, the size of
49 actual section data (including added section header).
51 DataSize - Length of data to Sign
53 Key - Key to use when signing. Currently only CRC32 is supported.
57 EFI_SUCCESS - Successful
58 EFI_OUT_OF_RESOURCES - Not enough resource to complete the operation.
66 CRC32_SECTION_HEADER Crc32Header
;
78 Status
= CalculateCrc32 (FileBuffer
, DataSize
, &Crc32Checksum
);
79 if (EFI_ERROR (Status
)) {
83 TotalSize
= DataSize
+ CRC32_SECTION_HEADER_SIZE
;
84 Crc32Header
.GuidSectionHeader
.CommonHeader
.Type
= EFI_SECTION_GUID_DEFINED
;
85 Crc32Header
.GuidSectionHeader
.CommonHeader
.Size
[0] = (UINT8
) (TotalSize
& 0xff);
86 Crc32Header
.GuidSectionHeader
.CommonHeader
.Size
[1] = (UINT8
) ((TotalSize
& 0xff00) >> 8);
87 Crc32Header
.GuidSectionHeader
.CommonHeader
.Size
[2] = (UINT8
) ((TotalSize
& 0xff0000) >> 16);
88 memcpy (&(Crc32Header
.GuidSectionHeader
.SectionDefinitionGuid
), &gEfiCrc32SectionGuid
, sizeof (EFI_GUID
));
89 Crc32Header
.GuidSectionHeader
.Attributes
= EFI_GUIDED_SECTION_AUTH_STATUS_VALID
;
90 Crc32Header
.GuidSectionHeader
.DataOffset
= CRC32_SECTION_HEADER_SIZE
;
91 Crc32Header
.CRC32Checksum
= Crc32Checksum
;
93 SwapBuffer
= (UINT8
*) malloc (DataSize
);
94 if (SwapBuffer
== NULL
) {
95 return EFI_OUT_OF_RESOURCES
;
98 memcpy (SwapBuffer
, FileBuffer
, DataSize
);
99 memcpy (FileBuffer
, &Crc32Header
, CRC32_SECTION_HEADER_SIZE
);
100 memcpy (FileBuffer
+ CRC32_SECTION_HEADER_SIZE
, SwapBuffer
, DataSize
);
103 // Make sure section ends on a DWORD boundary
105 while ((TotalSize
& 0x03) != 0) {
106 FileBuffer
[TotalSize
] = 0;
110 *BufferSize
= TotalSize
;
112 if (SwapBuffer
!= NULL
) {
127 Displays the standard utility information to SDTOUT
140 "%s v%d.%d -Utility for generating Firmware File System files.\n",
142 UTILITY_MAJOR_VERSION
,
143 UTILITY_MINOR_VERSION
155 printf ("\nUsage:\n");
156 printf (UTILITY_NAME
" -i Inputfile1 Inputfile2 -o Outputfile\n");
157 printf (" -i Inputfile: specifies the input files signed to CRC32 Guided section.\n");
158 printf (" -o Outputfile: specifies the output file that is a CRC32 Guided section.\n");
162 ReadFilesContentsIntoBuffer (
165 IN OUT UINT8
**FileBuffer
,
166 IN OUT UINT32
*BufferSize
,
167 OUT UINT32
*ContentSize
,
168 IN INT32 MaximumArguments
183 // read all input files into one file buffer
185 while (argv
[Start
+ Index
][0] != '-') {
187 FileName
= argv
[Start
+ Index
];
188 InputFile
= fopen (FileName
, "rb");
189 if (InputFile
== NULL
) {
190 Error (NULL
, 0, 0, FileName
, "failed to open input binary file");
194 fread (&Temp
, sizeof (UINT8
), 1, InputFile
);
195 while (!feof (InputFile
)) {
196 (*FileBuffer
)[Size
++] = Temp
;
197 fread (&Temp
, sizeof (UINT8
), 1, InputFile
);
204 // Make sure section ends on a DWORD boundary
206 while ((Size
& 0x03) != 0) {
207 (*FileBuffer
)[Size
] = 0;
212 if (Index
== MaximumArguments
) {
232 CHAR8
*OutputFileName
;
239 OutputFileName
= NULL
;
241 SetUtilityName (UTILITY_NAME
);
248 if ((strcmp(argv
[1], "-h") == 0) || (strcmp(argv
[1], "--help") == 0) ||
249 (strcmp(argv
[1], "-?") == 0) || (strcmp(argv
[1], "/?") == 0)) {
254 if ((strcmp(argv
[1], "-V") == 0) || (strcmp(argv
[1], "--version") == 0)) {
259 BufferSize
= 1024 * 1024 * 16;
260 FileBuffer
= (UINT8
*) malloc (BufferSize
* sizeof (UINT8
));
261 if (FileBuffer
== NULL
) {
262 Error (NULL
, 0, 0, "memory allocation failed", NULL
);
266 ZeroMem (FileBuffer
, BufferSize
);
268 for (Index
= 0; Index
< argc
; Index
++) {
269 if (strcmpi (argv
[Index
], "-i") == 0) {
270 ReturnValue
= ReadFilesContentsIntoBuffer (
278 if (ReturnValue
== -1) {
279 Error (NULL
, 0, 0, "failed to read file contents", NULL
);
283 Index
+= ReturnValue
;
286 if (strcmpi (argv
[Index
], "-o") == 0) {
287 OutputFileName
= argv
[Index
+ 1];
291 OutputFile
= fopen (OutputFileName
, "wb");
292 if (OutputFile
== NULL
) {
293 Error (NULL
, 0, 0, OutputFileName
, "failed to open output binary file");
300 // make sure section ends on a DWORD boundary ??
302 while ( (Size & 0x03) != 0 ) {
303 FileBuffer[Size] = 0;
307 Status
= SignSectionWithCrc32 (FileBuffer
, &BufferSize
, ContentSize
);
308 if (EFI_ERROR (Status
)) {
309 Error (NULL
, 0, 0, "failed to sign section", NULL
);
315 ContentSize
= fwrite (FileBuffer
, sizeof (UINT8
), BufferSize
, OutputFile
);
316 if (ContentSize
!= BufferSize
) {
317 Error (NULL
, 0, 0, "failed to write output buffer", NULL
);