qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 1/2] Add an in-memory block device
@ 2007-12-05 22:05 Anthony Liguori
  2007-12-05 22:07 ` [Qemu-devel] [PATCH 2/2] Use extboot to support -kernel Anthony Liguori
  0 siblings, 1 reply; 2+ messages in thread
From: Anthony Liguori @ 2007-12-05 22:05 UTC (permalink / raw)
  To: qemu-devel

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

This is a generic in-memory block device.  It is needed by the next patch.

Regards,

Anthony Liguori

[-- Attachment #2: mem-block-dev.diff --]
[-- Type: text/x-patch, Size: 3553 bytes --]

Index: qemu/Makefile
===================================================================
--- qemu.orig/Makefile	2007-12-05 15:39:09.000000000 -0600
+++ qemu/Makefile	2007-12-05 15:39:39.000000000 -0600
@@ -40,7 +40,7 @@
 BLOCK_OBJS=cutils.o
 BLOCK_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o
 BLOCK_OBJS+=block-dmg.o block-bochs.o block-vpc.o block-vvfat.o
-BLOCK_OBJS+=block-qcow2.o block-parallels.o
+BLOCK_OBJS+=block-qcow2.o block-parallels.o block-mem.o
 
 ######################################################################
 # libqemu_common.a: Target indepedent part of system emulation. The
Index: qemu/block-mem.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ qemu/block-mem.c	2007-12-05 15:54:02.000000000 -0600
@@ -0,0 +1,99 @@
+/*
+ * In-memory block driver
+ *
+ * Copyright IBM, Corp. 2007
+ *
+ * Authors:
+ *  Anthony Liguori   <aliguori@us.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu-common.h"
+#include "block_int.h"
+
+typedef struct BDRVMemState {
+    void *mem;
+    size_t size;
+} BDRVMemState;
+
+static int mem_probe(const uint8_t *buf, int buf_size, const char *filename)
+{
+    return 0;
+}
+
+static int mem_open(BlockDriverState *bs, const char *filename, int flags)
+{
+    return -1;
+}
+
+int bdrv_mem_open(BlockDriverState *bs, size_t size)
+{
+    BDRVMemState *s;
+
+    bs->read_only = 0;
+    bs->is_temporary = 0;
+    bs->encrypted = 0;
+    pstrcpy(bs->filename, sizeof(bs->filename), "<mem>");
+    bs->drv = &bdrv_mem;
+    bs->total_sectors = ((size + 511) & ~511) / 512;
+
+    bs->opaque = qemu_mallocz(bdrv_mem.instance_size);
+    if (bs->opaque == NULL)
+	    return -1;
+
+    s = bs->opaque;
+    s->mem = qemu_mallocz(size);
+    if (s->mem == NULL)
+	return -1;
+    s->size = size;
+
+    return 0;
+}
+
+static int mem_read(BlockDriverState *bs, int64_t sector_num,
+		    uint8_t *buf, int nb_sectors)
+{
+    BDRVMemState *s = bs->opaque;
+    size_t size;
+
+    sector_num = MIN(sector_num, bs->total_sectors - 1);
+    size = MIN(s->size - (sector_num * 512), nb_sectors * 512);
+
+    memcpy(buf, s->mem + (sector_num * 512), size);
+
+    return 0;
+}
+
+static int mem_write(BlockDriverState *bs, int64_t sector_num,
+		     const uint8_t *buf, int nb_sectors)
+{
+    BDRVMemState *s = bs->opaque;
+    size_t size;
+
+    sector_num = MIN(sector_num, bs->total_sectors - 1);
+    size = MIN(s->size - (sector_num * 512), nb_sectors * 512);
+
+    memcpy(s->mem + (sector_num * 512), buf, size);
+
+    return 0;
+}
+
+static void mem_close(BlockDriverState *bs)
+{
+    BDRVMemState *s = bs->opaque;
+
+    qemu_free(s->mem);
+}
+
+BlockDriver bdrv_mem = {
+    "mem",
+    sizeof(BDRVMemState),
+    mem_probe,
+    mem_open,
+    mem_read,
+    mem_write,
+    mem_close,
+};
Index: qemu/block.h
===================================================================
--- qemu.orig/block.h	2007-12-05 15:38:48.000000000 -0600
+++ qemu/block.h	2007-12-05 15:47:36.000000000 -0600
@@ -16,6 +16,7 @@
 extern BlockDriver bdrv_vvfat;
 extern BlockDriver bdrv_qcow2;
 extern BlockDriver bdrv_parallels;
+extern BlockDriver bdrv_mem;
 
 typedef struct BlockDriverInfo {
     /* in bytes, 0 if irrelevant */
@@ -155,4 +156,6 @@
                   const char *base_path,
                   const char *filename);
 
+int bdrv_mem_open(BlockDriverState *bs, size_t size);
+
 #endif

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

* [Qemu-devel] [PATCH 2/2] Use extboot to support -kernel
  2007-12-05 22:05 [Qemu-devel] [PATCH 1/2] Add an in-memory block device Anthony Liguori
@ 2007-12-05 22:07 ` Anthony Liguori
  0 siblings, 0 replies; 2+ messages in thread
