summaryrefslogtreecommitdiff
path: root/tests/gpt-header-move
blob: 18f58d0ec89631eeab9dbadd0bbccc86bd9dcec0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/python3

# open img file, subtract 33 from altlba address, and move the last 33 sectors
# back by 33 sectors

from struct import unpack_from, pack_into
from zipfile import crc32
import array
import sys

file = open(sys.argv[1],'rb+')
file.seek(512)
gptheader = file.read(512)
altlba = unpack_from('<q', gptheader, offset=32)[0]
gptheader = array.array('B', gptheader)
pack_into('<Q', gptheader, 32, altlba-33)
#zero header crc
pack_into('<L', gptheader, 16, 0)
#compute new crc
newcrc = ((crc32(gptheader[:92])) & 0xFFFFFFFF)
pack_into('<L', gptheader, 16, newcrc)
file.seek(512)
file.write(gptheader)
file.seek(512*altlba)
gptheader = file.read(512)
file.seek(512*(altlba-32))
backup = file.read(512*32)
altlba -= 33
gptheader = array.array('B',gptheader)
#update mylba
pack_into('<Q', gptheader, 24, altlba)
#update table lba
pack_into('<Q', gptheader, 72, altlba-32)
#zero header crc
pack_into('<L', gptheader, 16, 0)
#compute new crc
newcrc = ((crc32(gptheader[:92])) & 0xFFFFFFFF)
pack_into('<L', gptheader, 16, newcrc)
file.seek(512*(altlba-32))
file.write(backup)
file.write(gptheader)
file.write(b"\0" * (512 * 33))