Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not write file #1167

Closed
LordCasser opened this issue May 14, 2024 · 2 comments
Closed

Can not write file #1167

LordCasser opened this issue May 14, 2024 · 2 comments

Comments

@LordCasser
Copy link

LordCasser commented May 14, 2024

my code is

#![no_main]
#![no_std]

extern crate alloc;

use alloc::vec::Vec;
use core::ffi::c_void;

use uefi::{print, println};
use uefi::fs::FileSystem;
use uefi::prelude::*;
use uefi::proto::loaded_image::LoadedImage;

#[entry]
fn main(image_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
    uefi::helpers::init(&mut system_table).unwrap();
    let boot_services = system_table.boot_services();
    let result = print_image(boot_services);
    dump_image( boot_services,result.0,result.1);
    // boot_services.stall(10_000_000);
    Status::SUCCESS
}

fn print_image(bs: &BootServices) -> (*const c_void, u64) {
    let loaded_image = bs
        .open_protocol_exclusive::<LoadedImage>(bs.image_handle()).unwrap();
    println!("Images pointer {:p}, size: {}",loaded_image.info().0,loaded_image.info().1);
    loaded_image.info()
}

fn dump_image(bs: &BootServices, address: *const c_void, size: u64){


    let loaded_image = bs
        .open_protocol_exclusive::<LoadedImage>(bs.image_handle()).unwrap();
    let device_handle = loaded_image.device().unwrap();

    let mut sfs = bs.open_protocol_exclusive::<uefi::proto::media::fs::SimpleFileSystem>(device_handle).unwrap();
    // let mut root = fs.open_volume().unwrap();
    let mut fs = FileSystem::new(sfs);
    // let mut file_handle = root.open(cstr16!("memdump.bin"), FileMode::CreateReadWrite, FileAttribute::FLAGS.).unwrap();
    // //     .expect_success("Failed to open file");

    // let mut file = unsafe { RegularFile::new(file_handle) };



    let mut buffer = Vec::with_capacity(size as usize);
    let ptr: *mut c_void = buffer.as_mut_ptr() as *mut c_void;
    unsafe {
        address.copy_to_nonoverlapping(ptr, size as usize);
    }
    let buf = buffer.as_slice();
    println!("buffer address: {:p}",buf);
    // unsafe {
    //     print_hex_data(buf,size as usize);
    // }
    fs.write(cstr16!("memdump.bin"), buf).unwrap();

    // file.write(buf).unwrap();
    // file.flush().unwrap();
    // file.close();

}

unsafe fn print_hex_data(address: & [u8], length: usize) {
    for i in 0..length {
        let byte = *address.as_ptr().offset(i as isize);
        print!("{:02X} ", byte);
    }
    println!();
}

but I got
image

There is nothing writed to filesystem. I can't figure out why

@phip1611
Copy link
Contributor

phip1611 commented May 14, 2024

Hi there!

Because the buffer length is never set to anything different than 0 :)
assert_eq(buf.len(),0) will be true in your example.

Just setting the capacity does nothing to the length.

Instead of trying "to be smart", just create a slice with the source data
and let the vector extend from it. It automatically sets its capacity and length then.

@LordCasser
Copy link
Author

Thanks for your reply and you are right, this help me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants