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

Add SMBIOS OEM String support #10

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub struct Args {
/// GUI option for compatibility with vfkit (ignored).
#[arg(long, default_value_t = false)]
pub gui: bool,

/// SMBIOS OEM String
#[arg(long = "oem-string")]
pub oem_strings: Option<Vec<String>>,
}

/// Parse a string into a vector of substrings, all of which are separated by commas.
Expand Down
36 changes: 34 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ use crate::{
virtio::KrunContextSet,
};

use std::{convert::TryFrom, thread};
use std::ffi::{c_char, CString};
use std::{convert::TryFrom, ptr, thread};

use anyhow::anyhow;
use anyhow::{anyhow, Context};

#[link(name = "krun-efi")]
extern "C" {
fn krun_create_ctx() -> i32;
fn krun_set_gpu_options(ctx_id: u32, virgl_flags: u32) -> i32;
fn krun_set_vm_config(ctx_id: u32, num_vcpus: u8, ram_mib: u32) -> i32;
fn krun_set_smbios_oem_strings(ctx_id: u32, oem_strings: *const *const c_char) -> i32;
fn krun_start_enter(ctx_id: u32) -> i32;
}

Expand Down Expand Up @@ -67,6 +69,8 @@ impl TryFrom<Args> for KrunContext {
unsafe { device.krun_ctx_set(id)? }
}

set_smbios_oem_strings(id, &args.oem_strings)?;

Ok(Self { id, args })
}
}
Expand All @@ -89,3 +93,31 @@ impl KrunContext {
Ok(())
}
}

fn set_smbios_oem_strings(
ctx_id: u32,
oem_strings: &Option<Vec<String>>,
) -> Result<(), anyhow::Error> {
let Some(oem_strings) = oem_strings else {
return Ok(());
};

if oem_strings.len() > u8::MAX as usize {
return Err(anyhow!("invalid number of SMBIOS OEM strings"));
}

let mut cstr_vec = Vec::with_capacity(oem_strings.len());
for s in oem_strings {
let cs = CString::new(s.as_str()).context("invalid SMBIOS OEM string")?;
cstr_vec.push(cs);
}
let mut ptr_vec: Vec<_> = cstr_vec.iter().map(|s| s.as_ptr()).collect();
// libkrun requires an NULL terminator to indicate the end of the array
ptr_vec.push(ptr::null());

let ret = unsafe { krun_set_smbios_oem_strings(ctx_id, ptr_vec.as_ptr()) };
if ret < 0 {
return Err(anyhow!("unable to set SMBIOS OEM Strings"));
}
Ok(())
}
Loading