===================================================================
@@ -0,0 +1,116 @@
+/*
+ * QEMU File : wrapper for bdrv
+ *
+ * Copyright (c) 2011, 2012, 2013 IBM Corporation
+ *
+ * Authors:
+ * Stefan Berger <stefanb@linux.vnet.ibm.com>
+ *
+ * Permission is hereby granted, free of charge, to any person
obtaining a cop
+ * of this software and associated documentation files (the
"Software"), to de
+ * in the Software without restriction, including without limitation
the right
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FRO
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/*
+ * Note: This QEMUFile wrapper introduces a dependency on the block layer.
+ * We keep it in this separate file to avoid pulling block.o and its
+ * dependencies into test programs.
+ */
+
+#include "qemu-common.h"
+#include "hw/hw.h"
+#include "block/block.h"
+
+typedef struct QEMUBdrvFile {
+ BlockDriverState *bds;
+ uint64_t offset;
+ QEMUFile *file;
+} QEMUBdrvFile;
+
+static int qf_bdrv_put_buffer(void *opaque, const uint8_t *buf,
+ int64_t pos, int size)
+{
+ QEMUBdrvFile *s = opaque;
+
+ return bdrv_pwrite(s->bds, pos + s->offset, buf, size);
+}
+
+static int qf_bdrv_get_buffer(void *opaque, uint8_t *buf,
+ int64_t pos, int size)
+{
+ QEMUBdrvFile *s = opaque;
+ ssize_t len = bdrv_getlength(s->bds) - (s->offset + pos);
+
+ if (len <= 0) {
+ return 0;
+ }
+
+ if (len > size) {
+ len = size;
+ }
+
+ len = bdrv_pread(s->bds, pos + s->offset, buf, len);
+
+ return len;
+}
+
+static int qf_bdrv_close(void *opaque)
+{
+ QEMUBdrvFile *s = opaque;
+
+ g_free(s);
+
+ return 0;
+}
+
+static const QEMUFileOps bdrv_read_ops = {
+ .get_buffer = qf_bdrv_get_buffer,
+ .close = qf_bdrv_close
+};
+
+static const QEMUFileOps bdrv_write_ops = {
+ .put_buffer = qf_bdrv_put_buffer,
+ .close = qf_bdrv_close
+};
+
+
+QEMUFile *qemu_bdrv_open(const char *mode, BlockDriverState *bds,
+ uint64_t offset)
+{
+ QEMUBdrvFile *s;
+
+ if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1]
!= 0) {
+ fprintf(stderr, "qemu_bdrv_open: Argument validity check
failed\n");
+ return NULL;
+ }
+
+ s = g_malloc0(sizeof(QEMUBdrvFile));
+ if (!s) {
+ return NULL;
+ }
+