Skip to content
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

windows: add missing constants and structs for DeviceIoControl for manipulating drives #184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
219 changes: 218 additions & 1 deletion windows/types_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2024,7 +2024,8 @@ type SocketAddress struct {

// IP returns an IPv4 or IPv6 address, or nil if the underlying SocketAddress is neither.
func (addr *SocketAddress) IP() net.IP {
if uintptr(addr.SockaddrLength) >= unsafe.Sizeof(RawSockaddrInet4{}) && addr.Sockaddr.Addr.Family == AF_INET {
if uintptr(addr.SockaddrLength) >= unsafe.Sizeof(RawSockaddrInet4{}) &&
addr.Sockaddr.Addr.Family == AF_INET {
return (*RawSockaddrInet4)(unsafe.Pointer(addr.Sockaddr)).Addr[:]
} else if uintptr(addr.SockaddrLength) >= unsafe.Sizeof(RawSockaddrInet6{}) && addr.Sockaddr.Addr.Family == AF_INET6 {
return (*RawSockaddrInet6)(unsafe.Pointer(addr.Sockaddr)).Addr[:]
Expand Down Expand Up @@ -3380,3 +3381,219 @@ type BLOB struct {
Size uint32
BlobData *byte
}

// Constants and structs for DeviceIoControl
const (
FILE_ANY_ACCESS = 0
FILE_SPECIAL_ACCESS = 0
FILE_READ_ACCESS = FILE_READ_DATA
FILE_WRITE_ACCESS = FILE_WRITE_DATA
)

const (
METHOD_BUFFERED = 0
METHOD_IN_DIRECT = 1
METHOD_OUT_DIRECT = 2
METHOD_NEITHER = 3
)

const (
IOCTL_DISK_BASE = 0x00000007
IOCTL_SCSI_BASE = 0x00000004
IOCTL_STORAGE_BASE = 0x0000002D
IOCTL_VOLUME_BASE = 0x00000056
)

const (
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX = (IOCTL_DISK_BASE << 16) | (FILE_ANY_ACCESS << 14) | (0x0028 << 2) | METHOD_BUFFERED
IOCTL_SCSI_GET_ADDRESS = (IOCTL_SCSI_BASE << 16) | (FILE_ANY_ACCESS << 14) | (0x0406 << 2) | METHOD_BUFFERED
IOCTL_STORAGE_CHECK_VERIFY = (IOCTL_STORAGE_BASE << 16) | (FILE_READ_ACCESS << 14) | (0x0200 << 2) | METHOD_BUFFERED
IOCTL_STORAGE_CHECK_VERIFY2 = (IOCTL_STORAGE_BASE << 16) | (FILE_ANY_ACCESS << 14) | (0x0200 << 2) | METHOD_BUFFERED
IOCTL_STORAGE_QUERY_PROPERTY = (IOCTL_STORAGE_BASE << 16) | (FILE_ANY_ACCESS << 14) | (0x0500 << 2) | METHOD_BUFFERED
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = (IOCTL_VOLUME_BASE << 16) | (FILE_ANY_ACCESS << 14) | (0x0000 << 2) | METHOD_BUFFERED
)

// MEDIA_TYPE enumeration
type MEDIA_TYPE uint32

const (
Unknown = iota // Format is unknown
F5_1Pt2_512 // 5.25", 1.2MB, 512 bytes/sector
F3_1Pt44_512 // 3.5", 1.44MB, 512 bytes/sector
F3_2Pt88_512 // 3.5", 2.88MB, 512 bytes/sector
F3_20Pt8_512 // 3.5", 20.8MB, 512 bytes/sector
F3_720_512 // 3.5", 720KB, 512 bytes/sector
F5_360_512 // 5.25", 360KB, 512 bytes/sector
F5_320_512 // 5.25", 320KB, 512 bytes/sector
F5_320_1024 // 5.25", 320KB, 1024 bytes/sector
F5_180_512 // 5.25", 180KB, 512 bytes/sector
F5_160_512 // 5.25", 160KB, 512 bytes/sector
RemovableMedia // Removable media other than floppy
FixedMedia // Fixed hard disk media
F3_120M_512 // 3.5", 120M Floppy
F3_640_512 // 3.5" , 640KB, 512 bytes/sector
F5_640_512 // 5.25", 640KB, 512 bytes/sector
F5_720_512 // 5.25", 720KB, 512 bytes/sector
F3_1Pt2_512 // 3.5" , 1.2Mb, 512 bytes/sector
F3_1Pt23_1024 // 3.5" , 1.23Mb, 1024 bytes/sector
F5_1Pt23_1024 // 5.25", 1.23MB, 1024 bytes/sector
F3_128Mb_512 // 3.5" MO 128Mb 512 bytes/sector
F3_230Mb_512 // 3.5" MO 230Mb 512 bytes/sector
F8_256_128 // 8", 256KB, 128 bytes/sector
F3_200Mb_512 // 3.5", 200M Floppy (HiFD)
F3_240M_512 // 3.5", 240Mb Floppy (HiFD)
F3_32M_512 // 3.5", 32Mb Floppy
)

// PARTITION_STYLE enumeration
type PARTITION_STYLE uint32

const (
PARTITION_STYLE_MBR = iota
PARTITION_STYLE_GPT
PARTITION_STYLE_RAW
)

// STORAGE_BUS_TYPE enumeration
type STORAGE_BUS_TYPE uint32

const (
BusTypeUnknown = iota
BusTypeScsi
BusTypeAtapi
BusTypeAta
BusType1394
BusTypeSsa
BusTypeFibre
BusTypeUsb
BusTypeRAID
BusTypeiScsi
BusTypeSas
BusTypeSata
BusTypeSd
BusTypeMmc
BusTypeVirtual
BusTypeFileBackedVirtual
BusTypeMax
BusTypeMaxReserved = 0x7F
)

// STORAGE_PROPERTY_ID enumeration
type STORAGE_PROPERTY_ID uint32

const (
StorageDeviceProperty = iota
StorageAdapterProperty
StorageDeviceIdProperty
StorageDeviceUniqueIdProperty
StorageDeviceWriteCacheProperty
StorageMiniportProperty
StorageAccessAlignmentProperty
StorageDeviceSeekPenaltyProperty
StorageDeviceTrimProperty
StorageDeviceWriteAggregationProperty
StorageDeviceDeviceTelemetryProperty
StorageDeviceLBProvisioningProperty
StorageDevicePowerProperty
StorageDeviceCopyOffloadProperty
StorageDeviceResiliencyProperty
StorageDeviceMediumProductType
StorageAdapterRpmbProperty
StorageAdapterCryptoProperty
StorageDeviceTieringProperty
StorageDeviceFaultDomainProperty
StorageDeviceClusportProperty
StorageDeviceDependantDevicesProperty
StorageDeviceIoCapabilityProperty = 48
StorageAdapterProtocolSpecificProperty
StorageDeviceProtocolSpecificProperty
StorageAdapterTemperatureProperty
StorageDeviceTemperatureProperty
StorageAdapterPhysicalTopologyProperty
StorageDevicePhysicalTopologyProperty
StorageDeviceAttributesProperty
StorageDeviceManagementStatus
StorageAdapterSerialNumberProperty
StorageDeviceLocationProperty
StorageDeviceNumaProperty
StorageDeviceZonedDeviceProperty
StorageDeviceUnsafeShutdownCount
StorageDeviceEnduranceProperty
)

// STORAGE_QUERY_TYPE enumeration
type STORAGE_QUERY_TYPE uint32

const (
PropertyStandardQuery = iota
PropertyExistsQuery
PropertyMaskQuery
PropertyQueryMaxDefined
)

type DISK_EXTENT struct {
DiskNumber uint32
StartingOffset uint64
ExtentLength uint64
}

type DISK_PARTITION_INFO_MBR struct {
SizeOfPartitionInfo uint32
PartitionStyle PARTITION_STYLE
Signature uint32
CheckSum uint32
}

type DISK_PARTITION_INFO_GPT struct {
SizeOfPartitionInfo uint32
PartitionStyle PARTITION_STYLE
DiskId [16]byte // UUID
}

type DISK_GEOMETRY struct {
Cylinders uint64
MediaType MEDIA_TYPE
TracksPerCylinder uint32
SectorsPerTrack uint32
BytesPerSector uint32
}

type DISK_GEOMETRY_EX struct {
Geometry DISK_GEOMETRY
DiskSize uint64
DiskPartitionMBR *DISK_PARTITION_INFO_MBR
DiskPartitionGPT *DISK_PARTITION_INFO_GPT
}

type DISK_GEOMETRY_EX_RAW struct {
Geometry DISK_GEOMETRY
DiskSize uint64
}

type STORAGE_DEVICE_DESCRIPTOR struct {
Version uint32
Size uint32
DeviceType byte
DeviceTypeModifier byte
RemovableMedia bool
CommandQueueing bool
VendorIdOffset uint32
ProductIdOffset uint32
ProductRevisionOffset uint32
SerialNumberOffset uint32
BusType STORAGE_BUS_TYPE
RawPropertiesLength uint32
RawDeviceProperties [1]byte
}

type STORAGE_PROPERTY_QUERY struct {
PropertyId STORAGE_PROPERTY_ID
QueryType STORAGE_QUERY_TYPE
AdditionalParameters [1]byte
}

const (
FSCTL_LOCK_VOLUME = (0x00000009 << 16) | (FILE_ANY_ACCESS << 14) | (6 << 2) | METHOD_BUFFERED
FSCTL_UNLOCK_VOLUME = (0x00000009 << 16) | (FILE_ANY_ACCESS << 14) | (7 << 2) | METHOD_BUFFERED
FSCTL_DISMOUNT_VOLUME = (0x00000009 << 16) | (FILE_ANY_ACCESS << 14) | (8 << 2) | METHOD_BUFFERED
)