Link to home
Start Free TrialLog in
Avatar of lupomage
lupomage

asked on

Reading Disk Sectors - Help

Hi, I have been asked to Write a C++ Program in DOS to Read
the Hard Disk Sector By Sector and Store Tbe Contents in One Huge Binary File then Read Back the Binary File into Hard Disk Sector By Sector.

I am Need Help on What C++/DOS Functions I should be looking
at to be able to perform this. (Not Tbe Actual Coding)

Please Help.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

First of all, You might want to get a copy of Ralph Brown's PC Interrupts.  It contains this information and lots of other related information.  You might find it helpful.

Anyway, you want to execute interrupt 13H with the registers set as follows.

AH = 02h (function number.)
AL = The number of sectors to be read.  Must be > 0.
CH = low byte of cylinder number of starting sector.
CL = Sector number in bits 0-5.  Cylinder number bits 8 and 9 in 2 high bits
DH = head number
DL = Driver number in bits 0-5  Cylinder number bits 10 and 11 in 2 high bits.
ES:BX = pointer to the memory to receive the data.

Note that the cylinder number is spread between 3 registers.  This is because the function evolved as hard disks got bigger.

The function returns with the carry flag set if there was an error.  AL contains the number of sectors actually read.
Avatar of lupomage

ASKER

I am Lost, But First Can You Give More Details of this Book you are Talking about.

Second , Is there any chance you Can pass me a Sample Code for the above Just to illustrate what you have explained.

If the Book has got Samples for this King of Things then No need for the Code Sample, Just Send Me Book Details
i.e ISBN, Title etc..

Thanks in Advance.
The book is "PC Interrupts" but Ralp Brown and Jim Kyle.  Pub. By Benchmark Productions. Copyright 1991  ISBN 0-20157797-6

It is the only decent reference on the BIOS interrupts used to control PC hardware.  However, it is not a how to book, it is a reference.  No examples.  Just the facts.  But there is nothing better.
First of all, are you aware that A) this requires assembly language, or at least inline assembly within C++ and B) that this cannot be done from a 32 bit program?

Oh I see you said DOS, is the 32 bit issue shouldn't matter. Then you just need to execure the interrupt using inline assembly.   That isn't too hard.  But I wonder if you know what sector(s) you want to read?
You can use something like the following.

bool ReadSector(int SecCnt, // Number of sectors to read.
 int Sec, // Sector number
 int Cyl, // Cylinder count.
 int Head, // Head number.
 int DrvNum, // Drive Number
 void *far MemPtr) // Far pointer to memory to receive sectors read.
{
   int i;

   i = Cyl & 0x0300; // Get bits 8-9
   i <<= 6; Shift bits left.
  Sec |= i;  // stick 2 cylinder bits into sector number.
   i = Cyl & 0x0C00; // Get bits 10-11
   i <<= 6; Shift bits left.
  Head |= i;  // stick 2 cylinder bits into head number.

   _asm {
     MOV AH,02H
    MOV AL,SecCnt
    MOV CH,Cyl
     MOV CL,Sec
    MOV DH,Head
    MOV DL,DrvNum
    LES BX,MemPtr
    int 13h
   }

}
Several comments:
1. The interrupt list is available on-line
2. You *can* access a hard disk's sectors under Win32.  NT makes it easy.  Win9x requires thunkning down to 1 16-bit DPMI DLL.  Very nasty...
3. No assembly language is required.  There are funcs like int86() or bios_disk() to set up the registers and issue the interrupt
>> You *can* access a hard disk's sectors under Win32.

But this is a DOS program.
Of course.  Just commenting on "B) that this cannot be done from a 32 bit program".
However, a DOS program will have a hard time accessing physical disk sectors if run under NT...
I too need to read/write directly on the diskette, specified by sector.

Full details of my experiments to date are in the Windows Programming topic area. If anyone can help me, you have 200 points to win!   :o)
Email me, get the code
(but don't forget to introduce yourself so I dont mistake it for a spam...)
(win32 code)
emailed...
I've had apparent success reading diskettes under Windows 98 (it is documented to work under Windows 95 OEM Service Release 2 also) using VWin32 (I have not done complete testing yet). Here is the code. (Note: Since I want to make diskettes uncopiable, I think I have to write over a format block. I don't think that standard absolute I/O will do that.)

A different approach (CreateFile/ReadFile - email me for code, doing it right is nontrivial) must be used for NT and Windows 2000.

David

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

