Patchwork [2/2] pc: Support system flash memory with pflash

login
register
mail settings
Submitter jordan.l.justen@intel.com
Date July 25, 2011, 9:34 p.m.
Message ID <1311629692-5734-2-git-send-email-jordan.l.justen@intel.com>
Download mbox | patch
Permalink /patch/106756/
State New
Headers show

Comments

jordan.l.justen@intel.com - July 25, 2011, 9:34 p.m.
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>
Cc: Aurelien Jarno <aurelien@aurel32.net>
---
 Makefile.target                    |    2 +-
 default-configs/i386-softmmu.mak   |    1 +
 default-configs/x86_64-softmmu.mak |    1 +
 hw/boards.h                        |    1 +
 hw/pc.c                            |   46 +------------
 hw/pc.h                            |    2 +
 hw/pcflash.c                       |  130 ++++++++++++++++++++++++++++++++++++
 vl.c                               |    2 +-
 8 files changed, 141 insertions(+), 44 deletions(-)
 create mode 100644 hw/pcflash.c
Anthony Liguori - July 29, 2011, 1:06 p.m.
On 07/25/2011 04:34 PM, Jordan Justen 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>
> Cc: Aurelien Jarno<aurelien@aurel32.net>
> ---
>   Makefile.target                    |    2 +-
>   default-configs/i386-softmmu.mak   |    1 +
>   default-configs/x86_64-softmmu.mak |    1 +
>   hw/boards.h                        |    1 +
>   hw/pc.c                            |   46 +------------
>   hw/pc.h                            |    2 +
>   hw/pcflash.c                       |  130 ++++++++++++++++++++++++++++++++++++
>   vl.c                               |    2 +-
>   8 files changed, 141 insertions(+), 44 deletions(-)
>   create mode 100644 hw/pcflash.c
>
> diff --git a/Makefile.target b/Makefile.target
> index cde509b..4d8821f 100644
> --- a/Makefile.target
> +++ b/Makefile.target
> @@ -223,7 +223,7 @@ obj-$(CONFIG_IVSHMEM) += ivshmem.o
>
>   # Hardware support
>   obj-i386-y += vga.o
> -obj-i386-y += mc146818rtc.o i8259.o pc.o
> +obj-i386-y += mc146818rtc.o i8259.o pc.o pcflash.o

This should be able to live in hw-obj-y in Makefile.objs since it 
doesn't depend on any TARGET_ defines or CPUState.

