Skip to contentSkip to conversation

PC system flash support

PATCH Superseded Jordan Justen 3 participants 7 messages Nov 3, 2011 – Nov 7, 2011 7 messages not in the archive Kbuild

v7 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v7 cover PC system flash support

Enable flash emulation in a PC system using pflash_cfi01.

v7:
* Do not add system firmware to qemu roms
* If kvm is enabled, copy pflash drive contents into a read-only ram region, since kvm cannot currently execute code from a pflash device.
* Rename pcflash.c to pc_sysfw.c

v6:
* Rebase for memory API
* pflash_cfi01: Set error in status register when a write or erase is attempted in read-only mode.
* Add system firmware to qemu roms

v5:
* Enable pflash read-only mode
* Enable -drive with if=pflash to define system firmware image

v4:
* Rebase

v3:
* Fix code style issues
* Add additional comments

v2:
* Convert debug printf to DPRINTF

Jordan Justen (2):
pflash: Support read-only mode
pc: Support system flash memory with pflash

 Makefile.target                    |    1 +
 blockdev.c                         |    3 +-
 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/pc_sysfw.c                      |  196 ++++++++++++++++++++++++++++++++++++
 hw/pflash_cfi01.c                  |   44 ++++++---
 hw/pflash_cfi02.c                  |   83 ++++++++-------
 vl.c                               |    2 +-
 11 files changed, 286 insertions(+), 104 deletions(-)
 create mode 100644 hw/pc_sysfw.c
#1 lore raw

PATCH v7 1/2 pflash: Support read-only mode

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.

For pflash_cfi02 (AMD), if the flash is in read-only mode then the pflash will silently ignore all write/erase commands.

---
 blockdev.c        |    3 +-
 hw/pflash_cfi01.c |   44 +++++++++++++++++++---------
 hw/pflash_cfi02.c |   83 ++++++++++++++++++++++++++++------------------------
 3 files changed, 77 insertions(+), 53 deletions(-)
blockdev.c +2 -1
index 0827bf7..c94aee5 100644
@@ -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;
         }
hw/pflash_cfi01.c +30 -14

74 lines not on this page · raw message

hw/pflash_cfi02.c +45 -38

113 lines not on this page · raw message

Signature · 1 line
1.7.1
#2 lore raw

PATCH v7 2/2 pc: Support system flash memory with pflash

In reply to Jordan Justen

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.

KVM cannot execute from a pflash region currently. Therefore, when KVM is enabled, a (read-only) ram memory region is created and filled with the contents of the pflash drive.

Cc Anthony Liguori <[email protected]>
---
 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/pc_sysfw.c                      |  196 ++++++++++++++++++++++++++++++++++++
 vl.c                               |    2 +-
 8 files changed, 209 insertions(+), 51 deletions(-)
 create mode 100644 hw/pc_sysfw.c
Makefile.target +1 -0
index 0c86bc5..8adda6c 100644
@@ -232,6 +232,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 += pc_sysfw.o
 obj-i386-$(CONFIG_KVM) += kvmclock.o
 obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o
 
default-configs/i386-softmmu.mak +1 -0
index e67ebb3..cd407a9 100644
@@ -22,3 +22,4 @@ CONFIG_SOUND=y
 CONFIG_HPET=y
 CONFIG_APPLESMC=y
 CONFIG_I8259=y
+CONFIG_PFLASH_CFI01=y
default-configs/x86_64-softmmu.mak +1 -0
index b75757e..47734ea 100644
@@ -22,3 +22,4 @@ CONFIG_SOUND=y
 CONFIG_HPET=y
 CONFIG_APPLESMC=y
 CONFIG_I8259=y
+CONFIG_PFLASH_CFI01=y
hw/boards.h +1 -0
index 716fd7b..45a31a1 100644
@@ -33,6 +33,7 @@ typedef struct QEMUMachine {
 } QEMUMachine;
 
 int qemu_register_machine(QEMUMachine *m);
+QEMUMachine *find_default_machine(void);
 
 extern QEMUMachine *current_machine;
 
hw/pc.c +5 -50

80 lines not on this page · raw message

