Skip to content

Add rng protocole #386

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

Merged
merged 14 commits into from
Mar 11, 2022
Merged

Add rng protocole #386

merged 14 commits into from
Mar 11, 2022

Conversation

gelven4sec
Copy link
Contributor

@gelven4sec gelven4sec commented Mar 7, 2022

Implementation of the EFI_RNG_PROTOCOL

For the need of a project I would need this implantation as fast as possible.

GUID

#define EFI_RNG_PROTOCOL_GUID \
{ 0x3152bca5, 0xeade, 0x433d,\
 {0x86, 0x2e, 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44}}

Protocol Interface Structure

typedef struct _EFI_RNG_PROTOCOL {
 EFI_RNG_GET_INFO GetInfo
 EFI_RNG_GET_RNG GetRNG;
} EFI_RNG_PROTOCOL;

Parameters

GetInfo Returns information about the random number generation
implementation.
GetRNG Returns the next set of random numbers.

Description

This protocol allows retrieval of RNG values from an UEFI driver. The GetInfo service returns
information about the RNG algorithms the driver supports. The GetRNG service creates a RNG value
using an (optionally specified) RNG algorithm.

@gelven4sec
Copy link
Contributor Author

Hi guys,

I would need some help with this :

So I'm just trying to instantiate the Rng I just created and it's failing with the status UNSUPPORTED.
I'm really new about UEFI and everything that's behind the wrapper, so I would be glad that you tell me what i'm doing wrong.

@nicholasbishop
Copy link
Member

Thanks for the PR :)

So I'm just trying to instantiate the Rng I just created and it's failing with the status UNSUPPORTED.

The issue here is the handle being passed to open_protocol. Handles represent resources. For example, the image handle passed into the function represents the currently-running UEFI program. In your case you want a handle that represents an rng device, so first you have to find such a handle and then pass that to open_protocol. Something like this should work:

    let handle = *bt
        .find_handles::<Rng>()
        .expect_success("Failed to get Rng handles")
        .first()
        .expect("No Rng handles");

    let rng = bt
        .open_protocol::<Rng>(
            OpenProtocolParams {
                handle,
                agent: image,
                controller: None,
            },
            OpenProtocolAttributes::Exclusive,
        )
        .expect_success("Failed to open Rng protocol");

To make this work under QEMU, you will also need to add an rng device to the virtual machine:

diff --git a/xtask/src/qemu.rs b/xtask/src/qemu.rs
index 4ecb4d1..8934316 100644
--- a/xtask/src/qemu.rs
+++ b/xtask/src/qemu.rs
@@ -277,6 +277,8 @@ pub fn run_qemu(arch: UefiArch, opt: &QemuOpt) -> Result<()> {
     // QEMU by defaults enables a ton of devices which slow down boot.
     cmd.arg("-nodefaults");
 
+    cmd.args(&["-device", "virtio-rng-pci"]);
+
     match arch {
         UefiArch::AArch64 => {
             // Use a generic ARM environment. Sadly qemu can't emulate a

@gelven4sec
Copy link
Contributor Author

Awesome it works !

Thank you 👍

So now I just need to code the wrappers then we're good to go.

@gelven4sec gelven4sec marked this pull request as ready for review March 11, 2022 13:59
@gelven4sec
Copy link
Contributor Author

gelven4sec commented Mar 11, 2022

That's it, waiting for the CI tests for last checks.

If the AArch64 CI crash again, I might need some help fixing this.

EDIT: There's still a TODO in the code, if I find some time I should implement an AlgorithmType Enum for better usability of get_info

EDIT2: You might want to link this PR to #371

@nicholasbishop nicholasbishop self-requested a review March 11, 2022 16:23
@nicholasbishop
Copy link
Member

Taking a look at the aarch64 error now, not sure what's going on there yet.

Copy link
Member

@nicholasbishop nicholasbishop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Thanks for sticking with it through all my comments :)

For some reason the CI doesn't seem to be running, but I'll figure that out and merge it once it finishes.

@gelven4sec
Copy link
Contributor Author

Looks good to me! Thanks for sticking with it through all my comments :)

Glad I made it :)

I will be able to present this contribution as a part of my school project.

@nicholasbishop nicholasbishop merged commit 392e624 into rust-osdev:main Mar 11, 2022
@nicholasbishop
Copy link
Member

Nice! Thanks again for the PR

@gelven4sec gelven4sec deleted the add-rng-protocole branch March 11, 2022 23:27
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

Successfully merging this pull request may close these issues.

2 participants