The Wayback Machine - https://web.archive.org/web/20080513200937/http://www.codeguru.com:80/cpp/cpp/cpp_mfc/files/article.php/c13809/

CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.
Voip -Voice over IP

jobs.internet.com

internet.commerce
Partners & Affiliates
Promote Your Website
Imprinted Gifts
Career Education
Shop Online
Computer Deals
Cell Phones
Compare Prices
Remote Online Backup
GPS
Home Improvement
Auto Insurance Quote
Dental Insurance
Car Donations
Shop


RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home >> Visual C++ / C++ >> C++ >> C++ & MFC >> File I/O

Project Management Guide: Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.

Extract Floppy Disk Geometry from the Boot Sector
How to get a floppy disk's geometry without using DeviceIoControl API
Rating: none

Mufti Mohammed (view profile)
June 6, 2007

Environment:  Visual Studio 6.0 and Windows XP professional Edition

Introduction

This article explains the structure of the BOOT sector of a floppy disk, which is formatted in the Windows environment for the FAT File System.


(continued)

Access FREE IBM Developer Tools:
Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

e-Kit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

e-Kit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.


Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.

The boot sector in a floppy disk resides in sector zero (0) of the disk. The size of the boot sector will always be 512 bytes for a floppy formatted with FAT. In the FAT file system, the boot code is closely interwoven with the file system. In this article, you will learn how to interpret this data to get floppy disk geometry.

Floppy Disk BOOT Sector Analysis

As I mentioned before, the boot sector size for a floppy disk is 512 bytes. Out of these 512 bytes, the first 36 bytes contain important information about floppy disk geometry. Before starting to dig in this area, please refer to the FAT specification as mentioned by Microsoft at http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx.

Data Structure for FAT Boot Sector

Byte Range Stored Information
0 - 2 Assembly Instruction for jump code.
3 - 10 OEM Name.
11 - 12 Bytes per sector.
13 Sector per cluster.
14 - 15 Number of reserved sector(Boot Sector)
16 Number of File Allocation Table
17 - 18 Maximum entries possible under root directory.
19 - 20 Total number of sectors in file system.
21 Media Type(According to Microsoft 0xf8 for fixed disk and 0xf0 for removable disk.
22 - 23 Sectors allocated for each File allocation table.
24 - 25 Sectors per track.
26 - 27 Number of head in storage device.
28 - 31 Number of sectors before start of partition(Not applicable for floppy).
32 - 35 Number of sectors in file system(32-bit value, not applicable for floppy).
36 BIOS INT13h drive number.
37 Not Used.
38 Extended boot signature.
39 - 42 Volume Serial Number.
43 - 53 Volume label in ASCII.
54 - 61 File System Type.
62 - 509 Boot Code, otherwise contains information to replace disk.
510 - 511 Signature for File System.

Data Structure for C++

From the above mentioned table, you got the boot sector information for a floppy disk formatted with FAT. Your next step will be to create a equivalent data structure in C++ to hold data read from the boot structure. So, I have created one structure as shown below:

typedef struct _BIOS_PARAM_BLOCK
{
   BYTE jumpCode[3];
   BYTE oemName[8];
   WORD bytes_Sector;
   BYTE sec_Cluster;
   WORD size_Sector_Reserved;
   BYTE fatCount;
   WORD Max_Root_Entry;
   WORD Total_Sector_FS;
   BYTE Media_Type;
   WORD sectors_per_fat;
   WORD sectors_per_track;
   WORD total_Head_Count;
   DWORD no_Sectors_Before_Part;
   DWORD no_Sector_FS32;
   BYTE BIOS_13h_Drive_No;
   BYTE reserved;
   BYTE ext_Boot_Part_Signature;
   DWORD vol_Serial_Number;
   BYTE vol_Lebel_Name[11];
   BYTE FS_Type[8];
   BYTE boot_Code[448];
   WORD signature;
} BPB;

Now, the structure is ready to hold data from the boot sector. The next step is to open the device and read sector zero. (This is the boot sector for a floppy formatted with FAT.) This step can be achieved with two WIN32 APIs, "CreateFile()" and "ReadFile()". For details of these two API, please refer to the MSDN documentation.

How to use the CreateFile() API

Please look at the following code:

HANDLE hFloppy = NULL;
hFloppy = CreateFile(
          "\\\\.\\A:",        // Floppy drive to open
          GENERIC_READ,       // Access mode
          FILE_SHARE_READ,    // Share Mode
          NULL,               // Security Descriptor
          OPEN_EXISTING,      // How to create
          0,                  // File attributes
          NULL);              // Handle to template

This code snippet opens a floppy disk and returns a handle. This handle will be used to read the floppy boot sector.

Integrate CreateFile() API with the ReadFile() API

The following code snippet is used to read the boot sector for a floppy and store it into a buffer of size 512. Then, I used memcpy() to copy the boot information from the buffer to the structured data.

#pragma pack(1)
typedef struct _BIOS_PARAM_BLOCK
{
   BYTE jumpCode[3];
   BYTE oemName[8];
   WORD bytes_Sector;
   BYTE sec_Cluster;
   WORD size_Sector_Reserved;
   BYTE fatCount;
   WORD Max_Root_Entry;
   WORD Total_Sector_FS;
   BYTE Media_Type;
   WORD sectors_per_fat;
   WORD sectors_per_track;
   WORD total_Head_Count;
   DWORD no_Sectors_Before_Part;
   DWORD no_Sector_FS32;
   BYTE BIOS_13h_Drive_No;
   BYTE reserved;
   BYTE ext_Boot_Part_Signature;
   DWORD vol_Serial_Number;
   BYTE vol_Lebel_Name[11];
   BYTE FS_Type[8];
   BYTE boot_Code[448];
   WORD signature;
} BPB;
#pragma pack()

void PrintFloppyInformation(BPB _bpb);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
   int nRetCode = 0;
   // initialize MFC and print an error on failure
   if (!AfxWinInit(::GetModuleHandle(NULL), NULL,
       ::GetCommandLine(), 0))
   {
      // TODO: change error code to suit your needs
      cerr << _T("Fatal Error: MFC initialization failed") << endl;
      nRetCode = 1;
   }
   else
   {
      // TODO: code your application's behavior here.
      BYTE bBootSector[512];
      memset(bBootSector, 0, 512);
      DWORD dwBytesRead(0);
      BPB _bpb;

      HANDLE hFloppy = NULL;
      hFloppy = CreateFile("\\\\.\\A:",    // Floppy drive to open
                GENERIC_READ,              // Access mode
                FILE_SHARE_READ,           // Share Mode
                NULL,                      // Security Descriptor
                OPEN_EXISTING,             // How to create
                0,                         // File attributes
                NULL);                     // Handle to template

      if(hFloppy != NULL)
      {
         // Read the boot sector
         if (!ReadFile(hFloppy, bBootSector, 512, &dwBytesRead, NULL))
         {
            printf("Error in reading floppy disk\n");
         }
         else
         {
            memcpy(&_bpb, bBootSector, 512);
            // Print floppy information on Console.
            PrintFloppyInformation(_bpb);
         }

         CloseHandle(hFloppy);
         // Close the handle
      }
   }

   return nRetCode;
}

