The file WinDriver.fth is the VFX Forth for Windows driver layer for testing the FAT file system with floppy drive A or a Flash drive.
For demonstration on a host PC, this simple harness connects to a floppy or USB drive on any Windows 2000/XP PC. You can set the drive letter using SetDrive below. Your C: drive won't work if it is formatted for NTFS. Also, drives with multiple partitions are currently not supported.
The file system needs these words to access the mass storage:
SecRead |
( addr sector dev -- ) Reads a sector from the specified device. THROWs on error. |
SecWrite |
( addr sector dev -- ) Writes a sector to the specified device. THROWs on error. |
MassInit |
( -- ) Initializes mass storage access. |
MassTerm |
( -- ) Terminates mass storage access. |
To simplify the code, the raw read/write interface treats all read/write errors as fatal, and THROWs on error. Retries should be accommodated within the sector read/write code.
From Microsoft's documentation on how to read raw data from a disk: To open a logical drive, direct access is of the form
\\.\X:
where X: is a hard-drive partition letter, floppy disk drive, or CD-ROM drive.
You can open a physical or logical drive using the CreateFile() application programming interface (API) with these device names provided that you have the appropriate access rights to the drive (that is, you must be an administrator). You must use both the CreateFile() FILE_SHARE_READ and FILE_SHARE_WRITE flags to gain access to the drive.
Once the logical or physical drive has been opened, you can then perform direct I/O to the data on the entire drive. When performing direct disk I/O, you must seek, read, and write in multiples of sector sizes of the device and on sector boundaries. Call DeviceIoControl() using IOCTL_DISK_GET_DRIVE_GEOMETRY to get the bytes per sector, number of sectors, sectors per track, and so forth, so that you can compute the size of the buffer that you will need.
For this implementation, sectors are 512-byte, sector numbers starting from 0. Addresses are RAM source/dest, and ior is nonzero if an error occurs.
This section depends on the target on which the FAT file system is being hosted. Recode this for your hardware or host operating system.
: CHS \ LBA -- Sector Head Cylinder
Calculates the address on the drive of the given sector.
By default, this word is commented out as it is usually only
required by old IDE drives.
|
1 constant devices
Number of physical mass storage devices supported.
create Drive$ \ -- addr
Holds a counted string holding the drive name string.
The drive letter is the fifth character.
: SetDrive \ char --
Select the required drive (0..n). Must be done before OpenDrive
below is run. The default is 'A'.
-1 value hDrive \ -- handle
Handle returned by Windows for the current raw drive.
Set to -1 when closed.
: openDrive \ --
Open the raw drive.
: closeDrive \ --
Close the raw drive.
: selDrive \ dev --
Devices are numbered 0..n. In this case an error occurs
if the device is non-zero.
: brw \ sector addr -- addr len handle
Given a sector number and a buffer address, return the address
sector length and the raw disc handle.
: SecRead \ addr sector dev --
Read a sector from the specified device.
An err_RawRead THROW occurs on error.
: SecWrite \ addr sector dev --
Write a sector to the specified device.
A err_RawWrite THROW occurs on error.
: MassInit \ --
Initialize the mass storage manager. This is done at startup.
: MassTerm \ --
Shut down the mass storage manager. This is done at program exit.