3 Copyright (c) 2004 - 2006, 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 "TianoCommon.h"
24 #include "EfiFirmwareFileSystem.h"
25 #include "EfiFirmwareVolumeHeader.h"
28 #include "EfiUtilityMsgs.h"
29 #include "GenCRC32Section.h"
34 #include "CommonLib.h"
36 #include EFI_PROTOCOL_DEFINITION (GuidedSectionExtraction)
38 #define TOOLVERSION "0.2"
40 #define UTILITY_NAME "GenCrc32Section"
42 EFI_GUID gEfiCrc32SectionGuid
= EFI_CRC32_GUIDED_SECTION_EXTRACTION_PROTOCOL_GUID
;
45 SignSectionWithCrc32 (
46 IN OUT UINT8
*FileBuffer
,
47 IN OUT UINT32
*BufferSize
,
54 Signs the section with CRC32 and add GUIDed section header for the
55 signed data. data stays in same location (overwrites source data).
59 FileBuffer - Buffer containing data to sign
61 BufferSize - On input, the size of FileBuffer. On output, the size of
62 actual section data (including added section header).
64 DataSize - Length of data to Sign
66 Key - Key to use when signing. Currently only CRC32 is supported.
70 EFI_SUCCESS - Successful
71 EFI_OUT_OF_RESOURCES - Not enough resource to complete the operation.
79 CRC32_SECTION_HEADER Crc32Header
;
91 Status
= CalculateCrc32 (FileBuffer
, DataSize
, &Crc32Checksum
);
92 if (EFI_ERROR (Status
)) {
96 TotalSize
= DataSize
+ CRC32_SECTION_HEADER_SIZE
;
97 Crc32Header
.GuidSectionHeader
.CommonHeader
.Type
= EFI_SECTION_GUID_DEFINED
;
98 Crc32Header
.GuidSectionHeader
.CommonHeader
.Size
[0] = (UINT8
) (TotalSize
& 0xff);
99 Crc32Header
.GuidSectionHeader
.CommonHeader
.Size
[1] = (UINT8
) ((TotalSize
& 0xff00) >> 8);
100 Crc32Header
.GuidSectionHeader
.CommonHeader
.Size
[2] = (UINT8
) ((TotalSize
& 0xff0000) >> 16);
101 memcpy (&(Crc32Header
.GuidSectionHeader
.SectionDefinitionGuid
), &gEfiCrc32SectionGuid
, sizeof (EFI_GUID
));
102 Crc32Header
.GuidSectionHeader
.Attributes
= EFI_GUIDED_SECTION_AUTH_STATUS_VALID
;
103 Crc32Header
.GuidSectionHeader
.DataOffset
= CRC32_SECTION_HEADER_SIZE
;
104 Crc32Header
.CRC32Checksum
= Crc32Checksum
;
106 SwapBuffer
= (UINT8
*) malloc (DataSize
);
107 if (SwapBuffer
== NULL
) {
108 return EFI_OUT_OF_RESOURCES
;
111 memcpy (SwapBuffer
, FileBuffer
, DataSize
);
112 memcpy (FileBuffer
, &Crc32Header
, CRC32_SECTION_HEADER_SIZE
);
113 memcpy (FileBuffer
+ CRC32_SECTION_HEADER_SIZE
, SwapBuffer
, DataSize
);
116 // Make sure section ends on a DWORD boundary
118 while ((TotalSize
& 0x03) != 0) {
119 FileBuffer
[TotalSize
] = 0;
123 *BufferSize
= TotalSize
;
125 if (SwapBuffer
!= NULL
) {
138 printf (UTILITY_NAME
" -i \"inputfile1\" \"inputfile2\" -o \"outputfile\" \n");
139 printf (" -i \"inputfile\":\n ");
140 printf (" specifies the input files that would be signed to CRC32 Guided section.\n");
141 printf (" -o \"outputfile\":\n");
142 printf (" specifies the output file that is a CRC32 Guided section.\n");
146 ReadFilesContentsIntoBuffer (
149 IN OUT UINT8
**FileBuffer
,
150 IN OUT UINT32
*BufferSize
,
151 OUT UINT32
*ContentSize
,
152 IN INT32 MaximumArguments
167 // read all input files into one file buffer
169 while (argv
[Start
+ Index
][0] != '-') {
171 FileName
= argv
[Start
+ Index
];
172 InputFile
= fopen (FileName
, "rb");
173 if (InputFile
== NULL
) {
174 Error (NULL
, 0, 0, FileName
, "failed to open input binary file");
178 fread (&Temp
, sizeof (UINT8
), 1, InputFile
);
179 while (!feof (InputFile
)) {
180 (*FileBuffer
)[Size
++] = Temp
;
181 fread (&Temp
, sizeof (UINT8
), 1, InputFile
);
188 // Make sure section ends on a DWORD boundary
190 while ((Size
& 0x03) != 0) {
191 (*FileBuffer
)[Size
] = 0;
196 if (Index
== MaximumArguments
) {
216 CHAR8
*OutputFileName
;
223 OutputFileName
= NULL
;
225 SetUtilityName (UTILITY_NAME
);
232 BufferSize
= 1024 * 1024 * 16;
233 FileBuffer
= (UINT8
*) malloc (BufferSize
* sizeof (UINT8
));
234 if (FileBuffer
== NULL
) {
235 Error (NULL
, 0, 0, "memory allocation failed", NULL
);
239 ZeroMem (FileBuffer
, BufferSize
);
241 for (Index
= 0; Index
< argc
; Index
++) {
242 if (_strcmpi (argv
[Index
], "-i") == 0) {
243 ReturnValue
= ReadFilesContentsIntoBuffer (
251 if (ReturnValue
== -1) {
252 Error (NULL
, 0, 0, "failed to read file contents", NULL
);
256 Index
+= ReturnValue
;
259 if (_strcmpi (argv
[Index
], "-o") == 0) {
260 OutputFileName
= argv
[Index
+ 1];
264 OutputFile
= fopen (OutputFileName
, "wb");
265 if (OutputFile
== NULL
) {
266 Error (NULL
, 0, 0, OutputFileName
, "failed to open output binary file");
273 // make sure section ends on a DWORD boundary ??
275 while ( (Size & 0x03) != 0 ) {
276 FileBuffer[Size] = 0;
280 Status
= SignSectionWithCrc32 (FileBuffer
, &BufferSize
, ContentSize
);
281 if (EFI_ERROR (Status
)) {
282 Error (NULL
, 0, 0, "failed to sign section", NULL
);
288 ContentSize
= fwrite (FileBuffer
, sizeof (UINT8
), BufferSize
, OutputFile
);
289 if (ContentSize
!= BufferSize
) {
290 Error (NULL
, 0, 0, "failed to write output buffer", NULL
);