From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by mx.groups.io with SMTP id smtpd.web11.6023.1593633685502234814 for ; Wed, 01 Jul 2020 13:01:25 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@nuviainc-com.20150623.gappssmtp.com header.s=20150623 header.b=KzUsw/ud; spf=pass (domain: nuviainc.com, ip: 209.85.221.68, mailfrom: leif@nuviainc.com) Received: by mail-wr1-f68.google.com with SMTP id s10so25366965wrw.12 for ; Wed, 01 Jul 2020 13:01:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nuviainc-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UKg9+JTBWD3qge3kUFnuysTJZqGjQ2yR1PoZ61xVunw=; b=KzUsw/ud1U9o+5khYQG7kXHNAFWEynqgbvEnHBSFmEGelj96+bhH4w9tlPVSQ153no wF5imzOZybvHeucSgyL1bJcK5gQCWLpV/F3fKZcs92OKyj6SVPY4aNXSNp8SjDWwPk7m U+rzpE3BvKE+yZInDQjrrFyFYmDEoIasJKX4Vf1rHWSjqKEMHVK59yPluDlOeUZv8uoi jQlZoqj/5rz20+fEloK/tb5SS5z+vhchkqwMeN9OQDA4/VGXDOPtES5gn+Z4KYAgjN1n hWGNhUQ0WTRCRIb+Dnrs72b5oBfBrNuT4KDDTa0bg6x5SKGm5QhwS+1YknSqhgBy/axO dnyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UKg9+JTBWD3qge3kUFnuysTJZqGjQ2yR1PoZ61xVunw=; b=MjJaKIv7ccDYT67H+yW9OD/ZDsO84jnYhJM+bie/7zcjm9qJ9w51pi8JPFCv/JETZs FIPD1H2N1Nu1qnv7BXCU6zhDj1jjoI8SBn2oIyJ0e//IvllnLmqC31Y/6nxe8W1/+NDA lgNIEc7Tg7q/hrJXcvoXFdepFm1MYFdxZH1VmXWl2YGq9/l1+NWZDSanYH6IMZz/xWHL 91TaONNiah4xVP7s2yeRgQTYTsqW8NUT5yuhxDROA6BFwXzaFQU/HEwi699Xe0bEpm/Q 81bKO5LZAEnv3FHQ7f1hk2qcf+Ea5N11mx4oTYzbYzdm83Nyo15alfAl8wHAFEE8ujWM c3sg== X-Gm-Message-State: AOAM533AzAUjWjRu4O9YBXsODAVmKPlaX9r6yIeqLQNtxQLyCn0hG5EN ssFMdY/vctjr4KzXLnzj3ub1ntHAiwfmUOsYsptHQ1o7R7/icpdz+skxGTrKF8mUuSnf8pjCAXD 0PBnreaaVzJ4fTyVprU8zMy4rYvPWdIUbkj8O0KXce5l6IydteCzghOQ+Rn/EU4Oemw== X-Google-Smtp-Source: ABdhPJwU8x/0xOcXIs2mi2KjYZ5/6/279HOy2s9azzSHo/carAaKN0sheQi2Zw5R5tTdBgSTifwukA== X-Received: by 2002:a05:6000:1c7:: with SMTP id t7mr28026184wrx.14.1593633683540; Wed, 01 Jul 2020 13:01:23 -0700 (PDT) Return-Path: Received: from vanye.hemma.eciton.net (cpc92302-cmbg19-2-0-cust304.5-4.cable.virginm.net. [82.1.209.49]) by smtp.gmail.com with ESMTPSA id s8sm8519067wru.38.2020.07.01.13.01.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jul 2020 13:01:23 -0700 (PDT) From: "Leif Lindholm" To: devel@edk2.groups.io Cc: Ard Biesheuvel , macarl@microsoft.com Subject: [RFC 3/5] EmbeddedPkg/PrePiLib: refactor IS_SECTION2() handling Date: Wed, 1 Jul 2020 21:01:16 +0100 Message-Id: <20200701200118.3972-4-leif@nuviainc.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200701200118.3972-1-leif@nuviainc.com> References: <20200701200118.3972-1-leif@nuviainc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit There are a bunch of IS_SECTION2() conditional statements in FfsProcessSection, really breaking up the readability. Add a set of static helper functions instead. Signed-off-by: Leif Lindholm --- EmbeddedPkg/Library/PrePiLib/FwVol.c | 101 ++++++++++++++++----------- 1 file changed, 61 insertions(+), 40 deletions(-) diff --git a/EmbeddedPkg/Library/PrePiLib/FwVol.c b/EmbeddedPkg/Library/PrePiLib/FwVol.c index fc40d8650be1..a0672c084471 100644 --- a/EmbeddedPkg/Library/PrePiLib/FwVol.c +++ b/EmbeddedPkg/Library/PrePiLib/FwVol.c @@ -266,6 +266,57 @@ FindFileEx ( return EFI_NOT_FOUND; } +STATIC +UINTN +FfsSectionHeaderSize ( + IN EFI_COMMON_SECTION_HEADER *Section + ) +{ + if (IS_SECTION2 (Section)) { + return sizeof (EFI_COMMON_SECTION_HEADER2); + } + + return sizeof (EFI_COMMON_SECTION_HEADER); +} + +STATIC +UINTN +FfsSectionLength ( + IN EFI_COMMON_SECTION_HEADER *Section + ) +{ + if (IS_SECTION2 (Section)) { + return SECTION2_SIZE (Section); + } + + return SECTION_SIZE (Section); +} + +STATIC +UINTN +FfsSectionCompressionType ( + IN EFI_COMMON_SECTION_HEADER *Section + ) +{ + if (IS_SECTION2 (Section)) { + return ((EFI_COMPRESSION_SECTION2 *)Section)->CompressionType; + } + + return ((EFI_COMPRESSION_SECTION *)Section)->CompressionType; +} + +STATIC +UINTN +FfsCompressionSectionHeaderSize ( + IN EFI_COMMON_SECTION_HEADER *Section + ) +{ + if (IS_SECTION2 (Section)) { + return sizeof (EFI_COMPRESSION_SECTION2); + } + + return sizeof (EFI_COMPRESSION_SECTION); +} /** Go through the file to search SectionType section, @@ -289,8 +340,6 @@ FfsProcessSection ( EFI_STATUS Status; UINT32 SectionLength; UINT32 ParsedLength; - EFI_COMPRESSION_SECTION *CompressionSection; - EFI_COMPRESSION_SECTION2 *CompressionSection2; UINT32 DstBufferSize; VOID *ScratchBuffer; UINT32 ScratchBufferSize; @@ -310,39 +359,22 @@ FfsProcessSection ( } if (Section->Type == SectionType) { - if (IS_SECTION2 (Section)) { - *OutputBuffer = (VOID *)((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER2)); - } else { - *OutputBuffer = (VOID *)((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER)); - } + *OutputBuffer = (VOID *)((UINT8 *)Section + FfsSectionHeaderSize (Section)); return EFI_SUCCESS; } if ((Section->Type == EFI_SECTION_COMPRESSION) || (Section->Type == EFI_SECTION_GUID_DEFINED)) { if (Section->Type == EFI_SECTION_COMPRESSION) { - if (IS_SECTION2 (Section)) { - CompressionSection2 = (EFI_COMPRESSION_SECTION2 *) Section; - SectionLength = SECTION2_SIZE (Section); + SectionLength = FfsSectionLength (Section); - if (CompressionSection2->CompressionType != EFI_STANDARD_COMPRESSION) { - return EFI_UNSUPPORTED; - } - - CompressedData = (CHAR8 *) ((EFI_COMPRESSION_SECTION2 *) Section + 1); - CompressedDataLength = (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION2); - } else { - CompressionSection = (EFI_COMPRESSION_SECTION *) Section; - SectionLength = SECTION_SIZE (Section); - - if (CompressionSection->CompressionType != EFI_STANDARD_COMPRESSION) { - return EFI_UNSUPPORTED; - } - - CompressedData = (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1); - CompressedDataLength = (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION); + if (FfsSectionCompressionType (Section) != EFI_STANDARD_COMPRESSION) { + return EFI_UNSUPPORTED; } + CompressedData = (VOID *)((UINTN)Section + FfsCompressionSectionHeaderSize (Section)); + CompressedDataLength = SectionLength - FfsCompressionSectionHeaderSize (Section); + Status = UefiDecompressGetInfo ( CompressedData, CompressedDataLength, @@ -383,19 +415,12 @@ FfsProcessSection ( // DstBuffer still is one section. Adjust DstBuffer offset, skip EFI section header // to make section data at page alignment. // - if (IS_SECTION2 (Section)) - DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - sizeof (EFI_COMMON_SECTION_HEADER2); - else - DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - sizeof (EFI_COMMON_SECTION_HEADER); + DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - FfsSectionHeaderSize (Section); // // Call decompress function // if (Section->Type == EFI_SECTION_COMPRESSION) { - if (IS_SECTION2 (Section)) { - CompressedData = (CHAR8 *) ((EFI_COMPRESSION_SECTION2 *) Section + 1); - } else { - CompressedData = (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1); - } + CompressedData = (VOID *)((UINTN)Section + FfsCompressionSectionHeaderSize (Section)); Status = UefiDecompress ( CompressedData, @@ -427,11 +452,7 @@ FfsProcessSection ( } } - if (IS_SECTION2 (Section)) { - SectionLength = SECTION2_SIZE (Section); - } else { - SectionLength = SECTION_SIZE (Section); - } + SectionLength = FfsSectionLength (Section); // // SectionLength is adjusted it is 4 byte aligned. // Go to the next section -- 2.20.1