hw/pc.h +3 -0
index 4515006..b98d734 100644
@@ -244,6 +244,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
hw/pc_sysfw.c new file +196 -0

196 lines not on this page · raw message

vl.c +1 -1
index 624da0f..8a6032e 100644
@@ -1183,7 +1183,7 @@ static QEMUMachine *find_machine(const char *name)
     return NULL;
 }
 
-static QEMUMachine *find_default_machine(void)
+QEMUMachine *find_default_machine(void)
 {
     QEMUMachine *m;
 
Signature · 1 line
1.7.1
#3 lore raw

Re: [Qemu-devel] [PATCH v7 2/2] pc: Support system flash memory with pflash

Quoted 317 lines · Jordan Justen

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.

KVM cannot execute from a pflash region currently. Therefore, when KVM is enabled, a (read-only) ram memory region is created and filled with the contents of the pflash drive.

Cc Anthony Liguori <[email protected]>

---
 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/pc_sysfw.c                      |  196 ++++++++++++++++++++++++++++++++++++ vl.c                               |    2 +-

 8 files changed, 209 insertions(+), 51 deletions(-)

 create mode 100644 hw/pc_sysfw.c

Makefile.target +0 -0
index 0c86bc5..8adda6c 100644
@@ -232,6 +232,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 += pc_sysfw.o
 obj-i386-$(CONFIG_KVM) += kvmclock.o obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o

default-configs/i386-softmmu.mak +0 -0
index e67ebb3..cd407a9 100644
@@ -22,3 +22,4 @@ CONFIG_SOUND=y

 CONFIG_HPET=y
 CONFIG_APPLESMC=y
 CONFIG_I8259=y

default-configs/i386-softmmu.mak +1 -0
+CONFIG_PFLASH_CFI01=y
default-configs/x86_64-softmmu.mak +0 -0
index b75757e..47734ea 100644
@@ -22,3 +22,4 @@ CONFIG_SOUND=y

 CONFIG_HPET=y
 CONFIG_APPLESMC=y
 CONFIG_I8259=y

default-configs/x86_64-softmmu.mak +1 -0
+CONFIG_PFLASH_CFI01=y
hw/boards.h +1 -0
index 716fd7b..45a31a1 100644
@@ -33,6 +33,7 @@ typedef struct QEMUMachine {
  } QEMUMachine;
 
  int qemu_register_machine(QEMUMachine *m);
+QEMUMachine *find_default_machine(void);
 
  extern QEMUMachine *current_machine;
 
hw/pc.c +5 -50
index eb4c2d8..ce97eb7 100644
@@ -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
@@ -976,11 +972,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);
@@ -1005,43 +999,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);
@@ -1050,11 +1010,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);
 
hw/pc.h +3 -0
index 4515006..b98d734 100644
@@ -244,6 +244,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
hw/pc_sysfw.c new file +142 -0
new file mode 100644
index 0000000..21b35c6
@@ -0,0 +1,196 @@
+/*
+ * QEMU PC System Firmware
+ *
+ * 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"
+#include "kvm.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_fw_add_pflash_drv(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);
+}
+
+static void pc_system_rom_init(MemoryRegion *rom_memory,
+                               DriveInfo *pflash_drv)
+{
+    BlockDriverState *bdrv;
+    int64_t size;
+    target_phys_addr_t phys_addr;
+    int sector_bits, sector_size;
+    MemoryRegion *sys_rom;
+    void *buffer;
+    int ret;
+
+    bdrv = pflash_drv->bdrv;
+    size = bdrv_getlength(pflash_drv->bdrv);
+    sector_bits = 9;
+    sector_size = 1 << sector_bits;
+
+    if ((size % sector_size) != 0) {
+        fprintf(stderr,
+                "qemu: PC system rom (pflash) must be a multiple of 0x%x\n",

hw/pc_sysfw.c line 152

Quoted 10 lines
hw/pc_sysfw.c new file +10 -0
+                sector_size);
+        exit(1);
+    }
+
+    phys_addr = 0x100000000ULL - size;
+    sys_rom = g_malloc(sizeof(*sys_rom));
+    memory_region_init_ram(sys_rom, NULL, "system.rom", size);
+    buffer = memory_region_get_ram_ptr(sys_rom);
+    memory_region_add_subregion(rom_memory, phys_addr, sys_rom);
+    printf("rom: added from pflash drive @ 0x%lx\n", phys_addr);

Whoops. I will remove this debug print in v8. I wait about a week to see if any other feedback comes in.

hw/pc_sysfw.c line 163

Quoted 11 lines
hw/pc_sysfw.c new file +11 -0
+
+    /* read the rom content */
+    ret = bdrv_read(bdrv, 0, buffer, size >> sector_bits);
+    if (ret < 0) {
+        memory_region_destroy(sys_rom);
+        g_free(sys_rom);
+        fprintf(stderr,
+                "qemu: Failed to read rom image from pflash drive\n");
+        exit(1);
+    }
+    printf("rom: read from pflash drive\n");

