:: commit c20828138edae85ac76ab9066412b61f37810585

mintsuki <mintsuki@protonmail.com> — 2021-07-06 20:45

parents: ae2d924c14

Revert "uefi: Even saner handling of exiting boot services"

This reverts commit e6cc0e4b93d8c7f5fc4dd4f8726e34b65a8354b3.
diff --git a/stage23/lib/blib.c b/stage23/lib/blib.c
index 1f708b23..a827d259 100644
--- a/stage23/lib/blib.c
+++ b/stage23/lib/blib.c
@@ -86,10 +86,6 @@ bool efi_exit_boot_services(void) {
 
     efi_mmap_size += 4096;
 
-    status = uefi_call_wrapper(gBS->FreePool, 1, efi_mmap);
-    if (status)
-        goto fail;
-
     status = uefi_call_wrapper(gBS->AllocatePool, 3,
         EfiLoaderData, efi_mmap_size, &efi_mmap);
     if (status)
@@ -116,8 +112,19 @@ bool efi_exit_boot_services(void) {
     for (size_t i = 0; i < entry_count; i++) {
         EFI_MEMORY_DESCRIPTOR *entry = (void *)efi_mmap + i * efi_desc_size;
 
-        if (entry->Type == 0x80000000) {
-            entry->Type = EfiConventionalMemory;
+        uint64_t base = entry->PhysicalStart;
+        uint64_t length = entry->NumberOfPages * 4096;
+
+        // Find for a match in the untouched memory map
+        for (size_t j = 0; j < untouched_memmap_entries; j++) {
+            if (untouched_memmap[j].type != MEMMAP_USABLE)
+                continue;
+
+            if (untouched_memmap[j].base == base && untouched_memmap[j].length == length) {
+                // It's a match!
+                entry->Type = EfiConventionalMemory;
+                break;
+            }
         }
     }
 
diff --git a/stage23/mm/pmm.h b/stage23/mm/pmm.h
index f0b8a30d..7483478d 100644
--- a/stage23/mm/pmm.h
+++ b/stage23/mm/pmm.h
@@ -19,6 +19,11 @@
 extern struct e820_entry_t memmap[];
 extern size_t memmap_entries;
 
+#if defined (uefi)
+extern struct e820_entry_t untouched_memmap[];
+extern size_t untouched_memmap_entries;
+#endif
+
 void init_memmap(void);
 struct e820_entry_t *get_memmap(size_t *entries);
 struct e820_entry_t *get_raw_memmap(size_t *entry_count);
diff --git a/stage23/mm/pmm.s2.c b/stage23/mm/pmm.s2.c
index 4fc5b36f..41a5b4db 100644
--- a/stage23/mm/pmm.s2.c
+++ b/stage23/mm/pmm.s2.c
@@ -51,6 +51,11 @@ void *conv_mem_alloc(size_t count) {
 struct e820_entry_t memmap[MEMMAP_MAX_ENTRIES];
 size_t memmap_entries = 0;
 
+#if defined (uefi)
+struct e820_entry_t untouched_memmap[MEMMAP_MAX_ENTRIES];
+size_t untouched_memmap_entries = 0;
+#endif
+
 static const char *memmap_type(uint32_t type) {
     switch (type) {
         case MEMMAP_USABLE:
@@ -310,6 +315,9 @@ void init_memmap(void) {
 
     sanitise_entries(memmap, &memmap_entries, false);
 
+    memcpy(untouched_memmap, memmap, memmap_entries * sizeof(struct e820_entry_t));
+    untouched_memmap_entries = memmap_entries;
+
     allocations_disallowed = false;
 
     // Let's leave 64MiB to the firmware
@@ -323,7 +331,7 @@ void init_memmap(void) {
         EFI_PHYSICAL_ADDRESS base = memmap[i].base;
 
         status = uefi_call_wrapper(gBS->AllocatePages, 4,
-          AllocateAddress, 0x80000000, memmap[i].length / 4096, &base);
+          AllocateAddress, EfiLoaderData, memmap[i].length / 4096, &base);
 
         if (status)
             panic("pmm: AllocatePages failure (%x)", status);
tab: 248 wrap: offon