TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: DAVID VAN HOOSE
from: MIKE RUSKAI
date: 1999-12-13 11:36:00
subject: HPFS

Some senseless babbling from David Van Hoose to Mike Ruskai
on 12-13-99  07:07 about HPFS...

 -> I don't think any information I find would help there.  All of my
 -> "direct" sector reads are done via DosDevIOCtl(), which is a
 -> high-level interface, serviced by the base device driver required to
 -> operate the device in question (for IDE drives, IBM1S506.ADD, or a
 -> replacement).

 DVH> Damn. Well, can you send me some stuff on the high
 DVH> level end? It might help me a bit with the basic
 DVH> idea of how to do it. From there, I can learn how
 DVH> the BIOS fits into it all. :)

Here's a complete C program that reads the boot sector for drive C:

#define INCL_DOSDEVICES
#define INCL_DOSDEVIOCTL
#define INCL_DOSERRORS
#include 
#include 
#include 
#include 

USHORT setHeadCyl(ULONG secNum, PTRACKLAYOUT tl,
                  USHORT trackPerCyl, USHORT secPerTrack);

int main()
{
    PBIOSPARAMETERBLOCK bpb;
    PTRACKLAYOUT tl;
    HFILE driveHandle;
    FILE *sdf;
    PBYTE bpParm, bpData, sdata;
    ULONG bpParmLen, bpDataLen;
    ULONG openAction, openMode;
    APIRET rc;

    /*
        Allocate memory for currently known storage sizes.
    */

    bpb=(PBIOSPARAMETERBLOCK)malloc(sizeof(BIOSPARAMETERBLOCK));
    tl=(PTRACKLAYOUT)malloc(sizeof(TRACKLAYOUT)+sizeof(ULONG));
    bpParm=(PBYTE)malloc(2);
    bpData=(PBYTE)malloc(40);

    if (bpb==NULL || tl==NULL || bpParm==NULL || bpData==NULL)
        {
        printf("\nMemory allocation failure.\n");
        return 1;
        }

    openMode=0;
    openMode=openMode | OPEN_FLAGS_DASD | OPEN_ACCESS_READONLY
                      | OPEN_SHARE_DENYNONE | OPEN_FLAGS_NO_CACHE;

    /*
        Open drive for reading, to get a handle for using with category
        0x08 IOCtl functions.
    */

    rc=DosOpen("C:", &driveHandle, &openAction, 0L, 0L, FILE_OPEN,
               openMode, 0L);

    if (rc!=NO_ERROR)
        {
        printf("\nUnable to open drive C: for reading.\n");
        printf("\nDosOpen() returned %u.\n", rc);
        return 1;
        }

    memset(bpParm, 0, 2);
    memset(bpData, 0, 40);
    bpParmLen=2;
    bpDataLen=40;

    rc=DosDevIOCtl(driveHandle, IOCTL_DISK, DSK_GETDEVICEPARAMS,
                   bpParm, bpParmLen, &bpParmLen,
                   bpData, bpDataLen, &bpDataLen);

    if (rc!=NO_ERROR)
        {
        DosClose(driveHandle);
        printf("\nUnable to retrieve BIOS Parameter Block.\n");
        printf("\nDosDevIOCtl() returned %u.\n", rc);
        return 1;
        }

    memcpy(bpb, bpData, sizeof(BIOSPARAMETERBLOCK));

    sdata=(PBYTE)malloc(bpb->usBytesPerSector);

    if (sdata==NULL)
        {
        DosClose(driveHandle);
        printf("\nMemory allocation failure.\n");
        return 1;
        }

    bpParmLen=sizeof(TRACKLAYOUT)+sizeof(ULONG);
    bpDataLen=bpb->usBytesPerSector;

    tl->bCommand=0;
    tl->usHead=0;
    tl->usCylinder=0;
    tl->usFirstSector=0;
    tl->cSectors=1;

    tl->TrackTable[0].usSectorNumber=setHeadCyl(bpb->cHiddenSectors, tl,
        bpb->cHeads, bpb->usSectorsPerTrack);
    tl->TrackTable[0].usSectorSize=bpb->usBytesPerSector;

    rc=DosDevIOCtl(driveHandle, IOCTL_DISK, DSK_READTRACK, tl,
                   bpParmLen, &bpParmLen, sdata, bpDataLen, &bpDataLen);

    if (rc!=NO_ERROR)
        {
        DosClose(driveHandle);
        printf("\nUnable to read boot sector.\n");
        printf("\nDosDevIOCtl() returned %u.\n", rc);
        return 1;
        }

    DosClose(driveHandle);

    sdf=fopen("bootsect.dat", "wb");

    if (sdf==NULL)
        {
        printf("\nUnable to open sector data file.\n");
        return 1;
        }

    if (fwrite(sdata, bpDataLen, 1, sdf)!=1)
        {
        fclose(sdf);
        printf("\nUnable to write sector data.\n");
        return 1;
        }
    else
        {
        printf("\n%u-byte boot sector written to bootsect.dat\n",
               bpb->usBytesPerSector);
        }

    fclose(sdf);

    return 0;
}


USHORT setHeadCyl(ULONG secNum, PTRACKLAYOUT tl,
                  USHORT trackPerCyl, USHORT secPerTrack)
{
    ULONG secPerCyl=trackPerCyl*secPerTrack;
    ULONG wSecNum=secNum+1;

    tl->usCylinder=wSecNum/secPerCyl;
    tl->usHead=(wSecNum%secPerCyl)/secPerTrack;

    return (USHORT)((wSecNum%secPerCyl)%secPerTrack);
}

Mike Ruskai
thannymeister@yahoo.com


... FBI: Federal Bureau of Intimidation

___ Blue Wave/QWK v2.20
--- Platinum Xpress/Win/Wildcat5! v3.0pr2
* Origin: FIDO QWK MAIL & MORE! WWW.DOCSPLACE.ORG (1:3603/140)

SOURCE: echoes via The OS/2 BBS

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.