azuresphere_imagemetadata 0.0.1__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- azuresphere_imagemetadata/__init__.py +4 -0
- azuresphere_imagemetadata/certificates/app_test_sign.pfx +0 -0
- azuresphere_imagemetadata/certificates/tp_app_test_sign.cer +14 -0
- azuresphere_imagemetadata/guid_utils.py +35 -0
- azuresphere_imagemetadata/image.py +44 -0
- azuresphere_imagemetadata/image_metadata.py +254 -0
- azuresphere_imagemetadata/metadata_headers/__init__.py +4 -0
- azuresphere_imagemetadata/metadata_headers/image_metadata.py +62 -0
- azuresphere_imagemetadata/metadata_headers/image_metadata_section.py +51 -0
- azuresphere_imagemetadata/metadata_sections/__init__.py +4 -0
- azuresphere_imagemetadata/metadata_sections/abi.py +153 -0
- azuresphere_imagemetadata/metadata_sections/compression.py +108 -0
- azuresphere_imagemetadata/metadata_sections/debug.py +65 -0
- azuresphere_imagemetadata/metadata_sections/identity.py +269 -0
- azuresphere_imagemetadata/metadata_sections/image_policy.py +108 -0
- azuresphere_imagemetadata/metadata_sections/internal/__init__.py +4 -0
- azuresphere_imagemetadata/metadata_sections/internal/capabilities.py +187 -0
- azuresphere_imagemetadata/metadata_sections/internal/onebl.py +83 -0
- azuresphere_imagemetadata/metadata_sections/metadata.py +38 -0
- azuresphere_imagemetadata/metadata_sections/metadata_id.py +84 -0
- azuresphere_imagemetadata/metadata_sections/not_implemented.py +25 -0
- azuresphere_imagemetadata/metadata_sections/required_offset.py +54 -0
- azuresphere_imagemetadata/metadata_sections/revocation.py +57 -0
- azuresphere_imagemetadata/metadata_sections/signature.py +98 -0
- azuresphere_imagemetadata/metadata_sections/temporary_image.py +97 -0
- azuresphere_imagemetadata/metadata_sections/utils.py +93 -0
- azuresphere_imagemetadata/signing/__init__.py +4 -0
- azuresphere_imagemetadata/signing/app_development.py +78 -0
- azuresphere_imagemetadata/signing/der_encoded.py +89 -0
- azuresphere_imagemetadata/signing/signing_info.py +102 -0
- azuresphere_imagemetadata-0.0.1.dist-info/LICENSE +21 -0
- azuresphere_imagemetadata-0.0.1.dist-info/METADATA +19 -0
- azuresphere_imagemetadata-0.0.1.dist-info/RECORD +35 -0
- azuresphere_imagemetadata-0.0.1.dist-info/WHEEL +6 -0
- azuresphere_imagemetadata-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
# --------------------------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License. See LICENSE in the project root for license information.
|
|
4
|
+
# --------------------------------------------------------------------------------------------
|
|
Binary file
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
-----BEGIN CERTIFICATE-----
|
|
2
|
+
MIICLTCCAdOgAwIBAgIJAMCRCPSaH6lwMAoGCCqGSM49BAMCMHMxCzAJBgNVBAYT
|
|
3
|
+
AlVTMRMwEQYDVQQIDApXYXNoaW5ndG9uMRAwDgYDVQQHDAdSZWRtb25kMR4wHAYD
|
|
4
|
+
VQQKDBVNaWNyb3NvZnQgQ29ycG9yYXRpb24xHTAbBgNVBAMMFEFwcCBUZXN0IFNp
|
|
5
|
+
Z25pbmcgS2V5MB4XDTE3MDQxMjE3MTY1OFoXDTI3MDQxMDE3MTY1OFowczELMAkG
|
|
6
|
+
A1UEBhMCVVMxEzARBgNVBAgMCldhc2hpbmd0b24xEDAOBgNVBAcMB1JlZG1vbmQx
|
|
7
|
+
HjAcBgNVBAoMFU1pY3Jvc29mdCBDb3Jwb3JhdGlvbjEdMBsGA1UEAwwUQXBwIFRl
|
|
8
|
+
c3QgU2lnbmluZyBLZXkwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATt8jPADTEU
|
|
9
|
+
HY8/VjNDQMzYwWn7Z+LsVkv4pe9Yaxw/e8GIyiCHAebXx3VqEyY4/pjJoCAHQD3l
|
|
10
|
+
xCLmp+gmpL0ko1AwTjAdBgNVHQ4EFgQUY7CP8Pj32XhY4zObUHBS+h3MnoswHwYD
|
|
11
|
+
VR0jBBgwFoAUY7CP8Pj32XhY4zObUHBS+h3MnoswDAYDVR0TBAUwAwEB/zAKBggq
|
|
12
|
+
hkjOPQQDAgNIADBFAiAE/4dlH3mJzDGHkNzcBHs3AuOVwXk9tB74Ovf7qKyCWAIh
|
|
13
|
+
ALxSrGD9N2kWyYb78DlUlQFbJ6uECIZbgSnjc+/OnnsA
|
|
14
|
+
-----END CERTIFICATE-----
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# --------------------------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License. See LICENSE in the project root for license information.
|
|
4
|
+
# --------------------------------------------------------------------------------------------
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
Provides helper functions for working with Azure Sphere GUIDs.
|
|
8
|
+
Azure Sphere GUIDs have historically incorrectly ordered bytes.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def format_guid_as_string(guid):
|
|
13
|
+
"""
|
|
14
|
+
Formats a GUID as a string observing the same format as dotnet code.
|
|
15
|
+
"""
|
|
16
|
+
return f"{guid[0:8]}-{guid[8:12]}-{guid[12:16]}-{guid[16:20]}-{guid[20:32]}"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def to_guid(guid_bytes: bytes):
|
|
20
|
+
"""
|
|
21
|
+
Correctly orders a guid stored in an image package e.g. image/component uids.
|
|
22
|
+
"""
|
|
23
|
+
guid = [0] * 16
|
|
24
|
+
|
|
25
|
+
for i in range(0, 4):
|
|
26
|
+
guid[i] = guid_bytes[3 - i]
|
|
27
|
+
|
|
28
|
+
guid[4] = guid_bytes[5]
|
|
29
|
+
guid[5] = guid_bytes[4]
|
|
30
|
+
guid[6] = guid_bytes[7]
|
|
31
|
+
guid[7] = guid_bytes[6]
|
|
32
|
+
|
|
33
|
+
guid[8:] = guid_bytes[8:]
|
|
34
|
+
|
|
35
|
+
return bytearray(guid).hex()
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# --------------------------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License. See LICENSE in the project root for license information.
|
|
4
|
+
# --------------------------------------------------------------------------------------------
|
|
5
|
+
|
|
6
|
+
from .image_metadata import ImageMetadata
|
|
7
|
+
from .signing.app_development import AppDevelopmentSigner
|
|
8
|
+
from .signing.der_encoded import DEREncodedSignature
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Image:
|
|
12
|
+
def __init__(self, data=None):
|
|
13
|
+
self.signature = None
|
|
14
|
+
self.data = data
|
|
15
|
+
self.metadata = ImageMetadata(None)
|
|
16
|
+
if data is not None:
|
|
17
|
+
self.deserialize(data)
|
|
18
|
+
|
|
19
|
+
def as_bytes(self, signer=AppDevelopmentSigner(), include_signature=False):
|
|
20
|
+
new_data = self.data + self.metadata.serialize()
|
|
21
|
+
if include_signature:
|
|
22
|
+
if self.signature is None:
|
|
23
|
+
self.signature = DEREncodedSignature.to_stored_signature(
|
|
24
|
+
signer.sign_data(new_data)
|
|
25
|
+
)
|
|
26
|
+
return new_data + self.signature
|
|
27
|
+
return new_data
|
|
28
|
+
|
|
29
|
+
def deserialize(self, data):
|
|
30
|
+
try:
|
|
31
|
+
self.metadata = ImageMetadata(data)
|
|
32
|
+
except Exception as e:
|
|
33
|
+
print(f"Metadata parsing failed: {e}")
|
|
34
|
+
# metadata parsing could not correctly find the metadata
|
|
35
|
+
return
|
|
36
|
+
|
|
37
|
+
self.data = data[: self.metadata.start_of_metadata]
|
|
38
|
+
self.signature = data[self.metadata.end_of_metadata :]
|
|
39
|
+
|
|
40
|
+
if len(self.signature) != self.metadata.signature_size:
|
|
41
|
+
raise Exception("Signature size mismatch")
|
|
42
|
+
|
|
43
|
+
def serialize(self, signer=AppDevelopmentSigner(), include_signature=True):
|
|
44
|
+
return self.as_bytes(signer, include_signature)
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# --------------------------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License. See LICENSE in the project root for license information.
|
|
4
|
+
# --------------------------------------------------------------------------------------------
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
A class that deserialises/parses image metadata given an Azure Sphere image.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import struct
|
|
11
|
+
from .metadata_sections.utils import class_from_section_id
|
|
12
|
+
from .metadata_sections.internal.onebl import OneBlInformationSection
|
|
13
|
+
from .metadata_headers.image_metadata import ImageMetadataHeader
|
|
14
|
+
from .metadata_headers.image_metadata_section import ImageMetadataSectionHeader
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ImageMetadata:
|
|
18
|
+
"""
|
|
19
|
+
Image metadata contains a header, followed by a number of sections.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self, data):
|
|
23
|
+
self.sections = []
|
|
24
|
+
self.section_headers = []
|
|
25
|
+
self.header = None
|
|
26
|
+
self.onebl = None
|
|
27
|
+
self.end_of_metadata = 0
|
|
28
|
+
self.start_of_metadata = 0
|
|
29
|
+
self.signature_size = 0
|
|
30
|
+
self.raw_data = bytes()
|
|
31
|
+
if data is not None:
|
|
32
|
+
self.deserialize(data)
|
|
33
|
+
|
|
34
|
+
def deserialize(self, byte_data):
|
|
35
|
+
"""
|
|
36
|
+
Image metadata is placed at the end of the file.
|
|
37
|
+
"""
|
|
38
|
+
position = 0
|
|
39
|
+
# The image signature is appended to the end of the metadata and is:
|
|
40
|
+
# 64 bytes, if sha256 is used
|
|
41
|
+
# 96 bytes, if sha384 is used
|
|
42
|
+
possible_signature_sizes = [64, 96]
|
|
43
|
+
for possible_signature_size in possible_signature_sizes:
|
|
44
|
+
self.onebl = None # reset onebl on loop as it can be set incorrectly
|
|
45
|
+
minimum_valid_size = (
|
|
46
|
+
possible_signature_size + 4 + ImageMetadataSectionHeader.size()
|
|
47
|
+
)
|
|
48
|
+
metadata_length_offset = possible_signature_size + 4
|
|
49
|
+
onebl_metadata_length_offset = (
|
|
50
|
+
possible_signature_size + 4 + OneBlInformationSection.size()
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
if len(byte_data) < minimum_valid_size:
|
|
54
|
+
continue
|
|
55
|
+
|
|
56
|
+
sig_area_size = possible_signature_size
|
|
57
|
+
end_of_metadata = len(byte_data) - metadata_length_offset
|
|
58
|
+
# length is stored in the four bytes before the signature
|
|
59
|
+
target_length = struct.unpack_from("<I", byte_data, end_of_metadata)[0]
|
|
60
|
+
|
|
61
|
+
if target_length <= 28:
|
|
62
|
+
# This may be a 1BL only section
|
|
63
|
+
if len(byte_data) < minimum_valid_size + OneBlInformationSection.size():
|
|
64
|
+
continue
|
|
65
|
+
|
|
66
|
+
sig_area_size += OneBlInformationSection.size()
|
|
67
|
+
|
|
68
|
+
position = (
|
|
69
|
+
len(byte_data)
|
|
70
|
+
- possible_signature_size
|
|
71
|
+
- OneBlInformationSection.size()
|
|
72
|
+
)
|
|
73
|
+
self.onebl = OneBlInformationSection(
|
|
74
|
+
byte_data[position : position + OneBlInformationSection.size()]
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
end_of_metadata = len(byte_data) - onebl_metadata_length_offset
|
|
78
|
+
position = end_of_metadata
|
|
79
|
+
target_length = struct.unpack_from("<I", byte_data, position)[0]
|
|
80
|
+
|
|
81
|
+
if len(byte_data) < (sig_area_size + target_length):
|
|
82
|
+
# Not enough space, fail
|
|
83
|
+
continue
|
|
84
|
+
|
|
85
|
+
if target_length < ImageMetadataHeader.size():
|
|
86
|
+
# Length too small, fail
|
|
87
|
+
continue
|
|
88
|
+
|
|
89
|
+
header_position = len(byte_data) - sig_area_size - target_length
|
|
90
|
+
self.start_of_metadata = header_position
|
|
91
|
+
|
|
92
|
+
self.image_data = byte_data[0:header_position]
|
|
93
|
+
|
|
94
|
+
header = ImageMetadataHeader(
|
|
95
|
+
byte_data[
|
|
96
|
+
header_position : header_position + ImageMetadataHeader.size()
|
|
97
|
+
]
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
self.header = header
|
|
101
|
+
|
|
102
|
+
if header.section_count == 0:
|
|
103
|
+
continue
|
|
104
|
+
|
|
105
|
+
section_addr = header_position + ImageMetadataHeader.size()
|
|
106
|
+
|
|
107
|
+
for _ in range(0, header.section_count):
|
|
108
|
+
if section_addr >= end_of_metadata:
|
|
109
|
+
continue
|
|
110
|
+
|
|
111
|
+
section_header_data = byte_data[
|
|
112
|
+
section_addr : section_addr + ImageMetadataSectionHeader.size()
|
|
113
|
+
]
|
|
114
|
+
section_header = ImageMetadataSectionHeader(section_header_data)
|
|
115
|
+
|
|
116
|
+
self.section_headers += [section_header]
|
|
117
|
+
|
|
118
|
+
if section_header.data_length == 0:
|
|
119
|
+
continue
|
|
120
|
+
|
|
121
|
+
if (
|
|
122
|
+
section_addr
|
|
123
|
+
+ ImageMetadataSectionHeader.size()
|
|
124
|
+
+ section_header.data_length
|
|
125
|
+
> end_of_metadata
|
|
126
|
+
):
|
|
127
|
+
continue
|
|
128
|
+
|
|
129
|
+
section_addr += ImageMetadataSectionHeader.size()
|
|
130
|
+
|
|
131
|
+
self.sections += [
|
|
132
|
+
class_from_section_id(section_header.section_id)(
|
|
133
|
+
byte_data[
|
|
134
|
+
section_addr : section_addr + section_header.data_length
|
|
135
|
+
]
|
|
136
|
+
)
|
|
137
|
+
]
|
|
138
|
+
|
|
139
|
+
section_addr += section_header.data_length
|
|
140
|
+
|
|
141
|
+
self.end_of_metadata = (
|
|
142
|
+
end_of_metadata + 4
|
|
143
|
+
) # include the length in the metadata
|
|
144
|
+
self.signature_size = possible_signature_size
|
|
145
|
+
self.raw_data = byte_data[self.start_of_metadata : self.end_of_metadata]
|
|
146
|
+
return
|
|
147
|
+
raise Exception("Invalid metadata")
|
|
148
|
+
|
|
149
|
+
def serialize(self):
|
|
150
|
+
"""
|
|
151
|
+
Serialize the image metadata to a byte array.
|
|
152
|
+
"""
|
|
153
|
+
header = ImageMetadataHeader()
|
|
154
|
+
header.section_count = len(self.sections)
|
|
155
|
+
|
|
156
|
+
out = header.serialize()
|
|
157
|
+
|
|
158
|
+
for s in self.sections:
|
|
159
|
+
sec_bytes = s.serialize()
|
|
160
|
+
sec_header = ImageMetadataSectionHeader()
|
|
161
|
+
sec_header.section_id = s.section_id
|
|
162
|
+
sec_header.data_length = len(sec_bytes)
|
|
163
|
+
out += sec_header.serialize()
|
|
164
|
+
out += sec_bytes
|
|
165
|
+
|
|
166
|
+
# Add the length of the metadata to the end
|
|
167
|
+
# Include the length of the length field itself (4 bytes)
|
|
168
|
+
out += struct.pack("<I", len(out) + 4)
|
|
169
|
+
return out
|
|
170
|
+
|
|
171
|
+
def add_section(self, section):
|
|
172
|
+
"""
|
|
173
|
+
Add a section to the metadata.
|
|
174
|
+
"""
|
|
175
|
+
self.sections += [section]
|
|
176
|
+
|
|
177
|
+
def sections_by_name(self, name):
|
|
178
|
+
"""
|
|
179
|
+
Returns a list of sections with the given name.
|
|
180
|
+
"""
|
|
181
|
+
return [section for section in self.sections if section.name == name]
|
|
182
|
+
|
|
183
|
+
def sections_by_id(self, section_id):
|
|
184
|
+
"""
|
|
185
|
+
Returns a list of sections with the given id.
|
|
186
|
+
"""
|
|
187
|
+
return [
|
|
188
|
+
section for section in self.sections if section.section_id == section_id
|
|
189
|
+
]
|
|
190
|
+
|
|
191
|
+
def remove_section(self, section):
|
|
192
|
+
"""
|
|
193
|
+
Removes a section from the metadata.
|
|
194
|
+
"""
|
|
195
|
+
self.sections = [s for s in self.sections if s != section]
|
|
196
|
+
|
|
197
|
+
def str_dotnet(self) -> str:
|
|
198
|
+
"""
|
|
199
|
+
Returns a string representation of the metadata using the same formatting errors as
|
|
200
|
+
the dotnet tool.
|
|
201
|
+
"""
|
|
202
|
+
out_lines = []
|
|
203
|
+
# to match dotnet output exactly, each section header has \r\n at the end of it
|
|
204
|
+
# each content line has \r\r\n at the end of it.
|
|
205
|
+
# This is likely a bug in the dotnet code where the lines were split assuming \n
|
|
206
|
+
# as the line ending when infact the line ending was \r\n
|
|
207
|
+
# We will match it for testing purposes.
|
|
208
|
+
for section in self.sections:
|
|
209
|
+
out_lines.append(f" Section: {section.name}\r\n")
|
|
210
|
+
|
|
211
|
+
# the formatting error was not globally applied to all sections,
|
|
212
|
+
# so we need to special case this one
|
|
213
|
+
if section.name == "Required Offset":
|
|
214
|
+
for line in str(section).split("\n"):
|
|
215
|
+
if line.strip() == "":
|
|
216
|
+
continue
|
|
217
|
+
out_lines.append(f" {line}\r\n")
|
|
218
|
+
else:
|
|
219
|
+
for line in str(section).split("\n"):
|
|
220
|
+
if line.strip() == "":
|
|
221
|
+
continue
|
|
222
|
+
out_lines.append(f" {line}\r\r\n")
|
|
223
|
+
|
|
224
|
+
if self.onebl is not None:
|
|
225
|
+
out_lines.append(" Section: 1BL Information\r\n")
|
|
226
|
+
for line in str(self.onebl).split("\n"):
|
|
227
|
+
if line.strip() == "":
|
|
228
|
+
continue
|
|
229
|
+
out_lines.append(f" {line}\r\r\n")
|
|
230
|
+
|
|
231
|
+
out_str = "".join(out_lines)
|
|
232
|
+
return out_str + "\r\n"
|
|
233
|
+
|
|
234
|
+
def __str__(self):
|
|
235
|
+
"""
|
|
236
|
+
Returns a correctly formatted string representation of the metadata.
|
|
237
|
+
"""
|
|
238
|
+
out_lines = []
|
|
239
|
+
for section in self.sections:
|
|
240
|
+
out_lines.append(f" Section: {section.name}")
|
|
241
|
+
|
|
242
|
+
for line in str(section).split("\n"):
|
|
243
|
+
if line.strip() == "":
|
|
244
|
+
continue
|
|
245
|
+
out_lines.append(f" {line}")
|
|
246
|
+
|
|
247
|
+
if self.onebl is not None:
|
|
248
|
+
out_lines.append(" Section: 1BL Information")
|
|
249
|
+
for line in str(self.onebl).split("\n"):
|
|
250
|
+
if line.strip() == "":
|
|
251
|
+
continue
|
|
252
|
+
out_lines.append(f" {line}")
|
|
253
|
+
|
|
254
|
+
return "\r\n".join(out_lines) + "\r\n"
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
# --------------------------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License. See LICENSE in the project root for license information.
|
|
4
|
+
# --------------------------------------------------------------------------------------------
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# --------------------------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License. See LICENSE in the project root for license information.
|
|
4
|
+
# --------------------------------------------------------------------------------------------
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
Contains definition for the different headers used in image metadata.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import struct
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ImageMetadataHeader:
|
|
14
|
+
"""
|
|
15
|
+
image metadata header
|
|
16
|
+
describes the layout of the image metadata (i.e. number of sections).
|
|
17
|
+
|
|
18
|
+
contains:
|
|
19
|
+
uint32_t magic 0x4d345834
|
|
20
|
+
uint32_t section_count
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
CorrectMagicValue = 0x4D345834
|
|
24
|
+
|
|
25
|
+
def __init__(self, data=None):
|
|
26
|
+
if data is not None:
|
|
27
|
+
self.deserialize(data)
|
|
28
|
+
else:
|
|
29
|
+
self.magic_value = self.CorrectMagicValue
|
|
30
|
+
self.section_count = 0
|
|
31
|
+
|
|
32
|
+
@staticmethod
|
|
33
|
+
def size() -> int:
|
|
34
|
+
"""
|
|
35
|
+
size of the header (8 bytes).
|
|
36
|
+
"""
|
|
37
|
+
return 8
|
|
38
|
+
|
|
39
|
+
def deserialize(self, data: bytearray):
|
|
40
|
+
"""
|
|
41
|
+
deserialize the header and check magic values match.
|
|
42
|
+
"""
|
|
43
|
+
self.magic_value = struct.unpack_from("<I", data, 0)[0]
|
|
44
|
+
self.section_count = struct.unpack_from("<I", data, 4)[0]
|
|
45
|
+
|
|
46
|
+
if self.magic_value != ImageMetadataHeader.CorrectMagicValue:
|
|
47
|
+
raise Exception("ImageMetadataHeader: Invalid magic value")
|
|
48
|
+
|
|
49
|
+
def serialize(self) -> bytearray:
|
|
50
|
+
"""
|
|
51
|
+
serialize the header.
|
|
52
|
+
"""
|
|
53
|
+
return struct.pack("<II", self.magic_value, self.section_count)
|
|
54
|
+
|
|
55
|
+
def __str__(self):
|
|
56
|
+
return (
|
|
57
|
+
"ImageMetadataHeader\n\tmagic_value: "
|
|
58
|
+
+ str(self.magic_value)
|
|
59
|
+
+ "\n\tsection_count: "
|
|
60
|
+
+ str(self.section_count)
|
|
61
|
+
+ "\n"
|
|
62
|
+
)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# --------------------------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License. See LICENSE in the project root for license information.
|
|
4
|
+
# --------------------------------------------------------------------------------------------
|
|
5
|
+
|
|
6
|
+
import struct
|
|
7
|
+
from ..metadata_sections.utils import name_from_section_id
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ImageMetadataSectionHeader:
|
|
11
|
+
"""
|
|
12
|
+
section metadata header describes the upcoming section.
|
|
13
|
+
uint16_t section_id
|
|
14
|
+
uint16 length
|
|
15
|
+
bytes[length]
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def __init__(self, data=None):
|
|
19
|
+
if data is not None:
|
|
20
|
+
self.deserialize(data)
|
|
21
|
+
else:
|
|
22
|
+
self.section_id = 0
|
|
23
|
+
self.data_length = 0
|
|
24
|
+
|
|
25
|
+
@staticmethod
|
|
26
|
+
def size() -> int:
|
|
27
|
+
"""
|
|
28
|
+
size of the section header (4 bytes).
|
|
29
|
+
"""
|
|
30
|
+
return 4
|
|
31
|
+
|
|
32
|
+
def deserialize(self, data: bytearray):
|
|
33
|
+
"""
|
|
34
|
+
deserialize the section header from bytes.
|
|
35
|
+
"""
|
|
36
|
+
self.section_id = struct.unpack_from("<H", data, 0)[0]
|
|
37
|
+
self.data_length = struct.unpack_from("<H", data, 2)[0]
|
|
38
|
+
|
|
39
|
+
def serialize(self) -> bytearray:
|
|
40
|
+
"""
|
|
41
|
+
serialize the section header into bytes.
|
|
42
|
+
"""
|
|
43
|
+
return struct.pack("<HH", self.section_id, self.data_length)
|
|
44
|
+
|
|
45
|
+
def __str__(self) -> str:
|
|
46
|
+
return (
|
|
47
|
+
"ImageMetadataSectionHeader\n\t"
|
|
48
|
+
+ f"section_id: {self.section_id:x}"
|
|
49
|
+
+ f"({name_from_section_id(self.section_id)})\n\t"
|
|
50
|
+
+ f"data_length: {self.data_length}"
|
|
51
|
+
)
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
# --------------------------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License. See LICENSE in the project root for license information.
|
|
4
|
+
# --------------------------------------------------------------------------------------------
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# --------------------------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License. See LICENSE in the project root for license information.
|
|
4
|
+
# --------------------------------------------------------------------------------------------
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
The ABI depends/provides list software dependencies/requirements of a given image.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from .metadata_id import MetadataSectionId
|
|
11
|
+
from .metadata import MetadataSection
|
|
12
|
+
import struct
|
|
13
|
+
from typing import List
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ABIDependsType:
|
|
17
|
+
"""
|
|
18
|
+
The type of dependency.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
NoDependency = 0
|
|
22
|
+
SecureWorldRuntime = 1
|
|
23
|
+
OSRuntime = 2
|
|
24
|
+
ApplicationRuntime = 3
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class ABIIdentity:
|
|
28
|
+
"""
|
|
29
|
+
ABI Identity wraps the AbiDependsType.
|
|
30
|
+
It provides deserialisation and stringification.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
# uint32_t version
|
|
34
|
+
# uint32_t type
|
|
35
|
+
|
|
36
|
+
def __init__(self, data):
|
|
37
|
+
self.deserialize(data)
|
|
38
|
+
|
|
39
|
+
@staticmethod
|
|
40
|
+
def size():
|
|
41
|
+
"""
|
|
42
|
+
The size of the ABI Identity in bytes.
|
|
43
|
+
"""
|
|
44
|
+
return 8
|
|
45
|
+
|
|
46
|
+
def deserialize(self, data):
|
|
47
|
+
"""
|
|
48
|
+
Deserialise the ABI Identity member properties from the given data.
|
|
49
|
+
"""
|
|
50
|
+
self.raw_bytes = data
|
|
51
|
+
self.version = struct.unpack_from("<I", data, 0)[0]
|
|
52
|
+
self.type = struct.unpack_from("<I", data, 4)[0]
|
|
53
|
+
|
|
54
|
+
def serialize(self):
|
|
55
|
+
return struct.pack("<II", self.version, self.type)
|
|
56
|
+
|
|
57
|
+
def get_type_name(self):
|
|
58
|
+
"""
|
|
59
|
+
Get the stringified name of the ABI Identity type.
|
|
60
|
+
"""
|
|
61
|
+
if self.type == ABIDependsType.NoDependency:
|
|
62
|
+
return "NoDependency"
|
|
63
|
+
elif self.type == ABIDependsType.SecureWorldRuntime:
|
|
64
|
+
return "SecureWorldRuntime"
|
|
65
|
+
elif self.type == ABIDependsType.OSRuntime:
|
|
66
|
+
return "OSRuntime"
|
|
67
|
+
elif self.type == ABIDependsType.ApplicationRuntime:
|
|
68
|
+
return "ApplicationRuntime"
|
|
69
|
+
else:
|
|
70
|
+
raise Exception("Unknown AbiDependsIdentity type: %d" % (self.type))
|
|
71
|
+
|
|
72
|
+
def __str__(self):
|
|
73
|
+
return f"{self.get_type_name()}@{self.version}"
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class ABISection(MetadataSection):
|
|
77
|
+
"""
|
|
78
|
+
The superclass of ABIProvides/ABIDepends.
|
|
79
|
+
It provides functionality common to both.
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
versions: List[ABIIdentity]
|
|
83
|
+
version_count: int
|
|
84
|
+
|
|
85
|
+
def __init__(self, name, section_id, data):
|
|
86
|
+
super().__init__(name, section_id, data)
|
|
87
|
+
if data is not None:
|
|
88
|
+
self.deserialize(data)
|
|
89
|
+
else:
|
|
90
|
+
self.version_count = 0
|
|
91
|
+
self.versions = []
|
|
92
|
+
|
|
93
|
+
@staticmethod
|
|
94
|
+
def size():
|
|
95
|
+
"""
|
|
96
|
+
The size of the ABI section in bytes.
|
|
97
|
+
"""
|
|
98
|
+
return 4
|
|
99
|
+
|
|
100
|
+
def deserialize(self, data):
|
|
101
|
+
"""
|
|
102
|
+
Deserialise the ABI section member properties from the given data.
|
|
103
|
+
"""
|
|
104
|
+
self.version_count = struct.unpack_from("<I", data, 0)[0]
|
|
105
|
+
self.versions = []
|
|
106
|
+
offset = 4
|
|
107
|
+
for _ in range(self.version_count):
|
|
108
|
+
self.versions += [ABIIdentity(data[offset : offset + ABIIdentity.size()])]
|
|
109
|
+
offset += ABIIdentity.size()
|
|
110
|
+
|
|
111
|
+
def serialize(self):
|
|
112
|
+
"""
|
|
113
|
+
Serialise the ABI section member properties.
|
|
114
|
+
"""
|
|
115
|
+
out = struct.pack("<I", len(self.versions))
|
|
116
|
+
for v in self.versions:
|
|
117
|
+
out += v.serialize()
|
|
118
|
+
return out
|
|
119
|
+
|
|
120
|
+
def _to_string(self, prefix):
|
|
121
|
+
"""
|
|
122
|
+
Helper function to stringify the ABI section.
|
|
123
|
+
"""
|
|
124
|
+
out_lines = []
|
|
125
|
+
for v in self.versions:
|
|
126
|
+
out_lines.append(f"{prefix}{v}")
|
|
127
|
+
return "\n".join(out_lines)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
class ABIProvidesSection(ABISection):
|
|
131
|
+
"""
|
|
132
|
+
The ABI Provides section lists any binary dependencies that the image
|
|
133
|
+
provides.
|
|
134
|
+
"""
|
|
135
|
+
|
|
136
|
+
def __init__(self, data=None):
|
|
137
|
+
super().__init__("ABI Provides", MetadataSectionId.ABIProvides, data)
|
|
138
|
+
|
|
139
|
+
def __str__(self):
|
|
140
|
+
return self._to_string("Provides: ")
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
class ABIDependsSection(ABISection):
|
|
144
|
+
"""
|
|
145
|
+
The ABI Depends section lists any binary dependencies that the image
|
|
146
|
+
requires.
|
|
147
|
+
"""
|
|
148
|
+
|
|
149
|
+
def __init__(self, data=None):
|
|
150
|
+
super().__init__("ABI Depends", MetadataSectionId.ABIDepends, data)
|
|
151
|
+
|
|
152
|
+
def __str__(self):
|
|
153
|
+
return self._to_string("Depends: ")
|