Solved

Reading Disk Sectors - Help

Posted on 1998-09-17
15
557 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:lupomage
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1172945
You need to use Interrupt 13 h function 2.  details follow.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1172946
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.
0
 

Author Comment

by:lupomage
ID: 1172947
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 1172948
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1172949
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?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1172950
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
   }

}
0
 
LVL 11

Expert Comment

by:alexo
ID: 1172951
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
0
 
LVL 11

Expert Comment

by:alexo
ID: 1172952
0
 
LVL 22

Expert Comment

by:nietod
ID: 1172953
>> You *can* access a hard disk's sectors under Win32.

But this is a DOS program.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1172954
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...
0
 
LVL 1

Expert Comment

by:dspector
ID: 5395280
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)
0
 
LVL 11

Expert Comment

by:alexo
ID: 5403282
Email me, get the code
(but don't forget to introduce yourself so I dont mistake it for a spam...)
0
 
LVL 11

Expert Comment

by:alexo
ID: 5403299
(win32 code)
0
 
LVL 11

Expert Comment

by:alexo
ID: 5405100
emailed...
0
 
LVL 1

Expert Comment

by:dspector
ID: 5416100
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question