Skip to content

Commit

Permalink
refactor: move console into os module
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Apr 9, 2024
1 parent 7527f63 commit 28d172d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod macros;
#[cfg(target_os = "none")]
mod allocator;
mod arch;
mod console;
#[cfg(target_os = "none")]
mod log;
mod os;
Expand All @@ -26,5 +25,5 @@ extern crate alloc;
fn _print(args: core::fmt::Arguments<'_>) {
use core::fmt::Write;

console::CONSOLE.lock().write_fmt(args).unwrap();
self::os::CONSOLE.lock().write_fmt(args).unwrap();
}
14 changes: 14 additions & 0 deletions src/os/none/console.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use core::fmt;

use one_shot_mutex::OneShotMutex;

pub struct Console(());

impl fmt::Write for Console {
fn write_str(&mut self, s: &str) -> fmt::Result {
crate::arch::write_to_console(s.as_bytes());
Ok(())
}
}

pub static CONSOLE: OneShotMutex<Console> = OneShotMutex::new(Console(()));
6 changes: 4 additions & 2 deletions src/os/none.rs → src/os/none/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod console;
use core::fmt::Write;
use core::mem::MaybeUninit;
use core::slice;

use hermit_entry::elf::KernelObject;
use log::info;

use crate::{arch, console};
pub use self::console::CONSOLE;
use crate::arch;

extern "C" {
static kernel_end: u8;
Expand Down Expand Up @@ -44,7 +46,7 @@ pub(crate) unsafe extern "C" fn loader_main() -> ! {
#[panic_handler]
fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
// We can't use `println!` or related macros, because `_print` unwraps a result and might panic again
writeln!(console::CONSOLE.lock(), "[LOADER] {info}").ok();
writeln!(crate::os::CONSOLE.lock(), "[LOADER] {info}").ok();

loop {}
}
3 changes: 0 additions & 3 deletions src/console.rs → src/os/uefi/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ pub struct Console(());

impl fmt::Write for Console {
fn write_str(&mut self, s: &str) -> fmt::Result {
#[cfg(target_os = "none")]
crate::arch::write_to_console(s.as_bytes());
#[cfg(target_os = "uefi")]
uefi_services::system_table().stdout().write_str(s)?;
Ok(())
}
Expand Down
3 changes: 3 additions & 0 deletions src/os/uefi.rs → src/os/uefi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
mod console;
use alloc::string::String;
use alloc::vec::Vec;

use qemu_exit::QEMUExit;
use uefi::fs::{FileSystem, Path};
use uefi::prelude::*;

pub use self::console::CONSOLE;

fn read_app(bt: &BootServices) -> Vec<u8> {
let fs = bt
.get_image_file_system(bt.image_handle())
Expand Down

0 comments on commit 28d172d

Please sign in to comment.