---
 hw/mac_nvram.c    |  126 ++++++++++++++++++++++++++++++++++++++++++-----------
 hw/ppc_mac.h      |    4 +-
 hw/ppc_oldworld.c |    4 +-
 3 files changed, 104 insertions(+), 30 deletions(-)

Index: qemu/hw/mac_nvram.c
===================================================================
--- qemu.orig/hw/mac_nvram.c	2008-12-22 11:38:50.000000000 +0000
+++ qemu/hw/mac_nvram.c	2008-12-22 11:40:34.000000000 +0000
@@ -23,6 +23,8 @@
  * THE SOFTWARE.
  */
 #include "hw.h"
+#include "firmware_abi.h"
+#include "sysemu.h"
 #include "ppc_mac.h"
 
 struct MacIONVRAMState {
@@ -112,26 +114,35 @@
     cpu_register_physical_memory(mem_base, s->size, s->mem_index);
 }
 
-static uint8_t nvram_chksum (const uint8_t *buf, int n)
-{
-    int sum, i;
-    sum = 0;
-    for(i = 0; i < n; i++)
-        sum += buf[i];
-    return (sum & 0xff) + (sum >> 8);
-}
-
-/* set a free Mac OS NVRAM partition */
+/* Set up a system OpenBIOS NVRAM partition */
 void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len)
 {
-    uint8_t *buf;
-    char partition_name[12] = "wwwwwwwwwwww";
+    unsigned int i;
+    uint32_t start = 0, end;
+    struct OpenBIOS_nvpart_v1 *part_header;
+
+    // OpenBIOS nvram variables
+    // Variable partition
+    part_header = (struct OpenBIOS_nvpart_v1 *)nvr->data;
+    part_header->signature = OPENBIOS_PART_SYSTEM;
+    pstrcpy(part_header->name, sizeof(part_header->name), "system");
+
+    end = start + sizeof(struct OpenBIOS_nvpart_v1);
+    for (i = 0; i < nb_prom_envs; i++)
+        end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]);
+
+    // End marker
+    nvr->data[end++] = '\0';
+
+    end = start + ((end - start + 15) & ~15);
+    OpenBIOS_finish_partition(part_header, end - start);
+
+    // free partition
+    start = end;
+    part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start];
+    part_header->signature = OPENBIOS_PART_FREE;
+    pstrcpy(part_header->name, sizeof(part_header->name), "free");
 
-    buf = nvr->data;
-    buf[0] = 0x7f; /* free partition magic */
-    buf[1] = 0; /* checksum */
-    buf[2] = len >> 8;
-    buf[3] = len;
-    memcpy(buf + 4, partition_name, 12);
-    buf[1] = nvram_chksum(buf, 16);
+    end = len;
+    OpenBIOS_finish_partition(part_header, end - start);
 }
Index: qemu/sysemu.h
===================================================================
--- qemu.orig/sysemu.h	2008-12-22 11:38:50.000000000 +0000
+++ qemu/sysemu.h	2008-12-22 11:40:34.000000000 +0000
@@ -108,7 +108,7 @@
 extern const char *option_rom[MAX_OPTION_ROMS];
 extern int nb_option_roms;
 
-#ifdef TARGET_SPARC
+#if defined(TARGET_SPARC) || defined(TARGET_PPC)
 #define MAX_PROM_ENVS 128
 extern const char *prom_envs[MAX_PROM_ENVS];
 extern unsigned int nb_prom_envs;
Index: qemu/vl.c
===================================================================
--- qemu.orig/vl.c	2008-12-22 11:38:50.000000000 +0000
+++ qemu/vl.c	2008-12-22 11:40:34.000000000 +0000
@@ -231,7 +231,7 @@
 #endif
 const char *qemu_name;
 int alt_grab = 0;
-#ifdef TARGET_SPARC
+#if defined(TARGET_SPARC) || defined(TARGET_PPC)
 unsigned int nb_prom_envs = 0;
 const char *prom_envs[MAX_PROM_ENVS];
 #endif
@@ -4185,7 +4185,7 @@
     { "semihosting", 0, QEMU_OPTION_semihosting },
 #endif
     { "name", HAS_ARG, QEMU_OPTION_name },
-#if defined(TARGET_SPARC)
+#if defined(TARGET_SPARC) || defined(TARGET_PPC)
     { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
 #endif
 #if defined(TARGET_ARM)
@@ -5049,7 +5049,7 @@
             case QEMU_OPTION_name:
                 qemu_name = optarg;
                 break;
-#ifdef TARGET_SPARC
+#if defined(TARGET_SPARC) || defined(TARGET_PPC)
             case QEMU_OPTION_prom_env:
                 if (nb_prom_envs >= MAX_PROM_ENVS) {
                     fprintf(stderr, "Too many prom variables\n");
Index: qemu/qemu-doc.texi
===================================================================
--- qemu.orig/qemu-doc.texi	2008-12-22 11:38:50.000000000 +0000
+++ qemu/qemu-doc.texi	2008-12-22 11:40:34.000000000 +0000
@@ -2343,6 +2343,18 @@
 
 Set the initial VGA graphic mode. The default is 800x600x15.
 
+@item -prom-env string
+
+Set OpenBIOS variables in NVRAM, for example:
+
+@example
+qemu-system-ppc -prom-env 'auto-boot?=false' \
+ -prom-env 'boot-device=hd:2,\yaboot' \
+ -prom-env 'boot-args=conf=hd:2,\yaboot.conf'
+@end example
+
+These variables are not used by Open Hack'Ware.
+
 @end table
 
 @c man end
