2 # process FV image section generation
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 # This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 from __future__
import absolute_import
20 from io
import BytesIO
21 from .Ffs
import SectionSuffix
23 from .GenFdsGlobalVariable
import GenFdsGlobalVariable
24 import Common
.LongFilePathOs
as os
25 from CommonDataClass
.FdfClass
import FvImageSectionClassObject
26 from Common
import EdkLogger
27 from Common
.BuildToolError
import *
28 from Common
.DataType
import *
30 ## generate FV image section
33 class FvImageSection(FvImageSectionClassObject
):
37 # @param self The object pointer
40 FvImageSectionClassObject
.__init
__(self
)
42 ## GenSection() method
44 # Generate FV image section
46 # @param self The object pointer
47 # @param OutputPath Where to place output file
48 # @param ModuleName Which module this section belongs to
49 # @param SecNum Index of section
50 # @param KeyStringList Filter for inputs of section generation
51 # @param FfsInf FfsInfStatement object that contains this section data
52 # @param Dict dictionary contains macro and its value
53 # @retval tuple (Generated file name, section alignment)
55 def GenSection(self
, OutputPath
, ModuleName
, SecNum
, KeyStringList
, FfsInf
= None, Dict
= {}, IsMakefile
= False):
58 if self
.FvFileType
is not None:
59 FileList
, IsSect
= Section
.Section
.GetFileList(FfsInf
, self
.FvFileType
, self
.FvFileExtension
)
61 return FileList
, self
.Alignment
66 for FvFileName
in FileList
:
68 if os
.path
.isfile(FvFileName
):
69 FvFileObj
= open (FvFileName
, 'rb')
71 # PI FvHeader is 0x48 byte
72 FvHeaderBuffer
= FvFileObj
.read(0x48)
73 # FV alignment position.
74 if isinstance(FvHeaderBuffer
[0x2E], str):
75 FvAlignmentValue
= 1 << (ord(FvHeaderBuffer
[0x2E]) & 0x1F)
77 FvAlignmentValue
= 1 << (FvHeaderBuffer
[0x2E] & 0x1F)
79 if FvAlignmentValue
> MaxFvAlignment
:
80 MaxFvAlignment
= FvAlignmentValue
82 OutputFile
= os
.path
.join(OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ Num
+ SectionSuffix
.get("FV_IMAGE"))
83 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [FvFileName
], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile
=IsMakefile
)
84 OutputFileList
.append(OutputFile
)
86 # MaxFvAlignment is larger than or equal to 1K
87 if MaxFvAlignment
>= 0x400:
88 if MaxFvAlignment
>= 0x100000:
89 #The max alignment supported by FFS is 16M.
90 if MaxFvAlignment
>= 0x1000000:
91 self
.Alignment
= "16M"
93 self
.Alignment
= str(MaxFvAlignment
// 0x100000) + "M"
95 self
.Alignment
= str (MaxFvAlignment
// 0x400) + "K"
97 # MaxFvAlignment is less than 1K
98 self
.Alignment
= str (MaxFvAlignment
)
100 return OutputFileList
, self
.Alignment
104 if self
.FvName
is not None:
106 Fv
= GenFdsGlobalVariable
.FdfParser
.Profile
.FvDict
.get(self
.FvName
)
109 if not self
.FvAddr
and self
.Fv
.BaseAddress
:
110 self
.FvAddr
= self
.Fv
.BaseAddress
111 FvFileName
= Fv
.AddToBuffer(Buffer
, self
.FvAddr
, MacroDict
= Dict
, Flag
=IsMakefile
)
112 if Fv
.FvAlignment
is not None:
113 if self
.Alignment
is None:
114 self
.Alignment
= Fv
.FvAlignment
116 if GenFdsGlobalVariable
.GetAlignment (Fv
.FvAlignment
) > GenFdsGlobalVariable
.GetAlignment (self
.Alignment
):
117 self
.Alignment
= Fv
.FvAlignment
119 if self
.FvFileName
is not None:
120 FvFileName
= GenFdsGlobalVariable
.ReplaceWorkspaceMacro(self
.FvFileName
)
121 if os
.path
.isfile(FvFileName
):
122 FvFileObj
= open (FvFileName
, 'rb')
124 # PI FvHeader is 0x48 byte
125 FvHeaderBuffer
= FvFileObj
.read(0x48)
126 # FV alignment position.
127 if isinstance(FvHeaderBuffer
[0x2E], str):
128 FvAlignmentValue
= 1 << (ord(FvHeaderBuffer
[0x2E]) & 0x1F)
130 FvAlignmentValue
= 1 << (FvHeaderBuffer
[0x2E] & 0x1F)
131 # FvAlignmentValue is larger than or equal to 1K
132 if FvAlignmentValue
>= 0x400:
133 if FvAlignmentValue
>= 0x100000:
134 #The max alignment supported by FFS is 16M.
135 if FvAlignmentValue
>= 0x1000000:
136 self
.Alignment
= "16M"
138 self
.Alignment
= str(FvAlignmentValue
// 0x100000) + "M"
140 self
.Alignment
= str (FvAlignmentValue
// 0x400) + "K"
142 # FvAlignmentValue is less than 1K
143 self
.Alignment
= str (FvAlignmentValue
)
146 if len (mws
.getPkgPath()) == 0:
147 EdkLogger
.error("GenFds", FILE_NOT_FOUND
, "%s is not found in WORKSPACE: %s" % self
.FvFileName
, GenFdsGlobalVariable
.WorkSpaceDir
)
149 EdkLogger
.error("GenFds", FILE_NOT_FOUND
, "%s is not found in packages path:\n\t%s" % (self
.FvFileName
, '\n\t'.join(mws
.getPkgPath())))
152 EdkLogger
.error("GenFds", GENFDS_ERROR
, "FvImageSection Failed! %s NOT found in FDF" % self
.FvName
)
155 # Prepare the parameter of GenSection
157 OutputFile
= os
.path
.join(OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ SecNum
+ SectionSuffix
.get("FV_IMAGE"))
158 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [FvFileName
], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile
=IsMakefile
)
159 OutputFileList
.append(OutputFile
)
161 return OutputFileList
, self
.Alignment