Title: Floppy Disk IO
1Floppy Disk I/O
2Objectives
- Direct interaction with floppy disk
- Organization of MS-DOS floppy
3Magnetic disk
Track
Sector
Read write head
Spindle
4MS DOS Format
- Before a disk can be used, it must be formatted
- Specific information is added in specific
locations on the disk. This varies by OS format
used - Logical sector 0 BOOT
- Logical sector 1 FAT1
- Logical sector 10 FAT2
- Logical sector 19 Root directory
5Boot Sector
0x00 0x02 ltA jump instruction to 0x1egt 0x03
0x0a Computer manufacturer name 0x0b 0x0c Sectors
per cluster 0x0d 0x0f Reserved sectors for boot
record 0x10 0x10 Number of FATs 0x11 0x12 Number
of root directory entries 0x13 0x14 Number of
logical sectors 0x15 0x15 Medium descriptor
byte 0x16 0x17 Sectors per FAT 0x18 0x19 Sectors
per track 0x1a 0x1b Number of surfaces
(heads) 0x1c 0x1d Number of hidden sectors 0x1e
... Bootstrap program
6Hard disk
- Microsoft systems hard disk can be partitioned to
have 4 logical disks, each with its own set of
logical sectors. - head 0, track 0, sector 1 will contain a
partition sector (like the boot sector)
7Drivers
- Windows NT device drivers
- User-mode driver Software runs in user mode and
uses the Win 32 API - Kernel-mode driver (Run in executive module)
- To run verification tests on kernel mode drivers
- Install your application with its kernel mode
drivers. - Start Verifier.exe and select the Modify Settings
tab.
8Problem Statement
Write a function to determine the basic
information about a diskette in logical drive A,
a function to read disk sectors, and a function
to dump the info you find in the floppy disk to
standard output stream. Note We can use
CreateFile, ReadFile and WriteFile to open, read
and write to a device.
9CreateFileparameter settings
CreateFile( \\\\.\\A, //note cant assume
user will ask for a //drive. You need to
get the letter from command //prompt.
GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS,
0, NULL )
10ReadFileread data on disk
BOOL ReadFile( HANDLE hFile, // handle of file
to read LPVOID lpBuffer, // pointer to buffer
that receives data DWORD nNumberOfBytesToRead,
// number of bytes to read LPDWORD
lpNumberOfBytesRead, // pointer to number of
bytes read LPOVERLAPPED lpOverlapped // pointer
to structure for data //set to NULL for lab
10 ) Reads data from a disk, starting at
starting at the address indicated by the file
pointer. Set the file pointer with function
SetFilePointer
11SetFilePointerAccess to the desired disk sector
DWORD SetFilePointer( HANDLE hFile, // handle
of file (from CreateFile()) LONG
lDistanceToMove, // number of bytes to move
//file pointer PLONG lpDistanceToMoveHigh
, // pointer to high-order //DWORD of
distance to move DWORD dwMoveMethod // how to
move (where to start) ) If this is NULL, then
number of bytes to move will be the 32 bit value
in lDistanceToMove If not Null then
lpDistanceToMove and lDistanceToMove form a
single 64-bit signed value.
12Using SetFilePointerHow do we know
LdistanceToMove?
Before we can use SetFilePointer and ReadFile, we
need to know how many bytes are in a sector. We
can get this data using the DeviceIoControl
function.
13DeviceIoControlto get the disk info
BOOL DeviceIoControl( Handle hDevice, //handle
to A DWORD dwloControlCode//control code of op
to perform LPVOID lpInBuffer, // buffer for
input data DWORD nInBufferSize // size of
buffer LPVOID lpOutBuffer// buffer for output
data // A PTR TO DISK_GEOMETRY DWORD
nOutBufferSize //size of output buffer
//SIZE OF DISK_GEOMETRY LPDWORD
lpBytesReturned,// ptr to put byte
count LPOVERLAPPED //wont use dwloControlCode
should be IOCTL_DISK_GET_DRIVE_GEOMETRY
14DISK_GEOMETRY
typedef struct _DISK_GEOMETRY LARGE_INTEGER
Cylinders MEDIA_TYPE MediaType //see next
slide DWORD TracksPerCylinder DWORD
SectorsPerTrack DWORD BytesPerSector
DISK_GEOMETRY The DeviceIoControl function
receives a DISK_GEOMETRY structure in response to
an IOCTL_DISK_GET_DRIVE_GEOMETRY device input and
output control (IOCTL) operation.
15MEDIA_TYPEfield of DISK_GEOMETRY
typedef enum _MEDIA_TYPE Unknown, // Format is
unknown F5_1Pt2_512, // 5.25", 1.2MB, 512
bytes/sector F3_1Pt44_512, // 3.5", 1.44MB, 512
bytes/sector F3_2Pt88_512, // 3.5", 2.88MB, 512
bytes/sector F3_20Pt8_512, // 3.5", 20.8MB, 512
bytes/sector F3_720_512, // 3.5", 720KB, 512
bytes/sector F5_360_512, // 5.25", 360KB, 512
bytes/sector F5_320_512, // 5.25", 320KB, 512
bytes/sector F5_320_1024, // 5.25", 320KB, 1024
bytes/sector F5_180_512, // 5.25", 180KB, 512
bytes/sector F5_160_512, // 5.25", 160KB, 512
bytes/sector RemovableMedia, // Removable media
other than floppy FixedMedia // Fixed hard disk
media MEDIA_TYPE
16User defined structdisk
Typedef struct disk Disk //ptr to a disk
structure struct disk HANDLE
floppyDisk DISK_GEOMETRY geometry note
you may want to change the name of the disk ptr
to something more obvious like lpDisk or
diskPtr.
Note This is Nutts revised structure, not the
same as in the book
17Function calls in main
The main expects 3 parameters, the drive letter,
the starting sector to read, and the ending
sector number to read. The main function reads in
the data passed in argv and then calls two
functions physicalDisk() sectorDump(..) Lots
of unrelated work is done in these functions
Both functions are more than 1 page (remember
the style guide?)
18User defined functionsDisk physicalDisk(char)
- Dynamically allocates memory for a disk struct
- Calls CreateFile (assigns handle to disk
floppyDisk data member) - Checks for successful open
- exits program if unsuccessful
- Calls DeviceIoControl( ) to get disk info
- Prints disk info to screen (from disk geometry
data member) - Returns Ptr disk struct
-
19User defined functions Void SectorDump(theDisk,
sectorNum)
- Dynamically allocates BYTE buffer
- to size of 1 sector (from geometry.
BytesPerSector) - Calculate the first byte of the sector (sectorNum
BytesPerSector) - (note if sectorNum BOOT or FAT1 or FAT2 use
- BOOT_SECTOR ,FAT_SECTOR1, FAT_SECTOR2 as defined
- in the header file.)
-
- Print sector to screen in Hex format
- printf( " 02x", bufi )
- Optional if sector contains printable
characters, - Print them in char format. function
- Look up isprint()
20User defined functions BOOL SectorRead(Disk,
unsigned, BYTE )
- Reads the contents of the sector.
-
- Call function SetFilePointer()
- Call function ReadFile()
- Returns true if read is successful
21(No Transcript)
22(No Transcript)