Compare commits
8 Commits
add-serial
...
unit-tests
Author | SHA1 | Date | |
---|---|---|---|
1a41d087a9
|
|||
bd035f1c9a
|
|||
548acbbb5e
|
|||
4605190a4e
|
|||
cd817d2013
|
|||
a91d52890a
|
|||
34283780c0
|
|||
f1e6ef14b2
|
20
LICENSE
20
LICENSE
@@ -1,18 +1,18 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Noah Knegt <personal@noahknegt.com>
|
||||
Copyright (c) 2025 noah.knegt
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
associated documentation files (the "Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
associated documentation files (the "Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
|
||||
following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial
|
||||
portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
||||
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
||||
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
||||
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
||||
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
8
build.rs
8
build.rs
@@ -9,11 +9,15 @@ fn main() {
|
||||
|
||||
// create an UEFI disk image (optional)
|
||||
let uefi_path = out_dir.join("uefi.img");
|
||||
bootloader::UefiBoot::new(&kernel).create_disk_image(&uefi_path).unwrap();
|
||||
bootloader::UefiBoot::new(&kernel)
|
||||
.create_disk_image(&uefi_path)
|
||||
.unwrap();
|
||||
|
||||
// create a BIOS disk image
|
||||
let bios_path = out_dir.join("bios.img");
|
||||
bootloader::BiosBoot::new(&kernel).create_disk_image(&bios_path).unwrap();
|
||||
bootloader::BiosBoot::new(&kernel)
|
||||
.create_disk_image(&bios_path)
|
||||
.unwrap();
|
||||
|
||||
// pass the disk image paths as env variables to the `main.rs`
|
||||
println!("cargo:rustc-env=UEFI_PATH={}", uefi_path.display());
|
||||
|
13
kernel/src/adder.rs
Normal file
13
kernel/src/adder.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
pub fn add(a: i32, b: i32) -> i32 {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn add_test() {
|
||||
assert_eq!(add(1, 2), 3);
|
||||
}
|
||||
}
|
30
kernel/src/lib.rs
Normal file
30
kernel/src/lib.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
#![cfg_attr(not(test), no_std)]
|
||||
|
||||
use bootloader_api::BootInfo;
|
||||
|
||||
mod logging;
|
||||
mod adder;
|
||||
|
||||
pub fn main(boot_info: &'static mut BootInfo) -> ! {
|
||||
let info = boot_info.framebuffer.as_ref().unwrap().info();
|
||||
let buffer = boot_info.framebuffer.as_mut().unwrap().buffer_mut();
|
||||
|
||||
logging::init_logger(buffer, info);
|
||||
|
||||
log::info!("Hello World from KERNEL");
|
||||
log::debug!("ADDING 1 & 6, result == {}", adder::add(1, 6));
|
||||
|
||||
// Endless loop as the kernel must stay running
|
||||
#[allow(clippy::empty_loop)]
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
#[panic_handler]
|
||||
fn panic(info: &core::panic::PanicInfo) -> ! {
|
||||
logging::force_unlock();
|
||||
log::error!("{info}");
|
||||
|
||||
#[allow(clippy::empty_loop)]
|
||||
loop {}
|
||||
}
|
@@ -2,7 +2,7 @@ use bootloader_api::info::{FrameBufferInfo, PixelFormat};
|
||||
use core::{fmt, ptr};
|
||||
use font_constants::BACKUP_CHAR;
|
||||
use noto_sans_mono_bitmap::{
|
||||
get_raster, get_raster_width, FontWeight, RasterHeight, RasterizedChar,
|
||||
FontWeight, RasterHeight, RasterizedChar, get_raster, get_raster_width,
|
||||
};
|
||||
|
||||
/// Additional vertical space between lines
|
||||
@@ -130,7 +130,7 @@ impl FrameBufferWriter {
|
||||
// set a supported (but invalid) pixel format before panicking to avoid a double
|
||||
// panic; it might not be readable though
|
||||
self.info.pixel_format = PixelFormat::Rgb;
|
||||
panic!("pixel format {:?} not supported in logger", other)
|
||||
panic!("pixel format {other:?} not supported in logger")
|
||||
}
|
||||
};
|
||||
let bytes_per_pixel = self.info.bytes_per_pixel;
|
@@ -11,21 +11,21 @@ static LOGGER: OnceCell<LockedLogger> = OnceCell::uninit();
|
||||
/// A logger instance protected by a spinlock.
|
||||
struct LockedLogger {
|
||||
framebuffer: Option<Spinlock<framebuffer::FrameBufferWriter>>,
|
||||
serial: Option<Spinlock<serial::SerialPort>>
|
||||
serial: Option<Spinlock<serial::SerialPort>>,
|
||||
}
|
||||
|
||||
impl LockedLogger {
|
||||
/// Create a new instance that logs to the given framebuffer.
|
||||
pub fn new(
|
||||
framebuffer: &'static mut [u8],
|
||||
info: FrameBufferInfo,
|
||||
) -> Self {
|
||||
let framebuffer = Some(Spinlock::new(framebuffer::FrameBufferWriter::new(framebuffer, info)));
|
||||
pub fn new(framebuffer: &'static mut [u8], info: FrameBufferInfo) -> Self {
|
||||
let framebuffer = Some(Spinlock::new(framebuffer::FrameBufferWriter::new(
|
||||
framebuffer,
|
||||
info,
|
||||
)));
|
||||
let serial = Some(Spinlock::new(unsafe { serial::SerialPort::init() }));
|
||||
|
||||
LockedLogger {
|
||||
framebuffer,
|
||||
serial
|
||||
serial,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,16 +65,8 @@ impl log::Log for LockedLogger {
|
||||
}
|
||||
|
||||
/// Initialize a text-based logger using the given pixel-based framebuffer as output.
|
||||
pub fn init_logger(
|
||||
framebuffer: &'static mut [u8],
|
||||
info: FrameBufferInfo,
|
||||
) {
|
||||
let logger = LOGGER.get_or_init(move || {
|
||||
LockedLogger::new(
|
||||
framebuffer,
|
||||
info,
|
||||
)
|
||||
});
|
||||
pub fn init_logger(framebuffer: &'static mut [u8], info: FrameBufferInfo) {
|
||||
let logger = LOGGER.get_or_init(move || LockedLogger::new(framebuffer, info));
|
||||
log::set_logger(logger).expect("logger already set");
|
||||
#[cfg(debug_assertions)]
|
||||
log::set_max_level(log::LevelFilter::Debug);
|
||||
@@ -82,14 +74,9 @@ pub fn init_logger(
|
||||
#[cfg(not(debug_assertions))]
|
||||
log::set_max_level(log::LevelFilter::Info);
|
||||
|
||||
log::debug!("Framebuffer info: {:?}", info);
|
||||
log::debug!("Framebuffer info: {info:?}");
|
||||
}
|
||||
|
||||
pub fn force_unlock() {
|
||||
unsafe {
|
||||
LOGGER
|
||||
.get()
|
||||
.map(|l| l.force_unlock())
|
||||
};
|
||||
unsafe { LOGGER.get().map(|l| l.force_unlock()) };
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
pub mod logger;
|
||||
|
||||
mod framebuffer;
|
||||
mod serial;
|
||||
|
@@ -3,35 +3,13 @@
|
||||
|
||||
use bootloader_api::{
|
||||
config::{BootloaderConfig, Mapping},
|
||||
entry_point, BootInfo
|
||||
entry_point,
|
||||
};
|
||||
|
||||
mod logger;
|
||||
|
||||
pub static BOOTLOADER_CONFIG: BootloaderConfig = {
|
||||
static BOOTLOADER_CONFIG: BootloaderConfig = {
|
||||
let mut config = BootloaderConfig::new_default();
|
||||
config.mappings.physical_memory = Some(Mapping::Dynamic);
|
||||
config
|
||||
};
|
||||
|
||||
entry_point!(kernel_main);
|
||||
|
||||
fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
|
||||
let info = boot_info.framebuffer.as_ref().unwrap().info();
|
||||
let buffer = boot_info.framebuffer.as_mut().unwrap().buffer_mut();
|
||||
|
||||
logger::init_logger(buffer, info);
|
||||
|
||||
log::info!("Hello World from KERNEL");
|
||||
|
||||
// Endless loop as the kernel must stay running
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[panic_handler]
|
||||
#[cfg(not(test))]
|
||||
fn panic(info: &core::panic::PanicInfo) -> ! {
|
||||
logger::force_unlock();
|
||||
log::error!("{}", info);
|
||||
loop {}
|
||||
}
|
||||
entry_point!(kernel::main, config = &BOOTLOADER_CONFIG);
|
||||
|
@@ -1,7 +1,5 @@
|
||||
[toolchain]
|
||||
# The nightly version that was build on 24-07-2025 is the last known working version,
|
||||
# the c-int-width definition for rust targets has changed from a string to a u16
|
||||
channel = "nightly-2025-07-24"
|
||||
channel = "nightly"
|
||||
targets = [
|
||||
"x86_64-unknown-none"
|
||||
]
|
||||
|
@@ -17,9 +17,6 @@ fn main() {
|
||||
cmd.arg("-drive")
|
||||
.arg(format!("format=raw,file={bios_path}"));
|
||||
}
|
||||
|
||||
cmd.arg("-serial").arg("stdio");
|
||||
|
||||
let mut child = cmd.spawn().unwrap();
|
||||
child.wait().unwrap();
|
||||
}
|
||||
|
Reference in New Issue
Block a user