Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
loader.c
Go to the documentation of this file.
1#include "libk/serial.h"
2#include "libk/stivale2.h"
4#include <init/init.h>
5#include <init/loader.h>
6#include <str.h>
7#include <type.h>
8#include <memory/entry.h>
9
10// ini adalah besar dari stack yang akan digunakan oleh kernel
11static uint8_t stack[4096 * 32] __attribute__((aligned(4096)));
12
13// static struct stivale2_tag l5_tag = {
14// .identifier = STIVALE2_HEADER_TAG_5LV_PAGING_ID, .next = 0};
15
17 .tag = {.identifier = STIVALE2_HEADER_TAG_SMP_ID, .next = 0},
18 .flags = 0};
19
21 .tag = {.identifier = STIVALE2_HEADER_TAG_FRAMEBUFFER_ID,
22 .next = (uint64_t) &smp_hdr_tag},
23 .framebuffer_width = 0,
24 .framebuffer_height = 0,
25 .framebuffer_bpp = 0};
26
27__attribute__((section(".stivale2hdr"),
28 used)) static struct stivale2_header stivale_hdr = {
29 .entry_point = 0,
30 .stack = (uintptr_t) stack + sizeof(stack),
31 .flags = (1 << 1) | (1 << 2),
32 .tags = (uint64_t) &framebuffer_hdr_tag};
33
34__attribute__((noinline)) void*
36 struct stivale2_tag* current_tag =
37 (struct stivale2_tag*) (uintptr_t)
38 stivale2_struct->tags; // uint64_t → uintptr_t → ptr
39
40 for (;;) {
41 if (!current_tag)
42 return NULL;
43 if (current_tag->identifier == id)
44 return current_tag;
45 current_tag =
46 (struct stivale2_tag*) (uintptr_t) current_tag->next;
47 }
48}
49
73
74static void print_guid(struct stivale2_guid* g) {
75 LOG_INFO("KERNEL",
76 "GUID %.8x-%.4x-%.4x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x", g->a,
77 g->b, g->c, g->d[0], g->d[1], g->d[2], g->d[3], g->d[4],
78 g->d[5], g->d[6], g->d[7]);
79}
80
82 init_context_t* ctx) {
83 {
84 struct stivale2_struct_tag_modules* modules_info =
88
89 for (uint64_t i = 0; i < modules_info->module_count; i++) {
90 struct stivale2_module* module =
91 &modules_info->modules[i];
92 if (strncmp(module->string, "boot:///initrd.tar", 18)
93 == 0) {
94 LOG_INFO("INIT", "initrd was found");
95 LOG_INFO("INIT", "initrd size : %d",
96 module->end - module->begin);
97 ctx->initrd_module.size =
98 module->end - module->begin;
99 ctx->initrd_module.start = module->begin;
100 break;
101 }
102 }
103 LOG_INFO("INIT", "initrd module found at 0x%x",
104 ctx->initrd_module.start);
105 }
106
107 // initialize memori allocator
108 {
109 struct stivale2_struct_tag_memmap* memmap_info =
112
113 if (!memmap_info) {
114 serial_trace("FATAL: no memmap tag\n");
115 for (;;)
116 __asm__ volatile("hlt");
117 }
118
119 uint64_t entries = memmap_info->entries;
121 serial_trace("memory entries: %d\n", entries);
122
125
126 for (uint64_t i = 0; i < entries; i++) {
127 volatile struct stivale2_mmap_entry* entry =
128 &memmap_info->memmap[i];
129 ctx->memory.memory_map[i].base = entry->base;
130 ctx->memory.memory_map[i].length = entry->length;
131 ctx->memory.memory_map[i].type =
133 }
134 }
135
136 {
137 struct stivale2_struct_tag_rsdp* rsdp_info =
140 ctx->rsdp_addr = rsdp_info->rsdp;
141 }
142
143 // setup framebuffer
144 {
145 struct stivale2_struct_tag_framebuffer* framebuffer_info =
150
151 if (!framebuffer_info
152 || framebuffer_info->framebuffer_addr == 0) {
153 LOG_WARN("loader", "no graphic detected");
154 } else {
155
156 ctx->framebuffer.framebuffer_addr =
157 framebuffer_info->framebuffer_addr;
158 ctx->framebuffer.framebuffer_width =
159 framebuffer_info->framebuffer_width;
160 ctx->framebuffer.framebuffer_height =
161 framebuffer_info->framebuffer_height;
162 ctx->framebuffer.framebuffer_bpp =
163 framebuffer_info->framebuffer_bpp;
164 ctx->framebuffer.framebuffer_pitch =
165 framebuffer_info->framebuffer_pitch;
166
167 ctx->framebuffer.blue_mask_shift =
168 framebuffer_info->blue_mask_shift;
169 ctx->framebuffer.blue_mask_size =
170 framebuffer_info->blue_mask_size;
171 ctx->framebuffer.green_mask_shift =
172 framebuffer_info->green_mask_shift;
173 ctx->framebuffer.green_mask_size =
174 framebuffer_info->green_mask_size;
175 ctx->framebuffer.red_mask_shift =
176 framebuffer_info->red_mask_shift;
177 ctx->framebuffer.red_mask_size =
178 framebuffer_info->red_mask_size;
179 }
180 }
181
182 // kernel
183 {
184 struct stivale2_struct_tag_kernel_file_v2* kernel_info =
189
190 LOG_INFO("KERNEL", "kernel file addr 0x%x ",
191 kernel_info->kernel_file);
192 LOG_INFO("KERNEL", "kernel file size %d Kb (%d Mb)",
193 kernel_info->kernel_size / 1024,
194 kernel_info->kernel_size / 1024 / 1024);
195 ctx->kernel_raw_addr = VIRT2PHYS(kernel_info->kernel_file);
196 ctx->kernel_raw_size = kernel_info->kernel_size;
197 }
198
199 // boot device
200 {
201 struct stivale2_struct_tag_boot_volume* boot_info =
206
207 print_guid(&boot_info->guid);
208 print_guid(&boot_info->part_guid);
209 }
210}
211
214
216 LOG_INFO("INIT", "run all init");
217 size_t i = 0;
218
220 ++fn, i++)
221 (*fn)(ctx);
222
223 LOG_INFO("INIT", "called %d init function", i);
224}
boolean_t used
Definition ehci.hpp:6
@ ENTRY_MMAP_ACPI_NVS
Definition entry.h:10
@ ENTRY_MMAP_ACPI_RECLAIMABLE
Definition entry.h:9
@ ENTRY_MMAP_KERNEL_AND_MODULES
Definition entry.h:13
@ ENTRY_MMAP_RESERVED
Definition entry.h:8
@ ENTRY_MMAP_FRAMEBUFFER
Definition entry.h:14
@ ENTRY_MMAP_BAD_MEMORY
Definition entry.h:11
@ ENTRY_MMAP_BOOTLOADER_RECLAIMABLE
Definition entry.h:12
@ ENTRY_MMAP_USABLE
Definition entry.h:7
uint64_t stack
Definition thread.h:13
typedef __attribute__
Definition msi.c:47
#define MAX_MEMORY_ENTRIES
Definition init.h:8
void(* initcall_t)(init_context_t *ctx)
Definition init.h:24
initcall_t __init_early_start[]
static uint32_t stivale2_mem_entry_type_converter(uint32_t type)
Definition loader.c:50
void run_all_init_calls(init_context_t *ctx)
Definition loader.c:215
static struct stivale2_header_tag_smp smp_hdr_tag
Definition loader.c:16
static struct stivale2_header_tag_framebuffer framebuffer_hdr_tag
Definition loader.c:20
static void print_guid(struct stivale2_guid *g)
Definition loader.c:74
void build_context_from_stivale2(struct stivale2_struct *stivale2_struct, init_context_t *ctx)
Definition loader.c:81
initcall_t __init_early_end[]
void * stivale2_get_tag(struct stivale2_struct *stivale2_struct, uint64_t id)
#define VIRT2PHYS(x)
#define LOG_WARN(mod, fmt,...)
Definition serial.h:27
#define LOG_INFO(mod, fmt,...)
Definition serial.h:20
#define serial_trace(...)
Definition serial.h:19
#define STIVALE2_HEADER_TAG_FRAMEBUFFER_ID
Definition stivale2.h:54
#define STIVALE2_HEADER_TAG_SMP_ID
Definition stivale2.h:96
#define STIVALE2_MMAP_BAD_MEMORY
Definition stivale2.h:159
#define STIVALE2_STRUCT_TAG_MEMMAP_ID
Definition stivale2.h:153
#define STIVALE2_MMAP_USABLE
Definition stivale2.h:155
#define STIVALE2_STRUCT_TAG_FRAMEBUFFER_ID
Definition stivale2.h:177
#define STIVALE2_MMAP_FRAMEBUFFER
Definition stivale2.h:162
#define STIVALE2_MMAP_RESERVED
Definition stivale2.h:156
#define STIVALE2_MMAP_ACPI_NVS
Definition stivale2.h:158
#define STIVALE2_STRUCT_TAG_MODULES_ID
Definition stivale2.h:230
#define STIVALE2_MMAP_ACPI_RECLAIMABLE
Definition stivale2.h:157
#define STIVALE2_MMAP_KERNEL_AND_MODULES
Definition stivale2.h:161
#define STIVALE2_STRUCT_TAG_KERNEL_FILE_V2_ID
Definition stivale2.h:283
#define STIVALE2_STRUCT_TAG_RSDP_ID
Definition stivale2.h:246
#define STIVALE2_STRUCT_TAG_BOOT_VOLUME_ID
Definition stivale2.h:291
#define STIVALE2_MMAP_BOOTLOADER_RECLAIMABLE
Definition stivale2.h:160
int strncmp(const char *s1, const char *s2, size_t n)
size_t kernel_raw_size
Definition init.h:21
uintptr_t kernel_raw_addr
Definition init.h:20
initrd_module_t initrd_module
Definition init.h:16
framebuffer_t framebuffer
Definition init.h:19
memory_context_t memory
Definition init.h:17
uintptr_t rsdp_addr
Definition init.h:18
size_t size
Definition initrd.h:30
uint64_t start
Definition initrd.h:31
uint64_t memory_entries
Definition init.h:11
memory_entry_t memory_map[256]
Definition init.h:12
uint32_t a
Definition stivale2.h:294
uint16_t c
Definition stivale2.h:296
uint16_t b
Definition stivale2.h:295
uint8_t d[8]
Definition stivale2.h:297
Definition stivale2.h:164
uint32_t type
Definition stivale2.h:167
uint64_t base
Definition stivale2.h:165
uint64_t length
Definition stivale2.h:166
uint64_t end
Definition stivale2.h:234
uint64_t begin
Definition stivale2.h:233
char string[128]
Definition stivale2.h:237
struct stivale2_guid guid
Definition stivale2.h:303
struct stivale2_guid part_guid
Definition stivale2.h:304
struct stivale2_mmap_entry memmap[]
Definition stivale2.h:174
struct stivale2_module modules[]
Definition stivale2.h:243
uint64_t tags
Definition stivale2.h:117
uint64_t identifier
Definition stivale2.h:33
uint64_t next
Definition stivale2.h:34
#define NULL
Definition type.h:76
unsigned int uint32_t
Definition type.h:19
unsigned long uintptr_t
Definition type.h:73
unsigned long uint64_t
Definition type.h:25
unsigned char uint8_t
Definition type.h:7
uint8_t type
Definition vnode.h:2