Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 138 additions & 6 deletions kernel/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fn chcolor(_args: Vec<&str>) -> i32 {
for arg in _args {
if let Some(color) = STR_COLORS.iter().find(|&col| col.name == arg.replace("\n", "")) {
new_colors.push(color.color);

} else {
WRITER
.lock()
Expand All @@ -93,6 +94,7 @@ fn chcolor(_args: Vec<&str>) -> i32 {
}
}
WRITER.lock().change_color(new_colors[0], new_colors[1]);
WRITER.lock().clear_screen();
0
} else {
WRITER.lock().print_colored(
Expand All @@ -113,6 +115,104 @@ pub fn cmd_hist(_args: Vec<&str>) -> i32 {
0
}

// Command to shutdown the system (now public)
pub fn shutdown_command(_args: Vec<&str>) -> i32 {
println!("Shutting down system...");

// Legacy shutdown methods
unsafe {
// ACPI method (PM1a port)
let mut port: x86_64::instructions::port::Port<u16> = x86_64::instructions::port::Port::new(0x604);
port.write(0x2000);

// QEMU method
let mut port: x86_64::instructions::port::Port<u16> = x86_64::instructions::port::Port::new(0x604);
port.write(0x2000);

// Bochs method
let mut port: x86_64::instructions::port::Port<u16> = x86_64::instructions::port::Port::new(0xB004);
port.write(0x2000);

// VirtualBox method
let mut port: x86_64::instructions::port::Port<u16> = x86_64::instructions::port::Port::new(0x4004);
port.write(0x3400);
}

println!("Could not shutdown system via hardware.");
println!("On real systems, this would power off the machine.");
0
}

// Command to reboot the system (now public)
pub fn reboot_command(_args: Vec<&str>) -> i32 {
println!("Rebooting system...");

// Reboot method via keyboard controller
unsafe {
use x86_64::instructions::port::Port;

let mut port: Port<u8> = Port::new(0x64);
// Wait for buffer to be empty
for _ in 0..10000 {
if port.read() & 0x2 == 0 {
break;
}
}
port.write(0xFE);
}

// Small delay
for _ in 0..1000000 {
x86_64::instructions::nop();
}

println!("Could not reboot system via hardware.");
println!("On real systems, this would restart the machine.");
0
}

// Command to show current time
pub fn time_command(_args: Vec<&str>) -> i32 {
unsafe {
if let Some(rtc) = &mut crate::RTC_CONTROLLER {
let datetime = rtc.read_datetime();
println!("Current time: {}", datetime.format_time());
0
} else {
println!("Error: RTC not initialized");
2
}
}
}

// Command to show current date
pub fn date_command(_args: Vec<&str>) -> i32 {
unsafe {
if let Some(rtc) = &mut crate::RTC_CONTROLLER {
let datetime = rtc.read_datetime();
println!("Current date: {}", datetime.format_date());
0
} else {
println!("Error: RTC not initialized");
2
}
}
}

// Command to show full date and time
pub fn datetime_command(_args: Vec<&str>) -> i32 {
unsafe {
if let Some(rtc) = &mut crate::RTC_CONTROLLER {
let datetime = rtc.read_datetime();
println!("Date and time: {}", datetime.format_full());
0
} else {
println!("Error: RTC not initialized");
2
}
}
}

#[cfg(debug_assertions)]
fn crasher(_args: Vec<&str>) -> i32 {
println!("CRASHING...\n\n");
Expand Down Expand Up @@ -150,12 +250,6 @@ pub const COMMAND_LIST: &[Command] = &[
doc: "display the documentation of selected command",
fun: document,
},
// Command {
// name: "reinit",
// args: "",
// doc: "re-initialize the kernel",
// fun: crate::init_kernel,
// },
Command {
name: "chcolor",
args: "[fg] [bg]",
Expand All @@ -168,6 +262,44 @@ pub const COMMAND_LIST: &[Command] = &[
doc: "display command history",
fun: cmd_hist,
},
// Power management commands
Command {
name: "shutdown",
args: "",
doc: "shutdown the system",
fun: shutdown_command,
},
Command {
name: "reboot",
args: "",
doc: "reboot the system",
fun: reboot_command,
},
Command {
name: "poweroff",
args: "",
doc: "shutdown the system (alias for shutdown)",
fun: shutdown_command,
},
// RTC commands
Command {
name: "time",
args: "",
doc: "show current time",
fun: time_command,
},
Command {
name: "date",
args: "",
doc: "show current date",
fun: date_command,
},
Command {
name: "datetime",
args: "",
doc: "show full date and time",
fun: datetime_command,
},
#[cfg(debug_assertions)]
Command {
name: "crash_kernel",
Expand Down
118 changes: 111 additions & 7 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ use hlkernel::{
mod cmd;
use cmd::COMMAND_LIST;

// Add RTC module
mod rtc;

// Global variables
static mut ACPI_CONTROLLER: Option<AcpiController> = None;
static mut RTC_CONTROLLER: Option<rtc::Rtc> = None;

entry_point!(kernel_main);

struct RtrType {
Expand All @@ -27,6 +34,88 @@ struct RtrType {
color: Color,
}

// Simple structure for ACPI
struct AcpiController;

impl AcpiController {
pub unsafe fn new() -> Self {
Self
}

pub fn shutdown(&mut self) {
// Call the public shutdown function
cmd::shutdown_command(vec![]);
}

pub fn reboot(&mut self) {
// Call the public reboot function
cmd::reboot_command(vec![]);
}
}

pub fn started_colors() {
WRITER.lock().print_colored(
format!("\naA"),
Color::Blue,
Color::Blue,
);
WRITER.lock().print_colored(
format!("aA"),
Color::Pink,
Color::Pink,
);
WRITER.lock().print_colored(
format!("aA"),
Color::Red,
Color::Red,
);
WRITER.lock().print_colored(
format!("aA"),
Color::Green,
Color::Green,
);
WRITER.lock().print_colored(
format!("aA"),
Color::Yellow,
Color::Yellow,
);
WRITER.lock().print_colored(
format!("aA"),
Color::LightBlue,
Color::LightBlue,
);

WRITER.lock().print_colored(
format!("aA"),
Color::Magenta,
Color::Magenta,
);

WRITER.lock().print_colored(
format!("aA"),
Color::Cyan,
Color::Cyan,
);

WRITER.lock().print_colored(
format!("aA"),
Color::Brown,
Color::Brown,
);

WRITER.lock().print_colored(
format!("aA"),
Color::Blue,
Color::Blue,
);

WRITER.lock().print_colored(
format!("aA"),
Color::Blue,
Color::Blue,
);
}

pub fn init_kernel(boot_info: &'static BootInfo) {
use hlkernel::allocator;
use hlkernel::mem::{self, BootInfoFrameAlloc};
Expand All @@ -43,21 +132,36 @@ pub fn init_kernel(boot_info: &'static BootInfo) {

hlkernel::init();

// Initialize ACPI controller
unsafe {
ACPI_CONTROLLER = Some(AcpiController::new());
}

// Initialize RTC
unsafe {
RTC_CONTROLLER = Some(rtc::Rtc::new());
}

#[cfg(debug_assertions)]
WRITER.lock().print_colored(
format!("\nHighlightOS v{} DEBUG", env!("CARGO_PKG_VERSION")),
format!("\nHighlightOS v{} *DEBUG*", env!("CARGO_PKG_VERSION")),
Color::Black,
Color::Yellow,
);

#[cfg(not(debug_assertions))]
WRITER.lock().print_colored(
format!("\nHighlightOS v{}", env!("CARGO_PKG_VERSION")),
format!("\n HighlightOS v{}", env!("CARGO_PKG_VERSION")),
Color::Black,
Color::Yellow,
);
WRITER.lock().print_colored(
format!("\n Documentation: https://os.adamperkowski.dev"),
Color::Cyan,
Color::Black
);

print!("\n\nhls < ");
print!("\n\nhls > ");
}

fn kernel_main(boot_info: &'static BootInfo) -> ! {
Expand Down Expand Up @@ -86,17 +190,17 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
if let Some(return_code) = RTR_LIST.iter().find(|&rtr_t| rtr_t.code == &rtr) {
println!("\n > {}", req_com);
WRITER.lock().print_colored(
format!("{} : {}\n\n", rtr, return_code.info),
format!("{}:{}\n\n", rtr, return_code.info),
return_code.color,
Color::Black, // Color::None ??
Color::Black,
);
} else {
println!("\n > {}\nreturned : {}\n", req_com, rtr);
}
}
} else {
WRITER.lock().print_colored(
format!("\n > {}\ncommand not found\n\n", input),
format!("\n > hls: command not found: {}\n", input),
Color::LightRed,
Color::Black,
);
Expand All @@ -112,7 +216,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
}
}

print!("hls < ");
print!("hls > ");
}
}
}
Expand Down
Loading