qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Kazu" <kazoo@r3.dion.ne.jp>
To: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] Raw CD-ROM and hard disk for win32
Date: Wed, 9 Aug 2006 15:36:05 +0900	[thread overview]
Message-ID: <000a01c6bb7e$172d0e60$0464a8c0@athlon> (raw)
In-Reply-To: 44D7871D.2000202@bellard.org

[-- Attachment #1: Type: text/plain, Size: 633 bytes --]

Sent: Tuesday, August 08, 2006 3:31 AM Fabrice Bellard wrote:

> Hi,
>
> I find it strange that there is no win32 API to know the size of a
> CD-ROM. Maybe a CD-ROM specific IOCTL exists ?
>

Yes, it does. An attached patch gets a size of raw CD-ROM and raw hard disk.

> About the windows device patch detection, I would like a more precise
> test than "len == 5". Testing if the filename begins with "\\.\" would
> be better. Supporting also the syntax "/dev/cdrom" would be good too.
>

It is necessary to set drive letter to use CD-ROM. When -cdrom /dev/cdrom is
set, the first CD-ROM drive is used in the patch.


Regards,
Kazu

[-- Attachment #2: qemu-20060809-cdrom.patch --]
[-- Type: application/octet-stream, Size: 5218 bytes --]

Index: block-raw.c
===================================================================
RCS file: /sources/qemu/qemu/block-raw.c,v
retrieving revision 1.6
diff -u -r1.6 block-raw.c
--- block-raw.c	7 Aug 2006 02:38:06 -0000	1.6
+++ block-raw.c	9 Aug 2006 04:50:55 -0000
@@ -761,14 +761,53 @@
     return 0;
 }
 
+static int64_t get_size(BlockDriverState *bs, HANDLE hfile, const char *filename)
+{
+    int64_t size;
+    BOOL status;
+    ULARGE_INTEGER available, total, total_free; 
+    char *drive_letter;
+    DISK_GEOMETRY dg;
+    DWORD count;
+
+    if (bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM &&
+        (strstart(filename, "//", NULL) || strstart(filename, "\\\\", NULL))) {
+        drive_letter = (char *)filename + 4;
+        status = GetDiskFreeSpaceEx(drive_letter, &available, &total, &total_free);
+        if (status != FALSE){
+            return total.QuadPart;
+        } else {
+            return -1;
+        }
+    } else if (bdrv_get_type_hint(bs) == BDRV_TYPE_HD &&
+               (strstart(filename, "//", NULL) || strstart(filename, "\\\\", NULL))) {
+        status = DeviceIoControl(hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY,
+                                 NULL, 0, &dg, sizeof(dg), &count, NULL);
+        if (status != FALSE) {
+            size = dg.Cylinders.QuadPart * dg.TracksPerCylinder
+                * dg.SectorsPerTrack * dg.BytesPerSector;
+            return size;
+        } else {
+            return -1;
+        }
+    }
+    return -1;
+}
+
 static int64_t  raw_getlength(BlockDriverState *bs)
 {
     BDRVRawState *s = bs->opaque;
     LARGE_INTEGER l;
+    int64_t size;
 
     l.LowPart = GetFileSize(s->hfile, &l.HighPart);
-    if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
-	return -EIO;
+    if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) {
+        size = get_size(bs, s->hfile, bs->filename);
+        if (size == -1)
+            return -EIO;
+        else
+            return size;
+    }
     return l.QuadPart;
 }
 
Index: block.c
===================================================================
RCS file: /sources/qemu/qemu/block.c,v
retrieving revision 1.33
diff -u -r1.33 block.c
--- block.c	7 Aug 2006 19:10:16 -0000	1.33
+++ block.c	9 Aug 2006 04:50:57 -0000
@@ -198,6 +198,11 @@
         /* specific win32 case for driver letters */
         return &bdrv_raw;
     }
+    if (len == 5 &&
+        (strstart(filename, "//./", NULL) || strstart(filename, "\\\\.\\", NULL))) {
+        /* win32 drive. CD-ROM etc. */
+        return &bdrv_raw;
+    }
 #endif   
     memcpy(protocol, filename, len);
     protocol[len] = '\0';
@@ -225,7 +230,11 @@
         return drv;
     if (strstart(filename, "/dev/", NULL))
         return &bdrv_raw;
-    
+#ifdef _WIN32
+    if (strstart(filename, "//./", NULL) || strstart(filename, "\\\\.\\", NULL))
+        return &bdrv_raw;
+#endif
+
     ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
     if (ret < 0)
         return NULL;
@@ -265,6 +274,28 @@
     return 0;
 }
 
+#ifdef _WIN32
+static int find_cdrom(BlockDriverState *bs, char *cdrom_name)
+{
+    char drives[256], *pdrv = drives;
+    UINT type;
+
+    ZeroMemory(drives, 256);
+    GetLogicalDriveStrings(sizeof(drives), drives);
+    while(pdrv[0] != '\0') {
+        type = GetDriveType(pdrv);
+        switch(type) {
+        case DRIVE_CDROM:
+            sprintf(cdrom_name, "\\\\.\\%c:", pdrv[0]);
+            return 0;
+            break;
+        }
+        pdrv += lstrlen(pdrv) + 1;
+    }
+    return -1;
+}
+#endif
+            
 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
 {
     return bdrv_open2(bs, filename, flags, NULL);
@@ -309,14 +340,28 @@
         bs->is_temporary = 1;
     }
 
+#ifdef _WIN32
+    /* convert /dev/cdrom to raw CD-ROM name */
+    if (strstart(filename, "/dev/cdrom", NULL)) {
+        int ret;
+        char cdrom_name[256];
+        ret = find_cdrom(bs, cdrom_name);
+        if (ret == -1)
+            return -1;
+        pstrcpy(bs->filename, sizeof(bs->filename), cdrom_name);
+    } else {
+        pstrcpy(bs->filename, sizeof(bs->filename), filename);
+    }
+#else
     pstrcpy(bs->filename, sizeof(bs->filename), filename);
+#endif
     if (flags & BDRV_O_FILE) {
-        drv = find_protocol(filename);
+        drv = find_protocol(bs->filename);
         if (!drv)
             return -ENOENT;
     } else {
         if (!drv) {
-            drv = find_image_format(filename);
+            drv = find_image_format(bs->filename);
             if (!drv)
                 return -1;
         }
@@ -331,9 +376,9 @@
         open_flags = BDRV_O_RDWR;
     else
         open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
-    ret = drv->bdrv_open(bs, filename, open_flags);
+    ret = drv->bdrv_open(bs, bs->filename, open_flags);
     if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
-        ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
+        ret = drv->bdrv_open(bs, bs->filename, BDRV_O_RDONLY);
         bs->read_only = 1;
     }
     if (ret < 0) {

      reply	other threads:[~2006-08-09  6:36 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-07  5:06 [Qemu-devel] Raw CD-ROM and hard disk for win32 Kazu
2006-08-07 18:31 ` Fabrice Bellard
2006-08-09  6:36   ` Kazu [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='000a01c6bb7e$172d0e60$0464a8c0@athlon' \
    --to=kazoo@r3.dion.ne.jp \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).