]> git.proxmox.com Git - mirror_edk2.git/blob - UefiPayloadPkg/UniversalPayloadBuild.py
ac965766c749f4fb0cbc9fed88c4f8ac0acb80d7
[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 BuildArch = "X64" if Args.Arch == 'X64' else "IA32 -a X64"
57 ElfToolChain = 'CLANGDWARF'
58
59 EntryModuleInf = os.path.normpath("UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf")
60 DscPath = os.path.normpath("UefiPayloadPkg/UefiPayloadPkg.dsc")
61 BuildDir = os.path.join(os.environ['WORKSPACE'], os.path.normpath("Build/UefiPayloadPkgX64"))
62 FvOutputDir = os.path.join(BuildDir, f"{BuildTarget}_{ToolChain}", os.path.normpath("FV/DXEFV.Fv"))
63 EntryOutputDir = os.path.join(BuildDir, f"{BuildTarget}_{ElfToolChain}", os.path.normpath("X64/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry/DEBUG/UniversalPayloadEntry.dll"))
64 PayloadReportPath = os.path.join(BuildDir, "UefiUniversalPayload.txt")
65 ModuleReportPath = os.path.join(BuildDir, "UefiUniversalPayloadEntry.txt")
66 UpldInfoFile = os.path.join(BuildDir, "UniversalPayloadInfo.bin")
67
68 if "CLANG_BIN" in os.environ:
69 LlvmObjcopyPath = os.path.join(os.environ["CLANG_BIN"], "llvm-objcopy")
70 else:
71 LlvmObjcopyPath = "llvm-objcopy"
72 try:
73 RunCommand('"%s" --version'%LlvmObjcopyPath)
74 except:
75 print("- Failed - Please check if LLVM is installed or if CLANG_BIN is set correctly")
76 sys.exit(1)
77
78 Defines = ""
79 for key in MacroList:
80 Defines +=" -D {0}={1}".format(key, MacroList[key])
81
82 #
83 # Building DXE core and DXE drivers as DXEFV.
84 #
85 BuildPayload = f"build -p {DscPath} -b {BuildTarget} -a X64 -t {ToolChain} -y {PayloadReportPath}"
86 BuildPayload += Defines
87 RunCommand(BuildPayload)
88 #
89 # Building Universal Payload entry.
90 #
91 BuildModule = f"build -p {DscPath} -b {BuildTarget} -a {BuildArch} -m {EntryModuleInf} -t {ElfToolChain} -y {ModuleReportPath}"
92 BuildModule += Defines
93 RunCommand(BuildModule)
94
95 #
96 # Buid Universal Payload Information Section ".upld_info"
97 #
98 upld_info_hdr = UPLD_INFO_HEADER()
99 upld_info_hdr.ImageId = Args.ImageId.encode()[:16]
100 fp = open(UpldInfoFile, 'wb')
101 fp.write(bytearray(upld_info_hdr))
102 fp.close()
103
104 #
105 # Copy the DXEFV as a section in elf format Universal Payload entry.
106 #
107 remove_section = '"%s" -I elf64-x86-64 -O elf64-x86-64 --remove-section .upld_info --remove-section .upld.uefi_fv %s'%(LlvmObjcopyPath, EntryOutputDir)
108 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)
109 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)
110 RunCommand(remove_section)
111 RunCommand(add_section)
112 RunCommand(set_section)
113
114 shutil.copy (EntryOutputDir, os.path.join(BuildDir, 'UniversalPayload.elf'))
115
116 def main():
117 parser = argparse.ArgumentParser(description='For building Universal Payload')
118 parser.add_argument('-t', '--ToolChain')
119 parser.add_argument('-b', '--Target', default='DEBUG')
120 parser.add_argument('-a', '--Arch', choices=['IA32', 'X64'], help='Specify the ARCH for payload entry module. Default build X64 image.', default ='X64')
121 parser.add_argument("-D", "--Macro", action="append", default=["UNIVERSAL_PAYLOAD=TRUE"])
122 parser.add_argument('-i', '--ImageId', type=str, help='Specify payload ID (16 bytes maximal).', default ='UEFI')
123 MacroList = {}
124 args = parser.parse_args()
125 if args.Macro is not None:
126 for Argument in args.Macro:
127 if Argument.count('=') != 1:
128 print("Unknown variable passed in: %s"%Argument)
129 raise Exception("ERROR: Unknown variable passed in: %s"%Argument)
130 tokens = Argument.strip().split('=')
131 MacroList[tokens[0].upper()] = tokens[1]
132 BuildUniversalPayload(args, MacroList)
133 print ("Successfully build Universal Payload")
134
135 if __name__ == '__main__':
136 main()