use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, Copy, PartialEq, Eq)]
pub enum CompressionAlgorithm {
Gzip,
}
impl CompressionAlgorithm {
pub fn as_str(&self) -> &'static str {
match self {
CompressionAlgorithm::Gzip => "gzip",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FitImageConfig {
pub description: String,
pub kernel: Option<ComponentConfig>,
pub fdt: Option<ComponentConfig>,
pub ramdisk: Option<ComponentConfig>,
pub default_config: Option<String>,
pub configurations: std::collections::HashMap<String, FitConfiguration>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FitConfiguration {
pub name: String,
pub description: String,
pub kernel: Option<String>,
pub fdt: Option<String>,
pub ramdisk: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentConfig {
pub name: String,
pub data: Vec<u8>,
pub description: Option<String>,
pub component_type: Option<String>,
pub arch: Option<String>,
pub os: Option<String>,
pub compression: bool,
pub load_address: Option<u64>,
pub entry_point: Option<u64>,
}
impl ComponentConfig {
pub fn new(name: impl Into<String>, data: Vec<u8>) -> Self {
Self {
name: name.into(),
data,
description: None,
component_type: None,
arch: None,
os: None,
compression: false,
load_address: None,
entry_point: None,
}
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn with_type(mut self, component_type: impl Into<String>) -> Self {
self.component_type = Some(component_type.into());
self
}
pub fn with_arch(mut self, arch: impl Into<String>) -> Self {
self.arch = Some(arch.into());
self
}
pub fn with_os(mut self, os: impl Into<String>) -> Self {
self.os = Some(os.into());
self
}
pub fn with_compression(mut self, b: bool) -> Self {
self.compression = b;
self
}
pub fn with_load_address(mut self, load_address: u64) -> Self {
self.load_address = Some(load_address);
self
}
pub fn with_entry_point(mut self, entry_point: u64) -> Self {
self.entry_point = Some(entry_point);
self
}
}
impl FitImageConfig {
pub fn new(description: impl Into<String>) -> Self {
Self {
description: description.into(),
kernel: None,
fdt: None,
ramdisk: None,
default_config: None,
configurations: std::collections::HashMap::new(),
}
}
pub fn with_kernel(mut self, kernel: ComponentConfig) -> Self {
self.kernel = Some(kernel);
self
}
pub fn with_fdt(mut self, fdt: ComponentConfig) -> Self {
self.fdt = Some(fdt);
self
}
pub fn with_ramdisk(mut self, ramdisk: ComponentConfig) -> Self {
self.ramdisk = Some(ramdisk);
self
}
pub fn with_default_config(mut self, default: impl Into<String>) -> Self {
self.default_config = Some(default.into());
self
}
pub fn with_configuration(
mut self,
name: impl Into<String>,
description: impl Into<String>,
kernel: Option<impl Into<String>>,
fdt: Option<impl Into<String>>,
ramdisk: Option<impl Into<String>>,
) -> Self {
let name = name.into();
self.configurations.insert(
name.clone(),
FitConfiguration {
name,
description: description.into(),
kernel: kernel.map(Into::into),
fdt: fdt.map(Into::into),
ramdisk: ramdisk.map(Into::into),
},
);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_creation() {
let config = FitImageConfig::new("Test FIT")
.with_kernel(ComponentConfig::new("kernel", vec![1, 2, 3]).with_compression(true))
.with_fdt(ComponentConfig::new("fdt", vec![4, 5, 6]));
assert_eq!(config.description, "Test FIT");
assert!(config.kernel.is_some());
assert!(config.fdt.is_some());
assert!(config.ramdisk.is_none());
}
#[test]
fn test_component_config() {
let component = ComponentConfig::new("test", vec![1, 2, 3])
.with_description("Test component")
.with_type("kernel")
.with_arch("arm64")
.with_os("linux")
.with_compression(false)
.with_load_address(0x80000)
.with_entry_point(0x80000);
assert_eq!(component.name, "test");
assert_eq!(component.data, vec![1, 2, 3]);
assert_eq!(component.description, Some("Test component".to_string()));
assert_eq!(component.component_type, Some("kernel".to_string()));
assert_eq!(component.arch, Some("arm64".to_string()));
assert_eq!(component.os, Some("linux".to_string()));
assert!(!component.compression);
assert_eq!(component.load_address, Some(0x80000));
assert_eq!(component.entry_point, Some(0x80000));
}
#[test]
fn test_fit_image_config_with_configurations() {
let config = FitImageConfig::new("Test FIT")
.with_kernel(ComponentConfig::new("kernel", vec![1, 2, 3]))
.with_fdt(ComponentConfig::new("fdt", vec![4, 5, 6]))
.with_default_config("default")
.with_configuration(
"default",
"Default configuration",
Some("kernel"),
Some("fdt"),
None::<String>,
);
assert_eq!(config.description, "Test FIT");
assert!(config.kernel.is_some());
assert!(config.fdt.is_some());
assert_eq!(config.default_config, Some("default".to_string()));
assert!(config.configurations.contains_key("default"));
}
}