Physical hard disk drive for win32
Hi,
An attached patch fixes to use raw access to physical hard disk drive for win32.
It can be used as -hdb \\.\PhysicalDrive0 or -hdb //./PhysicalDrive0.
For example, if the guest OS is Linux, the second partition can be accessed by /dev/hdb2.
Regards,
Kazu
Index: block-raw.c
===================================================================
RCS file: /sources/qemu/qemu/block-raw.c,v
retrieving revision 1.10
block-raw.c +20 -0
@@ -833,6 +833,7 @@#define FTYPE_FILE 0#define FTYPE_CD 1 +#define FTYPE_HARDDISK 2 typedef struct BDRVRawState { HANDLE hfile;
@@ -1090,6 +1091,9 @@BDRVRawState *s = bs->opaque;LARGE_INTEGER l;ULARGE_INTEGER available, total, total_free; +DISK_GEOMETRY dg; +DWORD count; +BOOL status;switch (s->type) {case FTYPE_FILE :
@@ -1102,6 +1106,14 @@return -EIO ; l.QuadPart = total.QuadPart;break ; +case FTYPE_HARDDISK : + status =DeviceIoControl (s->hfile,IOCTL_DISK_GET_DRIVE_GEOMETRY , +NULL ,0 , &dg,sizeof (dg), &count,NULL ); +if (status != FALSE) { + l.QuadPart = dg.Cylinders.QuadPart * dg.TracksPerCylinder + * dg.SectorsPerTrack * dg.BytesPerSector; + } +break ;default :return -EIO ; }
@@ -1201,9 +1213,17 @@BDRVRawState *s = bs->opaque;UINT type;const char *p; +char *q; +int i; +char physical_drive[14 ];if (strstart (filename,"\\\\.\\" , &p) ||strstart (filename,"//./" , &p)) { +for (i =0 , q = p; i <13 ; i++, q++) { + physical_drive[i] =tolower (*q); + } +if (strstart (physical_drive,"physicaldrive" ,NULL )) +return FTYPE_HARDDISK ;snprintf (s->drive_path,sizeof (s->drive_path),"%c:\\" , p[0 ]); type =GetDriveType (s->drive_path);if (type ==DRIVE_CDROM )
Is it hard to access "raw" hardware under Windows ? like Hard Disk...?
Under Linux it is easy.
Quoted 3 lines · Alexey Eremenko
Is it hard to access "raw" hardware under Windows ? like Hard Disk...?
Under Linux it is easy.
CVS build cannot access host's physical hard disk drive by -hdb //./PhysicalDrive0 after AIO was introduced.
So I made a patch to fix it.
Regards,
Kazu
PATCH 1/1 Physical hard disk drive for win32
Hi,
raw harddisk access for Windows (//./PhysicalDrive0) gives wrong disk sizes. You can check this by booting a harddisk with the GRUB bootloader installed and using GRUB's geometry command.
The current QEMU code uses IOCTL_DISK_GET_DRIVE_GEOMETRY which is marked as obsolete by MS.
The appended patch gives correct results. Please apply it to CVS.
Stefan
Description of block-raw.patch:
* replace IOCTL_DISK_GET_DRIVE_GEOMETRY by IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
Index: block-raw.c
===================================================================
RCS file: /sources/qemu/qemu/block-raw.c,v
retrieving revision 1.24
block-raw.c +4 -5
@@ -1174,7 +1174,7 @@BDRVRawState *s = bs->opaque;LARGE_INTEGER l;ULARGE_INTEGER available, total, total_free; -DISK_GEOMETRY dg; +DISK_GEOMETRY_EX dg;DWORD count;BOOL status;
@@ -1190,11 +1190,10 @@ l.QuadPart = total.QuadPart;break ;case FTYPE_HARDDISK : - status =DeviceIoControl (s->hfile,IOCTL_DISK_GET_DRIVE_GEOMETRY , + status =DeviceIoControl (s->hfile,IOCTL_DISK_GET_DRIVE_GEOMETRY_EX ,NULL ,0 , &dg,sizeof (dg), &count,NULL ); -if (status != FALSE) { - l.QuadPart = dg.Cylinders.QuadPart * dg.TracksPerCylinder - * dg.SectorsPerTrack * dg.BytesPerSector; +if (status !=0 ) { + l = dg.DiskSize; }break ;default :
2007/10/19, Stefan Weil <[email protected]>:
Quoted 9 lines
Hi,
raw harddisk access for Windows (//./PhysicalDrive0) gives wrong disk sizes. You can check this by booting a harddisk with the GRUB bootloader installed and using GRUB's geometry command.
The current QEMU code uses
IOCTL_DISK_GET_DRIVE_GEOMETRYwhich is marked as obsolete by MS.
Quoted 7 lines
The appended patch gives correct results. Please apply it to CVS.
Stefan
Description of block-raw.patch:
* replace
IOCTL_DISK_GET_DRIVE_GEOMETRYbyIOCTL_DISK_GET_DRIVE_GEOMETRY_EX
According to http://msdn2.microsoft.com/en-us/library/aa365171.aspx
this function requires Win XP or Vista.
Is qemu supported only on these?
Quoted 4 lines · Ivan Kalvachev
According to http://msdn2.microsoft.com/en-us/library/aa365171.aspx
this function requires Win XP or Vista.
Is qemu supported only on these?
Good question. The old IOCTL_DISK_GET_DRIVE_GEOMETRY was also supported for W2K. Will QEMU support W2K as a host longer than MS does? It would make things easier if we dropped support for W2K hosts.
If we need host support for W2K, I'll check another method to get the disk size (reading size of partition 0) and send a new patch. Please send your feedback in this case.
I hope we don't need W95 or W98 host support!?
Stefan
Hi,
Quoted 9 lines · Stefan Weil
Quoted 4 lines · Ivan Kalvachev
According to http://msdn2.microsoft.com/en-us/library/aa365171.aspx
this function requires Win XP or Vista.
Is qemu supported only on these?Good question. The old
IOCTL_DISK_GET_DRIVE_GEOMETRYwas also supported for W2K. Will QEMU support W2K as a host longer than MS does? It would make things easier if we dropped support for W2K hosts.
Do we really want to be as evil as Microsoft and force all users to upgrade?
It should not be _that_ hard to stay at least a _little_ backwards compatible.
Ciao,
Dscho
Hi,
Quoted 3 lines · Stefan Weil
longer than MS does? It would make things easier if we dropped support for W2K hosts.
Surely not?!? I've heard of people being off hand about supporting Win95/98 (and I would agree with that) et al..., but surely W2K support should be sacrosanct ?!?
Regards,
Shaddy
Quoted 6 lines · Shaddy Baddah
Quoted 3 lines
longer than MS does? It would make things easier if we dropped support for W2K hosts.
Surely not?!? I've heard of people being off hand about supporting Win95/98 (and I would agree with that) et al..., but surely W2K support should be sacrosanct ?!?
I would hope so, as I get the impression W2K is the last Windows version which can be run reliably in a VM, due to the "legitimacy" checking done by XP and later. :-)
-- Jamie
2007/10/22, Stefan Weil <[email protected]>:
Quoted 10 lines
Quoted 4 lines · Ivan Kalvachev
According to http://msdn2.microsoft.com/en-us/library/aa365171.aspx
this function requires Win XP or Vista.
Is qemu supported only on these?Good question. The old
IOCTL_DISK_GET_DRIVE_GEOMETRYwas also supported for W2K. Will QEMU support W2K as a host longer than MS does? It would make things easier if we dropped support for W2K hosts.
Quoted 5 lines
If we need host support for W2K, I'll check another method to get the disk size (reading size of partition 0) and send a new patch. Please send your feedback in this case.
I hope we don't need W95 or W98 host support!?
Why not. :P
Anyway, calling the old ioctl code if the new one fails, should be compatible enough. (and the new structure contains the old one, so no need of having 2 variables).
It would be even better if somebody finds out what the error for not supported function is (I don't feel like checking 16000 error codes), but it is not critical.
Hi,
Quoted 15 lines · Ivan Kalvachev
2007/10/22, Stefan Weil <[email protected]>:
Quoted 14 lines
Quoted 4 lines · Ivan Kalvachev
According to http://msdn2.microsoft.com/en-us/library/aa365171.aspx
this function requires Win XP or Vista.
Is qemu supported only on these?Good question. The old
IOCTL_DISK_GET_DRIVE_GEOMETRYwas also supported for W2K. Will QEMU support W2K as a host longer than MS does? It would make things easier if we dropped support for W2K hosts.If we need host support for W2K, I'll check another method to get the disk size (reading size of partition 0) and send a new patch. Please send your feedback in this case.
Quoted 7 lines
Quoted 1 line
I hope we don't need W95 or W98 host support!?
Why not. :P
Anyway, calling the old ioctl code if the new one fails, should be compatible enough. (and the new structure contains the old one, so no need of having 2 variables).
It is not just that. You'll have to guard the new ioctl within #ifdef's so that it still compiles on MinGW, even if that version does not define the constant.
Staying backwards compatible in this case is so easy, it is not even funny. (And certainly no reason to groan over having to support it.)
Ciao,
Dscho
Quoted 27 lines · Johannes Schindelin
Hi,
Quoted 15 lines · Ivan Kalvachev
2007/10/22, Stefan Weil <[email protected]>:
Quoted 14 lines
Quoted 4 lines · Ivan Kalvachev
According to http://msdn2.microsoft.com/en-us/library/aa365171.aspx
this function requires Win XP or Vista.
Is qemu supported only on these?Good question. The old
IOCTL_DISK_GET_DRIVE_GEOMETRYwas also supported for W2K. Will QEMU support W2K as a host longer than MS does? It would make things easier if we dropped support for W2K hosts.If we need host support for W2K, I'll check another method to get the disk size (reading size of partition 0) and send a new patch. Please send your feedback in this case.
Quoted 7 lines
Quoted 1 line
I hope we don't need W95 or W98 host support!?
Why not. :P
Anyway, calling the old ioctl code if the new one fails, should be compatible enough. (and the new structure contains the old one, so no need of having 2 variables).
Quoted 6 lines
It is not just that. You'll have to guard the new ioctl within #ifdef's so that it still compiles on MinGW, even if that version does not define the constant.
Staying backwards compatible in this case is so easy, it is not even funny. (And certainly no reason to groan over having to support it.)
A undirected remark to the general public: If somebody comes up with a patch for better backward compatibility I'll certainly consider it.
Thiemo
Quoted 1 line
Hi,
Quoted 7 lines · Thiemo Seufer
Quoted 2 lines · Johannes Schindelin
Staying backwards compatible in this case is so easy, it is not even funny. (And certainly no reason to groan over having to support it.)
A undirected remark to the general public: If somebody comes up with a patch for better backward compatibility I'll certainly consider it.
I think I fixed it. Since I am not your regular Windows user (I only use Windows when I have to), I cannot tell if it works, though.
It is here: http://repo.or.cz/w/qemu/dscho.git?a=shortlog;h=diskgeometry
Any testing (especially the physical drive on Windows < XP) would be very much appreciated.
There is also a compilation bugfix with the current CVS on Windows: the tap down script patch left a for() loop without a body.
A question: on some other lists, the preferred way to submit patches is to inline them into the mail, but here I saw many attachments and/or weblinks to patches. The easiest way for me would be like I just did: provide a link to repo.or.cz.
What is the preferred way on this list?
Ciao,
Dscho
Quoted 11 lines · Johannes Schindelin
I think I fixed it. Since I am not your regular Windows user (I only use
Windows when I have to), I cannot tell if it works, though.
It is here: http://repo.or.cz/w/qemu/dscho.git?a=shortlog;h=diskgeometry
Any testing (especially the physical drive on Windows < XP) would be very much appreciated.
...
Ciao,
Dscho
With your patch, W2K will still see a wrong disk size. This is caused by the fact that the size of a modern harddisk has nothing to do with tracks per cylinder or sectors per track
- it is not simply the product of four values.
You can use a disk to some degree even when some part at the end is inaccessible, but it is also possible to destroy it (for example if a partition tool "corrects" the disk size in the boot sector) or get other kinds of errors.
I prefer a patch which gives correct values for all Windows versions and will send one as soon as possible.
Even without a patch, the current code in CVS HEAD still allows compilation using MinGW on any Windows. QEMU will also run on the same Windows variants as before. The only thing no longer working on W2K and older variants is physical hard disk access. I think this is better than working with a wrong disk size!
Regards,
Stefan
Hi,
Quoted 11 lines · Stefan Weil
Quoted 8 lines · Johannes Schindelin
I think I fixed it. Since I am not your regular Windows user (I only use Windows when I have to), I cannot tell if it works, though.
It is here:
http://repo.or.cz/w/qemu/dscho.git?a=shortlog;h=diskgeometryAny testing (especially the physical drive on Windows < XP) would be very much appreciated. ... Ciao, Dscho
With your patch, W2K will still see a wrong disk size.
Funny. I thought that since kazu did the original patch, and kazu is a Windows user, that it worked at _some_ stage.
Also I have to note that IOCTL_DISK_GET_DRIVE_GEOMETRY is a Windows 2000 feature, so unfortunately there is really no backwards compatibility to Windows 95/98/ME.
Quoted 3 lines
This is caused by the fact that the size of a modern harddisk has nothing to do with tracks per cylinder or sectors per track - it is not simply the product of four values.
Since it obviously worked in one setting, there might be a chance that it is fixable. Could you be a little bit more specific in what is going wrong? I.e. what is the real size, and what are the four values?
Quoted 2 lines
Even without a patch, the current code in CVS HEAD still allows compilation using MinGW on any Windows.
Yes, but a setup that was evidently working before was broken.
Ciao,
Dscho
Quoted 23 lines · Johannes Schindelin
Hi,
Quoted 7 lines · Thiemo Seufer
Quoted 2 lines · Johannes Schindelin
Staying backwards compatible in this case is so easy, it is not even funny. (And certainly no reason to groan over having to support it.)
A undirected remark to the general public: If somebody comes up with a patch for better backward compatibility I'll certainly consider it.
I think I fixed it. Since I am not your regular Windows user (I only use Windows when I have to), I cannot tell if it works, though.
It is here: http://repo.or.cz/w/qemu/dscho.git?a=shortlog;h=diskgeometry
Any testing (especially the physical drive on Windows < XP) would be very much appreciated.
There is also a compilation bugfix with the current CVS on Windows: the tap down script patch left a
for()loop without a body.
Quoted 6 lines
A question: on some other lists, the preferred way to submit patches is to inline them into the mail, but here I saw many attachments and/or weblinks to patches. The easiest way for me would be like I just did: provide a link to repo.or.cz.
What is the preferred way on this list?
I prefer inlined or attached patches, so they get archived and don't develop a 404 problem over time.
Thiemo