ata.h 5.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* ata.h - ATA disk access.  */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef GRUB_ATA_HEADER
#define GRUB_ATA_HEADER 1

#include <grub/misc.h>
#include <grub/symbol.h>
25
#include <grub/disk.h>
26 27 28 29 30 31 32 33 34 35
/* XXX: For now this only works on i386.  */
#include <grub/cpu/io.h>

typedef enum
  {
    GRUB_ATA_CHS,
    GRUB_ATA_LBA,
    GRUB_ATA_LBA48
  } grub_ata_addressing_t;

36 37 38 39 40 41
#define GRUB_ATA_CH0_PORT1 0x1f0
#define GRUB_ATA_CH1_PORT1 0x170

#define GRUB_ATA_CH0_PORT2 0x3f6
#define GRUB_ATA_CH1_PORT2 0x376

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
#define GRUB_ATA_REG_DATA	0
#define GRUB_ATA_REG_ERROR	1
#define GRUB_ATA_REG_FEATURES	1
#define GRUB_ATA_REG_SECTORS	2
#define GRUB_ATAPI_REG_IREASON	2
#define GRUB_ATA_REG_SECTNUM	3
#define GRUB_ATA_REG_CYLLSB	4
#define GRUB_ATA_REG_CYLMSB	5
#define GRUB_ATA_REG_LBALOW	3
#define GRUB_ATA_REG_LBAMID	4
#define GRUB_ATAPI_REG_CNTLOW	4
#define GRUB_ATA_REG_LBAHIGH	5
#define GRUB_ATAPI_REG_CNTHIGH	5
#define GRUB_ATA_REG_DISK	6
#define GRUB_ATA_REG_CMD	7
#define GRUB_ATA_REG_STATUS	7

#define GRUB_ATA_REG2_CONTROL	0

#define GRUB_ATA_STATUS_ERR	0x01
#define GRUB_ATA_STATUS_INDEX	0x02
#define GRUB_ATA_STATUS_ECC	0x04
#define GRUB_ATA_STATUS_DRQ	0x08
#define GRUB_ATA_STATUS_SEEK	0x10
#define GRUB_ATA_STATUS_WRERR	0x20
#define GRUB_ATA_STATUS_READY	0x40
#define GRUB_ATA_STATUS_BUSY	0x80

/* ATAPI interrupt reason values (I/O, D/C bits).  */
#define GRUB_ATAPI_IREASON_MASK     0x3
#define GRUB_ATAPI_IREASON_DATA_OUT 0x0
#define GRUB_ATAPI_IREASON_CMD_OUT  0x1
#define GRUB_ATAPI_IREASON_DATA_IN  0x2
#define GRUB_ATAPI_IREASON_ERROR    0x3

enum grub_ata_commands
  {
    GRUB_ATA_CMD_CHECK_POWER_MODE	= 0xe5,
    GRUB_ATA_CMD_IDENTIFY_DEVICE	= 0xec,
    GRUB_ATA_CMD_IDENTIFY_PACKET_DEVICE	= 0xa1,
    GRUB_ATA_CMD_IDLE			= 0xe3,
    GRUB_ATA_CMD_PACKET			= 0xa0,
    GRUB_ATA_CMD_READ_SECTORS		= 0x20,
    GRUB_ATA_CMD_READ_SECTORS_EXT	= 0x24,
86 87 88
    GRUB_ATA_CMD_READ_SECTORS_DMA	= 0xc8,
    GRUB_ATA_CMD_READ_SECTORS_DMA_EXT	= 0x25,

89 90 91 92 93 94 95
    GRUB_ATA_CMD_SECURITY_FREEZE_LOCK	= 0xf5,
    GRUB_ATA_CMD_SET_FEATURES		= 0xef,
    GRUB_ATA_CMD_SLEEP			= 0xe6,
    GRUB_ATA_CMD_SMART			= 0xb0,
    GRUB_ATA_CMD_STANDBY_IMMEDIATE	= 0xe0,
    GRUB_ATA_CMD_WRITE_SECTORS		= 0x30,
    GRUB_ATA_CMD_WRITE_SECTORS_EXT	= 0x34,
96 97
    GRUB_ATA_CMD_WRITE_SECTORS_DMA_EXT	= 0x35,
    GRUB_ATA_CMD_WRITE_SECTORS_DMA	= 0xca,
98 99 100 101 102
  };

