3 Copyright (c) 2004, 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 TOOLVERSION "0.2"
27 #define UTILITY_NAME "GenCrc32Section"
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
) {
125 printf (UTILITY_NAME
" -i \"inputfile1\" \"inputfile2\" -o \"outputfile\" \n");
126 printf (" -i \"inputfile\":\n ");
127 printf (" specifies the input files that would be signed to CRC32 Guided section.\n");
128 printf (" -o \"outputfile\":\n");
129 printf (" specifies the output file that is a CRC32 Guided section.\n");
133 ReadFilesContentsIntoBuffer (
136 IN OUT UINT8
**FileBuffer
,
137 IN OUT UINT32
*BufferSize
,
138 OUT UINT32
*ContentSize
,
139 IN INT32 MaximumArguments
154 // read all input files into one file buffer
156 while (argv
[Start
+ Index
][0] != '-') {
158 FileName
= argv
[Start
+ Index
];
159 InputFile
= fopen (FileName
, "rb");
160 if (InputFile
== NULL
) {
161 Error (NULL
, 0, 0, FileName
, "failed to open input binary file");
165 fread (&Temp
, sizeof (UINT8
), 1, InputFile
);
166 while (!feof (InputFile
)) {
167 (*FileBuffer
)[Size
++] = Temp
;
168 fread (&Temp
, sizeof (UINT8
), 1, InputFile
);
175 // Make sure section ends on a DWORD boundary
177 while ((Size
& 0x03) != 0) {
178 (*FileBuffer
)[Size
] = 0;
183 if (Index
== MaximumArguments
) {
203 CHAR8
*OutputFileName
;
210 OutputFileName
= NULL
;
212 SetUtilityName (UTILITY_NAME
);
219 BufferSize
= 1024 * 1024 * 16;
220 FileBuffer
= (UINT8
*) malloc (BufferSize
* sizeof (UINT8
));
221 if (FileBuffer
== NULL
) {
222 Error (NULL
, 0, 0, "memory allocation failed", NULL
);
226 ZeroMem (FileBuffer
, BufferSize
);
228 for (Index
= 0; Index
< argc
; Index
++) {
229 if (strcmpi (argv
[Index
], "-i") == 0) {
230 ReturnValue
= ReadFilesContentsIntoBuffer (
238 if (ReturnValue
== -1) {
239 Error (NULL
, 0, 0, "failed to read file contents", NULL
);
243 Index
+= ReturnValue
;
246 if (strcmpi (argv
[Index
], "-o") == 0) {
247 OutputFileName
= argv
[Index
+ 1];
251 OutputFile
= fopen (OutputFileName
, "wb");
252 if (OutputFile
== NULL
) {
253 Error (NULL
, 0, 0, OutputFileName
, "failed to open output binary file");
260 // make sure section ends on a DWORD boundary ??
262 while ( (Size & 0x03) != 0 ) {
263 FileBuffer[Size] = 0;
267 Status
= SignSectionWithCrc32 (FileBuffer
, &BufferSize
, ContentSize
);
268 if (EFI_ERROR (Status
)) {
269 Error (NULL
, 0, 0, "failed to sign section", NULL
);
275 ContentSize
= fwrite (FileBuffer
, sizeof (UINT8
), BufferSize
, OutputFile
);
276 if (ContentSize
!= BufferSize
) {
277 Error (NULL
, 0, 0, "failed to write output buffer", NULL
);