Reputation: 11
I'm trying to find the limit of a disk by writing/reading from it unil system returns error, that's when I know where the limit is.
So I used this python script to read to the disk
disk_path = r"\\.\PHYSICALDRIVE3"
offset = 120176576 * 512
i = 0
while True:
try:
with open(disk_path, 'r+b') as disk:
disk.seek(offset)
disk.read(512)
print(f"Successfully wrote {len(data)} bytes to {disk_path} at offset {offset}.")
except Exception as e:
print(e)
print(offset)
break
i += 1
offset += 512
The result is incorrect, which is 61530432000
. So I decided to use write instead through following script. And the result is correct
disk_path = r"\\.\PHYSICALDRIVE3"
hex_data = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
data = bytes.fromhex(hex_data)
offset = 120176576 * 512
i = 0
while True:
try:
with open(disk_path, 'r+b') as disk:
disk.seek(offset)
disk.write(data)
print(f"Successfully wrote {len(data)} bytes to {disk_path} at offset {offset}.")
except Exception as e:
print(e)
print(offset)
break
i += 1
offset += 512
This time the result is at 61530439680
which is exacat 15 sector more. Both script were stopped by [Errno 13] Permission denied
. So why would write work but read won't work? Note that the read/write both starts at 120176576 * 512
to save time, the location is the last bytes been used by a partition, it's found by this program by reading the MBR. I know 61530439680
is correct because Get-Disk | Select-Object Number, Size
gived me the same result.
Edit: I updated the code to find the error. All the error is the permission error, first is happens at write, if we don't break it, then flush will have error as well. Then it does on to the next sector, at the next sector, seek will have error as well as write and flush.
try:
# Open the disk once and keep it open
with open(disk_path, 'r+b') as disk:
while True:
try:
try:
disk.seek(offset)
except Exception as e:
print("3", e)
break
try:
disk.write(data)
except Exception as e:
print("4", e)
break
try:
disk.flush() # Force buffer to write to disk
except Exception as e:
print("5", e)
break
print(f"Wrote at offset: {offset}")
offset += sector_size
except Exception as e:
print("1", e)
break
except Exception as e:
print("2", e)
I named the error in the code to identify the location
If break is applied error will be [Loop1] 4 -> 2 [Break]
If break is not applied the loop goes on forever: [Loop1] 4 -> 5 -> [Loop2] 3 -> 4 -> 5 -> [Loop3]etc......
If we are doing reading, then only error 4 will occur
Upvotes: 1
Views: 79