atapi 0.1.5

Low-level ATAPI driver in no_std environments.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#![no_std]
#![allow(dead_code)]
#![allow(private_interfaces)]

/// function for write to port 1 byte data
pub fn outb(port: u16, data: u8){
    unsafe {
        core::arch::asm!(
            "out dx, al",
            in("dx") port,
            in("al") data
        )
    }
}
/// function for write to port 1 byte data
pub fn outw(port: u16, data: u16){
    unsafe {
        core::arch::asm!(
            "out dx, ax",
            in("dx") port,
            in("ax") data
        )
    }
}/// function to get 8 bit data from port
pub fn inb(port: u16) -> u8{
    unsafe {
        let value: u8;
        core::arch::asm!(
            "in al, dx",
            in("dx") port,
            out("al") value
        );
        value
    }
}
/// function to get 16 bit data from port
pub fn inw(port: u16) -> u16{
    unsafe {
        let value: u16;
        core::arch::asm!(
            "in ax, dx",
            in("dx") port,
            out("ax") value
        );
        value
    }
}

/// R - Read, W - Write, B - Byte, W - Word LBA48
pub struct ATAPI {
    pub io_registers: IORegisters,
    pub control_registers: ControlRegisters
}
pub struct IORegisters{
    pub data_register_rw_w: u16,
    pub error_r_or_features_w_w: u16,
    pub sector_count_rw_w: u16,
    pub lba_low_rw_w: u16,
    pub lba_mid_rw_w: u16,
    pub lba_high_rw_w: u16,
    pub device_or_head_rw_b: u16,
    pub command_w_or_status_r_b: u16
}
pub struct ControlRegisters{
    pub alternate_status_register_r_b: u16,
    pub device_control_register_w_b: u16,
    pub drive_address_register_r_b: u16
}
#[repr(u8)]
pub enum DMAOrPIO{
    DMA = 1,
    PIO = 0
}
#[repr(u8)]
pub enum MasterOrSlave{
    Slave = 1 << 4,
    Master = 0
}
#[repr(u8)]
pub enum LBAOrCHS{
    LBA = 1 << 6,
    CHS = 0,
}
#[derive(Clone, Copy)]
#[repr(u16)]
pub enum PrimaryOrSecondary{
    Primary = 0x1F0,
    Secondary = 0x170
}
/// construction of ErrorRegister
/// each field contain value matched with name
/// EXAMPLE: AddressMarkNotFound = 1 << 0,
/// you can match value of your register with this enum
#[repr(u8)]
pub enum IOErrorRegister{
    AddressMarkNotFound     = 1 << 0,
    TrackZeroNotFound       = 1 << 1,
    AbortedCommand          = 1 << 2,
    MediaChangeRequest      = 1 << 3,
    IDNotFound              = 1 << 4,
    MediaChanged            = 1 << 5,
    UncorrectableDataError  = 1 << 6,
    BadBlockDetected        = 1 << 7
}

