Sie sind auf Seite 1von 9

Mapping Logical Drives to Physical Hard Drives for MS-DOS

---------------------------------------------------------

Copyright (C) 1991-2008 Pierre R. Desloover

How does one go about converting MS-DOS logical drive


letters which represent hard disk physical partitions to their
equivalent ROM BIOS Hard Disk I/O Interrupt 13H physical hard
drive numbers (80H, 81H, etc.)? It's simple enough for floppy
disk logical drive letters A or B (just take the uppercase ASCII
logical drive letter and subtract the value of "A"), but for
hard disk logical drive letters, it's another matter. Given MS-
DOS logical drive D: (or a computer system with multiple hard
drives subdivided into numerous logical partitions), how does
one derive the associated ROM BIOS Hard Disk Interrupt 13H
parameters (hard disk number, starting head, cylinder and sector
numbers) needed to locate and access this logical drive directly
on a given hard disk?

Before I answer this question, however, let us consider


some of the practical uses of mapping logical drive letters to
physical hard disks. Suppose you had a program that could
perform this logical-to-physical mapping for you and display a
given logical drive's hard disk ROM BIOS hard disk number on the
screen. Armed with this program, you could walk up to an
unknown MS-DOS system and find out how many hard disks the
system has and on which hard disk a particular MS-DOS logical
drive letter is located on. Another practical use would be if
you were a developer of a hard disk defragmenter or space
optimizing application that needed to translate the user's
selection of a MS-DOS logical drive to the appropriate location
on a hard disk. If your application user specified MS-DOS
logical drive E:, then knowing the correct hard drive to address
and actual physical location of this logical drive's data on
that hard disk is a necessary part of that task. There's no
doubt that having a ready-made master look-up table containing
the host system's physical ROM BIOS hard drive information
arranged by logical drive number would be both useful and
convenient for the developer of this application.

The biggest obstacle to determining which hard drive a


particular MS-DOS logical drive is located on and where on that
hard disk it starts, is that MS-DOS does not assign logical
drive letters to logical partitions necessarily in the order
that they physically reside on a hard disk. For instance, if
you had two hard drives with a Primary partition and Extended
partition on each one, then MS-DOS will assign logical drive
1
letter C: to the first Primary partition on your first hard
drive, logical drive letter D: to the first Primary partition on
your second hard drive, then logical letter E: to the first
partition in the Extended partition on your first hard drive,
followed by letter F: for the first partition in the Extended
partition on your second drive. You will note that logical
drive D: is not physically located on the first drive, and
therefore its ROM BIOS hard disk number is 81H, not 80H, as one
might first be inclined to think. Furthermore, consider the
fact that logical drive E: has a ROM BIOS hard disk number of
80H because it's located on the first hard disk somewhere beyond
the first bootable partition logical drive C:. To obtain the
first sector from logical drive E: via ROM BIOS Interrupt 13H,
Function 2 (Read Sectors), it is necessary to know the exact
starting location of this logical drive in terms of physical
hard disk , head, track, and sector numbers to be supplied as
input arguments to this ROM BIOS function call.

This article features a sample program which


demonstrates a method for obtaining ROM BIOS Interrupt 13H
corresponding parameters for a hard disk logical drive. The
method is relatively straight forward, but requires an
exhaustive examination of a host system's hard disks and their
logical partitions. The first step is to perform a physical
inventory of a system's hard drives, i.e. determine how many
physical hard drives are present. The second step is to examine
each hard drives' MS-DOS master boot record and partition table
entries in order to find all the MS-DOS partitions and identify
a partition as either a Primary, Extended or Secondary primary
system type. Also, at this time, the starting head, cylinder
and sector numbers for those logical partitions are extracted
and saved since MS-DOS stores these physical parameters in the
partition tables themselves. Finally, once the number of hard
drives, number of logical partitions, and type of partitions on
each hard disk is known, the MS-DOS logical drive assignment
rules are applied to this hard disk information in order to
build a master look-up table ordered by MS-DOS logical drive
letters (C: to Z:) which contains the needed ROM BIOS physical
parameters for each partition on each hard drive.

