def test_section_guided():
    """Check that SECTION GUIDED can be added."""
    # Given
    # cSpell:ignore MAINFV
    SAMPLE_FDF_FILE = textwrap.dedent("""\
        [FV.MAINFV]
        FILE PEIM = aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa{
            SECTION GUIDED bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb
        }
    """)
    try:
        with tempfile.NamedTemporaryFile(mode='w',
                                         encoding='utf-8',
                                         delete=False) as f:
            f.write(SAMPLE_FDF_FILE)
            fdf_path = f.name

        # When
        parser = FdfParser()
        parser.ParseFile(fdf_path)
    finally:
        os.remove(fdf_path)

    # Then
    assert "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb" in \
        parser.FVs["MAINFV"]["Files"]["aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"]
Ejemplo n.º 2
0
    def ParseFdfFile(self):
        if (self.env.GetValue("FLASH_DEFINITION") is None):
            logging.debug("No flash definition set")
            return 0
        if (os.path.isfile(
                self.mws.join(self.ws,
                              self.env.GetValue("FLASH_DEFINITION")))):
            # parse the FDF file- fdf files have similar syntax to DSC and therefore parser works for both.
            logging.debug("Parse Active Flash Definition (FDF) file")

            # Get the vars from the environment that are not build keys
            input_vars = self.env.GetAllNonBuildKeyValues()
            # Update with special environment set build keys
            input_vars.update(self.env.GetAllBuildKeyValues())

            fdf_parser = FdfParser().SetBaseAbsPath(self.ws).SetPackagePaths(
                self.pp.split(os.pathsep)).SetInputVars(input_vars)
            pa = self.mws.join(self.ws, self.env.GetValue("FLASH_DEFINITION"))
            fdf_parser.ParseFile(pa)
            for key, value in fdf_parser.LocalVars.items():
                self.env.SetValue(key, value, "From Platform FDF File", True)

        else:
            logging.error("Failed to find FDF file")
            return -2

        return 0
Ejemplo n.º 3
0
    def test_included_defines(self):
        test_fdf = os.path.join(TEST_PATH, 'IncludedDefinesParent.fdf')
        parser = FdfParser().SetBaseAbsPath(TEST_PATH)
        parser.ParseFile(test_fdf)

        # Make sure that we can read local variables out of the file.
        self.assertEqual(parser.Dict['FD_BASE'], '0x00800000')
        self.assertEqual(parser.Dict['EXTRA_BLOCK_SIZE'], '0x00001000')
        self.assertFalse("AM_I_YOU" in parser.Dict)
Ejemplo n.º 4
0
    def test_primary_conditional_defines(self):
        test_fdf = os.path.join(TEST_PATH, 'SimpleDefines.fdf')
        print(test_fdf)
        parser = FdfParser().SetBaseAbsPath(TEST_PATH).SetInputVars({"TARGET": "TEST2"})
        parser.ParseFile(test_fdf)

        # Make sure that we can read local variables out of the file.
        self.assertEqual(parser.Dict['FD_BASE'], '0x00800000')
        self.assertEqual(parser.Dict['NUM_BLOCKS'], '0x850')
        self.assertTrue("EXTRA_DEF" in parser.Dict)
Ejemplo n.º 5
0
    def test_primary_defines(self):
        test_fdf = os.path.join(TEST_PATH, 'SimpleDefines.fdf')
        print(test_fdf)
        parser = FdfParser().SetBaseAbsPath(TEST_PATH)
        parser.ParseFile(test_fdf)

        # Make sure that we can read local variables out of the file.
        self.assertEqual(parser.Dict['FD_BASE'], '0x00800000')
        self.assertEqual(parser.Dict['NUM_BLOCKS'], '0x410')
        self.assertFalse("EXTRA_DEF" in parser.Dict)
Ejemplo n.º 6
0
    def test_conditionally_included_defines(self):
        test_fdf = os.path.join(TEST_PATH, 'IncludedDefinesParent.fdf')
        parser = FdfParser().SetBaseAbsPath(TEST_PATH)
        parser = FdfParser().SetBaseAbsPath(TEST_PATH).SetInputVars({"TARGET": "TEST5"})
        parser.ParseFile(test_fdf)

        # Make sure that we can read local variables out of the file.
        self.assertEqual(parser.Dict['FD_BASE'], '0x00800000')
        self.assertEqual(parser.Dict['INTERNAL_VALUE'], '104')
        self.assertEqual(parser.Dict['NUM_BLOCKS'], '0x410')
        self.assertEqual(parser.Dict['CONDITIONAL_VALUE'], '121')
