PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
boot_info.c
Go to the documentation of this file.
2
3#include <kernel/log/log.h>
4#include <kernel/log/panic.h>
6#include <kernel/mem/pmm.h>
7
8#include <boot/boot_info.h>
9
10#include <stdlib.h>
11#include <string.h>
12
13// Set in _start()
15
17{
18 return bootInfo;
19}
20
22{
23 // Pop everything from the lists and then readd them with higher half addresses.
24 list_t children = LIST_CREATE(children);
26
27 list_t* dirChildren = (list_t*)PML_ENSURE_LOWER_HALF(&dir->children);
28 list_t* dirFiles = (list_t*)PML_ENSURE_LOWER_HALF(&dir->files);
29
30 while (!list_is_empty(dirChildren))
31 {
32 boot_dir_t* child = CONTAINER_OF(list_pop_first(dirChildren), boot_dir_t, entry);
33
34 child = (boot_dir_t*)PML_ENSURE_HIGHER_HALF(child);
35 child->entry = LIST_ENTRY_CREATE(child->entry);
36
37 list_push_back(&children, &child->entry);
38 }
39
40 while (!list_is_empty(dirFiles))
41 {
43
45 file->entry = LIST_ENTRY_CREATE(file->entry);
46 file->data = (void*)PML_ENSURE_HIGHER_HALF(file->data);
47
48 list_push_back(&files, &file->entry);
49 }
50
51 dir->children = LIST_CREATE(dir->children);
52 dir->files = LIST_CREATE(dir->files);
53
54 while (!list_is_empty(&children))
55 {
56 boot_dir_t* child = CONTAINER_OF_SAFE(list_pop_first(&children), boot_dir_t, entry);
57 list_push_back(&dir->children, &child->entry);
59 }
60
61 while (!list_is_empty(&files))
62 {
64 list_push_back(&dir->files, &file->entry);
65 }
66}
67
86
88{
89 // The memory map will be stored in the data we are freeing so we copy it first.
90 boot_memory_map_t volatile mapCopy = bootInfo->memory.map;
92 EFI_MEMORY_DESCRIPTOR* volatile descriptorsCopy = malloc(descriptorsSize);
93 if (descriptorsCopy == NULL)
94 {
95 panic(NULL, "Failed to allocate memory for boot memory map copy");
96 }
97 memcpy_s(descriptorsCopy, descriptorsSize, bootInfo->memory.map.descriptors, descriptorsSize);
98 mapCopy.descriptors = (EFI_MEMORY_DESCRIPTOR* const)descriptorsCopy;
99
100 for (uint64_t i = 0; i < mapCopy.length; i++)
101 {
102 const EFI_MEMORY_DESCRIPTOR* desc = BOOT_MEMORY_MAP_GET_DESCRIPTOR(&mapCopy, i);
103
104 if (desc->Type == EfiLoaderData)
105 {
106 LOG_INFO("free boot memory [0x%016lx-0x%016lx]\n", desc->VirtualStart,
107 (uintptr_t)desc->VirtualStart + (desc->NumberOfPages * PAGE_SIZE));
108#ifndef NDEBUG
109 // Clear the memory to deliberately cause corruption if the memory is actually being used.
110 memset((void*)desc->VirtualStart, 0xCC, desc->NumberOfPages * PAGE_SIZE);
111#endif
112 pmm_free_region((void*)desc->VirtualStart, desc->NumberOfPages);
113 }
114 }
115
116 free(descriptorsCopy);
117
118 bootInfo = NULL;
119}
boot_info_t * bootInfo
Definition boot_info.c:14
static void boot_dir_to_higher_half(boot_dir_t *dir)
Definition boot_info.c:21
#define BOOT_MEMORY_MAP_GET_DESCRIPTOR(map, index)
Definition boot_info.h:52
void boot_info_to_higher_half(void)
Offset all pointers in the boot info structure to the higher half.
Definition boot_info.c:68
boot_info_t * boot_info_get(void)
Gets the boot info structure.
Definition boot_info.c:16
void boot_info_free(void)
Frees the boot info structure and all its associated data.
Definition boot_info.c:87
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
Definition panic.c:266
#define LOG_INFO(format,...)
Definition log.h:106
#define PML_ENSURE_HIGHER_HALF(addr)
Ensures that the given address is in the higher half of the address space.
#define PML_ENSURE_LOWER_HALF(addr)
Ensures that the given address is in the lower half of the address space.
void pmm_free_region(void *address, uint64_t count)
Frees a contiguous region of physical pages.
Definition pmm.c:235
static list_entry_t * list_pop_first(list_t *list)
Pops the first entry from the list.
Definition list.h:375
static void list_push_back(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
Definition list.h:343
#define LIST_CREATE(name)
Creates a list initializer.
Definition list.h:174
static bool list_is_empty(list_t *list)
Checks if a list is empty.
Definition list.h:227
#define LIST_ENTRY_CREATE(name)
Creates a list entry initializer.
Definition list.h:162
#define PAGE_SIZE
The size of a memory page in bytes.
Definition proc.h:106
#define NULL
Pointer error value.
Definition NULL.h:23
#define CONTAINER_OF(ptr, type, member)
Container of macro.
#define CONTAINER_OF_SAFE(ptr, type, member)
Safe container of macro.
static list_t files
Definition file.c:9
static dentry_t * file
Definition log_file.c:22
errno_t memcpy_s(void *_RESTRICT s1, rsize_t s1max, const void *_RESTRICT s2, rsize_t n)
Definition memcpy_s.c:9
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:43
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
_PUBLIC void * memset(void *s, int c, size_t n)
Definition memset.c:4
list_t files
Definition boot_info.h:77
list_t children
Definition boot_info.h:76
list_entry_t entry
Definition boot_info.h:74
boot_dir_t * root
Definition boot_info.h:82
uint32_t * virtAddr
Definition boot_info.h:45
uint32_t * physAddr
Definition boot_info.h:44
boot_memory_t memory
Definition boot_info.h:106
void * runtimeServices
Definition boot_info.h:103
boot_disk_t disk
Definition boot_info.h:104
boot_gop_t gop
Definition boot_info.h:101
void * rsdp
Definition boot_info.h:102
boot_kernel_t kernel
Definition boot_info.h:105
void * physAddr
Definition boot_info.h:90
uint64_t length
Definition boot_info.h:58
EFI_MEMORY_DESCRIPTOR * descriptors
Definition boot_info.h:57
boot_memory_map_t map
Definition boot_info.h:95
A doubly linked list.
Definition list.h:49