Skip to content

Commit

Permalink
memory mapをfileに保存する
Browse files Browse the repository at this point in the history
  • Loading branch information
AtsukiTak committed Mar 31, 2022
1 parent 6e3bb3c commit d8d8891
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 18 deletions.
43 changes: 34 additions & 9 deletions bootloader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
#![no_main]
#![feature(once_cell)]

mod uefi;
mod console;
mod uefi;

use self::uefi::{proto::fs::SimpleFileSystemProtocol, Handle, SystemTable};
use self::uefi::{
proto::{file::OpenMode, fs::SimpleFileSystemProtocol},
Handle, SystemTable,
};
use core::fmt::Write;

#[no_mangle]
extern "win64" fn efi_main(handle: Handle, table: SystemTable) -> usize {
Expand All @@ -14,18 +18,39 @@ extern "win64" fn efi_main(handle: Handle, table: SystemTable) -> usize {

println!("Hello World");

let mut buf = [0u8; 8000];
match table.boot().get_memory_map(&mut buf) {
Ok((_key, mem_map_iter)) => {
for mem_desc in mem_map_iter {
println!("{:?}", mem_desc);
}
}
let mut buf = [0u8; 6000];
let mem_map_iter = match table.boot().get_memory_map(&mut buf) {
Ok((_key, mem_map_iter)) => mem_map_iter,
Err(size) => {
println!("required memory map size : {}", size);
loop {}
}
};

let sfs = table.boot().locate_protocol::<SimpleFileSystemProtocol>();
let mut file = sfs
.open_volume()
.open("mem_map.csv", OpenMode::CreateReadWrite)
.unwrap();

// CSV header
write!(
file,
"type, physical_start, virtual_start, num_pages, attrs\n"
)
.unwrap();

for desc in mem_map_iter {
write!(
file,
"{}, {:x}, {:x}, {}, {:x}\n",
desc.typ, desc.physical_start, desc.virtual_start, desc.number_of_pages, desc.attribute
)
.unwrap();
}

file.flush();

loop {}
}

Expand Down
36 changes: 30 additions & 6 deletions bootloader/src/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use self::proto::{
text::SimpleTextOutputProtocol,
{Guid, Protocol},
};
use core::{ffi::c_void, ptr::NonNull};
use core::{ffi::c_void, ptr::NonNull, fmt};

#[repr(transparent)]
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -174,21 +174,45 @@ pub struct MapKey(usize);
#[repr(C)]
#[derive(Debug)]
pub struct MemoryDescriptor {
typ: u32,
physical_start: PhysicalAddress,
virtual_start: VirtualAddress,
number_of_pages: u64,
attribute: u64,
pub typ: u32,
pub physical_start: PhysicalAddress,
pub virtual_start: VirtualAddress,
pub number_of_pages: u64,
pub attribute: u64,
}

#[repr(transparent)]
#[derive(Debug)]
pub struct PhysicalAddress(u64);

impl fmt::Display for PhysicalAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl fmt::LowerHex for PhysicalAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:x}", self.0)
}
}

#[repr(transparent)]
#[derive(Debug)]
pub struct VirtualAddress(u64);

impl fmt::Display for VirtualAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl fmt::LowerHex for VirtualAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:x}", self.0)
}
}

pub struct MemoryMapIter<'buf> {
buf: &'buf mut [u8],
// size of memory descriptor in bytes
Expand Down
33 changes: 30 additions & 3 deletions bootloader/src/uefi/proto/file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::ffi::c_void;
use core::{ffi::c_void, fmt};

#[repr(C)]
pub struct FileProtocol {
Expand Down Expand Up @@ -30,7 +30,7 @@ pub struct FileProtocol {
}

impl FileProtocol {
pub fn open(&self, file_name: &str, mode: u64) -> Option<&'static FileProtocol> {
pub fn open(&self, file_name: &str, mode: OpenMode) -> Option<&'static FileProtocol> {
let mut new_handle: *mut FileProtocol = core::ptr::null_mut();

let mut file_name_c16 = [0u16; 32];
Expand All @@ -40,7 +40,13 @@ impl FileProtocol {
}

unsafe {
(self.open)(self, &mut new_handle, file_name_c16.as_ptr(), mode, 0);
(self.open)(
self,
&mut new_handle,
file_name_c16.as_ptr(),
mode as u64,
0,
);
if new_handle.is_null() {
return None;
} else {
Expand All @@ -60,3 +66,24 @@ impl FileProtocol {
unsafe { (self.flush)(self) }
}
}

impl fmt::Write for FileProtocol {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write(s);
Ok(())
}
}

impl fmt::Write for &FileProtocol {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write(s);
Ok(())
}
}

#[repr(u64)]
pub enum OpenMode {
Read = 0x01,
ReadWrite = 0x01 | 0x02,
CreateReadWrite = (1 << 63) | 0x01 | 0x02,
}

0 comments on commit d8d8891

Please sign in to comment.