IOCTL support for disk format

Posted on 2003-03-05
Medium Priority
Last Modified: 2008-02-01

I'm trying to write my own formating utility for various removable media in Visual C++. i have found out that I can use IOCTL device control codes. But I have problem with using them. Does anybody know what is the order of IOCTL codes to do a low level format and set up a apropriete partition and file system?
I found here an example for floppy disk, but most of the removable media are close to disk (or most of them apear to use the disk.sys driver)
Or does anybody know how can I get the list of supported IOCTL control codes directly from device drivers if I don't have a source code from these .sys drivers?
Is there any chance to do a media recovery?


Question by:dp7
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
LVL 12

Expert Comment

ID: 8073141
Unless you know something I don't I would assume it is the same codes as for floppy.

Be aware that formatting a hard disk is a very serious business and you must warn the user that all data on that disk is deleted when you do that.

Also be aware that in a multi tasking environment such as Win2000 or WinXP you can't just format the disk. The disk might be used by other tasks, so you first have to reserve the disk before you can format it.

Also, be aware that the disk which the system boot on (usually C:) cannot be reserved, since the system is using it. Disks that cannot be reserved cannot be formatted while the system is running. A drive such as C: would typically be formatted when you install a new OS on it and the formatting will be done before the system is up and running on it - i.e. in a controlled environment where there are no other tasks that might want to use the disk.

Other disks that cannot be reserved can under windows still be formatted but you place the command to do so in a special queue. When the system boots windows will check that queue and call any command stored in that queue. If that command is a format command the disk will be formatted before the system boot is completed. Once again, the format will be done in a controlled environment.

If the disk can be reserved, it means that no other application can access that disk and so you can just go ahead and format it.

Be sure you display proper warnings and so on to the user so that if he inadvertedly typed "format c:" he will get proper warnings and chance to cancel the operation before you attempt to do it or place the command into the boot queue I mentioned earlier.

If the boot is done in a non-windows environment such as unix it might be completely different though, generally 'formatting' in unix speak isn't the same as 'formatting' in windows speak. Formatting in unix speak (and most other OS except windows) means the low level formatting of a hard disk or other medium where the system divide the disk into blocks and places markers where each block begin and end and check that each block is ok to read and write from if any block give errors they are properly marked so that the disk won't attempt to access those block any more (so called bad blocks).

What windows refer to as 'formatting' is in all other OSes that I know of called 'creating a file system' and means that you attempt to create some form of file system on that medium.

Another term that is related to the two above is also 'partioning' where you divide a medium such as a hard disk into several partitions. Partitions is in reality a very simple file system which only has room for 1-4 or so files and each file must be contiguous. The files do not have names but are instead referred to as partitions. Each OS has their way of naming or numbering partitions. Each file is referred to as a 'partition' and is a portion of the disk that is available for a system which can view this partition as a medium by itself and therefore can create a filesystem or some other structure on that partition.

Note that partitions have a type - a one byte indicating what kind of partition it is and there is some (weak) attempt at using unique byte values for each partition type. Microsoft is infamous for stepping on other system's toes and using undocumented byte values and some times byte values which was really used by other systems for their own internal purpose. This can cause pain and much sorrow for a poor person who try to mix windows and some other OS that uses those bytes.

To my knowledge, windows isn't using the values used by Linux. Linux uses to my knowledge only two values. One for a regular file system and another for swap partition. Windows uses a bunch of different values in that type byte, one for FAT or FAT32, another for NTFS, and yet a bunch of others for internal and to some extent undocumented purposes.

It is really odd that they can't have separate values for each OS. One would think that since a byte can hold 256 different values there should be enough for everyone if each weren't so greedy. After all, the minimum would be for each system to use one type byte saying 'my system'. Then further info could be in the information in the initial sector of the partition. That would theoretically hold room for 256 different OSes and should be more than enough - there aren't that many different OSes that run on a PC. Anyway, if you ever get misinterpretations of partition table the reason is often that some system uses values also used by other vendors and they step on each other toes.


Accepted Solution

la_chose earned 225 total points
ID: 8075365

The best help you can get is from sysinternals:
It's a format utility with source code.

If you really want to do media recovery AND If you are advanced enough, try sniffing a existing software like (the example here is the first one I got w/ a search):
with the tools Irp Tracker and DeviceTree from OSR.
Softice will help too.

Featured Post

Independent Software Vendors: 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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

801 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