Skip to content

Commit 18a0306

Browse files
committed
feat: Add format detection logic
This logic is used both by the loader and by uhyve.
1 parent 18b2d71 commit 18a0306

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,35 @@ pub use const_parse::parse_u128 as _parse_u128;
2626
#[doc(hidden)]
2727
pub use note::{_AbiTag, _Note};
2828

29+
/// Possible input formats for a Hermit loader.
30+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
31+
pub enum Format {
32+
/// An ELF kernel image.
33+
ElfKernel,
34+
/// A gzipped tar file containing a config + ELF kernel image, and associated files.
35+
GzipImage,
36+
}
37+
38+
/// Attempts to detect the format of an input file (using magic bytes), whether it is an ELF kernel or an image.
39+
pub fn detect_format(data: &[u8]) -> Option<Format> {
40+
if data.len() < 8 {
41+
None
42+
} else if data[0] == 0x7f
43+
&& data[1] == b'E'
44+
&& data[2] == b'L'
45+
&& data[3] == b'F'
46+
&& data[7] == 0xff
47+
{
48+
// ELF with vendor-specific ABI => assume ELF kernel
49+
Some(Format::ElfKernel)
50+
} else if data[0] == 0x1f && data[1] == 0x8b && data[2] == 0x08 {
51+
// gzip => assume image
52+
Some(Format::GzipImage)
53+
} else {
54+
None
55+
}
56+
}
57+
2958
/// Kernel entry point.
3059
///
3160
/// This is the signature of the entry point of the kernel.

0 commit comments

Comments
 (0)