def _parseGeneral(lines, efifilepath): """ For MSFT, ICC, EBC @param lines line array for map file @return a list which element hold (PcdName, Offset, SectionName) """ status = 0 #0 - beginning of file; 1 - PE section definition; 2 - symbol table secs = [] # key = section name bPcds = [] symPattern = re.compile('^[_]+gPcd_BinaryPatch_([\w]+)') for line in lines: line = line.strip() if startPatternGeneral.match(line): status = 1 continue if addressPatternGeneral.match(line): status = 2 continue if line.startswith("entry point at"): status = 3 continue if status == 1 and len(line) != 0: m = secReGeneral.match(line) assert m is not None, "Fail to parse the section in map file , line is %s" % line sec_no, sec_start, sec_length, sec_name, sec_class = m.groups(0) secs.append([int(sec_no, 16), int(sec_start, 16), int(sec_length, 16), sec_name, sec_class]) if status == 2 and len(line) != 0: m = symRe.match(line) assert m is not None, "Fail to parse the symbol in map file, line is %s" % line sec_no, sym_offset, sym_name, vir_addr = m.groups(0) sec_no = int(sec_no, 16) sym_offset = int(sym_offset, 16) vir_addr = int(vir_addr, 16) m2 = symPattern.match(sym_name) if m2 is not None: # fond a binary pcd entry in map file for sec in secs: if sec[0] == sec_no and (sym_offset >= sec[1] and sym_offset < sec[1] + sec[2]): bPcds.append([m2.groups(0)[0], sec[3], sym_offset, vir_addr, sec_no]) if len(bPcds) == 0: return None # get section information from efi file efisecs = PeImageClass(efifilepath).SectionHeaderList if efisecs is None or len(efisecs) == 0: return None pcds = [] for pcd in bPcds: index = 0 for efisec in efisecs: index = index + 1 if pcd[1].strip() == efisec[0].strip(): pcds.append([pcd[0], efisec[2] + pcd[2], efisec[0]]) elif pcd[4] == index: pcds.append([pcd[0], efisec[2] + pcd[2], efisec[0]]) return pcds
def _parseForGCC(lines, efifilepath): """ Parse map file generated by GCC linker """ dataPattern = re.compile('^.data._gPcd_BinaryPatch_([\w_\d]+)$') status = 0 imageBase = -1 sections = [] bpcds = [] for index, line in enumerate(lines): line = line.strip() # status machine transection if status == 0 and line == "Memory Configuration": status = 1 continue elif status == 1 and line == 'Linker script and memory map': status = 2 continue elif status == 2 and line == 'START GROUP': status = 3 continue # status handler if status == 3: m = valuePatternGcc.match(line) if m is not None: sections.append(m.groups(0)) if status == 3: m = dataPattern.match(line) if m is not None: if lines[index + 1]: PcdName = m.groups(0)[0] m = pcdPatternGcc.match(lines[index + 1].strip()) if m is not None: bpcds.append( (PcdName, int(m.groups(0)[0], 16), int(sections[-1][1], 16), sections[-1][0])) # get section information from efi file efisecs = PeImageClass(efifilepath).SectionHeaderList if efisecs is None or len(efisecs) == 0: return None #redirection redirection = 0 for efisec in efisecs: for section in sections: if section[0].strip() == efisec[0].strip() and section[0].strip( ) == '.text': redirection = int(section[1], 16) - efisec[1] pcds = [] for pcd in bpcds: for efisec in efisecs: if pcd[1] >= efisec[1] and pcd[1] < efisec[1] + efisec[3]: #assert efisec[0].strip() == pcd[3].strip() and efisec[1] + redirection == pcd[2], "There are some differences between map file and efi file" pcds.append([ pcd[0], efisec[2] + pcd[1] - efisec[1] - redirection, efisec[0] ]) return pcds
def _parseForGCC(lines, efifilepath): """ Parse map file generated by GCC linker """ status = 0 imageBase = -1 sections = [] bpcds = [] for line in lines: line = line.strip() # status machine transection if status == 0 and line == "Memory Configuration": status = 1 continue elif status == 1 and line == 'Linker script and memory map': status = 2 continue elif status == 2 and line == 'START GROUP': status = 3 continue # status handler if status == 2: m = re.match('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$', line) if m != None: sections.append(m.groups(0)) if status == 2: m = re.match("^([\da-fA-Fx]+) +[_]+gPcd_BinaryPatch_([\w_\d]+)$", line) if m != None: bpcds.append((m.groups(0)[1], int(m.groups(0)[0], 16), int(sections[-1][1], 16), sections[-1][0])) # get section information from efi file efisecs = PeImageClass(efifilepath).SectionHeaderList if efisecs == None or len(efisecs) == 0: return None #redirection redirection = 0 for efisec in efisecs: for section in sections: if section[0].strip() == efisec[0].strip() and section[0].strip( ) == '.text': redirection = int(section[1], 16) - efisec[1] pcds = [] for pcd in bpcds: for efisec in efisecs: if pcd[1] >= efisec[1] and pcd[1] < efisec[1] + efisec[3]: #assert efisec[0].strip() == pcd[3].strip() and efisec[1] + redirection == pcd[2], "There are some differences between map file and efi file" pcds.append([ pcd[0], efisec[2] + pcd[1] - efisec[1] - redirection, efisec[0] ]) return pcds
def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = {}, IsMakefile = False) : if self.FileName is not None and self.FileName.startswith('PCD('): self.FileName = GenFdsGlobalVariable.GetPcdValue(self.FileName) """Prepare the parameter of GenSection""" if FfsInf is not None : InfFileName = FfsInf.InfFileName SectionType = FfsInf.__ExtendMacro__(self.SectionType) Filename = FfsInf.__ExtendMacro__(self.FileName) BuildNum = FfsInf.__ExtendMacro__(self.BuildNum) StringData = FfsInf.__ExtendMacro__(self.StringData) ModuleNameStr = FfsInf.__ExtendMacro__('$(MODULE_NAME)') NoStrip = True if FfsInf.ModuleType in (SUP_MODULE_SEC, SUP_MODULE_PEI_CORE, SUP_MODULE_PEIM) and SectionType in (BINARY_FILE_TYPE_TE, BINARY_FILE_TYPE_PE32): if FfsInf.KeepReloc is not None: NoStrip = FfsInf.KeepReloc elif FfsInf.KeepRelocFromRule is not None: NoStrip = FfsInf.KeepRelocFromRule elif self.KeepReloc is not None: NoStrip = self.KeepReloc elif FfsInf.ShadowFromInfFile is not None: NoStrip = FfsInf.ShadowFromInfFile else: EdkLogger.error("GenFds", GENFDS_ERROR, "Module %s apply rule for None!" %ModuleName) """If the file name was pointed out, add it in FileList""" FileList = [] if Filename is not None: Filename = GenFdsGlobalVariable.MacroExtend(Filename, Dict) # check if the path is absolute or relative if os.path.isabs(Filename): Filename = os.path.normpath(Filename) else: Filename = os.path.normpath(os.path.join(FfsInf.EfiOutputPath, Filename)) if not self.Optional: FileList.append(Filename) elif os.path.exists(Filename): FileList.append(Filename) elif IsMakefile: SuffixMap = FfsInf.GetFinalTargetSuffixMap() if '.depex' in SuffixMap: FileList.append(Filename) else: FileList, IsSect = Section.Section.GetFileList(FfsInf, self.FileType, self.FileExtension, Dict, IsMakefile=IsMakefile) if IsSect : return FileList, self.Alignment Index = 0 Align = self.Alignment """ If Section type is 'VERSION'""" OutputFileList = [] if SectionType == 'VERSION': InfOverrideVerString = False if FfsInf.Version is not None: #StringData = FfsInf.Version BuildNum = FfsInf.Version InfOverrideVerString = True if InfOverrideVerString: #VerTuple = ('-n', '"' + StringData + '"') if BuildNum is not None and BuildNum != '': BuildNumTuple = ('-j', BuildNum) else: BuildNumTuple = tuple() Num = SecNum OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + Ffs.SectionSuffix.get(SectionType)) GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION', #Ui=StringData, Ver=BuildNum, IsMakefile=IsMakefile) OutputFileList.append(OutputFile) elif FileList != []: for File in FileList: Index = Index + 1 Num = '%s.%d' %(SecNum, Index) OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + Ffs.SectionSuffix.get(SectionType)) f = open(File, 'r') VerString = f.read() f.close() BuildNum = VerString if BuildNum is not None and BuildNum != '': BuildNumTuple = ('-j', BuildNum) GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION', #Ui=VerString, Ver=BuildNum, IsMakefile=IsMakefile) OutputFileList.append(OutputFile) else: BuildNum = StringData if BuildNum is not None and BuildNum != '': BuildNumTuple = ('-j', BuildNum) else: BuildNumTuple = tuple() BuildNumString = ' ' + ' '.join(BuildNumTuple) #if VerString == '' and if BuildNumString == '': if self.Optional == True : GenFdsGlobalVariable.VerboseLogger( "Optional Section don't exist!") return [], None else: EdkLogger.error("GenFds", GENFDS_ERROR, "File: %s miss Version Section value" %InfFileName) Num = SecNum OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + Ffs.SectionSuffix.get(SectionType)) GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION', #Ui=VerString, Ver=BuildNum, IsMakefile=IsMakefile) OutputFileList.append(OutputFile) # # If Section Type is BINARY_FILE_TYPE_UI # elif SectionType == BINARY_FILE_TYPE_UI: InfOverrideUiString = False if FfsInf.Ui is not None: StringData = FfsInf.Ui InfOverrideUiString = True if InfOverrideUiString: Num = SecNum if IsMakefile and StringData == ModuleNameStr: StringData = "$(MODULE_NAME)" OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + Ffs.SectionSuffix.get(SectionType)) GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE', Ui=StringData, IsMakefile=IsMakefile) OutputFileList.append(OutputFile) elif FileList != []: for File in FileList: Index = Index + 1 Num = '%s.%d' %(SecNum, Index) OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + Ffs.SectionSuffix.get(SectionType)) f = open(File, 'r') UiString = f.read() f.close() if IsMakefile and UiString == ModuleNameStr: UiString = "$(MODULE_NAME)" GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE', Ui=UiString, IsMakefile=IsMakefile) OutputFileList.append(OutputFile) else: if StringData is not None and len(StringData) > 0: UiTuple = ('-n', '"' + StringData + '"') else: UiTuple = tuple() if self.Optional == True : GenFdsGlobalVariable.VerboseLogger( "Optional Section don't exist!") return '', None else: EdkLogger.error("GenFds", GENFDS_ERROR, "File: %s miss UI Section value" %InfFileName) Num = SecNum if IsMakefile and StringData == ModuleNameStr: StringData = "$(MODULE_NAME)" OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + Ffs.SectionSuffix.get(SectionType)) GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE', Ui=StringData, IsMakefile=IsMakefile) OutputFileList.append(OutputFile) else: """If File List is empty""" if FileList == [] : if self.Optional == True: GenFdsGlobalVariable.VerboseLogger("Optional Section don't exist!") return [], None else: EdkLogger.error("GenFds", GENFDS_ERROR, "Output file for %s section could not be found for %s" % (SectionType, InfFileName)) else: """Convert the File to Section file one by one """ for File in FileList: """ Copy Map file to FFS output path """ Index = Index + 1 Num = '%s.%d' %(SecNum, Index) OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + Num + Ffs.SectionSuffix.get(SectionType)) File = GenFdsGlobalVariable.MacroExtend(File, Dict) #Get PE Section alignment when align is set to AUTO if self.Alignment == 'Auto' and (SectionType == BINARY_FILE_TYPE_PE32 or SectionType == BINARY_FILE_TYPE_TE): ImageObj = PeImageClass (File) if ImageObj.SectionAlignment < 0x400: Align = str (ImageObj.SectionAlignment) elif ImageObj.SectionAlignment < 0x100000: Align = str (ImageObj.SectionAlignment / 0x400) + 'K' else: Align = str (ImageObj.SectionAlignment / 0x100000) + 'M' if File[(len(File)-4):] == '.efi': MapFile = File.replace('.efi', '.map') CopyMapFile = os.path.join(OutputPath, ModuleName + '.map') if IsMakefile: if GenFdsGlobalVariable.CopyList == []: GenFdsGlobalVariable.CopyList = [(MapFile, CopyMapFile)] else: GenFdsGlobalVariable.CopyList.append((MapFile, CopyMapFile)) else: if os.path.exists(MapFile): if not os.path.exists(CopyMapFile) or \ (os.path.getmtime(MapFile) > os.path.getmtime(CopyMapFile)): CopyLongFilePath(MapFile, CopyMapFile) if not NoStrip: FileBeforeStrip = os.path.join(OutputPath, ModuleName + '.efi') if IsMakefile: if GenFdsGlobalVariable.CopyList == []: GenFdsGlobalVariable.CopyList = [(File, FileBeforeStrip)] else: GenFdsGlobalVariable.CopyList.append((File, FileBeforeStrip)) else: if not os.path.exists(FileBeforeStrip) or \ (os.path.getmtime(File) > os.path.getmtime(FileBeforeStrip)): CopyLongFilePath(File, FileBeforeStrip) StrippedFile = os.path.join(OutputPath, ModuleName + '.stripped') GenFdsGlobalVariable.GenerateFirmwareImage( StrippedFile, [File], Strip=True, IsMakefile = IsMakefile ) File = StrippedFile """For TE Section call GenFw to generate TE image""" if SectionType == BINARY_FILE_TYPE_TE: TeFile = os.path.join( OutputPath, ModuleName + 'Te.raw') GenFdsGlobalVariable.GenerateFirmwareImage( TeFile, [File], Type='te', IsMakefile = IsMakefile ) File = TeFile """Call GenSection""" GenFdsGlobalVariable.GenerateSection(OutputFile, [File], Section.Section.SectionType.get (SectionType), IsMakefile=IsMakefile ) OutputFileList.append(OutputFile) return OutputFileList, Align
def GenSection(self, OutputPath, ModuleName, SecNum, keyStringList, FfsFile=None, Dict={}): # # Prepare the parameter of GenSection # if FfsFile != None: self.SectFileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro( self.SectFileName) self.SectFileName = GenFdsGlobalVariable.MacroExtend( self.SectFileName, Dict, FfsFile.CurrentArch) else: self.SectFileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro( self.SectFileName) self.SectFileName = GenFdsGlobalVariable.MacroExtend( self.SectFileName, Dict) """Check Section file exist or not !""" if not os.path.exists(self.SectFileName): self.SectFileName = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, self.SectFileName) """Copy Map file to Ffs output""" Filename = GenFdsGlobalVariable.MacroExtend(self.SectFileName) if Filename[(len(Filename) - 4):] == '.efi': MapFile = Filename.replace('.efi', '.map') if os.path.exists(MapFile): CopyMapFile = os.path.join(OutputPath, ModuleName + '.map') if not os.path.exists(CopyMapFile) or ( os.path.getmtime(MapFile) > os.path.getmtime(CopyMapFile)): CopyLongFilePath(MapFile, CopyMapFile) #Get PE Section alignment when align is set to AUTO if self.Alignment == 'Auto' and self.SecType in ('TE', 'PE32'): ImageObj = PeImageClass(Filename) if ImageObj.SectionAlignment < 0x400: self.Alignment = str(ImageObj.SectionAlignment) elif ImageObj.SectionAlignment < 0x100000: self.Alignment = str(ImageObj.SectionAlignment / 0x400) + 'K' else: self.Alignment = str( ImageObj.SectionAlignment / 0x100000) + 'M' NoStrip = True if self.SecType in ('TE', 'PE32'): if self.KeepReloc != None: NoStrip = self.KeepReloc if not NoStrip: FileBeforeStrip = os.path.join(OutputPath, ModuleName + '.efi') if not os.path.exists(FileBeforeStrip) or \ (os.path.getmtime(self.SectFileName) > os.path.getmtime(FileBeforeStrip)): CopyLongFilePath(self.SectFileName, FileBeforeStrip) StrippedFile = os.path.join(OutputPath, ModuleName + '.stripped') GenFdsGlobalVariable.GenerateFirmwareImage( StrippedFile, [GenFdsGlobalVariable.MacroExtend(self.SectFileName, Dict)], Strip=True) self.SectFileName = StrippedFile if self.SecType == 'TE': TeFile = os.path.join(OutputPath, ModuleName + 'Te.raw') GenFdsGlobalVariable.GenerateFirmwareImage( TeFile, [GenFdsGlobalVariable.MacroExtend(self.SectFileName, Dict)], Type='te') self.SectFileName = TeFile OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + SecNum + Ffs.SectionSuffix.get(self.SecType)) OutputFile = os.path.normpath(OutputFile) GenFdsGlobalVariable.GenerateSection( OutputFile, [self.SectFileName], Section.Section.SectionType.get(self.SecType)) FileList = [OutputFile] return FileList, self.Alignment
def __GenSimpleFileSection__(self, Rule): # # Prepare the parameter of GenSection # FileList = [] OutputFileList = [] GenSecInputFile = None if Rule.FileName != None: GenSecInputFile = self.__ExtendMacro__(Rule.FileName) if os.path.isabs(GenSecInputFile): GenSecInputFile = os.path.normpath(GenSecInputFile) else: GenSecInputFile = os.path.normpath(os.path.join(self.EfiOutputPath, GenSecInputFile)) else: FileList, IsSect = Section.Section.GetFileList(self, '', Rule.FileExtension) Index = 1 SectionType = Rule.SectionType # # Convert Fv Section Type for PI1.1 SMM driver. # if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) >= 0x0001000A: if SectionType == 'DXE_DEPEX': SectionType = 'SMM_DEPEX' # # Framework SMM Driver has no SMM_DEPEX section type # if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) < 0x0001000A: if SectionType == 'SMM_DEPEX': EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Framework SMM module doesn't support SMM_DEPEX section type", File=self.InfFileName) NoStrip = True if self.ModuleType in ('SEC', 'PEI_CORE', 'PEIM'): if self.KeepReloc != None: NoStrip = self.KeepReloc elif Rule.KeepReloc != None: NoStrip = Rule.KeepReloc elif self.ShadowFromInfFile != None: NoStrip = self.ShadowFromInfFile if FileList != [] : for File in FileList: SecNum = '%d' %Index GenSecOutputFile= self.__ExtendMacro__(Rule.NameGuid) + \ Ffs.Ffs.SectionSuffix[SectionType] + 'SEC' + SecNum Index = Index + 1 OutputFile = os.path.join(self.OutputPath, GenSecOutputFile) File = GenFdsGlobalVariable.MacroExtend(File, Dict, self.CurrentArch) #Get PE Section alignment when align is set to AUTO if self.Alignment == 'Auto' and (SectionType == 'PE32' or SectionType == 'TE'): ImageObj = PeImageClass (File) if ImageObj.SectionAlignment < 0x400: self.Alignment = str (ImageObj.SectionAlignment) else: self.Alignment = str (ImageObj.SectionAlignment / 0x400) + 'K' if not NoStrip: FileBeforeStrip = os.path.join(self.OutputPath, ModuleName + '.reloc') if not os.path.exists(FileBeforeStrip) or \ (os.path.getmtime(File) > os.path.getmtime(FileBeforeStrip)): CopyLongFilePath(File, FileBeforeStrip) StrippedFile = os.path.join(self.OutputPath, ModuleName + '.stipped') GenFdsGlobalVariable.GenerateFirmwareImage( StrippedFile, [File], Strip=True ) File = StrippedFile if SectionType == 'TE': TeFile = os.path.join( self.OutputPath, self.ModuleGuid + 'Te.raw') GenFdsGlobalVariable.GenerateFirmwareImage( TeFile, [File], Type='te' ) File = TeFile GenFdsGlobalVariable.GenerateSection(OutputFile, [File], Section.Section.SectionType[SectionType]) OutputFileList.append(OutputFile) else: SecNum = '%d' %Index GenSecOutputFile= self.__ExtendMacro__(Rule.NameGuid) + \ Ffs.Ffs.SectionSuffix[SectionType] + 'SEC' + SecNum OutputFile = os.path.join(self.OutputPath, GenSecOutputFile) GenSecInputFile = GenFdsGlobalVariable.MacroExtend(GenSecInputFile, Dict, self.CurrentArch) #Get PE Section alignment when align is set to AUTO if self.Alignment == 'Auto' and (SectionType == 'PE32' or SectionType == 'TE'): ImageObj = PeImageClass (GenSecInputFile) if ImageObj.SectionAlignment < 0x400: self.Alignment = str (ImageObj.SectionAlignment) else: self.Alignment = str (ImageObj.SectionAlignment / 0x400) + 'K' if not NoStrip: FileBeforeStrip = os.path.join(self.OutputPath, ModuleName + '.reloc') if not os.path.exists(FileBeforeStrip) or \ (os.path.getmtime(GenSecInputFile) > os.path.getmtime(FileBeforeStrip)): CopyLongFilePath(GenSecInputFile, FileBeforeStrip) StrippedFile = os.path.join(self.OutputPath, ModuleName + '.stipped') GenFdsGlobalVariable.GenerateFirmwareImage( StrippedFile, [GenSecInputFile], Strip=True ) GenSecInputFile = StrippedFile if SectionType == 'TE': TeFile = os.path.join( self.OutputPath, self.ModuleGuid + 'Te.raw') GenFdsGlobalVariable.GenerateFirmwareImage( TeFile, [GenSecInputFile], Type='te' ) GenSecInputFile = TeFile GenFdsGlobalVariable.GenerateSection(OutputFile, [GenSecInputFile], Section.Section.SectionType[SectionType]) OutputFileList.append(OutputFile) return OutputFileList