Same here.

-Jordan

Quoted 51 lines
hw/pc_sysfw.c new file +33 -0
+
+    memory_region_set_readonly(sys_rom, true);
+
+    pc_isa_bios_init(rom_memory, sys_rom, 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_fw_add_pflash_drv();
+        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);
+    }
+
+    if (!kvm_enabled()) {
+        pc_system_flash_init(rom_memory, pflash_drv);
+    } else {
+        pc_system_rom_init(rom_memory, pflash_drv);
+    }
+}
+
+
vl.c +1 -2
index 624da0f..8a6032e 100644
@@ -1183,7 +1183,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

A message that is not in the archive (sent off-list or not yet received): CAEH94Lhf-r4HGHq=vVLERauuR=yGNf-v8AnB2heyYy8wyZozoQ@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): 1320338977.4420.648.camel@jljusten-desk2

A message that is not in the archive (sent off-list or not yet received): CAEH94LjpkGeBAf8jB3_npuXGGQ9hqPRofGKT5F_aZWsQH77+-A@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): CAFe8ug_PpsA6_b0f=2-dSKNSwM_hB6CNp7YBp2yKiEQk4vqgjw@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): CAEH94LiTErxTqGAh9NtMcDkJZHX6k1+3mtssndUXp3_Nxc98mg@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): CAFe8ug_Gf=Xp=uCQhHxWvw1HHUtMVTzd8F+zX4Sv6UjTVLkmgg@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): CAEH94LhVab=CPuTG_Ydkxj+DLLSm3Njci_gBeFZWKyFN8caa9A@mail.gmail.com

#11 lore raw
Cc qemu-devel
Quoted 7 lines · Zhi Yong Wu
Quoted 6 lines · Jordan Justen
Quoted 1 line · Zhi Yong Wu

BTW: what is the relationship between -bios and -pflash?

Previously the QEMU/KVM PC system had no pflash support.  You could specify a pflash drive, but it would not appear in the VM.

Quoted 6 lines
Quoted 5 lines

Also, previously the -bios flag would cause the specified binary to be loaded by QEMU, and then it was put into a memory region to be used for the firmware.

After this change, the pflash drive is used for firmware.  So, it

This pflash drive is created with -pflash option, right?

-pflash can be used, but I think it is just a short cut.

-pflash /path/to/file
is similar to:
-drive if=pflash,file=/path/to/file

And, after this change,
-bios /path/to/file
is similar to:
-drive if=pflash,file=/path/to/file,readonly=on

Quoted 2 lines
Quoted 2 lines

should be able to support qcow2, since qemu drives support the different formats.

I confirmed that you can use a qcow2 image with -bios after this change.

Quoted 5 lines
Quoted 3 lines

For backward compatibility, this change makes the -bios parameter be another way to specify the pflash drive.  If -bios is used, then a read-only pflash drive is created.

Sorry, i don't fully understand this. How about -bios and -pflash are both specified.

In this case the -pflash will be used, and -bios will be ignored. I use -bios to create a pflash drive only if no pflash drive exists already.

I think this should be okay, since -pflash has no use in the QEMU PC system before this patch, so it is unlikely that both -pflash and -bios will be used previously.

Quoted 3 lines
Quoted 1 line
Quoted 1 line

If your patch is applied to my source tree, can you know how to use -pflash?

Sorry, i made a mistake. I wanted to ask you how to use -pflash if your patch is not applied.