How MS-DOS Partitions Hard Drives

Before we focus our attention on how MS-DOS assigns


logical drives to hard disk partitions, let's briefly review how
MS-DOS partitions hard drives. First, however, it is noteworthy
to point out that with the advent of MS-DOS version 5.0, the
maximum number of physical hard drives supported by MS-DOS has
2
been increased from two to eight, and that Secondary primary
partitions (other non-active bootable operating system partition
types) continue to be recognized for backward compatibility
reasons. The new maximum number of hard drives supported is a
limitation imposed by FDISK, and not the MS-DOS 5.0 kernel
initialization itself.

With MS-DOS 5.0 hard drives are partitioned in the same


manner as with MS-DOS versions 4.00 and 4.01. Physical space on
each hard disk space can be divided up via the FDISK utility
into Primary or Extended type partitions. The Primary and
bootable first partition can be defined to encompass the entire
hard disk space or to use only a portion of it with the
remainder of hard disk space assignable to an Extended partition
type. The Extended partition in turn may be subdivided into
multiple logical partitions. The real limitation as to the
maximum number of partitions that can be supported by MS-DOS is
determined by the fact that only logical drive letters C: - Z:
remain after logical drives A: and B: have been assigned to
floppy disk drives by the MS-DOS kernel initialization. Also
MS-DOS still requires that the first primary partition on a hard
disk be reserved for bootable logical drive C: for compatibility
reasons.

The Master partition table located at offset +1BEH from


the Master boot record on the first bootable hard disk, can
contain up to four entries or records. The first entry is
reserved for the Primary partition. The other entries can
denote an Extended type partition or even a Secondary primary
type partition created by a third party partitioning utility.
The entries in the Master partition table are actually 16 byte
records each which contain the Active (or bootable) flag,
partition type, starting head cylinder and sector numbers,
ending head, and the partition length in sectors. The partition
type byte within a partition record is a system indicator which
identifies partitions as having either a 12 or 16-bit sized MS-
DOS File Allocation Table (FAT) entry scheme. A partition type
value of 1 is assigned by MS-DOS to partitions with 12-bit Fat
entries, whereas values 4, 5 or 6 are used to mark 16-bit FAT
entries. The partition type value of 5 also means that it is an
Extended partition type, and a value of 6 is reserved for
partitions whose sizes are greater than 32 Megabytes (MS-DOS
4.00+). The information stored in each partition table record
can be used to determine the partition's type and the starting
head, cylinder, and sector values needed to physically locate it
on a hard disk, for example.

3
The Extended partition type entry in the Master
partition table can be thought of as a pointer to the start of
the Extended partition itself that is located physically beyond
the Primary partition on the hard disk. The Extended partition
has an extended boot record (non-bootable) and a logical drive
table analogous to the Primary Master boot record and its
partition table. This Extended logical drive table can contain
up to two entries or records. The first entry in the Extended
logical drive table is for the Extended partition itself (the
logical drive) and the second entry used to point to the next
possible Extended partition on the hard drive.

Highlights of Hard Disk Partitioning Changes through MS-DOS


versions:

MS-DOS Comments
---------------------------------------------------------------
2.00 Only one MS-DOS partition per partitioned device

3.30 Multiple logical partitions in new Extended part. type 5

4.00 Huge (> 32MB) partition type 6 added, 2 Gigabyte size max.

5.00 More than 2 hard drives recognized (up to 8 per FDISK)

How MS-DOS 5.0 Assigns Logical Drives

MS-DOS 5.0 assigns logical drive letters to physical