enum grub_ata_timeout_milliseconds
  {
    GRUB_ATA_TOUT_STD  =  1000,  /* 1s standard timeout.  */
103
    GRUB_ATA_TOUT_DATA = 10000,   /* 10s DATA I/O timeout.  */
104
    GRUB_ATA_TOUT_SPINUP  =  10000,  /* Give the device 10s on first try to spinon.  */
105 106
  };

107
typedef union
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
  grub_uint8_t raw[11];
  struct
  {
    union
    {
      grub_uint8_t features;
      grub_uint8_t error;
    };
    union
    {
      grub_uint8_t sectors;
      grub_uint8_t atapi_ireason;
    };
    union
    {
      grub_uint8_t lba_low;
      grub_uint8_t sectnum;
    };
    union
    {
      grub_uint8_t lba_mid;
      grub_uint8_t cyllsb;
      grub_uint8_t atapi_cntlow;
    };
    union
    {
      grub_uint8_t lba_high;
      grub_uint8_t cylmsb;
      grub_uint8_t atapi_cnthigh;
    };
    grub_uint8_t disk;
    union
    {
      grub_uint8_t cmd;
      grub_uint8_t status;
    };
    grub_uint8_t sectors48;
    grub_uint8_t lba48_low;
    grub_uint8_t lba48_mid;
    grub_uint8_t lba48_high;
  };
} grub_ata_regs_t;
151

152 153 154 155 156 157 158 159 160
/* ATA pass through parameters and function.  */
struct grub_disk_ata_pass_through_parms
{
  grub_ata_regs_t taskfile;
  void * buffer;
  grub_size_t size;
  int write;
  void *cmd;
  int cmdsize;
161
  int dma;
162
};
163

164 165
struct grub_ata
{
166 167 168 169 170 171 172
  /* Addressing methods available for accessing this device.  If CHS
     is only available, use that.  Otherwise use LBA, except for the
     high sectors.  In that case use LBA48.  */
  grub_ata_addressing_t addr;

  /* Sector count.  */
  grub_uint64_t size;
173
  grub_uint32_t log_sector_size;
174 175 176 177 178 179 180 181 182

  /* CHS maximums.  */
  grub_uint16_t cylinders;
  grub_uint16_t heads;
  grub_uint16_t sectors_per_track;

  /* Set to 0 for ATA, set to 1 for ATAPI.  */
  int atapi;

183 184
  int dma;

185 186
  grub_size_t maxbuffer;

187
  int *present;
188

189 190 191
  void *data;

  struct grub_ata_dev *dev;
192 193
};

194
typedef struct grub_ata *grub_ata_t;
195

196 197
typedef int (*grub_ata_dev_iterate_hook_t) (int id, int bus, void *data);

198
struct grub_ata_dev
199
{
200
  /* Call HOOK with each device name, until HOOK returns non-zero.  */
201
  int (*iterate) (grub_ata_dev_iterate_hook_t hook, void *hook_data,
202
		  grub_disk_pull_t pull);
203

204 205
  /* Open the device named NAME, and set up SCSI.  */
  grub_err_t (*open) (int id, int bus, struct grub_ata *scsi);
206

207 208
  /* Close the scsi device SCSI.  */
  void (*close) (struct grub_ata *ata);
209

210 211 212
  /* Read SIZE bytes from the device SCSI into BUF after sending the
     command CMD of size CMDSIZE.  */
  grub_err_t (*readwrite) (struct grub_ata *ata,
213 214
			   struct grub_disk_ata_pass_through_parms *parms,
			   int spinup);
215

216 217 218 219 220
  /* The next scsi device.  */
  struct grub_ata_dev *next;
};

typedef struct grub_ata_dev *grub_ata_dev_t;
221

222 223
void grub_ata_dev_register (grub_ata_dev_t dev);
void grub_ata_dev_unregister (grub_ata_dev_t dev);
224 225

#endif /* ! GRUB_ATA_HEADER */