]> git.proxmox.com Git - mirror_edk2.git/blob - UefiPayloadPkg/UniversalPayloadBuild.py
e624ec5874cf114f387239f3b3f92e49bc25f654
[mirror_edk2.git] / UefiPayloadPkg / UniversalPayloadBuild.py
1 ## @file
2 # This file contains the script to build UniversalPayload
3 #
4 # Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
6 ##
7
8 import argparse
9 import subprocess
10 import os
11 import shutil
12 import sys
13 from ctypes import *
14
15 sys.dont_write_bytecode = True
16
17 class UPLD_INFO_HEADER(LittleEndianStructure):
18 _pack_ = 1
19 _fields_ = [
20 ('Identifier', ARRAY(c_char, 4)),
21 ('HeaderLength', c_uint32),
22 ('SpecRevision', c_uint16),
23 ('Reserved', c_uint16),
24 ('Revision', c_uint32),
25 ('Attribute', c_uint32),
26 ('Capability', c_uint32),
27 ('ProducerId', ARRAY(c_char, 16)),
28 ('ImageId', ARRAY(c_char, 16)),
29 ]
30
31 def __init__(self):
32 self.Identifier = b'UPLD'
33 self.HeaderLength = sizeof(UPLD_INFO_HEADER)
34 self.HeaderRevision = 0x0075
35 self.Revision = 0x0000010105
36 self.ImageId = b'UEFI'
37 self.ProducerId = b'INTEL'
38
39 def RunCommand(cmd):
40 print(cmd)
41 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,cwd=os.environ['WORKSPACE'])
42 while True:
43 line = p.stdout.readline()
44 if not line:
45 break
46 print(line.strip().decode(errors='ignore'))
47
48 p.communicate()
49 if p.returncode != 0:
50 print("- Failed - error happened when run command: %s"%cmd)
51 raise Exception("ERROR: when run command: %s"%cmd)
52
53 def BuildUniversalPayload(Args, MacroList):
54 BuildTarget = Args.Target
55 ToolChain = Args.ToolChain
56 ElfToolChain = 'CLANGDWARF'
57
58 EntryModuleInf = os.path.normpath("UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf")
59 DscPath = os.path.normpath("UefiPayloadPkg/UefiPayloadPkg.dsc")
60 BuildDir = os.path.join(os.environ['WORKSPACE'], os.path.normpath("Build/UefiPayloadPkgX64"))
61 FvOutputDir = os.path.join(BuildDir, f"{BuildTarget}_{ToolChain}", os.path.normpath("FV/DXEFV.Fv"))
62 EntryOutputDir = os.path.join(BuildDir, f"{BuildTarget}_{ElfToolChain}", os.path.normpath("X64/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry/DEBUG/UniversalPayloadEntry.dll"))
63 PayloadReportPath = os.path.join(BuildDir, "UefiUniversalPayload.txt")
64 ModuleReportPath = os.path.join(BuildDir, "UefiUniversalPayloadEntry.txt")
65 UpldInfoFile = os.path.join(BuildDir, "UniversalPayloadInfo.bin")
66
67 if "CLANG_BIN" in os.environ:
68 LlvmObjcopyPath = os.path.join(os.environ["CLANG_BIN"], "llvm-objcopy")
69 else:
70 LlvmObjcopyPath = "llvm-objcopy"
71 try:
72 RunCommand('"%s" --version'%LlvmObjcopyPath)
73 except:
74 print("- Failed - Please check if LLVM is installed or if CLANG_BIN is set correctly")
75 sys.exit(1)
76
77 Defines = ""
78 for key in MacroList:
79 Defines +=" -D {0}={1}".format(key, MacroList[key])
80
81 #
82 # Building DXE core and DXE drivers as DXEFV.
83 #
84 BuildPayload = f"build -p {DscPath} -b {BuildTarget} -a X64 -t {ToolChain} -y {PayloadReportPath}"
85 BuildPayload += Defines
86 RunCommand(BuildPayload)
87 #
88 # Building Universal Payload entry.
89 #
90 BuildModule = f"build -p {DscPath} -b {BuildTarget} -a X64 -m {EntryModuleInf} -t {ElfToolChain} -y {ModuleReportPath}"
91 BuildModule += Defines
92 RunCommand(BuildModule)
93
94 #
95 # Buid Universal Payload Information Section ".upld_info"
96 #
97 upld_info_hdr = UPLD_INFO_HEADER()
98 upld_info_hdr.ImageId = Args.ImageId.encode()[:16]
99 fp = open(UpldInfoFile, 'wb')
100 fp.write(bytearray(upld_info_hdr))
101 fp.close()
102
103 #
104 # Copy the DXEFV as a section in elf format Universal Payload entry.
105 #
106 remove_section = '"%s" -I elf64-x86-64 -O elf64-x86-64 --remove-section .upld_info --remove-section .upld.uefi_fv %s'%(LlvmObjcopyPath, EntryOutputDir)
107 add_section = '"%s" -I elf64-x86-64 -O elf64-x86-64 --add-section .upld_info=%s --add-section .upld.uefi_fv=%s %s'%(LlvmObjcopyPath, UpldInfoFile, FvOutputDir, EntryOutputDir)
108 set_section = '"%s" -I elf64-x86-64 -O elf64-x86-64 --set-section-alignment .upld.upld_info=16 --set-section-alignment .upld.uefi_fv=16 %s'%(LlvmObjcopyPath, EntryOutputDir)
109 RunCommand(remove_section)
110 RunCommand(add_section)
111 RunCommand(set_section)
112
113 shutil.copy (EntryOutputDir, os.path.join(BuildDir, 'UniversalPayload.elf'))
114
115 def main():
116 parser = argparse.ArgumentParser(description='For building Universal Payload')
117 parser.add_argument('-t', '--ToolChain')
118 parser.add_argument('-b', '--Target', default='DEBUG')
119 parser.add_argument("-D", "--Macro", action="append", default=["UNIVERSAL_PAYLOAD=TRUE"])
120 parser.add_argument('-i', '--ImageId', type=str, help='Specify payload ID (16 bytes maximal).', default ='UEFI')
121 MacroList = {}
122 args = parser.parse_args()
123 if args.Macro is not None:
124 for Argument in args.Macro:
125 if Argument.count('=') != 1:
126 print("Unknown variable passed in: %s"%Argument)
127 raise Exception("ERROR: Unknown variable passed in: %s"%Argument)
128 tokens = Argument.strip().split('=')
129 MacroList[tokens[0].upper()] = tokens[1]
130 BuildUniversalPayload(args, MacroList)
131 print ("Successfully build Universal Payload")
132
133 if __name__ == '__main__':
134 main()