e7ebc2a83ef2f426592ea1b8346cdf740c215cf9
[seabios.git] / src / memmap.c
1 // Support for building memory maps suitable for int 15 e820 calls.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU GPLv3 license.
6
7 #include "memmap.h" // struct e820entry
8 #include "util.h" // dprintf.h
9
10 // Temporary storage used during map building.
11 static struct e820entry e820_list[64];
12 static int e820_count;
13
14 // Remove an entry from the e820_list.
15 static void
16 remove_e820(int i)
17 {
18     e820_count--;
19     memmove(&e820_list[i], &e820_list[i+1]
20             , sizeof(e820_list[0]) * (e820_count - i));
21 }
22
23 // Insert an entry in the e820_list at the given position.
24 static void
25 insert_e820(int i, u64 start, u64 size, u32 type)
26 {
27     if (e820_count >= ARRAY_SIZE(e820_list)) {
28         dprintf(1, "Overflowed e820 list!\n");
29         return;
30     }
31
32     memmove(&e820_list[i+1], &e820_list[i]
33             , sizeof(e820_list[0]) * (e820_count - i));
34     e820_count++;
35     struct e820entry *e = &e820_list[i];
36     e->start = start;
37     e->size = size;
38     e->type = type;
39 }
40
41 // Show the current e820_list.
42 static void
43 dump_map()
44 {
45     dprintf(1, "e820 map has %d items:\n", e820_count);
46     int i;
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
53                 , e->type);
54     }
55 }
56
57 // Add a new entry to the list.  This scans for overlaps and keeps the
58 // list sorted.
59 void
60 add_e820(u64 start, u64 size, u32 type)
61 {
62     dprintf(8, "Add to e820 map: %x %x %d\n", (u32)start, (u32)size, type);
63
64     if (! size)
65         // Huh?  Nothing to do.
66         return;
67
68     u64 end = start + size;
69     int i;
70     for (i=0; i<e820_count; i++) {
71         struct e820entry *e = &e820_list[i];
72         if (end <= e->start)
73             // Simple insertion point.
74             break;
75         u64 e_end = e->start + e->size;
76         if (start >= e_end)
77             // No overlap.
78             continue;
79         // New item overlaps with an existing one.
80         if (start > e->start) {
81             e->size = start - e->start;
82             i++;
83             if (end < e_end)
84                 // Need to split existing item
85                 insert_e820(i, end, e_end - end, e->type);
86         }
87         insert_e820(i, start, size, type);
88         i++;
89         // Remove all existing items that are completely overlapped.
90         while (i<e820_count) {
91             e = &e820_list[i];
92             if (end <= e->start)
93                 break;
94             e_end = e->start + e->size;
95             if (end < e_end) {
96                 // Existing item not completely overlapped - adjust its start.
97                 e->start = end;
98                 e->size = e_end - e->start;
99                 break;
100             }
101             remove_e820(i);
102         }
103         //dump_map();
104         return;
105     }
106     // Just insert item.
107     insert_e820(i, start, size, type);
108     //dump_map();
109 }
110
111 u32 bios_table_cur_addr, bios_table_end_addr;
112
113 // Prep for memmap stuff - init bios table locations.
114 void
115 memmap_setup()
116 {
117     bios_table_cur_addr = 0xf0000 | OFFSET_freespace2_start;
118     bios_table_end_addr = 0xf0000 | OFFSET_freespace2_end;
119     dprintf(1, "bios_table_addr: 0x%08x end=0x%08x\n",
120             bios_table_cur_addr, bios_table_end_addr);
121 }
122
123 // Copy the temporary e820 map info to its permanent location.
124 void
125 memmap_finalize()
126 {
127     dump_map();
128
129     u32 msize = e820_count * sizeof(e820_list[0]);
130     if (bios_table_cur_addr + msize > bios_table_end_addr) {
131         dprintf(1, "No room for e820 map!\n");
132         return;
133     }
134     memcpy((void*)bios_table_cur_addr, e820_list, msize);
135     SET_EBDA(e820_loc, bios_table_cur_addr);
136     SET_EBDA(e820_count, e820_count);
137     bios_table_cur_addr += msize;
138 }