diff --git a/alioth/src/device/fw_cfg/fw_cfg.rs b/alioth/src/device/fw_cfg/fw_cfg.rs index 558e006e..e1f96234 100644 --- a/alioth/src/device/fw_cfg/fw_cfg.rs +++ b/alioth/src/device/fw_cfg/fw_cfg.rs @@ -15,13 +15,13 @@ pub mod acpi; use std::ffi::CString; -use std::fmt; use std::fs::File; -use std::io::{ErrorKind, Read, Result, Seek, SeekFrom}; +use std::io::{self, ErrorKind, Read, Result}; use std::mem::{size_of, size_of_val}; use std::os::unix::fs::FileExt; use std::path::Path; use std::sync::Arc; +use std::{cmp, fmt}; use alioth_macros::Layout; use bitfield::bitfield; @@ -108,32 +108,19 @@ struct FwCfgContentAccess<'a> { impl Read for FwCfgContentAccess<'_> { fn read(&mut self, buf: &mut [u8]) -> Result { - match self.content { - FwCfgContent::File(offset, f) => { - Seek::seek(&mut (&*f), SeekFrom::Start(offset + self.offset as u64))?; - Read::read(&mut (&*f), buf) - } - FwCfgContent::Bytes(b) => match b.get(self.offset as usize..) { - Some(mut s) => s.read(buf), - None => Err(ErrorKind::UnexpectedEof)?, - }, - FwCfgContent::Slice(b) => match b.get(self.offset as usize..) { - Some(mut s) => s.read(buf), - None => Err(ErrorKind::UnexpectedEof)?, - }, - FwCfgContent::Lu16(n) => match n.as_bytes().get(self.offset as usize..) { - Some(mut s) => s.read(buf), - None => Err(ErrorKind::UnexpectedEof)?, - }, - FwCfgContent::Lu32(n) => match n.as_bytes().get(self.offset as usize..) { - Some(mut s) => s.read(buf), - None => Err(ErrorKind::UnexpectedEof)?, - }, - FwCfgContent::Lu64(n) => match n.as_bytes().get(self.offset as usize..) { - Some(mut s) => s.read(buf), - None => Err(ErrorKind::UnexpectedEof)?, - }, + fn read_from_bytes(bytes: &[u8], offset: u32, buf: &mut [u8]) -> Result { + bytes.get(offset as usize..).unwrap_or(&[]).read(buf) } + let n = match self.content { + FwCfgContent::File(offset, f) => f.read_at(buf, offset + self.offset as u64)?, + FwCfgContent::Bytes(b) => read_from_bytes(b, self.offset, buf)?, + FwCfgContent::Slice(b) => read_from_bytes(b, self.offset, buf)?, + FwCfgContent::Lu16(n) => read_from_bytes(n.as_bytes(), self.offset, buf)?, + FwCfgContent::Lu32(n) => read_from_bytes(n.as_bytes(), self.offset, buf)?, + FwCfgContent::Lu64(n) => read_from_bytes(n.as_bytes(), self.offset, buf)?, + }; + self.offset = self.offset.saturating_add(n as u32); + Ok(n) } } @@ -375,13 +362,15 @@ impl FwCfg { address: u64, ) -> Result { let content_size = content.size()?.saturating_sub(offset); - let op_size = std::cmp::min(content_size, len); - let r = self - .memory - .write_range(address, op_size as u64, content.access(offset)); + let op_size = cmp::min(content_size, len); + let r = self.memory.write_range( + address, + len as u64, + content.access(offset).chain(io::repeat(0)), + ); match r { Err(e) => { - log::error!("fw_cfg: dam read error: {e:x?}"); + log::error!("fw_cfg: dma read error: {e:x?}"); Err(ErrorKind::InvalidInput.into()) } Ok(()) => Ok(op_size), @@ -402,11 +391,12 @@ impl FwCfg { } fn dma_write(&self, _selector: u16, _len: u32, _address: u64) -> Result<()> { - unimplemented!() + Err(ErrorKind::Unsupported.into()) } fn do_dma(&mut self) { let dma_address = self.dma_address; + self.dma_address = 0; let dma_access: FwCfgDmaAccess = match self.memory.read_t(dma_address) { Ok(access) => access, Err(e) => { @@ -416,7 +406,8 @@ impl FwCfg { }; let control = AccessControl(dma_access.control.into()); if control.select() { - self.selector = control.select() as u16; + self.selector = control.selector() as u16; + self.data_offset = 0; } let len = dma_access.length.to_ne(); let addr = dma_access.address.to_ne(); @@ -425,7 +416,9 @@ impl FwCfg { } else if control.write() { self.dma_write(self.selector, len, addr) } else if control.skip() { - self.data_offset += len; + self.data_offset = self.data_offset.saturating_add(len); + Ok(()) + } else if control.select() { Ok(()) } else { Err(ErrorKind::InvalidData.into()) diff --git a/alioth/src/device/fw_cfg/fw_cfg_test.rs b/alioth/src/device/fw_cfg/fw_cfg_test.rs index 0c0afa20..00a61509 100644 --- a/alioth/src/device/fw_cfg/fw_cfg_test.rs +++ b/alioth/src/device/fw_cfg/fw_cfg_test.rs @@ -14,12 +14,21 @@ use std::fs::{self, File}; use std::io::{self, Read}; +use std::sync::Arc; use assert_matches::assert_matches; +use libc::{PROT_READ, PROT_WRITE}; +use parking_lot::Mutex; use rstest::rstest; use tempfile::TempDir; -use crate::device::fw_cfg::FwCfgContent; +use crate::arch::layout::{PORT_FW_CFG_DMA_HI, PORT_FW_CFG_DMA_LO, PORT_FW_CFG_SELECTOR}; +use crate::device::fw_cfg::{ + FW_CFG_FILE_FIRST, FW_CFG_INITRD_SIZE, FW_CFG_SIGNATURE, FwCfg, FwCfgContent, FwCfgDmaAccess, + FwCfgItem, +}; +use crate::mem::emulated::Mmio; +use crate::mem::mapped::{ArcMemPages, RamBus}; fn create_file_with_content(content: &str) -> io::Result { let tmp_dir = TempDir::new()?; @@ -109,3 +118,183 @@ fn test_fw_cfg_content_file_access() { let _ = fw_cfg_content.access(120).read(&mut buf); assert_eq!(&buf[..4], [0, 0, 0, 0]); } + +#[test] +fn test_fw_cfg_content_access_advances_offset_and_eof() { + let content = FwCfgContent::Slice(b"abcdef"); + let mut access = content.access(1); + + let mut first = [0u8; 2]; + assert_eq!(access.read(&mut first).unwrap(), 2); + assert_eq!(&first, b"bc"); + + let mut second = [0u8; 4]; + assert_eq!(access.read(&mut second).unwrap(), 3); + assert_eq!(&second[..3], b"def"); + + let mut eof = [0xffu8; 4]; + assert_eq!(access.read(&mut eof).unwrap(), 0); +} + +fn create_test_ram_bus() -> Arc { + const PAGE_SIZE: usize = 0x1000; + let bus = Arc::new(RamBus::new()); + let prot = PROT_READ | PROT_WRITE; + let low_mem = ArcMemPages::from_anonymous(PAGE_SIZE, Some(prot), None).unwrap(); + let high_mem = ArcMemPages::from_anonymous(PAGE_SIZE, Some(prot), None).unwrap(); + bus.update(|ram| ram.add(0x1000, low_mem)).unwrap(); + bus.update(|ram| ram.add(0x2_0000_0000, high_mem)).unwrap(); + bus +} + +fn run_dma(dev: &Mutex, bus: &RamBus, dma_gpa: u64, access: &FwCfgDmaAccess) -> u32 { + bus.write_t(dma_gpa, access).unwrap(); + let dma_hi_off = (PORT_FW_CFG_DMA_HI - PORT_FW_CFG_SELECTOR) as u64; + let dma_lo_off = (PORT_FW_CFG_DMA_LO - PORT_FW_CFG_SELECTOR) as u64; + let hi = (dma_gpa >> 32) as u32; + let lo = (dma_gpa & 0xffff_ffff) as u32; + if hi != 0 { + dev.write(dma_hi_off, 4, hi.to_be() as u64).unwrap(); + } + dev.write(dma_lo_off, 4, lo.to_be() as u64).unwrap(); + let resp: FwCfgDmaAccess = bus.read_t(dma_gpa).unwrap(); + resp.control.into() +} + +#[test] +fn test_fw_cfg_dma_read_zero_fills_past_eof() { + let bus = create_test_ram_bus(); + let item = FwCfgItem { + name: "test/short".to_owned(), + content: FwCfgContent::Slice(b"hi"), + }; + let dev = Mutex::new(FwCfg::new(bus.clone(), vec![item]).unwrap()); + let dma_gpa = 0x1000; + let buf_gpa = 0x1100; + + // Unset known item (empty slice): destination buffer must be zero-filled. + bus.write_t(buf_gpa, &0xdead_beef_u32).unwrap(); + dev.write(0, 2, FW_CFG_INITRD_SIZE as u64).unwrap(); + let access = FwCfgDmaAccess { + control: (1u32 << 1).into(), // READ + length: 4u32.into(), + address: buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &access), 0); + assert_eq!(bus.read_t::(buf_gpa).unwrap(), 0); + + // Partial read past EOF: copies item bytes and zero-fills remainder. + bus.write_t(buf_gpa, &[0xaa_u8; 6]).unwrap(); + dev.write(0, 2, FW_CFG_FILE_FIRST as u64).unwrap(); + let access = FwCfgDmaAccess { + control: (1u32 << 1).into(), // READ + length: 6u32.into(), + address: buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &access), 0); + assert_eq!( + bus.read_t::<[u8; 6]>(buf_gpa).unwrap(), + [b'h', b'i', 0, 0, 0, 0] + ); +} + +#[test] +fn test_fw_cfg_dma_resets_address_after_transfer() { + let bus = create_test_ram_bus(); + let dev = Mutex::new(FwCfg::new(bus.clone(), vec![]).unwrap()); + + // Perform a >4 GiB DMA transfer (writes both DMA_HI and DMA_LO). + let high_dma_gpa = 0x2_0000_0000; + let high_buf_gpa = 0x2_0000_0100; + dev.write(0, 2, FW_CFG_SIGNATURE as u64).unwrap(); + let high_access = FwCfgDmaAccess { + control: (1u32 << 1).into(), // READ + length: 4u32.into(), + address: high_buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, high_dma_gpa, &high_access), 0); + assert_eq!(bus.read_t::<[u8; 4]>(high_buf_gpa).unwrap(), *b"QEMU"); + + // Follow with a 32-bit DMA transfer (writes only DMA_LO); upper bits must be 0. + let low_dma_gpa = 0x1000; + let low_buf_gpa = 0x1100; + dev.write(0, 2, FW_CFG_SIGNATURE as u64).unwrap(); + let low_access = FwCfgDmaAccess { + control: (1u32 << 1).into(), // READ + length: 4u32.into(), + address: low_buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, low_dma_gpa, &low_access), 0); + assert_eq!(bus.read_t::<[u8; 4]>(low_buf_gpa).unwrap(), *b"QEMU"); +} + +#[test] +fn test_fw_cfg_dma_select() { + let bus = create_test_ram_bus(); + let item = FwCfgItem { + name: "test/item".to_owned(), + content: FwCfgContent::Slice(b"abcd"), + }; + let dev = Mutex::new(FwCfg::new(bus.clone(), vec![item]).unwrap()); + let dma_gpa = 0x1000; + let buf_gpa = 0x1100; + + // Advance offset on FW_CFG_FILE_FIRST via SKIP, then re-select via DMA SELECT | READ. + dev.write(0, 2, FW_CFG_FILE_FIRST as u64).unwrap(); + let skip_access = FwCfgDmaAccess { + control: (1u32 << 2).into(), // SKIP + length: 2u32.into(), + address: 0u64.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &skip_access), 0); + + let select_read = FwCfgDmaAccess { + control: (((FW_CFG_FILE_FIRST as u32) << 16) | (1 << 3) | (1 << 1)).into(), + length: 4u32.into(), + address: buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &select_read), 0); + assert_eq!(bus.read_t::<[u8; 4]>(buf_gpa).unwrap(), *b"abcd"); + + // Select-only DMA succeeds and resets offset. + let select_only = FwCfgDmaAccess { + control: (((FW_CFG_SIGNATURE as u32) << 16) | (1 << 3)).into(), + length: 0u32.into(), + address: 0u64.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &select_only), 0); + + let read_after_select = FwCfgDmaAccess { + control: (1u32 << 1).into(), // READ + length: 4u32.into(), + address: buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &read_after_select), 0); + assert_eq!(bus.read_t::<[u8; 4]>(buf_gpa).unwrap(), *b"QEMU"); +} + +#[test] +fn test_fw_cfg_dma_write_unsupported_and_skip_saturates() { + let bus = create_test_ram_bus(); + let dev = Mutex::new(FwCfg::new(bus.clone(), vec![]).unwrap()); + let dma_gpa = 0x1000; + let buf_gpa = 0x1100; + + // Unsupported DMA WRITE sets error bit instead of panicking. + let write_access = FwCfgDmaAccess { + control: (1u32 << 4).into(), // WRITE + length: 4u32.into(), + address: buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &write_access), 1); + + // Large DMA SKIP saturates data_offset instead of overflowing. + dev.write(0, 2, FW_CFG_SIGNATURE as u64).unwrap(); + let skip_access = FwCfgDmaAccess { + control: (1u32 << 2).into(), // SKIP + length: u32::MAX.into(), + address: 0u64.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &skip_access), 0); + assert_eq!(run_dma(&dev, &bus, dma_gpa, &skip_access), 0); +}