/// construction of StatusRegister
/// each field contain value matched with name
/// EXAMPLE: AddressMarkNotFound = 1 << 0,
/// you can match value of your register with this enum
#[repr(u8)]
pub enum IOStatusRegister{
    /// to reset this send a new command or nuke with software reset
    ErrorIndicator          = 1 << 0,
    IndexAlwaysZero         = 1 << 1,
    CorrectedDataAlwaysZero = 1 << 2,
    /// set when the drive has PIO data to transfer, or is ready to accept PIO data
    DRQ                     = 1 << 3,
    /// overlapped mode service request
    SRV                     = 1 << 4,
    DriveFaultError         = 1 << 5,
    /// Bit is clear when drive is spun down, or after an error. Set otherwise. 
    RDY                     = 1 << 6,
    /// Indicates the drive is preparing to send/receive data (wait for it to clear). 
    /// In case of 'hang' (it never clears), do a software reset. 
    BSY                     = 1 << 7
}
#[repr(u8)]
pub enum IODeviceOrHeadRegister{
    /// In CHS addressing, bits 0 to 3 of the head. In LBA addressing, bits 24 to 27 of the block number
    AddrHigh    = 1 << 0,
    ///  Selects the drive number
    DRV         = 1 << 4,
    /// Uses CHS addressing if clear or LBA addressing if set
    IsLBA       = 1 << 6,
}
#[repr(u8)]
pub enum ControlDeviceRegister{
    /// Set this to stop the current device from sending interrupts. 
    NIEN        = 1 << 1,    
}
impl ATAPI {
        pub fn new(base: PrimaryOrSecondary) -> Self {
        Self {
            io_registers: IORegisters {
                data_register_rw_w: base as u16,
                error_r_or_features_w_w: base as u16 + 1,
                sector_count_rw_w: base as u16 + 2,
                lba_low_rw_w: base as u16 + 3,
                lba_mid_rw_w: base as u16 + 4,
                lba_high_rw_w: base as u16 + 5,
                device_or_head_rw_b: base as u16 + 6,
                command_w_or_status_r_b: base as u16 + 7 
            },
            control_registers: ControlRegisters { 
                alternate_status_register_r_b: (base as u16) + 0x206,
                device_control_register_w_b: (base as u16) + 0x206,
                drive_address_register_r_b: (base as u16) + 0x206 + 1
            }
        }
    }
    /// this function set uo device or head register
        pub fn set_flags(
        &self, 
        master_or_slave: MasterOrSlave,
        lba_or_chs: LBAOrCHS)
    {
        outb(self.io_registers.device_or_head_rw_b, 
            master_or_slave as u8 | lba_or_chs as u8);
    }
    /// you need to use this function to set status of dma of PIO transfer
        pub fn set_dma_or_pio(&self, dma_or_pio: DMAOrPIO){
        outb(self.io_registers.error_r_or_features_w_w, dma_or_pio as u8);
    }
    /// you need to use this function after each command
        pub fn clear_cache(&self){
        outb(self.io_registers.command_w_or_status_r_b, 0xE7);
    }
    /// after this function need to clear_cache
        pub fn is_has_device(&self) -> bool {
        outb(self.io_registers.device_or_head_rw_b, 
            if self.io_registers.data_register_rw_w == 0x1F0 {0xA0} else {0xB0});
        outb(self.io_registers.sector_count_rw_w, 0);
        outb(self.io_registers.lba_low_rw_w, 0);
        outb(self.io_registers.lba_mid_rw_w, 0);
        outb(self.io_registers.lba_high_rw_w, 0);
        outb(self.io_registers.command_w_or_status_r_b, ATAPIOCommands::IdentifyDeviceB as u8);
        self.wait_drq_and_busy();
        if inb(self.io_registers.command_w_or_status_r_b) == 0 {
            false
        } else {
            if let None = self.wait_busy(){
                true
            } else {
                false
            }
        }
    }
    /// this function must to be used before each SCSCI command
    /// and than you need to use wait_drq function
        pub fn prepare_scsi(&self) {
        outb(self.io_registers.command_w_or_status_r_b, ATAOtherCommands::PacketB as u8);
    }
    /// this function wait while DRQ and BSY register is not ready
    /// you need to use this function after send prepare to SCSI command
        pub fn wait_drq_and_busy(&self) -> Option<IOStatusRegister> {
        if let Some(reg) = self.wait_busy(){
            Some(reg)
        } else {
            while inb(self.io_registers.command_w_or_status_r_b) & IOStatusRegister::DRQ as u8 == 0 {}
            None
        }
    }
    /// this function wait while command byte is busy
    /// if this function return None no one error register is not set
    /// otherwise return this register 
        pub fn wait_busy(&self) -> Option<IOStatusRegister>{
        while inb(self.io_registers.command_w_or_status_r_b) & IOStatusRegister::BSY as u8 != 0 {}
        let status = inb(self.io_registers.command_w_or_status_r_b);
        // if DriveFaultError ErrorIndicator not set
        if status & IOStatusRegister::DriveFaultError as u8 != 0 {
            Some(IOStatusRegister::DriveFaultError)
        } else if status & IOStatusRegister::ErrorIndicator as u8 != 0 {
            Some(IOStatusRegister::ErrorIndicator)
        } else {
            None
        }
    }
}
/// List of all SCSI comamnds with repr(u8)
/// Example: SCSCICommands::TestUnitReady as u8 -> 0x00
#[repr(u8)]
pub enum SCSICommands {
    TestUnitReady             	= 0x00,
    RequestSense           		= 0x03,
    FormatUnit	               	= 0x04,
    Inquiry		                = 0x12,
    StartStopUnitOrEjectDevice	= 0x1B,
    PreventAllowMediumRemoval	= 0x1E,
    ReadFormatCapacities	    = 0x23,
    ReadCapacity                = 0x25,
    Read10B		                = 0x28,
    Write10B              		= 0x2A,
    Seek10B		                = 0x2B,
    WriteAndVerify10B   		= 0x2E,
    Verify10B	                = 0x2F,
    SynchonizeCache	        	= 0x35,
    WriteBuffer		            = 0x3B,
    ReadBuffer             		= 0x3C,
    ReadTocORpmaORatip		    = 0x43,
    GetConfiguration	        = 0x46,
    GetEventStatusNotification	= 0x4A,
    ReadDiscInformation		    = 0x51,
    ReadTrackInformation	    = 0x52,
    ReserveTrack		        = 0x53,
    SendOpcInformation	    	= 0x54,
    ModeSelect10B		        = 0x55,
    RepairTrack		            = 0x58,
    ModeSense10B		        = 0x5A,
    CloseTrackSession		    = 0x5B,
    ReadBufferCapacity		    = 0x5C,
    SendCUESheet		        = 0x5D,
    ReportLUNS		            = 0xA0,
    Blank		                = 0xA1,
    SecurityProtocolIn		    = 0xA2,
    SendKey	                	= 0xA3,
    ReportKey		            = 0xA4,
    LoadOrUloadOrMedium		    = 0xA6,
    SetReadAhead         		= 0xA7,
    Read12B		                = 0xA8,
    Write12B		            = 0xAA,
    ReadMediaSerialNumber   	= 0xAB,
    ServiceActionIn12B          = 0x01,
    GetPerformance		        = 0xAC,
    ReadDiscStructure	      	= 0xAD,
    SecurityProtocolOut		    = 0xB5,
    SetStreaming           		= 0xB6,
    ReadCDMSF		            = 0xB9,
    SetCDspeed		            = 0xBB,
    MechanismStatus	        	= 0xBD,
    ReadCD		                = 0xBE,
    SendDiscStructure		    = 0xBF,
}
/// B - byte, W - word
#[repr(u8)]
pub enum ATADMACommands{
    DataSetManagementB          = 0x06,
    DataSetManagementXlB        = 0x07,
    GetPhysicalElementStatusW   = 0x12,
    ReadDMAExtW                 = 0x25,
    ReadDMAQueuedExtW           = 0x26,
    ReadStreamDmaExtW           = 0x2A,
    WriteDMAExtW                = 0x35,
    WriteDMAQueuedExtW          = 0x36,
    WriteStreamDMAExtW          = 0x3A,
    WriteDMAFuaExtW             = 0x3D,
    WWriteDMAQueuedFuaExtW      = 0x3E,
    ReadLogDMAExtW              = 0x47,
    ZacManagementInW            = 0x4A,
    WriteLogDMAExtW             = 0x57,
    TrustedRecieveDMAB          = 0x5D,
    TrustedSendDMAB             = 0x5F,
    ReadFPDMAQueuedW            = 0x60,
    WriteFPDMAQueuedW           = 0x61,
    SendFPDMAQueuedW            = 0x64,
    RecieveFPDMAQueuedW         = 0x65,
    DownloadMicrorodeDMAB       = 0x93,
    XacmanagementOutW           = 0x9F,
    NVCacheW                    = 0xB6,
    ReadDMAQueuedB              = 0xC7,
    ReadDMAB                    = 0xC8,
    ReadDmaWithoutRetryB        = 0xC9,
    WriteDmaB                   = 0xCA,
    WriteDmaWithoutRetryB       = 0xCB,
    WriteDMAQueuedB             = 0xCC,
    ReadBufferDMA               = 0xE9,
    WriteBufferDMA              = 0xEB,
    IdentifyDeviceDMA           = 0xEE
}
/// B - byte, W - word
#[repr(u8)]
pub enum ATAPIOCommands{
    ReadSectorsB                = 0x20,
    ReadSectorsWithoutRetryB    = 0x21,
    ReadLongB                   = 0x22,
    ReadLongWithoutRetryB       = 0x23,
    ReadSectorsExtW             = 0x24,
    ReadMultipleExtW            = 0x29,
    ReadStreamExtW              = 0x2B,
    ReadLogExtW                 = 0x2F,
    WriteSectorsB               = 0x30,
    WriteSectorsWithoutRetryB   = 0x31,
    WriteLongB                  = 0x32,
    WriteLongWithoutRetryB      = 0x33,
    WriteSectorsExtW            = 0x34,
    CfaWriteSectorsWithoutEraseB= 0x38,
    WriteMultipleExtW           = 0x39,
    WriteStreamExtW             = 0x3B,
    WriteVerifyB                = 0x3C,
    WriteLogExt                 = 0x3F,
    FormatTrackB                = 0x50,
    TrustedRecieveB             = 0x5C,
    TrustedSendB                = 0x5E,
    CfaTranslateSectorB         = 0x87,
    DownloadMicrocodeB          = 0x92,
    IdentifyPacketDeivceB       = 0xA1,
    SmartB                      = 0xB0,
    DeviceConfigurationOverlayB = 0xB1,
    ReadMultipleB               = 0xC4,
    WriteMultipleB              = 0xC5,
    CfaWriteMultipleWithoutEraseB= 0xCD,
    WriteMultipleFuaExtW        = 0xCE,
    ReadBufferB                 = 0xE4,
    WriteBufferB                = 0xE8,
    IdentifyDeviceB             = 0xEC,
    SecuritySetPasswordB        = 0xF1,
    SecurityUnlockB             = 0xF2,
    SecurityEraseUnitB          = 0xF4,
    SecurityDisablePasswordB    = 0xF6
}
/// B - byte, W - word
#[repr(u8)]
pub enum ATANoneCommands{
    NOPB                        = 0x00,
    CfaRequestExtendedErrorCodeB= 0x03,
    DeviceResetB                = 0x08,
    RequestSenseDataExtW        = 0x0B,
    ReadNativeMaxAddressExtW    = 0x27,
    SetMaxAddressExtW           = 0x37,
    ReadVerifySectorsB          = 0x40,
    ReadVerifySectorsWithoutRetryB= 0x41,
    ReadVerufySectorsExtW       = 0x42,
    ZeroExtW                    = 0x44,
    WriteUncorrectableExtW      = 0x45,
    ConfigureStreamW            = 0x51,
    TrustedNonDataB             = 0x5B,
    NcqNonDataW                 = 0x63,
    SetDateTimeExtW             = 0x77,
    AccessibleMaxAddressConfigurationW= 0x78,
    RemoveElementAndTruncateW   = 0x7C,
    RestoreElementsAndRebuildW  = 0x7D,
    RemoveElementAndModifyZonesW= 0x7E,
    ExecuteDeviceDiagnosticB    = 0x90,
    InitializeDeviceParametersB = 0x91,
    MutateExtW                  = 0x96,
    CheckPowerModeB             = 0x98,
    SleepW                      = 0x99,
    SetSectorConfigurationExtW  = 0xB2,
    SanitizeDeviceW             = 0xB4,
    CFAEraseSectorsB            = 0xC0,
    SetMultipleModeB            = 0xC6,
    CheckMediaCardTypeB         = 0xD1,
    GetMediaStatusB             = 0xDA,
    AcknowlegdeMediaChangeB     = 0xDB,
    BootPostBootB               = 0xDC,
    BootPrebootB                = 0xDD,
    MediaLockB                  = 0xDE,
    MediaUnlockB                = 0xDF,
    StandbyImmediateB           = 0xE0,
    IDLEImmediateB              = 0xE1,
    StandbyB                    = 0xE2,
    IDLEB                       = 0xE3,
    FlushCacheB                 = 0xE7,
    FlushCacheExtB              = 0xEA,
    MediaEjectB                 = 0xED,
    SetFeaturesB                = 0xEF,
    SecurityErasePrepare        = 0xF3,
    SecuriteFreezeLockB         = 0xF5,
    ReadNativeMaxAddressB       = 0xF8,
    SetMaxAddressB              = 0xF9
}
/// B - byte, W - word
#[repr(u8)]
pub enum ATAOtherCommands {
    PacketB     = 0xA0,
    ServiceB    = 0xA2,
}