qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 1/4] pflash: Support read-only mode
@ 2011-10-17 19:16 Jordan Justen
  2011-10-17 19:16 ` [Qemu-devel] [PATCH 2/4] pc: Support system flash memory with pflash Jordan Justen
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Jordan Justen @ 2011-10-17 19:16 UTC (permalink / raw)
  To: qemu-devel; +Cc: Jordan Justen, Jan Kiszka

Read-only mode is indicated by bdrv_is_read_only

When read-only mode is enabled, no changes will be made
to the flash image in memory, and no bdrv_write calls will be
made.

For pflash_cfi01 (Intel), if the flash is in read-only mode
then the status register will signal block erase error or
program error when these operations are attempted.

Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Cc: Jan Kiszka <jan.kiszka@siemens.com>
---
 blockdev.c        |    3 +-
 hw/pflash_cfi01.c |   44 +++++++++++++++++++---------
 hw/pflash_cfi02.c |   83 ++++++++++++++++++++++++++++------------------------
 3 files changed, 77 insertions(+), 53 deletions(-)

diff --git a/blockdev.c b/blockdev.c
index 0827bf7..c94aee5 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -506,7 +506,8 @@ DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
         /* CDROM is fine for any interface, don't check.  */
         ro = 1;
     } else if (ro == 1) {
-        if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
+        if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
+            type != IF_NONE && type != IF_PFLASH) {
             error_report("readonly not supported by this bus type");
             goto err;
         }
diff --git a/hw/pflash_cfi01.c b/hw/pflash_cfi01.c
index 69b8e3d..1e0a053 100644
--- a/hw/pflash_cfi01.c
+++ b/hw/pflash_cfi01.c
@@ -283,8 +283,12 @@ static void pflash_write(pflash_t *pfl, target_phys_addr_t offset,
                     TARGET_FMT_plx "\n",
                     __func__, offset, pfl->sector_len);
 
-            memset(p + offset, 0xff, pfl->sector_len);
-            pflash_update(pfl, offset, pfl->sector_len);
+            if (!pfl->ro) {
+                memset(p + offset, 0xff, pfl->sector_len);
+                pflash_update(pfl, offset, pfl->sector_len);
+            } else {
+                pfl->status |= 0x20; /* Block erase error */
+            }
             pfl->status |= 0x80; /* Ready! */
             break;
         case 0x50: /* Clear status bits */
@@ -323,8 +327,12 @@ static void pflash_write(pflash_t *pfl, target_phys_addr_t offset,
         case 0x10: /* Single Byte Program */
         case 0x40: /* Single Byte Program */
             DPRINTF("%s: Single Byte Program\n", __func__);
-            pflash_data_write(pfl, offset, value, width, be);
-            pflash_update(pfl, offset, width);
+            if (!pfl->ro) {
+                pflash_data_write(pfl, offset, value, width, be);
+                pflash_update(pfl, offset, width);
+            } else {
+                pfl->status |= 0x10; /* Programming error */
+            }
             pfl->status |= 0x80; /* Ready! */
             pfl->wcycle = 0;
         break;
@@ -372,7 +380,11 @@ static void pflash_write(pflash_t *pfl, target_phys_addr_t offset,
     case 2:
         switch (pfl->cmd) {
         case 0xe8: /* Block write */
-            pflash_data_write(pfl, offset, value, width, be);
+            if (!pfl->ro) {
+                pflash_data_write(pfl, offset, value, width, be);
+            } else {
+                pfl->status |= 0x10; /* Programming error */
+            }
 
             pfl->status |= 0x80;
 
@@ -382,8 +394,12 @@ static void pflash_write(pflash_t *pfl, target_phys_addr_t offset,
 
                 DPRINTF("%s: block write finished\n", __func__);
                 pfl->wcycle++;
-                /* Flush the entire write buffer onto backing storage.  */
-                pflash_update(pfl, offset & mask, pfl->writeblock_size);
+                if (!pfl->ro) {
+                    /* Flush the entire write buffer onto backing storage.  */
+                    pflash_update(pfl, offset & mask, pfl->writeblock_size);
+                } else {
+                    pfl->status |= 0x10; /* Programming error */
+                }
             }
 
             pfl->counter--;
@@ -605,13 +621,13 @@ pflash_t *pflash_cfi01_register(target_phys_addr_t base,
         }
         bdrv_attach_dev_nofail(pfl->bs, pfl);
     }
-#if 0 /* XXX: there should be a bit to set up read-only,
-       *      the same way the hardware does (with WP pin).
-       */
-    pfl->ro = 1;
-#else
-    pfl->ro = 0;
-#endif
+
+    if (pfl->bs) {
+        pfl->ro = bdrv_is_read_only(pfl->bs);
+    } else {
+        pfl->ro = 0;
+    }
+
     pfl->timer = qemu_new_timer_ns(vm_clock, pflash_timer, pfl);
     pfl->base = base;
     pfl->sector_len = sector_len;
diff --git a/hw/pflash_cfi02.c b/hw/pflash_cfi02.c
index e5a63da..9e91bdd 100644
--- a/hw/pflash_cfi02.c
+++ b/hw/pflash_cfi02.c
@@ -329,35 +329,37 @@ static void pflash_write (pflash_t *pfl, target_phys_addr_t offset,
             DPRINTF("%s: write data offset " TARGET_FMT_plx " %08x %d\n",
                     __func__, offset, value, width);
             p = pfl->storage;
-            switch (width) {
-            case 1:
-                p[offset] &= value;
-                pflash_update(pfl, offset, 1);
-                break;
-            case 2:
-                if (be) {
-                    p[offset] &= value >> 8;
-                    p[offset + 1] &= value;
-                } else {
+            if (!pfl->ro) {
+                switch (width) {
+                case 1:
                     p[offset] &= value;
-                    p[offset + 1] &= value >> 8;
+                    pflash_update(pfl, offset, 1);
+                    break;
+                case 2:
+                    if (be) {
+                        p[offset] &= value >> 8;
+                        p[offset + 1] &= value;
+                    } else {
+                        p[offset] &= value;
+                        p[offset + 1] &= value >> 8;
+                    }
+                    pflash_update(pfl, offset, 2);
+                    break;
+                case 4:
+                    if (be) {
+                        p[offset] &= value >> 24;
+                        p[offset + 1] &= value >> 16;
+                        p[offset + 2] &= value >> 8;
+                        p[offset + 3] &= value;
+                    } else {
+                        p[offset] &= value;
+                        p[offset + 1] &= value >> 8;
+                        p[offset + 2] &= value >> 16;
+                        p[offset + 3] &= value >> 24;
+                    }
+                    pflash_update(pfl, offset, 4);
+                    break;
                 }
-                pflash_update(pfl, offset, 2);
-                break;
-            case 4:
-                if (be) {
-                    p[offset] &= value >> 24;
-                    p[offset + 1] &= value >> 16;
-                    p[offset + 2] &= value >> 8;
-                    p[offset + 3] &= value;
-                } else {
-                    p[offset] &= value;
-                    p[offset + 1] &= value >> 8;
-                    p[offset + 2] &= value >> 16;
-                    p[offset + 3] &= value >> 24;
-                }
-                pflash_update(pfl, offset, 4);
-                break;
             }
             pfl->status = 0x00 | ~(value & 0x80);
             /* Let's pretend write is immediate */
@@ -403,9 +405,11 @@ static void pflash_write (pflash_t *pfl, target_phys_addr_t offset,
             }
             /* Chip erase */
             DPRINTF("%s: start chip erase\n", __func__);
-            memset(pfl->storage, 0xFF, pfl->chip_len);
+            if (!pfl->ro) {
+                memset(pfl->storage, 0xFF, pfl->chip_len);
+                pflash_update(pfl, 0, pfl->chip_len);
+            }
             pfl->status = 0x00;
-            pflash_update(pfl, 0, pfl->chip_len);
             /* Let's wait 5 seconds before chip erase is done */
             qemu_mod_timer(pfl->timer,
                            qemu_get_clock_ns(vm_clock) + (get_ticks_per_sec() * 5));
@@ -416,8 +420,10 @@ static void pflash_write (pflash_t *pfl, target_phys_addr_t offset,
             offset &= ~(pfl->sector_len - 1);
             DPRINTF("%s: start sector erase at " TARGET_FMT_plx "\n", __func__,
                     offset);
-            memset(p + offset, 0xFF, pfl->sector_len);
-            pflash_update(pfl, offset, pfl->sector_len);
+            if (!pfl->ro) {
+                memset(p + offset, 0xFF, pfl->sector_len);
+                pflash_update(pfl, offset, pfl->sector_len);
+            }
             pfl->status = 0x00;
             /* Let's wait 1/2 second before sector erase is done */
             qemu_mod_timer(pfl->timer,
@@ -643,16 +649,17 @@ pflash_t *pflash_cfi02_register(target_phys_addr_t base,
         }
         bdrv_attach_dev_nofail(pfl->bs, pfl);
     }
+
     pflash_setup_mappings(pfl);
     pfl->rom_mode = 1;
     memory_region_add_subregion(get_system_memory(), pfl->base, &pfl->mem);
-#if 0 /* XXX: there should be a bit to set up read-only,
-       *      the same way the hardware does (with WP pin).
-       */
-    pfl->ro = 1;
-#else
-    pfl->ro = 0;
-#endif
+
+    if (pfl->bs) {
+        pfl->ro = bdrv_is_read_only(pfl->bs);
+    } else {
+        pfl->ro = 0;
+    }
+
     pfl->timer = qemu_new_timer_ns(vm_clock, pflash_timer, pfl);
     pfl->sector_len = sector_len;
     pfl->width = width;
-- 
1.7.1

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

* [Qemu-devel] [PATCH 2/4] pc: Support system flash memory with pflash
  2011-10-17 19:16 [Qemu-devel] [PATCH 1/4] pflash: Support read-only mode Jordan Justen
@ 2011-10-17 19:16 ` Jordan Justen
  2011-10-17 19:27   ` Jordan Justen
  2011-10-17 19:16 ` [Qemu-devel] [PATCH 3/4] loader: Add rom_add_file_buf for adding roms from a buffer Jordan Justen
  2011-10-17 19:16 ` [Qemu-devel] [PATCH 4/4] pcflash: Add pc flash to qemu roms Jordan Justen
  2 siblings, 1 reply; 9+ messages in thread
From: Jordan Justen @ 2011-10-17 19:16 UTC (permalink / raw)
  To: qemu-devel; +Cc: Jordan Justen, Anthony Liguori

If a pflash image is found, then it is used for the system
firmware image.

If a pflash image is not initially found, then a read-only
pflash device is created using the -bios filename.

Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Cc: Anthony Liguori <aliguori@us.ibm.com>
---
 Makefile.target                    |    1 +
 default-configs/i386-softmmu.mak   |    1 +
 default-configs/x86_64-softmmu.mak |    1 +
 hw/boards.h                        |    1 +
 hw/pc.c                            |   55 +------------
 hw/pc.h                            |    3 +
 hw/pcflash.c                       |  145 ++++++++++++++++++++++++++++++++++++
 vl.c                               |    2 +-
 8 files changed, 158 insertions(+), 51 deletions(-)
 create mode 100644 hw/pcflash.c

diff --git a/Makefile.target b/Makefile.target
index 417f23e..37a5b56 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -225,6 +225,7 @@ obj-i386-y += vmport.o
 obj-i386-y += device-hotplug.o pci-hotplug.o smbios.o wdt_ib700.o
 obj-i386-y += debugcon.o multiboot.o
 obj-i386-y += pc_piix.o
+obj-i386-y += pcflash.o
 obj-i386-$(CONFIG_KVM) += kvmclock.o
 obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o
 
diff --git a/default-configs/i386-softmmu.mak b/default-configs/i386-softmmu.mak
index e67ebb3..cd407a9 100644
--- a/default-configs/i386-softmmu.mak
+++ b/default-configs/i386-softmmu.mak
@@ -22,3 +22,4 @@ CONFIG_SOUND=y
 CONFIG_HPET=y
 CONFIG_APPLESMC=y
 CONFIG_I8259=y
+CONFIG_PFLASH_CFI01=y
diff --git a/default-configs/x86_64-softmmu.mak b/default-configs/x86_64-softmmu.mak
index b75757e..47734ea 100644
--- a/default-configs/x86_64-softmmu.mak
+++ b/default-configs/x86_64-softmmu.mak
@@ -22,3 +22,4 @@ CONFIG_SOUND=y
 CONFIG_HPET=y
 CONFIG_APPLESMC=y
 CONFIG_I8259=y
+CONFIG_PFLASH_CFI01=y
diff --git a/hw/boards.h b/hw/boards.h
index 716fd7b..45a31a1 100644
--- a/hw/boards.h
+++ b/hw/boards.h
@@ -33,6 +33,7 @@ typedef struct QEMUMachine {
 } QEMUMachine;
 
 int qemu_register_machine(QEMUMachine *m);
+QEMUMachine *find_default_machine(void);
 
 extern QEMUMachine *current_machine;
 
diff --git a/hw/pc.c b/hw/pc.c
index f0802b7..0c9b7ba 100644
--- a/hw/pc.c
+++ b/hw/pc.c
@@ -57,10 +57,6 @@
 #define DPRINTF(fmt, ...)
 #endif
 
-#define BIOS_FILENAME "bios.bin"
-
-#define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
-
 /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables.  */
 #define ACPI_DATA_SIZE       0x10000
 #define BIOS_CFG_IOPORT 0x510
@@ -974,11 +970,9 @@ void pc_memory_init(MemoryRegion *system_memory,
                     MemoryRegion *rom_memory,
                     MemoryRegion **ram_memory)
 {
-    char *filename;
-    int ret, linux_boot, i;
-    MemoryRegion *ram, *bios, *isa_bios, *option_rom_mr;
+    int linux_boot, i;
+    MemoryRegion *ram, *option_rom_mr;
     MemoryRegion *ram_below_4g, *ram_above_4g;
-    int bios_size, isa_bios_size;
     void *fw_cfg;
 
     linux_boot = (kernel_filename != NULL);
@@ -1003,43 +997,9 @@ void pc_memory_init(MemoryRegion *system_memory,
                                     ram_above_4g);
     }
 
-    /* BIOS load */
-    if (bios_name == NULL)
-        bios_name = BIOS_FILENAME;
-    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
-    if (filename) {
-        bios_size = get_image_size(filename);
-    } else {
-        bios_size = -1;
-    }
-    if (bios_size <= 0 ||
-        (bios_size % 65536) != 0) {
-        goto bios_error;
-    }
-    bios = g_malloc(sizeof(*bios));
-    memory_region_init_ram(bios, NULL, "pc.bios", bios_size);
-    memory_region_set_readonly(bios, true);
-    ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
-    if (ret != 0) {
-    bios_error:
-        fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
-        exit(1);
-    }
-    if (filename) {
-        g_free(filename);
-    }
-    /* map the last 128KB of the BIOS in ISA space */
-    isa_bios_size = bios_size;
-    if (isa_bios_size > (128 * 1024))
-        isa_bios_size = 128 * 1024;
-    isa_bios = g_malloc(sizeof(*isa_bios));
-    memory_region_init_alias(isa_bios, "isa-bios", bios,
-                             bios_size - isa_bios_size, isa_bios_size);
-    memory_region_add_subregion_overlap(rom_memory,
-                                        0x100000 - isa_bios_size,
-                                        isa_bios,
-                                        1);
-    memory_region_set_readonly(isa_bios, true);
+
+    /* Initialize ROM or flash ranges for PC firmware */
+    pc_system_firmware_init(rom_memory);
 
     option_rom_mr = g_malloc(sizeof(*option_rom_mr));
     memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE);
@@ -1048,11 +1008,6 @@ void pc_memory_init(MemoryRegion *system_memory,
                                         option_rom_mr,
                                         1);
 
-    /* map all the bios at the top of memory */
-    memory_region_add_subregion(rom_memory,
-                                (uint32_t)(-bios_size),
-                                bios);
-
     fw_cfg = bochs_bios_init();
     rom_set_fw(fw_cfg);
 
diff --git a/hw/pc.h b/hw/pc.h
index b8ad9a3..333bd70 100644
--- a/hw/pc.h
+++ b/hw/pc.h
@@ -243,6 +243,9 @@ static inline bool isa_ne2000_init(int base, int irq, NICInfo *nd)
     return true;
 }
 
+/* pcflash.c */
+void pc_system_firmware_init(MemoryRegion *rom_memory);
+
 /* e820 types */
 #define E820_RAM        1
 #define E820_RESERVED   2
diff --git a/hw/pcflash.c b/hw/pcflash.c
new file mode 100644
index 0000000..eece7ec
--- /dev/null
+++ b/hw/pcflash.c
@@ -0,0 +1,145 @@
+/*
+ * QEMU PC System Flash
+ *
+ * Copyright (c) 2003-2004 Fabrice Bellard
+ * Copyright (c) 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * 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 FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "hw.h"
+#include "pc.h"
+#include "hw/boards.h"
+#include "loader.h"
+#include "sysemu.h"
+#include "flash.h"
+
+#define BIOS_FILENAME "bios.bin"
+
+static void pc_isa_bios_init(MemoryRegion *rom_memory,
+                             MemoryRegion *flash_mem,
+                             int ram_size)
+{
+    int isa_bios_size;
+    MemoryRegion *isa_bios;
+    uint64_t flash_size;
+    void *flash_ptr, *isa_bios_ptr;
+
+    flash_size = memory_region_size(flash_mem);
+
+    /* map the last 128KB of the BIOS in ISA space */
+    isa_bios_size = flash_size;
+    if (isa_bios_size > (128 * 1024)) {
+        isa_bios_size = 128 * 1024;
+    }
+    isa_bios = g_malloc(sizeof(*isa_bios));
+    memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size);
+    memory_region_add_subregion_overlap(rom_memory,
+                                        0x100000 - isa_bios_size,
+                                        isa_bios,
+                                        1);
+
+    /* copy ISA rom image from top of flash memory */
+    flash_ptr = memory_region_get_ram_ptr(flash_mem);
+    isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
+    memcpy(isa_bios_ptr,
+           ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
+           isa_bios_size);
+
+    memory_region_set_readonly(isa_bios, true);
+}
+
+static void pc_default_system_flash_init(void)
+{
+    QemuOpts *opts;
+    QEMUMachine *machine;
+    char *filename;
+
+    if (bios_name == NULL) {
+        bios_name = BIOS_FILENAME;
+    }
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
+
+    opts = drive_add(IF_PFLASH, -1, filename, "readonly=on");
+    if (opts == NULL) {
+      return;
+    }
+
+    machine = find_default_machine();
+    if (machine == NULL) {
+      return;
+    }
+
+    drive_init(opts, machine->use_scsi);
+}
+
+static void pc_system_flash_init(MemoryRegion *rom_memory,
+                                 DriveInfo *pflash_drv)
+{
+    BlockDriverState *bdrv;
+    int64_t size;
+    target_phys_addr_t phys_addr;
+    int sector_bits, sector_size;
+    pflash_t *system_flash;
+    MemoryRegion *flash_mem;
+
+    bdrv = pflash_drv->bdrv;
+    size = bdrv_getlength(pflash_drv->bdrv);
+    sector_bits = 12;
+    sector_size = 1 << sector_bits;
+
+    if ((size % sector_size) != 0) {
+        fprintf(stderr,
+                "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n",
+                sector_size);
+        exit(1);
+    }
+
+    phys_addr = 0x100000000ULL - size;
+    system_flash = pflash_cfi01_register(phys_addr, NULL, "system.flash", size,
+                                         bdrv, sector_size, size >> sector_bits,
+                                         1, 0x0000, 0x0000, 0x0000, 0x0000, 0);
+    flash_mem = pflash_cfi01_get_memory(system_flash);
+
+    pc_isa_bios_init(rom_memory, flash_mem, size);
+}
+
+void pc_system_firmware_init(MemoryRegion *rom_memory)
+{
+    int flash_present;
+    DriveInfo *pflash_drv;
+
+    pflash_drv = drive_get(IF_PFLASH, 0, 0);
+    flash_present = (pflash_drv != NULL);
+
+    if (!flash_present) {
+        pc_default_system_flash_init();
+        pflash_drv = drive_get(IF_PFLASH, 0, 0);
+        flash_present = (pflash_drv != NULL);
+    }
+
+    if (!flash_present) {
+        fprintf(stderr, "qemu: PC system firmware (pflash) not available\n");
+        exit(1);
+    }
+
+    pc_system_flash_init(rom_memory, pflash_drv);
+}
+
+
diff --git a/vl.c b/vl.c
index 2dce3ae..0deae10 100644
--- a/vl.c
+++ b/vl.c
@@ -1173,7 +1173,7 @@ static QEMUMachine *find_machine(const char *name)
     return NULL;
 }
 
-static QEMUMachine *find_default_machine(void)
+QEMUMachine *find_default_machine(void)
 {
     QEMUMachine *m;
 
-- 
1.7.1

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

* [Qemu-devel] [PATCH 3/4] loader: Add rom_add_file_buf for adding roms from a buffer
  2011-10-17 19:16 [Qemu-devel] [PATCH 1/4] pflash: Support read-only mode Jordan Justen
  2011-10-17 19:16 ` [Qemu-devel] [PATCH 2/4] pc: Support system flash memory with pflash Jordan Justen
