Skip to content

Commit f14fff5

Browse files
xuweiintelmergify[bot]
authored andcommitted
StandaloneMmPkg/FvLib: Support large file with EFI_FFS_FILE_HEADER2.
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3769 Current FvLib will hit parse issue when encountering LARGE file, then ignore latter ffs/section, thus causing required drivers not being dispatched. Therefore, need to add support for EFI_FFS_FILE_HEADER2 and EFI_COMMON_SECTION_HEADER2 in FvLib to fix this issue. Signed-off-by: Wei6 Xu <wei6.xu@intel.com> Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
1 parent 38f6d78 commit f14fff5

File tree

1 file changed

+45
-20
lines changed
  • StandaloneMmPkg/Library/FvLib

1 file changed

+45
-20
lines changed

StandaloneMmPkg/Library/FvLib/FvLib.c

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @file
22
3-
Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
3+
Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>
44
Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
55
66
SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -63,18 +63,20 @@ CalculateHeaderChecksum (
6363
UINT8 *ptr;
6464
UINTN Index;
6565
UINT8 Sum;
66+
UINTN Size;
6667

67-
Sum = 0;
68-
ptr = (UINT8 *)FileHeader;
68+
Sum = 0;
69+
ptr = (UINT8 *)FileHeader;
70+
Size = IS_FFS_FILE2 (FileHeader) ? sizeof (EFI_FFS_FILE_HEADER2) : sizeof (EFI_FFS_FILE_HEADER);
6971

70-
for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {
72+
for (Index = 0; Index < Size - 3; Index += 4) {
7173
Sum = (UINT8)(Sum + ptr[Index]);
7274
Sum = (UINT8)(Sum + ptr[Index + 1]);
7375
Sum = (UINT8)(Sum + ptr[Index + 2]);
7476
Sum = (UINT8)(Sum + ptr[Index + 3]);
7577
}
7678

77-
for ( ; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {
79+
for ( ; Index < Size; Index++) {
7880
Sum = (UINT8)(Sum + ptr[Index]);
7981
}
8082

@@ -157,7 +159,8 @@ FfsFindNextFile (
157159
// Length is 24 bits wide so mask upper 8 bits
158160
// FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
159161
//
160-
FileLength = FFS_FILE_SIZE (*FileHeader);
162+
FileLength = IS_FFS_FILE2 (*FileHeader) ?
163+
FFS_FILE2_SIZE (*FileHeader) : FFS_FILE_SIZE (*FileHeader);
161164
FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
162165
FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);
163166
}
@@ -172,14 +175,21 @@ FfsFindNextFile (
172175

173176
switch (FileState) {
174177
case EFI_FILE_HEADER_INVALID:
175-
FileOffset += sizeof (EFI_FFS_FILE_HEADER);
176-
FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
178+
if (IS_FFS_FILE2 (FfsFileHeader)) {
179+
FileOffset += sizeof (EFI_FFS_FILE_HEADER2);
180+
FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));
181+
} else {
182+
FileOffset += sizeof (EFI_FFS_FILE_HEADER);
183+
FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
184+
}
185+
177186
break;
178187

179188
case EFI_FILE_DATA_VALID:
180189
case EFI_FILE_MARKED_FOR_UPDATE:
181190
if (CalculateHeaderChecksum (FfsFileHeader) == 0) {
182-
FileLength = FFS_FILE_SIZE (FfsFileHeader);
191+
FileLength = IS_FFS_FILE2 (FfsFileHeader) ?
192+
FFS_FILE2_SIZE (FfsFileHeader) : FFS_FILE_SIZE (FfsFileHeader);
183193
FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
184194

185195
if ((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) {
@@ -197,7 +207,8 @@ FfsFindNextFile (
197207
break;
198208

199209
case EFI_FILE_DELETED:
200-
FileLength = FFS_FILE_SIZE (FfsFileHeader);
210+
FileLength = IS_FFS_FILE2 (FfsFileHeader) ?
211+
FFS_FILE2_SIZE (FfsFileHeader) : FFS_FILE_SIZE (FfsFileHeader);
201212
FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
202213
FileOffset += FileOccupiedSize;
203214
FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
@@ -253,7 +264,7 @@ FindFfsSectionInSections (
253264

254265
Section = (EFI_COMMON_SECTION_HEADER *)(UINTN)CurrentAddress;
255266

256-
Size = SECTION_SIZE (Section);
267+
Size = IS_SECTION2 (Section) ? SECTION2_SIZE (Section) : SECTION_SIZE (Section);
257268
if (Size < sizeof (*Section)) {
258269
return EFI_VOLUME_CORRUPTED;
259270
}
@@ -306,9 +317,13 @@ FfsFindSection (
306317
// Does not include FfsFileHeader header size
307318
// FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
308319
//
309-
Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
310-
FileSize = FFS_FILE_SIZE (FfsFileHeader);
311-
FileSize -= sizeof (EFI_FFS_FILE_HEADER);
320+
if (IS_FFS_FILE2 (FfsFileHeader)) {
321+
Section = (EFI_COMMON_SECTION_HEADER *)((EFI_FFS_FILE_HEADER2 *)FfsFileHeader + 1);
322+
FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);
323+
} else {
324+
Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
325+
FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);
326+
}
312327

313328
Status = FindFfsSectionInSections (
314329
Section,
@@ -351,16 +366,26 @@ FfsFindSectionData (
351366
// Does not include FfsFileHeader header size
352367
// FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
353368
//
354-
Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
355-
FileSize = FFS_FILE_SIZE (FfsFileHeader);
356-
FileSize -= sizeof (EFI_FFS_FILE_HEADER);
369+
if (IS_FFS_FILE2 (FfsFileHeader)) {
370+
Section = (EFI_COMMON_SECTION_HEADER *)((EFI_FFS_FILE_HEADER2 *)FfsFileHeader + 1);
371+
FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);
372+
} else {
373+
Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
374+
FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);
375+
}
357376

358377
*SectionData = NULL;
359378
ParsedLength = 0;
360379
while (ParsedLength < FileSize) {
361380
if (Section->Type == SectionType) {
362-
*SectionData = (VOID *)(Section + 1);
363-
*SectionDataSize = SECTION_SIZE (Section);
381+
if (IS_SECTION2 (Section)) {
382+
*SectionData = (VOID *)((EFI_COMMON_SECTION_HEADER2 *)Section + 1);
383+
*SectionDataSize = SECTION2_SIZE (Section);
384+
} else {
385+
*SectionData = (VOID *)(Section + 1);
386+
*SectionDataSize = SECTION_SIZE (Section);
387+
}
388+
364389
return EFI_SUCCESS;
365390
}
366391

@@ -369,7 +394,7 @@ FfsFindSectionData (
369394
// SectionLength is adjusted it is 4 byte aligned.
370395
// Go to the next section
371396
//
372-
SectionLength = SECTION_SIZE (Section);
397+
SectionLength = IS_SECTION2 (Section) ? SECTION2_SIZE (Section) : SECTION_SIZE (Section);
373398
SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
374399

375400
ParsedLength += SectionLength;

0 commit comments

Comments
 (0)