ATAPI: Difference between revisions
Jump to navigation
Jump to search
m
Ops sry forgot to add code tag
[unchecked revision] | [unchecked revision] |
(Made a better and working atapi reader code that can read multiple sectors :)) |
m (Ops sry forgot to add code tag) |
||
Line 268:
| 0xBF
|}
==x86 Examples==
Here is an example adapted from a working driver implementation.
<source lang="c">
// Handy register number defines
#define DATA 0
#define ERROR_R 1
Line 278 ⟶ 281:
#define COMMAND_REGISTER 7
// Control register defines
#define CONTROL 0x206
Line 294 ⟶ 298:
}
// This code is to wait 400 ns
static void ata_io_wait(const uint8_t p) {
inb(p + CONTROL + ALTERNATE_STATUS);
Line 303 ⟶ 308:
// Reads sectors starting from lba to buffer
int read_cdrom(uint16_t port, bool slave, uint32_t lba, uint32_t sectors, uint16_t *buffer) {
// The command
volatile uint8_t read_cmd[12] = {0xA8, 0,
(lba >> 0x18) & 0xFF, (lba >> 0x10) & 0xFF, (lba >> 0x08) & 0xFF,
Line 310 ⟶ 316:
0, 0};
outb(port + DRIVE_SELECT, 0xA0 & (slave << 4)); // Drive select
ata_io_wait(port);
outb(port + ERROR_R, 0x00);
outb(port + LBA_MID, 2048 & 0xFF);
outb(port + LBA_HIGH, 2048 >> 8);
outb(port + COMMAND_REGISTER, 0xA0); // Packet command
ata_io_wait(port); // I think we might need this delay, not sure, so keep this
// Wait for status
while (1) {
uint8_t status = inb(port + COMMAND_REGISTER);
Line 327 ⟶ 334:
}
// Send command
outsw(port + DATA, (uint16_t *) read_cmd, 6);
// Read words
for (uint32_t i = 0; i < sectors; i++) {
// Wait until ready
while (1) {
uint8_t status = inb(port + COMMAND_REGISTER);
Line 339 ⟶ 349:
int size = inb(port + LBA_HIGH) << 8
| inb(port + LBA_MID); // Get the size of transfer
insw(port + DATA, (uint16_t *) ((uint8_t *) buffer + i * 0x800), size / 2); // Read it
}
return 0;
}
</source>
==Detecting a Medium's Capacity==
|