From 4166d3ffd3277f671a70924fcc0b7a0735c1b7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20Ha=CC=88user?= <8659494+mhaeuser@users.noreply.github.com> Date: Sun, 11 Jun 2023 22:15:52 +0200 Subject: [PATCH] ImageTool: Support XIP configuration --- BaseTools/Conf/build_rule.template | 10 +++++----- BaseTools/ImageTool/ImageTool.c | 9 ++++++++- BaseTools/ImageTool/ImageTool.h | 1 + BaseTools/ImageTool/ImageToolEmit.c | 3 ++- BaseTools/ImageTool/ImageToolEmit.h | 1 + BaseTools/ImageTool/PeEmit.c | 9 +++++---- BaseTools/ImageTool/PeEmitCommon.c | 5 +++-- BaseTools/ImageTool/PeEmitCommon.h | 6 ++++-- BaseTools/Source/C/GenFv/GenFvInternalLib.c | 1 + BaseTools/Source/C/VolInfo/VolInfo.c | 1 + BaseTools/Source/Python/GenFds/DataSection.py | 1 + BaseTools/Source/Python/GenFds/EfiSection.py | 1 + BaseTools/Source/Python/GenFds/FfsInfStatement.py | 2 ++ BaseTools/Source/Python/GenFds/GenFdsGlobalVariable.py | 4 +++- BaseTools/Source/Python/build/BuildReport.py | 2 +- BaseTools/Source/Python/build/build.py | 8 ++++---- 16 files changed, 43 insertions(+), 21 deletions(-) diff --git a/BaseTools/Conf/build_rule.template b/BaseTools/Conf/build_rule.template index 4ab280a3d6..83f4c1fbc2 100755 --- a/BaseTools/Conf/build_rule.template +++ b/BaseTools/Conf/build_rule.template @@ -134,8 +134,8 @@ "$(CC)" $(DEPS_FLAGS) $(CC_FLAGS) -o ${dst} $(INC) ${src} -## Early stages on ARM/AArch64 execute with strict alignment enabled and will crash, -# when compiler generates misaligned access. +## Early stages on ARM/AArch64 execute with strict alignment enabled and will crash, +# when compiler generates misaligned access. # [C-Code-File.BASE.AARCH64,C-Code-File.SEC.AARCH64,C-Code-File.PEI_CORE.AARCH64,C-Code-File.PEIM.AARCH64,C-Code-File.BASE.ARM,C-Code-File.SEC.ARM,C-Code-File.PEI_CORE.ARM,C-Code-File.PEIM.ARM] @@ -352,7 +352,7 @@ $(OUTPUT_DIR)(+)$(MODULE_NAME).map - ImageTool GenImage -c PE -t $(MODULE_TYPE) -o ${dst} ${src} + ImageTool GenImage -c PE -x -t $(MODULE_TYPE) -o ${dst} ${src} $(CP) ${dst} $(DEBUG_DIR) $(CP) ${dst} $(BIN_DIR)(+)$(MODULE_NAME_GUID).efi -$(CP) $(DEBUG_DIR)(+)*.map $(OUTPUT_DIR) @@ -362,7 +362,7 @@ $(CP) ${src} $(DEBUG_DIR)(+)$(MODULE_NAME).strip $(OBJCOPY) $(OBJCOPY_STRIPFLAG) $(DEBUG_DIR)(+)$(MODULE_NAME).strip - ImageTool GenImage -c PE -t $(MODULE_TYPE) -o ${dst} $(DEBUG_DIR)(+)$(MODULE_NAME).strip + ImageTool GenImage -c PE -x -t $(MODULE_TYPE) -o ${dst} $(DEBUG_DIR)(+)$(MODULE_NAME).strip $(CP) ${dst} $(DEBUG_DIR) $(CP) ${dst} $(BIN_DIR)(+)$(MODULE_NAME_GUID).efi @@ -373,7 +373,7 @@ "$(MTOC)" -subsystem $(MODULE_TYPE) $(MTOC_FLAGS) ${src} $(DEBUG_DIR)(+)$(MODULE_NAME).pecoff # create symbol file for GDB debug -$(DSYMUTIL) ${src} - ImageTool GenImage -c PE -t $(MODULE_TYPE) -o ${dst} $(DEBUG_DIR)(+)$(MODULE_NAME).pecoff + ImageTool GenImage -c PE -x -t $(MODULE_TYPE) -o ${dst} $(DEBUG_DIR)(+)$(MODULE_NAME).pecoff $(CP) ${dst} $(DEBUG_DIR) $(CP) ${dst} $(BIN_DIR)(+)$(MODULE_NAME_GUID).efi -$(CP) $(DEBUG_DIR)(+)*.map $(OUTPUT_DIR) diff --git a/BaseTools/ImageTool/ImageTool.c b/BaseTools/ImageTool/ImageTool.c index 4a39ab3cab..4e954f0257 100644 --- a/BaseTools/ImageTool/ImageTool.c +++ b/BaseTools/ImageTool/ImageTool.c @@ -356,6 +356,7 @@ GenExecutable ( IN const char *FormatName, IN const char *TypeName, IN const char *BaseAddress, + IN bool Xip, IN bool Strip, IN bool FixedAddress ) @@ -411,6 +412,7 @@ GenExecutable ( BaseAddress != NULL, NewBaseAddress, InputFileName, + Xip, Strip, FixedAddress ); @@ -435,6 +437,7 @@ int main (int argc, const char *argv[]) const char *FormatName; const char *TypeName; const char *BaseAddress; + bool Xip; bool Strip; bool FixedAddress; int ArgIndex; @@ -452,7 +455,7 @@ int main (int argc, const char *argv[]) if (strcmp (argv[1], "GenImage") == 0) { if (argc < 5) { fprintf (stderr, "ImageTool: Command arguments are missing\n"); - fprintf (stderr, " Usage: ImageTool GenImage [-c Format] [-t ModuleType] [-b BaseAddress] [-s] [-f] -o OutputFile InputFile\n"); + fprintf (stderr, " Usage: ImageTool GenImage [-c Format] [-t ModuleType] [-b BaseAddress] [-x] [-s] [-f] -o OutputFile InputFile\n"); raise (); return -1; } @@ -462,6 +465,7 @@ int main (int argc, const char *argv[]) FormatName = NULL; TypeName = NULL; BaseAddress = NULL; + Xip = false; Strip = false; FixedAddress = false; for (ArgIndex = 2; ArgIndex < argc; ++ArgIndex) { @@ -497,6 +501,8 @@ int main (int argc, const char *argv[]) } BaseAddress = argv[ArgIndex]; + } else if (strcmp (argv[ArgIndex], "-x") == 0) { + Xip = true; } else if (strcmp (argv[ArgIndex], "-s") == 0) { Strip = true; } else if (strcmp (argv[ArgIndex], "-f") == 0) { @@ -527,6 +533,7 @@ int main (int argc, const char *argv[]) FormatName, TypeName, BaseAddress, + Xip, Strip, FixedAddress ); diff --git a/BaseTools/ImageTool/ImageTool.h b/BaseTools/ImageTool/ImageTool.h index 99da2b29e8..060dac9830 100644 --- a/BaseTools/ImageTool/ImageTool.h +++ b/BaseTools/ImageTool/ImageTool.h @@ -152,6 +152,7 @@ void * ToolImageEmitPe ( image_tool_image_info_t *Image, uint32_t *FileSize, + bool Xip, bool Strip ); diff --git a/BaseTools/ImageTool/ImageToolEmit.c b/BaseTools/ImageTool/ImageToolEmit.c index 459b217ac2..d8525d6505 100644 --- a/BaseTools/ImageTool/ImageToolEmit.c +++ b/BaseTools/ImageTool/ImageToolEmit.c @@ -94,6 +94,7 @@ ToolImageEmit ( IN bool Relocate, IN uint64_t BaseAddress, IN const char *SymbolsPath OPTIONAL, + IN bool Xip, IN bool Strip, IN bool FixedAddress ) @@ -158,7 +159,7 @@ ToolImageEmit ( OutputFile = NULL; if (Format == UefiImageFormatPe) { - OutputFile = ToolImageEmitPe (&ImageInfo, OutputFileSize, Strip); + OutputFile = ToolImageEmitPe (&ImageInfo, OutputFileSize, Xip, Strip); } else { assert (false); } diff --git a/BaseTools/ImageTool/ImageToolEmit.h b/BaseTools/ImageTool/ImageToolEmit.h index ae803fcd3a..f83c4a6a7a 100644 --- a/BaseTools/ImageTool/ImageToolEmit.h +++ b/BaseTools/ImageTool/ImageToolEmit.h @@ -20,6 +20,7 @@ ToolImageEmit ( IN bool Relocate, IN uint64_t BaseAddress, IN const char *SymbolsPath OPTIONAL, + IN bool Xip, IN bool Strip, IN bool FixedAddress ); diff --git a/BaseTools/ImageTool/PeEmit.c b/BaseTools/ImageTool/PeEmit.c index 4e9de38522..78fcac85ed 100644 --- a/BaseTools/ImageTool/PeEmit.c +++ b/BaseTools/ImageTool/PeEmit.c @@ -7,6 +7,7 @@ #include "ImageTool.h" #include "DynamicBuffer.h" +#include "PeEmitCommon.h" #if PE_ARCH == 32 @@ -644,8 +645,9 @@ ToolImageEmitPeFile ( #define ToolImageEmitPe PE_SUFFIX (ToolImageEmitPe) void * ToolImageEmitPe ( - image_tool_image_info_t *Image, - uint32_t *FileSize + const image_tool_image_info_t *Image, + uint32_t *FileSize, + bool Xip ) { bool Success; @@ -659,8 +661,7 @@ ToolImageEmitPe ( ImageToolBufferInit (&Buffer); - // FIXME: Non-XIP is not well-supported right now. - Success = ToolImageEmitPeFile (&Buffer, Image, true); + Success = ToolImageEmitPeFile (&Buffer, Image, Xip); if (!Success) { raise (); ImageToolBufferFree (&Buffer); diff --git a/BaseTools/ImageTool/PeEmitCommon.c b/BaseTools/ImageTool/PeEmitCommon.c index c136721a19..9946bdfe11 100644 --- a/BaseTools/ImageTool/PeEmitCommon.c +++ b/BaseTools/ImageTool/PeEmitCommon.c @@ -9,6 +9,7 @@ void * ToolImageEmitPe ( image_tool_image_info_t *Image, uint32_t *FileSize, + bool Xip, bool Strip ) { @@ -20,13 +21,13 @@ ToolImageEmitPe ( case IMAGE_FILE_MACHINE_I386: case IMAGE_FILE_MACHINE_ARMTHUMB_MIXED: { - return ToolImageEmitPe32 (Image, FileSize); + return ToolImageEmitPe32 (Image, FileSize, Xip); } case IMAGE_FILE_MACHINE_X64: case IMAGE_FILE_MACHINE_ARM64: { - return ToolImageEmitPe64 (Image, FileSize); + return ToolImageEmitPe64 (Image, FileSize, Xip); } default: diff --git a/BaseTools/ImageTool/PeEmitCommon.h b/BaseTools/ImageTool/PeEmitCommon.h index a65761f06f..0d28aa96d3 100644 --- a/BaseTools/ImageTool/PeEmitCommon.h +++ b/BaseTools/ImageTool/PeEmitCommon.h @@ -13,13 +13,15 @@ void * ToolImageEmitPe32 ( const image_tool_image_info_t *Image, - uint32_t *FileSize + uint32_t *FileSize, + bool Xip ); void * ToolImageEmitPe64 ( const image_tool_image_info_t *Image, - uint32_t *FileSize + uint32_t *FileSize, + bool Xip ); #endif // PE_EMIT_COMMON_H diff --git a/BaseTools/Source/C/GenFv/GenFvInternalLib.c b/BaseTools/Source/C/GenFv/GenFvInternalLib.c index e3db9d2cfb..2480df3a88 100644 --- a/BaseTools/Source/C/GenFv/GenFvInternalLib.c +++ b/BaseTools/Source/C/GenFv/GenFvInternalLib.c @@ -3855,6 +3855,7 @@ Returns: true, NewBaseAddress, NULL, + TRUE, Strip, FALSE ); diff --git a/BaseTools/Source/C/VolInfo/VolInfo.c b/BaseTools/Source/C/VolInfo/VolInfo.c index d36b1c83e9..1edc2e375d 100644 --- a/BaseTools/Source/C/VolInfo/VolInfo.c +++ b/BaseTools/Source/C/VolInfo/VolInfo.c @@ -1456,6 +1456,7 @@ Returns: true, NewBaseAddress, NULL, + TRUE, FALSE, TRUE ); diff --git a/BaseTools/Source/Python/GenFds/DataSection.py b/BaseTools/Source/Python/GenFds/DataSection.py index 61d162f31b..365f9695bc 100644 --- a/BaseTools/Source/Python/GenFds/DataSection.py +++ b/BaseTools/Source/Python/GenFds/DataSection.py @@ -95,6 +95,7 @@ class DataSection (DataSectionClassObject): GenFdsGlobalVariable.GenerateFirmwareImage( StrippedFile, GenFdsGlobalVariable.MacroExtend(self.SectFileName, Dict), + Xip=True, Strip=True, IsMakefile = IsMakefile ) diff --git a/BaseTools/Source/Python/GenFds/EfiSection.py b/BaseTools/Source/Python/GenFds/EfiSection.py index 34ce17224a..0315778b08 100644 --- a/BaseTools/Source/Python/GenFds/EfiSection.py +++ b/BaseTools/Source/Python/GenFds/EfiSection.py @@ -290,6 +290,7 @@ class EfiSection (EfiSectionClassObject): GenFdsGlobalVariable.GenerateFirmwareImage( StrippedFile, File, + Xip=True, Strip=True, IsMakefile = IsMakefile ) diff --git a/BaseTools/Source/Python/GenFds/FfsInfStatement.py b/BaseTools/Source/Python/GenFds/FfsInfStatement.py index f97171df68..0cad524b49 100644 --- a/BaseTools/Source/Python/GenFds/FfsInfStatement.py +++ b/BaseTools/Source/Python/GenFds/FfsInfStatement.py @@ -787,6 +787,7 @@ class FfsInfStatement(FfsInfStatementClassObject): GenFdsGlobalVariable.GenerateFirmwareImage( StrippedFile, File, + Xip=True, Strip=True, IsMakefile=IsMakefile ) @@ -821,6 +822,7 @@ class FfsInfStatement(FfsInfStatementClassObject): GenFdsGlobalVariable.GenerateFirmwareImage( StrippedFile, GenSecInputFile, + Xip=True, Strip=True, IsMakefile=IsMakefile ) diff --git a/BaseTools/Source/Python/GenFds/GenFdsGlobalVariable.py b/BaseTools/Source/Python/GenFds/GenFdsGlobalVariable.py index 68faace90f..6c78a67ca6 100644 --- a/BaseTools/Source/Python/GenFds/GenFdsGlobalVariable.py +++ b/BaseTools/Source/Python/GenFds/GenFdsGlobalVariable.py @@ -607,7 +607,7 @@ class GenFdsGlobalVariable: GenFdsGlobalVariable.CallExternalTool(Cmd, "Failed to generate FV") @staticmethod - def GenerateFirmwareImage(Output, Input, Format="Auto", Strip=False, IsMakefile=False): + def GenerateFirmwareImage(Output, Input, Format="Auto", Xip=False, Strip=False, IsMakefile=False): if not GenFdsGlobalVariable.NeedsUpdate(Output, Input) and not IsMakefile: return GenFdsGlobalVariable.DebugLogger(EdkLogger.DEBUG_5, "%s needs update because of newer %s" % (Output, Input)) @@ -615,6 +615,8 @@ class GenFdsGlobalVariable: Cmd = ["ImageTool GenImage"] if Format != "Auto": Cmd += ("-c", Format) + if Xip: + Cmd.append("-x") if Strip: Cmd.append("-s") Cmd += ("-o", Output, Input) diff --git a/BaseTools/Source/Python/build/BuildReport.py b/BaseTools/Source/Python/build/BuildReport.py index bb19dd6033..4c380e378d 100644 --- a/BaseTools/Source/Python/build/BuildReport.py +++ b/BaseTools/Source/Python/build/BuildReport.py @@ -642,7 +642,7 @@ class ModuleReport(object): if os.path.isfile(DefaultEFIfile): Tempfile = os.path.join(OutputDir, self.ModuleName + "_hash.tmp") # rebase the efi image since its base address may not zero - cmd = ["ImageTool GenImage -b 0 -o", Tempfile, DefaultEFIfile] + cmd = ["ImageTool GenImage -x -b 0 -o", Tempfile, DefaultEFIfile] try: PopenObject = subprocess.Popen(' '.join(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) except Exception as X: diff --git a/BaseTools/Source/Python/build/build.py b/BaseTools/Source/Python/build/build.py index c47da87378..9d4e39a7f3 100755 --- a/BaseTools/Source/Python/build/build.py +++ b/BaseTools/Source/Python/build/build.py @@ -1524,16 +1524,16 @@ class Build(): # # Update Image to new BaseAddress by ImageTool tool # - LaunchCommand(["ImageTool", "GenImage", "-b", str(BaseAddress), "-o", ModuleOutputImage, ModuleOutputImage], ModuleInfo.OutputDir) - LaunchCommand(["ImageTool", "GenImage", "-b", str(BaseAddress), "-o", ModuleOutputImage, ModuleOutputImage], ModuleInfo.DebugDir) + LaunchCommand(["ImageTool", "GenImage", "-x", "-b", str(BaseAddress), "-o", ModuleOutputImage, ModuleOutputImage], ModuleInfo.OutputDir) + LaunchCommand(["ImageTool", "GenImage", "-x", "-b", str(BaseAddress), "-o", ModuleOutputImage, ModuleOutputImage], ModuleInfo.DebugDir) ## for SMM module in SMRAM, the SMRAM will be allocated from base to top. else: BaseAddress = AlignUp(BaseAddress, ModuleInfo.Image.SectionAlignment) # # Set new address to the section header only for SMM driver. # - LaunchCommand(["ImageTool", "GenImage", "-f -b", str(BaseAddress), "-o", ModuleOutputImage, ModuleOutputImage], ModuleInfo.OutputDir) - LaunchCommand(["ImageTool", "GenImage", "-f -b", str(BaseAddress), "-o", ModuleOutputImage, ModuleOutputImage], ModuleInfo.DebugDir) + LaunchCommand(["ImageTool", "GenImage", "-x", "-f -b", str(BaseAddress), "-o", ModuleOutputImage, ModuleOutputImage], ModuleInfo.OutputDir) + LaunchCommand(["ImageTool", "GenImage", "-x", "-f -b", str(BaseAddress), "-o", ModuleOutputImage, ModuleOutputImage], ModuleInfo.DebugDir) # # Collect function address from Map file #