Skip to content

Fix Command12 and Command16. #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions smartie/scsi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def identify(

command16 = Command16(
operation_code=OperationCode.COMMAND_16,
protocol=ATAProtocol.PIO_DATA_IN << 1,
protocol=ATAProtocol.PIO_DATA_IN,
flags=CommandFlags(
t_length=CommandFlags.Length.IN_SECTOR_COUNT,
byt_blok=True,
Expand Down Expand Up @@ -227,7 +227,7 @@ def smart_thresholds(self) -> Tuple[SmartThresholdResponse, SCSIResponse]:

command16 = Command16(
operation_code=OperationCode.COMMAND_16,
protocol=ATAProtocol.PIO_DATA_IN << 1,
protocol=ATAProtocol.PIO_DATA_IN,
command=ATACommands.SMART,
flags=CommandFlags(
t_length=CommandFlags.Length.IN_SECTOR_COUNT,
Expand All @@ -236,7 +236,8 @@ def smart_thresholds(self) -> Tuple[SmartThresholdResponse, SCSIResponse]:
ck_cond=True,
),
features=ATASmartFeature.SMART_READ_THRESHOLDS,
).set_lba(0xC24F00)
)
command16.lba = 0xC24F00

response = self.issue_command(Direction.FROM, command16, thresholds)
return thresholds, response
Expand All @@ -249,7 +250,7 @@ def smart(self) -> Tuple[SmartDataResponse, SCSIResponse]:

command16 = Command16(
operation_code=OperationCode.COMMAND_16,
protocol=ATAProtocol.PIO_DATA_IN << 1,
protocol=ATAProtocol.PIO_DATA_IN,
command=ATACommands.SMART,
flags=CommandFlags(
t_length=CommandFlags.Length.IN_SECTOR_COUNT,
Expand All @@ -258,7 +259,8 @@ def smart(self) -> Tuple[SmartDataResponse, SCSIResponse]:
ck_cond=True,
),
features=ATASmartFeature.SMART_READ_DATA,
).set_lba(0xC24F00)
)
command16.lba = 0xC24F00

response = self.issue_command(Direction.FROM, command16, smart)
return smart, response
Expand Down
57 changes: 36 additions & 21 deletions smartie/scsi/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,23 +273,30 @@ class Command12(ctypes.Structure):
_pack_ = 1
_fields_ = [
("operation_code", ctypes.c_ubyte),
("protocol", ctypes.c_ubyte),
("reserved_1", ctypes.c_ubyte, 1),
("protocol", ctypes.c_ubyte, 4),
("multiple_count", ctypes.c_ubyte, 3),
("flags", CommandFlags),
("features", ctypes.c_ubyte),
("reserved_1", ctypes.c_ubyte, 1),
("sector_count", ctypes.c_ubyte, 7),
("reserved_2", ctypes.c_ubyte, 1),
("lba_low", ctypes.c_ubyte, 7),
("reserved_3", ctypes.c_ubyte, 1),
("lba_mid", ctypes.c_ubyte, 7),
("reserved_4", ctypes.c_ubyte, 1),
("lba_high", ctypes.c_ubyte, 7),
("sector_count", ctypes.c_ubyte),
("lba_low", ctypes.c_ubyte),
("lba_mid", ctypes.c_ubyte),
("lba_high", ctypes.c_ubyte),
("device", ctypes.c_ubyte),
("command", ctypes.c_ubyte),
("reserved_5", ctypes.c_ubyte),
("reserved_2", ctypes.c_ubyte),
("control", ctypes.c_ubyte),
]

@property
def lba(self):
return self.lba_low | (self.lba_mid << 8) | (self.lba_high << 16)

@lba.setter
def lba(self, lba: int):
self.lba_low, self.lba_mid, self.lba_high = \
lba.to_bytes(3, byteorder="little")


class Command16(ctypes.BigEndianStructure):
"""
Expand All @@ -304,7 +311,9 @@ class Command16(ctypes.BigEndianStructure):
_pack_ = 1
_fields_ = [
("operation_code", ctypes.c_ubyte),
("protocol", ctypes.c_ubyte),
("extend", ctypes.c_ubyte, 1),
("protocol", ctypes.c_ubyte, 4),
("multiple_count", ctypes.c_ubyte, 3),
("flags", CommandFlags),
("features", ctypes.c_ushort),
("sector_count", ctypes.c_ushort),
Expand All @@ -319,16 +328,22 @@ class Command16(ctypes.BigEndianStructure):
("control", ctypes.c_ubyte),
]

def set_lba(self, lba: int):
lba = lba.to_bytes(6, byteorder="little")
self.lba_high_low = lba[3]
self.lba_low = lba[0]
self.lba_high_mid = lba[4]
self.lba_mid = lba[1]
self.lba_high_high = lba[5]
self.lba_high = lba[2]

return self
@property
def lba(self):
lba = self.lba_low | (self.lba_mid << 8) | (self.lba_high << 16)
if not self.extend:
return lba
lba = lba | (self.lba_high_low << 24) | (self.lba_high_mid << 32) | \
(self.lba_high_high << 40)
return lba

@lba.setter
def lba(self, lba: int):
self.lba_low, self.lba_mid, self.lba_high, \
self.lba_high_low, self.lba_high_mid, self.lba_high_high = \
lba.to_bytes(6, byteorder="little")

self.extend = lba > 0xFFFFFF


class SGIOHeader(ctypes.Structure):
Expand Down