#pragma once

#include "KswordArkProcessIoctl.h"

// ============================================================
// KswordArkStorageControllerIoctl.h
// 作用：
// - 定义可选控制器 function driver 的唯一 R3/R0 协议；
// - 区分独占硬件所有权与不可证明一致性的共享 live 研究模式；
// - 为 AHCI、NVMe、IDE 读写提供会话、代际、确认和审计契约。
// ============================================================

#define KSWORD_ARK_STORAGE_CONTROLLER_PROTOCOL_VERSION 1UL

// PnP 设备接口由 companion function driver 发布；R3 必须枚举此接口，
// 不使用会发生多实例冲突的固定 DosDevices 名称。
#define KSWORD_ARK_STORAGE_CONTROLLER_INTERFACE_GUID_INIT \
    {0x50dbe48c, 0x6ca0, 0x4d31, {0x9a, 0xb9, 0x34, 0x85, 0xa1, 0x69, 0xb1, 0xb4}}

#define KSWORD_ARK_IOCTL_FUNCTION_QUERY_STORAGE_CONTROLLER 0x86CUL
#define KSWORD_ARK_IOCTL_FUNCTION_CONTROL_STORAGE_CONTROLLER 0x86DUL
#define KSWORD_ARK_IOCTL_FUNCTION_TRANSFER_STORAGE_CONTROLLER 0x86EUL
#define KSWORD_ARK_IOCTL_FUNCTION_QUERY_STORAGE_CONTROLLER_AUDIT 0x86FUL

#define IOCTL_KSWORD_ARK_QUERY_STORAGE_CONTROLLER \
    CTL_CODE( \
        KSWORD_ARK_IOCTL_DEVICE_TYPE, \
        KSWORD_ARK_IOCTL_FUNCTION_QUERY_STORAGE_CONTROLLER, \
        METHOD_BUFFERED, \
        FILE_READ_ACCESS)

#define IOCTL_KSWORD_ARK_CONTROL_STORAGE_CONTROLLER \
    CTL_CODE( \
        KSWORD_ARK_IOCTL_DEVICE_TYPE, \
        KSWORD_ARK_IOCTL_FUNCTION_CONTROL_STORAGE_CONTROLLER, \
        METHOD_BUFFERED, \
        FILE_WRITE_ACCESS)

#define IOCTL_KSWORD_ARK_TRANSFER_STORAGE_CONTROLLER \
    CTL_CODE( \
        KSWORD_ARK_IOCTL_DEVICE_TYPE, \
        KSWORD_ARK_IOCTL_FUNCTION_TRANSFER_STORAGE_CONTROLLER, \
        METHOD_BUFFERED, \
        FILE_WRITE_ACCESS)

#define IOCTL_KSWORD_ARK_QUERY_STORAGE_CONTROLLER_AUDIT \
    CTL_CODE( \
        KSWORD_ARK_IOCTL_DEVICE_TYPE, \
        KSWORD_ARK_IOCTL_FUNCTION_QUERY_STORAGE_CONTROLLER_AUDIT, \
        METHOD_BUFFERED, \
        FILE_READ_ACCESS)

#define KSWORD_ARK_STORAGE_CONTROLLER_TYPE_UNKNOWN 0UL
#define KSWORD_ARK_STORAGE_CONTROLLER_TYPE_AHCI 1UL
#define KSWORD_ARK_STORAGE_CONTROLLER_TYPE_NVME 2UL
#define KSWORD_ARK_STORAGE_CONTROLLER_TYPE_IDE 3UL

#define KSWORD_ARK_STORAGE_OWNERSHIP_NONE 0UL
#define KSWORD_ARK_STORAGE_OWNERSHIP_SHARED_UNSUPPORTED 1UL
#define KSWORD_ARK_STORAGE_OWNERSHIP_EXCLUSIVE 2UL

#define KSWORD_ARK_STORAGE_COHERENCY_UNKNOWN 0UL
#define KSWORD_ARK_STORAGE_COHERENCY_UNPROVABLE 1UL
#define KSWORD_ARK_STORAGE_COHERENCY_EXCLUSIVE 2UL

