Skip to content

Commit

Permalink
efi: Implement writing to block device
Browse files Browse the repository at this point in the history
Using the SectorWrite trait added to the block device implement writing
to the block device. This is useful for bootloaders that need to write
to the block device in order to be able support fallback booting.

In particular this enables the firmware to be used with Ubuntu 19.10
("eoan".)

Signed-off-by: Rob Bradford <[email protected]>
  • Loading branch information
rbradford committed Oct 1, 2019
1 parent 7381a97 commit f52c1a1
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/efi/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,43 @@ pub extern "win64" fn read_blocks(

#[cfg(not(test))]
pub extern "win64" fn write_blocks(
_: *mut BlockIoProtocol,
proto: *mut BlockIoProtocol,
_: u32,
_: u64,
_: usize,
_: *mut c_void,
start: u64,
size: usize,
buffer: *mut c_void,
) -> Status {
Status::UNSUPPORTED
let wrapper = container_of!(proto, BlockWrapper, proto);
let wrapper = unsafe { &*wrapper };

let blocks = (size / 512) as usize;
let mut region = crate::mem::MemoryRegion::new(buffer as u64, size as u64);

for i in 0..blocks {
use crate::block::SectorWrite;
let data = region.as_mut_slice(i as u64 * 512, 512);
let block = unsafe { &*wrapper.block };
match block.write(wrapper.start_lba + start + i as u64, data) {
Ok(()) => continue,
Err(_) => {
return Status::DEVICE_ERROR;
}
};
}

Status::SUCCESS
}

#[cfg(not(test))]
pub extern "win64" fn flush_blocks(_: *mut BlockIoProtocol) -> Status {
Status::UNSUPPORTED
pub extern "win64" fn flush_blocks(proto: *mut BlockIoProtocol) -> Status {
let wrapper = container_of!(proto, BlockWrapper, proto);
let wrapper = unsafe { &*wrapper };
use crate::block::SectorWrite;
let block = unsafe { &*wrapper.block };
match block.flush() {
Ok(()) => Status::SUCCESS,
Err(_) => Status::DEVICE_ERROR,
}
}

#[cfg(not(test))]
Expand Down

0 comments on commit f52c1a1

Please sign in to comment.