Avoid overwriting bootloader data
This commit is contained in:
parent
64df5d2b45
commit
60bb1bd039
@ -1,6 +1,7 @@
|
||||
#include <stdint.h>
|
||||
#include <multiboot.h>
|
||||
#include <memory.h>
|
||||
#include <debug.h>
|
||||
|
||||
// https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html
|
||||
|
||||
@ -39,7 +40,7 @@ struct fbinfo {
|
||||
uint8_t data[];
|
||||
}__attribute__((packed));
|
||||
|
||||
int parse_multiboot2(struct taglist *tags)
|
||||
static int parse_multiboot2(struct taglist *tags)
|
||||
{
|
||||
struct mmap *mmap;
|
||||
|
||||
@ -96,3 +97,17 @@ int multiboot_get_memory_area(size_t index, uintptr_t *start, uintptr_t *end, ui
|
||||
*type = entry->type;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int multiboot_page_used(uintptr_t page)
|
||||
{
|
||||
#define within_page(st, l) (((uintptr_t)st + l) > page && (uintptr_t)st < (page + PAGE_SIZE))
|
||||
if(
|
||||
within_page(kernel_boot_data.bootloader, strlen(kernel_boot_data.bootloader)) ||
|
||||
within_page(kernel_boot_data.commandline, strlen(kernel_boot_data.commandline)) ||
|
||||
within_page(kernel_boot_data.mmap, kernel_boot_data.mmap_size) ||
|
||||
0) {
|
||||
debug("Page %x used\n", page);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -30,4 +30,5 @@ extern struct kernel_boot_data_st kernel_boot_data;
|
||||
// boot/multiboot.c
|
||||
int multiboot_init(uint64_t magic, void *mboot_info);
|
||||
int multiboot_get_memory_area(size_t index, uintptr_t *start, uintptr_t *end, uint32_t *type);
|
||||
int multiboot_page_used(uintptr_t page);
|
||||
#endif
|
@ -19,9 +19,6 @@ void memory_init()
|
||||
|
||||
for(uint64_t p = start; p < end; p += PAGE_SIZE)
|
||||
{
|
||||
if(p >= V2P(&kernel_start) && p < V2P(&kernel_end))
|
||||
continue;
|
||||
|
||||
uint64_t vaddr = (uint64_t)P2V(p);
|
||||
uint64_t page = vmm_get_page(kernel_P4, vaddr);
|
||||
if(page == (uint64_t)-1 || !(page & PAGE_PRESENT))
|
||||
@ -30,7 +27,13 @@ void memory_init()
|
||||
vmm_set_page(kernel_P4, vaddr, p, flags);
|
||||
}
|
||||
|
||||
if(type == MMAP_FREE)
|
||||
if(type != MMAP_FREE)
|
||||
continue;
|
||||
if(p >= V2P(&kernel_start) && p < V2P(&kernel_end))
|
||||
continue;
|
||||
if(multiboot_page_used((uintptr_t)P2V(p)))
|
||||
continue;
|
||||
|
||||
pmm_free(p);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user