qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] DOSEMU image file support
@ 2004-09-02 15:15 Hampa Hug
  2004-09-02 16:07 ` Fabrice Bellard
  0 siblings, 1 reply; 10+ messages in thread
From: Hampa Hug @ 2004-09-02 15:15 UTC (permalink / raw)
  To: qemu-devel

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

Hi all

The attached patch adds support for DOSEMU image files to
qemu. This is useful if you want to share image files
between qemu and dosemu.

cheers
Hampa

[-- Attachment #2: qemu-dosemu.diff --]
[-- Type: text/plain, Size: 6686 bytes --]

diff -Nur qemu-cvs/Makefile qemu-dosemu/Makefile
--- qemu-cvs/Makefile	2004-08-03 23:13:35.000000000 +0200
+++ qemu-dosemu/Makefile	2004-09-02 16:10:17.000000000 +0200
@@ -20,7 +20,7 @@
 	$(MAKE) -C $$d $@ || exit 1 ; \
         done
 
-qemu-img: qemu-img.c block.c block-cow.c block-qcow.c aes.c block-vmdk.c
+qemu-img: qemu-img.c block.c block-cow.c block-dosemu.c block-qcow.c aes.c block-vmdk.c
 	$(CC) -DQEMU_TOOL $(CFLAGS) $(LDFLAGS) $(DEFINES) -o $@ $^ -lz $(LIBS)
 
 dyngen$(EXESUF): dyngen.c
diff -Nur qemu-cvs/Makefile.target qemu-dosemu/Makefile.target
--- qemu-cvs/Makefile.target	2004-08-25 19:34:05.000000000 +0200
+++ qemu-dosemu/Makefile.target	2004-09-02 16:10:17.000000000 +0200
@@ -241,7 +241,7 @@
 
 # must use static linking to avoid leaving stuff in virtual address space
 VL_OBJS=vl.o osdep.o block.o readline.o monitor.o pci.o console.o 
-VL_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o
+VL_OBJS+=block-dosemu.o block-cow.o block-qcow.o aes.o block-vmdk.o
 
 ifeq ($(TARGET_ARCH), i386)
 # Hardware support
diff -Nur qemu-cvs/block-dosemu.c qemu-dosemu/block-dosemu.c
--- qemu-cvs/block-dosemu.c	1970-01-01 01:00:00.000000000 +0100
+++ qemu-dosemu/block-dosemu.c	2004-09-02 16:10:17.000000000 +0200
@@ -0,0 +1,197 @@
+/*
+ * Block driver for the DOSEMU format
+ *
+ * Copyright (c) 2004 Hampa Hug <hampa@hampa.ch>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include "vl.h"
+#include "block_int.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**************************************************************/
+/* DOSEMU block driver */
+
+typedef struct BDRVDosemuState {
+    FILE     *fp;
+    uint32_t tracks;
+    uint32_t heads;
+    uint32_t sectors;
+    uint32_t offset;
+    uint64_t blocks;
+} BDRVDosemuState;
+
+
+static int dosemu_probe(const uint8_t *buf, int buf_size, const char *filename)
+{
+    if ((buf_size >= 7) && (memcmp(buf, "DOSEMU\x00", 7) == 0)) {
+        return 100;
+    }
+
+    return 0;
+}
+
+static int dosemu_open_fp(BlockDriverState *bs, FILE *fp)
+{
+    BDRVDosemuState *s = bs->opaque;
+    uint8_t         buf[128];
+
+    s->fp = fp;
+
+    if (fread(buf, 1, 128, fp) != 128) {
+        return 1;
+    }
+
+    if (memcmp(buf, "DOSEMU\x00", 7) != 0) {
+        return 1;
+    }
+
+    s->tracks = le32_to_cpupu ((void *) (buf + 15));
+    s->heads = le32_to_cpupu ((void *) (buf + 7));
+    s->sectors = le32_to_cpupu ((void *) (buf + 11));
+    s->offset = le32_to_cpupu ((void *) (buf + 19));
+    s->blocks = (uint64_t) s->tracks * s->heads * s->sectors;
+
+    bs->total_sectors = s->blocks;
+
+    bdrv_set_geometry_hint (bs, s->tracks, s->heads, s->sectors);
+
+    return 0;
+}
+
+static int dosemu_open(BlockDriverState *bs, const char *filename)
+{
+    FILE *fp;
+
+    fp = fopen(filename, "r+b");
+
+    if (fp == NULL) {
+        fp = fopen(filename, "rb");
+    }
+
+    if (fp == NULL) {
+        return -1;
+    }
+
+    if (dosemu_open_fp(bs, fp)) {
+        fclose (fp);
+        return -1;
+    }
+
+    return 0;
+}
+
+static int dosemu_read(BlockDriverState *bs, int64_t sector_num,
+                    uint8_t *buf, int nb_sectors)
+{
+    BDRVDosemuState *s = bs->opaque;
+
+    if (fseeko(s->fp, s->offset + 512 * sector_num, SEEK_SET)) {
+      return -1;
+    }
+
+    if (fread(buf, 512, nb_sectors, s->fp) != nb_sectors) {
+        return -1;
+    }
+
+    return 0;
+}
+
+static int dosemu_write(BlockDriverState *bs, int64_t sector_num,
+                     const uint8_t *buf, int nb_sectors)
+{
+    BDRVDosemuState *s = bs->opaque;
+    long            n;
+
+    n = 512L * nb_sectors;
+
+    if (fseeko(s->fp, s->offset + 512 * sector_num, SEEK_SET)) {
+      return -1;
+    }
+
+    if (fwrite(buf, 512, nb_sectors, s->fp) != nb_sectors) {
+        return -1;
+    }
+
+    fflush (s->fp);
+
+    return 0;
+}
+
+static int dosemu_close(BlockDriverState *bs)
+{
+    BDRVDosemuState *s = bs->opaque;
+
+    fclose(s->fp);
+
+    return 0;
+}
+
+static int dosemu_create(const char *filename, int64_t total_size,
+                         const char *backing_file, int flags)
+{
+    int           ret;
+    uint32_t      tracks, heads, sectors;
+    unsigned char buf[12];
+    FILE          *fp;
+
+    if (flags || backing_file) {
+        return -ENOTSUP;
+    }
+
+    /* should choose sectors and tracks based on total_size */
+    sectors = 63;
+    heads = 128;
+    tracks = (total_size + (sectors * heads) / 2) / (sectors * heads);
+
+    fp = fopen(filename, "wb");
+    if (fp == NULL) {
+        return -EIO;
+    }
+
+    memset (buf, 0, 128);
+    memcpy (buf, "DOSEMU\x00", 7);
+
+    cpu_to_le32wu ((void *) (buf + 15), tracks);
+    cpu_to_le32wu ((void *) (buf + 7), heads);
+    cpu_to_le32wu ((void *) (buf + 11), sectors);
+    cpu_to_le32wu ((void *) (buf + 19), 128);
+
+    ret = fwrite(buf, 1, 128, fp);
+
+    fclose(fp);
+
+    if (ret != 128) {
+        return -EIO;
+    }
+
+    return 0;
+}
+
+
+BlockDriver bdrv_dosemu = {
+    "dosemu",
+    sizeof(BDRVDosemuState),
+    dosemu_probe,
+    dosemu_open,
+    dosemu_read,
+    dosemu_write,
+    dosemu_close,
+    dosemu_create,
+};
diff -Nur qemu-cvs/block.c qemu-dosemu/block.c
--- qemu-cvs/block.c	2004-08-03 23:14:09.000000000 +0200
+++ qemu-dosemu/block.c	2004-09-02 16:10:17.000000000 +0200
@@ -558,6 +558,7 @@
 {
     BDRVRawState *s = bs->opaque;
     close(s->fd);
+    return 0;
 }
 
 static int raw_create(const char *filename, int64_t total_size,
@@ -591,6 +592,7 @@
 void bdrv_init(void)
 {
     bdrv_register(&bdrv_raw);
+    bdrv_register(&bdrv_dosemu);
 #ifndef _WIN32
     bdrv_register(&bdrv_cow);
 #endif
diff -Nur qemu-cvs/vl.h qemu-dosemu/vl.h
--- qemu-cvs/vl.h	2004-08-24 23:24:45.000000000 +0200
+++ qemu-dosemu/vl.h	2004-09-02 16:10:17.000000000 +0200
@@ -351,6 +351,7 @@
 typedef struct BlockDriver BlockDriver;
 
 extern BlockDriver bdrv_raw;
+extern BlockDriver bdrv_dosemu;
 extern BlockDriver bdrv_cow;
 extern BlockDriver bdrv_qcow;
 extern BlockDriver bdrv_vmdk;

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [PATCH] DOSEMU image file support
  2004-09-02 15:15 [Qemu-devel] [PATCH] DOSEMU image file support Hampa Hug
@ 2004-09-02 16:07 ` Fabrice Bellard
  2004-09-02 16:21   ` Johannes Schindelin
  2004-09-02 17:00   ` Hampa Hug
  0 siblings, 2 replies; 10+ messages in thread
