Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
Merge pull request #13 from ColdWindScholar/dev
Browse files Browse the repository at this point in the history
Delete redundant code
  • Loading branch information
ColdWindScholar authored Apr 9, 2024
2 parents 18e0d2b + 9564e3e commit 99976d2
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 1,099 deletions.
Binary file removed bin/Darwin/X86_64/gettype
Binary file not shown.
951 changes: 0 additions & 951 deletions bin/Darwin/X86_64/lpunpack.py

This file was deleted.

Binary file removed bin/Linux/aarch64/gettype
Binary file not shown.
Binary file removed bin/Linux/aarch64/vbmeta-disable-verification
Binary file not shown.
Binary file removed bin/Linux/x86_64/gettype
Binary file not shown.
Binary file removed bin/Linux/x86_64/imjtool
Binary file not shown.
Binary file removed bin/Linux/x86_64/lpunpack
Binary file not shown.
143 changes: 0 additions & 143 deletions bin/Linux/x86_64/sdat2img.py

This file was deleted.

Binary file removed bin/Linux/x86_64/vbmeta-disable-verification
Binary file not shown.
130 changes: 130 additions & 0 deletions bin/gettype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import struct
import sys
import os

formats = ([b'PK', "zip"], [b'OPPOENCRYPT!', "ozip"], [b'7z', "7z"], [b'\x53\xef', 'ext', 1080],
[b'\x3a\xff\x26\xed', "sparse"], [b'\xe2\xe1\xf5\xe0', "erofs", 1024], [b"CrAU", "payload"],
[b"AVB0", "vbmeta"], [b'\xd7\xb7\xab\x1e', "dtbo"],
[b'\xd0\x0d\xfe\xed', "dtb"], [b"MZ", "exe"], [b".ELF", 'elf'],
[b"ANDROID!", "boot"], [b"VNDRBOOT", "vendor_boot"],
[b'AVBf', "avb_foot"], [b'BZh', "bzip2"],
[b'CHROMEOS', 'chrome'], [b'\x1f\x8b', "gzip"],
[b'\x1f\x9e', "gzip"], [b'\x02\x21\x4c\x18', "lz4_legacy"],
[b'\x03\x21\x4c\x18', 'lz4'], [b'\x04\x22\x4d\x18', 'lz4'],
[b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\x03', "zopfli"], [b'\xfd7zXZ', 'xz'],
[b']\x00\x00\x00\x04\xff\xff\xff\xff\xff\xff\xff\xff', 'lzma'], [b'\x02!L\x18', 'lz4_lg'],
[b'\x89PNG', 'png'], [b"LOGO!!!!", 'logo', 4000], [b'\x28\xb5\x2f\xfd', 'zstd'])


def gettype(file) -> str:
if not os.path.exists(file):
return "fne"

def compare(header: bytes, number: int = 0) -> int:
with open(file, 'rb') as f:
f.seek(number)
return f.read(len(header)) == header

def is_super(fil) -> any:
with open(fil, 'rb') as file_:
buf = bytearray(file_.read(4))
if len(buf) < 4:
return False
file_.seek(0, 0)

while buf[0] == 0x00:
buf = bytearray(file_.read(1))
try:
file_.seek(-1, 1)
except BaseException or Exception:
return False
buf += bytearray(file_.read(4))
return buf[1:] == b'\x67\x44\x6c\x61'

try:
if is_super(file):
return 'super'
except IndexError:
...
for f_ in formats:
if len(f_) == 2:
if compare(f_[0]):
return f_[1]
elif len(f_) == 3:
if compare(f_[0], f_[2]):
return f_[1]
try:
if LOGO_DUMPER(file, str(None)).check_img(file):
return 'logo'
except AssertionError:
...
except struct.error:
...
return "unknown"


class DUMPCFG:
blksz = 0x1 << 0xc
headoff = 0x4000
magic = b"LOGO!!!!"
imgnum = 0
imgblkoffs = []
imgblkszs = []


class BMPHEAD:
def __init__(self, buf: bytes = None): # Read bytes buf and use this struct to parse
assert buf is not None, f"buf Should be bytes not {type(buf)}"
# print(buf)
self.structstr = "<H6I"
(
self.magic,
self.fsize,
self.reserved,
self.hsize,
self.dib,
self.width,
self.height,
) = struct.unpack(self.structstr, buf)


class XIAOMI_BLKSTRUCT:
def __init__(self, buf: bytes):
self.structstr = "2I"
(
self.img_offset,
self.blksz,
) = struct.unpack(self.structstr, buf)


class LOGO_DUMPER:
def __init__(self, img: str, out: str, dir__: str = "pic"):
self.magic = None
self.out = out
self.img = img
self.dir = dir__
self.struct_str = "<8s"
self.cfg = DUMPCFG()
self.check_img(img)

def check_img(self, img: str):
assert os.access(img, os.F_OK), f"{img} does not found!"
with open(img, 'rb') as f:
f.seek(self.cfg.headoff, 0)
self.magic = struct.unpack(
self.struct_str, f.read(struct.calcsize(self.struct_str))
)[0]
while True:
m = XIAOMI_BLKSTRUCT(f.read(8))
if m.img_offset != 0:
self.cfg.imgblkszs.append(m.blksz << 0xc)
self.cfg.imgblkoffs.append(m.img_offset << 0xc)
self.cfg.imgnum += 1
else:
break
assert self.magic == b"LOGO!!!!", "File does not match xiaomi logo magic!"
return True


if __name__ == '__main__':
print(gettype(sys.argv[1]))
File renamed without changes.
10 changes: 5 additions & 5 deletions port.sh
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,19 @@ elif [[ ${baserom_type} == 'br' ]];then
blue "开始分解底包 [new.dat.br]" "Unpacking BASEROM[new.dat.br]"
for i in ${super_list}; do
${tools_dir}/brotli -d build/baserom/$i.new.dat.br >/dev/null 2>&1
sudo python3 ${tools_dir}/sdat2img.py build/baserom/$i.transfer.list build/baserom/$i.new.dat build/baserom/images/$i.img >/dev/null 2>&1
sudo python3 ${work_dir}/bin/sdat2img.py build/baserom/$i.transfer.list build/baserom/$i.new.dat build/baserom/images/$i.img >/dev/null 2>&1
rm -rf build/baserom/$i.new.dat* build/baserom/$i.transfer.list build/baserom/$i.patch.*
done
fi

for part in system system_dlkm system_ext product product_dlkm mi_ext ;do
if [[ -f build/baserom/images/${part}.img ]];then
if [[ $($tools_dir/gettype -i build/baserom/images/${part}.img) == "ext" ]];then
if [[ $(python3 $work_dir/bin/gettype.py build/baserom/images/${part}.img) == "ext" ]];then
blue "正在分解底包 ${part}.img [ext]" "Extracing ${part}.img [ext] from BASEROM"
sudo python3 bin/imgextractor/imgextractor.py build/baserom/images/${part}.img build/baserom/images/ >/dev/null 2>&1
blue "分解底包 [${part}.img] 完成" "BASEROM ${part}.img [ext] extracted."
rm -rf build/baserom/images/${part}.img
elif [[ $($tools_dir/gettype -i build/baserom/images/${part}.img) == "erofs" ]]; then
elif [[ $(python3 $work_dir/bin/gettype.py build/baserom/images/${part}.img) == "erofs" ]]; then
pack_type=EROFS
blue "正在分解底包 ${part}.img [erofs]" "Extracing ${part}.img [erofs] from BASEROM"
extract.erofs -x -i build/baserom/images/${part}.img -o build/baserom/images/ > /dev/null 2>&1 || error "分解 ${part}.img 失败" "Extracting ${part}.img failed."
Expand Down Expand Up @@ -244,13 +244,13 @@ for part in ${super_list};do
if [ -f "${work_dir}/build/portrom/images/${part}.img" ];then
blue "开始提取 ${part}.img" "Extracting ${part}.img"

if [[ $($tools_dir/gettype -i build/portrom/images/${part}.img) == "ext" ]];then
if [[ $(python3 $work_dir/bin/gettype.py build/portrom/images/${part}.img) == "ext" ]];then
pack_type=EXT
python3 bin/imgextractor/imgextractor.py build/portrom/images/${part}.img build/portrom/images/ > /dev/null 2>&1 || error "提取${part}失败" "Extracting partition ${part} failed"
mkdir -p build/portrom/images/${part}/lost+found
rm -rf build/portrom/images/${part}.img
green "提取 [${part}] [ext]镜像完毕" "Extracting [${part}].img [ext] done"
elif [[ $(gettype -i build/portrom/images/${part}.img) == "erofs" ]];then
elif [[ $(python3 $work_dir/bin/gettype.py build/portrom/images/${part}.img) == "erofs" ]];then
pack_type=EROFS
green "移植包为 [erofs] 文件系统" "PORTROM filesystem: [erofs]. "
[ "${repackext4}" = "true" ] && pack_type=EXT
Expand Down

0 comments on commit 99976d2

Please sign in to comment.