Link to home
Start Free TrialLog in
Avatar of dp7
dp7

asked on

IOCTL support for disk format

Hi,

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?

Thanks

dp7
Avatar of Salte
Salte

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.

Alf
ASKER CERTIFIED SOLUTION
Avatar of la_chose
la_chose

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