@ 2011-10-17 19:16 ` Jordan Justen
  2011-10-18 18:05   ` Blue Swirl
  2011-10-17 19:16 ` [Qemu-devel] [PATCH 4/4] pcflash: Add pc flash to qemu roms Jordan Justen
  2 siblings, 1 reply; 9+ messages in thread
From: Jordan Justen @ 2011-10-17 19:16 UTC (permalink / raw)
  To: qemu-devel; +Cc: Jordan Justen

rom_add_file_buf is similar to rom_add_file, except the rom's
contents are provided in a buffer.

rom_add_file is modified to call rom_add_file_buf after
reading the rom's contents from the file.

Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
---
 hw/loader.c |   71 +++++++++++++++++++++++++++++++++++++++-------------------
 hw/loader.h |    5 ++++
 2 files changed, 53 insertions(+), 23 deletions(-)

diff --git a/hw/loader.c b/hw/loader.c
index 5676c18..d1a4a98 100644
--- a/hw/loader.c
+++ b/hw/loader.c
@@ -557,11 +557,11 @@ static void rom_insert(Rom *rom)
     QTAILQ_INSERT_TAIL(&roms, rom, next);
 }
 
-int rom_add_file(const char *file, const char *fw_dir,
-                 target_phys_addr_t addr, int32_t bootindex)
+int rom_add_file_buf(const char *file, const void *data, size_t size,
+                     const char *fw_dir,
+                     target_phys_addr_t addr, int32_t bootindex)
 {
     Rom *rom;
-    int rc, fd = -1;
     char devpath[100];
 
     rom = g_malloc0(sizeof(*rom));
@@ -571,28 +571,16 @@ int rom_add_file(const char *file, const char *fw_dir,
         rom->path = g_strdup(file);
     }
 
-    fd = open(rom->path, O_RDONLY | O_BINARY);
-    if (fd == -1) {
-        fprintf(stderr, "Could not open option rom '%s': %s\n",
-                rom->path, strerror(errno));
-        goto err;
-    }
-
     if (fw_dir) {
         rom->fw_dir  = g_strdup(fw_dir);
         rom->fw_file = g_strdup(file);
     }
     rom->addr    = addr;
-    rom->romsize = lseek(fd, 0, SEEK_END);
+    rom->romsize = size;
     rom->data    = g_malloc0(rom->romsize);
-    lseek(fd, 0, SEEK_SET);
-    rc = read(fd, rom->data, rom->romsize);
-    if (rc != rom->romsize) {
-        fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
-                rom->name, rc, rom->romsize);
-        goto err;
-    }
-    close(fd);
+
+    memcpy(rom->data, data, rom->romsize);
+
     rom_insert(rom);
     if (rom->fw_file && fw_cfg) {
         const char *basename;
@@ -614,14 +602,51 @@ int rom_add_file(const char *file, const char *fw_dir,
 
     add_boot_device_path(bootindex, NULL, devpath);
     return 0;
+}
+
+int rom_add_file(const char *file, const char *fw_dir,
+                 target_phys_addr_t addr, int32_t bootindex)
+{
+    char *filename;
+    void *data = NULL;
+    size_t size;
+    int rc, fd = -1;
+
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, file);
+    if (filename == NULL) {
+        filename = g_strdup(file);
+    }
+
+    fd = open(filename, O_RDONLY | O_BINARY);
+    if (fd == -1) {
+        fprintf(stderr, "Could not open option rom '%s': %s\n",
+                filename, strerror(errno));
+        goto err;
+    }
+
+    size = lseek(fd, 0, SEEK_END);
+    data = g_malloc0(size);
+    lseek(fd, 0, SEEK_SET);
+    rc = read(fd, data, size);
+    if (rc != size) {
+        fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
+                filename, rc, size);
+        goto err;
+    }
+    close(fd);
+
+    rc = rom_add_file_buf(filename, data, size, fw_dir, addr, bootindex);
+    if (rc != 0) {
+        goto err;
+    }
+
+    g_free(data);
+    return 0;
 
 err:
     if (fd != -1)
         close(fd);
-    g_free(rom->data);
-    g_free(rom->path);
-    g_free(rom->name);
-    g_free(rom);
+    g_free(data);
     return -1;
 }
 
diff --git a/hw/loader.h b/hw/loader.h
index fc6bdff..9efe64a 100644
--- a/hw/loader.h
+++ b/hw/loader.h
@@ -21,6 +21,9 @@ void pstrcpy_targphys(const char *name,
                       const char *source);
 
 
+int rom_add_file_buf(const char *file, const void *data, size_t size,
+                     const char *fw_dir,
+                     target_phys_addr_t addr, int32_t bootindex);
 int rom_add_file(const char *file, const char *fw_dir,
                  target_phys_addr_t addr, int32_t bootindex);
 int rom_add_blob(const char *name, const void *blob, size_t len,
@@ -31,6 +34,8 @@ int rom_copy(uint8_t *dest, target_phys_addr_t addr, size_t size);
 void *rom_ptr(target_phys_addr_t addr);
 void do_info_roms(Monitor *mon);
 
+#define rom_add_file_buf_fixed(_f, _d, _s, _a, _i)          \
+    rom_add_file_buf(_f, _d, _s, NULL, _a, _i)
 #define rom_add_file_fixed(_f, _a, _i)          \
     rom_add_file(_f, NULL, _a, _i)
 #define rom_add_blob_fixed(_f, _b, _l, _a)      \
-- 
1.7.1

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

* [Qemu-devel] [PATCH 4/4] pcflash: Add pc flash to qemu roms
  2011-10-17 19:16 [Qemu-devel] [PATCH 1/4] pflash: Support read-only mode Jordan Justen
  2011-10-17 19:16 ` [Qemu-devel] [PATCH 2/4] pc: Support system flash memory with pflash Jordan Justen
  2011-10-17 19:16 ` [Qemu-devel] [PATCH 3/4] loader: Add rom_add_file_buf for adding roms from a buffer Jordan Justen
@ 2011-10-17 19:16 ` Jordan Justen
  2 siblings, 0 replies; 9+ messages in thread