From: Fabrice Bellard @ 2004-09-02 16:07 UTC (permalink / raw)
  To: qemu-devel

Hi,

Thank you for the patch. Two remarks if you want that I merge it:

1) use open/read/write/close instead of fopen/fread/fwrite/fclose for 
consistency with other block drivers.

2) change the license to BSD

Fabrice.

Hampa Hug wrote:
> Hi all
> 
> The attached patch adds support for DOSEMU image files to
> qemu. This is useful if you want to share image files
> between qemu and dosemu.
> 
> cheers
> Hampa
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Qemu-devel mailing list
> Qemu-devel@nongnu.org
> http://lists.nongnu.org/mailman/listinfo/qemu-devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [PATCH] DOSEMU image file support
  2004-09-02 16:07 ` Fabrice Bellard
@ 2004-09-02 16:21   ` Johannes Schindelin
  2004-09-02 17:00   ` Hampa Hug
  1 sibling, 0 replies; 10+ messages in thread
From: Johannes Schindelin @ 2004-09-02 16:21 UTC (permalink / raw)
  To: qemu-devel

Hi,

On Thu, 2 Sep 2004, Fabrice Bellard wrote:
> 2) change the license to BSD

Not GPL?

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [PATCH] DOSEMU image file support
  2004-09-02 16:07 ` Fabrice Bellard
  2004-09-02 16:21   ` Johannes Schindelin
@ 2004-09-02 17:00   ` Hampa Hug
  2004-09-02 17:12     ` Fabrice Bellard
  1 sibling, 1 reply; 10+ messages in thread
From: Hampa Hug @ 2004-09-02 17:00 UTC (permalink / raw)
  To: qemu-devel

Fabrice Bellard wrote:

> Thank you for the patch. Two remarks if you want that I merge it:
> 
> 1) use open/read/write/close instead of fopen/fread/fwrite/fclose for 
> consistency with other block drivers.

The reason I used stdio is that read/write are non-blocking. It is
not an error for read to return fewer bytes than requested. If that
happens, the other block drivers fail.

> 2) change the license to BSD

I was under the impression that we used GPL. At least that's what
COPYING says.

cheers
Hampa

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [PATCH] DOSEMU image file support
  2004-09-02 17:00   ` Hampa Hug
