Add linker magic to ensure 16bit variables aren't repeated in 32bit code.
[seabios.git] / src / memmap.c
index 66fba097c5dac2a3e0ca5e8636bcae9829b52272..2bcb2545f242007d538ea384d57bc27e21cd9fb0 100644 (file)
@@ -6,10 +6,7 @@
 
 #include "memmap.h" // struct e820entry
 #include "util.h" // dprintf.h
-
-// Temporary storage used during map building.
-static struct e820entry e820_list[64];
-static int e820_count;
+#include "biosvar.h" // SET_EBDA
 
 // Remove an entry from the e820_list.
 static void
@@ -24,7 +21,7 @@ remove_e820(int i)
 static void
 insert_e820(int i, u64 start, u64 size, u32 type)
 {
-    if (e820_count >= ARRAY_SIZE(e820_list)) {
+    if (e820_count >= CONFIG_MAX_E820) {
         dprintf(1, "Overflowed e820 list!\n");
         return;
     }
@@ -91,8 +88,10 @@ add_e820(u64 start, u64 size, u32 type)
                 remove_e820(i);
             }
         }
-        insert_e820(i, start, size, type);
-        i++;
+        if (type != E820_HOLE) {
+            insert_e820(i, start, size, type);
+            i++;
+        }
         // Remove all existing items that are completely overlapped.
         while (i<e820_count) {
             e = &e820_list[i];
@@ -123,31 +122,41 @@ add_e820(u64 start, u64 size, u32 type)
     //dump_map();
 }
 
+// Symbols defined in romlayout.S
+extern char freespace2_start, freespace2_end;
+
 u32 bios_table_cur_addr, bios_table_end_addr;
 
 // Prep for memmap stuff - init bios table locations.
 void
 memmap_setup()
 {
-    bios_table_cur_addr = 0xf0000 | OFFSET_freespace2_start;
-    bios_table_end_addr = 0xf0000 | OFFSET_freespace2_end;
+    bios_table_cur_addr = (u32)&freespace2_start;
+    bios_table_end_addr = (u32)&freespace2_end;
     dprintf(1, "bios_table_addr: 0x%08x end=0x%08x\n",
             bios_table_cur_addr, bios_table_end_addr);
+
+    bios_table_cur_addr = ALIGN(bios_table_cur_addr, 4);
+    u32 msize = CONFIG_MAX_E820 * sizeof(e820_list[0]);
+    if (bios_table_cur_addr + msize > bios_table_end_addr) {
+        dprintf(1, "No room for e820 map!\n");
+        return;
+    }
+    e820_count = 0;
+    e820_list = (void*)bios_table_cur_addr;
+    bios_table_cur_addr += msize;
 }
 
-// Copy the temporary e820 map info to its permanent location.
+// Report on final memory locations.
 void
 memmap_finalize()
 {
     dump_map();
 
-    u32 msize = e820_count * sizeof(e820_list[0]);
-    if (bios_table_cur_addr + msize > bios_table_end_addr) {
-        dprintf(1, "No room for e820 map!\n");
-        return;
-    }
-    memcpy((void*)bios_table_cur_addr, e820_list, msize);
-    SET_EBDA(e820_loc, bios_table_cur_addr);
-    SET_EBDA(e820_count, e820_count);
-    bios_table_cur_addr += msize;
+    dprintf(1, "final bios_table_addr: 0x%08x (used %d%%)\n"
+            , bios_table_cur_addr
+            , (100 * (bios_table_cur_addr - (u32)&freespace2_start)
+               / ((u32)&freespace2_end - (u32)&freespace2_start)));
+    if (bios_table_cur_addr > bios_table_end_addr)
+        BX_PANIC("bios_table_end_addr overflow!\n");
 }