From: Jordan Justen @ 2011-10-17 19:16 UTC (permalink / raw)
  To: qemu-devel; +Cc: Jordan Justen

The pflash image is added to the roms using the memory
region buffer and rom_add_file_buf_fixed.

Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
---
 hw/pcflash.c |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/hw/pcflash.c b/hw/pcflash.c
index eece7ec..e28bdb0 100644
--- a/hw/pcflash.c
+++ b/hw/pcflash.c
@@ -65,6 +65,21 @@ static void pc_isa_bios_init(MemoryRegion *rom_memory,
     memory_region_set_readonly(isa_bios, true);
 }
 
+static void pc_flash_add_qemu_rom(DriveInfo *pflash_drv,
+                                  MemoryRegion *flash_mem)
+{
+    const char *filename;
+    void *data;
+    uint64_t size;
+    uint32_t addr;
+
+    filename = qemu_opt_get(pflash_drv->opts, "file");
+    data = memory_region_get_ram_ptr(flash_mem);
+    size = memory_region_size(flash_mem);
+    addr = (uint32_t) -((int32_t)size);
+    rom_add_file_buf_fixed(filename, data, size, addr, -1);
+}
+
 static void pc_default_system_flash_init(void)
 {
     QemuOpts *opts;
@@ -118,6 +133,7 @@ static void pc_system_flash_init(MemoryRegion *rom_memory,
     flash_mem = pflash_cfi01_get_memory(system_flash);
 
     pc_isa_bios_init(rom_memory, flash_mem, size);
+    pc_flash_add_qemu_rom(pflash_drv, flash_mem);
 }
 
 void pc_system_firmware_init(MemoryRegion *rom_memory)
-- 
1.7.1

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

* Re: [Qemu-devel] [PATCH 2/4] pc: Support system flash memory with pflash
  2011-10-17 19:16 ` [Qemu-devel] [PATCH 2/4] pc: Support system flash memory with pflash Jordan Justen
@ 2011-10-17 19:27   ` Jordan Justen
  0 siblings, 0 replies; 9+ messages in thread
From: Jordan Justen @ 2011-10-17 19:27 UTC (permalink / raw)
  To: qemu-devel; +Cc: Jordan Justen

On Mon, Oct 17, 2011 at 12:16, Jordan Justen <jordan.l.justen@intel.com> wrote:
> If a pflash image is found, then it is used for the system
> firmware image.
>
> If a pflash image is not initially found, then a read-only
> pflash device is created using the -bios filename.
>
> Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
> Cc: Anthony Liguori <aliguori@us.ibm.com>
> ---
>  Makefile.target                    |    1 +
>  default-configs/i386-softmmu.mak   |    1 +
>  default-configs/x86_64-softmmu.mak |    1 +
>  hw/boards.h                        |    1 +
>  hw/pc.c                            |   55 +------------
>  hw/pc.h                            |    3 +
>  hw/pcflash.c                       |  145 ++++++++++++++++++++++++++++++++++++
>  vl.c                               |    2 +-
>  8 files changed, 158 insertions(+), 51 deletions(-)
>  create mode 100644 hw/pcflash.c
>
> diff --git a/Makefile.target b/Makefile.target
> index 417f23e..37a5b56 100644
> --- a/Makefile.target
> +++ b/Makefile.target
> @@ -225,6 +225,7 @@ obj-i386-y += vmport.o
>  obj-i386-y += device-hotplug.o pci-hotplug.o smbios.o wdt_ib700.o
>  obj-i386-y += debugcon.o multiboot.o
>  obj-i386-y += pc_piix.o
> +obj-i386-y += pcflash.o
>  obj-i386-$(CONFIG_KVM) += kvmclock.o
>  obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o
>
> diff --git a/default-configs/i386-softmmu.mak b/default-configs/i386-softmmu.mak
> index e67ebb3..cd407a9 100644
> --- a/default-configs/i386-softmmu.mak
> +++ b/default-configs/i386-softmmu.mak
> @@ -22,3 +22,4 @@ CONFIG_SOUND=y
>  CONFIG_HPET=y
>  CONFIG_APPLESMC=y
>  CONFIG_I8259=y
> +CONFIG_PFLASH_CFI01=y
> diff --git a/default-configs/x86_64-softmmu.mak b/default-configs/x86_64-softmmu.mak
> index b75757e..47734ea 100644
> --- a/default-configs/x86_64-softmmu.mak
> +++ b/default-configs/x86_64-softmmu.mak
> @@ -22,3 +22,4 @@ CONFIG_SOUND=y
>  CONFIG_HPET=y
>  CONFIG_APPLESMC=y
>  CONFIG_I8259=y
> +CONFIG_PFLASH_CFI01=y
> diff --git a/hw/boards.h b/hw/boards.h
> index 716fd7b..45a31a1 100644
> --- a/hw/boards.h
> +++ b/hw/boards.h
> @@ -33,6 +33,7 @@ typedef struct QEMUMachine {
>  } QEMUMachine;
>
>  int qemu_register_machine(QEMUMachine *m);
> +QEMUMachine *find_default_machine(void);
>
>  extern QEMUMachine *current_machine;
>
> diff --git a/hw/pc.c b/hw/pc.c
> index f0802b7..0c9b7ba 100644
> --- a/hw/pc.c
> +++ b/hw/pc.c
> @@ -57,10 +57,6 @@
>  #define DPRINTF(fmt, ...)
>  #endif
>
> -#define BIOS_FILENAME "bios.bin"
> -
> -#define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
> -
>  /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables.  */
>  #define ACPI_DATA_SIZE       0x10000
>  #define BIOS_CFG_IOPORT 0x510
> @@ -974,11 +970,9 @@ void pc_memory_init(MemoryRegion *system_memory,
>                     MemoryRegion *rom_memory,
>                     MemoryRegion **ram_memory)
>  {
> -    char *filename;
> -    int ret, linux_boot, i;
> -    MemoryRegion *ram, *bios, *isa_bios, *option_rom_mr;
> +    int linux_boot, i;
> +    MemoryRegion *ram, *option_rom_mr;
>     MemoryRegion *ram_below_4g, *ram_above_4g;
> -    int bios_size, isa_bios_size;
>     void *fw_cfg;
>
>     linux_boot = (kernel_filename != NULL);
> @@ -1003,43 +997,9 @@ void pc_memory_init(MemoryRegion *system_memory,
>                                     ram_above_4g);
>     }
>
> -    /* BIOS load */
> -    if (bios_name == NULL)
> -        bios_name = BIOS_FILENAME;
> -    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
> -    if (filename) {
> -        bios_size = get_image_size(filename);
> -    } else {
> -        bios_size = -1;
> -    }
> -    if (bios_size <= 0 ||
> -        (bios_size % 65536) != 0) {
> -        goto bios_error;
> -    }
> -    bios = g_malloc(sizeof(*bios));
> -    memory_region_init_ram(bios, NULL, "pc.bios", bios_size);
> -    memory_region_set_readonly(bios, true);
> -    ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
> -    if (ret != 0) {
> -    bios_error:
> -        fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
> -        exit(1);
> -    }
> -    if (filename) {
> -        g_free(filename);
> -    }
> -    /* map the last 128KB of the BIOS in ISA space */
> -    isa_bios_size = bios_size;
> -    if (isa_bios_size > (128 * 1024))
> -        isa_bios_size = 128 * 1024;
> -    isa_bios = g_malloc(sizeof(*isa_bios));
> -    memory_region_init_alias(isa_bios, "isa-bios", bios,
> -                             bios_size - isa_bios_size, isa_bios_size);
> -    memory_region_add_subregion_overlap(rom_memory,
> -                                        0x100000 - isa_bios_size,
> -                                        isa_bios,
> -                                        1);
> -    memory_region_set_readonly(isa_bios, true);
> +
> +    /* Initialize ROM or flash ranges for PC firmware */
> +    pc_system_firmware_init(rom_memory);
>
>     option_rom_mr = g_malloc(sizeof(*option_rom_mr));
>     memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE);
> @@ -1048,11 +1008,6 @@ void pc_memory_init(MemoryRegion *system_memory,
>                                         option_rom_mr,
>                                         1);
>
> -    /* map all the bios at the top of memory */
> -    memory_region_add_subregion(rom_memory,
> -                                (uint32_t)(-bios_size),
> -                                bios);
> -
>     fw_cfg = bochs_bios_init();
>     rom_set_fw(fw_cfg);
>
> diff --git a/hw/pc.h b/hw/pc.h
> index b8ad9a3..333bd70 100644
> --- a/hw/pc.h
> +++ b/hw/pc.h
> @@ -243,6 +243,9 @@ static inline bool isa_ne2000_init(int base, int irq, NICInfo *nd)
>     return true;
>  }
>
> +/* pcflash.c */
> +void pc_system_firmware_init(MemoryRegion *rom_memory);
> +
>  /* e820 types */
>  #define E820_RAM        1
>  #define E820_RESERVED   2
> diff --git a/hw/pcflash.c b/hw/pcflash.c
> new file mode 100644
> index 0000000..eece7ec
> --- /dev/null
> +++ b/hw/pcflash.c
> @@ -0,0 +1,145 @@
> +/*
> + * QEMU PC System Flash
> + *
> + * Copyright (c) 2003-2004 Fabrice Bellard
> + * Copyright (c) 2011 Intel Corporation
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a copy
> + * of this software and associated documentation files (the "Software"), to deal
> + * in the Software without restriction, including without limitation the rights
> + * 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 FROM,
> + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> + * THE SOFTWARE.
> + */
> +
> +#include "hw.h"
> +#include "pc.h"
> +#include "hw/boards.h"
> +#include "loader.h"
> +#include "sysemu.h"
> +#include "flash.h"
> +
> +#define BIOS_FILENAME "bios.bin"
> +
> +static void pc_isa_bios_init(MemoryRegion *rom_memory,
> +                             MemoryRegion *flash_mem,
> +                             int ram_size)
> +{
> +    int isa_bios_size;
> +    MemoryRegion *isa_bios;
> +    uint64_t flash_size;
> +    void *flash_ptr, *isa_bios_ptr;
> +
> +    flash_size = memory_region_size(flash_mem);
> +
> +    /* map the last 128KB of the BIOS in ISA space */
> +    isa_bios_size = flash_size;
> +    if (isa_bios_size > (128 * 1024)) {
> +        isa_bios_size = 128 * 1024;
> +    }
> +    isa_bios = g_malloc(sizeof(*isa_bios));
> +    memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size);
> +    memory_region_add_subregion_overlap(rom_memory,
> +                                        0x100000 - isa_bios_size,
> +                                        isa_bios,
> +                                        1);
> +
> +    /* copy ISA rom image from top of flash memory */
> +    flash_ptr = memory_region_get_ram_ptr(flash_mem);
> +    isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
> +    memcpy(isa_bios_ptr,
> +           ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
> +           isa_bios_size);
> +
> +    memory_region_set_readonly(isa_bios, true);

