Mac OS 9 looks for a CHRP NVRAM partition with signature 0xa0 named
"APL,MacOS75" to store its PRAM and name registry.
pmac_format_nvram_partition() only creates the OpenBIOS "common" partition
and the Mac OS X one, so every boot prints

  MacOS: unable to find a usable NVRAM partition - using offset 0x1400.

and the guest then keeps PRAM at offset 0x1400, which is inside the
partition reserved for Mac OS X.

Fill the free area left between both partitions with the Mac OS 9 partition
instead of a free space one, and keep the previous behaviour when that area
is too small (for example with many -prom-env variables). Only the 16 byte
CHRP header is written on each boot, so the PRAM contents survive as long as
the NVRAM has a backing store.

Tested with Mac OS 9.2.2 on the mac99 machine: the warning is gone and the
guest boots and runs normally.

Signed-off-by: Federico Krum <[email protected]>
---
Note: written with the help of an AI assistant; the change was reviewed,
built and tested by me with Mac OS 9.2.2 on mac99.

 hw/nvram/mac_nvram.c | 43 +++++++++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 10 deletions(-)

diff --git a/hw/nvram/mac_nvram.c b/hw/nvram/mac_nvram.c
index efadb3c..adca155 100644
--- a/hw/nvram/mac_nvram.c
+++ b/hw/nvram/mac_nvram.c
@@ -164,18 +164,31 @@ static void macio_nvram_register_types(void)
     type_register_static(&macio_nvram_type_info);
 }

-/* Set up a system OpenBIOS NVRAM partition */
-static void pmac_format_nvram_partition_of(MacIONVRAMState *nvr, int off,
-                                           int len)
+/* Set up a system OpenBIOS NVRAM partition, return the offset past it */
+static int pmac_format_nvram_partition_of(MacIONVRAMState *nvr, int off,
+                                          int len)
 {
-    int sysp_end;
-
     /* OpenBIOS nvram variables partition */
-    sysp_end = chrp_nvram_create_system_partition(&nvr->data[off],
-                                                  DEF_SYSTEM_SIZE, len) + off;
+    return chrp_nvram_create_system_partition(&nvr->data[off],
+                                              DEF_SYSTEM_SIZE, len) + off;
+}

-    /* Free space partition */
-    chrp_nvram_create_free_partition(&nvr->data[sysp_end], len - sysp_end);
+/*
+ * Mac OS 9 looks for a CHRP "APL,MacOS75" partition to keep its PRAM
+ * (0x100 bytes right after the 0x10 byte header) and name registry.
+ */
+#define MACOS_NVRAM_SIGNATURE   (0xA0)
+#define MACOS_NVRAM_MIN_SIZE    (0x110)
+
+static void pmac_format_nvram_partition_macos(MacIONVRAMState *nvr, int off,
+                                              int len)
+{
+    ChrpNvramPartHdr *part_header = (ChrpNvramPartHdr *)&nvr->data[off];
+
+    part_header->signature = MACOS_NVRAM_SIGNATURE;
+    pstrcpy(part_header->name, sizeof(part_header->name), "APL,MacOS75");
+
+    chrp_nvram_finish_partition(part_header, len);
+}

 #define OSX_NVRAM_SIGNATURE     (0x5A)
@@ -205,12 +218,22 @@ static void
pmac_format_nvram_partition_osx(MacIONVRAMState *nvr, int off,
 /* Set up NVRAM with OF and OSX partitions */
 void pmac_format_nvram_partition(MacIONVRAMState *nvr, int len)
 {
+    int sysp_end, hole;
+
     /*
      * Mac OS X expects side "B" of the flash at the second half of NVRAM,
      * so we use half of the chip for OF and the other half for a free OSX
      * partition.
      */
-    pmac_format_nvram_partition_of(nvr, 0, len / 2);
+    sysp_end = pmac_format_nvram_partition_of(nvr, 0, len / 2);
+    hole = len / 2 - sysp_end;
+    if (hole >= MACOS_NVRAM_MIN_SIZE) {
+        /* Mac OS 9 PRAM partition, keeps the previous PRAM contents */
+        pmac_format_nvram_partition_macos(nvr, sysp_end, hole);
+    } else if (hole > 0) {
+        /* Free space partition */
+        chrp_nvram_create_free_partition(&nvr->data[sysp_end], hole);
+    }
     pmac_format_nvram_partition_osx(nvr, len / 2, len / 2);
 }
 type_init(macio_nvram_register_types)
-- 
2.56.0

Reply via email to