@ 2004-09-02 17:12     ` Fabrice Bellard
  2004-09-03 18:04       ` [Qemu-devel] Solved: QEMU messed up in Windows Harry Sufehmi
                         ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Fabrice Bellard @ 2004-09-02 17:12 UTC (permalink / raw)
  To: qemu-devel

Hampa Hug wrote:
> Fabrice Bellard wrote:
> 
> 
>>Thank you for the patch. Two remarks if you want that I merge it:
>>
>>1) use open/read/write/close instead of fopen/fread/fwrite/fclose for 
>>consistency with other block drivers.
> 
> 
> The reason I used stdio is that read/write are non-blocking. It is
> not an error for read to return fewer bytes than requested. If that
> happens, the other block drivers fail.

I agree, but for disk accesses it is usual to assume they return the 
requested amount of bytes. fread/fwrite are buffered I/Os which is a bit 
overkill for block drivers.

>>2) change the license to BSD
> 
> 
> I was under the impression that we used GPL. At least that's what
> COPYING says.

The different parts of QEMU have different licenses (and of course it 
affects the license of the resulting program). As mentionned on the web 
page:

- The QEMU virtual CPU core library is released under the GNU Lesser 
General Public License.
- The Linux QEMU emulator is released under the GNU General Public License.
- The QEMU PC system emulator is released under the MIT/BSD License.