Note: This does not 'alias' the pflash memory region, and therefore
this does not emulate a real 440 chipset well.  I could not get this
to work.  Is there a limitation with aliasing rom memory regions like
pflash?

Instead I create a new ram memory region, copy the initial pflash
contents, and then present it as a ROM in the 0xe0000-0xfffff range.
This worked for seabios.

Thanks,

-Jordan

> +}
> +
> +static void pc_default_system_flash_init(void)
> +{
> +    QemuOpts *opts;
> +    QEMUMachine *machine;
> +    char *filename;
> +
> +    if (bios_name == NULL) {
> +        bios_name = BIOS_FILENAME;
> +    }
> +    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
> +
> +    opts = drive_add(IF_PFLASH, -1, filename, "readonly=on");
> +    if (opts == NULL) {
> +      return;
> +    }
> +
> +    machine = find_default_machine();
> +    if (machine == NULL) {
> +      return;
> +    }
> +
> +    drive_init(opts, machine->use_scsi);
> +}
> +
> +static void pc_system_flash_init(MemoryRegion *rom_memory,
> +                                 DriveInfo *pflash_drv)
> +{
> +    BlockDriverState *bdrv;
> +    int64_t size;
> +    target_phys_addr_t phys_addr;
> +    int sector_bits, sector_size;
> +    pflash_t *system_flash;
> +    MemoryRegion *flash_mem;
> +
> +    bdrv = pflash_drv->bdrv;
> +    size = bdrv_getlength(pflash_drv->bdrv);
> +    sector_bits = 12;
> +    sector_size = 1 << sector_bits;
> +
> +    if ((size % sector_size) != 0) {
> +        fprintf(stderr,
> +                "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n",
> +                sector_size);
> +        exit(1);
> +    }
> +
> +    phys_addr = 0x100000000ULL - size;
> +    system_flash = pflash_cfi01_register(phys_addr, NULL, "system.flash", size,
> +                                         bdrv, sector_size, size >> sector_bits,
> +                                         1, 0x0000, 0x0000, 0x0000, 0x0000, 0);
> +    flash_mem = pflash_cfi01_get_memory(system_flash);
> +
> +    pc_isa_bios_init(rom_memory, flash_mem, size);
> +}
> +
> +void pc_system_firmware_init(MemoryRegion *rom_memory)
> +{
> +    int flash_present;
> +    DriveInfo *pflash_drv;
> +
> +    pflash_drv = drive_get(IF_PFLASH, 0, 0);
> +    flash_present = (pflash_drv != NULL);
> +
> +    if (!flash_present) {
> +        pc_default_system_flash_init();
> +        pflash_drv = drive_get(IF_PFLASH, 0, 0);
> +        flash_present = (pflash_drv != NULL);
> +    }
> +
> +    if (!flash_present) {
> +        fprintf(stderr, "qemu: PC system firmware (pflash) not available\n");
> +        exit(1);
> +    }
> +
> +    pc_system_flash_init(rom_memory, pflash_drv);
> +}
> +
> +
> diff --git a/vl.c b/vl.c
> index 2dce3ae..0deae10 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -1173,7 +1173,7 @@ static QEMUMachine *find_machine(const char *name)
>     return NULL;
>  }
>
> -static QEMUMachine *find_default_machine(void)
> +QEMUMachine *find_default_machine(void)
>  {
>     QEMUMachine *m;
>
> --
> 1.7.1
>
>
>

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

* Re: [Qemu-devel] [PATCH 3/4] loader: Add rom_add_file_buf for adding roms from a buffer
  2011-10-17 19:16 ` [Qemu-devel] [PATCH 3/4] loader: Add rom_add_file_buf for adding roms from a buffer Jordan Justen
@ 2011-10-18 18:05   ` Blue Swirl
  2011-10-18 21:17     ` Jordan Justen
  0 siblings, 1 reply; 9+ messages in thread
From: Blue Swirl @ 2011-10-18 18:05 UTC (permalink / raw)
  To: Jordan Justen; +Cc: qemu-devel

On Mon, Oct 17, 2011 at 7:16 PM, Jordan Justen
<jordan.l.justen@intel.com> wrote:
> rom_add_file_buf is similar to rom_add_file, except the rom's
> contents are provided in a buffer.
>
> rom_add_file is modified to call rom_add_file_buf after
> reading the rom's contents from the file.
>
> Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
> ---
>  hw/loader.c |   71 +++++++++++++++++++++++++++++++++++++++-------------------
>  hw/loader.h |    5 ++++
>  2 files changed, 53 insertions(+), 23 deletions(-)
>
> diff --git a/hw/loader.c b/hw/loader.c
> index 5676c18..d1a4a98 100644
> --- a/hw/loader.c
> +++ b/hw/loader.c
> @@ -557,11 +557,11 @@ static void rom_insert(Rom *rom)
>     QTAILQ_INSERT_TAIL(&roms, rom, next);
>  }
>
> -int rom_add_file(const char *file, const char *fw_dir,
> -                 target_phys_addr_t addr, int32_t bootindex)
> +int rom_add_file_buf(const char *file, const void *data, size_t size,
> +                     const char *fw_dir,
> +                     target_phys_addr_t addr, int32_t bootindex)
>  {
>     Rom *rom;
> -    int rc, fd = -1;
>     char devpath[100];
>
>     rom = g_malloc0(sizeof(*rom));
> @@ -571,28 +571,16 @@ int rom_add_file(const char *file, const char *fw_dir,
>         rom->path = g_strdup(file);
>     }
>
> -    fd = open(rom->path, O_RDONLY | O_BINARY);
> -    if (fd == -1) {
> -        fprintf(stderr, "Could not open option rom '%s': %s\n",
> -                rom->path, strerror(errno));
> -        goto err;
> -    }
> -
>     if (fw_dir) {
>         rom->fw_dir  = g_strdup(fw_dir);
>         rom->fw_file = g_strdup(file);
>     }
>     rom->addr    = addr;
> -    rom->romsize = lseek(fd, 0, SEEK_END);
> +    rom->romsize = size;
>     rom->data    = g_malloc0(rom->romsize);
> -    lseek(fd, 0, SEEK_SET);
> -    rc = read(fd, rom->data, rom->romsize);
> -    if (rc != rom->romsize) {
> -        fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
> -                rom->name, rc, rom->romsize);
> -        goto err;
> -    }
> -    close(fd);
> +
> +    memcpy(rom->data, data, rom->romsize);

This is not optimal, instead the data should be used directly. That
way also mmap()ed, deduplicated ROM files are possible.

> +
>     rom_insert(rom);
>     if (rom->fw_file && fw_cfg) {
>         const char *basename;
> @@ -614,14 +602,51 @@ int rom_add_file(const char *file, const char *fw_dir,
>
>     add_boot_device_path(bootindex, NULL, devpath);
>     return 0;
> +}
> +
> +int rom_add_file(const char *file, const char *fw_dir,
> +                 target_phys_addr_t addr, int32_t bootindex)
> +{
> +    char *filename;
> +    void *data = NULL;
> +    size_t size;
> +    int rc, fd = -1;
> +
> +    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, file);
> +    if (filename == NULL) {
> +        filename = g_strdup(file);
> +    }
> +
> +    fd = open(filename, O_RDONLY | O_BINARY);
> +    if (fd == -1) {
> +        fprintf(stderr, "Could not open option rom '%s': %s\n",
> +                filename, strerror(errno));
> +        goto err;
> +    }
> +
> +    size = lseek(fd, 0, SEEK_END);
> +    data = g_malloc0(size);
> +    lseek(fd, 0, SEEK_SET);
> +    rc = read(fd, data, size);

It should be easy to replace this with mmap(), maybe later.

> +    if (rc != size) {
> +        fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
> +                filename, rc, size);
> +        goto err;
> +    }
> +    close(fd);
> +
> +    rc = rom_add_file_buf(filename, data, size, fw_dir, addr, bootindex);
> +    if (rc != 0) {
> +        goto err;
> +    }
> +
> +    g_free(data);
> +    return 0;
>
>  err:
>     if (fd != -1)
>         close(fd);
> -    g_free(rom->data);
> -    g_free(rom->path);
> -    g_free(rom->name);
> -    g_free(rom);
> +    g_free(data);
>     return -1;
>  }
>
> diff --git a/hw/loader.h b/hw/loader.h
> index fc6bdff..9efe64a 100644
> --- a/hw/loader.h
> +++ b/hw/loader.h
> @@ -21,6 +21,9 @@ void pstrcpy_targphys(const char *name,
>                       const char *source);
>
>
> +int rom_add_file_buf(const char *file, const void *data, size_t size,
> +                     const char *fw_dir,
> +                     target_phys_addr_t addr, int32_t bootindex);
>  int rom_add_file(const char *file, const char *fw_dir,
>                  target_phys_addr_t addr, int32_t bootindex);
>  int rom_add_blob(const char *name, const void *blob, size_t len,
> @@ -31,6 +34,8 @@ int rom_copy(uint8_t *dest, target_phys_addr_t addr, size_t size);
>  void *rom_ptr(target_phys_addr_t addr);
>  void do_info_roms(Monitor *mon);
>
> +#define rom_add_file_buf_fixed(_f, _d, _s, _a, _i)          \
> +    rom_add_file_buf(_f, _d, _s, NULL, _a, _i)
>  #define rom_add_file_fixed(_f, _a, _i)          \
>     rom_add_file(_f, NULL, _a, _i)
>  #define rom_add_blob_fixed(_f, _b, _l, _a)      \
> --
> 1.7.1
>
>
>

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

* Re: [Qemu-devel] [PATCH 3/4] loader: Add rom_add_file_buf for adding roms from a buffer
  2011-10-18 18:05   ` Blue Swirl
@ 2011-10-18 21:17     ` Jordan Justen
  2011-10-23 11:27       ` Blue Swirl
  0 siblings, 1 reply; 9+ messages in thread
From: Jordan Justen @ 2011-10-18 21:17 UTC (permalink / raw)
  To: Blue Swirl; +Cc: Jordan Justen, qemu-devel

On Tue, Oct 18, 2011 at 11:05, Blue Swirl <blauwirbel@gmail.com> wrote:
> On Mon, Oct 17, 2011 at 7:16 PM, Jordan Justen
> <jordan.l.justen@intel.com> wrote:
>> rom_add_file_buf is similar to rom_add_file, except the rom's
>> contents are provided in a buffer.
>>
>> rom_add_file is modified to call rom_add_file_buf after
>> reading the rom's contents from the file.
>>
>> Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
>> ---
>>  hw/loader.c |   71 +++++++++++++++++++++++++++++++++++++++-------------------
>>  hw/loader.h |    5 ++++
>>  2 files changed, 53 insertions(+), 23 deletions(-)
>>
>> diff --git a/hw/loader.c b/hw/loader.c
>> index 5676c18..d1a4a98 100644
>> --- a/hw/loader.c
>> +++ b/hw/loader.c
>> @@ -557,11 +557,11 @@ static void rom_insert(Rom *rom)
>>     QTAILQ_INSERT_TAIL(&roms, rom, next);
>>  }
>>
>> -int rom_add_file(const char *file, const char *fw_dir,
>> -                 target_phys_addr_t addr, int32_t bootindex)
>> +int rom_add_file_buf(const char *file, const void *data, size_t size,
>> +                     const char *fw_dir,
>> +                     target_phys_addr_t addr, int32_t bootindex)
>>  {
>>     Rom *rom;
>> -    int rc, fd = -1;
>>     char devpath[100];
>>
>>     rom = g_malloc0(sizeof(*rom));
>> @@ -571,28 +571,16 @@ int rom_add_file(const char *file, const char *fw_dir,
>>         rom->path = g_strdup(file);
>>     }
>>
>> -    fd = open(rom->path, O_RDONLY | O_BINARY);
>> -    if (fd == -1) {
>> -        fprintf(stderr, "Could not open option rom '%s': %s\n",
>> -                rom->path, strerror(errno));
>> -        goto err;
>> -    }
>> -
>>     if (fw_dir) {
>>         rom->fw_dir  = g_strdup(fw_dir);
>>         rom->fw_file = g_strdup(file);
>>     }
>>     rom->addr    = addr;
>> -    rom->romsize = lseek(fd, 0, SEEK_END);
>> +    rom->romsize = size;
>>     rom->data    = g_malloc0(rom->romsize);
>> -    lseek(fd, 0, SEEK_SET);
>> -    rc = read(fd, rom->data, rom->romsize);
>> -    if (rc != rom->romsize) {
>> -        fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
>> -                rom->name, rc, rom->romsize);
>> -        goto err;
>> -    }
>> -    close(fd);
>> +
>> +    memcpy(rom->data, data, rom->romsize);
>
> This is not optimal, instead the data should be used directly. That
> way also mmap()ed, deduplicated ROM files are possible.

