rust-osdev / uefi-rs

Rusty wrapper for the Unified Extensible Firmware Interface (UEFI). This crate makes it easy to develop Rust software that leverages safe, convenient, and performant abstractions for UEFI functionality.
https://rust-osdev.com/uefi-book
Mozilla Public License 2.0
1.33k stars 160 forks source link

print macro dosen`t work. #1453

Closed Motanescu1357 closed 3 weeks ago

phip1611 commented 1 month ago

From looking at your profile, it seems that you are relatively new in the world of software development and new on GitHub. As a rule of thumb, please me more expressive in any kind of issue for any project :)

Always tell in the issue description:

Can you please be more specific about the environment, your setup, your code?


Have you read the documentation?

Use this similar to print! from the Rust standard library, but only as long as boot services have not been exited.

Does that help?

Motanescu1357 commented 1 month ago

I did not run my code in a vm. I atempted to print a String to the screen. The print macro does not print to the screen. I did not exit boot services. The uefi library version is 0.33.0 Here is my code:

#![no_main]
#![no_std]
extern crate alloc;
use uefi::prelude::*;
use uefi::{print, println, CStr16};
use uefi::proto::console::text::{Input, ScanCode};
use uefi::proto::console::text::Key;
use alloc::string::{String, ToString};
use uefi::proto::media::file::{File, FileAttribute, FileMode};
use uefi::runtime::ResetType;

#[entry]
fn main() -> Status {
    uefi::helpers::init().unwrap();
    println!("This is a small project that I made.");
    println!("Use The escape key or the exit command to shutdown the system.");
    print!(">");
    let handle = boot::get_handle_for_protocol::<Input>().unwrap();
    let mut input_protocol = boot::open_protocol_exclusive::<Input>(handle).unwrap();
    let mut input:String = String::new();
    loop {
        boot::wait_for_event(&mut [input_protocol.wait_for_key_event().unwrap()]).unwrap();
        if input == "exit" {
            runtime::reset(ResetType::SHUTDOWN, Status::SUCCESS, None);
        }
        let key = input_protocol.read_key().unwrap();
        let mut char:String;
        if let Some(Key::Printable(char2)) = key {
            char = char2.to_string();
            if char != "\r" {
                println!("{}", char);
                input.push_str(&char.as_str());
            }else{
                char = char2.to_string();
                os(char)
            }
        }
        if let Some(Key::Special(scancode)) = key {
            if scancode == ScanCode::ESCAPE {
                runtime::reset(ResetType::SHUTDOWN, Status::SUCCESS, None);
            }
            if scancode == ScanCode::DELETE {
                input.pop();
            }
        };
    }
}
fn os(input:String) {
    if input.contains("help") {
        println!();
        println!("Commands:");
        println!("    exit: Exits the system");
        println!("    remove [<target_path>]: The command removes the target.");
        println!("    new-file [<target_path>]: A new file at the target directory.");
        println!("    new-dir [<target_path>]: A new directory at the target directory.");
        println!("    edit-file <file_contents> [<target_path>]: Edits a file and ads the file_contents to the file at the target directory.");
        println!("    copy [<file1_path>] | [<file2_path>]:Creates a copy of the file.");
        println!("    cat [<target_path>]:Prints the text in a file");

    }
    if input.contains("remove") {
        let mut filesystem = boot::get_image_file_system(boot::image_handle()).unwrap();
        let buf= &mut [0; 99999];
        let binding = input.replace("remove", "").replace(" ", "").replace("/", "\\");
        let str = binding.as_str();
        let input_cstr16 = CStr16::from_str_with_buf(str, buf).unwrap();
        let file = filesystem.open_volume().unwrap().open(input_cstr16, FileMode::ReadWrite, FileAttribute::default()).unwrap();
        if file.is_regular_file().unwrap() {
            file.into_regular_file().unwrap().delete().unwrap();
        }else {
            file.into_directory().unwrap().delete().unwrap();
        }
    }
    if input.contains("new-dir") {
        let mut filesystem = boot::get_image_file_system(boot::image_handle()).unwrap();
        let buf= &mut [0; 99999];
        let binding = input.replace("new-dir", "").replace(" ", "").replace("/", "\\");
        let str = binding.as_str();
        let input_cstr16 = CStr16::from_str_with_buf(str, buf).unwrap();
        let _ = filesystem.open_volume().unwrap().open(input_cstr16, FileMode::CreateReadWrite, FileAttribute::DIRECTORY).unwrap().into_directory().unwrap();
    }
    if input.contains("new-file") {
        let mut filesystem = boot::get_image_file_system(boot::image_handle()).unwrap();
        let buf= &mut [0; 99999];
        let binding = input.replace("new-file", "").replace(" ", "").replace("/", "\\");
        let str = binding.as_str();
        let input_cstr16 = CStr16::from_str_with_buf(str, buf).unwrap();
        let _ = filesystem.open_volume().unwrap().open(input_cstr16, FileMode::CreateReadWrite, FileAttribute::default()).unwrap().into_regular_file().unwrap();
    }
    if input.contains("edit-file") {
        let mut filesystem = boot::get_image_file_system(boot::image_handle()).unwrap();
        let buf= &mut [0; 99999];
        let binding = input.replace("edit-file", "").replace(" ", "").replace("/", "\\");
        let split_point = binding.find("\\").unwrap() - 2;
        let(file_contents, file_path) = binding.split_at(split_point);
        let file_path_cstr16 = CStr16::from_str_with_buf(file_path, buf).unwrap();
        let file = filesystem.open_volume().unwrap().open(file_path_cstr16, FileMode::ReadWrite, FileAttribute::default()).unwrap();
        if file.is_regular_file().unwrap() {
            let mut file = file.into_regular_file().unwrap();
            file.write(file_contents.as_bytes()).unwrap();
            file.close()
        }
    }
    if input.contains("copy") {
        let mut filesystem = boot::get_image_file_system(boot::image_handle()).unwrap();
        let path1_buf= &mut [0; 999];
        let path2_buf= &mut [0; 999];
        let binding = input.replace("copy", "").replace(" ", "").replace("/", "\\");
        let split_location = binding.find("|").unwrap() - 1;
        let (path1, path2) = binding.split_at(split_location);
        let path1_cstr16 = CStr16::from_str_with_buf(path1, path1_buf).unwrap();
        let path2_cstr16 = CStr16::from_str_with_buf(path2, path2_buf).unwrap();
        let file1 = filesystem.open_volume().unwrap().open(path1_cstr16, FileMode::Read, FileAttribute::default()).unwrap();
        let file2 = filesystem.open_volume().unwrap().open(path2_cstr16, FileMode::ReadWrite, FileAttribute::default()).unwrap();
        if file1.is_regular_file().unwrap() == true {
            if file2.is_regular_file().unwrap() == true {
                let mut file1_contents:&mut [u8] = &mut [];
                let mut file1 = file1.into_regular_file().unwrap();
                let mut file2 = file2.into_regular_file().unwrap();
                let _ = file1.read(&mut file1_contents).unwrap();
                let _ = file2.write(&file1_contents);
                file1.close();
                file2.close();
            }
        }
    }
    if input.contains("move") {
        let mut filesystem = boot::get_image_file_system(boot::image_handle()).unwrap();
        let path1_buf= &mut [0; 999];
        let path2_buf= &mut [0; 999];
        let binding = input.replace("move", "").replace(" ", "").replace("/", "\\");
        let split_location = binding.find("|").unwrap() - 1;
        let (path1, path2) = binding.split_at(split_location);
        let path1_cstr16 = CStr16::from_str_with_buf(path1, path1_buf).unwrap();
        let path2_cstr16 = CStr16::from_str_with_buf(path2, path2_buf).unwrap();
        let file1 = filesystem.open_volume().unwrap().open(path1_cstr16, FileMode::Read, FileAttribute::default()).unwrap();
        let file2 = filesystem.open_volume().unwrap().open(path2_cstr16, FileMode::ReadWrite, FileAttribute::default()).unwrap();
        if file1.is_regular_file().unwrap() == true {
            if file2.is_regular_file().unwrap() == true {
                let mut file1 = file1.into_regular_file().unwrap();
                let mut file2 = file2.into_regular_file().unwrap();
                let mut file1_contents:&mut [u8] = &mut [];
                let _ = file1.read(&mut file1_contents).unwrap();
                let _ = file2.write(&file1_contents);
                file1.delete().unwrap();
                file2.close();
            }
        }
    }
    if input.contains("cat") {
        let mut filesystem = boot::get_image_file_system(boot::image_handle()).unwrap();
        let buf = &mut [0; 999];
        let binding = input.replace("cat", "").replace(" ", "").replace("/", "\\");
        let str = binding.as_str();
        let file_path = CStr16::from_str_with_buf(str, buf).unwrap();
        let file = filesystem.open_volume().unwrap().open(file_path, FileMode::Read, FileAttribute::default()).unwrap();
        if file.is_regular_file().unwrap() == true {
            let file_contents:&mut [u8] = &mut [];
            file.into_regular_file().unwrap().read(file_contents).unwrap();
            println!("File contents: {:?}", String::from_utf8(file_contents.to_vec()).unwrap());

        }
    }
}
nicholasbishop commented 1 month ago

When providing code to demonstrate a problem, try to create a minimal reproducible example.

Can you clarify -- is the issue specific to print!, or does println! also fail to output anything?

Motanescu1357 commented 3 weeks ago

No, println! does print a line to the screen.

Motanescu1357 commented 3 weeks ago

I made the input string actually work and now print! actually works but I have problems with implementing the enter and backspace keys.