Skip to content

Commit a5afad0

Browse files
unpacker: add broadcom SAO image support
Co-authored-by: Jörg Stucke <joerg.stucke@fkie.fraunhofer.de>
1 parent 21fb851 commit a5afad0

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

fact_extractor/plugins/unpacking/broadcom/__init__.py

Whitespace-only changes.

fact_extractor/plugins/unpacking/broadcom/code/__init__.py

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
This plugin unpacks Broadcom SAO images.
3+
'''
4+
5+
import os
6+
from pathlib import Path
7+
from common_helper_files import write_binary_to_file
8+
9+
NAME = "Broadcom SAO"
10+
MIME_PATTERNS = ['firmware/broadcom-sao']
11+
VERSION = '0.1'
12+
13+
def unpack_function(file_path, tmp_dir):
14+
'''
15+
file_path specifies the input file.
16+
tmp_dir must be used to store the extracted files.
17+
Optional: Return a dict with meta information
18+
'''
19+
20+
FILE_SIZE = os.stat(file_path).st_size
21+
22+
try:
23+
with open(file_path, 'rb') as f:
24+
while (f.tell() < FILE_SIZE):
25+
f.read(8) # skip 8 bytes to part name offset
26+
name = f.read(4).decode('utf-8')
27+
f.read(4) # skip 4 more bytes to size offset
28+
size = int.from_bytes(f.read(4), 'big')
29+
f.read(44) # skip rest of header
30+
data = f.read(size)
31+
outfile = Path(tmp_dir) / name
32+
write_binary_to_file(data, outfile)
33+
34+
except IOError as io_error:
35+
return {'output': 'failed to read file: {}'.format(str(io_error))}
36+
37+
38+
return {
39+
'output': 'successfully unpacked Broadcom SAO image'
40+
}
41+
42+
# ----> Do not edit below this line <----
43+
def setup(unpack_tool):
44+
for item in MIME_PATTERNS:
45+
unpack_tool.register_plugin(item, (unpack_function, NAME, VERSION))

fact_extractor/plugins/unpacking/broadcom/test/__init__.py

Whitespace-only changes.
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
3+
from test.unit.unpacker.test_unpacker import TestUnpackerBase
4+
5+
TEST_DATA_DIR = Path(__file__).parent / 'data'
6+
7+
class TestBroadcomSAOUnpacker(TestUnpackerBase):
8+
9+
def test_unpacker_selection_generic(self):
10+
self.check_unpacker_selection('firmware/broadcom-sao', 'Broadcom SAO')
11+
12+
def test_extraction(self):
13+
test_file_path = Path(TEST_DATA_DIR) / 'broadcom-sao.bin'
14+
extracted_files, meta_data = self.unpacker.extract_files_from_file(str(test_file_path), self.tmp_dir.name)
15+
16+
assert meta_data['plugin_used'] == 'Broadcom SAO', 'wrong plugin applied'
17+
18+
assert len(extracted_files) == 4, 'not all files extracted'
19+
assert all(
20+
Path(element).name in ['META', 'DTBB', 'KRNL', 'RTFS'] for element in extracted_files
21+
), 'not all files extracted'

0 commit comments

Comments
 (0)