diff options
| author | Tianhao Wang <shrik3@mailbox.org> | 2024-05-29 05:41:51 +0200 |
|---|---|---|
| committer | Tianhao Wang <shrik3@mailbox.org> | 2024-06-11 15:17:11 +0200 |
| commit | 0234279ca861179d73ea125b947acc9baacb31ec (patch) | |
| tree | bc6ae49ae2b5d5cace677913c9f7012e58d1ab2e | |
| parent | 890bac538171d231d1dccfb4b28091d016ec6118 (diff) | |
multiboot: parse mmap blocks
Signed-off-by: Tianhao Wang <shrik3@mailbox.org>
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/machine/multiboot.rs | 55 |
2 files changed, 56 insertions, 1 deletions
@@ -34,6 +34,8 @@ pub extern "C" fn _entry() -> ! { let mmap = unsafe { mbi.get_mmap() }.unwrap(); println!("memory: {:#X?}", mem); println!("mmap (start): {:#X?}", mmap); + + multiboot::_test_mmap(); interrupt::init(); pic_8259::allow(PicDeviceInt::KEYBOARD); interrupt::interrupt_enable(); diff --git a/src/machine/multiboot.rs b/src/machine/multiboot.rs index c420c2b..885e104 100644 --- a/src/machine/multiboot.rs +++ b/src/machine/multiboot.rs @@ -1,4 +1,6 @@ use crate::io::*; +use core::fmt; +use core::mem::size_of; use lazy_static::lazy_static; // provide functions to parse information provided by grub multiboot // see docs/multiboot.txt @@ -24,7 +26,6 @@ pub fn check() -> bool { #[repr(C)] #[repr(packed)] -#[derive(Debug)] pub struct MultibootMmap { pub size: u32, pub addr: u64, @@ -32,6 +33,41 @@ pub struct MultibootMmap { pub mtype: u32, } +impl fmt::Debug for MultibootMmap { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let addr = self.addr; + let len = self.len; + let mtype = self.mtype; + write!( + f, + "[{}] @ {:#X} + {:#X}", + match mtype { + MultibootMmap::MTYPE_RAM => "GOOD", + MultibootMmap::MTYPE_RAM_RES => "RESV", + MultibootMmap::MTYPE_ACPI => "ACPI", + MultibootMmap::MTYPE_RAM_NVS => "NVS ", + MultibootMmap::MTYPE_RAM_DEFECT => "BAD ", + _ => "UNKN", + }, + addr, + len, + ) + } +} + +impl MultibootMmap { + /// avaialble ram + pub const MTYPE_RAM: u32 = 1; + /// reserved ram + pub const MTYPE_RAM_RES: u32 = 2; + /// usable memory holding ACPI info + pub const MTYPE_ACPI: u32 = 3; + /// WHAT IS THIS 4??? + pub const MTYPE_RAM_NVS: u32 = 4; + /// defective RAM + pub const MTYPE_RAM_DEFECT: u32 = 5; +} + #[repr(C)] #[repr(packed)] #[derive(Debug, Clone, Copy)] @@ -40,6 +76,23 @@ pub struct MultibootInfoMmap { pub mmap_addr: u32, } +pub fn _test_mmap() { + let mmapinfo = unsafe { MBOOTINFO.get_mmap() }.unwrap(); + let buf_start = mmapinfo.mmap_addr; + let buf_len = mmapinfo.mmap_length; + let buf_end = buf_start + buf_len; + let mut curr = buf_start as u64; + loop { + if curr >= buf_end as u64 { + break; + } + let mblock = unsafe { &*(curr as *const MultibootMmap) }; + curr += mblock.size as u64; + curr += 4; // mmap.size does not include the the size itself + println!("mem block {:#X?}", mblock); + } +} + #[repr(C)] #[repr(packed)] #[derive(Debug, Clone, Copy)] |
