Skip to content

Commit e7ae438

Browse files
committed
feat: converted stage_4 of bootloader to rust
1 parent 2674dd2 commit e7ae438

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

src/main.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ global_asm!(include_str!("stage_1.s"));
3030
global_asm!(include_str!("stage_2.s"));
3131
global_asm!(include_str!("e820.s"));
3232
global_asm!(include_str!("stage_3.s"));
33-
global_asm!(include_str!("stage_4.s"));
3433
global_asm!(include_str!("context_switch.s"));
3534

3635
#[cfg(feature = "vga_320x200")]
@@ -63,8 +62,44 @@ impl IdentityMappedAddr {
6362
}
6463
}
6564

65+
// Symbols defined in `linker.ld`
66+
extern {
67+
static mmap_ent: usize;
68+
static _memory_map: usize;
69+
static _kib_kernel_size: usize;
70+
static __page_table_start: usize;
71+
static __page_table_end: usize;
72+
static __bootloader_end: usize;
73+
static __bootloader_start: usize;
74+
}
75+
6676
#[no_mangle]
67-
pub extern "C" fn load_elf(
77+
pub unsafe extern "C" fn stage_4() -> ! {
78+
asm!("mov bx, 0x0" :::: "intel");
79+
asm!("mov ss, bx" :::: "intel"); // Set stack segment
80+
81+
let kernel_start = 0x400000;
82+
let kernel_size = _kib_kernel_size as u64;
83+
let memory_map_addr = &_memory_map as *const _ as u64;
84+
let memory_map_entry_count = (mmap_ent & 0xff) as u64; // Extract lower 8 bits
85+
let page_table_start = &__page_table_start as *const _ as u64;
86+
let page_table_end = &__page_table_end as *const _ as u64;
87+
let bootloader_start = &__bootloader_start as *const _ as u64;
88+
let bootloader_end = &__bootloader_end as *const _ as u64;
89+
90+
load_elf(
91+
IdentityMappedAddr(PhysAddr::new(kernel_start)),
92+
kernel_size,
93+
VirtAddr::new(memory_map_addr),
94+
memory_map_entry_count,
95+
PhysAddr::new(page_table_start),
96+
PhysAddr::new(page_table_end),
97+
PhysAddr::new(bootloader_start),
98+
PhysAddr::new(bootloader_end),
99+
)
100+
}
101+
102+
fn load_elf(
68103
kernel_start: IdentityMappedAddr,
69104
kernel_size: u64,
70105
memory_map_addr: VirtAddr,

src/stage_4.s

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)