partitions on hard disks in the following manner. Starting with
the first hard disk, MS-DOS assigns logical drive letter C: to
the first Primary partition, and then attempts to assign the
next available logical drive letter to other Primary type
partitions on subsequent hard drives if they exist. All Primary
partitions are thus assigned first on up to eight hard drives.
MS-DOS then returns to the first hard disk and begins assigning
the next available logical drive letters to any Extended type
partitions on that hard disk. When all the possible Extended
type partitions have been assigned, then MS-DOS again progresses
to subsequent hard drives in order to assign logical drive
letters to their Extended type partitions if they exist. MS-DOS
once more returns to the first hard disk to continue assigning
logical drive letters to any Secondary primary partitions found
there this time. And as previously, MS-DOS continues to assign
logical drive letters to Secondary primary partitions on any
subsequent hard drive in the system. Finally, after all the
hard disk partitions are accounted for, MS-DOS goes on to assign
logical drive letters to possible third or fourth additional
4
floppy disk drives, and then to any other logical drives invoked
via CONFIG.SYS device driver statements, for example, a RAMDRIVE
or CD-ROM.

Sample .ASM Test Code using Logical-to-Physical Mapping

Summary

The test program code featured in this article serves as


a simple but efficient way to obtain and display multiple MS-DOS
Logical drives corresponding physical hard disk information.
The underlying function of interest, HDSysInventory, which
performs all the detective and deductive work, is invoked in the
test program via the single call to HDMapLogToPhy. The function
HDMapLogToPhy checks for existence of a final look-up table
named HDindex, and builds it via function HDSysInventory if
necessary prior to returning a pointer to that information to
the calling test routine. Note that any subsequent calls to
function HDMapLogToPhy by the Main test routine will not incur
the necessary initial overhead of performing the system hard
disk inventory and subsequent master look-up table preparation.

Any MS-DOS logical drive which fails the MS-DOS


Interrupt 21H IOCTL 440DH (Get Device Parameters) function call
or test for hard drive media type ID within this test routine,
does not generate a call to HDMapLogToPhy (floppies, Ram, CD-
ROM, tape, remote and unknown drive types could cause this).
Statistics are not returned or displayed for these media types.
Furthermore, the prepared look-up table HDindex only contains
valid entries for logical drives based on actual hard drives
found during the system inventory. Therefore, MS-DOS Assigned,
Substituted or Joined logical drives will not have a valid entry
in HDindex either, and HDMapLogToPhy will return an error status
to signal to caller that no data exists for these logical
drives. An obvious improvement to this test routine would be to
map such logical drives to their true identities and return a
pointer to that data instead of returning an error status.

See companion document for the Source Code. Assemble


with Microsoft MASM 5.0+ (or 6.0 with appropriate backward
compatibility command line switches) and link the single object
module to create an .EXE program type image file. A minimum
version of MS-DOS 3.1 is required to run the test program
successfully, since the test program Main routine uses the MS-
DOS Interrupt 21H IOCTL 440DH (Get Device Parameters) function
call to determine whether or not any one logical drive is
considered to be a hard drive type by MS-DOS.
5
High-Level Pseudo Code for the Test Program:

Main

FOR LOGICAL_DRV = A: TO Z:

IF LOGICAL_DRV is hard drive type (via Int 21H IOCTL Get Device
Parms)
THEN CALL HDMapLogToPhy (LOGICAL_DRV)
display returned stats for LOGICAL_DRV
(ELSE)
ENDIF

NEXT LOGICAL_DRV

End Main

Proc HDMapLogToPhy:

IF system inventory not done yet


THEN CALL HDSysInventory
(ELSE)
ENDIF

RETURN pointer to stats for LOGICAL_DRV

endproc

Proc HDSysInventory:

Set SYS_INVENTORY done flag


Perform system hard disk inventory
Build final logical-to-physical look-up table

RETURN

endproc

Program Structure

The program is structured in four parts: (1) Main, or