#define KSWORD_ARK_STORAGE_CONTROLLER_CAP_READ 0x00000001UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CAP_WRITE 0x00000002UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CAP_FLUSH 0x00000004UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CAP_FUA 0x00000008UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CAP_DMA 0x00000010UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CAP_PIO 0x00000020UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CAP_64BIT_DMA 0x00000040UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CAP_EXCLUSIVE 0x00000080UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CAP_FORCE_LIVE 0x00000100UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CAP_WRITE_VERIFY 0x00000200UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CAP_ROLLBACK 0x00000400UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CAP_AUDIT 0x00000800UL

#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_NONE 0x00000000UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_SHARED_OWNER 0x00000001UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_COHERENCY_UNPROVABLE 0x00000002UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_SYSTEM_DISK 0x00000004UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_LIVE_VOLUMES 0x00000008UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_CONTROLLER_RESET 0x00000010UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_NO_RECOVERY_GUARANTEE 0x00000020UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_WRITE 0x00000040UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_FORCE_USED 0x00000080UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_IDENTITY_CHANGED 0x00000100UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_BEFORE_MISMATCH 0x00000200UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_VERIFY_FAILED 0x00000400UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_TIMEOUT 0x00000800UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_SYSTEM_DISK_UNKNOWN 0x00001000UL
#define KSWORD_ARK_STORAGE_CONTROLLER_RISK_LIVE_VOLUMES_UNKNOWN 0x00002000UL

#define KSWORD_ARK_STORAGE_CONTROLLER_CONTROL_ACQUIRE 1UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CONTROL_RELEASE 2UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CONTROL_RESET 3UL

#define KSWORD_ARK_STORAGE_CONTROLLER_CONTROL_FLAG_EXCLUSIVE 0x00000001UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CONTROL_FLAG_FORCE_LIVE_UNSAFE 0x00000002UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CONTROL_FLAG_ALLOW_SYSTEM_DISK 0x00000004UL
#define KSWORD_ARK_STORAGE_CONTROLLER_CONTROL_FLAG_UI_CONFIRMED 0x00000008UL

#define KSWORD_ARK_STORAGE_CONTROLLER_TRANSFER_READ 1UL
#define KSWORD_ARK_STORAGE_CONTROLLER_TRANSFER_WRITE 2UL
#define KSWORD_ARK_STORAGE_CONTROLLER_TRANSFER_ROLLBACK 3UL

#define KSWORD_ARK_STORAGE_CONTROLLER_TRANSFER_FLAG_FUA 0x00000001UL
#define KSWORD_ARK_STORAGE_CONTROLLER_TRANSFER_FLAG_FLUSH 0x00000002UL
#define KSWORD_ARK_STORAGE_CONTROLLER_TRANSFER_FLAG_VERIFY 0x00000004UL
#define KSWORD_ARK_STORAGE_CONTROLLER_TRANSFER_FLAG_FORCE 0x00000008UL
#define KSWORD_ARK_STORAGE_CONTROLLER_TRANSFER_FLAG_ALLOW_SYSTEM_DISK 0x00000010UL
#define KSWORD_ARK_STORAGE_CONTROLLER_TRANSFER_FLAG_UI_CONFIRMED 0x00000020UL
#define KSWORD_ARK_STORAGE_CONTROLLER_TRANSFER_FLAG_EXPECT_BEFORE_HASH 0x00000040UL

#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_OK 0UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_INVALID_REQUEST 1UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_NOT_SUPPORTED 2UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_NOT_READY 3UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_BUSY 4UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_CONFIRMATION_REQUIRED 5UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_OWNERSHIP_REQUIRED 6UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_GENERATION_MISMATCH 7UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_SESSION_MISMATCH 8UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_RANGE_INVALID 9UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_ALIGNMENT_REQUIRED 10UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_SYSTEM_DISK_BLOCKED 11UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_BEFORE_MISMATCH 12UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_IO_FAILED 13UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_VERIFY_FAILED 14UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_TIMEOUT 15UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_BUFFER_TOO_SMALL 16UL
#define KSWORD_ARK_STORAGE_CONTROLLER_STATUS_RECOVERY_UNAVAILABLE 17UL