I don't think you can use pflash in a PC system when my patch is not applied. I think without the patch qemu will create the pflash drive internally, but it will not be hooked to a device that the emulated PC system can see.

I attached a log of me interacting with the pflash device under the EFI shell on OVMF *with* the patch applied.

Thanks,

-Jordan

Notes below refer to quoted item just above.

Quoted 11 lines

Device mapping table
blk0 :Floppy - Alias (null)

        PciRoot(0x0)/Pci(0x1,0x0)/Floppy(0x0)
  blk1 :Floppy - Alias (null)
        PciRoot(0x0)/Pci(0x1,0x0)/Floppy(0x1)
  blk2 :BlockDevice - Alias (null)
        PciRoot(0x0)/Pci(0x1,0x1)/Ata(Secondary,Master,0x0)

Press ESC in 5 seconds to skip startup.nsh, any other key to continue. Press ESC in 4 seconds to skip startup.nsh, any other key to continue.

EFI shell started

Quoted 4 lines

Shell> mem fffe0 32
Memory Address 00000000000FFFE0 20 Bytes
000FFFE0: EB C3 90 90 90 90 90 90-00 00 00 00 56 54 46 00 *............VTF.* 000FFFF0: 90 90 EB AC 90 90 90 90-90 90 90 90 90 90 90 90 *................*

Dump firmware range in legacy F000 segment

Quoted 4 lines

Shell> mem ffffffe0 32
Memory Address 00000000FFFFFFE0 20 Bytes
FFFFFFE0: EB C3 90 90 90 90 90 90-00 00 00 00 56 54 46 00 *............VTF.*
FFFFFFF0: 90 90 EB AC 90 90 90 90-90 90 90 90 90 90 90 90 *................*

Dump firmware range at top of 4GB.

Quoted 4 lines

Shell> mem fff00000 32
Memory Address 00000000FFF00000 20 Bytes
FFF00000: 01 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 *................*
FFF00010: 78 E5 8C 8C 3D 8A 1C 4F-99 35 89 61 85 C3 2D D3 *x...=..O.5.a..-.*

Dump start of OVMF flash image. The first 16 bytes are reserved, and therefore we can program them without a negative side effect. Notice I have previously programmed the first byte from 00 to 01.

Quoted 2 lines

Shell> mm -mmio -w 1 -n fff00000
MMIO 0x00000000FFF00000 : 0x01

Read the data in the first byte of the rom.

Quoted 1 line

Shell> mm -mmio -w 1 -n fff00000 70

Request to read the pflash_cfi01 status register. (0x70 is the command written to address 0xfff00000.)

Quoted 2 lines

Shell> mm -mmio -w 1 -n fff00000
MMIO 0x00000000FFF00000 : 0x00

Read the status register by reading address 0xfff00000.

Quoted 1 line

Shell> mm -mmio -w 1 -n fff00000 ff

Put the pflash_cfi01 back into read-data mode. (0xff is the command for putting the flash into read mode.)

Quoted 2 lines

Shell> mm -mmio -w 1 -n fff00000
MMIO 0x00000000FFF00000 : 0x01

Notice we read the proper data again... 0x01.

Quoted 2 lines

Shell> mm -mmio -w 1 -n fff00001
MMIO 0x00000000FFF00001 : 0x00

Now read the data at 0xfff00001. It is 0x00.

Quoted 1 line

Shell> mm -mmio -w 1 -n fff00001 10

Put the pflash_cfi01 into programming mode.

Quoted 1 line

Shell> mm -mmio -w 1 -n fff00001 ab

Program the data at 0xfff00001. Data is 0xab.

Quoted 1 line

Shell> mm -mmio -w 1 -n fff00001 ff

Put the pflash_cfi01 back into read-data mode.

Quoted 2 lines

Shell> mm -mmio -w 1 -n fff00001
MMIO 0x00000000FFF00001 : 0xAB

0xab was programmed into 0xfff00001.

Quoted 1 line

Shell> mm -mmio -w 1 -n fff00001 70

Request to read the status register.

Quoted 2 lines

Shell> mm -mmio -w 1 -n fff00001
MMIO 0x00000000FFF00001 : 0x80

