-// Request SENSE
-static int
-atapi_get_sense(u16 device, u8 *asc, u8 *ascq)
-{
- u8 buffer[18];
- u8 atacmd[12];
- memset(atacmd, 0, sizeof(atacmd));
- atacmd[0] = ATA_CMD_REQUEST_SENSE;
- atacmd[4] = sizeof(buffer);
- int ret = ata_cmd_packet(device, atacmd, sizeof(atacmd), sizeof(buffer)
- , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
- if (ret != 0)
- return ret;
-
- *asc = buffer[12];
- *ascq = buffer[13];
-
- return 0;
-}
-
-static int
-atapi_is_ready(u16 device)
-{
- if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI) {
- printf("not implemented for non-ATAPI device\n");
- return -1;
- }
-
- dprintf(6, "ata_detect_medium: begin\n");
- u8 packet[12];
- memset(packet, 0, sizeof(packet));
- packet[0] = 0x25; /* READ CAPACITY */
-
- /* Retry READ CAPACITY 50 times unless MEDIUM NOT PRESENT
- * is reported by the device. If the device reports "IN PROGRESS",
- * 30 seconds is added. */
- u8 buf[8];
- u32 timeout = 5000;
- u32 time = 0;
- u8 in_progress = 0;
- for (;; time+=100) {
- if (time >= timeout) {
- dprintf(1, "read capacity failed\n");
- return -1;
- }
- int ret = ata_cmd_packet(device, packet, sizeof(packet), sizeof(buf)
- , MAKE_FARPTR(GET_SEG(SS), (u32)buf));
- if (ret == 0)
- break;
-
- u8 asc=0, ascq=0;
- ret = atapi_get_sense(device, &asc, &ascq);
- if (!ret)
- continue;
-
- if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
- dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
- return -1;
- }
-
- if (asc == 0x04 && ascq == 0x01 && !in_progress) {
- /* IN PROGRESS OF BECOMING READY */
- printf("Waiting for device to detect medium... ");
- /* Allow 30 seconds more */
- timeout = 30000;
- in_progress = 1;
- }
- }
-
- u32 block_len = (u32) buf[4] << 24
- | (u32) buf[5] << 16
- | (u32) buf[6] << 8
- | (u32) buf[7] << 0;
-
- if (block_len != 2048 && block_len != 512) {
- printf("Unsupported sector size %u\n", block_len);
- return -1;
- }
- SET_EBDA(ata.devices[device].blksize, block_len);
-
- u32 sectors = (u32) buf[0] << 24
- | (u32) buf[1] << 16
- | (u32) buf[2] << 8
- | (u32) buf[3] << 0;
-
- dprintf(6, "sectors=%u\n", sectors);
- if (block_len == 2048)
- sectors <<= 2; /* # of sectors in 512-byte "soft" sector */
- if (sectors != GET_EBDA(ata.devices[device].sectors))
- printf("%dMB medium detected\n", sectors>>(20-9));
- SET_EBDA(ata.devices[device].sectors, sectors);
- return 0;
-}
-
-static int
-atapi_is_cdrom(u8 device)
-{
- if (device >= CONFIG_MAX_ATA_DEVICES)
- return 0;
-
- if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI)
- return 0;
-
- if (GET_EBDA(ata.devices[device].device) != ATA_DEVICE_CDROM)
- return 0;
-
- return 1;
-}
-
-// Compare a string on the stack to one in the code segment.
-static int
-streq_cs(u8 *s1, char *cs_s2)
-{
- u8 *s2 = (u8*)cs_s2;
- for (;;) {
- if (*s1 != GET_VAR(CS, *s2))
- return 0;
- if (! *s1)
- return 1;
- s1++;
- s2++;
- }
-}
-