#define KSWORD_ARK_STORAGE_CONTROLLER_CONFIRMATION_TOKEN 0x4B534343UL
#define KSWORD_ARK_STORAGE_CONTROLLER_MAX_TRANSFER_BYTES (256UL * 1024UL)
#define KSWORD_ARK_STORAGE_CONTROLLER_MODEL_CHARS 96U
#define KSWORD_ARK_STORAGE_CONTROLLER_SERIAL_CHARS 96U
#define KSWORD_ARK_STORAGE_CONTROLLER_DETAIL_CHARS 256U
#define KSWORD_ARK_STORAGE_CONTROLLER_AUDIT_ROWS 64UL
#define KSWORD_ARK_STORAGE_CONTROLLER_HASH_BYTES 32U
#define KSWORD_ARK_STORAGE_CONTROLLER_INDEX_UNAVAILABLE 0xFFFFFFFFUL

typedef struct _KSWORD_ARK_QUERY_STORAGE_CONTROLLER_REQUEST
{
    unsigned long version;
    unsigned long size;
    unsigned long flags;
    unsigned long reserved;
} KSWORD_ARK_QUERY_STORAGE_CONTROLLER_REQUEST,
  *PKSWORD_ARK_QUERY_STORAGE_CONTROLLER_REQUEST;

typedef struct _KSWORD_ARK_QUERY_STORAGE_CONTROLLER_RESPONSE
{
    unsigned long version;
    unsigned long size;
    unsigned long status;
    unsigned long controllerType;
    unsigned long capabilityFlags;
    unsigned long riskFlags;
    unsigned long ownership;
    unsigned long coherency;
    unsigned long generation;
    unsigned long controllerIndex;
    unsigned long portOrNamespace;
    unsigned long logicalSectorSize;
    unsigned long physicalSectorSize;
    unsigned long maximumTransferBytes;
    unsigned long pciSegment;
    unsigned long pciBus;
    unsigned long pciDevice;
    unsigned long pciFunction;
    unsigned long lastControllerStatus;
    long lastStatus;
    unsigned long reserved;
    unsigned long long capacityBytes;
    unsigned long long activeSessionId;
    wchar_t model[KSWORD_ARK_STORAGE_CONTROLLER_MODEL_CHARS];
    wchar_t serial[KSWORD_ARK_STORAGE_CONTROLLER_SERIAL_CHARS];
    wchar_t detail[KSWORD_ARK_STORAGE_CONTROLLER_DETAIL_CHARS];
} KSWORD_ARK_QUERY_STORAGE_CONTROLLER_RESPONSE,
  *PKSWORD_ARK_QUERY_STORAGE_CONTROLLER_RESPONSE;

typedef struct _KSWORD_ARK_CONTROL_STORAGE_CONTROLLER_REQUEST
{
    unsigned long version;
    unsigned long size;
    unsigned long command;
    unsigned long flags;
    unsigned long expectedGeneration;
    unsigned long controllerIndex;
    unsigned long portOrNamespace;
    unsigned long confirmationToken;
    unsigned long timeoutMilliseconds;
    unsigned long reserved;
    unsigned long long expectedSessionId;
} KSWORD_ARK_CONTROL_STORAGE_CONTROLLER_REQUEST,
  *PKSWORD_ARK_CONTROL_STORAGE_CONTROLLER_REQUEST;

typedef struct _KSWORD_ARK_CONTROL_STORAGE_CONTROLLER_RESPONSE
{
    unsigned long version;
    unsigned long size;
    unsigned long status;
    unsigned long riskFlags;
    unsigned long oldGeneration;
    unsigned long newGeneration;
    unsigned long ownership;
    unsigned long coherency;
    unsigned long controllerStatus;
    long lastStatus;
    unsigned long reserved[2];
    unsigned long long sessionId;
} KSWORD_ARK_CONTROL_STORAGE_CONTROLLER_RESPONSE,
  *PKSWORD_ARK_CONTROL_STORAGE_CONTROLLER_RESPONSE;

