From 8e0fbfe2637b0675c870f410fd2a9cc2aec0fa1f Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Tue, 13 Dec 2022 21:38:09 +0100 Subject: [PATCH] struggling with drives PIC hardware interrupts Signed-off-by: Julien CLEMENT --- src/drivers/atapi/mod.rs | 12 ++++++------ src/interrupts/mod.rs | 11 ++++++++++- src/interrupts/pic/disk.rs | 24 ++++++++++++++++++++++++ src/interrupts/pic/mod.rs | 4 ++++ src/lib.rs | 4 +++- 5 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 src/interrupts/pic/disk.rs diff --git a/src/drivers/atapi/mod.rs b/src/drivers/atapi/mod.rs index 7db5239..e296595 100644 --- a/src/drivers/atapi/mod.rs +++ b/src/drivers/atapi/mod.rs @@ -54,7 +54,7 @@ static ATAPI_SIG: [u8; 4] = [ ]; lazy_static! { - static ref DRIVE: Mutex> = { + pub static ref DRIVE: Mutex> = { Mutex::new(ATABus::discover_atapi_drive()) }; } @@ -80,7 +80,7 @@ pub fn init() { } #[derive(Debug)] -struct ATABus { +pub struct ATABus { base_port: u16, // IO ports @@ -99,7 +99,7 @@ struct ATABus { current_drive: u8, - block: [u8; CD_SECTOR_SIZE], + pub block: [u8; CD_SECTOR_SIZE], } impl ATABus { @@ -108,7 +108,7 @@ impl ATABus { unsafe { primary_bus.dcr.write(ATA_SRST); - primary_bus.dcr.write(ATA_INTERRUPT_DISABLE); + primary_bus.dcr.write(0); } primary_bus.select_drive(ATA_DRIVE_MASTER); @@ -125,7 +125,7 @@ impl ATABus { unsafe { secondary_bus.dcr.write(ATA_SRST); - primary_bus.dcr.write(ATA_INTERRUPT_DISABLE); + primary_bus.dcr.write(0); } secondary_bus.select_drive(ATA_DRIVE_MASTER); @@ -204,7 +204,7 @@ impl ATABus { } } - fn read_block(&mut self, lba: u32) { + pub fn read_block(&mut self, lba: u32) { let mut packet = SCSIPacket::new(); packet.op_code = SCSI_READ_12; diff --git a/src/interrupts/mod.rs b/src/interrupts/mod.rs index 9ff7524..9a70f34 100644 --- a/src/interrupts/mod.rs +++ b/src/interrupts/mod.rs @@ -3,7 +3,14 @@ use crate::hlt_loop; use crate::memory::gdt; use crate::println; use lazy_static::lazy_static; -use pic::{init_pic, keyboard_interrupt_handler, timer_interrupt_handler, InterruptIndex}; +use pic::{ + init_pic, + keyboard_interrupt_handler, + timer_interrupt_handler, + disk1_interrupt_handler, + disk2_interrupt_handler, + InterruptIndex +}; use x86_64::structures::idt::PageFaultErrorCode; use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; @@ -21,6 +28,8 @@ lazy_static! { } idt[InterruptIndex::Timer.as_usize()].set_handler_fn(timer_interrupt_handler); idt[InterruptIndex::Keyboard.as_usize()].set_handler_fn(keyboard_interrupt_handler); + idt[InterruptIndex::HardDisk1.as_usize()].set_handler_fn(disk1_interrupt_handler); + idt[InterruptIndex::HardDisk2.as_usize()].set_handler_fn(disk2_interrupt_handler); idt }; } diff --git a/src/interrupts/pic/disk.rs b/src/interrupts/pic/disk.rs new file mode 100644 index 0000000..2288cf9 --- /dev/null +++ b/src/interrupts/pic/disk.rs @@ -0,0 +1,24 @@ +use crate::println; + +use super::{InterruptIndex, PICS}; +use x86_64::structures::idt::InterruptStackFrame; + +fn disk_interrupt_handler(disk: u16) { + println!("Received disk {} interrupt", disk); +} + +pub extern "x86-interrupt" fn disk1_interrupt_handler(_stack_frame: InterruptStackFrame) { + disk_interrupt_handler(1); + unsafe { + PICS.lock() + .notify_end_of_interrupt(InterruptIndex::HardDisk1.as_u8()); + } +} + +pub extern "x86-interrupt" fn disk2_interrupt_handler(_stack_frame: InterruptStackFrame) { + disk_interrupt_handler(2); + unsafe { + PICS.lock() + .notify_end_of_interrupt(InterruptIndex::HardDisk2.as_u8()); + } +} \ No newline at end of file diff --git a/src/interrupts/pic/mod.rs b/src/interrupts/pic/mod.rs index 3a79a30..f27d68d 100644 --- a/src/interrupts/pic/mod.rs +++ b/src/interrupts/pic/mod.rs @@ -2,9 +2,11 @@ use crate::println; pub use keyboard::keyboard_interrupt_handler; use pic8259::ChainedPics; pub use pit::timer_interrupt_handler; +pub use disk::{disk1_interrupt_handler, disk2_interrupt_handler}; pub mod keyboard; pub mod pit; +pub mod disk; pub const PIC_1_OFFSET: u8 = 32; pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8; @@ -14,6 +16,8 @@ pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8; pub enum InterruptIndex { Timer = PIC_1_OFFSET, Keyboard, + HardDisk1 = PIC_1_OFFSET + 14, + HardDisk2 = PIC_1_OFFSET + 15, } impl InterruptIndex { diff --git a/src/lib.rs b/src/lib.rs index 6a8d1dc..3e7fcb4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,8 +40,10 @@ pub fn init(boot_info: &BootInformation) { println!("Starting init"); memory::init(boot_info); memory::gdt::init_gdt(); - drivers::atapi::init(); interrupts::init_idt(); + drivers::atapi::init(); + drivers::atapi::DRIVE.lock().as_mut().unwrap().read_block(0); + serial_println!("{:x?}", drivers::atapi::DRIVE.lock().as_mut().unwrap().block); vga::change_color(ColorCode::new(Color::LightGreen, Color::Black)); }