Skip to content

Commit 6b19b44

Browse files
leiflindholmmergify[bot]
authored andcommitted
EmbeddedPkg/PrePiLib: refactor FfsProcessSection ()
Create some helper functions to make FfsProcessSection at least halfway readable by both humans and compilers. Signed-off-by: Leif Lindholm <[email protected]>
1 parent 4a1dca5 commit 6b19b44

File tree

1 file changed

+122
-60
lines changed
  • EmbeddedPkg/Library/PrePiLib

1 file changed

+122
-60
lines changed

EmbeddedPkg/Library/PrePiLib/FwVol.c

Lines changed: 122 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,95 @@ FindFileEx (
260260
return EFI_NOT_FOUND;
261261
}
262262

263+
/**
264+
Return the size of a section whether SECTION or SECTION2
265+
266+
@param Section - Pointer to start of section header
267+
268+
@retval Size in bytes.
269+
**/
270+
STATIC
271+
UINT32
272+
GetSectionNSize (
273+
IN EFI_COMMON_SECTION_HEADER *Section
274+
)
275+
{
276+
UINT32 SectionSize;
277+
278+
if (IS_SECTION2 (Section)) {
279+
ASSERT (SECTION2_SIZE (Section) > 0x00FFFFFF);
280+
SectionSize = SECTION2_SIZE (Section);
281+
} else {
282+
SectionSize = SECTION_SIZE (Section);
283+
}
284+
285+
return SectionSize;
286+
}
287+
288+
/**
289+
Return the size of a section header whether SECTION or SECTION2
290+
291+
@param Section - Pointer to start of section header
292+
293+
@retval Size in bytes.
294+
**/
295+
STATIC
296+
UINT32
297+
GetCommonSectionNHeaderSize (
298+
IN EFI_COMMON_SECTION_HEADER *Section
299+
)
300+
{
301+
if (IS_SECTION2 (Section)) {
302+
return sizeof (EFI_COMMON_SECTION_HEADER2);
303+
} else {
304+
return sizeof (EFI_COMMON_SECTION_HEADER);
305+
}
306+
}
307+
308+
/**
309+
Return the size of a compression section header whether SECTION or SECTION2
310+
311+
@param Section - Pointer to start of section header
312+
313+
@retval Size in bytes.
314+
**/
315+
STATIC
316+
UINT32
317+
GetCompressionSectionNHeaderSize (
318+
IN EFI_COMMON_SECTION_HEADER *Section
319+
)
320+
{
321+
ASSERT (Section->Type == EFI_SECTION_COMPRESSION);
322+
323+
if (IS_SECTION2 (Section)) {
324+
return sizeof (EFI_COMPRESSION_SECTION2);
325+
} else {
326+
return sizeof (EFI_COMPRESSION_SECTION);
327+
}
328+
}
329+
330+
/**
331+
Return the compression type of a section whether SECTION or SECTION2
332+
333+
@param Section - Pointer to start of section header
334+
335+
@retval EFI_SECTION_TYPE
336+
**/
337+
STATIC
338+
EFI_SECTION_TYPE
339+
GetSectionNCompressionType (
340+
IN EFI_COMMON_SECTION_HEADER *Section
341+
)
342+
{
343+
ASSERT (Section->Type == EFI_SECTION_COMPRESSION);
344+
345+
if (IS_SECTION2 (Section)) {
346+
return ((EFI_COMPRESSION_SECTION2 *)Section)->CompressionType;
347+
} else {
348+
return ((EFI_COMPRESSION_SECTION *)Section)->CompressionType;
349+
}
350+
}
351+
263352
/**
264353
Go through the file to search SectionType section,
265354
when meeting an encapsuled section.
@@ -281,29 +370,18 @@ FfsProcessSection (
281370
OUT VOID **OutputBuffer
282371
)
283372
{
284-
EFI_STATUS Status;
285-
UINT32 SectionLength;
286-
UINTN ParsedLength;
287-
EFI_COMPRESSION_SECTION *CompressionSection;
288-
EFI_COMPRESSION_SECTION2 *CompressionSection2;
289-
UINT32 DstBufferSize;
290-
VOID *ScratchBuffer;
291-
UINT32 ScratchBufferSize;
292-
VOID *DstBuffer;
293-
UINT16 SectionAttribute;
294-
UINT32 AuthenticationStatus;
295-
CHAR8 *CompressedData;
296-
UINT32 CompressedDataLength;
297-
BOOLEAN Found;
298-
299-
Found = FALSE;
300-
*OutputBuffer = NULL;
301-
ParsedLength = 0;
302-
Status = EFI_NOT_FOUND;
373+
EFI_STATUS Status;
374+
UINT32 SectionLength;
375+
UINTN ParsedLength;
376+
UINT32 DstBufferSize;
377+
VOID *DstBuffer;
378+
379+
ParsedLength = 0;
380+
303381
while (ParsedLength < SectionSize) {
304-
if (IS_SECTION2 (Section)) {
305-
ASSERT (SECTION2_SIZE (Section) > 0x00FFFFFF);
306-
}
382+
UINT32 SectionHeaderSize;
383+
384+
SectionHeaderSize = GetCommonSectionNHeaderSize (Section);
307385

308386
if (Section->Type == SectionType) {
309387
if (SectionCheckHook != NULL) {
@@ -313,38 +391,29 @@ FfsProcessSection (
313391
}
314392

315393
if (Found) {
316-
if (IS_SECTION2 (Section)) {
317-
*OutputBuffer = (VOID *)((UINT8 *)Section + sizeof (EFI_COMMON_SECTION_HEADER2));
318-
} else {
319-
*OutputBuffer = (VOID *)((UINT8 *)Section + sizeof (EFI_COMMON_SECTION_HEADER));
320-
}
394+
*OutputBuffer = (VOID *)((UINT8 *)Section + SectionHeaderSize);
321395

322396
return EFI_SUCCESS;
323397
} else {
324398
goto CheckNextSection;
325399
}
326400
} else if ((Section->Type == EFI_SECTION_COMPRESSION) || (Section->Type == EFI_SECTION_GUID_DEFINED)) {
327-
if (Section->Type == EFI_SECTION_COMPRESSION) {
328-
if (IS_SECTION2 (Section)) {
329-
CompressionSection2 = (EFI_COMPRESSION_SECTION2 *)Section;
330-
SectionLength = SECTION2_SIZE (Section);
401+
CHAR8 *CompressedData;
402+
UINT32 CompressionSectionHeaderSize;
403+
VOID *ScratchBuffer;
404+
UINT32 ScratchBufferSize;
331405

332-
if (CompressionSection2->CompressionType != EFI_STANDARD_COMPRESSION) {
333-
return EFI_UNSUPPORTED;
334-
}
406+
if (Section->Type == EFI_SECTION_COMPRESSION) {
407+
UINT32 CompressedDataLength;
335408

336-
CompressedData = (CHAR8 *)((EFI_COMPRESSION_SECTION2 *)Section + 1);
337-
CompressedDataLength = SectionLength - sizeof (EFI_COMPRESSION_SECTION2);
338-
} else {
339-
CompressionSection = (EFI_COMPRESSION_SECTION *)Section;
340-
SectionLength = SECTION_SIZE (Section);
409+
CompressionSectionHeaderSize = GetCompressionSectionNHeaderSize (Section);
410+
SectionLength = GetSectionNSize (Section);
341411

342-
if (CompressionSection->CompressionType != EFI_STANDARD_COMPRESSION) {
343-
return EFI_UNSUPPORTED;
344-
}
412+
CompressedData = (CHAR8 *)((UINTN)(Section) + CompressionSectionHeaderSize);
413+
CompressedDataLength = SectionLength - CompressionSectionHeaderSize;
345414

346-
CompressedData = (CHAR8 *)((EFI_COMPRESSION_SECTION *)Section + 1);
347-
CompressedDataLength = SectionLength - sizeof (EFI_COMPRESSION_SECTION);
415+
if (GetSectionNCompressionType (Section) != EFI_STANDARD_COMPRESSION) {
416+
return EFI_UNSUPPORTED;
348417
}
349418

350419
Status = UefiDecompressGetInfo (
@@ -353,7 +422,10 @@ FfsProcessSection (
353422
&DstBufferSize,
354423
&ScratchBufferSize
355424
);
356-
} else if (Section->Type == EFI_SECTION_GUID_DEFINED) {
425+
} else {
426+
// Section->Type == EFI_SECTION_GUID_DEFINED)
427+
UINT16 SectionAttribute;
428+
357429
Status = ExtractGuidedSectionGetInfo (
358430
Section,
359431
&DstBufferSize,
@@ -390,28 +462,22 @@ FfsProcessSection (
390462
// DstBuffer still is one section. Adjust DstBuffer offset, skip EFI section header
391463
// to make section data at page alignment.
392464
//
393-
if (IS_SECTION2 (Section)) {
394-
DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - sizeof (EFI_COMMON_SECTION_HEADER2);
395-
} else {
396-
DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - sizeof (EFI_COMMON_SECTION_HEADER);
397-
}
465+
DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - SectionHeaderSize;
398466

399467
//
400468
// Call decompress function
401469
//
402470
if (Section->Type == EFI_SECTION_COMPRESSION) {
403-
if (IS_SECTION2 (Section)) {
404-
CompressedData = (CHAR8 *)((EFI_COMPRESSION_SECTION2 *)Section + 1);
405-
} else {
406-
CompressedData = (CHAR8 *)((EFI_COMPRESSION_SECTION *)Section + 1);
407-
}
471+
CompressedData = (CHAR8 *)((UINTN)(Section) + CompressionSectionHeaderSize);
408472

409473
Status = UefiDecompress (
410474
CompressedData,
411475
DstBuffer,
412476
ScratchBuffer
413477
);
414478
} else if (Section->Type == EFI_SECTION_GUID_DEFINED) {
479+
UINT32 AuthenticationStatus;
480+
415481
Status = ExtractGuidedSectionDecode (
416482
Section,
417483
&DstBuffer,
@@ -438,11 +504,7 @@ FfsProcessSection (
438504
}
439505

440506
CheckNextSection:
441-
if (IS_SECTION2 (Section)) {
442-
SectionLength = SECTION2_SIZE (Section);
443-
} else {
444-
SectionLength = SECTION_SIZE (Section);
445-
}
507+
SectionLength = GetSectionNSize (Section);
446508

447509
//
448510
// SectionLength is adjusted it is 4 byte aligned.

0 commit comments

Comments
 (0)