modified: patch.py

This commit is contained in:
elseif 2026-07-15 01:57:33 +08:00
parent dc5e2e0ac8
commit 22fefaf3f4

View File

@ -106,40 +106,6 @@ def patch_bzimage(data:bytes,key_dict:dict):
new_data = new_data.replace(vmlinux_xz,new_vmlinux_xz) new_data = new_data.replace(vmlinux_xz,new_vmlinux_xz)
return new_data return new_data
def patch_block(dev:str,file:str,key_dict):
BLOCK_SIZE = 4096
#sudo debugfs /dev/nbd0p1 -R 'stats' | grep "Block size" | sed -n '1p' | cut -d ':' -f 2
#sudo debugfs /dev/nbd0p1 -R 'stat boot/initrd.rgz' 2> /dev/null | sed -n '11p'
stdout,_ = run_shell_command(f"debugfs {dev} -R 'stat {file}' 2> /dev/null | sed -n '11p' ")
#(0-11):1592-1603, (IND):1173, (12-15):1604-1607, (16-26):1424-1434
blocks_info = stdout.decode().strip().split(',')
print(f'blocks_info : {blocks_info}')
blocks = []
ind_block_id = None
for block_info in blocks_info:
_tmp = block_info.strip().split(':')
if _tmp[0].strip() == '(IND)':
ind_block_id = int(_tmp[1])
else:
print(f'block_info : {block_info}')
id_range = _tmp[0].strip().replace('(','').replace(')','').split('-')
block_range = _tmp[1].strip().replace('(','').replace(')','').split('-')
blocks += [id for id in range(int(block_range[0]),int(block_range[1])+1)]
print(f' blocks : {len(blocks)} ind_block_id : {ind_block_id}')
#sudo debugfs /dev/nbd0p1 -R 'cat boot/initrd.rgz' > data
data,stderr = run_shell_command(f"debugfs {dev} -R 'cat {file}' 2> /dev/null")
new_data = patch_kernel(data,key_dict)
print(f'write block {len(blocks)} : [',end="")
with open(dev,'wb') as f:
for index,block_id in enumerate(blocks):
print('#',end="")
f.seek(block_id*BLOCK_SIZE)
f.write(new_data[index*BLOCK_SIZE:(index+1)*BLOCK_SIZE])
f.flush()
print(']')
def patch_initrd_xz(initrd_xz:bytes,key_dict:dict,ljust=True): def patch_initrd_xz(initrd_xz:bytes,key_dict:dict,ljust=True):
try: try:
initrd = lzma.decompress(initrd_xz) initrd = lzma.decompress(initrd_xz)
@ -495,9 +461,6 @@ if __name__ == '__main__':
kernel_parser = subparsers.add_parser('kernel',help='patch kernel file') kernel_parser = subparsers.add_parser('kernel',help='patch kernel file')
kernel_parser.add_argument('input',type=str, help='Input file') kernel_parser.add_argument('input',type=str, help='Input file')
kernel_parser.add_argument('-O','--output',type=str,help='Output file') kernel_parser.add_argument('-O','--output',type=str,help='Output file')
block_parser = subparsers.add_parser('block',help='patch block file')
block_parser.add_argument('dev',type=str, help='block device')
block_parser.add_argument('file',type=str, help='file path')
buildefi_parser = subparsers.add_parser('buildefi',help='build efi file') buildefi_parser = subparsers.add_parser('buildefi',help='build efi file')
buildefi_parser.add_argument('input',type=str, help='kernel file') buildefi_parser.add_argument('input',type=str, help='kernel file')
buildefi_parser.add_argument('output',type=str,help='Output to file') buildefi_parser.add_argument('output',type=str,help='Output to file')
@ -518,9 +481,6 @@ if __name__ == '__main__':
print(f'patching {args.input} ...') print(f'patching {args.input} ...')
data = patch_kernel(open(args.input,'rb').read(),key_dict) data = patch_kernel(open(args.input,'rb').read(),key_dict)
open(args.output or args.input,'wb').write(data) open(args.output or args.input,'wb').write(data)
elif args.command == 'block':
print(f'patching {args.file} in {args.dev} ...')
patch_block(args.dev,args.file,key_dict)
elif args.command == 'buildefi': elif args.command == 'buildefi':
print(f'building EFI from {args.input} ...') print(f'building EFI from {args.input} ...')
build_efi(args.input,args.output) build_efi(args.input,args.output)