Skip to content

Commit c9310bb

Browse files
committed
move MaybeVendoredImage util into vendored crate
1 parent dedd36e commit c9310bb

File tree

5 files changed

+50
-34
lines changed

5 files changed

+50
-34
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fortanix-vme/tools/confidential-vm-blobs/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
anyhow = "1.0.100"
8+
tempfile = "3"

fortanix-vme/tools/confidential-vm-blobs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// TODO: cannot place in lfs - decide on final versions, store these somewhere and download them
2+
pub mod maybe_vendored;
23

34
pub const INIT: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/blobs/init"));
45

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! Utility type for blobs that are either passed in as an argument by a user or vendored
2+
3+
use anyhow::{Context as _, Result};
4+
use std::{
5+
io::Write as _,
6+
path::{Path, PathBuf},
7+
};
8+
9+
use tempfile::NamedTempFile;
10+
11+
pub enum MaybeVendoredImage {
12+
External(PathBuf),
13+
/// Unfortunately `ukify` receives its input as a file, so we store fallback blobs in temporary named
14+
/// files before passing them
15+
Vendored(NamedTempFile),
16+
}
17+
18+
impl MaybeVendoredImage {
19+
pub fn path(&self) -> &Path {
20+
match self {
21+
MaybeVendoredImage::External(path_buf) => path_buf,
22+
MaybeVendoredImage::Vendored(named_temp_file) => named_temp_file.path(),
23+
}
24+
}
25+
26+
/// Load a vendored blob to a temp file and create a instance of `Self` from that
27+
pub fn from_vendored(blob: &[u8]) -> Result<Self> {
28+
let temp_file = NamedTempFile::new()
29+
.and_then(|mut tempfile| tempfile.write_all(blob).map(|_| tempfile))
30+
.and_then(|mut tempfile| tempfile.flush().map(|_| tempfile))
31+
.context("failed to write backup kernel image to file")?;
32+
Ok(MaybeVendoredImage::Vendored(temp_file))
33+
}
34+
}
35+
36+
impl From<PathBuf> for MaybeVendoredImage {
37+
fn from(value: PathBuf) -> Self {
38+
MaybeVendoredImage::External(value)
39+
}
40+
}

fortanix-vme/tools/elf2uki/src/main.rs

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::io::{Cursor, Write};
1+
use std::io::Cursor;
22
use std::path::Path;
33
use std::process::Command;
44
use std::{fs::File, path::PathBuf};
55

66
use anyhow::{anyhow, Context as _, Result};
77
use clap::{crate_authors, crate_version, Args, Parser};
8-
use confidential_vm_blobs::{EFI_BOOT_STUB, INIT, KERNEL};
8+
use confidential_vm_blobs::{EFI_BOOT_STUB, INIT, KERNEL, maybe_vendored::MaybeVendoredImage};
99
use tempfile::NamedTempFile;
1010

1111
mod initramfs;
@@ -86,36 +86,6 @@ struct NonDefaultedArgs {
8686
kernel_cmdline: Option<String>,
8787
}
8888

89-
enum MaybeVendoredImage {
90-
External(PathBuf),
91-
/// Unfortunately `ukify` receives its input as a file, so we store fallback blobs in temporary named
92-
/// files before passing them
93-
Vendored(NamedTempFile),
94-
}
95-
96-
impl MaybeVendoredImage {
97-
fn path(&self) -> &Path {
98-
match self {
99-
MaybeVendoredImage::External(path_buf) => path_buf,
100-
MaybeVendoredImage::Vendored(named_temp_file) => named_temp_file.path(),
101-
}
102-
}
103-
104-
/// Load a vendored blob to a temp file and create a instance of `Self` from that
105-
fn from_vendored(blob: &[u8]) -> Result<Self> {
106-
let temp_file = NamedTempFile::new()
107-
.and_then(|mut tempfile| tempfile.write_all(blob).map(|_| tempfile))
108-
.and_then(|mut tempfile| tempfile.flush().map(|_| tempfile))
109-
.context("failed to write backup kernel image to file")?;
110-
Ok(MaybeVendoredImage::Vendored(temp_file))
111-
}
112-
}
113-
114-
impl From<PathBuf> for MaybeVendoredImage {
115-
fn from(value: PathBuf) -> Self {
116-
MaybeVendoredImage::External(value)
117-
}
118-
}
11989

12090
pub fn open_file(path: &Path) -> Result<File> {
12191
File::open(path).with_context(|| format!("failed to open file at path {}", path.display()))
@@ -207,8 +177,7 @@ fn main() -> Result<()> {
207177
let application_elf = open_file(&validated_args.non_defaulted_args.application_elf_path)?;
208178
let init = Cursor::new(INIT);
209179

210-
// Unfortunately `aws_nitro_enclaves_image_format::EifBuilder` forces us to have data in
211-
// files.
180+
// Unfortunately `ukify` forces us to have data in files.
212181
let mut initramfs_file = NamedTempFile::new().context("failed to create initramfs file")?;
213182
initramfs_file = initramfs::build(application_elf, init, initramfs_file)
214183
.context("failed to create initramfs")?;

0 commit comments

Comments
 (0)