Now, I have printed values on the console using the following code snippet.

void PrintFloppyInformation(BPB _bpb)
{
   printf("Floppy Disk Information: \n");
   printf("===========================\n");
   printf("Assembly Instruction to jump to Boot code: 0x%x\n",
          _bpb.jumpCode);
   printf("OEM Name: %s\n", _bpb.oemName);
   printf("Bytes per sector: %d\n", _bpb.bytes_Sector);
   printf("Sector per cluster: %d\n", _bpb.sec_Cluster);
   printf("Size in sector for reserved area(Boot Sector): %d\n",
          _bpb.size_Sector_Reserved);
   printf("Number of FATs(File Allocation Table): %d\n",
          _bpb.fatCount);
   printf("Number of files for root directory: %d\n",
           _bpb.Max_Root_Entry);
   printf("Number of Sectors in File System: %d\n",
          _bpb.Total_Sector_FS);
   printf("Media Type\n(According to Microsoft,
          0xF8 == fixed disk and 0xF0 == Removable disk):
          0x%x\n", _bpb.Media_Type);
   printf("Number of Sectors for each FAT: %d\n",
          _bpb.sectors_per_fat);
   printf("Sectors per track: %d\n", _bpb.sectors_per_track);
   printf("Number of head in storage device: %d\n",
          _bpb.total_Head_Count);
   printf("BIOS INT13h Drive number: 0x%x\n", _bpb.BIOS_13h_Drive_No);
   printf("Volume Serial Number: %d\n", _bpb.vol_Serial_Number);
   printf("Volume label Name: %s\n", _bpb.vol_Lebel_Name);
   printf("Boot Sector Signature: 0x%x\n", _bpb.signature);
}

That's all. Now, run the program and you will have all the information about a floppy disk. Please look at following picture and assemble all the required information and floppy disk geometry is in front of you.


(Full Size Image)

Do some calculation based on the picture above:

  1. 0th Sector reserved one (Boot Sector).
  2. The next 18 sectors are allocated for FAT (you have two copies of the FAT with nine sectors each).
  3. It means that the First 19 sectors for Boot Sector + FATs.
  4. The next few sectors are for file name entry with cluster address (called as root directory).
  5. Now, from the picture above, it is clear that the FAT for a floppy will have 224 max entries.
  6. Each entry is 32 bytes in size. Then total bytes are allocated for root entries are (32*224) bytes, which is 7168 bytes.
  7. 7168 bytes is equal to 14 sectors because each sector is 512 bytes in size.
  8. You have 2880 total sectors for the floppy. Please refer to the picture above.
  9. Out of the 2880 sectors for the floppy, the boot sector + FATs sectors + Root Directory sectors are fixed. This comes to 33 sectors altogether.
  10. The rest of the sectors are used for data storage. In other words, 2847 sectors are used for data storage.

So, in summary, the floppy geometry looks like the following figure:

About the Author
Senior Software Engineer in Ness Technologies (India) Ltd.

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

Best Practices for Developing a Web Site. Checklists, Tips & Strategies. Download Exclusive eBook Now.
Data Sheet: IBM Information Server Blade
Flash Demo: Learn how IBM Information Server Blade is easy to manage, highly scalable and efficient.
Intel Go Parallel Portal: Translating Multicore Power into Application Performance
Guide to Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.


RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
No Comments Posted.
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer �08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server �08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES