Moved find_segment into elf_loader.c
authorMichael Brown <mcb30@etherboot.org>
Tue, 17 May 2005 13:37:29 +0000 (13:37 +0000)
committerMichael Brown <mcb30@etherboot.org>
Tue, 17 May 2005 13:37:29 +0000 (13:37 +0000)
src/core/elf_loader.c

index c1906d8..88a2975 100644 (file)
@@ -32,6 +32,45 @@ struct elf_state
 
 static struct elf_state estate;
 
+static unsigned long find_segment(unsigned long size, unsigned long align)
+{
+       unsigned i;
+       /* Verify I have a power of 2 alignment */
+       if (align & (align - 1)) {
+               return ULONG_MAX;
+       }
+       for(i = 0; i < meminfo.map_count; i++) {
+               unsigned long r_start, r_end;
+               if (meminfo.map[i].type != E820_RAM)
+                       continue;
+               if ((meminfo.map[i].addr + meminfo.map[i].size) > ULONG_MAX) {
+                       continue;
+               }
+               r_start = meminfo.map[i].addr;
+               r_end = r_start + meminfo.map[i].size;
+               /* Don't allow the segment to overlap etherboot */
+               if ((r_end > virt_to_phys(_text)) && (r_start < virt_to_phys(_text))) {
+                       r_end = virt_to_phys(_text);
+               }
+               if ((r_start > virt_to_phys(_text)) && (r_start < virt_to_phys(_end))) {
+                       r_start = virt_to_phys(_end);
+               }
+               /* Don't allow the segment to overlap the heap */
+               if ((r_end > heap_ptr) && (r_start < heap_ptr)) {
+                       r_end = heap_ptr;
+               }
+               if ((r_start > heap_ptr) && (r_start < heap_bot)) {
+                       r_start = heap_ptr;
+               }
+               r_start = (r_start + align - 1) & ~(align - 1);
+               if ((r_end >= r_start) && ((r_end - r_start) >= size)) {
+                       return r_start;
+               }
+       }
+       /* I did not find anything :( */
+       return ULONG_MAX;
+}
+
 static void elf_boot(unsigned long machine, unsigned long entry)
 {
        int result;