> diff --git a/hw/pc.c b/hw/pc.c
> index a3e8539..cf7257b 100644
> --- a/hw/pc.c
> +++ b/hw/pc.c
> @@ -55,10 +55,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
> @@ -963,10 +959,8 @@ void pc_memory_init(const char *kernel_filename,
>                       ram_addr_t below_4g_mem_size,
>                       ram_addr_t above_4g_mem_size)
>   {
> -    char *filename;
> -    int ret, linux_boot, i;
> -    ram_addr_t ram_addr, bios_offset, option_rom_offset;
> -    int bios_size, isa_bios_size;
> +    int linux_boot, i;
> +    ram_addr_t ram_addr, option_rom_offset;
>       void *fw_cfg;
>
>       linux_boot = (kernel_filename != NULL);
> @@ -983,44 +977,12 @@ void pc_memory_init(const char *kernel_filename,
>                                        ram_addr + below_4g_mem_size);
>       }
>
> -    /* 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_offset = qemu_ram_alloc(NULL, "pc.bios", bios_size);
> -    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) {
> -        qemu_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;
> -    cpu_register_physical_memory(0x100000 - isa_bios_size,
> -                                 isa_bios_size,
> -                                 (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
> +    /* Initialize ROM or flash ranges for PC firmware */
> +    pc_system_firmware_init();
>
>       option_rom_offset = qemu_ram_alloc(NULL, "pc.rom", PC_ROM_SIZE);
>       cpu_register_physical_memory(PC_ROM_MIN_VGA, PC_ROM_SIZE, option_rom_offset);
>
> -    /* map all the bios at the top of memory */
> -    cpu_register_physical_memory((uint32_t)(-bios_size),
> -                                 bios_size, bios_offset | IO_MEM_ROM);
> -
>       fw_cfg = bochs_bios_init();
>       rom_set_fw(fw_cfg);
>
> diff --git a/hw/pc.h b/hw/pc.h
> index 6d5730b..114816d 100644
> --- a/hw/pc.h
> +++ b/hw/pc.h
> @@ -238,4 +238,6 @@ static inline bool isa_ne2000_init(int base, int irq, NICInfo *nd)
>
>   int e820_add_entry(uint64_t, uint64_t, uint32_t);
>
> +void pc_system_firmware_init(void);
> +
>   #endif
> diff --git a/hw/pcflash.c b/hw/pcflash.c
> new file mode 100644
> index 0000000..514b1ed
> --- /dev/null
> +++ b/hw/pcflash.c
> @@ -0,0 +1,130 @@
> +/*
> + * 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"
> +
> +#define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
> +
> +static void pc_isa_bios_init(ram_addr_t ram_offset, int ram_size)
> +{
> +    int isa_bios_size;
> +
> +    /* map the last 128KB of the BIOS in ISA space */
> +    isa_bios_size = ram_size;
> +    if (isa_bios_size>  (128 * 1024)) {
> +        isa_bios_size = 128 * 1024;
> +    }
> +    ram_offset = ram_offset + ram_size - isa_bios_size;
> +    cpu_register_physical_memory(0x100000 - isa_bios_size,
> +                                 isa_bios_size,
> +                                 ram_offset | IO_MEM_ROM);
> +}
> +
> +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);
> +    fprintf(stderr, "ROM: %s\n", filename);
> +
> +    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(DriveInfo *pflash_drv)
> +{
> +    BlockDriverState *bdrv;
> +    int64_t size;
> +    target_phys_addr_t phys_addr;
> +    ram_addr_t addr;
> +    int sector_bits, sector_size;
> +
> +    bdrv = NULL;
> +
> +    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;
> +    addr = qemu_ram_alloc(NULL, "system.flash", size);
> +    pflash_cfi01_register(phys_addr, addr, bdrv,
> +                          sector_size, size>>  sector_bits,
> +                          4, 0x0000, 0x0000, 0x0000, 0x0000, 0);
> +
> +    pc_isa_bios_init(addr, size);
> +}
> +
> +void pc_system_firmware_init(void)
> +{
> +    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(pflash_drv);
> +}
> +

This didn't quite turn out how I had hoped but without touching every 
machine init, I don't see a better way.

Regards,

Anthony Liguori

