yggdrasil/src/init.rs

76 lines
2.3 KiB
Rust

//! Kernel main process implementation: filesystem initialization and userspace init start
use abi::{
error::Error,
io::{FileMode, OpenOptions, RawFd},
};
use memfs::MemoryFilesystem;
use vfs::{Filesystem, IoContext, VnodeRef};
use crate::{
fs::{devfs, FileBlockAllocator, INITRD_DATA},
proc,
};
fn setup_root() -> Result<VnodeRef, Error> {
let initrd_data = INITRD_DATA.get();
let fs = MemoryFilesystem::<FileBlockAllocator>::from_slice(initrd_data.data).unwrap();
fs.root()
}
/// Kernel's "main" process function.
///
/// # Note
///
/// This function is meant to be used as a kernel-space process after all the platform-specific
/// initialization has finished.
pub fn kinit() {
infoln!("In main");
#[cfg(feature = "fb_console")]
{
use crate::{device::display::console::update_consoles_task, task::runtime};
runtime::spawn(async move {
update_consoles_task().await;
})
.expect("Could not start periodic console auto-flush task");
}
let root = match setup_root() {
Ok(root) => root,
Err(err) => {
warnln!("Could not setup root from initrd: {:?}", err);
return;
}
};
let ioctx = IoContext::new(root);
let node = ioctx.find(None, "/init", true, true).unwrap();
let file = node.open(OpenOptions::READ, FileMode::empty()).unwrap();
let devfs = devfs::root();
#[cfg(target_arch = "x86_64")]
let console = ioctx.find(Some(devfs.clone()), "tty0", true, true).unwrap();
#[cfg(target_arch = "aarch64")]
let console = ioctx
.find(Some(devfs.clone()), "ttyS0", true, true)
.unwrap();
let stdin = console.open(OpenOptions::READ, FileMode::empty()).unwrap();
let stdout = console.open(OpenOptions::WRITE, FileMode::empty()).unwrap();
let stderr = stdout.clone();
{
let user_init = proc::exec::load_elf("init", file, &["/init", "xxx"], &[]).unwrap();
let mut io = user_init.io.lock();
io.set_ioctx(ioctx);
io.set_file(RawFd::STDIN, stdin).unwrap();
io.set_file(RawFd::STDOUT, stdout).unwrap();
io.set_file(RawFd::STDERR, stderr).unwrap();
drop(io);
user_init.set_session_terminal(console);
user_init.enqueue_somewhere();
}
}