Using BLKGETSIZE * BLKSSZGET instead of BLKGETSIZE64

This commit is contained in:
Devaev Maxim 2019-06-12 08:28:41 +03:00
parent 81a9e1fa07
commit f21f1d2836

View File

@ -30,6 +30,7 @@ import dataclasses
import types import types
from typing import Dict from typing import Dict
from typing import IO
from typing import Callable from typing import Callable
from typing import Type from typing import Type
from typing import AsyncGenerator from typing import AsyncGenerator
@ -145,15 +146,21 @@ def _parse_image_info_bytes(data: bytes) -> Optional[_ImageInfo]:
return None return None
def _ioctl_uint32(device_file: IO, request: int) -> int:
buf = b"\0" * 4
buf = fcntl.ioctl(device_file.fileno(), request, buf)
result = struct.unpack("I", buf)[0]
assert result > 0, (device_file, request, buf)
return result
def _explore_device(device_path: str) -> _MassStorageDeviceInfo: def _explore_device(device_path: str) -> _MassStorageDeviceInfo:
if not stat.S_ISBLK(os.stat(device_path).st_mode): if not stat.S_ISBLK(os.stat(device_path).st_mode):
raise RuntimeError("Not a block device: %s" % (device_path)) raise RuntimeError("Not a block device: %s" % (device_path))
with open(device_path, "rb") as device_file: with open(device_path, "rb") as device_file:
buf = b"\0" * 8 # size = BLKGETSIZE * BLKSSZGET
buf = fcntl.ioctl(device_file.fileno(), 0x80081272, buf) # BLKGETSIZE64 size = _ioctl_uint32(device_file, 0x1260) * _ioctl_uint32(device_file, 0x1268)
size = struct.unpack("L", buf)[0]
assert size > 0, (size, buf)
device_file.seek(size - _IMAGE_INFO_SIZE) device_file.seek(size - _IMAGE_INFO_SIZE)
image_info = _parse_image_info_bytes(device_file.read()) image_info = _parse_image_info_bytes(device_file.read())