diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2026-09-06 21:11:34 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2026-09-06 21:11:34 +0800 |
| commit | 2430b7b2a7354c31a9b58b553a75c3a685b2e2ce (patch) | |
| tree | ea9afc5d5febe93b923b451b90088c0d843682dd /kernel | |
| parent | d1128b8b4c210acbc48ead1303adb1e7c360359a (diff) | |
uart: more elegant init
earlyprintk: actually init COM1, not sure why the other coms arent
working
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/Cargo.toml | 1 | ||||
| -rw-r--r-- | kernel/src/init/earlyprintk.rs | 7 | ||||
| -rw-r--r-- | kernel/src/io/output/serial/uart_16550.rs | 32 | ||||
| -rw-r--r-- | kernel/src/lib.rs | 1 |
4 files changed, 20 insertions, 21 deletions
diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 10b152b..d44911c 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -11,6 +11,7 @@ bootloader_api = { git = "https://github.com/rust-osdev/bootloader" } bytemuck = { version = "1.25.2", features = ["derive"] } linkme = "0.3.37" spin = "0.12.3" +strum = { version = "0.28.0", default-features = false, features = ["derive", "strum_macros"] } uart_16550 = "0.8.0" [profile.dev] diff --git a/kernel/src/init/earlyprintk.rs b/kernel/src/init/earlyprintk.rs index a217a92..1748620 100644 --- a/kernel/src/init/earlyprintk.rs +++ b/kernel/src/init/earlyprintk.rs @@ -3,11 +3,12 @@ use spin::{lazylock::LazyLock, mutex::Mutex}; use crate::io::output::serial::uart_16550::Uart16550; // TODO: use linkme instead -pub static EARLY_PRINTK_SERIAL: LazyLock<Option<Mutex<Uart16550>>> = - LazyLock::new(|| match unsafe { Uart16550::init([const { None }; 4]) } { +pub static EARLY_PRINTK_SERIAL: LazyLock<Option<Mutex<Uart16550>>> = LazyLock::new(|| { + match unsafe { Uart16550::init([Some(Default::default()), None, None, None]) } { Ok(o) => Some(Mutex::new(o)), Err(_) => None, - }); + } +}); #[macro_export] // TODO: configurable via option or arguments diff --git a/kernel/src/io/output/serial/uart_16550.rs b/kernel/src/io/output/serial/uart_16550.rs index acdebda..2df3668 100644 --- a/kernel/src/io/output/serial/uart_16550.rs +++ b/kernel/src/io/output/serial/uart_16550.rs @@ -1,8 +1,12 @@ +use core::array; + +use strum::{EnumIter, IntoEnumIterator}; use uart_16550::{ Config, Uart16550Tty, Uart16550TtyError, backend::{PioBackend, PortIoAddress}, }; +#[derive(EnumIter)] #[repr(u16)] // TODO: parse & validate via acpi alors scratch enum SerialPortAddress { @@ -35,23 +39,15 @@ impl Uart16550 { // TODO: more type safe mut config: [Option<Config>; 4], ) -> Result<Self, Uart16550TtyError<PortIoAddress>> { - unsafe { - Ok(Self { - coms: [ - init_port(&mut config[0], SerialPortAddress::COM1)?, - init_port(&mut config[1], SerialPortAddress::COM2)?, - init_port(&mut config[2], SerialPortAddress::COM3)?, - init_port(&mut config[3], SerialPortAddress::COM4)?, - ], - }) - } - } -} + let mut addrs = SerialPortAddress::iter(); -#[inline(always)] -unsafe fn init_port( - cfg: &mut Option<Config>, - addr: SerialPortAddress, -) -> Result<Option<Uart16550Tty<PioBackend>>, Uart16550TtyError<PortIoAddress>> { - cfg.take().map(|c| unsafe { Uart16550Tty::new_port(addr.into(), c) }).transpose() + Ok(Self { + coms: array::try_from_fn(|i| { + config[i] + .take() + .map(|c| unsafe { Uart16550Tty::new_port(addrs.next().unwrap().into(), c) }) + .transpose() + })?, + }) + } } diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index ed7bd16..eb1ab68 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -1,4 +1,5 @@ #![no_std] +#![feature(array_try_from_fn)] pub mod init; pub mod io; |