Ejemplo n.º 7
0
    def test_included_conditional_defines(self):
        test_fdf = os.path.join(TEST_PATH, 'IncludedDefinesParent.fdf')
        parser = FdfParser().SetBaseAbsPath(TEST_PATH)
        parser = FdfParser().SetBaseAbsPath(TEST_PATH).SetInputVars({"TARGET": "TEST4"})
        parser.ParseFile(test_fdf)

        # Make sure that we can read local variables out of the file.
        self.assertEqual(parser.Dict['FD_BASE'], '0x00800000')
        self.assertEqual(parser.Dict['EXTRA_BLOCK_SIZE'], '0x00001000')
        self.assertEqual(parser.Dict['NUM_BLOCKS'], '0x410')
        self.assertTrue("AM_I_YOU" in parser.Dict)
        self.assertFalse("CONDITIONAL_VALUE" in parser.Dict)
Ejemplo n.º 8
0
    def do_pre_build(self, thebuilder):
        try:
            error_count = 0
            '''
            # this scans the whole build directory for bmp's
            bmp_search_path = os.path.join(thebuilder.ws,"**","*.bmp");
            for found_item in glob.iglob(bmp_search_path, recursive=True):
              if CheckBmp(found_item):
                logging.error("{} failed image check".format(found_item))
                error_count += 1
            return error_count
            '''

            fp = FdfParser()
            dp = DscParser()

            ws = thebuilder.ws
            pp = thebuilder.pp.split(";")
            edk2 = Edk2Path(ws, pp)

            ActiveDsc = edk2.GetAbsolutePathOnThisSystemFromEdk2RelativePath(
                thebuilder.env.GetValue("ACTIVE_PLATFORM"))
            ActiveFdf = edk2.GetAbsolutePathOnThisSystemFromEdk2RelativePath(
                thebuilder.env.GetValue("FLASH_DEFINITION"))

            if ActiveFdf is None:
                self.logger.info("No FDF found- BMP check skipped")
                return 0
            # parse the DSC and the FDF
            dp.SetBaseAbsPath(ws).SetPackagePaths(pp)
            dp.SetInputVars(thebuilder.env.GetAllBuildKeyValues()).ParseFile(
                ActiveDsc)  # parse the DSC for build vars
            fp.SetBaseAbsPath(ws).SetPackagePaths(pp)
            fp.SetInputVars(dp.LocalVars).ParseFile(
                ActiveFdf)  # give FDF parser the vars from DSC

            # for each FV section in the DSC
            for FV_name in fp.FVs:
                FV_files = fp.FVs[FV_name]["Files"]
                # now look for images in each file of this FV
                for fv_file_name in FV_files:
                    fv_file = FV_files[fv_file_name]
                    if fv_file["type"].upper() != 'FREEFORM':
                        continue
                    fv_file_raw = fv_file['RAW']
                    fv_file_raw_list = []
                    if isinstance(fv_file_raw, list):
                        fv_file_raw_list = fv_file_raw
                    else:
                        fv_file_raw_list.append(fv_file_raw)
                    # for each file that is RAW type
                    for fv_file_raw_item in fv_file_raw_list:
                        # check if it ends with a bmp
                        if fv_file_raw_item.lower().endswith(".bmp"):
                            logging.debug(fv_file_raw_item)
                            BmpFilePath = edk2.GetAbsolutePathOnThisSystemFromEdk2RelativePath(
                                fv_file_raw_item)
                            logging.debug(BmpFilePath)
                            if BmpCheckPlugin.CheckBmp(
                                    BmpFilePath):  # do the check
                                self.logger.error(
                                    "{} failed image check".format(
                                        fv_file_raw_item))
                                error_count += 1
            return error_count
        except:
            self.logger.warning(
                "Unable to read the FDF. Please update your Edk2-Pytools-* Packages"
            )
            return 0
