multiboot.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //! Parsing the Multiboot information. Glue code for the [`multiboot`] code.
  2. use anyhow::anyhow;
  3. use core::slice;
  4. pub use multiboot::information::ModuleIter;
  5. pub use multiboot::information::Multiboot as Mbi;
  6. use multiboot::information::{MemoryManagement, Multiboot, PAddr, SIGNATURE_EAX};
  7. static mut MEMORY_MANAGEMENT: Mem = Mem;
  8. /// Returns an object to access the fields of the Multiboot information
  9. /// structure.
  10. pub fn get_mbi<'a>(magic: u32, ptr: u32) -> anyhow::Result<Multiboot<'a, 'static>> {
  11. if magic != SIGNATURE_EAX {
  12. return Err(anyhow!("Unknown Multiboot signature {magic:x}"));
  13. }
  14. unsafe { Multiboot::from_ptr(ptr as u64, &mut MEMORY_MANAGEMENT) }.ok_or(anyhow!(
  15. "Can't read Multiboot boot information from pointer"
  16. ))
  17. }
  18. /// Glue object between the global allocator and the multiboot crate.
  19. struct Mem;
  20. impl MemoryManagement for Mem {
  21. unsafe fn paddr_to_slice(&self, addr: PAddr, size: usize) -> Option<&'static [u8]> {
  22. let ptr = addr as *const u64 as *const u8;
  23. Some(slice::from_raw_parts(ptr, size))
  24. }
  25. // If you only want to read fields, you can simply return `None`.
  26. unsafe fn allocate(&mut self, _length: usize) -> Option<(PAddr, &mut [u8])> {
  27. None
  28. }
  29. unsafe fn deallocate(&mut self, addr: PAddr) {
  30. if addr != 0 {
  31. unimplemented!()
  32. }
  33. }
  34. }