diff --git a/.gitignore b/.gitignore index d01bd1a..7adf114 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,9 @@ Cargo.lock # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ \ No newline at end of file +#.idea/ + +# Generated grub files +kernel/grub/ +aphrodite-grub.iso +aphrodite.iso \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 24db3d8..08771cf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,7 +2,6 @@ "rust-analyzer.cargo.allTargets": false, "rust-analyzer.check.allTargets": false, "rust-analyzer.linkedProjects": [ - "kernel/Cargo.toml", - "patcher/Cargo.toml" + "kernel/Cargo.toml" ] } \ No newline at end of file diff --git a/emulation/bochsrc b/emulation/bochsrc index 6412e97..2b51c95 100644 --- a/emulation/bochsrc +++ b/emulation/bochsrc @@ -1,3 +1,6 @@ display_library: x, options="gui_debug" port_e9_hack: enabled=1 -cpu: reset_on_triple_fault=0 \ No newline at end of file +cpu: reset_on_triple_fault=0 + +ata0-master: type=cdrom, path=../kernel/aphrodite.iso, status=inserted +boot: cdrom \ No newline at end of file diff --git a/emulation/bx_enh_dbg.ini b/emulation/bx_enh_dbg.ini new file mode 100644 index 0000000..6fb0bbe --- /dev/null +++ b/emulation/bx_enh_dbg.ini @@ -0,0 +1,26 @@ +# bx_enh_dbg_ini +SeeReg[0] = TRUE +SeeReg[1] = TRUE +SeeReg[2] = TRUE +SeeReg[3] = TRUE +SeeReg[4] = FALSE +SeeReg[5] = FALSE +SeeReg[6] = FALSE +SeeReg[7] = FALSE +SingleCPU = FALSE +ShowIOWindows = TRUE +ShowButtons = TRUE +SeeRegColors = TRUE +ignoreNxtT = TRUE +ignSSDisasm = TRUE +UprCase = 0 +DumpInAsciiMode = 3 +isLittleEndian = TRUE +DefaultAsmLines = 512 +DumpWSIndex = 0 +DockOrder = 0x123 +ListWidthPix[0] = 354 +ListWidthPix[1] = 411 +ListWidthPix[2] = 512 +MainWindow = 0, 0, 895, 500 +FontName = Normal diff --git a/kernel/.cargo/config.toml b/kernel/.cargo/config.toml index d666294..0a329c6 100644 --- a/kernel/.cargo/config.toml +++ b/kernel/.cargo/config.toml @@ -1,5 +1,6 @@ [build] target = "i686-unknown-none.json" +rustflags = ["-Clink-arg=--script=link.x"] [unstable] build-std = ["core"] \ No newline at end of file diff --git a/kernel/build b/kernel/build index 8734455..818018b 100755 --- a/kernel/build +++ b/kernel/build @@ -7,23 +7,21 @@ if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi get_version -cd ../patcher -cargo build --release cd ../kernel -RUSTFLAGS='-Clink-arg=--script=link.x' cargo build --target i686-unknown-none.json --release -Zbuild-std --bin entrypoint +cargo build --target i686-unknown-none.json --release -Zbuild-std --bin entrypoint # build the kernel's entrypoint cp target/i686-unknown-none/release/entrypoint kernel.flat # copy it out -../patcher/target/release/patcher -# run the custom patching program to add the multiboot2 header - rm -rf grub cp -r ./grub_template ./grub cp kernel.flat ./grub/boot/aphrodite.kernel -sed -i "s@%{VERSION}@$VERSION@g" ./grub/boot/grub/grub.cfg \ No newline at end of file +sed -i "s@%{VERSION}@$VERSION@g" ./grub/boot/grub/grub.cfg + +grub-mkrescue -o aphrodite-grub.iso grub +cp aphrodite-grub.iso aphrodite.iso \ No newline at end of file diff --git a/kernel/build_and_emulate b/kernel/build_and_emulate new file mode 100755 index 0000000..07b0ea1 --- /dev/null +++ b/kernel/build_and_emulate @@ -0,0 +1,4 @@ +#!/bin/bash + +./build +./emulate \ No newline at end of file diff --git a/kernel/emulate b/kernel/emulate new file mode 100755 index 0000000..0468435 --- /dev/null +++ b/kernel/emulate @@ -0,0 +1,4 @@ +#!/bin/bash + +cd ../emulation +bochs -q \ No newline at end of file diff --git a/kernel/functions b/kernel/functions index 29fb751..e2d2f1a 100644 --- a/kernel/functions +++ b/kernel/functions @@ -1,9 +1,13 @@ function get_version() { local TEMP_SUFFIX - if [[ ! $(git diff-index --quiet HEAD --) ]]; then + if git diff-index HEAD -- 2>&1 > /dev/null + then + echo -n + # N/A + else TEMP_SUFFIX="-out-of-tree" fi - SUFFIX="${SUFFIX:-$TEMP_SUFFIX}" + SUFFIX="$SUFFIX$TEMP_SUFFIX" VERSION="${VERSION:-devel-$(git rev-parse --short HEAD)$SUFFIX}" } diff --git a/kernel/grub/boot/aphrodite.kernel b/kernel/grub/boot/aphrodite.kernel index 987af55..f2713ed 100755 Binary files a/kernel/grub/boot/aphrodite.kernel and b/kernel/grub/boot/aphrodite.kernel differ diff --git a/kernel/grub/boot/grub/grub.cfg b/kernel/grub/boot/grub/grub.cfg index b03fce2..99a2758 100644 --- a/kernel/grub/boot/grub/grub.cfg +++ b/kernel/grub/boot/grub/grub.cfg @@ -1,5 +1,8 @@ -menuentry "Aphrodite" --class aphrodite --class kernel --class os $menuentry_id_option 'aphrodite-basic-devel-6985552-out-of-tree' { - set root='hd1,gpt2' - echo 'Loading Aphrodite aphrodite-devel-6985552-out-of-tree ...' +set timeout=15 +set default=0 + +menuentry "Aphrodite" --class aphrodite --class kernel --class os $menuentry_id_option 'aphrodite-basic-devel-0b87ccb' { + echo 'Loading Aphrodite aphrodite-devel-0b87ccb ...' multiboot2 /boot/aphrodite.kernel + boot } \ No newline at end of file diff --git a/kernel/grub_template/boot/grub/grub.cfg b/kernel/grub_template/boot/grub/grub.cfg index 964ef9a..5b54914 100644 --- a/kernel/grub_template/boot/grub/grub.cfg +++ b/kernel/grub_template/boot/grub/grub.cfg @@ -1,5 +1,8 @@ +set timeout=15 +set default=0 + menuentry "Aphrodite" --class aphrodite --class kernel --class os $menuentry_id_option 'aphrodite-basic-%{VERSION}' { - set root='hd1,gpt2' echo 'Loading Aphrodite aphrodite-%{VERSION} ...' multiboot2 /boot/aphrodite.kernel + boot } \ No newline at end of file diff --git a/kernel/kernel.flat b/kernel/kernel.flat index 987af55..f2713ed 100755 Binary files a/kernel/kernel.flat and b/kernel/kernel.flat differ diff --git a/kernel/link.x b/kernel/link.x index c357a89..feeb333 100644 --- a/kernel/link.x +++ b/kernel/link.x @@ -1,9 +1,10 @@ ENTRY(_start) -OUTPUT_FORMAT(binary) +OUTPUT_FORMAT(elf32-i386) SECTIONS { .text : { . = ALIGN(8); + KEEP(*(.multiboot2)) KEEP(*(.start)) KEEP(*(.text)) KEEP(*(.panic)) diff --git a/kernel/src/include/arch/x86/output.rs b/kernel/src/include/arch/x86/output.rs index ec4cfae..8ab15c5 100644 --- a/kernel/src/include/arch/x86/output.rs +++ b/kernel/src/include/arch/x86/output.rs @@ -18,6 +18,11 @@ macro_rules! message_funcs { ports::outbs(super::DEBUG_PORT, $prefix.as_bytes()); ports::outbs(super::DEBUG_PORT, s); } + /// Outputs a(n) $func_name message u8 to the debug serial port. + pub fn [< s $func_name u >](s: u8) { + ports::outbs(super::DEBUG_PORT, $prefix.as_bytes()); + ports::outb(super::DEBUG_PORT, s); + } } } } @@ -27,4 +32,5 @@ message_funcs!(info, "[INFO] "); message_funcs!(warning, "[WARN] "); message_funcs!(error, "[ERROR] "); message_funcs!(fatal, "[FATAL] "); +message_funcs!(output, ""); diff --git a/kernel/src/include/errors.rs b/kernel/src/include/errors.rs index af4e191..a286ca3 100644 --- a/kernel/src/include/errors.rs +++ b/kernel/src/include/errors.rs @@ -1,24 +1,5 @@ //! Stuff related to errors. -/// Converts an i16 to an [u8; 6]. -pub fn i16_as_u8_slice(mut value: i16) -> [u8; 6] { - let mut buf = [0u8; 6]; - let mut i = 0; - if value < 0 { - buf[i] = b'-'; - value = -value; - } - i = 5; - while value > 0 { - let digit = value%10; - let char = b'0' + digit as u8; - buf[i] = char; - value = value / 10; - i -= 1; - } - buf -} - /// An error used by aphrodite pub struct Error<'a> { message: &'a str, @@ -27,7 +8,7 @@ pub struct Error<'a> { impl core::fmt::Debug for Error<'_> { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.write_str(core::str::from_utf8(&i16_as_u8_slice(self.code)).unwrap())?; + f.write_str(core::str::from_utf8(&crate::i16_as_u8_slice(self.code)).unwrap())?; f.write_str(": ")?; f.write_str(self.message) } @@ -35,7 +16,7 @@ impl core::fmt::Debug for Error<'_> { impl core::fmt::Display for Error<'_> { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.write_str(core::str::from_utf8(&i16_as_u8_slice(self.code)).unwrap())?; + f.write_str(core::str::from_utf8(&crate::i16_as_u8_slice(self.code)).unwrap())?; f.write_str(": ")?; f.write_str(self.message) } diff --git a/kernel/src/include/mod.rs b/kernel/src/include/mod.rs index 09eb5d2..5a45b2a 100644 --- a/kernel/src/include/mod.rs +++ b/kernel/src/include/mod.rs @@ -4,6 +4,7 @@ #![feature(ptr_metadata)] mod constants; +mod util; pub mod multiboot2; pub mod arch; mod errors; @@ -11,4 +12,5 @@ mod errors; #[allow(unused_imports)] // if there are no constants, then it gives a warning pub use constants::*; -pub use errors::*; \ No newline at end of file +pub use errors::*; +pub use util::*; \ No newline at end of file diff --git a/kernel/src/include/util.rs b/kernel/src/include/util.rs new file mode 100644 index 0000000..495d7c7 --- /dev/null +++ b/kernel/src/include/util.rs @@ -0,0 +1,57 @@ +//! Utility functions + +/// Converts an i16 to an [u8; 6]. +pub fn i16_as_u8_slice(mut value: i16) -> [u8; 6] { + let mut buf = [0u8; 6]; + let mut i = 0; + if value < 0 { + buf[i] = b'-'; + value = -value; + } + if value == 0 { + buf[0] = b'0'; + } + i = 5; + while value > 0 { + let digit = value%10; + let char = b'0' + digit as u8; + buf[i] = char; + value = value / 10; + i -= 1; + } + buf +} + +/// Converts an u32 to an [u8; 10]. +pub fn u32_as_u8_slice(mut value: u32) -> [u8; 10] { + let mut buf = [0u8; 10]; + let mut i = 9; + if value == 0 { + buf[0] = b'0'; + } + while value > 0 { + let digit = value%10; + let char = b'0' + digit as u8; + buf[i] = char; + value = value / 10; + i -= 1; + } + buf +} + +/// Converts an usize(32 or 64 bit) to an [u8; 10]. +pub fn usize_as_u8_slice(mut value: usize) -> [u8; 20] { + let mut buf = [0u8; 20]; + let mut i = 19; + if value == 0 { + buf[0] = b'0'; + } + while value > 0 { + let digit = value%10; + let char = b'0' + digit as u8; + buf[i] = char; + value = value / 10; + i -= 1; + } + buf +} diff --git a/kernel/src/internal/arch/x86/entry.rs b/kernel/src/internal/arch/x86/entry.rs index bf40be0..a04d06a 100644 --- a/kernel/src/internal/arch/x86/entry.rs +++ b/kernel/src/internal/arch/x86/entry.rs @@ -6,6 +6,16 @@ use core::{arch::asm, ffi::CStr, panic::PanicInfo}; use aphrodite::multiboot2::{BootInfo, CString, ColorInfo, FramebufferInfo, MemoryMap, PaletteColorDescriptor, RawMemoryMap, RootTag, Tag}; +use aphrodite::arch::x86::output::*; + +#[unsafe(link_section = ".multiboot2")] +#[unsafe(no_mangle)] +static MULTIBOOT2_HEADER: [u8; 29] = [ + 0xd6, 0x50, 0x52, 0xe8, 0x00, 0x00, 0x00, 0x00, + 0x18, 0x00, 0x00, 0x00, 0x12, 0xaf, 0xad, 0x17, + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, + 0xe9, 0x55, 0x00, 0x00, 0x00 +]; // The root tag, provided directly from the multiboot bootloader. static mut RT: *const RootTag = core::ptr::null(); @@ -31,10 +41,9 @@ static mut MAGIC: u32 = 0xFFFFFFFF; extern "C" fn _start() -> ! { unsafe { // Copy values provided by the bootloader out asm!( - "mov {0:e}, eax", out(reg) MAGIC // Magic number - ); - asm!( - "mov {0:e}, ebx", out(reg) O // Bootloader-specific data + "mov ebx, ebx", out("ebx") O, // Bootloader-specific data(ebx) + out("eax") MAGIC, // Magic number(eax) + options(nomem, nostack, preserves_flags, pure) ); } unsafe { @@ -42,38 +51,66 @@ extern "C" fn _start() -> ! { 0x36D76289 => { // Multiboot2 RT = O as *const RootTag; // This is unsafe rust! We can do whatever we want! *manical laughter* + sdebugs("Total boot info length: "); + soutputb(&aphrodite::u32_as_u8_slice((*RT).total_len)); + soutputu(b'\n'); + + sdebugs("Root tag address is: "); + soutputb(&aphrodite::usize_as_u8_slice(O as usize)); + soutputu(b'\n'); + + if (*RT).total_len<16 { // Size of root tag+size of terminating tag. Something's up. + panic!("total length < 16") + } + + soutputu(b'\n'); + let mut ptr = O as usize; ptr += size_of::(); - let mut current_tag = ptr as *const Tag; + let mut current_tag = core::ptr::read_volatile(ptr as *const Tag); loop { - match (*current_tag).tag_type { + sdebugs("Tag address is: "); + soutputb(&aphrodite::usize_as_u8_slice(ptr)); + soutputu(b'\n'); + + sdebugs("Tag type is: "); + soutputb(&aphrodite::u32_as_u8_slice(current_tag.tag_type)); + soutputu(b'\n'); + + sdebugs("Tag length is: "); + soutputb(&aphrodite::u32_as_u8_slice(current_tag.tag_len)); + soutputu(b'\n'); + + soutputu(b'\n'); + + match current_tag.tag_type { 0 => { // Ending tag - if (*current_tag).tag_len != 8 { // Unexpected size, something is probably up + if current_tag.tag_len != 8 { // Unexpected size, something is probably up panic!("size of ending tag != 8"); } break }, 4 => { // Basic memory information - if (*current_tag).tag_len != 16 { // Unexpected size, something is probably up + if current_tag.tag_len != 16 { // Unexpected size, something is probably up panic!("size of basic memory information tag != 16"); } - BI.mem_lower = Some(*((current_tag as usize + 8) as *const u32)); - BI.mem_upper = Some(*((current_tag as usize + 12) as *const u32)); + BI.mem_lower = Some(*((ptr + 8) as *const u32)); + BI.mem_upper = Some(*((ptr + 12) as *const u32)); // The end result of the above is adding an offset to a pointer and retrieving the value at that pointer }, 5 => { // BIOS boot device, ignore - if (*current_tag).tag_len != 20 { // Unexpected size, something is probably up + if current_tag.tag_len != 20 { // Unexpected size, something is probably up panic!("size of bios boot device tag != 20"); } }, 1 => { // Command line - if (*current_tag).tag_len < 8 { // Unexpected size, something is probably up + if current_tag.tag_len < 8 { // Unexpected size, something is probably up panic!("size of command line tag < 8"); } - let cstring = CStr::from_ptr((current_tag as usize + 8) as *const i8); + let cstring = CStr::from_ptr((ptr + 8) as *const i8); // creates a &core::ffi::CStr from the start of the command line... let cstring = CString { @@ -86,11 +123,11 @@ extern "C" fn _start() -> ! { // ...before the BootInfo's commandline is set. }, 6 => { // Memory map tag - if (*current_tag).tag_len < 16 { // Unexpected size, something is probably up + if current_tag.tag_len < 16 { // Unexpected size, something is probably up panic!("size of memory map tag < 16"); } let rawmemorymap: *const RawMemoryMap = core::ptr::from_raw_parts( - current_tag, ((*current_tag).tag_len / *((current_tag as usize + 8usize) as *const u32)) as usize + ptr as *const u8, (current_tag.tag_len / *((ptr + 8usize) as *const u32)) as usize ); // The end result of the above is creating a *const RawMemoryMap that has the same address as current_tag // and has all of the [aphrodite::multiboot2::MemorySection]s for the memory map @@ -103,10 +140,10 @@ extern "C" fn _start() -> ! { }); }, 2 => { // Bootloader name - if (*current_tag).tag_len < 8 { // Unexpected size, something is probably up + if current_tag.tag_len < 8 { // Unexpected size, something is probably up panic!("size of command line tag < 8"); } - let cstring = CStr::from_ptr((current_tag as usize + 8) as *const i8); + let cstring = CStr::from_ptr((ptr + 8) as *const i8); // creates a &core::ffi::CStr from the start of the bootloader name... let cstring = CString { @@ -119,26 +156,26 @@ extern "C" fn _start() -> ! { // ...before the BootInfo's bootloader_name is set. }, 8 => { // Framebuffer info - if (*current_tag).tag_len < 40 { // Unexpected size, something is probably up + if current_tag.tag_len < 40 { // Unexpected size, something is probably up panic!("size of framebuffer info tag < 40"); } - let framebufferinfo: *const FramebufferInfo = current_tag as *const FramebufferInfo; + let framebufferinfo: *const FramebufferInfo = ptr as *const FramebufferInfo; let colorinfo: ColorInfo; match (*framebufferinfo).fb_type { 0 => { // Indexed colorinfo = ColorInfo::Palette { - num_colors: *((current_tag as usize + 40) as *const u32), - palette: (current_tag as usize + 44) as *const PaletteColorDescriptor + num_colors: *((ptr + 40) as *const u32), + palette: (ptr + 44) as *const PaletteColorDescriptor }; }, 1 => { // RGB colorinfo = ColorInfo::RGBColor { - red_field_position: *((current_tag as usize + 40) as *const u8), - red_mask_size: *((current_tag as usize + 41) as *const u8), - green_field_position: *((current_tag as usize + 42) as *const u8), - green_mask_size: *((current_tag as usize + 43) as *const u8), - blue_field_position: *((current_tag as usize + 44) as *const u8), - blue_mask_size: *((current_tag as usize + 45) as *const u8) + red_field_position: *((ptr + 40) as *const u8), + red_mask_size: *((ptr + 41) as *const u8), + green_field_position: *((ptr + 42) as *const u8), + green_mask_size: *((ptr + 43) as *const u8), + blue_field_position: *((ptr + 44) as *const u8), + blue_mask_size: *((ptr + 45) as *const u8) } }, 2 => { // EGA Text @@ -151,11 +188,15 @@ extern "C" fn _start() -> ! { BI.framebuffer_info = Some((*framebufferinfo).clone()); BI.color_info = Some(colorinfo); }, + 4294967295 => { // oh no, THIS bug happened + panic!("your code is fucked up") + }, _ => { // Unknown/unimplemented tag type, ignore // TODO: Add info message } } - current_tag = (current_tag as usize + (*current_tag).tag_len as usize) as *const Tag; + ptr = ptr + current_tag.tag_len as usize; + current_tag = core::ptr::read_volatile(ptr as *const Tag); } }, _ => { // Unknown bootloader, panic @@ -172,7 +213,7 @@ extern "C" fn _start() -> ! { fn handle_panic(info: &PanicInfo) -> ! { let message = info.message().as_str().unwrap_or(""); if message != "" { - aphrodite::arch::x86::output::sfatals(message); + sfatals(message); aphrodite::arch::x86::ports::outb(aphrodite::arch::x86::DEBUG_PORT, b'\n'); } unsafe { diff --git a/patcher/Cargo.toml b/patcher/Cargo.toml deleted file mode 100644 index 1699210..0000000 --- a/patcher/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "patcher" -version = "0.1.0" -edition = "2024" - -[dependencies] diff --git a/patcher/src/main.rs b/patcher/src/main.rs deleted file mode 100644 index 974422f..0000000 --- a/patcher/src/main.rs +++ /dev/null @@ -1,45 +0,0 @@ -use std::{fs, io::Write}; - -static MULTIBOOT_HEADER: [u32; 6] = [ - // Magic fields - 0xE85250D6, // Magic number - 0x00000000, // Architecture, 0=i386 - 0x00000018, // length of MULTIBOOT_HEADER - 0x17ADAF12, // checksum=all magic field excluding this+this=0 - - // Ending tag- empty flags, size 8 - 0x00000000, - 0x00000008 -]; - -fn from_u32(from: &mut [u32]) -> &[u8] { - if cfg!(target_endian = "big") { - for byte in from.iter_mut() { - *byte = byte.to_le(); - } - } - - let len = from.len().checked_mul(4).unwrap(); - let ptr: *const u8 = from.as_ptr().cast(); - unsafe { std::slice::from_raw_parts(ptr, len) } -} - -fn main() { - let path = "./kernel.flat"; - let mut buf = fs::read(path).unwrap(); - buf = [ - from_u32(&mut (MULTIBOOT_HEADER.clone())).to_vec(), - vec![ // jump past patch text - 0xE9, 0x55, 0x00, 0x00, 0x00 - ], - b"Multiboot2 header patched by Aphrodite ".to_vec(), - b"APHROKERN: OSS at github.com/AverseABFun/Aphrodite".to_vec(), - buf - ].concat(); - fs::OpenOptions::new() - .write(true) - .open(path) - .unwrap() - .write(buf.as_mut_slice()) - .unwrap(); -}