1 // Support for building memory maps suitable for int 15 e820 calls.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU GPLv3 license.
7 #include "memmap.h" // struct e820entry
8 #include "util.h" // dprintf.h
10 // Temporary storage used during map building.
11 static struct e820entry e820_list[64];
12 static int e820_count;
14 // Remove an entry from the e820_list.
19 memmove(&e820_list[i], &e820_list[i+1]
20 , sizeof(e820_list[0]) * (e820_count - i));
23 // Insert an entry in the e820_list at the given position.
25 insert_e820(int i, u64 start, u64 size, u32 type)
27 if (e820_count >= ARRAY_SIZE(e820_list)) {
28 dprintf(1, "Overflowed e820 list!\n");
32 memmove(&e820_list[i+1], &e820_list[i]
33 , sizeof(e820_list[0]) * (e820_count - i));
35 struct e820entry *e = &e820_list[i];
41 // Show the current e820_list.
45 dprintf(1, "e820 map has %d items:\n", e820_count);
47 for (i=0; i<e820_count; i++) {
48 struct e820entry *e = &e820_list[i];
49 u64 e_end = e->start + e->size;
50 dprintf(1, " %d: %x%x - %x%x = %d\n", i
51 , (u32)(e->start >> 32), (u32)e->start
52 , (u32)(e_end >> 32), (u32)e_end
57 // Add a new entry to the list. This scans for overlaps and keeps the
60 add_e820(u64 start, u64 size, u32 type)
62 dprintf(8, "Add to e820 map: %x %x %d\n", (u32)start, (u32)size, type);
65 // Huh? Nothing to do.
68 u64 end = start + size;
70 for (i=0; i<e820_count; i++) {
71 struct e820entry *e = &e820_list[i];
73 // Simple insertion point.
75 u64 e_end = e->start + e->size;
79 // New item overlaps (or borders) an existing one.
80 if (start > e->start) {
81 e->size = start - e->start;
84 // Need to split existing item
85 insert_e820(i, end, e_end - end, e->type);
86 if (type == e->type) {
87 // Same type - merge them.
88 size += start - e->start;
94 insert_e820(i, start, size, type);
96 // Remove all existing items that are completely overlapped.
97 while (i<e820_count) {
100 // No overlap - done.
102 e_end = e->start + e->size;
104 // Existing item completely overlapped - remove it.
108 // Not completely overlapped - adjust its start.
110 e->size = e_end - e->start;
111 if (type == e->type) {
112 // Same type - merge them.
113 (e-1)->size += e->size;
122 insert_e820(i, start, size, type);
126 u32 bios_table_cur_addr, bios_table_end_addr;
128 // Prep for memmap stuff - init bios table locations.
132 bios_table_cur_addr = 0xf0000 | OFFSET_freespace2_start;
133 bios_table_end_addr = 0xf0000 | OFFSET_freespace2_end;
134 dprintf(1, "bios_table_addr: 0x%08x end=0x%08x\n",
135 bios_table_cur_addr, bios_table_end_addr);
138 // Copy the temporary e820 map info to its permanent location.
144 u32 msize = e820_count * sizeof(e820_list[0]);
145 if (bios_table_cur_addr + msize > bios_table_end_addr) {
146 dprintf(1, "No room for e820 map!\n");
149 memcpy((void*)bios_table_cur_addr, e820_list, msize);
150 SET_EBDA(e820_loc, bios_table_cur_addr);
151 SET_EBDA(e820_count, e820_count);
152 bios_table_cur_addr += msize;