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
= {}, IsMakefile
= False):
53 if self
.FvFileType
is not None:
54 FileList
, IsSect
= Section
.Section
.GetFileList(FfsInf
, self
.FvFileType
, self
.FvFileExtension
)
56 return FileList
, self
.Alignment
61 for FvFileName
in FileList
:
63 if os
.path
.isfile(FvFileName
):
64 FvFileObj
= open (FvFileName
, 'rb')
66 # PI FvHeader is 0x48 byte
67 FvHeaderBuffer
= FvFileObj
.read(0x48)
68 # FV alignment position.
69 if isinstance(FvHeaderBuffer
[0x2E], str):
70 FvAlignmentValue
= 1 << (ord(FvHeaderBuffer
[0x2E]) & 0x1F)
72 FvAlignmentValue
= 1 << (FvHeaderBuffer
[0x2E] & 0x1F)
74 if FvAlignmentValue
> MaxFvAlignment
:
75 MaxFvAlignment
= FvAlignmentValue
77 OutputFile
= os
.path
.join(OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ Num
+ SectionSuffix
.get("FV_IMAGE"))
78 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [FvFileName
], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile
=IsMakefile
)
79 OutputFileList
.append(OutputFile
)
81 # MaxFvAlignment is larger than or equal to 1K
82 if MaxFvAlignment
>= 0x400:
83 if MaxFvAlignment
>= 0x100000:
84 #The max alignment supported by FFS is 16M.
85 if MaxFvAlignment
>= 0x1000000:
86 self
.Alignment
= "16M"
88 self
.Alignment
= str(MaxFvAlignment
// 0x100000) + "M"
90 self
.Alignment
= str (MaxFvAlignment
// 0x400) + "K"
92 # MaxFvAlignment is less than 1K
93 self
.Alignment
= str (MaxFvAlignment
)
95 return OutputFileList
, self
.Alignment
99 if self
.FvName
is not None:
101 Fv
= GenFdsGlobalVariable
.FdfParser
.Profile
.FvDict
.get(self
.FvName
)
104 if not self
.FvAddr
and self
.Fv
.BaseAddress
:
105 self
.FvAddr
= self
.Fv
.BaseAddress
106 FvFileName
= Fv
.AddToBuffer(Buffer
, self
.FvAddr
, MacroDict
= Dict
, Flag
=IsMakefile
)
107 if Fv
.FvAlignment
is not None:
108 if self
.Alignment
is None:
109 self
.Alignment
= Fv
.FvAlignment
111 if GenFdsGlobalVariable
.GetAlignment (Fv
.FvAlignment
) > GenFdsGlobalVariable
.GetAlignment (self
.Alignment
):
112 self
.Alignment
= Fv
.FvAlignment
114 if self
.FvFileName
is not None:
115 FvFileName
= GenFdsGlobalVariable
.ReplaceWorkspaceMacro(self
.FvFileName
)
116 if os
.path
.isfile(FvFileName
):
117 FvFileObj
= open (FvFileName
, 'rb')
119 # PI FvHeader is 0x48 byte
120 FvHeaderBuffer
= FvFileObj
.read(0x48)
121 # FV alignment position.
122 if isinstance(FvHeaderBuffer
[0x2E], str):
123 FvAlignmentValue
= 1 << (ord(FvHeaderBuffer
[0x2E]) & 0x1F)
125 FvAlignmentValue
= 1 << (FvHeaderBuffer
[0x2E] & 0x1F)
126 # FvAlignmentValue is larger than or equal to 1K
127 if FvAlignmentValue
>= 0x400:
128 if FvAlignmentValue
>= 0x100000:
129 #The max alignment supported by FFS is 16M.
130 if FvAlignmentValue
>= 0x1000000:
131 self
.Alignment
= "16M"
133 self
.Alignment
= str(FvAlignmentValue
// 0x100000) + "M"
135 self
.Alignment
= str (FvAlignmentValue
// 0x400) + "K"
137 # FvAlignmentValue is less than 1K
138 self
.Alignment
= str (FvAlignmentValue
)
141 if len (mws
.getPkgPath()) == 0:
142 EdkLogger
.error("GenFds", FILE_NOT_FOUND
, "%s is not found in WORKSPACE: %s" % self
.FvFileName
, GenFdsGlobalVariable
.WorkSpaceDir
)
144 EdkLogger
.error("GenFds", FILE_NOT_FOUND
, "%s is not found in packages path:\n\t%s" % (self
.FvFileName
, '\n\t'.join(mws
.getPkgPath())))
147 EdkLogger
.error("GenFds", GENFDS_ERROR
, "FvImageSection Failed! %s NOT found in FDF" % self
.FvName
)
150 # Prepare the parameter of GenSection
152 OutputFile
= os
.path
.join(OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ SecNum
+ SectionSuffix
.get("FV_IMAGE"))
153 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [FvFileName
], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile
=IsMakefile
)
154 OutputFileList
.append(OutputFile
)
156 return OutputFileList
, self
.Alignment