matos/drivers/ata.h

110 lines
3.1 KiB
C
Raw Permalink Normal View History

2021-10-03 23:19:00 +02:00
#pragma once
#include "stdarg.h"
2021-10-05 23:10:10 +02:00
#include "synchro.h"
2021-10-03 23:19:00 +02:00
2021-10-05 21:59:36 +02:00
#define ATA_PIO_DATA 0
#define ATA_PIO_ERR 1
#define ATA_PIO_FEAT 1
2021-10-03 23:19:00 +02:00
#define ATA_PIO_SEC_COUNT 2
#define ATA_PIO_LBALO 3
#define ATA_PIO_LBAMID 4
#define ATA_PIO_CYL_LOW 4
#define ATA_PIO_LBAHI 5
#define ATA_PIO_CYL_HI 5
#define ATA_PIO_DRIVE 6
#define ATA_PIO_STATUS 7
#define ATA_PIO_CMD 7
2021-10-05 21:59:36 +02:00
// cmd details
// https://people.freebsd.org/~imp/asiabsdcon2015/works/d2161r5-ATAATAPI_Command_Set_-_3.pdf
2021-11-04 20:14:48 +01:00
#define ATA_PIO_CMD_IDENTIFY 0xEC /* get ATA params */
2021-10-05 23:10:10 +02:00
#define ATA_PIO_CMD_ATAPI_IDENTIFY 0xA1 /* get ATAPI params*/
#define ATA_PIO_CMD_READ 0x20 /* read command */
#define ATA_PIO_CMD_WRITE 0x30 /* write command */
#define ATA_PIO_CMD_READ_MULTI 0xC4 /* read multi command */
#define ATA_PIO_CMD_WRITE_MULTI 0xC5 /* write multi command */
#define ATA_PIO_CMD_SET_MULTI 0xC6 /* set multi size command */
#define ATA_PIO_CMD_PACKET_CMD 0xA0 /* set multi size command */
2021-11-04 20:14:48 +01:00
#define ATA_PIO_DRIVE_IBM 0xA0 /* bits that must be set */
#define ATA_PIO_DRIVE_LBA 0x40 /* use LBA ? */
#define ATA_PIO_DRIVE_MASTER 0x00 /* select master */
#define ATA_PIO_DRIVE_SLAVE 0x10 /* select slave */
2021-10-03 23:19:00 +02:00
#define ATA_PIO_ERROR_ADDR_MARK_NOT_FOUND 0
#define ATA_PIO_ERROR_TRACK_ZERO_NOT_FOUND 1
#define ATA_PIO_ERROR_ABORTED 2
#define ATA_PIO_ERROR_MEDIA_CHANGE_REQUEST 3
#define ATA_PIO_ERROR_ID_NOT_FOUND 4
#define ATA_PIO_ERROR_MEDIA_CHANGED 5
#define ATA_PIO_ERROR_UNCORRECTABLE_DATA 6
#define ATA_PIO_ERROR_BAD_BLOCK 7
2021-10-05 21:59:36 +02:00
#define ATA_PIO_STATUS_REG_ERR (1 << 0)
#define ATA_PIO_STATUS_IDX (1 << 1)
#define ATA_PIO_STATUS_CORP (1 << 2)
#define ATA_PIO_STATUS_DRQ (1 << 3)
#define ATA_PIO_STATUS_SRV (1 << 4)
#define ATA_PIO_STATUS_DRIVE_FAULT (1 << 5)
#define ATA_PIO_STATUS_DRIVE_RDY (1 << 6)
#define ATA_PIO_STATUS_DRIVE_BUSY (1 << 7)
#define MAX_ATA_CONTROLLER 2
#define MAX_ATA_DEVICES 2
#define DISK_SECTOR_SIZE 512
2021-11-04 20:14:48 +01:00
#define PART_TYPE_EXTENDED 0x5
#define PART_TYPE_FAT16 0x6
#define PART_TYPE_FAT16_LBA 0xe
#define PART_TYPE_FAT32 0xb
#define PART_TYPE_FAT32_LBA 0xc
#define PART_TYPE_LINUX_SWAP 0x82
#define PART_TYPE_LINUX 0x83
2021-10-05 21:59:36 +02:00
typedef enum {
ATA_DEV_UNKNOWN,
ATA_DEV_PATA,
ATA_DEV_SATA,
ATA_DEV_PATAPI,
ATA_DEV_SATAPI
} ata_type;
struct ata_device {
int id;
ata_type type;
2021-10-03 23:19:00 +02:00
int isSlave;
2021-10-05 21:59:36 +02:00
struct ata_controller *ctl;
int heads;
int cyls;
int sectors;
2021-10-03 23:19:00 +02:00
};
2021-10-05 21:59:36 +02:00
struct ata_controller {
int id;
int16_t base;
int16_t dev_ctl;
int present;
struct ata_device devices[MAX_ATA_DEVICES];
2021-10-05 23:10:10 +02:00
struct mutex mutex;
int last_device_used;
2021-10-05 21:59:36 +02:00
};
2021-10-03 23:19:00 +02:00
2021-11-04 20:14:48 +01:00
struct ata_partition {
int id;
uint type;
uint size;
uint32_t lba;
struct ata_device *device;
struct ata_partition *prev, *next;
};
2021-10-03 23:19:00 +02:00
int ATAInit();
2021-11-04 20:14:48 +01:00
int ATAReadPartitionSector(struct ata_partition *part, int offset, uint nbSector, void *buf);
2021-10-05 23:55:15 +02:00
int ATAReadSector(struct ata_device *dev, int lba, uint nbSector, void *buf);
int ATAWriteSector(struct ata_device *dev, int lba, uint nbSector, void *buf);
struct ata_device *ATAGetDevice(int ctlId, int devId);
struct ata_partition *ATAGetPartitionByType(uint type);
2021-11-04 20:14:48 +01:00
int ATAReadPartition(struct ata_device *dev);
struct ata_partition *ATAGetPartition(int id);