Skip to content

Commit

Permalink
[202405] ImageValidation: Determine profile from inf MODULE_TYPE (#1124)
Browse files Browse the repository at this point in the history
## Description

In previous iterations, the profile was determined by parsing the
makefile, looking for MODULE_TYPE. As each OS / tool chain may use a
different makefile type, this was not a reliable method. This updates
the plugin to read the INF for the compiled efi file to determine the
MODULE_TYPE and thus the profile.

- [ ] Impacts functionality?
- [ ] Impacts security?
- [ ] Breaking change?
- [ ] Includes tests?
- [ ] Includes documentation?

## How This Was Tested

Validated on qemuq35 that the module type was successfully parsed.

## Integration Instructions

N/A
  • Loading branch information
Javagedes authored Aug 27, 2024
1 parent f481172 commit 0a17aa9
Showing 1 changed file with 11 additions and 41 deletions.
52 changes: 11 additions & 41 deletions .pytool/Plugin/ImageValidation/ImageValidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from edk2toollib.uefi.edk2.parsers.fdf_parser import FdfParser
from edk2toollib.uefi.edk2.parsers.dsc_parser import DscParser
from edk2toollib.uefi.edk2.parsers.inf_parser import InfParser
import yaml
from typing import List
import logging
Expand Down Expand Up @@ -192,25 +193,16 @@ def do_post_build(self, thebuilder):
# Perform Image Verification on any output efi's
# Grab profile from makefile
if "OUTPUT" in efi_path:
try:
if "VS" in tool_chain_tag:
profile = self._get_profile_from_makefile(
f'{Path(efi_path).parent.parent}/Makefile')

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

elif "CLANG" in tool_chain_tag:
profile = self._get_profile_from_makefile(
f'{Path(efi_path).parent.parent}/GNUmakefile')
else:
logging.warning("Unexpected TOOL_CHAIN_TAG... Cannot parse makefile. Using DEFAULT profile.")
profile = "DEFAULT"
except Exception:
logging.warning(f'Failed to parse makefile at [{Path(efi_path).parent.parent}/GNUmakefile]')
logging.warning('Using DEFAULT profile')
profile = "DEFAULT"
efi_inf = Path(efi_path).with_suffix('.inf')
if not efi_inf.is_file():
logging.warning(
f"Cannot find {os.path.basename(efi_inf)} for {os.path.basename(efi_path)}, Skipping...")
continue
infp = InfParser().SetEdk2Path(thebuilder.edk2path)
infp.ParseFile(efi_inf)
profile = infp.Dict.get("MODULE_TYPE", "DEFAULT")
if profile == "DEFAULT":
logging.debug("Failed to parse MODULE_TYPE from INF, using DEFAULT")

logging.debug(
f'Performing Image Verification ... {os.path.basename(efi_path)}')
Expand Down Expand Up @@ -249,28 +241,6 @@ def _validate_image(self, efi_path, profile="DEFAULT"):

return self.test_manager.run_tests(pe, profile)

# Reads the Makefile of an efi, if present, to determine profile
def _get_profile_from_makefile(self, makefile):
with open(makefile) as file:
for line in file.readlines():
if "MODULE_TYPE" in line:
line = line.split('=')
module_type = line[1]
module_type = module_type.strip()
return module_type
return "DEFAULT"

# Attempts to convert shorthand arch such as X64 to the
# Fully describe architecture. Additional support for
# Fallback architectures can be added here
def _try_convert_full_arch(self, arch):
full_arch = self.arch_dict.get(arch)
if full_arch is None:
if "ARM" in arch:
full_arch = "IMAGE_FILE_MACHINE_ARM"
# Add other Arches
return full_arch

# Resolves variable names matching the $(...) pattern.
def _resolve_vars(self, thebuilder, s):
var_pattern = re.compile(r'\$\([^)]*\)') # Detect $(...) pattern
Expand Down

0 comments on commit 0a17aa9

Please sign in to comment.