2012年2月29日 星期三

Access Physical Disk Content ( partitiion, boot sector, FAT)

Physical Disks and Volumes

Direct access to the disk or to a volume is restricted. For more information, see "Changes to the file system and to the storage stack to restrict direct disk access and direct volume access in Windows Vista and in Windows Server 2008" in the Help and Support Knowledge Base athttp://support.microsoft.com/kb/942448.
Windows Server 2003 and Windows XP:  Direct access to the disk or to a volume is not restricted in this manner.
You can use the CreateFile function to open a physical disk drive or a volume, which returns a direct access storage device (DASD) handle that can be used with the DeviceIoControl function. This enables you to access the disk or volume directly, for example such disk metadata as the partition table. However, this type of access also exposes the disk drive or volume to potential data loss, because an incorrect write to a disk using this mechanism could make its contents inaccessible to the operating system. To ensure data integrity, be sure to become familiar withDeviceIoControl and how other APIs behave differently with a direct access handle as opposed to a file system handle.
The following requirements must be met for such a call to succeed:
  • The caller must have administrative privileges. For more information, see Running with Special Privileges.
  • The dwCreationDisposition parameter must have the OPEN_EXISTINGflag.
  • When opening a volume or floppy disk, the dwShareMode parameter must have the FILE_SHARE_WRITEflag.
Note  The dwDesiredAccess parameter can be zero, allowing the application to query device attributes without accessing a device. This is useful for an application to determine the size of a floppy disk drive and the formats it supports without requiring a floppy disk in a drive, for instance. It can also be used for reading statistics without requiring higher-level data read/write permission.
When opening a physical drive x:, the lpFileName string should be the following form: "\\.\PhysicalDriveX". Hard disk numbers start at zero. The following table shows some examples of physical drive strings.
StringMeaning
"\\.\PhysicalDrive0"Opens the first physical drive.
"\\.\PhysicalDrive2"Opens the third physical drive.

To obtain the physical drive identifier for a volume, open a handle to the volume and call the DeviceIoControl function withIOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS. This control code returns the disk number and offset for each of the volume's one or more extents; a volume can span multiple physical disks.
For an example of opening a physical drive, see Calling DeviceIoControl.
When opening a volume or removable media drive (for example, a floppy disk drive or flash memory thumb drive), the lpFileName string should be the following form: "\\.\X:". Do not use a trailing backslash (\), which indicates the root directory of a drive. The following table shows some examples of drive strings.
StringMeaning
"\\.\A:"Opens floppy disk drive A.
"\\.\C:"Opens the C: volume.
"\\.\C:\"Opens the file system of the C: volume.

You can also open a volume by referring to its volume name. For more information, see Naming a Volume.
A volume contains one or more mounted file systems. Volume handles can be opened as noncached at the discretion of the particular file system, even when the noncached option is not specified in CreateFile. You should assume that all Microsoft file systems open volume handles as noncached. The restrictions on noncached I/O for files also apply to volumes.
A file system may or may not require buffer alignment even though the data is noncached. However, if the noncached option is specified when opening a volume, buffer alignment is enforced regardless of the file system on the volume. It is recommended on all file systems that you open volume handles as noncached, and follow the noncached I/O restrictions.
Note  To read or write to the last few sectors of the volume, you must call DeviceIoControl and specifyFSCTL_ALLOW_EXTENDED_DASD_IO. This signals the file system driver not to perform any I/O boundary checks on partition read or write calls. Instead, boundary checks are performed by the device driver.

上面寫的是在Windows 環境
而在WINCE 中,要 Open 的 filename 應該是像 L"\\USB Disk\\Vol:" <-- 錯 要用 L"DSK1:" 注意有冒號
CreateFile 成功後,再用DeviceIoControl  IOCTL_DISK_READ
用 L"\\USB Disk\\Vol:" 會成功但是之後 DeviceIoControl  IOCTL_DISK_READ 會 hang

IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
http://a-jordan.blogspot.com/2008/05/usb-flash-physical-drive.html

2012年2月2日 星期四

USB flash problem, 沒有partition, superfloopy

最近有一個問題
就是用WINCE 5.0 format 的 USB Flash
沒辦法在USB200下讀取

仔細檢驗了一下不能讀取的USB Disk
發現竟然沒有partition table

查了一下 沒有 Partition 的 Disk
就叫 Super Floopy

http://msdn.microsoft.com/en-us/windows/hardware/gg463525


Q.What about removable media?
  
A.Removable media must be MBR or "superfloppy."
Q.What is a superfloppy?
  
A.
Removable media without either GPT or MBR formatting is considered a "superfloppy." The entire media is treated as a single partition.
The media manufacturer performs any MBR partitioning of removable media. If the media does have an MBR, only one partition is supported. There is little user-discernible difference between MBR-partitioned media and superfloppies.
Examples of removable media include floppy disk drives, JAZ disk cartridges, magneto-optical media, DVD-ROM, and CD-ROM. Hard disk drives on external buses such as SCSI or IEEE 1394 are not considered removable.