the test routine which calls the function HDMapLogToPhy, (2) the
HDMapLogToPhy function, (3) the HDSysInventory function, and (4)
miscellaneous support routines.
6
Function HDSysInventory takes a system's hard disk inventory and
builds two preliminary tables. The first contains information
needed to apply the MS-DOS logical letter rules of assignment to
the hard disk physical inventory and also link it to a second
table. The second table contains the actual starting head,
cylinder and sector numbers encountered during the inventory.
HDSysInventory then applies MS-DOS logical letter rules of
assignment to the first table and uses the information stored
there and in the second table to build a final look-up table
that can be indexed by a zero-based MS-DOS logical letter drive
number.

Function HDMapLogToPhy returns a pointer into the final look-up


table for a specified MS-DOS logical drive letter representing a
hard disk. The pointer points to the drive's associated data:
logical drive number, ROM BIOS hard disk number (80H, 81H etc.),
FAT type, starting head, cylinder and track numbers. Note that
these values are directly usable by ROM BIOS Interrupt 13H (DISK
I/O) Read, Write functions. HDMapLogToPhy can also return an
error status indicating that there was no valid associated data
found in the final look-up table for a particular MS-DOS logical
drive.

Note that the two underlying functions HDSysInventory


and HDMapLogToPhy, and their data structures, could be
retrofitted to serve in your version of a test Main routine or
more complex application that would simply reference the master
logical-to-physical table to convert MS-DOS logical drive
letters to corresponding ROM BIOS Interrupt 13H Read, Write
function parameters when needed.

Details on the Program Table Processing Algorithm Table Diagrams

Below are the three tables or structures used by the


test program:

Initial hardware inventory hard disk table HDtable:

Partition #Logical Sequence


Flags Drives Number
1st HD ? ? ?
2nd HD ? ? ?

7
. . . .

Last HD ? ? ?

Second initial hardware inventory partition data table HDphys:

BIOS FAT Start Start Start


HD # TYPE Head# Cyl# Sec#

HD 1, 1st Part ? ? ? ? ?
HD 1, 2nd Part ? ? ? ? ?

. . . . . .

HD 1, Last Part ? ? ? ? ?

HD N, 1st Part ? ? ? ? ?
HD N, 2nd Part ? ? ? ? ?

. . . . . .

HD N, Last Part ? ? ? ? ?

Final MS-DOS logical drive master look-up table HDindex:

DOS BIOS FAT Start Start Start


LOG# HD # TYPE Head# Cyl# Sec#

1st LOG ? ? ? ? ? ?
2nd LOG ? ? ? ? ? ?

. . . . . . .

Last LOG ? ? ? ? ? ?

How the Program Tables are Built and Processed

As hard drives are encountered during system inventory,


the first table's (HDtable) row entries for partition type and
total number logical drives are updated. The hard drive's type
of partition is recorded as either a Primary or Secondary type,
and the number of logical drives contained in any one partition
is tallied. The sequence number entry is to be used later to
8
create the final master index table. Also as hard disks
partitions are detected, the second hardware inventory table
(HDphys) is updated with each partition's physical
characteristics, i.e., the ROM BIOS hard disk number, Fat type,
starting head, cylinder, and sector numbers. The low order 7
bits of the starting cylinder number are stored as well as bits
5-0 of the starting sector number in this second table.

Once the initial physical inventory is complete, the MS-


DOS logical drive rules of assignment are applied to the first
table and associated partition data moved from the second table
to the third final master table (HDindex). The final look-up
table is organized by logical MS-DOS drive number so that the
associated physical data can be obtained for any one MS-DOS
logical drive number that represents a valid hard drive
partition. The algorithm processes the first table following the
MS-DOS logical drive rules of assignment and updates the first
table's values to reflect which partitions have been processed,
the number of logical drives in each one that remain to be
processed, and a sequence number which serves as an index into
the final master look-up table.

Das könnte Ihnen auch gefallen