The block layer belongs to the QEMU PC system emulator, so its license 
is BSD.

Fabrice.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Qemu-devel] Solved: QEMU messed up in Windows
  2004-09-02 17:12     ` Fabrice Bellard
@ 2004-09-03 18:04       ` Harry Sufehmi
  2004-09-14  0:19       ` [Qemu-devel] [PATCH] DOSEMU image file support Hampa Hug
  2004-09-25 12:04       ` Gianni Tedesco
  2 siblings, 0 replies; 10+ messages in thread
From: Harry Sufehmi @ 2004-09-03 18:04 UTC (permalink / raw)
  To: qemu-devel

I ran QEMU on Windows, running linux.img
However, everything was garbled.

After changing the color depth from 24-bit to 16-bit, now it works fine.

Hopefully that'll save some other people's time.


Thanks,
Harry

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [PATCH] DOSEMU image file support
  2004-09-02 17:12     ` Fabrice Bellard
  2004-09-03 18:04       ` [Qemu-devel] Solved: QEMU messed up in Windows Harry Sufehmi
@ 2004-09-14  0:19       ` Hampa Hug
  2004-09-14  9:04         ` Johannes Schindelin
  2004-09-25 12:04       ` Gianni Tedesco
  2 siblings, 1 reply; 10+ messages in thread
From: Hampa Hug @ 2004-09-14  0:19 UTC (permalink / raw)
  To: qemu-devel

Fabrice Bellard wrote:
> Hampa Hug wrote:
> > Fabrice Bellard wrote:
> >>Thank you for the patch. Two remarks if you want that I merge it:
> >>
> >>1) use open/read/write/close instead of fopen/fread/fwrite/fclose for 
> >>consistency with other block drivers.
> > 
> > 
> > The reason I used stdio is that read/write are non-blocking. It is
> > not an error for read to return fewer bytes than requested. If that
> > happens, the other block drivers fail.
> 
> I agree, but for disk accesses it is usual to assume they return the 
> requested amount of bytes. fread/fwrite are buffered I/Os which is a bit 
> overkill for block drivers.

I don't think that buffered I/O is an issue here. We are doing
fread/fwrite of multiples of 512 byte blocks and most implementations
don't actually buffer these.

I am reluctant to just use read/write (as the other block drivers
do) because it is simply wrong. It may work for local files under
Linux, but what about remote files (such as files on NFS mounts)
and other host operating systems?

At the very least we should write small wrapper functions for
read and write that fail only if there was an error.


> >>2) change the license to BSD
> > 
> > I was under the impression that we used GPL. At least that's what
> > COPYING says.
> 
> The different parts of QEMU have different licenses (and of course it 
> affects the license of the resulting program). As mentionned on the web 
> page:
> 
> - The QEMU virtual CPU core library is released under the GNU Lesser 
> General Public License.
> - The Linux QEMU emulator is released under the GNU General Public License.
> - The QEMU PC system emulator is released under the MIT/BSD License.
> 
> The block layer belongs to the QEMU PC system emulator, so its license 
> is BSD.

Alright, I'll change that.

cheers
Hampa

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [PATCH] DOSEMU image file support
  2004-09-14  0:19       ` [Qemu-devel] [PATCH] DOSEMU image file support Hampa Hug