bit7 (0x80) indicates device is ready.

Quoted 1 line

Shell> mm -mmio -w 1 -n fff00001 ff

Back to read-data mode again.

Quoted 2 lines

Shell> mm -mmio -w 1 -n fff00001
MMIO 0x00000000FFF00001 : 0xAB

The proper data is still returned.

Quoted 1 line

Shell> reset -s

Shutdown the system...

#12 lore raw
Quoted 51 lines · Jordan Justen
Cc qemu-devel
Quoted 13 lines · Zhi Yong Wu
Quoted 11 lines · Jordan Justen
Quoted 1 line · Zhi Yong Wu

BTW: what is the relationship between -bios and -pflash?

Previously the QEMU/KVM PC system had no pflash support.  You could specify a pflash drive, but it would not appear in the VM.

Also, previously the -bios flag would cause the specified binary to be loaded by QEMU, and then it was put into a memory region to be used for the firmware.

After this change, the pflash drive is used for firmware.  So, it

This pflash drive is created with -pflash option, right?

(quoted diff) +0 -2
-pflash can be used, but I think it is just a short cut.
 
-pflash /path/to/file

is similar to:
-drive if=pflash,file=/path/to/file

And, after this change,
-bios /path/to/file
is similar to:
-drive if=pflash,file=/path/to/file,readonly=on

Quoted 2 lines
Quoted 2 lines

should be able to support qcow2, since qemu drives support the different formats.

I confirmed that you can use a qcow2 image with -bios after this change.

Quoted 5 lines
Quoted 3 lines

For backward compatibility, this change makes the -bios parameter be another way to specify the pflash drive.  If -bios is used, then a read-only pflash drive is created.

Sorry, i don't fully understand this. How about -bios and -pflash are both specified.

In this case the -pflash will be used, and -bios will be ignored.  I use -bios to create a pflash drive only if no pflash drive exists already.

I think this should be okay, since -pflash has no use in the QEMU PC system before this patch, so it is unlikely that both -pflash and -bios will be used previously.

Quoted 3 lines
Quoted 1 line
Quoted 1 line

If your patch is applied to my source tree, can you know how to use -pflash?

Sorry, i made a mistake. I wanted to ask you how to use -pflash if your patch is not applied.

Quoted 8 lines

I don't think you can use pflash in a PC system when my patch is not applied.  I think without the patch qemu will create the pflash drive internally, but it will not be hooked to a device that the emulated PC system can see.

I attached a log of me interacting with the pflash device under the EFI shell on OVMF *with* the patch applied.

Great, thanks a lot. Let me try next week.

Quoted 4 lines

Thanks,

-Jordan

Signature · 3 lines
Regards,

Zhi Yong Wu
#13 lore raw
In reply to Jordan Justen

Reviewed-by and Tested-by: Zhi Yong Wu <[email protected]>

Quoted 49 lines · Jordan Justen

Enable flash emulation in a PC system using pflash_cfi01.

v7:
* Do not add system firmware to qemu roms
* If kvm is enabled, copy pflash drive contents into a read-only ram region, since kvm cannot currently execute code from a pflash device.
* Rename pcflash.c to pc_sysfw.c

v6:
* Rebase for memory API
* pflash_cfi01: Set error in status register when a write or erase is attempted in read-only mode.
* Add system firmware to qemu roms

v5:
* Enable pflash read-only mode
* Enable -drive with if=pflash to define system firmware image

v4:
* Rebase

v3:
* Fix code style issues
* Add additional comments

v2:
* Convert debug printf to DPRINTF

Jordan Justen (2):
 pflash: Support read-only mode
 pc: Support system flash memory with pflash

 Makefile.target                    |    1 +
 blockdev.c                         |    3 +- 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/pc_sysfw.c                      |  196 ++++++++++++++++++++++++++++++++++++ hw/pflash_cfi01.c                  |   44 ++++++--- hw/pflash_cfi02.c                  |   83 ++++++++-------
 vl.c                               |    2 +-

 11 files changed, 286 insertions(+), 104 deletions(-)

 create mode 100644 hw/pc_sysfw.c

Signature · 3 lines
Regards,

Zhi Yong Wu