> diff --git a/vl.c b/vl.c
> index 4b6688b..00d8fcb 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -1063,7 +1063,7 @@ static QEMUMachine *find_machine(const char *name)
>       return NULL;
>   }
>
> -static QEMUMachine *find_default_machine(void)
> +QEMUMachine *find_default_machine(void)
>   {
>       QEMUMachine *m;
>

Patch

diff --git a/Makefile.target b/Makefile.target
index cde509b..4d8821f 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -223,7 +223,7 @@  obj-$(CONFIG_IVSHMEM) += ivshmem.o
 
 # Hardware support
 obj-i386-y += vga.o
-obj-i386-y += mc146818rtc.o i8259.o pc.o
+obj-i386-y += mc146818rtc.o i8259.o pc.o pcflash.o
 obj-i386-y += cirrus_vga.o sga.o apic.o ioapic.o piix_pci.o
 obj-i386-y += vmport.o
 obj-i386-y += device-hotplug.o pci-hotplug.o smbios.o wdt_ib700.o
diff --git a/default-configs/i386-softmmu.mak b/default-configs/i386-softmmu.mak
index 55589fa..8697cd4 100644
--- a/default-configs/i386-softmmu.mak
+++ b/default-configs/i386-softmmu.mak
@@ -21,3 +21,4 @@  CONFIG_PIIX_PCI=y
 CONFIG_SOUND=y
 CONFIG_HPET=y
 CONFIG_APPLESMC=y
+CONFIG_PFLASH_CFI01=y
diff --git a/default-configs/x86_64-softmmu.mak b/default-configs/x86_64-softmmu.mak
index 8895028..eca9284 100644
--- a/default-configs/x86_64-softmmu.mak
+++ b/default-configs/x86_64-softmmu.mak
@@ -21,3 +21,4 @@  CONFIG_PIIX_PCI=y
 CONFIG_SOUND=y
 CONFIG_HPET=y
 CONFIG_APPLESMC=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 a3e8539..cf7257b 100644
--- a/hw/pc.c
+++ b/hw/pc.c
@@ -55,10 +55,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
@@ -963,10 +959,8 @@  void pc_memory_init(const char *kernel_filename,
                     ram_addr_t below_4g_mem_size,
                     ram_addr_t above_4g_mem_size)
 {
-    char *filename;
-    int ret, linux_boot, i;
-    ram_addr_t ram_addr, bios_offset, option_rom_offset;
-    int bios_size, isa_bios_size;
+    int linux_boot, i;
+    ram_addr_t ram_addr, option_rom_offset;
     void *fw_cfg;
 
     linux_boot = (kernel_filename != NULL);
@@ -983,44 +977,12 @@  void pc_memory_init(const char *kernel_filename,
                                      ram_addr + below_4g_mem_size);
     }
 
-    /* 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_offset = qemu_ram_alloc(NULL, "pc.bios", bios_size);
-    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) {
-        qemu_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;
-    cpu_register_physical_memory(0x100000 - isa_bios_size,
-                                 isa_bios_size,
-                                 (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
+    /* Initialize ROM or flash ranges for PC firmware */
+    pc_system_firmware_init();
 
     option_rom_offset = qemu_ram_alloc(NULL, "pc.rom", PC_ROM_SIZE);
     cpu_register_physical_memory(PC_ROM_MIN_VGA, PC_ROM_SIZE, option_rom_offset);
 
-    /* map all the bios at the top of memory */
-    cpu_register_physical_memory((uint32_t)(-bios_size),
-                                 bios_size, bios_offset | IO_MEM_ROM);
-
     fw_cfg = bochs_bios_init();
     rom_set_fw(fw_cfg);
 
diff --git a/hw/pc.h b/hw/pc.h
index 6d5730b..114816d 100644
--- a/hw/pc.h
+++ b/hw/pc.h
@@ -238,4 +238,6 @@  static inline bool isa_ne2000_init(int base, int irq, NICInfo *nd)
 
 int e820_add_entry(uint64_t, uint64_t, uint32_t);
 
+void pc_system_firmware_init(void);
+
 #endif
diff --git a/hw/pcflash.c b/hw/pcflash.c
new file mode 100644
index 0000000..514b1ed
--- /dev/null
+++ b/hw/pcflash.c
@@ -0,0 +1,130 @@ 
+/*
+ * 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"
+
+#define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
+
+static void pc_isa_bios_init(ram_addr_t ram_offset, int ram_size)
+{
+    int isa_bios_size;
+
+    /* map the last 128KB of the BIOS in ISA space */
+    isa_bios_size = ram_size;
+    if (isa_bios_size > (128 * 1024)) {
+        isa_bios_size = 128 * 1024;
+    }
+    ram_offset = ram_offset + ram_size - isa_bios_size;
+    cpu_register_physical_memory(0x100000 - isa_bios_size,
+                                 isa_bios_size,
+                                 ram_offset | IO_MEM_ROM);
+}
+
+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);
+    fprintf(stderr, "ROM: %s\n", filename);
+
+    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(DriveInfo *pflash_drv)
+{
+    BlockDriverState *bdrv;
+    int64_t size;
+    target_phys_addr_t phys_addr;
+    ram_addr_t addr;
+    int sector_bits, sector_size;
+
+    bdrv = NULL;
+
+    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;
+    addr = qemu_ram_alloc(NULL, "system.flash", size);
+    pflash_cfi01_register(phys_addr, addr, bdrv,
+                          sector_size, size >> sector_bits,
+                          4, 0x0000, 0x0000, 0x0000, 0x0000, 0);
+
+    pc_isa_bios_init(addr, size);
+}
+
+void pc_system_firmware_init(void)
+{
+    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(pflash_drv);
+}
+
+
diff --git a/vl.c b/vl.c
index 4b6688b..00d8fcb 100644
--- a/vl.c
+++ b/vl.c
@@ -1063,7 +1063,7 @@  static QEMUMachine *find_machine(const char *name)
     return NULL;
 }
 
-static QEMUMachine *find_default_machine(void)
+QEMUMachine *find_default_machine(void)
 {
     QEMUMachine *m;