forked from Project-Aloha/mu_aloha_platforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_uefi.py
executable file
·308 lines (251 loc) · 11 KB
/
build_uefi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/python3
# @file
# This script help you build any devices in any silicons in this repo.
#
# Copyright (c) DuoWoa Authors.
# SPDX-License-Identifier: BSD-2-Clause-Patent#
##
# TODO Port to windows if possible.
import argparse
import os
import shutil
import json
import Levenshtein
class Target:
def __init__(self, device, silicon, package, bootshim_uefi_base, bootshim_uefi_size, bootshim_padding_size):
self.device = device
self.silicon = silicon
self.package = package
self.bootshim_uefi_base = bootshim_uefi_base
self.bootshim_uefi_size = bootshim_uefi_size
self.bootshim_padding_size = bootshim_padding_size
def merge(self, target_b):
if self.device is None:
self.device = target_b.device
if self.silicon is None:
self.silicon = target_b.silicon
if self.package is None:
self.package = target_b.package
if self.bootshim_uefi_base is None:
self.bootshim_uefi_base = target_b.bootshim_uefi_base
if self.bootshim_uefi_size is None:
self.bootshim_uefi_size = target_b.bootshim_uefi_size
if self.bootshim_padding_size is None:
self.bootshim_padding_size = target_b.bootshim_padding_size
def print_content(self):
print("Target Info: ")
print("device", self.device)
print("silicon", self.silicon)
print("package", self.package)
print("bootshim_uefi_base", self.bootshim_uefi_base)
print("bootshim_uefi_size", self.bootshim_uefi_size)
print("bootshim_padding_size", self.bootshim_padding_size)
def is_system_supported():
return os.name == "posix"
def build_bootshim(this_target):
bootshim_cmd = os.path.abspath("build_boot_shim.sh") + " -a " + str(this_target.bootshim_uefi_base) + " -b " + str(
this_target.bootshim_uefi_size) + " -p " + str(this_target.bootshim_padding_size)
return os.system(bootshim_cmd)
def prepare_build(package_name):
stuart_setup_cmd = "stuart_setup -c " + os.path.join("Platforms", package_name,
"PlatformBuild.py") + " TOOL_CHAIN_TAG=CLANG38"
stuart_update_cmd = "stuart_update -c" + os.path.join("Platforms", package_name,
"PlatformBuild.py") + " TOOL_CHAIN_TAG=CLANG38"
os.system(stuart_setup_cmd)
os.system(stuart_update_cmd)
def update_device_configuration_map(this_target):
# Delete cache.
try:
shutil.rmtree(os.path.join("Build", this_target.package[:-3] + "-AARCH64", "DEBUG_CLANG38", "AARCH64", "QcomPkg",
"PlatformPei"))
except FileNotFoundError:
print("First Building...")
# Get Configuration Map Path.
src_path = os.path.join("Platforms", this_target.package, "Device", this_target.device, "Include", "Configuration",
"DeviceConfigurationMap.h")
des_path = os.path.join("Silicon", "QC", this_target.silicon, "QcomPkg", "Include", "Configuration",
"DeviceConfigurationMap.h")
# Copy Header to destination Place.
shutil.copy(src_path, des_path)
def get_devices_list(package_name):
return os.listdir(os.path.join("Platforms", package_name, "Device"))
def get_silicons_list():
return os.listdir("Silicon/QC")
# Check is args provided by you available.
def check_args(this_target):
usage = "Usage: build_uefi.sh -d <target_device> -s <target_silicon> \n"
available_devices_msg = "Available devices:"
available_silicons_msg = "Available silicons:"
available_silicons_list = get_silicons_list()
help_msg = "See \033[32mhttps://github.com/woa-msmnile/msmnilePkg#target-list\033[0m for more details."
if this_target.silicon is not None and this_target.package is not None:
current_silicon_msg = "\033[33mCurrent silicon: " + this_target.silicon + "\033[0m"
available_devices_list = get_devices_list(this_target.package)
# check if target_device illegal
device_available = False
for this_device in available_devices_list:
if this_device == this_target.device or this_target.device == "all":
device_available = True
return device_available
# if illegal, print all supported devices.
if not device_available:
print(usage)
print(available_devices_msg)
print(current_silicon_msg)
for this_device in available_devices_list:
print("\t" + this_device)
if this_target.device is None:
print("\nPlease provide target device.")
else:
print("\nThe target device \033[1;33;41m" + this_target.device + "\033[0m is not supported.")
exit(help_msg)
elif this_target.package is None: # if package == None, that means parse failed before,
# which means silicon provided is unsupported.
print(usage)
print(available_silicons_msg)
for this_silicon in available_silicons_list:
print("\t" + this_silicon)
exit(help_msg)
print()
def device_error_exit(device_name, possible_devices_list):
usage = "Usage: build_uefi.sh -d <target_device> \n"
not_found_msg = "Target device not found."
help_msg = "See \033[32mhttps://github.com/woa-msmnile/msmnilePkg#target-list\033[0m for more details."
print(usage)
if not possible_devices_list:
print(not_found_msg)
else:
possible_devices_msg = "Target device \033[31m" + device_name + "\033[0m not found, did you mean: "
print(possible_devices_msg)
for dev_name in possible_devices_list:
print('\t' + dev_name)
print()
print(help_msg)
exit()
# This function get a file object and return a target object.
# The file must be a json file.
def parse_cfg(pfile):
this_target = Target(None, None, None, None, None, None)
cfg_dict = json.load(pfile)
this_target.silicon = cfg_dict["silicon"]
this_target.package = cfg_dict["package"]
this_target.bootshim_uefi_base = cfg_dict["bootshim"]["UEFI_BASE"]
this_target.bootshim_uefi_size = cfg_dict["bootshim"]["UEFI_SIZE"]
this_target.bootshim_padding_size = cfg_dict["bootshim"]["PADDING_SIZE"]
return this_target
# Build uefi for a single device
def build_single_device(this_target):
# Print args
this_target.print_content()
# Check args
check_args(this_target)
# Prepare Environment
build_bootshim(this_target)
update_device_configuration_map(this_target)
prepare_build(this_target.package)
# Start Actual Build
os.system("stuart_build -c" + os.path.join("Platforms", this_target.package, "PlatformBuild.py")
+ " TOOL_CHAIN_TAG=CLANG38 TARGET_DEVICE=" + this_target.device)
# Build uefi for all devices in one silicon.
def build_all_devices(this_target):
# device == "all" here.
check_args(this_target)
device_list = get_devices_list(this_target.package)
for device_name in device_list:
this_target.device = device_name
build_single_device(this_target)
# Build all uefi for all devices.
def build_all_silicons(all_the_targets):
# silicon =="all" here.
for this_target in all_the_targets:
this_target.device = "all"
build_all_devices(this_target)
# Align all strings in a list to a fixed length.
# The rest part will be fill with placeholder
# and return a list.
def string_to_fixed_len(this_list, max_length, ph=" "):
if max_length < max([len(i) for i in this_list]):
max_length = max([len(i) for i in this_list])
# Find the longest device name.
aligned_string_list = []
for this_string in this_list:
# Fill the rest part with ' '
aligned_string_list.append(this_string + (max_length - len(this_string)) * ph)
return aligned_string_list
# Find out all devices.
def find_device_by_name(device_name):
if device_name is None:
return None
# Get all targets from cfg files.
this_all_targets = []
get_all_target(this_all_targets)
for this_target in this_all_targets:
device_list = get_devices_list(this_target.package)
if device_list.count(device_name): # count != 0
des_target = this_target
des_target.device = device_name
return des_target
# if the given name was not found, provide a guess by calculating
# Levenshtein distance
possible_dict = {}
device_list = []
for this_target in this_all_targets:
device_list.extend(get_devices_list(this_target.package))
device_list = string_to_fixed_len(device_list, 0)
for device_name_in_list in device_list:
possible_dict[device_name_in_list] = Levenshtein.distance(device_name, device_name_in_list)
# Sort
possible_dict = dict(sorted(possible_dict.items(), key=lambda x: x[1], reverse=False))
possible_list = []
for i in range(len(possible_dict)):
if list(possible_dict.values())[i] - list(possible_dict.values())[0] < 1:
possible_list.append(list(possible_dict.keys())[i])
# Debug Use
# for _key,_val in possible_dict.items():
# print(_key, _val)
#
# print()
# for i in possible_list:
# print(i)
return possible_list
# Parse config files in folder.
def get_all_target(this_all_targets):
config_dir = "build_cfg"
config_list = os.listdir(config_dir)
for this_config in config_list:
if this_config[-5:] == ".json":
pfile = open(os.path.join(config_dir, this_config), "r")
this_all_targets.append(parse_cfg(pfile))
pfile.close()
# main
if __name__ == '__main__':
# Check host os
if not is_system_supported():
exit("Building is not supported on your host system!")
# Parse Args
parser = argparse.ArgumentParser(description='Build Uefi for target device.')
parser.add_argument('-d', type=str, default=None, help="target device")
parser.add_argument('-s', type=str, default=None, help="target silicon")
args = parser.parse_args()
# Initial target object
current_target = Target(args.d, args.s, None, None, None, None)
destination_target = find_device_by_name(current_target.device) if (
current_target.silicon != "all" and current_target.device != "all") else current_target
if type(destination_target) != Target:
device_error_exit(current_target.device, destination_target) # destination_target is possible_device_list here.
current_target.merge(destination_target)
# Parse Config Files
all_targets = []
get_all_target(all_targets)
# Build all devices in one silicon
if current_target.silicon == "all":
build_all_silicons(all_targets)
elif current_target.device == "all":
# Find current target from config and merge.
for the_target in all_targets:
if the_target.silicon == current_target.silicon:
current_target.merge(the_target)
break
build_all_devices(current_target)
else:
build_single_device(current_target)