// AbsIO.cpp
// For Windows 95?/98
// Uses VWIN32 to execute the instruction command Int 21h Function 7305h
// For the first release of Windows 95, use 21h Function 440Dh Minor Code 61h
// You can also try Int 21h Function 440Dh Minor Code 61h, and Int 25

#include "stdafx.h"

#define VWIN32_DIOC_DOS_INT25     2
#define VWIN32_DIOC_DOS_INT26     3
#define VWIN32_DIOC_DOS_DRIVEINFO 6

typedef struct _DIOC_REGISTERS {
    DWORD reg_EBX;
    DWORD reg_EDX;
    DWORD reg_ECX;
    DWORD reg_EAX;
    DWORD reg_EDI;
    DWORD reg_ESI;
    DWORD reg_Flags;
    } DIOC_REGISTERS, *PDIOC_REGISTERS;

#define CARRY_FLAG 1

#pragma pack(1)
typedef struct _DISKIO {
    DWORD       dwStartSector;   // starting logical sector number
    WORD        wSectors;        // number of sectors
    BYTE *      pBuffer;        // address of read/write buffer
    } DISKIO, * PDISKIO;
#pragma pack()

class               CUseAbsIO
    {
public:
    BOOL            m_Error;
    HANDLE          m_hVWin32;
    CUseAbsIO(BYTE *& pBuffer)
        {
        // "\\.\vwin32"
        m_hVWin32 = CreateFile("\\\\.\\VWIN32",
            0, 0, NULL, 0, FILE_FLAG_DELETE_ON_CLOSE, NULL);
        m_Error = (m_hVWin32 == INVALID_HANDLE_VALUE);
        pBuffer = new BYTE[4096]; // Adopted by caller
        }
    ~CUseAbsIO()
        {
        CloseHandle(m_hVWin32);
        }
    }; // CUseAbsIO

BOOL                AbsRead(BYTE *& pBuffer, const char DriveLetter,
                            WORD StartHead,
                            WORD StartCylinder,
                            WORD StartSector,
                            WORD NrSectors)
    {
    BOOL           fResult;
    DWORD          cb;
    DIOC_REGISTERS reg = {0};
    DISKIO         dio;
   
    CUseAbsIO       UseAbsIO(pBuffer);
    if (UseAbsIO.m_Error)
        return FALSE;

    dio.dwStartSector = StartSector;
    dio.wSectors      = NrSectors;
    dio.pBuffer      = pBuffer;
   
    reg.reg_EAX = 0x7305;   // Ext_ABSDiskReadWrite
    reg.reg_EBX = (DWORD)&dio;
    reg.reg_ECX = (DWORD)-1;
    reg.reg_EDX =  DriveLetter - 'A' + 1;   // Int 21h, fn 7305h drive numbers are 1-based
   
    fResult = DeviceIoControl(UseAbsIO.m_hVWin32, VWIN32_DIOC_DOS_DRIVEINFO,
        &reg, sizeof(reg),
        &reg, sizeof(reg), &cb, 0);
   
    // Determine if the DeviceIoControl call and the read succeeded.
    fResult = fResult && !(reg.reg_Flags & CARRY_FLAG);
    return fResult;
    } // AbsRead

BOOL                AbsWrite(BYTE *& pBuffer, const char DriveLetter,
                            WORD StartHead,
                            WORD StartCylinder,
                            WORD StartSector,
                            WORD NrSectors)
    {
    BOOL           fResult;
    DWORD          cb;
    DIOC_REGISTERS reg = {0};
    DISKIO         dio;
   
    CUseAbsIO       UseAbsIO(pBuffer);
    if (UseAbsIO.m_Error)
        return FALSE;

    dio.dwStartSector = StartSector;
    dio.wSectors      = NrSectors;
    dio.pBuffer      = pBuffer;
   
    reg.reg_EAX = 0x7305;   // Ext_ABSDiskReadWrite
    reg.reg_EBX = (DWORD)&dio;
    reg.reg_ECX = (DWORD)-1;
    reg.reg_EDX = DriveLetter - 'A' + 1;   // Int 21h, fn 7305h drive numbers are 1-based
    reg.reg_ESI = 0x6001;   // Normal file data (See function
    // documentation for other values)
   
    fResult = DeviceIoControl(UseAbsIO.m_hVWin32, VWIN32_DIOC_DOS_DRIVEINFO,
        &reg, sizeof(reg),
        &reg, sizeof(reg), &cb, 0);
   
    // Determine if the DeviceIoControl call and the write succeeded.
    fResult = fResult && !(reg.reg_Flags & CARRY_FLAG);
   
    return fResult;
    } // AbsWrite

// End of AbsIO.cpp