Solved

Reading Disk Sectors - Help

Posted on 1998-09-17
15
547 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
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 22

Accepted Solution

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

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 11

Expert Comment

by:alexo
Comment Utility
0
 
LVL 22

Expert Comment

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

But this is a DOS program.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
(win32 code)
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
emailed...
0
 
LVL 1

Expert Comment

by:dspector
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now