typedef struct _KSWORD_ARK_TRANSFER_STORAGE_CONTROLLER_REQUEST
{
    unsigned long version;
    unsigned long size;
    unsigned long operation;
    unsigned long flags;
    unsigned long expectedGeneration;
    unsigned long length;
    unsigned long confirmationToken;
    unsigned long timeoutMilliseconds;
    unsigned long long sessionId;
    unsigned long long offset;
    unsigned char expectedBeforeHash[KSWORD_ARK_STORAGE_CONTROLLER_HASH_BYTES];
    unsigned char data[1];
} KSWORD_ARK_TRANSFER_STORAGE_CONTROLLER_REQUEST,
  *PKSWORD_ARK_TRANSFER_STORAGE_CONTROLLER_REQUEST;

#define KSWORD_ARK_TRANSFER_STORAGE_CONTROLLER_REQUEST_HEADER_SIZE \
    FIELD_OFFSET(KSWORD_ARK_TRANSFER_STORAGE_CONTROLLER_REQUEST, data)

typedef struct _KSWORD_ARK_TRANSFER_STORAGE_CONTROLLER_RESPONSE
{
    unsigned long version;
    unsigned long size;
    unsigned long status;
    unsigned long riskFlags;
    unsigned long operation;
    unsigned long bytesTransferred;
    unsigned long generation;
    unsigned long controllerStatus;
    long lastStatus;
    unsigned long reserved;
    unsigned long long sessionId;
    unsigned long long offset;
    unsigned char beforeHash[KSWORD_ARK_STORAGE_CONTROLLER_HASH_BYTES];
    unsigned char afterHash[KSWORD_ARK_STORAGE_CONTROLLER_HASH_BYTES];
    unsigned char data[1];
} KSWORD_ARK_TRANSFER_STORAGE_CONTROLLER_RESPONSE,
  *PKSWORD_ARK_TRANSFER_STORAGE_CONTROLLER_RESPONSE;

#define KSWORD_ARK_TRANSFER_STORAGE_CONTROLLER_RESPONSE_HEADER_SIZE \
    FIELD_OFFSET(KSWORD_ARK_TRANSFER_STORAGE_CONTROLLER_RESPONSE, data)

typedef struct _KSWORD_ARK_STORAGE_CONTROLLER_AUDIT_ROW
{
    unsigned long long sequence;
    unsigned long long timestampTicks;
    unsigned long long sessionId;
    unsigned long long offset;
    unsigned long processId;
    unsigned long operation;
    unsigned long flags;
    unsigned long riskFlags;
    unsigned long length;
    unsigned long bytesTransferred;
    unsigned long status;
    long lastStatus;
    unsigned char beforeHash[KSWORD_ARK_STORAGE_CONTROLLER_HASH_BYTES];
    unsigned char afterHash[KSWORD_ARK_STORAGE_CONTROLLER_HASH_BYTES];
    unsigned char actorSidHash[KSWORD_ARK_STORAGE_CONTROLLER_HASH_BYTES];
    unsigned char mediaIdentityHash[KSWORD_ARK_STORAGE_CONTROLLER_HASH_BYTES];
} KSWORD_ARK_STORAGE_CONTROLLER_AUDIT_ROW,
  *PKSWORD_ARK_STORAGE_CONTROLLER_AUDIT_ROW;

typedef struct _KSWORD_ARK_QUERY_STORAGE_CONTROLLER_AUDIT_REQUEST
{
    unsigned long version;
    unsigned long size;
    unsigned long maximumRows;
    unsigned long reserved;
    unsigned long long afterSequence;
} KSWORD_ARK_QUERY_STORAGE_CONTROLLER_AUDIT_REQUEST,
  *PKSWORD_ARK_QUERY_STORAGE_CONTROLLER_AUDIT_REQUEST;

typedef struct _KSWORD_ARK_QUERY_STORAGE_CONTROLLER_AUDIT_RESPONSE
{
    unsigned long version;
    unsigned long size;
    unsigned long status;
    unsigned long rowCount;
    unsigned long availableCount;
    unsigned long truncated;
    unsigned long reserved[2];
    unsigned long long newestSequence;
    KSWORD_ARK_STORAGE_CONTROLLER_AUDIT_ROW rows[
        KSWORD_ARK_STORAGE_CONTROLLER_AUDIT_ROWS];
} KSWORD_ARK_QUERY_STORAGE_CONTROLLER_AUDIT_RESPONSE,
  *PKSWORD_ARK_QUERY_STORAGE_CONTROLLER_AUDIT_RESPONSE;
