Skip to content

Commit

Permalink
remove init example, add hello_world
Browse files Browse the repository at this point in the history
  • Loading branch information
bootreer committed May 29, 2024
1 parent 501a78e commit 37bd8a2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 271 deletions.
25 changes: 25 additions & 0 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::error::Error;
use std::{env, process};

pub fn main() -> Result<(), Box<dyn Error>> {
let mut args = env::args();
args.next();

let pci_addr = match args.next() {
Some(arg) => arg,
None => {
eprintln!("Usage: cargo run --example hello_world <pci bus id>");
process::exit(1);
}
};

let mut nvme = vroom::init(&pci_addr)?;
nvme.write_copied("hello world".as_bytes(), 0)?;

let mut dest = [0u8; 12];
nvme.read_copied(&mut dest, 0)?;

println!("{}", std::str::from_utf8(&dest)?);

Ok(())
}
268 changes: 0 additions & 268 deletions examples/init.rs

This file was deleted.

5 changes: 2 additions & 3 deletions src/nvme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ impl NvmeDevice {

// TODO: idk bout this/don't hardcode len
let data: &[u32] =
// unsafe { std::slice::from_raw_parts(self.buffer.virt.as_ptr() as *const u32, 1024) };
unsafe { std::slice::from_raw_parts(self.buffer.virt as *const u32, 1024) };

data.iter()
Expand Down Expand Up @@ -465,6 +464,7 @@ impl NvmeDevice {
namespace
}

// TODO: currently namespace 1 is hardcoded
pub fn write(&mut self, data: &impl DmaSlice, mut lba: u64) -> Result<(), Box<dyn Error>> {
for chunk in data.chunks(2 * 4096) {
let blocks = (chunk.slice.len() as u64 + 512 - 1) / 512;
Expand Down Expand Up @@ -499,11 +499,10 @@ impl NvmeDevice {

pub fn read_copied(
&mut self,
ns_id: u32,
dest: &mut [u8],
mut lba: u64,
) -> Result<(), Box<dyn Error>> {
let ns = *self.namespaces.get(&ns_id).unwrap();
let ns = *self.namespaces.get(&1).unwrap();
for chunk in dest.chunks_mut(128 * 4096) {
let blocks = (chunk.len() as u64 + ns.block_size - 1) / ns.block_size;
self.namespace_io(1, blocks, lba, self.buffer.phys as u64, false)?;
Expand Down

0 comments on commit 37bd8a2

Please sign in to comment.