Ejemplo n.º 9
0
    def do_post_build(self, thebuilder):

        starttime = datetime.now()
        logging.info(
            "---------------------------------------------------------")
        logging.info(
            "-----------Postbuild Image Validation Starting-----------")
        logging.info(
            "---------------------------------------------------------")

        # Load Configuration Data
        config_path = thebuilder.env.GetValue("PE_VALIDATION_PATH", None)
        tool_chain_tag = thebuilder.env.GetValue("TOOL_CHAIN_TAG")
        if config_path is None:
            logging.info(
                "PE_VALIDATION_PATH not set, PE Image Validation Skipped")
            return 0  # Path not set, Plugin skipped

        if not os.path.isfile(config_path):
            logging.error("Invalid PE_VALIDATION_PATH. File not Found")
            return 1

        with open(config_path) as jsonfile:
            config_data = json.load(jsonfile)

        self.test_manager.config_data = config_data
        self.config_data = config_data
        self.ignore_list = config_data["IGNORE_LIST"]
        self.arch_dict = config_data["TARGET_ARCH"]

        count = 0

        # Start Pre-Compiled Image Verification
        fdf_parser = FdfParser()
        dsc_parser = DscParser()

        ws = thebuilder.ws
        pp = thebuilder.pp.split(os.pathsep)
        edk2 = Edk2Path(ws, pp)

        ActiveDsc = edk2.GetAbsolutePathOnThisSystemFromEdk2RelativePath(
            thebuilder.env.GetValue("ACTIVE_PLATFORM"))
        ActiveFdf = edk2.GetAbsolutePathOnThisSystemFromEdk2RelativePath(
            thebuilder.env.GetValue("FLASH_DEFINITION"))

        if ActiveFdf is None:
            logging.info("No FDF found - PE Image Validation skipped")
            return 0

        # parse the DSC and the FDF
        dsc_parser.SetBaseAbsPath(ws).SetPackagePaths(pp)
        dsc_parser.SetInputVars(
            thebuilder.env.GetAllBuildKeyValues()).ParseFile(
                ActiveDsc)  # parse the DSC for build vars
        fdf_parser.SetBaseAbsPath(ws).SetPackagePaths(pp)
        fdf_parser.SetInputVars(dsc_parser.LocalVars).ParseFile(
            ActiveFdf)  # give FDF parser the vars from DSC

        # Test all pre-compiled efis described in the fdf
        result = Result.PASS
        for FV_name in fdf_parser.FVs:  # Get all Firmware volumes
            FV_files = fdf_parser.FVs[FV_name]["Files"]
            for fv_file_name in FV_files:  # Iterate over each file in the firmware volume
                fv_file = FV_files[fv_file_name]
                if "PE32" in fv_file:  # Any PE32 section in the FV contains a path to the efi
                    # could have multiple PE32 sections
                    for efi_path in fv_file["PE32"]:
                        efi_path = self._resolve_vars(thebuilder, efi_path)
                        efi_path = edk2.GetAbsolutePathOnThisSystemFromEdk2RelativePath(
                            efi_path)
                        if efi_path == None:
                            logging.warn(
                                "Unable to parse the path to the pre-compiled efi"
                            )
                            continue
                        if os.path.basename(efi_path) in self.ignore_list:
                            continue
                        logging.info(
                            f'Performing Image Verification ... {os.path.basename(efi_path)}'
                        )
                        if self._validate_image(
                                efi_path, fv_file["type"]) == Result.FAIL:
                            result = Result.FAIL
                        count += 1
        # End Pre-Compiled Image Verification

        # Start Build Time Compiled Image Verification
        result = Result.PASS
        for arch in thebuilder.env.GetValue("TARGET_ARCH").split():
            efi_path_list = self._walk_directory_for_extension(
                ['.efi'],
                f'{thebuilder.env.GetValue("BUILD_OUTPUT_BASE")}/{arch}')

            for efi_path in efi_path_list:
                if os.path.basename(efi_path) in self.ignore_list:
                    continue

                # Perform Image Verification on any output efi's
                # Grab profile from makefile
                if efi_path.__contains__("OUTPUT"):
                    try:
                        if tool_chain_tag.__contains__("VS"):
                            profile = self._get_profile_from_makefile(
                                f'{Path(efi_path).parent.parent}/Makefile')

                        elif tool_chain_tag.__contains__("GCC"):
                            profile = self._get_profile_from_makefile(
                                f'{Path(efi_path).parent.parent}/GNUmakefile')

                        elif tool_chain_tag.__contains__("CLANG"):
                            profile = self._get_profile_from_makefile(
                                f'{Path(efi_path).parent.parent}/GNUmakefile')
                        else:
                            logging.warn(
                                "Unexpected TOOL_CHAIN_TAG... Cannot parse makefile. Using DEFAULT profile."
                            )
                            profile = "DEFAULT"
                    except:
                        logging.warn(
                            f'Failed to parse makefile at [{Path(efi_path).parent.parent}/GNUmakefile]'
                        )
                        logging.warn(f'Using DEFAULT profile')
                        profile = "DEFAULT"

                    logging.info(
                        f'Performing Image Verification ... {os.path.basename(efi_path)}'
                    )
                    if self._validate_image(efi_path, profile) == Result.FAIL:
                        result = Result.FAIL
                    count += 1
        # End Built Time Compiled Image Verification

        endtime = datetime.now()
        delta = endtime - starttime
        logging.info(
            "---------------------------------------------------------")
        logging.info(
            "-----------Postbuild Image Validation Finished-----------")
        logging.info(
            "------------------{:04d} Images Verified-------------------".
            format(count))
        logging.info(
            "-------------- Running Time (mm:ss): {0[0]:02}:{0[1]:02} --------------"
            .format(divmod(delta.seconds, 60)))
        logging.info(
            "---------------------------------------------------------")

        if result == Result.FAIL:
            return 1
        else:
            return 0