the ata/ide interface

26
The ATA/IDE Interface Can we write a character- mode device driver for the hard disk?

Upload: teness

Post on 05-Feb-2016

32 views

Category:

Documents


3 download

DESCRIPTION

The ATA/IDE Interface. Can we write a character-mode device driver for the hard disk?. Persistent data storage. Our ‘dram.c’ module implemented a Linux driver for the processor’s physical memory That primary storage was ‘volatile’ – all its data disappears when power is turned off - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The ATA/IDE Interface

The ATA/IDE Interface

Can we write a character-mode device driver for the hard disk?

Page 2: The ATA/IDE Interface

Persistent data storage

• Our ‘dram.c’ module implemented a Linux driver for the processor’s physical memory

• That primary storage was ‘volatile’ – all its data disappears when power is turned off

• But secondary storage, provided by disks, is ‘persistent’ – the data is preserved even when the power supply gets interrupted

Page 3: The ATA/IDE Interface

Hardware interfacing

• To gain experience with writing drivers for actual hardware, we propose to construct a character-mode device driver, similar to our ‘dram.c’ module, which will allow us to read and write to a portion of the hard disk

• This will acquaint us – at an elementary level -- with many of the key issues that hardware interfacing typically involves

Page 4: The ATA/IDE Interface

A few cautions

• Our classroom and laboratory computers are shared by many users who are taking various computer sciences courses

• Writing to the hard disk in a careless way can do damage to the operating systems (making a machine completely unusable)

• Our first job will be to discover what areas on our hard disk can be safely modified

Page 5: The ATA/IDE Interface

Fixed-Size ‘blocks’

• All data-transfers to and from the hard disk are comprised of fixed-size blocks called ‘sectors’ (whose size equals 512 bytes)

• On modern hard disks, these sectors are identified by sector-numbers starting at 0

• This scheme for addressing disk sectors is known as Logical Block Addressing (LBA)

• So the hard disk is just an array of sectors

Page 6: The ATA/IDE Interface

Visualizing the hard disk

0 1 2 3 …..

A large array of 512-byte disk sectors

Disk storage-capacity (in bytes) = (total number of sectors) x (512 bytes/sector)

Page 7: The ATA/IDE Interface

Disk Partitions

• The total storage-area of the hard disk is usually subdivided into non-overlapping regions called ‘disk partitions’

Partition #1 Partition #2 Partition #3unused

Page 8: The ATA/IDE Interface

Master Boot Record

• A small area at the beginning of the disk is dedicated to ‘managing’ the disk partitions

• In particular, sector number 0 is known as the Master Boot Record (very important!)

MBR

0 1 2 …

partition #1

Page 9: The ATA/IDE Interface

Format of the MBR

• The MBR is subdivided into three areas:– The bootstrap loader program– The ‘partition table’ data-structure– The MBR signature (i.e., 0x55, 0xAA)

signature (2 bytes)

Partition Table (64 bytes)

Bootstrap Loader(446 bytes) 512

bytes

Page 10: The ATA/IDE Interface

‘Safe’ areas

• If our hard disk contains ‘unused’ sectors, then we could safely modify their contents

• Or if our hard disk contains a partition that nobody currently uses, then we could just take over its sectors for our own purposes -- at least during this current semester

• So we need to look at the ‘partition table’ to find out if any ‘safe areas’ are available

Page 11: The ATA/IDE Interface

‘Reading’ the MBR

• To get the hard disk’s Partition Table, we must ‘read’ the entire Master Boot Record

• (We ignore the boot-loader and signature)

• But we will need to understand the format of the data stored in that Partition Table

• And we will need to know how to devise a privileged code-fragment that can transfer the MBR (sector 0) from disk to memory

Page 12: The ATA/IDE Interface

Partition Table Entries

• The MBR is an array containing four data-structures (called ‘partition table entries’):

Starting sectorID-number

Partition length(in sectors)

STATUS

TYPE 16

bytes

Some fields contain ‘obsolete’ information

Page 13: The ATA/IDE Interface

TYPE-ID

• Each partition-table entry has a TYPE-ID– TYPE-ID is 0x07 for a ‘Windows’ partition– TYPE-ID is 0x83 for our ‘Linux’ partition– TYPE-ID is 0x00 when the entry is ‘unused’

• You can find a list of TYPE-ID numbers posted on the internet (see our website)

• Our hard disks have a ‘Minix’ partition that nobody is using during Spring semester

Page 14: The ATA/IDE Interface

How can we read the MBR?

