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
.MultipleWorkspace
import MultipleWorkspace
as mws
21 from Common
import EdkLogger
22 from Common
.BuildToolError
import *
23 from Common
.DataType
import *
25 ## generate FV image section
28 class FvImageSection(FvImageSectionClassObject
):
32 # @param self The object pointer
35 FvImageSectionClassObject
.__init
__(self
)
37 ## GenSection() method
39 # Generate FV image section
41 # @param self The object pointer
42 # @param OutputPath Where to place output file
43 # @param ModuleName Which module this section belongs to
44 # @param SecNum Index of section
45 # @param KeyStringList Filter for inputs of section generation
46 # @param FfsInf FfsInfStatement object that contains this section data
47 # @param Dict dictionary contains macro and its value
48 # @retval tuple (Generated file name, section alignment)
50 def GenSection(self
, OutputPath
, ModuleName
, SecNum
, KeyStringList
, FfsInf
= None, Dict
= None, IsMakefile
= False):
55 if self
.FvFileType
is not None:
56 FileList
, IsSect
= Section
.Section
.GetFileList(FfsInf
, self
.FvFileType
, self
.FvFileExtension
)
58 return FileList
, self
.Alignment
63 for FvFileName
in FileList
:
65 if os
.path
.isfile(FvFileName
):
66 FvFileObj
= open (FvFileName
, 'rb')
68 # PI FvHeader is 0x48 byte
69 FvHeaderBuffer
= FvFileObj
.read(0x48)
70 # FV alignment position.
71 if isinstance(FvHeaderBuffer
[0x2E], str):
72 FvAlignmentValue
= 1 << (ord(FvHeaderBuffer
[0x2E]) & 0x1F)
74 FvAlignmentValue
= 1 << (FvHeaderBuffer
[0x2E] & 0x1F)
76 if FvAlignmentValue
> MaxFvAlignment
:
77 MaxFvAlignment
= FvAlignmentValue
79 OutputFile
= os
.path
.join(OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ Num
+ SectionSuffix
.get("FV_IMAGE"))
80 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [FvFileName
], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile
=IsMakefile
)
81 OutputFileList
.append(OutputFile
)
83 # MaxFvAlignment is larger than or equal to 1K
84 if MaxFvAlignment
>= 0x400:
85 if MaxFvAlignment
>= 0x100000:
86 #The max alignment supported by FFS is 16M.
87 if MaxFvAlignment
>= 0x1000000:
88 self
.Alignment
= "16M"
90 self
.Alignment
= str(MaxFvAlignment
// 0x100000) + "M"
92 self
.Alignment
= str (MaxFvAlignment
// 0x400) + "K"
94 # MaxFvAlignment is less than 1K
95 self
.Alignment
= str (MaxFvAlignment
)
97 return OutputFileList
, self
.Alignment
101 if self
.FvName
is not None:
103 Fv
= GenFdsGlobalVariable
.FdfParser
.Profile
.FvDict
.get(self
.FvName
)
106 if not self
.FvAddr
and self
.Fv
.BaseAddress
:
107 self
.FvAddr
= self
.Fv
.BaseAddress
108 FvFileName
= Fv
.AddToBuffer(Buffer
, self
.FvAddr
, MacroDict
= Dict
, Flag
=IsMakefile
)
109 if Fv
.FvAlignment
is not None:
110 if self
.Alignment
is None:
111 self
.Alignment
= Fv
.FvAlignment
113 if GenFdsGlobalVariable
.GetAlignment (Fv
.FvAlignment
) > GenFdsGlobalVariable
.GetAlignment (self
.Alignment
):
114 self
.Alignment
= Fv
.FvAlignment
116 if self
.FvFileName
is not None:
117 FvFileName
= GenFdsGlobalVariable
.ReplaceWorkspaceMacro(self
.FvFileName
)
118 if os
.path
.isfile(FvFileName
):
119 FvFileObj
= open (FvFileName
, 'rb')
121 # PI FvHeader is 0x48 byte
122 FvHeaderBuffer
= FvFileObj
.read(0x48)
123 # FV alignment position.
124 if isinstance(FvHeaderBuffer
[0x2E], str):
125 FvAlignmentValue
= 1 << (ord(FvHeaderBuffer
[0x2E]) & 0x1F)
127 FvAlignmentValue
= 1 << (FvHeaderBuffer
[0x2E] & 0x1F)
128 # FvAlignmentValue is larger than or equal to 1K
129 if FvAlignmentValue
>= 0x400:
130 if FvAlignmentValue
>= 0x100000:
131 #The max alignment supported by FFS is 16M.
132 if FvAlignmentValue
>= 0x1000000:
133 self
.Alignment
= "16M"
135 self
.Alignment
= str(FvAlignmentValue
// 0x100000) + "M"
137 self
.Alignment
= str (FvAlignmentValue
// 0x400) + "K"
139 # FvAlignmentValue is less than 1K
140 self
.Alignment
= str (FvAlignmentValue
)
143 if len (mws
.getPkgPath()) == 0:
144 EdkLogger
.error("GenFds", FILE_NOT_FOUND
, "%s is not found in WORKSPACE: %s" % self
.FvFileName
, GenFdsGlobalVariable
.WorkSpaceDir
)
146 EdkLogger
.error("GenFds", FILE_NOT_FOUND
, "%s is not found in packages path:\n\t%s" % (self
.FvFileName
, '\n\t'.join(mws
.getPkgPath())))
149 EdkLogger
.error("GenFds", GENFDS_ERROR
, "FvImageSection Failed! %s NOT found in FDF" % self
.FvName
)
152 # Prepare the parameter of GenSection
154 OutputFile
= os
.path
.join(OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ SecNum
+ SectionSuffix
.get("FV_IMAGE"))
155 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [FvFileName
], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile
=IsMakefile
)
156 OutputFileList
.append(OutputFile
)
158 return OutputFileList
, self
.Alignment