Skip to content

Latest commit

 

History

History
150 lines (122 loc) · 5.85 KB

File metadata and controls

150 lines (122 loc) · 5.85 KB

ATAPI

ATAPI (Advanced Technology Attachment Packet Interface) is a protocol used to communicate with CD-ROM, DVD-ROM, and other optical storage devices over the ATA (Advanced Technology Attachment) interface. Writing low-level code for ATAPI can be a complex process, but there are several steps that can be taken to simplify the process.

First, it is important to have a good understanding of the ATA/ATAPI protocol and how it works. This involves understanding the different types of ATA commands and how they are used to read and write data to the storage device. There are several resources available online that provide detailed information about the ATA/ATAPI protocol, including the ATA/ATAPI-6 specification and the SCSI Multimedia Commands-3 (MMC-3) specification.

Once you have a good understanding of the ATA/ATAPI protocol, the next step is to write low-level code to interface with the ATA controller. This involves setting up the registers and issuing commands to the ATA controller in order to communicate with the storage device. Some common tasks that low-level code might perform include detecting the presence of a device, initializing the device, and reading or writing data to the device.

Writing low-level code for ATAPI requires a good understanding of the hardware and software involved in the communication process. This can include knowledge of the ATA controller, the device driver, and the operating system. It is also important to have access to appropriate development tools, such as an assembler or debugger, in order to test and debug the code.

In conclusion, writing low-level code for ATAPI can be a challenging task, but with the right knowledge and tools, it is possible to develop robust and reliable code for communicating with optical storage devices over the ATA interface. By understanding the ATA/ATAPI protocol, setting up the appropriate registers and issuing commands, and testing and debugging the code, developers can create effective and efficient solutions for working with these types of devices.

Example code:

Note that the INQUIRY command returns a standard set of device identification information, but other ATAPI commands may have different response formats and data lengths. When sending other commands, the caller should adjust the pData parameter and dwDataLen parameters of the IssueAtapiCommand function to match the expected response data.

Linux

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <scsi/sg.h>

void IssueAtapiCommand(int fd, unsigned char opcode, unsigned char *param, unsigned int param_len, unsigned char *data, unsigned int data_len)
{
    unsigned char cmd[16] = { 0 };
    cmd[0] = opcode;
    memcpy(cmd + 1, param, param_len);
    cmd[4] = data_len;
    unsigned char sense[32] = { 0 };
    sg_io_hdr_t io_hdr = { 0 };
    io_hdr.interface_id = 'S';
    io_hdr.cmd_len = sizeof(cmd);
    io_hdr.mx_sb_len = sizeof(sense);
    io_hdr.dxfer_direction = (data_len > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
    io_hdr.dxfer_len = data_len;
    io_hdr.dxferp = data;
    io_hdr.cmdp = cmd;
    io_hdr.sbp = sense;
    io_hdr.timeout = 5000;
    if (ioctl(fd, SG_IO, &io_hdr) < 0)
    {
        // handle error
    }
}

int main()
{
    int fd = open("/dev/sr0", O_RDWR);
    if (fd < 0)
    {
        // handle error
    }

    // Send INQUIRY command
    const unsigned int inquiry_data_len = 96;
    unsigned char inquiry_data[inquiry_data_len] = { 0 };
    IssueAtapiCommand(fd, 0x12, NULL, 0, inquiry_data, inquiry_data_len);

    // Print INQUIRY data
    printf("Vendor ID: %.8s\n", inquiry_data + 8);
    printf("Product ID: %.16s\n", inquiry_data + 16);
    printf("Firmware Version: %.4s\n", inquiry_data + 32);
    printf("Serial Number: %.12s\n", inquiry_data + 36);

    close(fd);
    return 0;
}

Windows:

#include <windows.h>
#include <winioctl.h>
#include <ntddscsi.h>
#include <stdio.h>

#define ATAPI_IOCTL_CDROM_BASE     FILE_DEVICE_CD_ROM
#define IOCTL_CDROM_RAW_READ      CTL_CODE(ATAPI_IOCTL_CDROM_BASE, 0x000F, METHOD_OUT_DIRECT, FILE_READ_ACCESS)

void IssueAtapiCommand(HANDLE hDevice, BYTE bOpcode, BYTE bParam, BYTE bLen, BYTE* pData, DWORD dwDataLen)
{
    SCSI_PASS_THROUGH_DIRECT sptd = { 0 };
    sptd.Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
    sptd.PathId = 0;
    sptd.TargetId = 1;
    sptd.Lun = 0;
    sptd.CdbLength = bLen;
    sptd.DataIn = SCSI_IOCTL_DATA_IN;
    sptd.DataTransferLength = dwDataLen;
    sptd.TimeOutValue = 2;
    sptd.Cdb[0] = bOpcode;
    sptd.Cdb[1] = bParam;
    sptd.Cdb[2] = 0;
    sptd.Cdb[3] = 0;
    sptd.Cdb[4] = (BYTE)dwDataLen;
    sptd.Cdb[5] = 0;
    sptd.Cdb[6] = 0;
    sptd.Cdb[7] = 0;
    sptd.Cdb[8] = 0;
    sptd.Cdb[9] = 0;
    sptd.Cdb[10] = 0;
    sptd.Cdb[11] = 0;
    sptd.Cdb[12] = 0;
    sptd.Cdb[13] = 0;
    sptd.Cdb[14] = 0;

    DWORD dwReturned = 0;
    if (!DeviceIoControl(hDevice, IOCTL_SCSI_PASS_THROUGH_DIRECT, &sptd, sizeof(SCSI_PASS_THROUGH_DIRECT), &sptd, sizeof(SCSI_PASS_THROUGH_DIRECT), &dwReturned, NULL))
    {
        // handle error
    }

    if (pData != NULL)
    {
        memcpy(pData, sptd.DataBuffer, min(dwDataLen, sptd.DataTransferLength));
    }
}

int main()
{
    HANDLE hDevice = CreateFile("\\\\.\\CDROM0", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    if (hDevice == INVALID_HANDLE_VALUE)
    {
        // handle error
    }

    // Send INQUIRY command
    const DWORD dwInquiryDataLen = 96;
    BYTE byInquiryData[dwInquiryDataLen] = { 0 };
    IssueAtapiCommand(hDevice, SCSIOP_INQUIRY, 0, 6, byInquiryData, dwInquiryDataLen);

    // Print INQUIRY data
    printf("Vendor ID: %.8s\n", byInquiryData + 8);
    printf("Product ID: %.16s\n", byInquiryData + 16);
    printf("Firmware Version: %.4s\n", byInquiryData + 32);
    printf("Serial Number: %.12s\n", byInquiryData + 36);

    CloseHandle(hDevice);
    return 0;
}