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; 506d5e247SLoGin use crate::driver::base::device::bus::Bus; 6*a03c4f9dSLoGin use crate::driver::base::device::driver::Driver; 706d5e247SLoGin use crate::driver::base::device::{Device, DeviceType, IdTable}; 806d5e247SLoGin use crate::driver::base::kobject::{KObjType, KObject, KObjectState}; 906d5e247SLoGin use crate::driver::base::kset::KSet; 10004e86ffSlogin use crate::driver::disk::ahci::HBA_PxIS_TFES; 11*a03c4f9dSLoGin 1206d5e247SLoGin use crate::filesystem::kernfs::KernFSInode; 13004e86ffSlogin use crate::filesystem::mbr::MbrDiskPartionTable; 14660a04ceSlogin use crate::include::bindings::bindings::verify_area; 15004e86ffSlogin 16b087521eSChiichen use crate::kdebug; 1706d5e247SLoGin use crate::libs::rwlock::{RwLockReadGuard, RwLockWriteGuard}; 18004e86ffSlogin use crate::libs::{spinlock::SpinLock, vec_cursor::VecCursor}; 19004e86ffSlogin use crate::mm::phys_2_virt; 20676b8ef6SMork use crate::syscall::SystemError; 21004e86ffSlogin use crate::{ 22004e86ffSlogin driver::disk::ahci::hba::{ 23004e86ffSlogin FisRegH2D, FisType, HbaCmdHeader, ATA_CMD_READ_DMA_EXT, ATA_CMD_WRITE_DMA_EXT, 24004e86ffSlogin ATA_DEV_BUSY, ATA_DEV_DRQ, 25004e86ffSlogin }, 26004e86ffSlogin kerror, 27004e86ffSlogin }; 28004e86ffSlogin 29004e86ffSlogin use alloc::sync::Weak; 30004e86ffSlogin use alloc::{string::String, sync::Arc, vec::Vec}; 31004e86ffSlogin 32004e86ffSlogin use core::fmt::Debug; 337ae679ddSLoGin use core::sync::atomic::{compiler_fence, Ordering}; 34004e86ffSlogin use core::{mem::size_of, ptr::write_bytes}; 35004e86ffSlogin 36004e86ffSlogin /// @brief: 只支持MBR分区格式的磁盘结构体 37004e86ffSlogin pub struct AhciDisk { 38004e86ffSlogin pub name: String, 39004e86ffSlogin pub flags: u16, // 磁盘的状态flags 40004e86ffSlogin pub partitions: Vec<Arc<Partition>>, // 磁盘分区数组 41004e86ffSlogin // port: &'static mut HbaPort, // 控制硬盘的端口 42004e86ffSlogin pub ctrl_num: u8, 43004e86ffSlogin pub port_num: u8, 44004e86ffSlogin /// 指向LockAhciDisk的弱引用 45004e86ffSlogin self_ref: Weak<LockedAhciDisk>, 46004e86ffSlogin } 47004e86ffSlogin 48004e86ffSlogin /// @brief: 带锁的AhciDisk 49004e86ffSlogin #[derive(Debug)] 50004e86ffSlogin pub struct LockedAhciDisk(pub SpinLock<AhciDisk>); 51004e86ffSlogin /// 函数实现 52004e86ffSlogin impl Debug for AhciDisk { 53004e86ffSlogin fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 54004e86ffSlogin write!( 55004e86ffSlogin f, 56004e86ffSlogin "{{ name: {}, flags: {}, part_s: {:?} }}", 57004e86ffSlogin self.name, self.flags, self.partitions 58004e86ffSlogin )?; 59004e86ffSlogin return Ok(()); 60004e86ffSlogin } 61004e86ffSlogin } 62004e86ffSlogin 63004e86ffSlogin impl AhciDisk { 64004e86ffSlogin fn read_at( 65004e86ffSlogin &self, 66b087521eSChiichen lba_id_start: BlockId, // 起始lba编号 67004e86ffSlogin count: usize, // 读取lba的数量 68004e86ffSlogin buf: &mut [u8], 69676b8ef6SMork ) -> Result<usize, SystemError> { 707ae679ddSLoGin assert!((buf.len() & 511) == 0); 71004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 72004e86ffSlogin let check_length = ((count - 1) >> 4) + 1; // prdt length 737ae679ddSLoGin if count * 512 > buf.len() || check_length > 8 as usize { 74004e86ffSlogin kerror!("ahci read: e2big"); 75004e86ffSlogin // 不可能的操作 76676b8ef6SMork return Err(SystemError::E2BIG); 77004e86ffSlogin } else if count == 0 { 78004e86ffSlogin return Ok(0); 79004e86ffSlogin } 80004e86ffSlogin 81004e86ffSlogin let port = _port(self.ctrl_num, self.port_num); 82004e86ffSlogin volatile_write!(port.is, u32::MAX); // Clear pending interrupt bits 83004e86ffSlogin 84004e86ffSlogin let slot = port.find_cmdslot().unwrap_or(u32::MAX); 85004e86ffSlogin 86004e86ffSlogin if slot == u32::MAX { 87676b8ef6SMork return Err(SystemError::EIO); 88004e86ffSlogin } 89004e86ffSlogin 90004e86ffSlogin #[allow(unused_unsafe)] 91004e86ffSlogin let cmdheader: &mut HbaCmdHeader = unsafe { 92004e86ffSlogin (phys_2_virt( 93004e86ffSlogin volatile_read!(port.clb) as usize 94004e86ffSlogin + slot as usize * size_of::<HbaCmdHeader>() as usize, 95004e86ffSlogin ) as *mut HbaCmdHeader) 96004e86ffSlogin .as_mut() 97004e86ffSlogin .unwrap() 98004e86ffSlogin }; 99004e86ffSlogin 1007ae679ddSLoGin cmdheader.cfl = (size_of::<FisRegH2D>() / size_of::<u32>()) as u8; 101004e86ffSlogin 102004e86ffSlogin volatile_set_bit!(cmdheader.cfl, 1 << 6, false); // Read/Write bit : Read from device 103004e86ffSlogin volatile_write!(cmdheader.prdtl, check_length as u16); // PRDT entries count 104004e86ffSlogin 105004e86ffSlogin // 设置数据存放地址 106004e86ffSlogin let mut buf_ptr = buf as *mut [u8] as *mut usize as usize; 107660a04ceSlogin 108660a04ceSlogin // 由于目前的内存管理机制无法把用户空间的内存地址转换为物理地址,所以只能先把数据拷贝到内核空间 109660a04ceSlogin // TODO:在内存管理重构后,可以直接使用用户空间的内存地址 110660a04ceSlogin let user_buf = if unsafe { verify_area(buf_ptr as u64, buf.len() as u64) } { 111660a04ceSlogin true 112660a04ceSlogin } else { 113660a04ceSlogin false 114660a04ceSlogin }; 115660a04ceSlogin let mut kbuf = if user_buf { 1167ae679ddSLoGin let mut x: Vec<u8> = Vec::new(); 1177ae679ddSLoGin x.resize(buf.len(), 0); 118660a04ceSlogin Some(x) 119660a04ceSlogin } else { 120660a04ceSlogin None 121660a04ceSlogin }; 122660a04ceSlogin 123660a04ceSlogin if kbuf.is_some() { 124660a04ceSlogin buf_ptr = kbuf.as_mut().unwrap().as_mut_ptr() as usize; 125660a04ceSlogin } 126660a04ceSlogin 127004e86ffSlogin #[allow(unused_unsafe)] 128004e86ffSlogin let cmdtbl = unsafe { 129004e86ffSlogin (phys_2_virt(volatile_read!(cmdheader.ctba) as usize) as *mut HbaCmdTable) 130004e86ffSlogin .as_mut() 131004e86ffSlogin .unwrap() // 必须使用 as_mut ,得到的才是原来的变量 132004e86ffSlogin }; 133004e86ffSlogin let mut tmp_count = count; 134004e86ffSlogin 135004e86ffSlogin unsafe { 136004e86ffSlogin // 清空整个table的旧数据 137004e86ffSlogin write_bytes(cmdtbl, 0, 1); 138004e86ffSlogin } 1397ae679ddSLoGin // kdebug!("cmdheader.prdtl={}", volatile_read!(cmdheader.prdtl)); 140004e86ffSlogin 141004e86ffSlogin // 8K bytes (16 sectors) per PRDT 142004e86ffSlogin for i in 0..((volatile_read!(cmdheader.prdtl) - 1) as usize) { 143004e86ffSlogin volatile_write!(cmdtbl.prdt_entry[i].dba, virt_2_phys(buf_ptr) as u64); 1447ae679ddSLoGin cmdtbl.prdt_entry[i].dbc = 8 * 1024 - 1; 145004e86ffSlogin volatile_set_bit!(cmdtbl.prdt_entry[i].dbc, 1 << 31, true); // 允许中断 prdt_entry.i 146004e86ffSlogin buf_ptr += 8 * 1024; 147004e86ffSlogin tmp_count -= 16; 148004e86ffSlogin } 149004e86ffSlogin 150004e86ffSlogin // Last entry 151004e86ffSlogin let las = (volatile_read!(cmdheader.prdtl) - 1) as usize; 152004e86ffSlogin volatile_write!(cmdtbl.prdt_entry[las].dba, virt_2_phys(buf_ptr) as u64); 1537ae679ddSLoGin cmdtbl.prdt_entry[las].dbc = ((tmp_count << 9) - 1) as u32; // 数据长度 1547ae679ddSLoGin 155004e86ffSlogin volatile_set_bit!(cmdtbl.prdt_entry[las].dbc, 1 << 31, true); // 允许中断 156004e86ffSlogin 157004e86ffSlogin // 设置命令 158004e86ffSlogin let cmdfis = unsafe { 159004e86ffSlogin ((&mut cmdtbl.cfis) as *mut [u8] as *mut usize as *mut FisRegH2D) 160004e86ffSlogin .as_mut() 161004e86ffSlogin .unwrap() 162004e86ffSlogin }; 163004e86ffSlogin volatile_write!(cmdfis.fis_type, FisType::RegH2D as u8); 164004e86ffSlogin volatile_set_bit!(cmdfis.pm, 1 << 7, true); // command_bit set 165004e86ffSlogin volatile_write!(cmdfis.command, ATA_CMD_READ_DMA_EXT); 166004e86ffSlogin 167004e86ffSlogin volatile_write!(cmdfis.lba0, (lba_id_start & 0xFF) as u8); 168004e86ffSlogin volatile_write!(cmdfis.lba1, ((lba_id_start >> 8) & 0xFF) as u8); 169004e86ffSlogin volatile_write!(cmdfis.lba2, ((lba_id_start >> 16) & 0xFF) as u8); 170004e86ffSlogin volatile_write!(cmdfis.lba3, ((lba_id_start >> 24) & 0xFF) as u8); 171004e86ffSlogin volatile_write!(cmdfis.lba4, ((lba_id_start >> 32) & 0xFF) as u8); 172004e86ffSlogin volatile_write!(cmdfis.lba5, ((lba_id_start >> 40) & 0xFF) as u8); 173004e86ffSlogin 174004e86ffSlogin volatile_write!(cmdfis.countl, (count & 0xFF) as u8); 175004e86ffSlogin volatile_write!(cmdfis.counth, ((count >> 8) & 0xFF) as u8); 176004e86ffSlogin 177004e86ffSlogin volatile_write!(cmdfis.device, 1 << 6); // LBA Mode 178004e86ffSlogin 179004e86ffSlogin // 等待之前的操作完成 180004e86ffSlogin let mut spin_count = 0; 181004e86ffSlogin const SPIN_LIMIT: u32 = 10000; 182004e86ffSlogin 183004e86ffSlogin while (volatile_read!(port.tfd) as u8 & (ATA_DEV_BUSY | ATA_DEV_DRQ)) > 0 184004e86ffSlogin && spin_count < SPIN_LIMIT 185004e86ffSlogin { 186004e86ffSlogin spin_count += 1; 187004e86ffSlogin } 188004e86ffSlogin 189004e86ffSlogin if spin_count == SPIN_LIMIT { 190004e86ffSlogin kerror!("Port is hung"); 191676b8ef6SMork return Err(SystemError::EIO); 192004e86ffSlogin } 193004e86ffSlogin 194004e86ffSlogin volatile_set_bit!(port.ci, 1 << slot, true); // Issue command 195004e86ffSlogin // kdebug!("To wait ahci read complete."); 196004e86ffSlogin // 等待操作完成 197004e86ffSlogin loop { 198004e86ffSlogin if (volatile_read!(port.ci) & (1 << slot)) == 0 { 199004e86ffSlogin break; 200004e86ffSlogin } 201004e86ffSlogin if (volatile_read!(port.is) & HBA_PxIS_TFES) > 0 { 202004e86ffSlogin kerror!("Read disk error"); 203676b8ef6SMork return Err(SystemError::EIO); 204004e86ffSlogin } 205004e86ffSlogin } 206004e86ffSlogin 207660a04ceSlogin if kbuf.is_some() { 208660a04ceSlogin buf.copy_from_slice(kbuf.as_ref().unwrap()); 209660a04ceSlogin } 210660a04ceSlogin 211004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 212004e86ffSlogin // successfully read 213004e86ffSlogin return Ok(count * 512); 214004e86ffSlogin } 215004e86ffSlogin 216004e86ffSlogin fn write_at( 217004e86ffSlogin &self, 218b087521eSChiichen lba_id_start: BlockId, 219004e86ffSlogin count: usize, 220004e86ffSlogin buf: &[u8], 221676b8ef6SMork ) -> Result<usize, SystemError> { 2227ae679ddSLoGin assert!((buf.len() & 511) == 0); 223004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 224004e86ffSlogin let check_length = ((count - 1) >> 4) + 1; // prdt length 2257ae679ddSLoGin if count * 512 > buf.len() || check_length > 8 as usize { 226004e86ffSlogin // 不可能的操作 227676b8ef6SMork return Err(SystemError::E2BIG); 228004e86ffSlogin } else if count == 0 { 229004e86ffSlogin return Ok(0); 230004e86ffSlogin } 231004e86ffSlogin 232004e86ffSlogin let port = _port(self.ctrl_num, self.port_num); 233004e86ffSlogin 234004e86ffSlogin volatile_write!(port.is, u32::MAX); // Clear pending interrupt bits 235004e86ffSlogin 236004e86ffSlogin let slot = port.find_cmdslot().unwrap_or(u32::MAX); 237004e86ffSlogin 238004e86ffSlogin if slot == u32::MAX { 239676b8ef6SMork return Err(SystemError::EIO); 240004e86ffSlogin } 241004e86ffSlogin 242004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 243004e86ffSlogin #[allow(unused_unsafe)] 244004e86ffSlogin let cmdheader: &mut HbaCmdHeader = unsafe { 245004e86ffSlogin (phys_2_virt( 246004e86ffSlogin volatile_read!(port.clb) as usize 247004e86ffSlogin + slot as usize * size_of::<HbaCmdHeader>() as usize, 248004e86ffSlogin ) as *mut HbaCmdHeader) 249004e86ffSlogin .as_mut() 250004e86ffSlogin .unwrap() 251004e86ffSlogin }; 252004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 253004e86ffSlogin 254004e86ffSlogin volatile_write_bit!( 255004e86ffSlogin cmdheader.cfl, 256004e86ffSlogin (1 << 5) - 1 as u8, 257004e86ffSlogin (size_of::<FisRegH2D>() / size_of::<u32>()) as u8 258004e86ffSlogin ); // Command FIS size 259004e86ffSlogin 260004e86ffSlogin volatile_set_bit!(cmdheader.cfl, 7 << 5, true); // (p,c,w)都设置为1, Read/Write bit : Write from device 261004e86ffSlogin volatile_write!(cmdheader.prdtl, check_length as u16); // PRDT entries count 262004e86ffSlogin 263004e86ffSlogin // 设置数据存放地址 264004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 265004e86ffSlogin let mut buf_ptr = buf as *const [u8] as *mut usize as usize; 266660a04ceSlogin 267660a04ceSlogin // 由于目前的内存管理机制无法把用户空间的内存地址转换为物理地址,所以只能先把数据拷贝到内核空间 268660a04ceSlogin // TODO:在内存管理重构后,可以直接使用用户空间的内存地址 269660a04ceSlogin let user_buf = if unsafe { verify_area(buf_ptr as u64, buf.len() as u64) } { 270660a04ceSlogin true 271660a04ceSlogin } else { 272660a04ceSlogin false 273660a04ceSlogin }; 274660a04ceSlogin let mut kbuf = if user_buf { 275660a04ceSlogin let mut x: Vec<u8> = Vec::with_capacity(buf.len()); 276660a04ceSlogin x.resize(buf.len(), 0); 277660a04ceSlogin x.copy_from_slice(buf); 278660a04ceSlogin Some(x) 279660a04ceSlogin } else { 280660a04ceSlogin None 281660a04ceSlogin }; 282660a04ceSlogin 283660a04ceSlogin if kbuf.is_some() { 284660a04ceSlogin buf_ptr = kbuf.as_mut().unwrap().as_mut_ptr() as usize; 285660a04ceSlogin } 286660a04ceSlogin 287004e86ffSlogin #[allow(unused_unsafe)] 288004e86ffSlogin let cmdtbl = unsafe { 289004e86ffSlogin (phys_2_virt(volatile_read!(cmdheader.ctba) as usize) as *mut HbaCmdTable) 290004e86ffSlogin .as_mut() 291004e86ffSlogin .unwrap() 292004e86ffSlogin }; 293004e86ffSlogin let mut tmp_count = count; 294004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 295004e86ffSlogin 296004e86ffSlogin unsafe { 297004e86ffSlogin // 清空整个table的旧数据 298004e86ffSlogin write_bytes(cmdtbl, 0, 1); 299004e86ffSlogin } 300004e86ffSlogin 301004e86ffSlogin // 8K bytes (16 sectors) per PRDT 302004e86ffSlogin for i in 0..((volatile_read!(cmdheader.prdtl) - 1) as usize) { 303004e86ffSlogin volatile_write!(cmdtbl.prdt_entry[i].dba, virt_2_phys(buf_ptr) as u64); 304004e86ffSlogin volatile_write_bit!(cmdtbl.prdt_entry[i].dbc, (1 << 22) - 1, 8 * 1024 - 1); // 数据长度 305004e86ffSlogin volatile_set_bit!(cmdtbl.prdt_entry[i].dbc, 1 << 31, true); // 允许中断 306004e86ffSlogin buf_ptr += 8 * 1024; 307004e86ffSlogin tmp_count -= 16; 308004e86ffSlogin } 309004e86ffSlogin 310004e86ffSlogin // Last entry 311004e86ffSlogin let las = (volatile_read!(cmdheader.prdtl) - 1) as usize; 312004e86ffSlogin volatile_write!(cmdtbl.prdt_entry[las].dba, virt_2_phys(buf_ptr) as u64); 313004e86ffSlogin volatile_set_bit!(cmdtbl.prdt_entry[las].dbc, 1 << 31, true); // 允许中断 314004e86ffSlogin volatile_write_bit!( 315004e86ffSlogin cmdtbl.prdt_entry[las].dbc, 316004e86ffSlogin (1 << 22) - 1, 317004e86ffSlogin ((tmp_count << 9) - 1) as u32 318004e86ffSlogin ); // 数据长度 319004e86ffSlogin 320004e86ffSlogin // 设置命令 321004e86ffSlogin let cmdfis = unsafe { 322004e86ffSlogin ((&mut cmdtbl.cfis) as *mut [u8] as *mut usize as *mut FisRegH2D) 323004e86ffSlogin .as_mut() 324004e86ffSlogin .unwrap() 325004e86ffSlogin }; 326004e86ffSlogin volatile_write!(cmdfis.fis_type, FisType::RegH2D as u8); 327004e86ffSlogin volatile_set_bit!(cmdfis.pm, 1 << 7, true); // command_bit set 328004e86ffSlogin volatile_write!(cmdfis.command, ATA_CMD_WRITE_DMA_EXT); 329004e86ffSlogin 330004e86ffSlogin volatile_write!(cmdfis.lba0, (lba_id_start & 0xFF) as u8); 331004e86ffSlogin volatile_write!(cmdfis.lba1, ((lba_id_start >> 8) & 0xFF) as u8); 332004e86ffSlogin volatile_write!(cmdfis.lba2, ((lba_id_start >> 16) & 0xFF) as u8); 333004e86ffSlogin volatile_write!(cmdfis.lba3, ((lba_id_start >> 24) & 0xFF) as u8); 334004e86ffSlogin volatile_write!(cmdfis.lba4, ((lba_id_start >> 32) & 0xFF) as u8); 335004e86ffSlogin volatile_write!(cmdfis.lba5, ((lba_id_start >> 40) & 0xFF) as u8); 336004e86ffSlogin 337004e86ffSlogin volatile_write!(cmdfis.countl, (count & 0xFF) as u8); 338004e86ffSlogin volatile_write!(cmdfis.counth, ((count >> 8) & 0xFF) as u8); 339004e86ffSlogin 340004e86ffSlogin volatile_write!(cmdfis.device, 1 << 6); // LBA Mode 341004e86ffSlogin 342004e86ffSlogin volatile_set_bit!(port.ci, 1 << slot, true); // Issue command 343004e86ffSlogin 344004e86ffSlogin // 等待操作完成 345004e86ffSlogin loop { 346004e86ffSlogin if (volatile_read!(port.ci) & (1 << slot)) == 0 { 347004e86ffSlogin break; 348004e86ffSlogin } 349004e86ffSlogin if (volatile_read!(port.is) & HBA_PxIS_TFES) > 0 { 350004e86ffSlogin kerror!("Write disk error"); 351676b8ef6SMork return Err(SystemError::EIO); 352004e86ffSlogin } 353004e86ffSlogin } 354004e86ffSlogin 355004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 356004e86ffSlogin // successfully read 357004e86ffSlogin return Ok(count * 512); 358004e86ffSlogin } 359004e86ffSlogin 360676b8ef6SMork fn sync(&self) -> Result<(), SystemError> { 361004e86ffSlogin // 由于目前没有block cache, 因此sync返回成功即可 362004e86ffSlogin return Ok(()); 363004e86ffSlogin } 364004e86ffSlogin } 365004e86ffSlogin 366004e86ffSlogin impl LockedAhciDisk { 367004e86ffSlogin pub fn new( 368004e86ffSlogin name: String, 369004e86ffSlogin flags: u16, 370004e86ffSlogin ctrl_num: u8, 371004e86ffSlogin port_num: u8, 372676b8ef6SMork ) -> Result<Arc<LockedAhciDisk>, SystemError> { 373004e86ffSlogin // 构建磁盘结构体 374004e86ffSlogin let result: Arc<LockedAhciDisk> = Arc::new(LockedAhciDisk(SpinLock::new(AhciDisk { 375004e86ffSlogin name, 376004e86ffSlogin flags, 377004e86ffSlogin partitions: Default::default(), 378004e86ffSlogin ctrl_num, 379004e86ffSlogin port_num, 380004e86ffSlogin self_ref: Weak::default(), 381004e86ffSlogin }))); 382004e86ffSlogin 383004e86ffSlogin let table: MbrDiskPartionTable = result.read_mbr_table()?; 384004e86ffSlogin 385004e86ffSlogin // 求出有多少可用分区 386004e86ffSlogin for i in 0..4 { 3877ae679ddSLoGin compiler_fence(Ordering::SeqCst); 388004e86ffSlogin if table.dpte[i].part_type != 0 { 3897ae679ddSLoGin let w = Arc::downgrade(&result); 3907ae679ddSLoGin result.0.lock().partitions.push(Partition::new( 391004e86ffSlogin table.dpte[i].starting_sector() as u64, 392004e86ffSlogin table.dpte[i].starting_lba as u64, 393004e86ffSlogin table.dpte[i].total_sectors as u64, 3947ae679ddSLoGin w, 395004e86ffSlogin i as u16, 396004e86ffSlogin )); 397004e86ffSlogin } 398004e86ffSlogin } 3997ae679ddSLoGin 4007ae679ddSLoGin result.0.lock().self_ref = Arc::downgrade(&result); 4017ae679ddSLoGin 402004e86ffSlogin return Ok(result); 403004e86ffSlogin } 404004e86ffSlogin 405004e86ffSlogin /// @brief: 从磁盘中读取 MBR 分区表结构体 TODO: Cursor 406676b8ef6SMork pub fn read_mbr_table(&self) -> Result<MbrDiskPartionTable, SystemError> { 407004e86ffSlogin let mut table: MbrDiskPartionTable = Default::default(); 408004e86ffSlogin 409004e86ffSlogin // 数据缓冲区 410004e86ffSlogin let mut buf: Vec<u8> = Vec::new(); 411004e86ffSlogin buf.resize(size_of::<MbrDiskPartionTable>(), 0); 412004e86ffSlogin 4137ae679ddSLoGin self.read_at(0, 1, &mut buf)?; 414004e86ffSlogin // 创建 Cursor 用于按字节读取 415004e86ffSlogin let mut cursor = VecCursor::new(buf); 416004e86ffSlogin cursor.seek(SeekFrom::SeekCurrent(446))?; 417004e86ffSlogin 418004e86ffSlogin for i in 0..4 { 419b087521eSChiichen kdebug!("infomation of partition {}:\n", i); 420004e86ffSlogin 421004e86ffSlogin table.dpte[i].flags = cursor.read_u8()?; 422004e86ffSlogin table.dpte[i].starting_head = cursor.read_u8()?; 423004e86ffSlogin table.dpte[i].starting_sector_cylinder = cursor.read_u16()?; 424004e86ffSlogin table.dpte[i].part_type = cursor.read_u8()?; 425004e86ffSlogin table.dpte[i].ending_head = cursor.read_u8()?; 426004e86ffSlogin table.dpte[i].ending_sector_cylingder = cursor.read_u16()?; 427004e86ffSlogin table.dpte[i].starting_lba = cursor.read_u32()?; 428004e86ffSlogin table.dpte[i].total_sectors = cursor.read_u32()?; 429004e86ffSlogin 430b087521eSChiichen kdebug!("dpte[i] = {:?}", table.dpte[i]); 431004e86ffSlogin } 432004e86ffSlogin table.bs_trailsig = cursor.read_u16()?; 433004e86ffSlogin // kdebug!("bs_trailsig = {}", unsafe { 434004e86ffSlogin // read_unaligned(addr_of!(table.bs_trailsig)) 435004e86ffSlogin // }); 436004e86ffSlogin 437004e86ffSlogin return Ok(table); 438004e86ffSlogin } 439004e86ffSlogin } 440004e86ffSlogin 44106d5e247SLoGin impl KObject for LockedAhciDisk { 44206d5e247SLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 44306d5e247SLoGin self 44406d5e247SLoGin } 44506d5e247SLoGin 44606d5e247SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 44706d5e247SLoGin todo!() 44806d5e247SLoGin } 44906d5e247SLoGin 45006d5e247SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 45106d5e247SLoGin todo!() 45206d5e247SLoGin } 45306d5e247SLoGin 45406d5e247SLoGin fn kset(&self) -> Option<Arc<KSet>> { 45506d5e247SLoGin todo!() 45606d5e247SLoGin } 45706d5e247SLoGin 45806d5e247SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 45906d5e247SLoGin todo!() 46006d5e247SLoGin } 46106d5e247SLoGin 46206d5e247SLoGin fn set_inode(&self, _inode: Option<Arc<KernFSInode>>) { 46306d5e247SLoGin todo!() 46406d5e247SLoGin } 46506d5e247SLoGin 46606d5e247SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 46706d5e247SLoGin todo!() 46806d5e247SLoGin } 46906d5e247SLoGin 47006d5e247SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 47106d5e247SLoGin todo!() 47206d5e247SLoGin } 47306d5e247SLoGin 47406d5e247SLoGin fn set_kobj_state(&self, _state: KObjectState) { 47506d5e247SLoGin todo!() 47606d5e247SLoGin } 47706d5e247SLoGin 47806d5e247SLoGin fn name(&self) -> alloc::string::String { 47906d5e247SLoGin todo!() 48006d5e247SLoGin } 48106d5e247SLoGin 48206d5e247SLoGin fn set_name(&self, _name: alloc::string::String) { 48306d5e247SLoGin todo!() 48406d5e247SLoGin } 48506d5e247SLoGin 48606d5e247SLoGin fn set_kset(&self, _kset: Option<Arc<KSet>>) { 48706d5e247SLoGin todo!() 48806d5e247SLoGin } 48906d5e247SLoGin 49006d5e247SLoGin fn set_parent(&self, _parent: Option<Weak<dyn KObject>>) { 49106d5e247SLoGin todo!() 49206d5e247SLoGin } 493*a03c4f9dSLoGin 494*a03c4f9dSLoGin fn set_kobj_type(&self, _ktype: Option<&'static dyn KObjType>) { 495*a03c4f9dSLoGin todo!() 496*a03c4f9dSLoGin } 49706d5e247SLoGin } 498b087521eSChiichen 499b087521eSChiichen impl Device for LockedAhciDisk { 500b087521eSChiichen fn dev_type(&self) -> DeviceType { 501b087521eSChiichen return DeviceType::Block; 502b087521eSChiichen } 503b087521eSChiichen 50406d5e247SLoGin fn id_table(&self) -> IdTable { 505b087521eSChiichen todo!() 506b087521eSChiichen } 507b087521eSChiichen 50806d5e247SLoGin fn bus(&self) -> Option<Arc<dyn Bus>> { 50906d5e247SLoGin todo!("LockedAhciDisk::bus()") 510b087521eSChiichen } 511b087521eSChiichen 512*a03c4f9dSLoGin fn set_bus(&self, _bus: Option<Arc<dyn Bus>>) { 513*a03c4f9dSLoGin todo!("LockedAhciDisk::set_bus()") 514*a03c4f9dSLoGin } 515*a03c4f9dSLoGin 51606d5e247SLoGin fn driver(&self) -> Option<Arc<dyn Driver>> { 51706d5e247SLoGin todo!("LockedAhciDisk::driver()") 51806d5e247SLoGin } 51906d5e247SLoGin 52006d5e247SLoGin fn is_dead(&self) -> bool { 52106d5e247SLoGin false 52206d5e247SLoGin } 52306d5e247SLoGin 524*a03c4f9dSLoGin fn set_driver(&self, _driver: Option<Weak<dyn Driver>>) { 52506d5e247SLoGin todo!("LockedAhciDisk::set_driver()") 526b087521eSChiichen } 527*a03c4f9dSLoGin 528*a03c4f9dSLoGin fn can_match(&self) -> bool { 529*a03c4f9dSLoGin todo!() 530*a03c4f9dSLoGin } 531*a03c4f9dSLoGin 532*a03c4f9dSLoGin fn set_can_match(&self, _can_match: bool) { 533*a03c4f9dSLoGin todo!() 534*a03c4f9dSLoGin } 535*a03c4f9dSLoGin 536*a03c4f9dSLoGin fn state_synced(&self) -> bool { 537*a03c4f9dSLoGin todo!() 538*a03c4f9dSLoGin } 539b087521eSChiichen } 540b087521eSChiichen 541004e86ffSlogin impl BlockDevice for LockedAhciDisk { 542004e86ffSlogin #[inline] 543004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 544004e86ffSlogin self 545004e86ffSlogin } 546004e86ffSlogin 547004e86ffSlogin #[inline] 548004e86ffSlogin fn blk_size_log2(&self) -> u8 { 549004e86ffSlogin 9 550004e86ffSlogin } 551004e86ffSlogin 552676b8ef6SMork fn sync(&self) -> Result<(), SystemError> { 553004e86ffSlogin return self.0.lock().sync(); 554004e86ffSlogin } 555004e86ffSlogin 556004e86ffSlogin #[inline] 557b087521eSChiichen fn device(&self) -> Arc<dyn Device> { 558004e86ffSlogin return self.0.lock().self_ref.upgrade().unwrap(); 559004e86ffSlogin } 560004e86ffSlogin 561004e86ffSlogin fn block_size(&self) -> usize { 562004e86ffSlogin todo!() 563004e86ffSlogin } 564004e86ffSlogin 565004e86ffSlogin fn partitions(&self) -> Vec<Arc<Partition>> { 566004e86ffSlogin return self.0.lock().partitions.clone(); 567004e86ffSlogin } 568b087521eSChiichen 569b087521eSChiichen #[inline] 570b087521eSChiichen fn read_at( 571b087521eSChiichen &self, 572b087521eSChiichen lba_id_start: BlockId, // 起始lba编号 573b087521eSChiichen count: usize, // 读取lba的数量 574b087521eSChiichen buf: &mut [u8], 575b087521eSChiichen ) -> Result<usize, SystemError> { 576b087521eSChiichen self.0.lock().read_at(lba_id_start, count, buf) 577b087521eSChiichen } 578b087521eSChiichen 579b087521eSChiichen #[inline] 580b087521eSChiichen fn write_at( 581b087521eSChiichen &self, 582b087521eSChiichen lba_id_start: BlockId, 583b087521eSChiichen count: usize, 584b087521eSChiichen buf: &[u8], 585b087521eSChiichen ) -> Result<usize, SystemError> { 586b087521eSChiichen self.0.lock().write_at(lba_id_start, count, buf) 587b087521eSChiichen } 588004e86ffSlogin } 589