Skip to content

Commit

Permalink
split lib.rs and main.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
12101111 committed Jun 30, 2019
1 parent 3cc400e commit 2c8294c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 49 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,3 @@ raw-cpuid = "6.1"

fbterm = { git = "https://github.com/12101111/fbterm"}
uart_16550 = { git = "https://github.com/12101111/uart_16550"}

[package.metadata.cargo-xbuild]
memcpy = true
sysroot_path = "target/sysroot"
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly
40 changes: 40 additions & 0 deletions src/drivers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
pub mod acpi;
pub mod console;
pub mod uefifb;

use acpi::rsdp;
use console::{fbterm::init_fbterm, init_logger, uart::init_uart};
use uefifb::init_fb;
use uefi::{
prelude::*,
table::boot::{MemoryDescriptor, MemoryType},
};

pub fn init(image: uefi::Handle, st: SystemTable<Boot>){
let fb = init_fb(&st);
init_logger();
init_fbterm(fb);
init_uart();
let rsdp = rsdp(&st);
let buffer = alloc_mmap(&st);
let (st, map) = st
.exit_boot_services(image, &mut buffer[..])
.expect_success("Failed to exit boot services");
let _rt = unsafe { st.runtime_services() };
for mem in map {
trace!(
"mem: 0x{:016X} Size(Page): {:8} Type: {:?}",
mem.phys_start,
mem.page_count,
mem.ty
);
}
trace!("RDSP: {:?}", rsdp);
}

fn alloc_mmap(st: &SystemTable<Boot>) -> &'static mut [u8] {
let bt = st.boot_services();
let mmap_size =
st.boot_services().memory_map_size() + 8 * core::mem::size_of::<MemoryDescriptor>();
let mmap_buffer = bt
.allocate_pool(MemoryType::LOADER_DATA, mmap_size)
.expect_success("alloc failed");
unsafe { core::slice::from_raw_parts_mut(mmap_buffer, mmap_size) }
}
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![no_std]
#![cfg_attr(test, no_main)]
#![feature(custom_test_frameworks)]
#![feature(decl_macro)]

pub mod drivers;
pub mod kmain;
#[macro_use]
extern crate log;
52 changes: 7 additions & 45 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,21 @@
#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_main)]
#![no_std]
#![no_main]
#![feature(decl_macro)]

pub mod drivers;
pub mod kmain;
#[macro_use]
extern crate log;

use drivers::acpi::rsdp;
use drivers::console::{fbterm::init_fbterm, init_logger, uart::init_uart};
use drivers::uefifb::init_fb;
use kmain::kmain;
use uefi::{
prelude::*,
table::boot::{MemoryDescriptor, MemoryType},
};
use os::kmain::kmain;
use os::drivers::init;
use uefi::prelude::*;

#[no_mangle]
pub extern "C" fn efi_main(image: uefi::Handle, st: SystemTable<Boot>) -> ! {
let fb = init_fb(&st);
init_logger();
init_fbterm(fb);
init_uart();
let rsdp = rsdp(&st);
let buffer = alloc_mmap(&st);
let (st, map) = st
.exit_boot_services(image, &mut buffer[..])
.expect_success("Failed to exit boot services");
let _rt = unsafe { st.runtime_services() };
for mem in map {
trace!(
"mem: 0x{:016X} Size(Page): {:8} Type: {:?}",
mem.phys_start,
mem.page_count,
mem.ty
);
}
trace!("RDSP: {:?}", rsdp);
init(image,st);
kmain()
}

fn alloc_mmap(st: &SystemTable<Boot>) -> &'static mut [u8] {
let bt = st.boot_services();
let mmap_size =
st.boot_services().memory_map_size() + 8 * core::mem::size_of::<MemoryDescriptor>();
let mmap_buffer = bt
.allocate_pool(MemoryType::LOADER_DATA, mmap_size)
.expect_success("alloc failed");
unsafe { core::slice::from_raw_parts_mut(mmap_buffer, mmap_size) }
}

#[cfg(not(test))]
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
error!("{}", info);
log::error!("{}", info);
x86_64::instructions::hlt();
loop {}
}

0 comments on commit 2c8294c

Please sign in to comment.