From: Anthony Liguori @ 2007-12-05 22:07 UTC (permalink / raw)
  To: qemu-devel

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

This patch uses the extboot option ROM to support -kernel.  Instead of 
hijacking the first boot sector of the first IDE drive, we use extboot 
to read the boot sector from an in-memory block device.  This eliminates 
the need to have an IDE device (or any device) configured when using 
-kernel.

This depends on the extboot series I posted today.

Regards,

Anthony Liguori

[-- Attachment #2: extboot-kernel.diff --]
[-- Type: text/x-patch, Size: 1927 bytes --]

Index: qemu/hw/pc.c
===================================================================
--- qemu.orig/hw/pc.c	2007-12-05 15:53:35.000000000 -0600
+++ qemu/hw/pc.c	2007-12-05 15:59:32.000000000 -0600
@@ -383,20 +383,26 @@
 static void generate_bootsect(uint32_t gpr[8], uint16_t segs[6], uint16_t ip)
 {
     uint8_t bootsect[512], *p;
-    int i;
-    int hda;
+    BlockDriverState *bs;
+    int i, cyls, heads, secs;
 
-    hda = drive_get_index(IF_IDE, 0, 0);
-    if (hda == -1) {
-	fprintf(stderr, "A disk image must be given for 'hda' when booting "
-		"a Linux kernel\n");
+    /* Create a device with at least one cylinder */
+    bs = bdrv_new("mem");
+    if (bdrv_mem_open(bs, 63 * 512) == -1) {
+	fprintf(stderr, "Error initializing memory block device\n");
 	exit(1);
     }
 
-    memset(bootsect, 0, sizeof(bootsect));
-
     /* Copy the MSDOS partition table if possible */
-    bdrv_read(drives_table[hda].bdrv, 0, bootsect, 1);
+    bdrv_read(bs, 0, bootsect, 1);
+
+    /* Setup extboot to boot from the memory block device */
+    bdrv_guess_geometry(bs, &cyls, &heads, &secs);
+    bdrv_set_geometry_hint(bs, cyls, heads, secs);
+    extboot_init(bs, 1);
+
+    /* Prevent double initialization */
+    extboot_drive = -1;
 
     /* Make sure we have a partition signature */
     bootsect[510] = 0x55;
@@ -433,7 +439,7 @@
     *p++ = segs[1];		/* CS */
     *p++ = segs[1] >> 8;
 
-    bdrv_set_boot_sector(drives_table[hda].bdrv, bootsect, sizeof(bootsect));
+    bdrv_write(bs, 0, bootsect, 1);
 }
 
 static int load_kernel(const char *filename, uint8_t *addr,
@@ -834,7 +840,7 @@
     for (i = 0; i < nb_option_roms; i++)
 	opt_rom_offset += load_option_rom(option_rom[i], opt_rom_offset);
 
-    if (extboot_drive != -1) {
+    if (extboot_drive != -1 || linux_boot) {
 	snprintf(buf, sizeof(buf), "%s/%s", bios_dir, EXTBOOT_FILENAME);
 	opt_rom_offset += load_option_rom(buf, opt_rom_offset);
     }

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

end of thread, other threads:[~2007-12-05 22:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-12-05 22:05 [Qemu-devel] [PATCH 1/2] Add an in-memory block device Anthony Liguori
2007-12-05 22:07 ` [Qemu-devel] [PATCH 2/2] Use extboot to support -kernel Anthony Liguori

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