• Our device-driver must send a command to the Hard Disk Controller, together with command-parameters that specify which disk sector we want to read (i.e., sector 0)

• But we can’t issue a fresh command if the controller is still busy processing an earlier command (e.g., issued by the OS kernel)

• So we also need to get controller ‘status’

Page 15: The ATA/IDE Interface

The ATA/IDE Interface

• All communication between our driver and the Hard Disk Controller is performed with ‘in’ and ‘out’ instructions that refer to ports

• PCs have standard i/o port-numbers for communicating with the Disk Controller

• Altogether twelve registers are involved, using nine different I/O Port-Numbers

Page 16: The ATA/IDE Interface

Command Block registers

• When reading…– Data – Error – Sector Count– LBA Low– LBA Mid– LBA High– Device– Status

• When writing…– Data– Features– Sector Count– LBA Low– LBA Mid– LBA High– Device– Command

Page 17: The ATA/IDE Interface

Control Block Registers

• When reading…– Alternate Status

• When writing…– Device Control

INCRITSInterNational Committee on Information Technology Standards

Committee T-13

Page 18: The ATA/IDE Interface

Algorithm overview

• First select the device to read from:– Wait until the controller is not busy and does

not have any data that it wants to transfer– Write to Command Block’s Device register to

select the disk to send the command to– Wait until the controller indicates that it is

ready to receive your new command

Page 19: The ATA/IDE Interface

Overview (continued)

• Place the command’s parameters into the appropriate Command Block registers

• Put command-code in Command register

• Then wait until the controller indicates that it has read the requested sector’s data and is ready to transfer it to your device driver

• Use a loop to input 256 words (one sector) from the Command Block’s Data register

Page 20: The ATA/IDE Interface

Overview (conclusion)

• After your driver has transferred a sector, check the Controller Status to see if there was any error (if so read the Error register)

• To implement this algorithm, we need to look at the meaning of some individual bits in the Status register (and Error register)

Page 21: The ATA/IDE Interface

Status register (port 0x1F7)

BSY DRDY DF DRQ ERR

Legend:

BSY (Device still Busy with prior command): 1=yes, 0=no DRDY (Device is Ready for a new command): 1=yes, 0=no DF (Device Fault – command cannot finish): 1=yes, 0=no DRQ (Data-transfer is currently Requested): 1=yes, 0=no ERR (Error information is in Error Register): 1 = yes, 0=no

7 6 5 4 3 2 1 0

Page 22: The ATA/IDE Interface

Device register (0x1F6)

1LBA(=1)

1DEV(0/1)

Sector-ID[ 27..24 ]

Legend:

LBA (Logical Block Addressing): 1=yes, 0=no DEV (Device selection): 1=slave, 0=master Sector-ID: Most significant 4-bits of 28-bit Sector-Address

7 6 5 4 3 2 1 0

Page 23: The ATA/IDE Interface

Error register (0x1F1)

UNC MC IDNF MCR ABRT NM

Legend:

UNC (Data error was UnCorrectable): 1=yes, 0=no MC (Media was Changed): 1=yes, 0=no IDNF (ID Not Found): 1=yes, 0=no MCR (Media Change was Requested): 1=yes, 0=no ABRT (Command was Aborted): 1 = yes, 0=no NM (No Media was present): 1=yes, 0=no

7 6 5 4 3 2 1 0

Page 24: The ATA/IDE Interface

Device Control register (0x3F6)

HOB 0 0 0 0 SRST nIEN 0

Legend:

HOB (High-Order Byte): 1=yes, 0=no SRST (Software Reset requested): 1=yes, 0=no nIEN (negate Interrupt Enabled): 1=yes, 0=no

NOTE: The HOB-bit is unimplemented on our machines; it isfor large-capacity disks that require 44-bit sector-addresses

7 6 5 4 3 2 1 0

Page 25: The ATA/IDE Interface

Demo module: ‘mbr.c’

• We have created a miniature device-driver which implements read-only access to our hard disk’s Master Boot Record (sector 0)

• Its purpose is two-fold: – It shows how you can read a disk sector– It lets you find out where the ‘minix’ partition

begins and ends on our disk (the ‘safe’ area)

Page 26: The ATA/IDE Interface

In-Class Exercise

• Compile and install the ‘mbr.c’ driver• Then write an application program that a

user can execute to open the ‘/dev/hd’ device-file, seek to the proper offset, read the 64-byte Partition Table data-structure, and display its four Partition Table Entries on the screen (in hexadecimal format)

• Identify the TYPE-ID of each partition, and see where the ‘minix’ area starts and ends