-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from contextlib import redirect_stdout | ||
from io import StringIO | ||
from pathlib import Path | ||
|
||
from pakler import PAK | ||
from pycramfs import Cramfs | ||
from pycramfs.extract import extract_dir as extract_cramfs | ||
from PySquashfsImage import SquashFsImage | ||
from PySquashfsImage.extract import extract_dir as extract_squashfs | ||
from ubireader.ubifs import ubifs | ||
from ubireader.ubifs.output import extract_files as extract_ubifs | ||
|
||
from reolinkfw import FS_SECTIONS, ROOTFS_SECTIONS | ||
from reolinkfw.util import ( | ||
DummyLEB, | ||
get_fs_from_ubi, | ||
is_cramfs, | ||
is_squashfs, | ||
is_ubi, | ||
is_ubifs | ||
) | ||
|
||
|
||
def extract_file_system(fs_bytes, dest: Path = None): | ||
dest = (Path.cwd() / "reolink_fs") if dest is None else dest | ||
dest.mkdir(parents=True, exist_ok=True) | ||
if is_ubi(fs_bytes): | ||
extract_file_system(get_fs_from_ubi(fs_bytes), dest) | ||
elif is_ubifs(fs_bytes): | ||
with DummyLEB.from_bytes(fs_bytes) as leb: | ||
with redirect_stdout(StringIO()): | ||
# If files already exist they are not written again. | ||
extract_ubifs(ubifs(leb), dest) | ||
elif is_squashfs(fs_bytes): | ||
with SquashFsImage.from_bytes(fs_bytes) as image: | ||
extract_squashfs(image.root, dest, True) | ||
elif is_cramfs(fs_bytes): | ||
with Cramfs.from_bytes(fs_bytes) as image: | ||
extract_cramfs(image.rootdir, dest, True) | ||
else: | ||
raise Exception("Unknown file system") | ||
|
||
|
||
def extract_pak(pak: PAK, dest: Path = None, force: bool = False): | ||
dest = (Path.cwd() / "reolink_firmware") if dest is None else dest | ||
dest.mkdir(parents=True, exist_ok=force) | ||
rootfsdir = [s.name for s in pak.sections if s.name in ROOTFS_SECTIONS][0] | ||
for section in pak.sections: | ||
if section.name in FS_SECTIONS: | ||
if section.name == "app": | ||
outpath = dest / rootfsdir / "mnt" / "app" | ||
else: | ||
outpath = dest / rootfsdir | ||
extract_file_system(pak.extract_section(section), outpath) |