Skip to content

Commit

Permalink
Update patch.py
Browse files Browse the repository at this point in the history
  • Loading branch information
loskiq committed Jan 20, 2025
1 parent e637ca3 commit f54a620
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,35 @@ def patch_bzimage(data: bytes, key_dict: dict):
return new_data


def patch_block(dev: str, file: str, key_dict):
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 '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
#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])
ind_block_id = int(_tmp[1])
else:
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'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="")

#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()
Expand Down

0 comments on commit f54a620

Please sign in to comment.