2022-08-05 06:22:23 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Able <able@ablecorp.us>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-08-02 01:52:03 -05:00
|
|
|
use crate::arch::drivers::sysinfo::master;
|
2022-04-25 13:39:39 -05:00
|
|
|
use crate::arch::interrupts::{reset_pit_for_cpu, set_pit_2};
|
2022-08-17 05:25:53 -05:00
|
|
|
use crate::devices::pci::ide::{Channel, Drive};
|
2022-08-16 08:35:34 -05:00
|
|
|
use crate::devices::pci::{PciDevice, PCI_DEVICES};
|
2022-08-07 07:35:55 -05:00
|
|
|
use crate::filesystem;
|
2022-08-07 12:39:42 -05:00
|
|
|
use crate::filesystem::vfs::VFS;
|
2022-08-02 01:52:03 -05:00
|
|
|
use crate::systeminfo::{KERNEL_VERSION, RELEASE_TYPE};
|
|
|
|
use crate::time::fetch_time;
|
2022-08-01 06:09:05 -05:00
|
|
|
use crate::{
|
2022-08-09 06:53:25 -05:00
|
|
|
arch::shutdown, rhai_shell::KEYBUFF, vterm::VTerm, wasm_jumploader::run_program, KERNEL_STATE,
|
2022-08-01 06:09:05 -05:00
|
|
|
};
|
|
|
|
|
2022-04-19 02:15:45 -05:00
|
|
|
use acpi::{AcpiTables, PlatformInfo};
|
2022-08-01 06:09:05 -05:00
|
|
|
use cpuio::{inb, outb};
|
2022-08-02 01:52:03 -05:00
|
|
|
use kernel::allocator::ALLOCATOR;
|
2022-07-31 01:54:01 -05:00
|
|
|
use spin::Lazy;
|
2022-08-02 01:52:03 -05:00
|
|
|
use x86_64::instructions::interrupts::{disable, enable};
|
2022-04-25 15:40:13 -05:00
|
|
|
|
2022-08-02 06:00:21 -05:00
|
|
|
pub const BANNER_WIDTH: &str =
|
|
|
|
"================================================================================";
|
|
|
|
|
2022-04-25 15:40:13 -05:00
|
|
|
// TODO: move to a better place
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
pub struct AcpiStruct {}
|
|
|
|
impl acpi::AcpiHandler for AcpiStruct {
|
|
|
|
unsafe fn map_physical_region<T>(
|
|
|
|
&self,
|
|
|
|
physical_address: usize,
|
|
|
|
size: usize,
|
|
|
|
) -> acpi::PhysicalMapping<Self, T> {
|
|
|
|
info!("PHYS ADDR: {:?}", physical_address);
|
|
|
|
info!("Size: {:?}", size);
|
|
|
|
todo!("map_physical_region");
|
|
|
|
}
|
|
|
|
fn unmap_physical_region<T>(_region: &acpi::PhysicalMapping<Self, T>) {
|
|
|
|
todo!("unmap_physical_region");
|
|
|
|
}
|
|
|
|
}
|
2022-07-31 01:54:01 -05:00
|
|
|
|
2022-11-23 04:02:02 -06:00
|
|
|
// pub static TERM: Lazy<spin::Mutex<VTerm>> = Lazy::new(|| {
|
|
|
|
// trace!("mutex");
|
|
|
|
|
|
|
|
// loop {}
|
|
|
|
// let mutex = spin::Mutex::new({
|
|
|
|
// trace!("vterm");
|
|
|
|
// let vterm = VTerm::new();
|
|
|
|
// trace!("vterm-done");
|
|
|
|
// vterm
|
|
|
|
// });
|
|
|
|
// trace!("mutex-done");
|
|
|
|
// mutex
|
|
|
|
// });
|
2022-04-25 15:40:13 -05:00
|
|
|
|
2022-08-01 06:09:05 -05:00
|
|
|
#[derive(Debug)]
|
2022-04-25 15:40:13 -05:00
|
|
|
pub struct Path {
|
|
|
|
pub path: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Path {
|
|
|
|
pub fn new(path: String) -> Self {
|
|
|
|
let mut path_vec_string = vec![];
|
|
|
|
|
|
|
|
for part in path.split(&['\\', '/'][..]) {
|
|
|
|
path_vec_string.push(part.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
Path {
|
|
|
|
path: path_vec_string,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-08 03:01:29 -06:00
|
|
|
|
|
|
|
/// Experimental scratchpad for testing.
|
|
|
|
pub fn scratchpad() {
|
2022-07-31 06:21:50 -05:00
|
|
|
// bruh();
|
2022-08-02 01:52:03 -05:00
|
|
|
// panic!(":>");
|
|
|
|
|
2022-08-05 01:14:13 -05:00
|
|
|
// for c in 0..144_697 {
|
|
|
|
// trace!("{:?}", char::from_u32(c));
|
|
|
|
// }
|
|
|
|
|
2022-08-03 02:09:34 -05:00
|
|
|
// bruh();
|
|
|
|
|
2022-08-02 01:52:03 -05:00
|
|
|
disable();
|
|
|
|
let tick_time = fetch_time();
|
2022-08-07 07:35:55 -05:00
|
|
|
let hostname = KERNEL_STATE.lock().hostname.clone();
|
2022-08-02 01:52:03 -05:00
|
|
|
|
|
|
|
let allocator = ALLOCATOR.lock();
|
|
|
|
let size = allocator.size();
|
|
|
|
let used = allocator.used();
|
|
|
|
drop(allocator);
|
|
|
|
enable();
|
|
|
|
|
|
|
|
println!(
|
2022-08-02 06:00:21 -05:00
|
|
|
"{}
|
|
|
|
,-------. OS: \0BLUE\0AbleOS\0RESET\0
|
2022-08-06 08:16:08 -05:00
|
|
|
,'\\ _ _`. Host: \0PINK\0{}\0RESET\0
|
2022-08-02 06:00:21 -05:00
|
|
|
/ \\)_)-)_)-\\ Kernel: \0RED\0AKern-{}-v{}\0RESET\0
|
2022-08-05 01:14:13 -05:00
|
|
|
: : Uptime: \0GREEN\0{}\0RESET\0
|
2022-08-02 06:00:21 -05:00
|
|
|
\\ / Packages: None
|
|
|
|
\\ / Shell: BuiltinShell
|
|
|
|
`. ,' Resolution: 640x480
|
|
|
|
`. ,' Terminal: VGABuffer
|
|
|
|
`.,' CPU: {}
|
|
|
|
/\\`. ,-._ GPU: VGA Compatible
|
|
|
|
`-' Memory: {}/{}
|
|
|
|
{}",
|
|
|
|
// include_str!("../assets/balloon.txt"),
|
2022-08-02 01:52:03 -05:00
|
|
|
// kstate.hostname,
|
2022-08-02 06:00:21 -05:00
|
|
|
BANNER_WIDTH,
|
2022-08-06 08:16:08 -05:00
|
|
|
hostname,
|
2022-08-02 01:52:03 -05:00
|
|
|
RELEASE_TYPE,
|
|
|
|
KERNEL_VERSION,
|
|
|
|
tick_time,
|
|
|
|
master().unwrap().brand_string().unwrap(),
|
|
|
|
// "",
|
|
|
|
// mem
|
|
|
|
used,
|
2022-08-02 06:00:21 -05:00
|
|
|
size,
|
|
|
|
BANNER_WIDTH
|
2022-08-02 01:52:03 -05:00
|
|
|
);
|
|
|
|
|
2022-08-17 05:25:53 -05:00
|
|
|
let pci_ide_device = {
|
2022-08-16 08:35:34 -05:00
|
|
|
let pci_devices = PCI_DEVICES.lock();
|
|
|
|
pci_devices
|
|
|
|
.iter()
|
|
|
|
.find_map(|device_ref| {
|
|
|
|
let device = device_ref.lock();
|
2022-08-16 09:09:22 -05:00
|
|
|
if let PciDevice::Ide(_) = &*device {
|
2022-08-16 08:35:34 -05:00
|
|
|
Some(device_ref.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
};
|
2022-08-17 05:25:53 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut pci_ide_device = pci_ide_device.lock();
|
|
|
|
if let PciDevice::Ide(device) = &mut *pci_ide_device {
|
2022-08-17 07:10:00 -05:00
|
|
|
let mut first_sector = Vec::with_capacity(512);
|
2022-11-23 04:02:02 -06:00
|
|
|
device
|
|
|
|
.read(Channel::Primary, Drive::Master, 0, 1, &mut first_sector)
|
|
|
|
.unwrap();
|
2022-08-17 05:25:53 -05:00
|
|
|
trace!("IDE Primary/Master sector 0: {first_sector:?}");
|
|
|
|
}
|
2022-08-16 08:35:34 -05:00
|
|
|
}
|
2022-08-17 05:25:53 -05:00
|
|
|
|
2022-08-06 13:38:30 -05:00
|
|
|
real_shell();
|
2022-02-08 04:13:53 -06:00
|
|
|
}
|
2022-02-12 03:25:02 -06:00
|
|
|
|
2022-03-11 13:51:47 -06:00
|
|
|
pub fn acpi() {
|
|
|
|
let acpi_handler = AcpiStruct {};
|
|
|
|
let _table;
|
|
|
|
unsafe {
|
|
|
|
_table = AcpiTables::search_for_rsdp_bios(acpi_handler);
|
2022-02-12 03:25:02 -06:00
|
|
|
}
|
2022-04-19 02:15:45 -05:00
|
|
|
match _table.unwrap().platform_info().unwrap() {
|
|
|
|
PlatformInfo {
|
|
|
|
power_profile,
|
|
|
|
interrupt_model,
|
2022-04-25 15:40:13 -05:00
|
|
|
..
|
2022-04-19 02:15:45 -05:00
|
|
|
} => {
|
|
|
|
info!("{:?}", power_profile);
|
|
|
|
info!("{:?}", interrupt_model);
|
|
|
|
// info!("{:?}", processor_info.unwrap());
|
|
|
|
// info!("{:?}", pm_timer.unwrap());
|
|
|
|
}
|
|
|
|
}
|
2022-02-12 03:25:02 -06:00
|
|
|
}
|
2022-04-25 04:56:01 -05:00
|
|
|
|
|
|
|
pub fn real_shell() {
|
2022-07-31 05:22:39 -05:00
|
|
|
let prompt = "-> ";
|
|
|
|
|
2022-04-25 04:56:01 -05:00
|
|
|
let _current_dir = "/".to_string();
|
|
|
|
let current_user = "able".to_string();
|
|
|
|
|
|
|
|
let mut buf = String::new();
|
2022-07-31 05:22:39 -05:00
|
|
|
print!("{}", prompt);
|
2022-08-03 02:09:34 -05:00
|
|
|
// panic!(":<");
|
2022-04-25 04:56:01 -05:00
|
|
|
loop {
|
|
|
|
match x86_64::instructions::interrupts::without_interrupts(|| KEYBUFF.lock().pop()) {
|
|
|
|
Some('\n') => {
|
2022-08-02 04:14:01 -05:00
|
|
|
// panic!();
|
2022-08-02 06:00:21 -05:00
|
|
|
println!();
|
2022-04-25 04:56:01 -05:00
|
|
|
// match engine.eval_with_scope::<rhai::Dynamic>(&mut scope, &buf) {
|
|
|
|
// Ok(o) => println!("{o}"),
|
|
|
|
|
|
|
|
// Err(e) => println!("Eval error: {e}"),
|
|
|
|
// };
|
|
|
|
if !buf.is_empty() {
|
|
|
|
command_parser(current_user.clone(), buf.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.clear();
|
2022-08-02 06:00:21 -05:00
|
|
|
print!("{}", prompt);
|
2022-04-25 04:56:01 -05:00
|
|
|
}
|
2022-08-03 02:09:34 -05:00
|
|
|
Some('\u{8}') => {
|
|
|
|
print!("\u{8}");
|
2022-08-05 01:14:13 -05:00
|
|
|
|
2022-04-25 04:56:01 -05:00
|
|
|
buf.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
Some('\u{0009}') => {
|
|
|
|
buf.push(' ');
|
|
|
|
buf.push(' ');
|
|
|
|
buf.push(' ');
|
|
|
|
buf.push(' ');
|
|
|
|
}
|
|
|
|
|
2022-08-01 02:16:19 -05:00
|
|
|
Some(chr) => {
|
|
|
|
buf.push(chr);
|
|
|
|
print!("{}", chr);
|
|
|
|
}
|
2022-08-01 04:06:04 -05:00
|
|
|
None => {
|
|
|
|
// trace!("{}", buf);
|
|
|
|
}
|
2022-04-25 04:56:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn command_parser(user: String, command: String) {
|
|
|
|
let mut iter = command.split_whitespace();
|
|
|
|
|
2022-07-28 05:02:43 -05:00
|
|
|
let current_path = Path::new("/home/able".to_string());
|
|
|
|
trace!("Current path: {:?}", current_path);
|
2022-08-07 12:39:42 -05:00
|
|
|
let current_path = "/home/able/";
|
2022-07-28 05:02:43 -05:00
|
|
|
|
2022-04-25 04:56:01 -05:00
|
|
|
let bin_name = iter.next().unwrap();
|
|
|
|
|
2022-08-06 22:12:48 -05:00
|
|
|
let mut strin = String::new();
|
2022-08-07 12:39:42 -05:00
|
|
|
for stri in iter.clone() {
|
2022-08-06 22:12:48 -05:00
|
|
|
trace!("{}", stri);
|
|
|
|
strin.push_str(stri);
|
|
|
|
}
|
|
|
|
let conf_args;
|
|
|
|
match clparse::Arguments::parse_from_string(strin) {
|
|
|
|
Ok(ok) => conf_args = ok,
|
|
|
|
Err(err) => {
|
|
|
|
println!("ERROR: {}", err);
|
|
|
|
error!("{}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-28 05:02:43 -05:00
|
|
|
match bin_name {
|
2022-08-01 05:46:59 -05:00
|
|
|
// note: able asked for rhaish to stay in the repo but will be removed
|
|
|
|
// in the future so just comment it out for now
|
|
|
|
// "rhai" => {
|
|
|
|
// shell();
|
|
|
|
// }
|
2022-08-07 12:39:42 -05:00
|
|
|
"list" | "ls" => {
|
|
|
|
let mut vfs = VFS.lock();
|
|
|
|
let handle = vfs.resolve(current_path).unwrap();
|
|
|
|
let dir = vfs.fs_node(handle).unwrap();
|
|
|
|
drop(vfs);
|
|
|
|
for dir_entry in dir.directory().unwrap() {
|
|
|
|
println!("{}", dir_entry.name());
|
|
|
|
}
|
|
|
|
}
|
2022-08-06 22:12:48 -05:00
|
|
|
|
2022-08-07 12:39:42 -05:00
|
|
|
"echo" => match conf_args.1.arguments.get("p") {
|
|
|
|
Some(path) => echo_file(path.to_string()),
|
|
|
|
None => println!("No path provided"),
|
2022-08-09 06:53:25 -05:00
|
|
|
},
|
2022-08-03 12:30:05 -05:00
|
|
|
"test" => {}
|
2022-07-28 05:02:43 -05:00
|
|
|
"quit" => shutdown(),
|
2022-08-07 07:35:55 -05:00
|
|
|
"tree" => filesystem::tree("/").unwrap(),
|
2022-04-25 04:56:01 -05:00
|
|
|
|
2022-07-28 05:02:43 -05:00
|
|
|
_ => {
|
2022-08-07 12:39:42 -05:00
|
|
|
let file = {
|
|
|
|
let mut vfs = VFS.lock();
|
|
|
|
let path = format!("/home/{user}/bins/{bin_name}.wasm");
|
|
|
|
let handle = if let Ok(file) = vfs.resolve(path) {
|
|
|
|
file
|
|
|
|
} else {
|
|
|
|
let path = format!("/shared/bins/{bin_name}.wasm");
|
|
|
|
if let Ok(file) = vfs.resolve(path) {
|
|
|
|
file
|
|
|
|
} else {
|
|
|
|
let path = format!("/system/bins/{bin_name}.wasm");
|
|
|
|
match vfs.resolve(path) {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(error) => {
|
|
|
|
trace!("{:?}", error);
|
|
|
|
println!("No such binary: {}", bin_name);
|
|
|
|
error!("No such binary: {}", bin_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
vfs.fs_node(handle).unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut binary = vec![];
|
|
|
|
file.read(0, file.size(), &mut binary).unwrap();
|
|
|
|
|
|
|
|
let args = iter.collect::<Vec<&str>>();
|
|
|
|
println!("{:?}", args);
|
|
|
|
run_program(&binary);
|
2022-07-28 05:02:43 -05:00
|
|
|
}
|
2022-04-25 04:56:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:39:39 -05:00
|
|
|
pub fn sound(n_frequency: u32) {
|
|
|
|
let div: u32;
|
|
|
|
let tmp: u8;
|
|
|
|
|
|
|
|
div = 1193180 / n_frequency;
|
|
|
|
unsafe {
|
|
|
|
outb(0xb6, 0x43);
|
|
|
|
|
|
|
|
set_pit_2(div);
|
|
|
|
|
2022-08-01 06:09:05 -05:00
|
|
|
// And play the sound using the PC speaker
|
2022-04-25 13:39:39 -05:00
|
|
|
tmp = inb(0x61);
|
|
|
|
if tmp != (tmp | 3) {
|
|
|
|
outb(tmp | 3, 0x61);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sound_off() {
|
|
|
|
unsafe {
|
|
|
|
let tmp = inb(0x61) & 0xFC;
|
|
|
|
outb(tmp, 0x61)
|
|
|
|
};
|
|
|
|
reset_pit_for_cpu();
|
|
|
|
}
|
2022-07-28 05:02:43 -05:00
|
|
|
|
2022-08-07 12:39:42 -05:00
|
|
|
pub fn echo_file(path: String) {
|
|
|
|
let mut current_path = String::from("/");
|
|
|
|
current_path.push_str(&path);
|
|
|
|
let mut vfs = VFS.lock();
|
2022-08-09 06:53:25 -05:00
|
|
|
let handle = vfs.resolve(¤t_path).unwrap();
|
2022-08-07 12:39:42 -05:00
|
|
|
let file = vfs.fs_node(handle).unwrap();
|
2022-08-02 04:14:01 -05:00
|
|
|
|
|
|
|
if file.is_dir() {
|
|
|
|
// println!("{} is a directory", path);
|
|
|
|
} else {
|
|
|
|
let mut file_contents = Vec::new();
|
2022-08-07 12:39:42 -05:00
|
|
|
file.read(0, file.size(), &mut file_contents).unwrap();
|
2022-08-02 04:14:01 -05:00
|
|
|
let file_contents_str = String::from_utf8_lossy(&file_contents);
|
|
|
|
println!("{}", file_contents_str);
|
|
|
|
}
|
|
|
|
}
|