2009-07-17 11:10:31 +02:00
|
|
|
## @file
|
|
|
|
# generate capsule
|
|
|
|
#
|
2018-04-17 16:40:15 +02:00
|
|
|
# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
|
2009-07-17 11:10:31 +02:00
|
|
|
#
|
2010-05-18 07:04:32 +02:00
|
|
|
# This program and the accompanying materials
|
2009-07-17 11:10:31 +02:00
|
|
|
# are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
# which accompanies this distribution. The full text of the license may be found at
|
|
|
|
# http://opensource.org/licenses/bsd-license.php
|
|
|
|
#
|
|
|
|
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
#
|
|
|
|
|
|
|
|
##
|
|
|
|
# Import Modules
|
|
|
|
#
|
2018-07-13 12:18:33 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from .GenFdsGlobalVariable import GenFdsGlobalVariable
|
2018-07-13 12:18:35 +02:00
|
|
|
from .GenFdsGlobalVariable import FindExtendTool
|
2009-07-17 11:10:31 +02:00
|
|
|
from CommonDataClass.FdfClass import CapsuleClassObject
|
2014-08-15 05:06:48 +02:00
|
|
|
import Common.LongFilePathOs as os
|
2009-07-17 11:10:31 +02:00
|
|
|
import subprocess
|
2018-06-25 12:31:36 +02:00
|
|
|
from io import BytesIO
|
2009-07-17 11:10:31 +02:00
|
|
|
from Common.Misc import SaveFileOnChange
|
2015-06-23 08:46:01 +02:00
|
|
|
from Common.Misc import PackRegistryFormatGuid
|
|
|
|
import uuid
|
|
|
|
from struct import pack
|
2016-08-15 07:52:12 +02:00
|
|
|
from Common import EdkLogger
|
|
|
|
from Common.BuildToolError import *
|
2009-07-17 11:10:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
T_CHAR_LF = '\n'
|
2016-08-15 07:52:12 +02:00
|
|
|
WIN_CERT_REVISION = 0x0200
|
|
|
|
WIN_CERT_TYPE_EFI_GUID = 0x0EF1
|
|
|
|
EFI_CERT_TYPE_PKCS7_GUID = uuid.UUID('{4aafd29d-68df-49ee-8aa9-347d375665a7}')
|
|
|
|
EFI_CERT_TYPE_RSA2048_SHA256_GUID = uuid.UUID('{a7717414-c616-4977-9420-844712a735bf}')
|
2009-07-17 11:10:31 +02:00
|
|
|
|
|
|
|
## create inf file describes what goes into capsule and call GenFv to generate capsule
|
|
|
|
#
|
|
|
|
#
|
|
|
|
class Capsule (CapsuleClassObject) :
|
|
|
|
## The constructor
|
|
|
|
#
|
|
|
|
# @param self The object pointer
|
|
|
|
#
|
|
|
|
def __init__(self):
|
|
|
|
CapsuleClassObject.__init__(self)
|
|
|
|
# For GenFv
|
|
|
|
self.BlockSize = None
|
|
|
|
# For GenFv
|
|
|
|
self.BlockNum = None
|
2009-09-11 05:14:43 +02:00
|
|
|
self.CapsuleName = None
|
2009-07-17 11:10:31 +02:00
|
|
|
|
2015-06-23 08:46:01 +02:00
|
|
|
## Generate FMP capsule
|
|
|
|
#
|
|
|
|
# @retval string Generated Capsule file path
|
|
|
|
#
|
|
|
|
def GenFmpCapsule(self):
|
|
|
|
#
|
|
|
|
# Generate capsule header
|
|
|
|
# typedef struct {
|
|
|
|
# EFI_GUID CapsuleGuid;
|
|
|
|
# UINT32 HeaderSize;
|
|
|
|
# UINT32 Flags;
|
|
|
|
# UINT32 CapsuleImageSize;
|
|
|
|
# } EFI_CAPSULE_HEADER;
|
|
|
|
#
|
2018-06-25 12:31:36 +02:00
|
|
|
Header = BytesIO()
|
2015-06-23 08:46:01 +02:00
|
|
|
#
|
|
|
|
# Use FMP capsule GUID: 6DCBD5ED-E82D-4C44-BDA1-7194199AD92A
|
|
|
|
#
|
|
|
|
Header.write(PackRegistryFormatGuid('6DCBD5ED-E82D-4C44-BDA1-7194199AD92A'))
|
|
|
|
HdrSize = 0
|
|
|
|
if 'CAPSULE_HEADER_SIZE' in self.TokensDict:
|
|
|
|
Header.write(pack('=I', int(self.TokensDict['CAPSULE_HEADER_SIZE'], 16)))
|
|
|
|
HdrSize = int(self.TokensDict['CAPSULE_HEADER_SIZE'], 16)
|
|
|
|
else:
|
|
|
|
Header.write(pack('=I', 0x20))
|
|
|
|
HdrSize = 0x20
|
|
|
|
Flags = 0
|
|
|
|
if 'CAPSULE_FLAGS' in self.TokensDict:
|
|
|
|
for flag in self.TokensDict['CAPSULE_FLAGS'].split(','):
|
|
|
|
flag = flag.strip()
|
|
|
|
if flag == 'PopulateSystemTable':
|
|
|
|
Flags |= 0x00010000 | 0x00020000
|
|
|
|
elif flag == 'PersistAcrossReset':
|
|
|
|
Flags |= 0x00010000
|
|
|
|
elif flag == 'InitiateReset':
|
|
|
|
Flags |= 0x00040000
|
|
|
|
Header.write(pack('=I', Flags))
|
|
|
|
#
|
|
|
|
# typedef struct {
|
|
|
|
# UINT32 Version;
|
|
|
|
# UINT16 EmbeddedDriverCount;
|
|
|
|
# UINT16 PayloadItemCount;
|
|
|
|
# // UINT64 ItemOffsetList[];
|
|
|
|
# } EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER;
|
|
|
|
#
|
2018-06-25 12:31:36 +02:00
|
|
|
FwMgrHdr = BytesIO()
|
2015-06-23 08:46:01 +02:00
|
|
|
if 'CAPSULE_HEADER_INIT_VERSION' in self.TokensDict:
|
|
|
|
FwMgrHdr.write(pack('=I', int(self.TokensDict['CAPSULE_HEADER_INIT_VERSION'], 16)))
|
|
|
|
else:
|
|
|
|
FwMgrHdr.write(pack('=I', 0x00000001))
|
|
|
|
FwMgrHdr.write(pack('=HH', len(self.CapsuleDataList), len(self.FmpPayloadList)))
|
|
|
|
FwMgrHdrSize = 4+2+2+8*(len(self.CapsuleDataList)+len(self.FmpPayloadList))
|
|
|
|
|
2016-08-15 07:52:12 +02:00
|
|
|
#
|
|
|
|
# typedef struct _WIN_CERTIFICATE {
|
|
|
|
# UINT32 dwLength;
|
|
|
|
# UINT16 wRevision;
|
|
|
|
# UINT16 wCertificateType;
|
|
|
|
# //UINT8 bCertificate[ANYSIZE_ARRAY];
|
|
|
|
# } WIN_CERTIFICATE;
|
|
|
|
#
|
|
|
|
# typedef struct _WIN_CERTIFICATE_UEFI_GUID {
|
|
|
|
# WIN_CERTIFICATE Hdr;
|
|
|
|
# EFI_GUID CertType;
|
|
|
|
# //UINT8 CertData[ANYSIZE_ARRAY];
|
|
|
|
# } WIN_CERTIFICATE_UEFI_GUID;
|
|
|
|
#
|
|
|
|
# typedef struct {
|
|
|
|
# UINT64 MonotonicCount;
|
|
|
|
# WIN_CERTIFICATE_UEFI_GUID AuthInfo;
|
|
|
|
# } EFI_FIRMWARE_IMAGE_AUTHENTICATION;
|
|
|
|
#
|
|
|
|
# typedef struct _EFI_CERT_BLOCK_RSA_2048_SHA256 {
|
|
|
|
# EFI_GUID HashType;
|
|
|
|
# UINT8 PublicKey[256];
|
|
|
|
# UINT8 Signature[256];
|
|
|
|
# } EFI_CERT_BLOCK_RSA_2048_SHA256;
|
|
|
|
#
|
|
|
|
|
2015-06-23 08:46:01 +02:00
|
|
|
PreSize = FwMgrHdrSize
|
2018-06-25 12:31:36 +02:00
|
|
|
Content = BytesIO()
|
2015-06-23 08:46:01 +02:00
|
|
|
for driver in self.CapsuleDataList:
|
|
|
|
FileName = driver.GenCapsuleSubItem()
|
|
|
|
FwMgrHdr.write(pack('=Q', PreSize))
|
|
|
|
PreSize += os.path.getsize(FileName)
|
|
|
|
File = open(FileName, 'rb')
|
|
|
|
Content.write(File.read())
|
|
|
|
File.close()
|
|
|
|
for fmp in self.FmpPayloadList:
|
2017-06-06 07:28:07 +02:00
|
|
|
if fmp.Existed:
|
|
|
|
FwMgrHdr.write(pack('=Q', PreSize))
|
|
|
|
PreSize += len(fmp.Buffer)
|
|
|
|
Content.write(fmp.Buffer)
|
|
|
|
continue
|
2016-10-09 03:30:06 +02:00
|
|
|
if fmp.ImageFile:
|
|
|
|
for Obj in fmp.ImageFile:
|
|
|
|
fmp.ImageFile = Obj.GenCapsuleSubItem()
|
|
|
|
if fmp.VendorCodeFile:
|
|
|
|
for Obj in fmp.VendorCodeFile:
|
|
|
|
fmp.VendorCodeFile = Obj.GenCapsuleSubItem()
|
2016-08-15 07:52:12 +02:00
|
|
|
if fmp.Certificate_Guid:
|
|
|
|
ExternalTool, ExternalOption = FindExtendTool([], GenFdsGlobalVariable.ArchList, fmp.Certificate_Guid)
|
|
|
|
CmdOption = ''
|
|
|
|
CapInputFile = fmp.ImageFile
|
|
|
|
if not os.path.isabs(fmp.ImageFile):
|
|
|
|
CapInputFile = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, fmp.ImageFile)
|
|
|
|
CapOutputTmp = os.path.join(GenFdsGlobalVariable.FvDir, self.UiCapsuleName) + '.tmp'
|
2018-03-26 22:25:43 +02:00
|
|
|
if ExternalTool is None:
|
2016-08-15 07:52:12 +02:00
|
|
|
EdkLogger.error("GenFds", GENFDS_ERROR, "No tool found with GUID %s" % fmp.Certificate_Guid)
|
|
|
|
else:
|
|
|
|
CmdOption += ExternalTool
|
|
|
|
if ExternalOption:
|
|
|
|
CmdOption = CmdOption + ' ' + ExternalOption
|
|
|
|
CmdOption += ' -e ' + ' --monotonic-count ' + str(fmp.MonotonicCount) + ' -o ' + CapOutputTmp + ' ' + CapInputFile
|
|
|
|
CmdList = CmdOption.split()
|
|
|
|
GenFdsGlobalVariable.CallExternalTool(CmdList, "Failed to generate FMP auth capsule")
|
|
|
|
if uuid.UUID(fmp.Certificate_Guid) == EFI_CERT_TYPE_PKCS7_GUID:
|
|
|
|
dwLength = 4 + 2 + 2 + 16 + os.path.getsize(CapOutputTmp) - os.path.getsize(CapInputFile)
|
|
|
|
else:
|
|
|
|
dwLength = 4 + 2 + 2 + 16 + 16 + 256 + 256
|
2016-08-29 09:44:59 +02:00
|
|
|
fmp.ImageFile = CapOutputTmp
|
|
|
|
AuthData = [fmp.MonotonicCount, dwLength, WIN_CERT_REVISION, WIN_CERT_TYPE_EFI_GUID, fmp.Certificate_Guid]
|
2017-06-06 07:28:07 +02:00
|
|
|
fmp.Buffer = fmp.GenCapsuleSubItem(AuthData)
|
2016-08-15 07:52:12 +02:00
|
|
|
else:
|
2017-06-06 07:28:07 +02:00
|
|
|
fmp.Buffer = fmp.GenCapsuleSubItem()
|
2016-08-29 09:44:59 +02:00
|
|
|
FwMgrHdr.write(pack('=Q', PreSize))
|
2017-06-06 07:28:07 +02:00
|
|
|
PreSize += len(fmp.Buffer)
|
|
|
|
Content.write(fmp.Buffer)
|
2015-06-23 08:46:01 +02:00
|
|
|
BodySize = len(FwMgrHdr.getvalue()) + len(Content.getvalue())
|
|
|
|
Header.write(pack('=I', HdrSize + BodySize))
|
|
|
|
#
|
|
|
|
# The real capsule header structure is 28 bytes
|
|
|
|
#
|
|
|
|
Header.write('\x00'*(HdrSize-28))
|
|
|
|
Header.write(FwMgrHdr.getvalue())
|
|
|
|
Header.write(Content.getvalue())
|
|
|
|
#
|
|
|
|
# Generate FMP capsule file
|
|
|
|
#
|
|
|
|
CapOutputFile = os.path.join(GenFdsGlobalVariable.FvDir, self.UiCapsuleName) + '.Cap'
|
|
|
|
SaveFileOnChange(CapOutputFile, Header.getvalue(), True)
|
|
|
|
return CapOutputFile
|
|
|
|
|
2009-07-17 11:10:31 +02:00
|
|
|
## Generate capsule
|
|
|
|
#
|
|
|
|
# @param self The object pointer
|
2009-09-11 05:14:43 +02:00
|
|
|
# @retval string Generated Capsule file path
|
2009-07-17 11:10:31 +02:00
|
|
|
#
|
|
|
|
def GenCapsule(self):
|
2018-07-13 12:18:36 +02:00
|
|
|
if self.UiCapsuleName.upper() + 'cap' in GenFdsGlobalVariable.ImageBinDict:
|
|
|
|
return GenFdsGlobalVariable.ImageBinDict[self.UiCapsuleName.upper() + 'cap']
|
2009-09-11 05:14:43 +02:00
|
|
|
|
|
|
|
GenFdsGlobalVariable.InfLogger( "\nGenerate %s Capsule" %self.UiCapsuleName)
|
2018-07-05 11:40:04 +02:00
|
|
|
if ('CAPSULE_GUID' in self.TokensDict and
|
2015-06-23 08:46:01 +02:00
|
|
|
uuid.UUID(self.TokensDict['CAPSULE_GUID']) == uuid.UUID('6DCBD5ED-E82D-4C44-BDA1-7194199AD92A')):
|
|
|
|
return self.GenFmpCapsule()
|
|
|
|
|
2009-07-17 11:10:31 +02:00
|
|
|
CapInfFile = self.GenCapInf()
|
|
|
|
CapInfFile.writelines("[files]" + T_CHAR_LF)
|
2009-09-11 05:14:43 +02:00
|
|
|
CapFileList = []
|
2009-07-17 11:10:31 +02:00
|
|
|
for CapsuleDataObj in self.CapsuleDataList :
|
2009-09-11 05:14:43 +02:00
|
|
|
CapsuleDataObj.CapsuleName = self.CapsuleName
|
2009-07-17 11:10:31 +02:00
|
|
|
FileName = CapsuleDataObj.GenCapsuleSubItem()
|
2009-09-11 05:14:43 +02:00
|
|
|
CapsuleDataObj.CapsuleName = None
|
|
|
|
CapFileList.append(FileName)
|
2009-07-17 11:10:31 +02:00
|
|
|
CapInfFile.writelines("EFI_FILE_NAME = " + \
|
|
|
|
FileName + \
|
|
|
|
T_CHAR_LF)
|
|
|
|
SaveFileOnChange(self.CapInfFileName, CapInfFile.getvalue(), False)
|
|
|
|
CapInfFile.close()
|
|
|
|
#
|
|
|
|
# Call GenFv tool to generate capsule
|
|
|
|
#
|
|
|
|
CapOutputFile = os.path.join(GenFdsGlobalVariable.FvDir, self.UiCapsuleName)
|
|
|
|
CapOutputFile = CapOutputFile + '.Cap'
|
|
|
|
GenFdsGlobalVariable.GenerateFirmwareVolume(
|
|
|
|
CapOutputFile,
|
|
|
|
[self.CapInfFileName],
|
2009-09-11 05:14:43 +02:00
|
|
|
Capsule=True,
|
|
|
|
FfsList=CapFileList
|
2009-07-17 11:10:31 +02:00
|
|
|
)
|
2009-09-11 05:14:43 +02:00
|
|
|
|
|
|
|
GenFdsGlobalVariable.VerboseLogger( "\nGenerate %s Capsule Successfully" %self.UiCapsuleName)
|
2009-07-17 11:10:31 +02:00
|
|
|
GenFdsGlobalVariable.SharpCounter = 0
|
2018-07-13 12:18:36 +02:00
|
|
|
GenFdsGlobalVariable.ImageBinDict[self.UiCapsuleName.upper() + 'cap'] = CapOutputFile
|
2009-09-11 05:14:43 +02:00
|
|
|
return CapOutputFile
|
2009-07-17 11:10:31 +02:00
|
|
|
|
|
|
|
## Generate inf file for capsule
|
|
|
|
#
|
|
|
|
# @param self The object pointer
|
|
|
|
# @retval file inf file object
|
|
|
|
#
|
|
|
|
def GenCapInf(self):
|
|
|
|
self.CapInfFileName = os.path.join(GenFdsGlobalVariable.FvDir,
|
|
|
|
self.UiCapsuleName + "_Cap" + '.inf')
|
2018-06-25 12:31:36 +02:00
|
|
|
CapInfFile = BytesIO() #open (self.CapInfFileName , 'w+')
|
2009-07-17 11:10:31 +02:00
|
|
|
|
|
|
|
CapInfFile.writelines("[options]" + T_CHAR_LF)
|
|
|
|
|
2018-04-17 16:40:15 +02:00
|
|
|
for Item in self.TokensDict:
|
2009-07-17 11:10:31 +02:00
|
|
|
CapInfFile.writelines("EFI_" + \
|
|
|
|
Item + \
|
|
|
|
' = ' + \
|
2018-04-17 16:40:15 +02:00
|
|
|
self.TokensDict[Item] + \
|
2009-07-17 11:10:31 +02:00
|
|
|
T_CHAR_LF)
|
|
|
|
|
|
|
|
return CapInfFile
|