2 # process FV image section generation
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
12 from __future__
import absolute_import
14 from io
import BytesIO
15 from .Ffs
import SectionSuffix
17 from .GenFdsGlobalVariable
import GenFdsGlobalVariable
18 import Common
.LongFilePathOs
as os
19 from CommonDataClass
.FdfClass
import FvImageSectionClassObject
20 from Common
import EdkLogger
21 from Common
.BuildToolError
import *
22 from Common
.DataType
import *
24 ## generate FV image section
27 class FvImageSection(FvImageSectionClassObject
):
31 # @param self The object pointer
34 FvImageSectionClassObject
.__init
__(self
)
36 ## GenSection() method
38 # Generate FV image section
40 # @param self The object pointer
41 # @param OutputPath Where to place output file
42 # @param ModuleName Which module this section belongs to
43 # @param SecNum Index of section
44 # @param KeyStringList Filter for inputs of section generation
45 # @param FfsInf FfsInfStatement object that contains this section data
46 # @param Dict dictionary contains macro and its value
47 # @retval tuple (Generated file name, section alignment)
49 def GenSection(self
, OutputPath
, ModuleName
, SecNum
, KeyStringList
, FfsInf
= None, Dict
= {}, IsMakefile
= False):
52 if self
.FvFileType
is not None:
53 FileList
, IsSect
= Section
.Section
.GetFileList(FfsInf
, self
.FvFileType
, self
.FvFileExtension
)
55 return FileList
, self
.Alignment
60 for FvFileName
in FileList
:
62 if os
.path
.isfile(FvFileName
):
63 FvFileObj
= open (FvFileName
, 'rb')
65 # PI FvHeader is 0x48 byte
66 FvHeaderBuffer
= FvFileObj
.read(0x48)
67 # FV alignment position.
68 if isinstance(FvHeaderBuffer
[0x2E], str):
69 FvAlignmentValue
= 1 << (ord(FvHeaderBuffer
[0x2E]) & 0x1F)
71 FvAlignmentValue
= 1 << (FvHeaderBuffer
[0x2E] & 0x1F)
73 if FvAlignmentValue
> MaxFvAlignment
:
74 MaxFvAlignment
= FvAlignmentValue
76 OutputFile
= os
.path
.join(OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ Num
+ SectionSuffix
.get("FV_IMAGE"))
77 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [FvFileName
], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile
=IsMakefile
)
78 OutputFileList
.append(OutputFile
)
80 # MaxFvAlignment is larger than or equal to 1K
81 if MaxFvAlignment
>= 0x400:
82 if MaxFvAlignment
>= 0x100000:
83 #The max alignment supported by FFS is 16M.
84 if MaxFvAlignment
>= 0x1000000:
85 self
.Alignment
= "16M"
87 self
.Alignment
= str(MaxFvAlignment
// 0x100000) + "M"
89 self
.Alignment
= str (MaxFvAlignment
// 0x400) + "K"
91 # MaxFvAlignment is less than 1K
92 self
.Alignment
= str (MaxFvAlignment
)
94 return OutputFileList
, self
.Alignment
98 if self
.FvName
is not None:
100 Fv
= GenFdsGlobalVariable
.FdfParser
.Profile
.FvDict
.get(self
.FvName
)
103 if not self
.FvAddr
and self
.Fv
.BaseAddress
:
104 self
.FvAddr
= self
.Fv
.BaseAddress
105 FvFileName
= Fv
.AddToBuffer(Buffer
, self
.FvAddr
, MacroDict
= Dict
, Flag
=IsMakefile
)
106 if Fv
.FvAlignment
is not None:
107 if self
.Alignment
is None:
108 self
.Alignment
= Fv
.FvAlignment
110 if GenFdsGlobalVariable
.GetAlignment (Fv
.FvAlignment
) > GenFdsGlobalVariable
.GetAlignment (self
.Alignment
):
111 self
.Alignment
= Fv
.FvAlignment
113 if self
.FvFileName
is not None:
114 FvFileName
= GenFdsGlobalVariable
.ReplaceWorkspaceMacro(self
.FvFileName
)
115 if os
.path
.isfile(FvFileName
):
116 FvFileObj
= open (FvFileName
, 'rb')
118 # PI FvHeader is 0x48 byte
119 FvHeaderBuffer
= FvFileObj
.read(0x48)
120 # FV alignment position.
121 if isinstance(FvHeaderBuffer
[0x2E], str):
122 FvAlignmentValue
= 1 << (ord(FvHeaderBuffer
[0x2E]) & 0x1F)
124 FvAlignmentValue
= 1 << (FvHeaderBuffer
[0x2E] & 0x1F)
125 # FvAlignmentValue is larger than or equal to 1K
126 if FvAlignmentValue
>= 0x400:
127 if FvAlignmentValue
>= 0x100000:
128 #The max alignment supported by FFS is 16M.
129 if FvAlignmentValue
>= 0x1000000:
130 self
.Alignment
= "16M"
132 self
.Alignment
= str(FvAlignmentValue
// 0x100000) + "M"
134 self
.Alignment
= str (FvAlignmentValue
// 0x400) + "K"
136 # FvAlignmentValue is less than 1K
137 self
.Alignment
= str (FvAlignmentValue
)
140 if len (mws
.getPkgPath()) == 0:
141 EdkLogger
.error("GenFds", FILE_NOT_FOUND
, "%s is not found in WORKSPACE: %s" % self
.FvFileName
, GenFdsGlobalVariable
.WorkSpaceDir
)
143 EdkLogger
.error("GenFds", FILE_NOT_FOUND
, "%s is not found in packages path:\n\t%s" % (self
.FvFileName
, '\n\t'.join(mws
.getPkgPath())))
146 EdkLogger
.error("GenFds", GENFDS_ERROR
, "FvImageSection Failed! %s NOT found in FDF" % self
.FvName
)
149 # Prepare the parameter of GenSection
151 OutputFile
= os
.path
.join(OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ SecNum
+ SectionSuffix
.get("FV_IMAGE"))
152 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [FvFileName
], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile
=IsMakefile
)
153 OutputFileList
.append(OutputFile
)
155 return OutputFileList
, self
.Alignment