modified: .gitignore

modified:   npk.py
	modified:   patch.py
This commit is contained in:
zyb 2024-07-20 11:14:55 +08:00
parent 0ce6484e60
commit 0ff0485a87
3 changed files with 6 additions and 14 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
__pycache__/ __pycache__/
.vscode/
venv/ venv/
app/ app/
test_*.py test_*.py

5
npk.py
View File

@ -102,7 +102,6 @@ class NpkInfo:
def build_time(self,value:datetime): def build_time(self,value:datetime):
self._build_time = int(value.timestamp()) self._build_time = int(value.timestamp())
class NpkNameInfo(NpkInfo): class NpkNameInfo(NpkInfo):
_format = '<16s4sI12s' _format = '<16s4sI12s'
def __init__(self,name:str,version:str,build_time=datetime.now(),unknow=b'\x00'*12): def __init__(self,name:str,version:str,build_time=datetime.now(),unknow=b'\x00'*12):
@ -171,7 +170,6 @@ class Package:
def __iter__(self): def __iter__(self):
for part in self._parts: for part in self._parts:
yield part yield part
def __getitem__(self, id:NpkPartID): def __getitem__(self, id:NpkPartID):
for part in self._parts: for part in self._parts:
if part.id == id: if part.id == id:
@ -313,8 +311,6 @@ class NovaPackage(Package):
assert int.from_bytes(data[4:8],'little') == len(data) - 8, 'Invalid Nova Package Size' assert int.from_bytes(data[4:8],'little') == len(data) - 8, 'Invalid Nova Package Size'
return NovaPackage(data[8:]) return NovaPackage(data[8:])
if __name__=='__main__': if __name__=='__main__':
parser = argparse.ArgumentParser(description='nova package creator and editor') parser = argparse.ArgumentParser(description='nova package creator and editor')
subparsers = parser.add_subparsers(dest="command") subparsers = parser.add_subparsers(dest="command")
@ -335,7 +331,6 @@ if __name__=='__main__':
kcdsa_public_key = bytes.fromhex(os.environ['CUSTOM_LICENSE_PUBLIC_KEY']) kcdsa_public_key = bytes.fromhex(os.environ['CUSTOM_LICENSE_PUBLIC_KEY'])
eddsa_public_key = bytes.fromhex(os.environ['CUSTOM_NPK_SIGN_PUBLIC_KEY']) eddsa_public_key = bytes.fromhex(os.environ['CUSTOM_NPK_SIGN_PUBLIC_KEY'])
if args.command =='sign': if args.command =='sign':
print(f'Signing {args.input}') print(f'Signing {args.input}')
npk = NovaPackage.load(args.input) npk = NovaPackage.load(args.input)

View File

@ -108,7 +108,6 @@ def patch_elf(data: bytes,key_dict:dict):
initrd_xz = find_7zXZ_data(data) initrd_xz = find_7zXZ_data(data)
return patch_initrd_xz(initrd_xz,key_dict) return patch_initrd_xz(initrd_xz,key_dict)
def patch_pe(data: bytes,key_dict:dict): def patch_pe(data: bytes,key_dict:dict):
vmlinux_xz = find_7zXZ_data(data) vmlinux_xz = find_7zXZ_data(data)
vmlinux = lzma.decompress(vmlinux_xz) vmlinux = lzma.decompress(vmlinux_xz)
@ -266,8 +265,7 @@ def run_shell_command(command):
process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return process.stdout, process.stderr return process.stdout, process.stderr
def patch_npk_package(package,key_dict):
def patch_npk_package(package,key_dict,kcdsa_private_key,eddsa_private_key):
if package[NpkPartID.NAME_INFO].data.name == 'system': if package[NpkPartID.NAME_INFO].data.name == 'system':
file_container = NpkFileContainer.unserialize_from(package[NpkPartID.FILE_CONTAINER].data) file_container = NpkFileContainer.unserialize_from(package[NpkPartID.FILE_CONTAINER].data)
for item in file_container: for item in file_container:
@ -280,8 +278,6 @@ def patch_npk_package(package,key_dict,kcdsa_private_key,eddsa_private_key):
elif item.name == b'boot/initrd.rgz': elif item.name == b'boot/initrd.rgz':
print(f'patch {item.name} ...') print(f'patch {item.name} ...')
item.data = patch_kernel(item.data,key_dict) item.data = patch_kernel(item.data,key_dict)
open('initrd.rgz','wb').write(item.data)
package[NpkPartID.FILE_CONTAINER].data = file_container.serialize() package[NpkPartID.FILE_CONTAINER].data = file_container.serialize()
try: try:
squashfs_file = 'squashfs-root.sfs' squashfs_file = 'squashfs-root.sfs'
@ -298,6 +294,7 @@ def patch_npk_package(package,key_dict,kcdsa_private_key,eddsa_private_key):
elif 'ARCH' in os.environ and os.environ['ARCH'] == '-arm64': elif 'ARCH' in os.environ and os.environ['ARCH'] == '-arm64':
run_shell_command(f"sudo cp keygen/keygen_aarch64 {keygen}") run_shell_command(f"sudo cp keygen/keygen_aarch64 {keygen}")
run_shell_command(f"sudo chmod a+x {keygen}") run_shell_command(f"sudo chmod a+x {keygen}")
run_shell_command(f'echo -e "\n https://github.com/elseif/MikroTikPatch" | sudo tee -a {os.path.join(extract_dir,"nova/lib/console/logo.txt")}')
print(f"pack {extract_dir} ...") print(f"pack {extract_dir} ...")
run_shell_command(f"rm -f {squashfs_file}") run_shell_command(f"rm -f {squashfs_file}")
_, stderr = run_shell_command(f"mksquashfs {extract_dir} {squashfs_file} -quiet -comp xz -no-xattrs -b 256k") _, stderr = run_shell_command(f"mksquashfs {extract_dir} {squashfs_file} -quiet -comp xz -no-xattrs -b 256k")
@ -313,10 +310,9 @@ def patch_npk_file(key_dict,kcdsa_private_key,eddsa_private_key,input_file,outpu
npk = NovaPackage.load(input_file) npk = NovaPackage.load(input_file)
if len(npk._packages) > 0: if len(npk._packages) > 0:
for package in npk._packages: for package in npk._packages:
patch_npk_package(package,key_dict,kcdsa_private_key,eddsa_private_key) patch_npk_package(package,key_dict)
else: else:
patch_npk_package(npk,key_dict,kcdsa_private_key,eddsa_private_key) patch_npk_package(npk,key_dict)
npk.sign(kcdsa_private_key,eddsa_private_key) npk.sign(kcdsa_private_key,eddsa_private_key)
npk.save(output_file or input_file) npk.save(output_file or input_file)