1004e86ffSlogin use super::{_port, hba::HbaCmdTable, virt_2_phys}; 2004e86ffSlogin use crate::driver::disk::ahci::HBA_PxIS_TFES; 3004e86ffSlogin use crate::filesystem::mbr::MbrDiskPartionTable; 4*660a04ceSlogin use crate::include::bindings::bindings::verify_area; 5004e86ffSlogin use crate::io::{device::BlockDevice, disk_info::Partition, SeekFrom}; 6004e86ffSlogin 7004e86ffSlogin use crate::libs::{spinlock::SpinLock, vec_cursor::VecCursor}; 8004e86ffSlogin use crate::mm::phys_2_virt; 9676b8ef6SMork use crate::syscall::SystemError; 10004e86ffSlogin use crate::{ 11004e86ffSlogin driver::disk::ahci::hba::{ 12004e86ffSlogin FisRegH2D, FisType, HbaCmdHeader, ATA_CMD_READ_DMA_EXT, ATA_CMD_WRITE_DMA_EXT, 13004e86ffSlogin ATA_DEV_BUSY, ATA_DEV_DRQ, 14004e86ffSlogin }, 15004e86ffSlogin kerror, 16004e86ffSlogin }; 17004e86ffSlogin 18004e86ffSlogin use alloc::sync::Weak; 19004e86ffSlogin use alloc::{string::String, sync::Arc, vec::Vec}; 20004e86ffSlogin 21004e86ffSlogin use core::fmt::Debug; 22004e86ffSlogin use core::sync::atomic::compiler_fence; 23004e86ffSlogin use core::{mem::size_of, ptr::write_bytes}; 24004e86ffSlogin 25004e86ffSlogin /// @brief: 只支持MBR分区格式的磁盘结构体 26004e86ffSlogin pub struct AhciDisk { 27004e86ffSlogin pub name: String, 28004e86ffSlogin pub flags: u16, // 磁盘的状态flags 29004e86ffSlogin pub partitions: Vec<Arc<Partition>>, // 磁盘分区数组 30004e86ffSlogin // port: &'static mut HbaPort, // 控制硬盘的端口 31004e86ffSlogin pub ctrl_num: u8, 32004e86ffSlogin pub port_num: u8, 33004e86ffSlogin /// 指向LockAhciDisk的弱引用 34004e86ffSlogin self_ref: Weak<LockedAhciDisk>, 35004e86ffSlogin } 36004e86ffSlogin 37004e86ffSlogin /// @brief: 带锁的AhciDisk 38004e86ffSlogin #[derive(Debug)] 39004e86ffSlogin pub struct LockedAhciDisk(pub SpinLock<AhciDisk>); 40004e86ffSlogin /// 函数实现 41004e86ffSlogin impl Debug for AhciDisk { 42004e86ffSlogin fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 43004e86ffSlogin write!( 44004e86ffSlogin f, 45004e86ffSlogin "{{ name: {}, flags: {}, part_s: {:?} }}", 46004e86ffSlogin self.name, self.flags, self.partitions 47004e86ffSlogin )?; 48004e86ffSlogin return Ok(()); 49004e86ffSlogin } 50004e86ffSlogin } 51004e86ffSlogin 52004e86ffSlogin impl AhciDisk { 53004e86ffSlogin fn read_at( 54004e86ffSlogin &self, 55004e86ffSlogin lba_id_start: crate::io::device::BlockId, // 起始lba编号 56004e86ffSlogin count: usize, // 读取lba的数量 57004e86ffSlogin buf: &mut [u8], 58676b8ef6SMork ) -> Result<usize, SystemError> { 59004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 60004e86ffSlogin let check_length = ((count - 1) >> 4) + 1; // prdt length 61004e86ffSlogin if count * 512 > buf.len() || check_length > u16::MAX as usize { 62004e86ffSlogin kerror!("ahci read: e2big"); 63004e86ffSlogin // 不可能的操作 64676b8ef6SMork return Err(SystemError::E2BIG); 65004e86ffSlogin } else if count == 0 { 66004e86ffSlogin return Ok(0); 67004e86ffSlogin } 68004e86ffSlogin 69004e86ffSlogin let port = _port(self.ctrl_num, self.port_num); 70004e86ffSlogin volatile_write!(port.is, u32::MAX); // Clear pending interrupt bits 71004e86ffSlogin 72004e86ffSlogin let slot = port.find_cmdslot().unwrap_or(u32::MAX); 73004e86ffSlogin 74004e86ffSlogin if slot == u32::MAX { 75676b8ef6SMork return Err(SystemError::EIO); 76004e86ffSlogin } 77004e86ffSlogin 78004e86ffSlogin #[allow(unused_unsafe)] 79004e86ffSlogin let cmdheader: &mut HbaCmdHeader = unsafe { 80004e86ffSlogin (phys_2_virt( 81004e86ffSlogin volatile_read!(port.clb) as usize 82004e86ffSlogin + slot as usize * size_of::<HbaCmdHeader>() as usize, 83004e86ffSlogin ) as *mut HbaCmdHeader) 84004e86ffSlogin .as_mut() 85004e86ffSlogin .unwrap() 86004e86ffSlogin }; 87004e86ffSlogin 88004e86ffSlogin volatile_write_bit!( 89004e86ffSlogin cmdheader.cfl, 90004e86ffSlogin (1 << 5) - 1 as u8, 91004e86ffSlogin (size_of::<FisRegH2D>() / size_of::<u32>()) as u8 92004e86ffSlogin ); // Command FIS size 93004e86ffSlogin 94004e86ffSlogin volatile_set_bit!(cmdheader.cfl, 1 << 6, false); // Read/Write bit : Read from device 95004e86ffSlogin volatile_write!(cmdheader.prdtl, check_length as u16); // PRDT entries count 96004e86ffSlogin 97004e86ffSlogin // 设置数据存放地址 98004e86ffSlogin let mut buf_ptr = buf as *mut [u8] as *mut usize as usize; 99*660a04ceSlogin 100*660a04ceSlogin // 由于目前的内存管理机制无法把用户空间的内存地址转换为物理地址,所以只能先把数据拷贝到内核空间 101*660a04ceSlogin // TODO:在内存管理重构后,可以直接使用用户空间的内存地址 102*660a04ceSlogin let user_buf = if unsafe { verify_area(buf_ptr as u64, buf.len() as u64) } { 103*660a04ceSlogin true 104*660a04ceSlogin } else { 105*660a04ceSlogin false 106*660a04ceSlogin }; 107*660a04ceSlogin let mut kbuf = if user_buf { 108*660a04ceSlogin let mut x: Vec<u8> = Vec::with_capacity(buf.len()); 109*660a04ceSlogin unsafe { 110*660a04ceSlogin x.set_len(buf.len()); 111*660a04ceSlogin } 112*660a04ceSlogin Some(x) 113*660a04ceSlogin } else { 114*660a04ceSlogin None 115*660a04ceSlogin }; 116*660a04ceSlogin 117*660a04ceSlogin if kbuf.is_some() { 118*660a04ceSlogin buf_ptr = kbuf.as_mut().unwrap().as_mut_ptr() as usize; 119*660a04ceSlogin } 120*660a04ceSlogin 121*660a04ceSlogin 122004e86ffSlogin #[allow(unused_unsafe)] 123004e86ffSlogin let cmdtbl = unsafe { 124004e86ffSlogin (phys_2_virt(volatile_read!(cmdheader.ctba) as usize) as *mut HbaCmdTable) 125004e86ffSlogin .as_mut() 126004e86ffSlogin .unwrap() // 必须使用 as_mut ,得到的才是原来的变量 127004e86ffSlogin }; 128004e86ffSlogin let mut tmp_count = count; 129004e86ffSlogin 130004e86ffSlogin unsafe { 131004e86ffSlogin // 清空整个table的旧数据 132004e86ffSlogin write_bytes(cmdtbl, 0, 1); 133004e86ffSlogin } 134004e86ffSlogin 135004e86ffSlogin // 8K bytes (16 sectors) per PRDT 136004e86ffSlogin for i in 0..((volatile_read!(cmdheader.prdtl) - 1) as usize) { 137004e86ffSlogin volatile_write!(cmdtbl.prdt_entry[i].dba, virt_2_phys(buf_ptr) as u64); 138004e86ffSlogin volatile_write_bit!(cmdtbl.prdt_entry[i].dbc, (1 << 22) - 1, 8 * 1024 - 1); // 数据长度 prdt_entry.dbc 139004e86ffSlogin volatile_set_bit!(cmdtbl.prdt_entry[i].dbc, 1 << 31, true); // 允许中断 prdt_entry.i 140004e86ffSlogin buf_ptr += 8 * 1024; 141004e86ffSlogin tmp_count -= 16; 142004e86ffSlogin } 143004e86ffSlogin 144004e86ffSlogin // Last entry 145004e86ffSlogin let las = (volatile_read!(cmdheader.prdtl) - 1) as usize; 146004e86ffSlogin volatile_write!(cmdtbl.prdt_entry[las].dba, virt_2_phys(buf_ptr) as u64); 147004e86ffSlogin volatile_write_bit!( 148004e86ffSlogin cmdtbl.prdt_entry[las].dbc, 149004e86ffSlogin (1 << 22) - 1, 150004e86ffSlogin ((tmp_count << 9) - 1) as u32 151004e86ffSlogin ); // 数据长度 152004e86ffSlogin volatile_set_bit!(cmdtbl.prdt_entry[las].dbc, 1 << 31, true); // 允许中断 153004e86ffSlogin 154004e86ffSlogin // 设置命令 155004e86ffSlogin let cmdfis = unsafe { 156004e86ffSlogin ((&mut cmdtbl.cfis) as *mut [u8] as *mut usize as *mut FisRegH2D) 157004e86ffSlogin .as_mut() 158004e86ffSlogin .unwrap() 159004e86ffSlogin }; 160004e86ffSlogin volatile_write!(cmdfis.fis_type, FisType::RegH2D as u8); 161004e86ffSlogin volatile_set_bit!(cmdfis.pm, 1 << 7, true); // command_bit set 162004e86ffSlogin volatile_write!(cmdfis.command, ATA_CMD_READ_DMA_EXT); 163004e86ffSlogin 164004e86ffSlogin volatile_write!(cmdfis.lba0, (lba_id_start & 0xFF) as u8); 165004e86ffSlogin volatile_write!(cmdfis.lba1, ((lba_id_start >> 8) & 0xFF) as u8); 166004e86ffSlogin volatile_write!(cmdfis.lba2, ((lba_id_start >> 16) & 0xFF) as u8); 167004e86ffSlogin volatile_write!(cmdfis.lba3, ((lba_id_start >> 24) & 0xFF) as u8); 168004e86ffSlogin volatile_write!(cmdfis.lba4, ((lba_id_start >> 32) & 0xFF) as u8); 169004e86ffSlogin volatile_write!(cmdfis.lba5, ((lba_id_start >> 40) & 0xFF) as u8); 170004e86ffSlogin 171004e86ffSlogin volatile_write!(cmdfis.countl, (count & 0xFF) as u8); 172004e86ffSlogin volatile_write!(cmdfis.counth, ((count >> 8) & 0xFF) as u8); 173004e86ffSlogin 174004e86ffSlogin volatile_write!(cmdfis.device, 1 << 6); // LBA Mode 175004e86ffSlogin 176004e86ffSlogin // 等待之前的操作完成 177004e86ffSlogin let mut spin_count = 0; 178004e86ffSlogin const SPIN_LIMIT: u32 = 10000; 179004e86ffSlogin 180004e86ffSlogin while (volatile_read!(port.tfd) as u8 & (ATA_DEV_BUSY | ATA_DEV_DRQ)) > 0 181004e86ffSlogin && spin_count < SPIN_LIMIT 182004e86ffSlogin { 183004e86ffSlogin spin_count += 1; 184004e86ffSlogin } 185004e86ffSlogin 186004e86ffSlogin if spin_count == SPIN_LIMIT { 187004e86ffSlogin kerror!("Port is hung"); 188676b8ef6SMork return Err(SystemError::EIO); 189004e86ffSlogin } 190004e86ffSlogin 191004e86ffSlogin volatile_set_bit!(port.ci, 1 << slot, true); // Issue command 192004e86ffSlogin // kdebug!("To wait ahci read complete."); 193004e86ffSlogin // 等待操作完成 194004e86ffSlogin loop { 195004e86ffSlogin if (volatile_read!(port.ci) & (1 << slot)) == 0 { 196004e86ffSlogin break; 197004e86ffSlogin } 198004e86ffSlogin if (volatile_read!(port.is) & HBA_PxIS_TFES) > 0 { 199004e86ffSlogin kerror!("Read disk error"); 200676b8ef6SMork return Err(SystemError::EIO); 201004e86ffSlogin } 202004e86ffSlogin } 203004e86ffSlogin 204*660a04ceSlogin if kbuf.is_some() { 205*660a04ceSlogin buf.copy_from_slice(kbuf.as_ref().unwrap()); 206*660a04ceSlogin } 207*660a04ceSlogin 208004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 209004e86ffSlogin // successfully read 210004e86ffSlogin return Ok(count * 512); 211004e86ffSlogin } 212004e86ffSlogin 213004e86ffSlogin fn write_at( 214004e86ffSlogin &self, 215004e86ffSlogin lba_id_start: crate::io::device::BlockId, 216004e86ffSlogin count: usize, 217004e86ffSlogin buf: &[u8], 218676b8ef6SMork ) -> Result<usize, SystemError> { 219004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 220004e86ffSlogin let check_length = ((count - 1) >> 4) + 1; // prdt length 221004e86ffSlogin if count * 512 > buf.len() || check_length > u16::MAX as usize { 222004e86ffSlogin // 不可能的操作 223676b8ef6SMork return Err(SystemError::E2BIG); 224004e86ffSlogin } else if count == 0 { 225004e86ffSlogin return Ok(0); 226004e86ffSlogin } 227004e86ffSlogin 228004e86ffSlogin let port = _port(self.ctrl_num, self.port_num); 229004e86ffSlogin 230004e86ffSlogin volatile_write!(port.is, u32::MAX); // Clear pending interrupt bits 231004e86ffSlogin 232004e86ffSlogin let slot = port.find_cmdslot().unwrap_or(u32::MAX); 233004e86ffSlogin 234004e86ffSlogin if slot == u32::MAX { 235676b8ef6SMork return Err(SystemError::EIO); 236004e86ffSlogin } 237004e86ffSlogin 238004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 239004e86ffSlogin #[allow(unused_unsafe)] 240004e86ffSlogin let cmdheader: &mut HbaCmdHeader = unsafe { 241004e86ffSlogin (phys_2_virt( 242004e86ffSlogin volatile_read!(port.clb) as usize 243004e86ffSlogin + slot as usize * size_of::<HbaCmdHeader>() as usize, 244004e86ffSlogin ) as *mut HbaCmdHeader) 245004e86ffSlogin .as_mut() 246004e86ffSlogin .unwrap() 247004e86ffSlogin }; 248004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 249004e86ffSlogin 250004e86ffSlogin volatile_write_bit!( 251004e86ffSlogin cmdheader.cfl, 252004e86ffSlogin (1 << 5) - 1 as u8, 253004e86ffSlogin (size_of::<FisRegH2D>() / size_of::<u32>()) as u8 254004e86ffSlogin ); // Command FIS size 255004e86ffSlogin 256004e86ffSlogin volatile_set_bit!(cmdheader.cfl, 7 << 5, true); // (p,c,w)都设置为1, Read/Write bit : Write from device 257004e86ffSlogin volatile_write!(cmdheader.prdtl, check_length as u16); // PRDT entries count 258004e86ffSlogin 259004e86ffSlogin // 设置数据存放地址 260004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 261004e86ffSlogin let mut buf_ptr = buf as *const [u8] as *mut usize as usize; 262*660a04ceSlogin 263*660a04ceSlogin // 由于目前的内存管理机制无法把用户空间的内存地址转换为物理地址,所以只能先把数据拷贝到内核空间 264*660a04ceSlogin // TODO:在内存管理重构后,可以直接使用用户空间的内存地址 265*660a04ceSlogin let user_buf = if unsafe { verify_area(buf_ptr as u64, buf.len() as u64) } { 266*660a04ceSlogin true 267*660a04ceSlogin } else { 268*660a04ceSlogin false 269*660a04ceSlogin }; 270*660a04ceSlogin let mut kbuf = if user_buf { 271*660a04ceSlogin let mut x: Vec<u8> = Vec::with_capacity(buf.len()); 272*660a04ceSlogin x.resize(buf.len(), 0); 273*660a04ceSlogin x.copy_from_slice(buf); 274*660a04ceSlogin Some(x) 275*660a04ceSlogin } else { 276*660a04ceSlogin None 277*660a04ceSlogin }; 278*660a04ceSlogin 279*660a04ceSlogin if kbuf.is_some() { 280*660a04ceSlogin buf_ptr = kbuf.as_mut().unwrap().as_mut_ptr() as usize; 281*660a04ceSlogin } 282*660a04ceSlogin 283004e86ffSlogin #[allow(unused_unsafe)] 284004e86ffSlogin let cmdtbl = unsafe { 285004e86ffSlogin (phys_2_virt(volatile_read!(cmdheader.ctba) as usize) as *mut HbaCmdTable) 286004e86ffSlogin .as_mut() 287004e86ffSlogin .unwrap() 288004e86ffSlogin }; 289004e86ffSlogin let mut tmp_count = count; 290004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 291004e86ffSlogin 292004e86ffSlogin unsafe { 293004e86ffSlogin // 清空整个table的旧数据 294004e86ffSlogin write_bytes(cmdtbl, 0, 1); 295004e86ffSlogin } 296004e86ffSlogin 297004e86ffSlogin // 8K bytes (16 sectors) per PRDT 298004e86ffSlogin for i in 0..((volatile_read!(cmdheader.prdtl) - 1) as usize) { 299004e86ffSlogin volatile_write!(cmdtbl.prdt_entry[i].dba, virt_2_phys(buf_ptr) as u64); 300004e86ffSlogin volatile_write_bit!(cmdtbl.prdt_entry[i].dbc, (1 << 22) - 1, 8 * 1024 - 1); // 数据长度 301004e86ffSlogin volatile_set_bit!(cmdtbl.prdt_entry[i].dbc, 1 << 31, true); // 允许中断 302004e86ffSlogin buf_ptr += 8 * 1024; 303004e86ffSlogin tmp_count -= 16; 304004e86ffSlogin } 305004e86ffSlogin 306004e86ffSlogin // Last entry 307004e86ffSlogin let las = (volatile_read!(cmdheader.prdtl) - 1) as usize; 308004e86ffSlogin volatile_write!(cmdtbl.prdt_entry[las].dba, virt_2_phys(buf_ptr) as u64); 309004e86ffSlogin volatile_set_bit!(cmdtbl.prdt_entry[las].dbc, 1 << 31, true); // 允许中断 310004e86ffSlogin volatile_write_bit!( 311004e86ffSlogin cmdtbl.prdt_entry[las].dbc, 312004e86ffSlogin (1 << 22) - 1, 313004e86ffSlogin ((tmp_count << 9) - 1) as u32 314004e86ffSlogin ); // 数据长度 315004e86ffSlogin 316004e86ffSlogin // 设置命令 317004e86ffSlogin let cmdfis = unsafe { 318004e86ffSlogin ((&mut cmdtbl.cfis) as *mut [u8] as *mut usize as *mut FisRegH2D) 319004e86ffSlogin .as_mut() 320004e86ffSlogin .unwrap() 321004e86ffSlogin }; 322004e86ffSlogin volatile_write!(cmdfis.fis_type, FisType::RegH2D as u8); 323004e86ffSlogin volatile_set_bit!(cmdfis.pm, 1 << 7, true); // command_bit set 324004e86ffSlogin volatile_write!(cmdfis.command, ATA_CMD_WRITE_DMA_EXT); 325004e86ffSlogin 326004e86ffSlogin volatile_write!(cmdfis.lba0, (lba_id_start & 0xFF) as u8); 327004e86ffSlogin volatile_write!(cmdfis.lba1, ((lba_id_start >> 8) & 0xFF) as u8); 328004e86ffSlogin volatile_write!(cmdfis.lba2, ((lba_id_start >> 16) & 0xFF) as u8); 329004e86ffSlogin volatile_write!(cmdfis.lba3, ((lba_id_start >> 24) & 0xFF) as u8); 330004e86ffSlogin volatile_write!(cmdfis.lba4, ((lba_id_start >> 32) & 0xFF) as u8); 331004e86ffSlogin volatile_write!(cmdfis.lba5, ((lba_id_start >> 40) & 0xFF) as u8); 332004e86ffSlogin 333004e86ffSlogin volatile_write!(cmdfis.countl, (count & 0xFF) as u8); 334004e86ffSlogin volatile_write!(cmdfis.counth, ((count >> 8) & 0xFF) as u8); 335004e86ffSlogin 336004e86ffSlogin volatile_write!(cmdfis.device, 1 << 6); // LBA Mode 337004e86ffSlogin 338004e86ffSlogin volatile_set_bit!(port.ci, 1 << slot, true); // Issue command 339004e86ffSlogin 340004e86ffSlogin // 等待操作完成 341004e86ffSlogin loop { 342004e86ffSlogin if (volatile_read!(port.ci) & (1 << slot)) == 0 { 343004e86ffSlogin break; 344004e86ffSlogin } 345004e86ffSlogin if (volatile_read!(port.is) & HBA_PxIS_TFES) > 0 { 346004e86ffSlogin kerror!("Write disk error"); 347676b8ef6SMork return Err(SystemError::EIO); 348004e86ffSlogin } 349004e86ffSlogin } 350004e86ffSlogin 351004e86ffSlogin compiler_fence(core::sync::atomic::Ordering::SeqCst); 352004e86ffSlogin // successfully read 353004e86ffSlogin return Ok(count * 512); 354004e86ffSlogin } 355004e86ffSlogin 356676b8ef6SMork fn sync(&self) -> Result<(), SystemError> { 357004e86ffSlogin // 由于目前没有block cache, 因此sync返回成功即可 358004e86ffSlogin return Ok(()); 359004e86ffSlogin } 360004e86ffSlogin } 361004e86ffSlogin 362004e86ffSlogin impl LockedAhciDisk { 363004e86ffSlogin pub fn new( 364004e86ffSlogin name: String, 365004e86ffSlogin flags: u16, 366004e86ffSlogin ctrl_num: u8, 367004e86ffSlogin port_num: u8, 368676b8ef6SMork ) -> Result<Arc<LockedAhciDisk>, SystemError> { 369004e86ffSlogin let mut part_s: Vec<Arc<Partition>> = Vec::new(); 370004e86ffSlogin 371004e86ffSlogin // 构建磁盘结构体 372004e86ffSlogin let result: Arc<LockedAhciDisk> = Arc::new(LockedAhciDisk(SpinLock::new(AhciDisk { 373004e86ffSlogin name, 374004e86ffSlogin flags, 375004e86ffSlogin partitions: Default::default(), 376004e86ffSlogin ctrl_num, 377004e86ffSlogin port_num, 378004e86ffSlogin self_ref: Weak::default(), 379004e86ffSlogin }))); 380004e86ffSlogin 381004e86ffSlogin let table: MbrDiskPartionTable = result.read_mbr_table()?; 382004e86ffSlogin let weak_this: Weak<LockedAhciDisk> = Arc::downgrade(&result); // 获取this的弱指针 383004e86ffSlogin 384004e86ffSlogin // 求出有多少可用分区 385004e86ffSlogin for i in 0..4 { 386004e86ffSlogin if table.dpte[i].part_type != 0 { 387004e86ffSlogin part_s.push(Partition::new( 388004e86ffSlogin table.dpte[i].starting_sector() as u64, 389004e86ffSlogin table.dpte[i].starting_lba as u64, 390004e86ffSlogin table.dpte[i].total_sectors as u64, 391004e86ffSlogin weak_this.clone(), 392004e86ffSlogin i as u16, 393004e86ffSlogin )); 394004e86ffSlogin } 395004e86ffSlogin } 396004e86ffSlogin 397004e86ffSlogin result.0.lock().partitions = part_s; 398004e86ffSlogin result.0.lock().self_ref = weak_this; 399004e86ffSlogin return Ok(result); 400004e86ffSlogin } 401004e86ffSlogin 402004e86ffSlogin /// @brief: 从磁盘中读取 MBR 分区表结构体 TODO: Cursor 403676b8ef6SMork pub fn read_mbr_table(&self) -> Result<MbrDiskPartionTable, SystemError> { 404004e86ffSlogin let mut table: MbrDiskPartionTable = Default::default(); 405004e86ffSlogin 406004e86ffSlogin // 数据缓冲区 407004e86ffSlogin let mut buf: Vec<u8> = Vec::new(); 408004e86ffSlogin buf.resize(size_of::<MbrDiskPartionTable>(), 0); 409004e86ffSlogin 410004e86ffSlogin self.read_at(0, 1, &mut buf)?; 411004e86ffSlogin 412004e86ffSlogin // 创建 Cursor 用于按字节读取 413004e86ffSlogin let mut cursor = VecCursor::new(buf); 414004e86ffSlogin cursor.seek(SeekFrom::SeekCurrent(446))?; 415004e86ffSlogin 416004e86ffSlogin for i in 0..4 { 417004e86ffSlogin // kdebug!("infomation of partition {}:\n", i); 418004e86ffSlogin 419004e86ffSlogin table.dpte[i].flags = cursor.read_u8()?; 420004e86ffSlogin table.dpte[i].starting_head = cursor.read_u8()?; 421004e86ffSlogin table.dpte[i].starting_sector_cylinder = cursor.read_u16()?; 422004e86ffSlogin table.dpte[i].part_type = cursor.read_u8()?; 423004e86ffSlogin table.dpte[i].ending_head = cursor.read_u8()?; 424004e86ffSlogin table.dpte[i].ending_sector_cylingder = cursor.read_u16()?; 425004e86ffSlogin table.dpte[i].starting_lba = cursor.read_u32()?; 426004e86ffSlogin table.dpte[i].total_sectors = cursor.read_u32()?; 427004e86ffSlogin 428004e86ffSlogin // kdebug!("dpte[i] = {:?}", table.dpte[i]); 429004e86ffSlogin } 430004e86ffSlogin table.bs_trailsig = cursor.read_u16()?; 431004e86ffSlogin // kdebug!("bs_trailsig = {}", unsafe { 432004e86ffSlogin // read_unaligned(addr_of!(table.bs_trailsig)) 433004e86ffSlogin // }); 434004e86ffSlogin 435004e86ffSlogin return Ok(table); 436004e86ffSlogin } 437004e86ffSlogin } 438004e86ffSlogin 439004e86ffSlogin impl BlockDevice for LockedAhciDisk { 440004e86ffSlogin #[inline] 441004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 442004e86ffSlogin self 443004e86ffSlogin } 444004e86ffSlogin 445004e86ffSlogin #[inline] 446004e86ffSlogin fn blk_size_log2(&self) -> u8 { 447004e86ffSlogin 9 448004e86ffSlogin } 449004e86ffSlogin 450004e86ffSlogin #[inline] 451004e86ffSlogin fn read_at( 452004e86ffSlogin &self, 453004e86ffSlogin lba_id_start: crate::io::device::BlockId, 454004e86ffSlogin count: usize, 455004e86ffSlogin buf: &mut [u8], 456676b8ef6SMork ) -> Result<usize, SystemError> { 457004e86ffSlogin // kdebug!( 458004e86ffSlogin // "ahci read at {lba_id_start}, count={count}, lock={:?}", 459004e86ffSlogin // self.0 460004e86ffSlogin // ); 461004e86ffSlogin return self.0.lock().read_at(lba_id_start, count, buf); 462004e86ffSlogin } 463004e86ffSlogin 464004e86ffSlogin #[inline] 465004e86ffSlogin fn write_at( 466004e86ffSlogin &self, 467004e86ffSlogin lba_id_start: crate::io::device::BlockId, 468004e86ffSlogin count: usize, 469004e86ffSlogin buf: &[u8], 470676b8ef6SMork ) -> Result<usize, SystemError> { 471004e86ffSlogin self.0.lock().write_at(lba_id_start, count, buf) 472004e86ffSlogin } 473004e86ffSlogin 474676b8ef6SMork fn sync(&self) -> Result<(), SystemError> { 475004e86ffSlogin return self.0.lock().sync(); 476004e86ffSlogin } 477004e86ffSlogin 478004e86ffSlogin #[inline] 479004e86ffSlogin fn device(&self) -> Arc<dyn crate::io::device::Device> { 480004e86ffSlogin return self.0.lock().self_ref.upgrade().unwrap(); 481004e86ffSlogin } 482004e86ffSlogin 483004e86ffSlogin fn block_size(&self) -> usize { 484004e86ffSlogin todo!() 485004e86ffSlogin } 486004e86ffSlogin 487004e86ffSlogin fn partitions(&self) -> Vec<Arc<Partition>> { 488004e86ffSlogin return self.0.lock().partitions.clone(); 489004e86ffSlogin } 490004e86ffSlogin } 491