Add kernel-side protocol implementation and split it from Magic

This commit is contained in:
Mark 2020-09-21 18:28:23 +03:00
parent 7be4052928
commit 1591988e76
6 changed files with 80 additions and 19 deletions

View File

@ -1,13 +0,0 @@
use crate::ProtoV1;
pub trait KernelProtocol: Sized {
const KERNEL_MAGIC: [u8; 8];
}
#[cfg(feature = "kernel-protocol")]
impl KernelProtocol for ProtoV1 {
const KERNEL_MAGIC: [u8; 8] = [
0x07, 0xB0, 0x07, 0xB0, 0xA9, 0x97, 0xA1, 0x00
];
}

View File

@ -7,15 +7,16 @@ pub mod video;
pub mod load;
#[cfg(feature = "kernel-protocol")]
pub mod kernel;
pub mod magic;
pub mod v1;
pub use v1::ProtoV1;
pub use video::VideoInfo;
pub use mmap::MemoryMapInfo;
pub use magic::Magic;
#[cfg(feature = "load-protocol")]
pub use load::LoadProtocol;
#[cfg(feature = "kernel-protocol")]
pub use kernel::KernelProtocol;
pub const FLAG_VIDEO: u32 = 1 << 0;
pub const FLAG_INITRD: u32 = 1 << 1;

View File

@ -1,5 +1,6 @@
use core::convert::TryInto;
use crate::{Error, VideoInfo, MemoryMapInfo, ProtoV1};
use crate::magic::Magic;
pub trait LoadProtocol: Sized {
fn get_flags(&self) -> u32;
@ -13,10 +14,7 @@ pub trait LoadProtocol: Sized {
impl LoadProtocol for ProtoV1 {
fn set_loader_magic(&mut self) {
const LOADER_MAGIC: [u8; 8] = [
0x1A, 0x79, 0x9A, 0x0B, 0x70, 0x0B, 0x70, 0x00
];
self.hdr.loader_magic = LOADER_MAGIC;
self.hdr.loader_magic = Self::LOADER_MAGIC;
}
fn get_video_info(&self) -> &VideoInfo {
&self.video

15
src/magic.rs Normal file
View File

@ -0,0 +1,15 @@
use crate::ProtoV1;
pub trait Magic: Sized {
const KERNEL_MAGIC: [u8; 8];
const LOADER_MAGIC: [u8; 8];
}
impl Magic for ProtoV1 {
const KERNEL_MAGIC: [u8; 8] = [
0x07, 0xB0, 0x07, 0xB0, 0xA9, 0x97, 0xA1, 0x00
];
const LOADER_MAGIC: [u8; 8] = [
0x1A, 0x79, 0x9A, 0x0B, 0x70, 0x0B, 0x70, 0x00
];
}

View File

@ -14,3 +14,6 @@ impl MemoryMapInfo {
}
}
}
#[cfg(feature = "kernel-protocol")]
pub mod r#impl;

57
src/mmap/impl.rs Normal file
View File

@ -0,0 +1,57 @@
use crate::MemoryMapInfo;
// TODO: I don't want to bring the whole
// EFI module here, so I'll just
// leave a single struct here
#[repr(C)]
pub struct MemoryDescriptor {
pub _type: u32,
pub physical_start: usize,
pub virtual_start: usize,
pub number_of_pages: u64,
pub attribute: u64
}
pub struct MemoryMapIterator {
index: usize,
base: usize,
entsize: usize,
limit: usize
}
impl MemoryDescriptor {
pub fn begin(&self) -> usize {
self.physical_start
}
pub fn end(&self) -> usize {
self.physical_start + (self.number_of_pages as usize) * 0x1000
}
pub fn is_usable(&self) -> bool {
self._type == 7
}
}
impl Iterator for MemoryMapIterator {
type Item = &'static MemoryDescriptor;
fn next(&mut self) -> Option<Self::Item> {
if self.index == self.limit {
return None;
}
let res = unsafe {&*((self.base + self.index * self.entsize) as *const MemoryDescriptor)};
self.index += 1;
Some(res)
}
}
impl MemoryMapInfo {
pub fn iter(&self, upper: bool) -> MemoryMapIterator {
MemoryMapIterator {
limit: (self.size / self.entsize) as usize,
entsize: self.entsize as usize,
base: self.address as usize + if upper { 0xFFFFFF0000000000 } else { 0 },
index: 0
}
}
}