From 9968c9513e1a503dcfa3ae11a85d80765f6a5b34 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 17 May 2005 13:37:29 +0000 Subject: [PATCH] Moved find_segment into elf_loader.c --- src/core/elf_loader.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/core/elf_loader.c b/src/core/elf_loader.c index c1906d8a..88a2975e 100644 --- a/src/core/elf_loader.c +++ b/src/core/elf_loader.c @@ -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;