@ 2004-09-14  9:04         ` Johannes Schindelin
  0 siblings, 0 replies; 10+ messages in thread
From: Johannes Schindelin @ 2004-09-14  9:04 UTC (permalink / raw)
  To: qemu-devel

Hi,

On Tue, 14 Sep 2004, Hampa Hug wrote:

> I am reluctant to just use read/write (as the other block drivers
> do) because it is simply wrong. It may work for local files under
> Linux, but what about remote files (such as files on NFS mounts)
> and other host operating systems?

Well, it doesn't. I tried several times to use a CD image which was
mounted via samba, and it always failed miserably (took me almost half an
hour to realize what the problem was, too).

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [PATCH] DOSEMU image file support
  2004-09-02 17:12     ` Fabrice Bellard
  2004-09-03 18:04       ` [Qemu-devel] Solved: QEMU messed up in Windows Harry Sufehmi
  2004-09-14  0:19       ` [Qemu-devel] [PATCH] DOSEMU image file support Hampa Hug
@ 2004-09-25 12:04       ` Gianni Tedesco
  2004-09-25 12:08         ` Gianni Tedesco
  2 siblings, 1 reply; 10+ messages in thread
From: Gianni Tedesco @ 2004-09-25 12:04 UTC (permalink / raw)
  To: qemu-devel

On Thu, 2004-09-02 at 19:12 +0200, Fabrice Bellard wrote:
> > The reason I used stdio is that read/write are non-blocking. It is
> > not an error for read to return fewer bytes than requested. If that
> > happens, the other block drivers fail.
> 
> I agree, but for disk accesses it is usual to assume they return the 
> requested amount of bytes. fread/fwrite are buffered I/Os which is a bit 
> overkill for block drivers.

Heh,

Not if an unmasked signal is received on NFS mounted soft/intr. That is
because POSIX "allows a read that is interrupted after reading some data
to return -1 (with errno set to EINTR) or to return the number of bytes
already read."

-- 
// Gianni Tedesco (gianni at scaramanga dot co dot uk)
lynx --source www.scaramanga.co.uk/scaramanga.asc | gpg --import
8646BE7D: 6D9F 2287 870E A2C9 8F60 3A3C 91B5 7669 8646 BE7D

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [PATCH] DOSEMU image file support
  2004-09-25 12:04       ` Gianni Tedesco
@ 2004-09-25 12:08         ` Gianni Tedesco
  0 siblings, 0 replies; 10+ messages in thread
From: Gianni Tedesco @ 2004-09-25 12:08 UTC (permalink / raw)
  To: qemu-devel

On Sat, 2004-09-25 at 13:04 +0100, Gianni Tedesco wrote:
> On Thu, 2004-09-02 at 19:12 +0200, Fabrice Bellard wrote:
> > > The reason I used stdio is that read/write are non-blocking. It is
> > > not an error for read to return fewer bytes than requested. If that
> > > happens, the other block drivers fail.
> > 
> > I agree, but for disk accesses it is usual to assume they return the 
> > requested amount of bytes. fread/fwrite are buffered I/Os which is a bit 
> > overkill for block drivers.
> 
> Heh,
> 
> Not if an unmasked signal is received on NFS mounted soft/intr. That is
> because POSIX "allows a read that is interrupted after reading some data
> to return -1 (with errno set to EINTR) or to return the number of bytes
> already read."

Ignore me. I read "disk" as "filesystem".

Either way, POSIX says things are allowed to change - and we should
probably be allowing for images on network filesystems anyway...

-- 
// Gianni Tedesco (gianni at scaramanga dot co dot uk)
lynx --source www.scaramanga.co.uk/scaramanga.asc | gpg --import
8646BE7D: 6D9F 2287 870E A2C9 8F60 3A3C 91B5 7669 8646 BE7D

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2004-09-25 12:15 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-09-02 15:15 [Qemu-devel] [PATCH] DOSEMU image file support Hampa Hug
2004-09-02 16:07 ` Fabrice Bellard
2004-09-02 16:21   ` Johannes Schindelin
2004-09-02 17:00   ` Hampa Hug
2004-09-02 17:12     ` Fabrice Bellard
2004-09-03 18:04       ` [Qemu-devel] Solved: QEMU messed up in Windows Harry Sufehmi
2004-09-14  0:19       ` [Qemu-devel] [PATCH] DOSEMU image file support Hampa Hug
2004-09-14  9:04         ` Johannes Schindelin
2004-09-25 12:04       ` Gianni Tedesco
2004-09-25 12:08         ` Gianni Tedesco

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).