-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvce-1.0-fw-convertor.py
executable file
·72 lines (64 loc) · 3.09 KB
/
vce-1.0-fw-convertor.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
#!/usr/bin/python -utt
# Create a VCE 1.0 firmware using the new format for amdgpu, which simply adds a header with some information.
# This work is based on Piotr Redlewski's work.
#
# Structure of the header is as follow:
#
#struct common_firmware_header {
# uint32_t size_bytes; /* size of the entire header with full offset+image(s) in bytes: 256+original firmware's length */
# uint32_t header_size_bytes; /* size of just the header's structure in bytes: 32 */
# uint16_t header_version_major; /* header version: 1 */
# uint16_t header_version_minor; /* header version: 0 */
# uint16_t ip_version_major; /* IP version: 1 */
# uint16_t ip_version_minor; /* IP version: 0 */
# uint32_t ucode_version;
# uint32_t ucode_size_bytes; /* size of ucode in bytes: original firmware's length */
# uint32_t ucode_array_offset_bytes; /* payload offset from the start of the header: 256 */
# uint32_t crc32; /* crc32 checksum of the payload */
#};
import sys
import struct
import binascii
def payload_crc32_checksum(payload_file):
crc32_checksum = 0
payload = open(payload_file, mode='rb')
for eachLine in payload:
crc32_checksum = binascii.crc32(eachLine, crc32_checksum)
payload.close
return (crc32_checksum & 0xFFFFFFFF)
with open(sys.argv[1], mode='rb') as file:
fileContent = file.read()
header_size_bytes = 32
header_version_major = 1
header_version_minor = 0
ip_version_major = 1
ip_version_minor = 0
ucode_version = 1
ucode_size_bytes = len(fileContent)
ucode_array_offset_bytes = 256
firmware_size_bytes = ucode_size_bytes + ucode_array_offset_bytes
crc32 = payload_crc32_checksum(sys.argv[1])
cfh_struct_format = "IIHHHHIIII"
print ("Header's properties to be added to original VCE firmware [{}]".format(sys.argv[1]))
print ("Total size of new firmware [{}]: {}B [uint32]".format(sys.argv[2], firmware_size_bytes))
print ("Header's size: {}B [uint32]".format(header_size_bytes))
print ("Header's version: {}.{} [2*uint16]]".format(header_version_major, header_version_minor))
print ("IP [VCE] version: {}.{} [2*uint16]".format(ip_version_major, ip_version_minor))
print ("uCode version: {} [uint32]".format(ucode_version))
print ("uCode's size: {}B [uint32]".format(ucode_size_bytes))
print ("uCode's offset: {}B [uint32]".format(ucode_array_offset_bytes))
print ("uCode's CRC32 checksum: {} [uint32]".format(crc32))
with open(sys.argv[2], mode='wb') as output:
output.write(struct.pack(cfh_struct_format,
firmware_size_bytes,
header_size_bytes,
header_version_major,
header_version_minor,
ip_version_major,
ip_version_minor,
ucode_version,
ucode_size_bytes,
ucode_array_offset_bytes,
crc32))
output.write(bytearray(ucode_array_offset_bytes - header_size_bytes))
output.write(fileContent)