1004e86ffSlogin use super::{_port, hba::HbaCmdTable, virt_2_phys}; 2b087521eSChiichen use crate::driver::base::block::block_device::{BlockDevice, BlockId}; 3b087521eSChiichen use crate::driver::base::block::disk_info::Partition; 4b087521eSChiichen use crate::driver::base::block::SeekFrom; 5b087521eSChiichen use crate::driver::base::device::{Device, DeviceType, KObject}; 6004e86ffSlogin use crate::driver::disk::ahci::HBA_PxIS_TFES; 7004e86ffSlogin use crate::filesystem::mbr::MbrDiskPartionTable; 8660a04ceSlogin use crate::include::bindings::bindings::verify_area; 9004e86ffSlogin 10b087521eSChiichen use crate::kdebug; 11004e86ffSlogin use crate::libs::{spinlock::SpinLock, vec_cursor::VecCursor}; 12004e86ffSlogin use crate::mm::phys_2_virt; 13676b8ef6SMork use crate::syscall::SystemError; 14004e86ffSlogin use crate::{ 15004e86ffSlogin driver::disk::ahci::hba::{ 16004e86ffSlogin FisRegH2D, FisType, HbaCmdHeader, ATA_CMD_READ_DMA_EXT, ATA_CMD_WRITE_DMA_EXT, 17004e86ffSlogin ATA_DEV_BUSY, ATA_DEV_DRQ, 18004e86ffSlogin }, 19004e86ffSlogin kerror, 20004e86ffSlogin }; 21004e86ffSlogin 22004e86ffSlogin use alloc::sync::Weak; 23004e86ffSlogin use alloc::{string::String, sync::Arc, vec::Vec}; 24004e86ffSlogin 25004e86ffSlogin use core::fmt::Debug; 26*7ae679ddSLoGin use core::sync::atomic::{compiler_fence, Ordering}; 27004e86ffSlogin use core::{mem::size_of, ptr::write_bytes}; 28004e86ffSlogin 29004e86ffSlogin /// @brief: 只支持MBR分区格式的磁盘结构体 30004e86ffSlogin pub struct AhciDisk { 31004e86ffSlogin pub name: String, 32004e86ffSlogin pub flags: u16, // 磁盘的状态flags 33004e86ffSlogin pub partitions: Vec<Arc<Partition>>, // 磁盘分区数组 34004e86ffSlogin // port: &'static mut HbaPort, // 控制硬盘的端口 35004e86ffSlogin pub ctrl_num: u8, 36004e86ffSlogin pub port_num: u8, 37004e86ffSlogin /// 指向LockAhciDisk的弱引用 38004e86ffSlogin self_ref: Weak<LockedAhciDisk>, 39004e86ffSlogin } 40004e86ffSlogin 41004e86ffSlogin /// @brief: 带锁的AhciDisk 42004e86ffSlogin #[derive(Debug)] 43004e86ffSlogin pub struct LockedAhciDisk(pub SpinLock<AhciDisk>); 44004e86ffSlogin /// 函数实现 45004e86ffSlogin impl Debug for AhciDisk { 46004e86ffSlogin fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 47004e86ffSlogin write!( 48004e86ffSlogin f, 49004e86ffSlogin "{{ name: {}, flags: {}, part_s: {:?} }}", 50004e86ffSlogin self.name, self.flags, self.partitions 51004e86ffSlogin )?; 52004e86ffSlogin return Ok(()); 53004e86ffSlogin } 54004e86ffSlogin } 55004e86ffSlogin 56004e86ffSlogin impl AhciDisk { 57004e86ffSlogin fn read_at( 58004e86ffSlogin &self, 59b087521eSChiichen lba_id_start: BlockId, // 起始lba编号 60004e86ffSlogin count: usize, // 读取lba的数量 61004e86ffSlogin buf: &mut [u8], 62676b8ef6SMork ) -> Result<usize, SystemError> { 63*7ae679ddSLoGin assert!((buf.len() & 511) == 0); 64004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 65004e86ffSlogin let check_length = ((count - 1) >> 4) + 1; // prdt length 66*7ae679ddSLoGin if count * 512 > buf.len() || check_length > 8 as usize { 67004e86ffSlogin kerror!("ahci read: e2big"); 68004e86ffSlogin // 不可能的操作 69676b8ef6SMork return Err(SystemError::E2BIG); 70004e86ffSlogin } else if count == 0 { 71004e86ffSlogin return Ok(0); 72004e86ffSlogin } 73004e86ffSlogin 74004e86ffSlogin let port = _port(self.ctrl_num, self.port_num); 75004e86ffSlogin volatile_write!(port.is, u32::MAX); // Clear pending interrupt bits 76004e86ffSlogin 77004e86ffSlogin let slot = port.find_cmdslot().unwrap_or(u32::MAX); 78004e86ffSlogin 79004e86ffSlogin if slot == u32::MAX { 80676b8ef6SMork return Err(SystemError::EIO); 81004e86ffSlogin } 82004e86ffSlogin 83004e86ffSlogin #[allow(unused_unsafe)] 84004e86ffSlogin let cmdheader: &mut HbaCmdHeader = unsafe { 85004e86ffSlogin (phys_2_virt( 86004e86ffSlogin volatile_read!(port.clb) as usize 87004e86ffSlogin + slot as usize * size_of::<HbaCmdHeader>() as usize, 88004e86ffSlogin ) as *mut HbaCmdHeader) 89004e86ffSlogin .as_mut() 90004e86ffSlogin .unwrap() 91004e86ffSlogin }; 92004e86ffSlogin 93*7ae679ddSLoGin cmdheader.cfl = (size_of::<FisRegH2D>() / size_of::<u32>()) as u8; 94004e86ffSlogin 95004e86ffSlogin volatile_set_bit!(cmdheader.cfl, 1 << 6, false); // Read/Write bit : Read from device 96004e86ffSlogin volatile_write!(cmdheader.prdtl, check_length as u16); // PRDT entries count 97004e86ffSlogin 98004e86ffSlogin // 设置数据存放地址 99004e86ffSlogin let mut buf_ptr = buf as *mut [u8] as *mut usize as usize; 100660a04ceSlogin 101660a04ceSlogin // 由于目前的内存管理机制无法把用户空间的内存地址转换为物理地址,所以只能先把数据拷贝到内核空间 102660a04ceSlogin // TODO:在内存管理重构后,可以直接使用用户空间的内存地址 103660a04ceSlogin let user_buf = if unsafe { verify_area(buf_ptr as u64, buf.len() as u64) } { 104660a04ceSlogin true 105660a04ceSlogin } else { 106660a04ceSlogin false 107660a04ceSlogin }; 108660a04ceSlogin let mut kbuf = if user_buf { 109*7ae679ddSLoGin let mut x: Vec<u8> = Vec::new(); 110*7ae679ddSLoGin x.resize(buf.len(), 0); 111660a04ceSlogin Some(x) 112660a04ceSlogin } else { 113660a04ceSlogin None 114660a04ceSlogin }; 115660a04ceSlogin 116660a04ceSlogin if kbuf.is_some() { 117660a04ceSlogin buf_ptr = kbuf.as_mut().unwrap().as_mut_ptr() as usize; 118660a04ceSlogin } 119660a04ceSlogin 120004e86ffSlogin #[allow(unused_unsafe)] 121004e86ffSlogin let cmdtbl = unsafe { 122004e86ffSlogin (phys_2_virt(volatile_read!(cmdheader.ctba) as usize) as *mut HbaCmdTable) 123004e86ffSlogin .as_mut() 124004e86ffSlogin .unwrap() // 必须使用 as_mut ,得到的才是原来的变量 125004e86ffSlogin }; 126004e86ffSlogin let mut tmp_count = count; 127004e86ffSlogin 128004e86ffSlogin unsafe { 129004e86ffSlogin // 清空整个table的旧数据 130004e86ffSlogin write_bytes(cmdtbl, 0, 1); 131004e86ffSlogin } 132*7ae679ddSLoGin // kdebug!("cmdheader.prdtl={}", volatile_read!(cmdheader.prdtl)); 133004e86ffSlogin 134004e86ffSlogin // 8K bytes (16 sectors) per PRDT 135004e86ffSlogin for i in 0..((volatile_read!(cmdheader.prdtl) - 1) as usize) { 136004e86ffSlogin volatile_write!(cmdtbl.prdt_entry[i].dba, virt_2_phys(buf_ptr) as u64); 137*7ae679ddSLoGin cmdtbl.prdt_entry[i].dbc = 8 * 1024 - 1; 138004e86ffSlogin volatile_set_bit!(cmdtbl.prdt_entry[i].dbc, 1 << 31, true); // 允许中断 prdt_entry.i 139004e86ffSlogin buf_ptr += 8 * 1024; 140004e86ffSlogin tmp_count -= 16; 141004e86ffSlogin } 142004e86ffSlogin 143004e86ffSlogin // Last entry 144004e86ffSlogin let las = (volatile_read!(cmdheader.prdtl) - 1) as usize; 145004e86ffSlogin volatile_write!(cmdtbl.prdt_entry[las].dba, virt_2_phys(buf_ptr) as u64); 146*7ae679ddSLoGin cmdtbl.prdt_entry[las].dbc = ((tmp_count << 9) - 1) as u32; // 数据长度 147*7ae679ddSLoGin 148004e86ffSlogin volatile_set_bit!(cmdtbl.prdt_entry[las].dbc, 1 << 31, true); // 允许中断 149004e86ffSlogin 150004e86ffSlogin // 设置命令 151004e86ffSlogin let cmdfis = unsafe { 152004e86ffSlogin ((&mut cmdtbl.cfis) as *mut [u8] as *mut usize as *mut FisRegH2D) 153004e86ffSlogin .as_mut() 154004e86ffSlogin .unwrap() 155004e86ffSlogin }; 156004e86ffSlogin volatile_write!(cmdfis.fis_type, FisType::RegH2D as u8); 157004e86ffSlogin volatile_set_bit!(cmdfis.pm, 1 << 7, true); // command_bit set 158004e86ffSlogin volatile_write!(cmdfis.command, ATA_CMD_READ_DMA_EXT); 159004e86ffSlogin 160004e86ffSlogin volatile_write!(cmdfis.lba0, (lba_id_start & 0xFF) as u8); 161004e86ffSlogin volatile_write!(cmdfis.lba1, ((lba_id_start >> 8) & 0xFF) as u8); 162004e86ffSlogin volatile_write!(cmdfis.lba2, ((lba_id_start >> 16) & 0xFF) as u8); 163004e86ffSlogin volatile_write!(cmdfis.lba3, ((lba_id_start >> 24) & 0xFF) as u8); 164004e86ffSlogin volatile_write!(cmdfis.lba4, ((lba_id_start >> 32) & 0xFF) as u8); 165004e86ffSlogin volatile_write!(cmdfis.lba5, ((lba_id_start >> 40) & 0xFF) as u8); 166004e86ffSlogin 167004e86ffSlogin volatile_write!(cmdfis.countl, (count & 0xFF) as u8); 168004e86ffSlogin volatile_write!(cmdfis.counth, ((count >> 8) & 0xFF) as u8); 169004e86ffSlogin 170004e86ffSlogin volatile_write!(cmdfis.device, 1 << 6); // LBA Mode 171004e86ffSlogin 172004e86ffSlogin // 等待之前的操作完成 173004e86ffSlogin let mut spin_count = 0; 174004e86ffSlogin const SPIN_LIMIT: u32 = 10000; 175004e86ffSlogin 176004e86ffSlogin while (volatile_read!(port.tfd) as u8 & (ATA_DEV_BUSY | ATA_DEV_DRQ)) > 0 177004e86ffSlogin && spin_count < SPIN_LIMIT 178004e86ffSlogin { 179004e86ffSlogin spin_count += 1; 180004e86ffSlogin } 181004e86ffSlogin 182004e86ffSlogin if spin_count == SPIN_LIMIT { 183004e86ffSlogin kerror!("Port is hung"); 184676b8ef6SMork return Err(SystemError::EIO); 185004e86ffSlogin } 186004e86ffSlogin 187004e86ffSlogin volatile_set_bit!(port.ci, 1 << slot, true); // Issue command 188004e86ffSlogin // kdebug!("To wait ahci read complete."); 189004e86ffSlogin // 等待操作完成 190004e86ffSlogin loop { 191004e86ffSlogin if (volatile_read!(port.ci) & (1 << slot)) == 0 { 192004e86ffSlogin break; 193004e86ffSlogin } 194004e86ffSlogin if (volatile_read!(port.is) & HBA_PxIS_TFES) > 0 { 195004e86ffSlogin kerror!("Read disk error"); 196676b8ef6SMork return Err(SystemError::EIO); 197004e86ffSlogin } 198004e86ffSlogin } 199004e86ffSlogin 200660a04ceSlogin if kbuf.is_some() { 201660a04ceSlogin buf.copy_from_slice(kbuf.as_ref().unwrap()); 202660a04ceSlogin } 203660a04ceSlogin 204004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 205004e86ffSlogin // successfully read 206004e86ffSlogin return Ok(count * 512); 207004e86ffSlogin } 208004e86ffSlogin 209004e86ffSlogin fn write_at( 210004e86ffSlogin &self, 211b087521eSChiichen lba_id_start: BlockId, 212004e86ffSlogin count: usize, 213004e86ffSlogin buf: &[u8], 214676b8ef6SMork ) -> Result<usize, SystemError> { 215*7ae679ddSLoGin assert!((buf.len() & 511) == 0); 216004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 217004e86ffSlogin let check_length = ((count - 1) >> 4) + 1; // prdt length 218*7ae679ddSLoGin if count * 512 > buf.len() || check_length > 8 as usize { 219004e86ffSlogin // 不可能的操作 220676b8ef6SMork return Err(SystemError::E2BIG); 221004e86ffSlogin } else if count == 0 { 222004e86ffSlogin return Ok(0); 223004e86ffSlogin } 224004e86ffSlogin 225004e86ffSlogin let port = _port(self.ctrl_num, self.port_num); 226004e86ffSlogin 227004e86ffSlogin volatile_write!(port.is, u32::MAX); // Clear pending interrupt bits 228004e86ffSlogin 229004e86ffSlogin let slot = port.find_cmdslot().unwrap_or(u32::MAX); 230004e86ffSlogin 231004e86ffSlogin if slot == u32::MAX { 232676b8ef6SMork return Err(SystemError::EIO); 233004e86ffSlogin } 234004e86ffSlogin 235004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 236004e86ffSlogin #[allow(unused_unsafe)] 237004e86ffSlogin let cmdheader: &mut HbaCmdHeader = unsafe { 238004e86ffSlogin (phys_2_virt( 239004e86ffSlogin volatile_read!(port.clb) as usize 240004e86ffSlogin + slot as usize * size_of::<HbaCmdHeader>() as usize, 241004e86ffSlogin ) as *mut HbaCmdHeader) 242004e86ffSlogin .as_mut() 243004e86ffSlogin .unwrap() 244004e86ffSlogin }; 245004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 246004e86ffSlogin 247004e86ffSlogin volatile_write_bit!( 248004e86ffSlogin cmdheader.cfl, 249004e86ffSlogin (1 << 5) - 1 as u8, 250004e86ffSlogin (size_of::<FisRegH2D>() / size_of::<u32>()) as u8 251004e86ffSlogin ); // Command FIS size 252004e86ffSlogin 253004e86ffSlogin volatile_set_bit!(cmdheader.cfl, 7 << 5, true); // (p,c,w)都设置为1, Read/Write bit : Write from device 254004e86ffSlogin volatile_write!(cmdheader.prdtl, check_length as u16); // PRDT entries count 255004e86ffSlogin 256004e86ffSlogin // 设置数据存放地址 257004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 258004e86ffSlogin let mut buf_ptr = buf as *const [u8] as *mut usize as usize; 259660a04ceSlogin 260660a04ceSlogin // 由于目前的内存管理机制无法把用户空间的内存地址转换为物理地址,所以只能先把数据拷贝到内核空间 261660a04ceSlogin // TODO:在内存管理重构后,可以直接使用用户空间的内存地址 262660a04ceSlogin let user_buf = if unsafe { verify_area(buf_ptr as u64, buf.len() as u64) } { 263660a04ceSlogin true 264660a04ceSlogin } else { 265660a04ceSlogin false 266660a04ceSlogin }; 267660a04ceSlogin let mut kbuf = if user_buf { 268660a04ceSlogin let mut x: Vec<u8> = Vec::with_capacity(buf.len()); 269660a04ceSlogin x.resize(buf.len(), 0); 270660a04ceSlogin x.copy_from_slice(buf); 271660a04ceSlogin Some(x) 272660a04ceSlogin } else { 273660a04ceSlogin None 274660a04ceSlogin }; 275660a04ceSlogin 276660a04ceSlogin if kbuf.is_some() { 277660a04ceSlogin buf_ptr = kbuf.as_mut().unwrap().as_mut_ptr() as usize; 278660a04ceSlogin } 279660a04ceSlogin 280004e86ffSlogin #[allow(unused_unsafe)] 281004e86ffSlogin let cmdtbl = unsafe { 282004e86ffSlogin (phys_2_virt(volatile_read!(cmdheader.ctba) as usize) as *mut HbaCmdTable) 283004e86ffSlogin .as_mut() 284004e86ffSlogin .unwrap() 285004e86ffSlogin }; 286004e86ffSlogin let mut tmp_count = count; 287004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 288004e86ffSlogin 289004e86ffSlogin unsafe { 290004e86ffSlogin // 清空整个table的旧数据 291004e86ffSlogin write_bytes(cmdtbl, 0, 1); 292004e86ffSlogin } 293004e86ffSlogin 294004e86ffSlogin // 8K bytes (16 sectors) per PRDT 295004e86ffSlogin for i in 0..((volatile_read!(cmdheader.prdtl) - 1) as usize) { 296004e86ffSlogin volatile_write!(cmdtbl.prdt_entry[i].dba, virt_2_phys(buf_ptr) as u64); 297004e86ffSlogin volatile_write_bit!(cmdtbl.prdt_entry[i].dbc, (1 << 22) - 1, 8 * 1024 - 1); // 数据长度 298004e86ffSlogin volatile_set_bit!(cmdtbl.prdt_entry[i].dbc, 1 << 31, true); // 允许中断 299004e86ffSlogin buf_ptr += 8 * 1024; 300004e86ffSlogin tmp_count -= 16; 301004e86ffSlogin } 302004e86ffSlogin 303004e86ffSlogin // Last entry 304004e86ffSlogin let las = (volatile_read!(cmdheader.prdtl) - 1) as usize; 305004e86ffSlogin volatile_write!(cmdtbl.prdt_entry[las].dba, virt_2_phys(buf_ptr) as u64); 306004e86ffSlogin volatile_set_bit!(cmdtbl.prdt_entry[las].dbc, 1 << 31, true); // 允许中断 307004e86ffSlogin volatile_write_bit!( 308004e86ffSlogin cmdtbl.prdt_entry[las].dbc, 309004e86ffSlogin (1 << 22) - 1, 310004e86ffSlogin ((tmp_count << 9) - 1) as u32 311004e86ffSlogin ); // 数据长度 312004e86ffSlogin 313004e86ffSlogin // 设置命令 314004e86ffSlogin let cmdfis = unsafe { 315004e86ffSlogin ((&mut cmdtbl.cfis) as *mut [u8] as *mut usize as *mut FisRegH2D) 316004e86ffSlogin .as_mut() 317004e86ffSlogin .unwrap() 318004e86ffSlogin }; 319004e86ffSlogin volatile_write!(cmdfis.fis_type, FisType::RegH2D as u8); 320004e86ffSlogin volatile_set_bit!(cmdfis.pm, 1 << 7, true); // command_bit set 321004e86ffSlogin volatile_write!(cmdfis.command, ATA_CMD_WRITE_DMA_EXT); 322004e86ffSlogin 323004e86ffSlogin volatile_write!(cmdfis.lba0, (lba_id_start & 0xFF) as u8); 324004e86ffSlogin volatile_write!(cmdfis.lba1, ((lba_id_start >> 8) & 0xFF) as u8); 325004e86ffSlogin volatile_write!(cmdfis.lba2, ((lba_id_start >> 16) & 0xFF) as u8); 326004e86ffSlogin volatile_write!(cmdfis.lba3, ((lba_id_start >> 24) & 0xFF) as u8); 327004e86ffSlogin volatile_write!(cmdfis.lba4, ((lba_id_start >> 32) & 0xFF) as u8); 328004e86ffSlogin volatile_write!(cmdfis.lba5, ((lba_id_start >> 40) & 0xFF) as u8); 329004e86ffSlogin 330004e86ffSlogin volatile_write!(cmdfis.countl, (count & 0xFF) as u8); 331004e86ffSlogin volatile_write!(cmdfis.counth, ((count >> 8) & 0xFF) as u8); 332004e86ffSlogin 333004e86ffSlogin volatile_write!(cmdfis.device, 1 << 6); // LBA Mode 334004e86ffSlogin 335004e86ffSlogin volatile_set_bit!(port.ci, 1 << slot, true); // Issue command 336004e86ffSlogin 337004e86ffSlogin // 等待操作完成 338004e86ffSlogin loop { 339004e86ffSlogin if (volatile_read!(port.ci) & (1 << slot)) == 0 { 340004e86ffSlogin break; 341004e86ffSlogin } 342004e86ffSlogin if (volatile_read!(port.is) & HBA_PxIS_TFES) > 0 { 343004e86ffSlogin kerror!("Write disk error"); 344676b8ef6SMork return Err(SystemError::EIO); 345004e86ffSlogin } 346004e86ffSlogin } 347004e86ffSlogin 348004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 349004e86ffSlogin // successfully read 350004e86ffSlogin return Ok(count * 512); 351004e86ffSlogin } 352004e86ffSlogin 353676b8ef6SMork fn sync(&self) -> Result<(), SystemError> { 354004e86ffSlogin // 由于目前没有block cache, 因此sync返回成功即可 355004e86ffSlogin return Ok(()); 356004e86ffSlogin } 357004e86ffSlogin } 358004e86ffSlogin 359004e86ffSlogin impl LockedAhciDisk { 360004e86ffSlogin pub fn new( 361004e86ffSlogin name: String, 362004e86ffSlogin flags: u16, 363004e86ffSlogin ctrl_num: u8, 364004e86ffSlogin port_num: u8, 365676b8ef6SMork ) -> Result<Arc<LockedAhciDisk>, SystemError> { 366004e86ffSlogin // 构建磁盘结构体 367004e86ffSlogin let result: Arc<LockedAhciDisk> = Arc::new(LockedAhciDisk(SpinLock::new(AhciDisk { 368004e86ffSlogin name, 369004e86ffSlogin flags, 370004e86ffSlogin partitions: Default::default(), 371004e86ffSlogin ctrl_num, 372004e86ffSlogin port_num, 373004e86ffSlogin self_ref: Weak::default(), 374004e86ffSlogin }))); 375004e86ffSlogin 376004e86ffSlogin let table: MbrDiskPartionTable = result.read_mbr_table()?; 377004e86ffSlogin 378004e86ffSlogin // 求出有多少可用分区 379004e86ffSlogin for i in 0..4 { 380*7ae679ddSLoGin compiler_fence(Ordering::SeqCst); 381004e86ffSlogin if table.dpte[i].part_type != 0 { 382*7ae679ddSLoGin let w = Arc::downgrade(&result); 383*7ae679ddSLoGin result.0.lock().partitions.push(Partition::new( 384004e86ffSlogin table.dpte[i].starting_sector() as u64, 385004e86ffSlogin table.dpte[i].starting_lba as u64, 386004e86ffSlogin table.dpte[i].total_sectors as u64, 387*7ae679ddSLoGin w, 388004e86ffSlogin i as u16, 389004e86ffSlogin )); 390004e86ffSlogin } 391004e86ffSlogin } 392*7ae679ddSLoGin 393*7ae679ddSLoGin result.0.lock().self_ref = Arc::downgrade(&result); 394*7ae679ddSLoGin 395004e86ffSlogin return Ok(result); 396004e86ffSlogin } 397004e86ffSlogin 398004e86ffSlogin /// @brief: 从磁盘中读取 MBR 分区表结构体 TODO: Cursor 399676b8ef6SMork pub fn read_mbr_table(&self) -> Result<MbrDiskPartionTable, SystemError> { 400004e86ffSlogin let mut table: MbrDiskPartionTable = Default::default(); 401004e86ffSlogin 402004e86ffSlogin // 数据缓冲区 403004e86ffSlogin let mut buf: Vec<u8> = Vec::new(); 404004e86ffSlogin buf.resize(size_of::<MbrDiskPartionTable>(), 0); 405004e86ffSlogin 406*7ae679ddSLoGin self.read_at(0, 1, &mut buf)?; 407004e86ffSlogin // 创建 Cursor 用于按字节读取 408004e86ffSlogin let mut cursor = VecCursor::new(buf); 409004e86ffSlogin cursor.seek(SeekFrom::SeekCurrent(446))?; 410004e86ffSlogin 411004e86ffSlogin for i in 0..4 { 412b087521eSChiichen kdebug!("infomation of partition {}:\n", i); 413004e86ffSlogin 414004e86ffSlogin table.dpte[i].flags = cursor.read_u8()?; 415004e86ffSlogin table.dpte[i].starting_head = cursor.read_u8()?; 416004e86ffSlogin table.dpte[i].starting_sector_cylinder = cursor.read_u16()?; 417004e86ffSlogin table.dpte[i].part_type = cursor.read_u8()?; 418004e86ffSlogin table.dpte[i].ending_head = cursor.read_u8()?; 419004e86ffSlogin table.dpte[i].ending_sector_cylingder = cursor.read_u16()?; 420004e86ffSlogin table.dpte[i].starting_lba = cursor.read_u32()?; 421004e86ffSlogin table.dpte[i].total_sectors = cursor.read_u32()?; 422004e86ffSlogin 423b087521eSChiichen kdebug!("dpte[i] = {:?}", table.dpte[i]); 424004e86ffSlogin } 425004e86ffSlogin table.bs_trailsig = cursor.read_u16()?; 426004e86ffSlogin // kdebug!("bs_trailsig = {}", unsafe { 427004e86ffSlogin // read_unaligned(addr_of!(table.bs_trailsig)) 428004e86ffSlogin // }); 429004e86ffSlogin 430004e86ffSlogin return Ok(table); 431004e86ffSlogin } 432004e86ffSlogin } 433004e86ffSlogin 434b087521eSChiichen impl KObject for LockedAhciDisk {} 435b087521eSChiichen 436b087521eSChiichen impl Device for LockedAhciDisk { 437b087521eSChiichen fn dev_type(&self) -> DeviceType { 438b087521eSChiichen return DeviceType::Block; 439b087521eSChiichen } 440b087521eSChiichen 441b087521eSChiichen fn as_any_ref(&self) -> &dyn core::any::Any { 442b087521eSChiichen return self; 443b087521eSChiichen } 444b087521eSChiichen 445b087521eSChiichen fn id_table(&self) -> crate::driver::base::device::IdTable { 446b087521eSChiichen todo!() 447b087521eSChiichen } 448b087521eSChiichen 449b087521eSChiichen fn set_sys_info(&self, _sys_info: Option<Arc<dyn crate::filesystem::vfs::IndexNode>>) { 450b087521eSChiichen todo!() 451b087521eSChiichen } 452b087521eSChiichen 453b087521eSChiichen fn sys_info(&self) -> Option<Arc<dyn crate::filesystem::vfs::IndexNode>> { 454b087521eSChiichen todo!() 455b087521eSChiichen } 456b087521eSChiichen } 457b087521eSChiichen 458004e86ffSlogin impl BlockDevice for LockedAhciDisk { 459004e86ffSlogin #[inline] 460004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 461004e86ffSlogin self 462004e86ffSlogin } 463004e86ffSlogin 464004e86ffSlogin #[inline] 465004e86ffSlogin fn blk_size_log2(&self) -> u8 { 466004e86ffSlogin 9 467004e86ffSlogin } 468004e86ffSlogin 469676b8ef6SMork fn sync(&self) -> Result<(), SystemError> { 470004e86ffSlogin return self.0.lock().sync(); 471004e86ffSlogin } 472004e86ffSlogin 473004e86ffSlogin #[inline] 474b087521eSChiichen fn device(&self) -> Arc<dyn Device> { 475004e86ffSlogin return self.0.lock().self_ref.upgrade().unwrap(); 476004e86ffSlogin } 477004e86ffSlogin 478004e86ffSlogin fn block_size(&self) -> usize { 479004e86ffSlogin todo!() 480004e86ffSlogin } 481004e86ffSlogin 482004e86ffSlogin fn partitions(&self) -> Vec<Arc<Partition>> { 483004e86ffSlogin return self.0.lock().partitions.clone(); 484004e86ffSlogin } 485b087521eSChiichen 486b087521eSChiichen #[inline] 487b087521eSChiichen fn read_at( 488b087521eSChiichen &self, 489b087521eSChiichen lba_id_start: BlockId, // 起始lba编号 490b087521eSChiichen count: usize, // 读取lba的数量 491b087521eSChiichen buf: &mut [u8], 492b087521eSChiichen ) -> Result<usize, SystemError> { 493b087521eSChiichen self.0.lock().read_at(lba_id_start, count, buf) 494b087521eSChiichen } 495b087521eSChiichen 496b087521eSChiichen #[inline] 497b087521eSChiichen fn write_at( 498b087521eSChiichen &self, 499b087521eSChiichen lba_id_start: BlockId, 500b087521eSChiichen count: usize, 501b087521eSChiichen buf: &[u8], 502b087521eSChiichen ) -> Result<usize, SystemError> { 503b087521eSChiichen self.0.lock().write_at(lba_id_start, count, buf) 504b087521eSChiichen } 505004e86ffSlogin } 506