Skip to content

Commit bf65e87

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

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-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+
from pathlib import Path
6+
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+
14+
def unpack_function(file_path, tmp_dir):
15+
"""
16+
file_path specifies the input file.
17+
tmp_dir must be used to store the extracted files.
18+
Optional: Return a dict with meta information
19+
"""
20+
21+
file = Path(file_path)
22+
file_size = file.stat().st_size
23+
24+
try:
25+
with file.open('rb') as f:
26+
while f.tell() < file_size:
27+
f.read(8) # skip 8 bytes to part name offset
28+
name = f.read(4).decode('utf-8')
29+
f.read(4) # skip 4 more bytes to size offset
30+
size = int.from_bytes(f.read(4), 'big')
31+
f.read(44) # skip rest of header
32+
data = f.read(size)
33+
outfile = Path(tmp_dir) / name
34+
write_binary_to_file(data, outfile)
35+
36+
except OSError as io_error:
37+
return {'output': f'failed to read file: {io_error!s}'}
38+
39+
return {'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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from pathlib import Path
2+
3+
from test.unit.unpacker.test_unpacker import TestUnpackerBase
4+
5+
TEST_DATA_DIR = Path(__file__).parent / 'data'
6+
7+
EXPECTED_FILE_COUNT = 4
8+
9+
10+
class TestBroadcomSAOUnpacker(TestUnpackerBase):
11+
def test_unpacker_selection_generic(self):
12+
self.check_unpacker_selection('firmware/broadcom-sao', 'Broadcom SAO')
13+
14+
def test_extraction(self):
15+
test_file_path = Path(TEST_DATA_DIR) / 'broadcom-sao.bin'
16+
extracted_files, meta_data = self.unpacker.extract_files_from_file(str(test_file_path), self.tmp_dir.name)
17+
18+
assert meta_data['plugin_used'] == 'Broadcom SAO', 'wrong plugin applied'
19+
20+
assert len(extracted_files) == EXPECTED_FILE_COUNT, 'not all files extracted'
21+
assert all(
22+
Path(element).name in ['META', 'DTBB', 'KRNL', 'RTFS'] for element in extracted_files
23+
), 'not all files extracted'

0 commit comments

Comments
 (0)