In my 4th patch I use a buffer from a memory region via
memory_region_get_ram_ptr.  Comments for memory_region_get_ram_ptr say
'Use with care'.

So, would the best thing be for me to allocate a new buffer in my 4th
patch, do the memcpy there, and then use the pointer directly here?

Thanks,

-Jordan

>
>> +
>>     rom_insert(rom);
>>     if (rom->fw_file && fw_cfg) {
>>         const char *basename;
>> @@ -614,14 +602,51 @@ int rom_add_file(const char *file, const char *fw_dir,
>>
>>     add_boot_device_path(bootindex, NULL, devpath);
>>     return 0;
>> +}
>> +
>> +int rom_add_file(const char *file, const char *fw_dir,
>> +                 target_phys_addr_t addr, int32_t bootindex)
>> +{
>> +    char *filename;
>> +    void *data = NULL;
>> +    size_t size;
>> +    int rc, fd = -1;
>> +
>> +    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, file);
>> +    if (filename == NULL) {
>> +        filename = g_strdup(file);
>> +    }
>> +
>> +    fd = open(filename, O_RDONLY | O_BINARY);
>> +    if (fd == -1) {
>> +        fprintf(stderr, "Could not open option rom '%s': %s\n",
>> +                filename, strerror(errno));
>> +        goto err;
>> +    }
>> +
>> +    size = lseek(fd, 0, SEEK_END);
>> +    data = g_malloc0(size);
>> +    lseek(fd, 0, SEEK_SET);
>> +    rc = read(fd, data, size);
>
> It should be easy to replace this with mmap(), maybe later.
>
>> +    if (rc != size) {
>> +        fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
>> +                filename, rc, size);
>> +        goto err;
>> +    }
>> +    close(fd);
>> +
>> +    rc = rom_add_file_buf(filename, data, size, fw_dir, addr, bootindex);
>> +    if (rc != 0) {
>> +        goto err;
>> +    }
>> +
>> +    g_free(data);
>> +    return 0;
>>
>>  err:
>>     if (fd != -1)
>>         close(fd);
>> -    g_free(rom->data);
>> -    g_free(rom->path);
>> -    g_free(rom->name);
>> -    g_free(rom);
>> +    g_free(data);
>>     return -1;
>>  }
>>
>> diff --git a/hw/loader.h b/hw/loader.h
>> index fc6bdff..9efe64a 100644
>> --- a/hw/loader.h
>> +++ b/hw/loader.h
>> @@ -21,6 +21,9 @@ void pstrcpy_targphys(const char *name,
>>                       const char *source);
>>
>>
>> +int rom_add_file_buf(const char *file, const void *data, size_t size,
>> +                     const char *fw_dir,
>> +                     target_phys_addr_t addr, int32_t bootindex);
>>  int rom_add_file(const char *file, const char *fw_dir,
>>                  target_phys_addr_t addr, int32_t bootindex);
>>  int rom_add_blob(const char *name, const void *blob, size_t len,
>> @@ -31,6 +34,8 @@ int rom_copy(uint8_t *dest, target_phys_addr_t addr, size_t size);
>>  void *rom_ptr(target_phys_addr_t addr);
>>  void do_info_roms(Monitor *mon);
>>
>> +#define rom_add_file_buf_fixed(_f, _d, _s, _a, _i)          \
>> +    rom_add_file_buf(_f, _d, _s, NULL, _a, _i)
>>  #define rom_add_file_fixed(_f, _a, _i)          \
>>     rom_add_file(_f, NULL, _a, _i)
>>  #define rom_add_blob_fixed(_f, _b, _l, _a)      \
>> --
>> 1.7.1
>>
>>
>>
>
>

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

* Re: [Qemu-devel] [PATCH 3/4] loader: Add rom_add_file_buf for adding roms from a buffer
  2011-10-18 21:17     ` Jordan Justen
@ 2011-10-23 11:27       ` Blue Swirl
  2011-10-24 22:19         ` Jordan Justen
  0 siblings, 1 reply; 9+ messages in thread
From: Blue Swirl @ 2011-10-23 11:27 UTC (permalink / raw)
  To: Jordan Justen; +Cc: Jordan Justen, qemu-devel

On Tue, Oct 18, 2011 at 21:17, Jordan Justen <jljusten@gmail.com> wrote:
> On Tue, Oct 18, 2011 at 11:05, Blue Swirl <blauwirbel@gmail.com> wrote:
>> On Mon, Oct 17, 2011 at 7:16 PM, Jordan Justen
>> <jordan.l.justen@intel.com> wrote:
>>> rom_add_file_buf is similar to rom_add_file, except the rom's
>>> contents are provided in a buffer.
>>>
>>> rom_add_file is modified to call rom_add_file_buf after
>>> reading the rom's contents from the file.
>>>
>>> Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
>>> ---
>>>  hw/loader.c |   71 +++++++++++++++++++++++++++++++++++++++-------------------
>>>  hw/loader.h |    5 ++++
>>>  2 files changed, 53 insertions(+), 23 deletions(-)
>>>
>>> diff --git a/hw/loader.c b/hw/loader.c
>>> index 5676c18..d1a4a98 100644
>>> --- a/hw/loader.c
>>> +++ b/hw/loader.c
>>> @@ -557,11 +557,11 @@ static void rom_insert(Rom *rom)
>>>     QTAILQ_INSERT_TAIL(&roms, rom, next);
>>>  }
>>>
>>> -int rom_add_file(const char *file, const char *fw_dir,
>>> -                 target_phys_addr_t addr, int32_t bootindex)
>>> +int rom_add_file_buf(const char *file, const void *data, size_t size,
>>> +                     const char *fw_dir,
>>> +                     target_phys_addr_t addr, int32_t bootindex)
>>>  {
>>>     Rom *rom;
>>> -    int rc, fd = -1;
>>>     char devpath[100];
>>>
>>>     rom = g_malloc0(sizeof(*rom));
>>> @@ -571,28 +571,16 @@ int rom_add_file(const char *file, const char *fw_dir,
>>>         rom->path = g_strdup(file);
>>>     }
>>>
>>> -    fd = open(rom->path, O_RDONLY | O_BINARY);
>>> -    if (fd == -1) {
>>> -        fprintf(stderr, "Could not open option rom '%s': %s\n",
>>> -                rom->path, strerror(errno));
>>> -        goto err;
>>> -    }
>>> -
>>>     if (fw_dir) {
>>>         rom->fw_dir  = g_strdup(fw_dir);
>>>         rom->fw_file = g_strdup(file);
>>>     }
>>>     rom->addr    = addr;
>>> -    rom->romsize = lseek(fd, 0, SEEK_END);
>>> +    rom->romsize = size;
>>>     rom->data    = g_malloc0(rom->romsize);
>>> -    lseek(fd, 0, SEEK_SET);
>>> -    rc = read(fd, rom->data, rom->romsize);
>>> -    if (rc != rom->romsize) {
>>> -        fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
>>> -                rom->name, rc, rom->romsize);
>>> -        goto err;
>>> -    }
>>> -    close(fd);
>>> +
>>> +    memcpy(rom->data, data, rom->romsize);
>>
>> This is not optimal, instead the data should be used directly. That
>> way also mmap()ed, deduplicated ROM files are possible.
>
> In my 4th patch I use a buffer from a memory region via
> memory_region_get_ram_ptr.  Comments for memory_region_get_ram_ptr say
> 'Use with care'.
>
> So, would the best thing be for me to allocate a new buffer in my 4th
> patch, do the memcpy there, and then use the pointer directly here?

No, instead of memcpy just do
rom->data = data;

Then also the corresponding g_free(data) below should be removed.

The line g_free(rom->data) in the error path would be a problem for
the future mmap() case though. Should be solvable with with some
refactoring then, we'd need to be able to munmap() anyway.

> Thanks,
>
> -Jordan
>
>>
>>> +
>>>     rom_insert(rom);
>>>     if (rom->fw_file && fw_cfg) {
>>>         const char *basename;
>>> @@ -614,14 +602,51 @@ int rom_add_file(const char *file, const char *fw_dir,
>>>
>>>     add_boot_device_path(bootindex, NULL, devpath);
>>>     return 0;
>>> +}
>>> +
>>> +int rom_add_file(const char *file, const char *fw_dir,
>>> +                 target_phys_addr_t addr, int32_t bootindex)
>>> +{
>>> +    char *filename;
>>> +    void *data = NULL;
>>> +    size_t size;
>>> +    int rc, fd = -1;
>>> +
>>> +    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, file);
>>> +    if (filename == NULL) {
>>> +        filename = g_strdup(file);
>>> +    }
>>> +
>>> +    fd = open(filename, O_RDONLY | O_BINARY);
>>> +    if (fd == -1) {
>>> +        fprintf(stderr, "Could not open option rom '%s': %s\n",
>>> +                filename, strerror(errno));
>>> +        goto err;
>>> +    }
>>> +
>>> +    size = lseek(fd, 0, SEEK_END);
>>> +    data = g_malloc0(size);
>>> +    lseek(fd, 0, SEEK_SET);
>>> +    rc = read(fd, data, size);
>>
>> It should be easy to replace this with mmap(), maybe later.
>>
>>> +    if (rc != size) {
>>> +        fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
>>> +                filename, rc, size);
>>> +        goto err;
>>> +    }
>>> +    close(fd);
>>> +
>>> +    rc = rom_add_file_buf(filename, data, size, fw_dir, addr, bootindex);
>>> +    if (rc != 0) {
>>> +        goto err;
>>> +    }
>>> +
>>> +    g_free(data);
>>> +    return 0;
>>>
>>>  err:
>>>     if (fd != -1)
>>>         close(fd);
>>> -    g_free(rom->data);
>>> -    g_free(rom->path);
>>> -    g_free(rom->name);
>>> -    g_free(rom);
>>> +    g_free(data);
>>>     return -1;
>>>  }
>>>
>>> diff --git a/hw/loader.h b/hw/loader.h
>>> index fc6bdff..9efe64a 100644
>>> --- a/hw/loader.h
>>> +++ b/hw/loader.h
>>> @@ -21,6 +21,9 @@ void pstrcpy_targphys(const char *name,
>>>                       const char *source);
>>>
>>>
>>> +int rom_add_file_buf(const char *file, const void *data, size_t size,
>>> +                     const char *fw_dir,
>>> +                     target_phys_addr_t addr, int32_t bootindex);
>>>  int rom_add_file(const char *file, const char *fw_dir,
>>>                  target_phys_addr_t addr, int32_t bootindex);
>>>  int rom_add_blob(const char *name, const void *blob, size_t len,
>>> @@ -31,6 +34,8 @@ int rom_copy(uint8_t *dest, target_phys_addr_t addr, size_t size);
>>>  void *rom_ptr(target_phys_addr_t addr);
>>>  void do_info_roms(Monitor *mon);
>>>
>>> +#define rom_add_file_buf_fixed(_f, _d, _s, _a, _i)          \
>>> +    rom_add_file_buf(_f, _d, _s, NULL, _a, _i)
>>>  #define rom_add_file_fixed(_f, _a, _i)          \
>>>     rom_add_file(_f, NULL, _a, _i)
>>>  #define rom_add_blob_fixed(_f, _b, _l, _a)      \
>>> --
>>> 1.7.1
>>>
>>>
>>>
>>
>>
>

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

* Re: [Qemu-devel] [PATCH 3/4] loader: Add rom_add_file_buf for adding roms from a buffer
  2011-10-23 11:27       ` Blue Swirl
@ 2011-10-24 22:19         ` Jordan Justen
  0 siblings, 0 replies; 9+ messages in thread
From: Jordan Justen @ 2011-10-24 22:19 UTC (permalink / raw)
  To: Blue Swirl; +Cc: Jordan Justen, qemu-devel, Alexander Graf

On Sun, Oct 23, 2011 at 04:27, Blue Swirl <blauwirbel@gmail.com> wrote:
> On Tue, Oct 18, 2011 at 21:17, Jordan Justen <jljusten@gmail.com> wrote:
>> On Tue, Oct 18, 2011 at 11:05, Blue Swirl <blauwirbel@gmail.com> wrote:
>>> On Mon, Oct 17, 2011 at 7:16 PM, Jordan Justen
>>> <jordan.l.justen@intel.com> wrote:
>>>> rom_add_file_buf is similar to rom_add_file, except the rom's
>>>> contents are provided in a buffer.
>>>>
>>>> rom_add_file is modified to call rom_add_file_buf after
>>>> reading the rom's contents from the file.
>>>>
>>>> Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
>>>> ---
>>>>  hw/loader.c |   71 +++++++++++++++++++++++++++++++++++++++-------------------
>>>>  hw/loader.h |    5 ++++
>>>>  2 files changed, 53 insertions(+), 23 deletions(-)
>>>>
>>>> diff --git a/hw/loader.c b/hw/loader.c
>>>> index 5676c18..d1a4a98 100644
>>>> --- a/hw/loader.c
>>>> +++ b/hw/loader.c
>>>> @@ -557,11 +557,11 @@ static void rom_insert(Rom *rom)
>>>>     QTAILQ_INSERT_TAIL(&roms, rom, next);
>>>>  }
>>>>
>>>> -int rom_add_file(const char *file, const char *fw_dir,
>>>> -                 target_phys_addr_t addr, int32_t bootindex)
>>>> +int rom_add_file_buf(const char *file, const void *data, size_t size,
>>>> +                     const char *fw_dir,
>>>> +                     target_phys_addr_t addr, int32_t bootindex)
>>>>  {
>>>>     Rom *rom;
>>>> -    int rc, fd = -1;
>>>>     char devpath[100];
>>>>
>>>>     rom = g_malloc0(sizeof(*rom));
>>>> @@ -571,28 +571,16 @@ int rom_add_file(const char *file, const char *fw_dir,
>>>>         rom->path = g_strdup(file);
>>>>     }
>>>>
>>>> -    fd = open(rom->path, O_RDONLY | O_BINARY);
>>>> -    if (fd == -1) {
>>>> -        fprintf(stderr, "Could not open option rom '%s': %s\n",
>>>> -                rom->path, strerror(errno));
>>>> -        goto err;
>>>> -    }
>>>> -
>>>>     if (fw_dir) {
>>>>         rom->fw_dir  = g_strdup(fw_dir);
>>>>         rom->fw_file = g_strdup(file);
>>>>     }
>>>>     rom->addr    = addr;
>>>> -    rom->romsize = lseek(fd, 0, SEEK_END);
>>>> +    rom->romsize = size;
>>>>     rom->data    = g_malloc0(rom->romsize);
>>>> -    lseek(fd, 0, SEEK_SET);
>>>> -    rc = read(fd, rom->data, rom->romsize);
>>>> -    if (rc != rom->romsize) {
>>>> -        fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
>>>> -                rom->name, rc, rom->romsize);
>>>> -        goto err;
>>>> -    }
>>>> -    close(fd);
>>>> +
>>>> +    memcpy(rom->data, data, rom->romsize);
>>>
>>> This is not optimal, instead the data should be used directly. That
>>> way also mmap()ed, deduplicated ROM files are possible.
>>
>> In my 4th patch I use a buffer from a memory region via
>> memory_region_get_ram_ptr.  Comments for memory_region_get_ram_ptr say
>> 'Use with care'.
>>
>> So, would the best thing be for me to allocate a new buffer in my 4th
>> patch, do the memcpy there, and then use the pointer directly here?
>
> No, instead of memcpy just do
> rom->data = data;
>
> Then also the corresponding g_free(data) below should be removed.
>
> The line g_free(rom->data) in the error path would be a problem for
> the future mmap() case though. Should be solvable with with some
> refactoring then, we'd need to be able to munmap() anyway.

I was discussing this change with Alex, and his opinion was that I
should not need to add the rom_add_file_buf function because the
pflash device is being used.  So, I plan to drop patches 3 & 4 from
this changeset.

Thanks for the suggestion though, and I'll keep it in mind for future changes.

-Jordan

>>>
>>>> +
>>>>     rom_insert(rom);
>>>>     if (rom->fw_file && fw_cfg) {
>>>>         const char *basename;
>>>> @@ -614,14 +602,51 @@ int rom_add_file(const char *file, const char *fw_dir,
>>>>
>>>>     add_boot_device_path(bootindex, NULL, devpath);
>>>>     return 0;
>>>> +}
>>>> +
>>>> +int rom_add_file(const char *file, const char *fw_dir,
>>>> +                 target_phys_addr_t addr, int32_t bootindex)
>>>> +{
>>>> +    char *filename;
>>>> +    void *data = NULL;
>>>> +    size_t size;
>>>> +    int rc, fd = -1;
>>>> +
>>>> +    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, file);
>>>> +    if (filename == NULL) {
>>>> +        filename = g_strdup(file);
>>>> +    }
>>>> +
>>>> +    fd = open(filename, O_RDONLY | O_BINARY);
>>>> +    if (fd == -1) {
>>>> +        fprintf(stderr, "Could not open option rom '%s': %s\n",
>>>> +                filename, strerror(errno));
>>>> +        goto err;
>>>> +    }
>>>> +
>>>> +    size = lseek(fd, 0, SEEK_END);
>>>> +    data = g_malloc0(size);
>>>> +    lseek(fd, 0, SEEK_SET);
>>>> +    rc = read(fd, data, size);
>>>
>>> It should be easy to replace this with mmap(), maybe later.
>>>
>>>> +    if (rc != size) {
>>>> +        fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
>>>> +                filename, rc, size);
>>>> +        goto err;
>>>> +    }
>>>> +    close(fd);
>>>> +
>>>> +    rc = rom_add_file_buf(filename, data, size, fw_dir, addr, bootindex);
>>>> +    if (rc != 0) {
>>>> +        goto err;
>>>> +    }
>>>> +
>>>> +    g_free(data);
>>>> +    return 0;
>>>>
>>>>  err:
>>>>     if (fd != -1)
>>>>         close(fd);
>>>> -    g_free(rom->data);
>>>> -    g_free(rom->path);
>>>> -    g_free(rom->name);
>>>> -    g_free(rom);
>>>> +    g_free(data);
>>>>     return -1;
>>>>  }
>>>>
>>>> diff --git a/hw/loader.h b/hw/loader.h
>>>> index fc6bdff..9efe64a 100644
>>>> --- a/hw/loader.h
>>>> +++ b/hw/loader.h
>>>> @@ -21,6 +21,9 @@ void pstrcpy_targphys(const char *name,
>>>>                       const char *source);
>>>>
>>>>
>>>> +int rom_add_file_buf(const char *file, const void *data, size_t size,
>>>> +                     const char *fw_dir,
>>>> +                     target_phys_addr_t addr, int32_t bootindex);
>>>>  int rom_add_file(const char *file, const char *fw_dir,
>>>>                  target_phys_addr_t addr, int32_t bootindex);
>>>>  int rom_add_blob(const char *name, const void *blob, size_t len,
>>>> @@ -31,6 +34,8 @@ int rom_copy(uint8_t *dest, target_phys_addr_t addr, size_t size);
>>>>  void *rom_ptr(target_phys_addr_t addr);
>>>>  void do_info_roms(Monitor *mon);
>>>>
>>>> +#define rom_add_file_buf_fixed(_f, _d, _s, _a, _i)          \
>>>> +    rom_add_file_buf(_f, _d, _s, NULL, _a, _i)
>>>>  #define rom_add_file_fixed(_f, _a, _i)          \
>>>>     rom_add_file(_f, NULL, _a, _i)
>>>>  #define rom_add_blob_fixed(_f, _b, _l, _a)      \
>>>> --
>>>> 1.7.1
>>>>
>>>>
>>>>
>>>
>>>
>>
>

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

end of thread, other threads:[~2011-10-24 22:19 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-17 19:16 [Qemu-devel] [PATCH 1/4] pflash: Support read-only mode Jordan Justen
2011-10-17 19:16 ` [Qemu-devel] [PATCH 2/4] pc: Support system flash memory with pflash Jordan Justen
2011-10-17 19:27   ` Jordan Justen
2011-10-17 19:16 ` [Qemu-devel] [PATCH 3/4] loader: Add rom_add_file_buf for adding roms from a buffer Jordan Justen
2011-10-18 18:05   ` Blue Swirl
2011-10-18 21:17     ` Jordan Justen
2011-10-23 11:27       ` Blue Swirl
2011-10-24 22:19         ` Jordan Justen
2011-10-17 19:16 ` [Qemu-devel] [PATCH 4/4] pcflash: Add pc flash to qemu roms Jordan Justen

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