From 29e243e60abf1ff9e3839c27179599bc159fcaf0 Mon Sep 17 00:00:00 2001 From: 0x6D70 Date: Mon, 15 Jan 2024 20:49:56 +0100 Subject: [PATCH 1/3] build image using fatfs crate --- build/xtask/Cargo.toml | 1 + build/xtask/src/main.rs | 62 ++++++++++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/build/xtask/Cargo.toml b/build/xtask/Cargo.toml index 2ce032b9..37482e8d 100644 --- a/build/xtask/Cargo.toml +++ b/build/xtask/Cargo.toml @@ -11,3 +11,4 @@ pico-args = "0.5.0" anyhow = "1" wsl = "0.1.0" dotenv = "0.15.0" +fatfs = "0.3.6" diff --git a/build/xtask/src/main.rs b/build/xtask/src/main.rs index d4286381..2d8c4a5d 100644 --- a/build/xtask/src/main.rs +++ b/build/xtask/src/main.rs @@ -1,8 +1,11 @@ use anyhow::{bail, Result}; use dotenv::dotenv; +use fatfs::{format_volume, FormatVolumeOptions}; use pico_args::Arguments; -use std::env; +use std::io::{Cursor, Read, Seek, Write}; use std::path::{Path, PathBuf}; +use std::ptr::slice_from_raw_parts; +use std::{env, fs, vec}; use xshell::{cmd, Shell}; const HELP: &str = "\ @@ -85,14 +88,7 @@ fn build(sh: &Shell, rl: bool, mut args: Arguments) -> Result<()> { .opt_value_from_str::<_, String>("--target")? .unwrap_or_else(|| "x86_64".to_string()); - if !Path::new( - sh.current_dir() - .as_path() - .join("xernel/kernel/limine") - .as_path(), - ) - .exists() - { + if !Path::new(sh.current_dir().as_path().join("xernel/kernel/limine").as_path()).exists() { sh.change_dir(sh.current_dir().as_path().join("xernel/kernel")); cmd!( sh, @@ -121,6 +117,35 @@ fn build(sh: &Shell, rl: bool, mut args: Arguments) -> Result<()> { let build_dir = if rl { "release" } else { "debug" }; let diskname = "xernel.hdd"; + + let data_vec = vec![0_u8; 64 * 1024 * 1024]; + let ptr = data_vec.as_ptr(); + + let mut disk = Cursor::new(data_vec); + + format_volume(&mut disk, FormatVolumeOptions::new().fat_type(fatfs::FatType::Fat32))?; + + let fs = fatfs::FileSystem::new(disk, fatfs::FsOptions::new())?; + + let root_dir = fs.root_dir(); + + copy_to_image(&root_dir, &format!("./target/{target}/{build_dir}/xernel"), "xernel")?; + + copy_to_image(&root_dir, "./xernel/kernel/limine.cfg", "limine.cfg")?; + copy_to_image(&root_dir, "./logo.bmp", "logo.bmp")?; + + let dir = root_dir.create_dir("EFI")?; + let dir = dir.create_dir("BOOT")?; + + copy_to_image(&dir, "./xernel/kernel/limine/BOOTX64.EFI", "BOOTX64.EFI")?; + + // fs.unmount()?; + + let illegal_copy = unsafe { &*slice_from_raw_parts(ptr, 64 * 1024 * 1024) }; + + fs::write(diskname, illegal_copy)?; + + /* let disksize = 64.to_string(); cmd!( @@ -149,6 +174,7 @@ fn build(sh: &Shell, rl: bool, mut args: Arguments) -> Result<()> { "mcopy -i {diskname} xernel/kernel/limine/BOOTX64.EFI ::/EFI/BOOT" ) .run()?; + */ Ok(()) } @@ -159,10 +185,7 @@ fn run(sh: &Shell, gdb: bool, mut args: Arguments) -> Result<()> { let ram = args .opt_value_from_str::<_, String>("--ram")? .unwrap_or_else(|| "128M".to_string()); - let cpus = args - .opt_value_from_str::<_, u32>("--cpus")? - .unwrap_or(2) - .to_string(); + let cpus = args.opt_value_from_str::<_, u32>("--cpus")?.unwrap_or(2).to_string(); let kvm = if args.contains("--kvm") { &["-enable-kvm"] @@ -174,10 +197,7 @@ fn run(sh: &Shell, gdb: bool, mut args: Arguments) -> Result<()> { let qemu_in_wsl_arg = args.contains("--wsl-qemu"); - let qemu_in_wsl_env = env::var("qemu_in_wsl") - .unwrap_or("false".to_string()) - .parse() - .unwrap(); + let qemu_in_wsl_env = env::var("qemu_in_wsl").unwrap_or("false".to_string()).parse().unwrap(); let qemu_in_wsl = qemu_in_wsl_arg || qemu_in_wsl_env; @@ -242,3 +262,11 @@ fn root() -> PathBuf { path.pop(); path } + +fn copy_to_image(dir: &fatfs::Dir, src_path: &str, dst_path: &str) -> Result<()> { + let data = fs::read(src_path)?; + + dir.create_file(dst_path)?.write_all(&data)?; + + Ok(()) +} From 51f5b5772d4a84c6da7c8eb82702c567d97aea51 Mon Sep 17 00:00:00 2001 From: 0x6D70 Date: Thu, 18 Jan 2024 09:27:02 +0100 Subject: [PATCH 2/3] build: remove unsafe code --- build/xtask/src/main.rs | 60 +++++++++-------------------------------- 1 file changed, 12 insertions(+), 48 deletions(-) diff --git a/build/xtask/src/main.rs b/build/xtask/src/main.rs index 2d8c4a5d..f39fc400 100644 --- a/build/xtask/src/main.rs +++ b/build/xtask/src/main.rs @@ -4,7 +4,6 @@ use fatfs::{format_volume, FormatVolumeOptions}; use pico_args::Arguments; use std::io::{Cursor, Read, Seek, Write}; use std::path::{Path, PathBuf}; -use std::ptr::slice_from_raw_parts; use std::{env, fs, vec}; use xshell::{cmd, Shell}; @@ -119,62 +118,27 @@ fn build(sh: &Shell, rl: bool, mut args: Arguments) -> Result<()> { let diskname = "xernel.hdd"; let data_vec = vec![0_u8; 64 * 1024 * 1024]; - let ptr = data_vec.as_ptr(); - let mut disk = Cursor::new(data_vec); format_volume(&mut disk, FormatVolumeOptions::new().fat_type(fatfs::FatType::Fat32))?; - let fs = fatfs::FileSystem::new(disk, fatfs::FsOptions::new())?; - - let root_dir = fs.root_dir(); - - copy_to_image(&root_dir, &format!("./target/{target}/{build_dir}/xernel"), "xernel")?; - - copy_to_image(&root_dir, "./xernel/kernel/limine.cfg", "limine.cfg")?; - copy_to_image(&root_dir, "./logo.bmp", "logo.bmp")?; - - let dir = root_dir.create_dir("EFI")?; - let dir = dir.create_dir("BOOT")?; - - copy_to_image(&dir, "./xernel/kernel/limine/BOOTX64.EFI", "BOOTX64.EFI")?; - - // fs.unmount()?; + let fs = fatfs::FileSystem::new(&mut disk, fatfs::FsOptions::new())?; + { + let root_dir = fs.root_dir(); - let illegal_copy = unsafe { &*slice_from_raw_parts(ptr, 64 * 1024 * 1024) }; + copy_to_image(&root_dir, &format!("./target/{target}/{build_dir}/xernel"), "xernel")?; - fs::write(diskname, illegal_copy)?; + copy_to_image(&root_dir, "./xernel/kernel/limine.cfg", "limine.cfg")?; + copy_to_image(&root_dir, "./logo.bmp", "logo.bmp")?; - /* - let disksize = 64.to_string(); + let dir = root_dir.create_dir("EFI")?; + let dir = dir.create_dir("BOOT")?; - cmd!( - sh, - "dd if=/dev/zero of={diskname} bs=1M count=0 seek={disksize}" - ) - .run()?; - - cmd!(sh, "mformat -i {diskname} -F").run()?; - cmd!( - sh, - "mcopy -i {diskname} ./target/{target}/{build_dir}/xernel ::/xernel" - ) - .run()?; - cmd!( - sh, - "mcopy -i {diskname} xernel/kernel/limine.cfg ::/limine.cfg" - ) - .run()?; + copy_to_image(&dir, "./xernel/kernel/limine/BOOTX64.EFI", "BOOTX64.EFI")?; + } + fs.unmount()?; - cmd!(sh, "mcopy -i {diskname} ./logo.bmp ::/logo.bmp").run()?; - cmd!(sh, "mmd -i {diskname} ::/EFI").run()?; - cmd!(sh, "mmd -i {diskname} ::/EFI/BOOT").run()?; - cmd!( - sh, - "mcopy -i {diskname} xernel/kernel/limine/BOOTX64.EFI ::/EFI/BOOT" - ) - .run()?; - */ + fs::write(diskname, disk.into_inner())?; Ok(()) } From a4a49e18330a2d277f7a1f4c3752b2ddac4701f2 Mon Sep 17 00:00:00 2001 From: 0x6D70 Date: Thu, 18 Jan 2024 10:30:31 +0100 Subject: [PATCH 3/3] readme: remove mtools depedency --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 38442af1..fbe5e785 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Mostly made for learning purposes, no real-world application. ![xernel](status_quo.png) ## Building -For building, you simply need a working Rust installation with the nightly toolchain installed and the `mtools` package which we use for the generation of the disk image. +For building, you simply need a working Rust installation with the nightly toolchain installed. We use xtask as our workflow for building and running the kernel. Therefore, the command `cargo kernel` is used.