diff --git a/devtools/td-layout-config/config_image.json b/devtools/td-layout-config/config_image.json index dbf4e392..f00ed142 100644 --- a/devtools/td-layout-config/config_image.json +++ b/devtools/td-layout-config/config_image.json @@ -4,7 +4,7 @@ "TempStack": "0x020000", "TempHeap": "0x020000", "Metadata": "0x001000", - "Payload": "0xC2D000", + "Payload": "0xC2E000", "Ipl": "0x348000", "ResetVector": "0x008000" } \ No newline at end of file diff --git a/devtools/td-layout-config/config_memory_exec.json b/devtools/td-layout-config/config_memory_exec.json index 90b93368..937e711e 100644 --- a/devtools/td-layout-config/config_memory_exec.json +++ b/devtools/td-layout-config/config_memory_exec.json @@ -10,6 +10,11 @@ "size": "0x20000", "type": "Memory" }, + { + "name": "PayloadImage", + "size": "0xC2E000", + "type": "Memory" + }, { "name": "EventLog", "size": "0x100000", diff --git a/devtools/td-layout-config/src/image.rs b/devtools/td-layout-config/src/image.rs index 7696da27..319dcb11 100644 --- a/devtools/td-layout-config/src/image.rs +++ b/devtools/td-layout-config/src/image.rs @@ -2,6 +2,9 @@ use serde::Deserialize; use super::{layout::LayoutConfig, render}; +const FIRMWARE_ROM_BASE: usize = 0xFF00_0000; +const FIRMWARE_ROM_SIZE: usize = 0x100_0000; + #[derive(Deserialize, Debug, PartialEq)] struct ImageConfig { #[serde(rename = "Config")] @@ -28,60 +31,62 @@ pub fn parse_image(data: String) -> String { let image_config = serde_json::from_str::(&data) .expect("Content is configuration file is invalid"); - let mut image_layout = LayoutConfig::new(0, 0x100_0000); - image_layout.reserve_low( - "Config", - parse_int::parse::(&image_config.config).unwrap() as usize, - "Reserved", - ); - image_layout.reserve_low( - "Mailbox", - parse_int::parse::(&image_config.mailbox).unwrap() as usize, - "Reserved", - ); - image_layout.reserve_low( - "TempStack", - parse_int::parse::(&image_config.temp_stack).unwrap() as usize, - "Reserved", - ); - image_layout.reserve_low( - "TempHeap", - parse_int::parse::(&image_config.temp_heap).unwrap() as usize, - "Reserved", - ); - - image_layout.reserve_high( - "ResetVector", - parse_int::parse::(&image_config.reset_vector).unwrap() as usize, - "Reserved", - ); - image_layout.reserve_high( - "Ipl", - parse_int::parse::(&image_config.bootloader).unwrap() as usize, - "Reserved", - ); + let config_size = parse_int::parse::(&image_config.config).unwrap() as usize; + let mailbox_size = parse_int::parse::(&image_config.mailbox).unwrap() as usize; + let temp_stack_size = parse_int::parse::(&image_config.temp_stack).unwrap() as usize; + let temp_heap_size = parse_int::parse::(&image_config.temp_heap).unwrap() as usize; + let reset_vector_size = parse_int::parse::(&image_config.reset_vector).unwrap() as usize; + let ipl_size = parse_int::parse::(&image_config.bootloader).unwrap() as usize; + let metadata_size = parse_int::parse::(&image_config.metadata).unwrap() as usize; + let payload_size = parse_int::parse::( + &image_config + .builtin_payload + .unwrap_or_else(|| "0".to_string()), + ) + .unwrap() as usize; + let td_info_size = + parse_int::parse::(&image_config.td_info.unwrap_or_else(|| "0".to_string())).unwrap() + as usize; - image_layout.reserve_high( - "Metadata", - parse_int::parse::(&image_config.metadata).unwrap() as usize, - "Reserved", - ); - - if let Some(td_info_config) = image_config.td_info { - image_layout.reserve_high( - "TdInfo", - parse_int::parse::(&td_info_config).unwrap() as usize, - "Reserved", - ) + // Build firmware image layout + let image_size = config_size + + reset_vector_size + + mailbox_size + + temp_heap_size + + temp_stack_size + + ipl_size + + metadata_size + + payload_size + + td_info_size; + let mut image_layout = LayoutConfig::new(0, image_size); + image_layout.reserve_low("Config", config_size, "Image"); + image_layout.reserve_low("Mailbox", mailbox_size, "Rom"); + image_layout.reserve_low("TempStack", temp_stack_size, "Rom"); + image_layout.reserve_low("TempHeap", temp_heap_size, "Rom"); + image_layout.reserve_high("ResetVector", reset_vector_size, "Image"); + image_layout.reserve_high("Ipl", ipl_size, "Image"); + image_layout.reserve_high("Metadata", metadata_size, "Image"); + if td_info_size != 0 { + image_layout.reserve_high("TdInfo", td_info_size, "Image") + } + if payload_size != 0 { + image_layout.reserve_high("Payload", payload_size, "Image") } - if let Some(payload_config) = image_config.builtin_payload { - image_layout.reserve_high( - "Payload", - parse_int::parse::(&payload_config).unwrap() as usize, - "Reserved", - ) + // Build ROM layout at memory space: 0xFF00_0000 - 0xFFFF_FFFF + // Payload image is not loaded into ROM space. + let mut rom_layout = + LayoutConfig::new(FIRMWARE_ROM_BASE, FIRMWARE_ROM_BASE + FIRMWARE_ROM_SIZE); + rom_layout.reserve_low("Config", config_size, "Rom"); + rom_layout.reserve_low("Mailbox", mailbox_size, "Rom"); + rom_layout.reserve_low("TempStack", temp_stack_size, "Rom"); + rom_layout.reserve_low("TempHeap", temp_heap_size, "Rom"); + rom_layout.reserve_high("ResetVector", reset_vector_size, "Rom"); + rom_layout.reserve_high("Ipl", ipl_size, "Rom"); + rom_layout.reserve_high("Metadata", metadata_size, "Rom"); + if td_info_size != 0 { + rom_layout.reserve_high("TdInfo", td_info_size, "Rom") } - render::render_image(&image_layout).expect("Render image layout failed!") + render::render_image(&image_layout, &rom_layout).expect("Render image layout failed!") } diff --git a/devtools/td-layout-config/src/render.rs b/devtools/td-layout-config/src/render.rs index b125b6d0..2c2b2d5c 100644 --- a/devtools/td-layout-config/src/render.rs +++ b/devtools/td-layout-config/src/render.rs @@ -9,7 +9,7 @@ use tera::{Context, Result, Tera}; use super::layout::{LayoutConfig, ENTRY_TYPE_FILTER}; /// Render image layout file. -pub fn render_image(image_layout: &LayoutConfig) -> Result { +pub fn render_image(image_layout: &LayoutConfig, rom_layout: &LayoutConfig) -> Result { let mut tera = Tera::default(); tera.register_filter("format_hex", format_hex); tera.register_filter("format_name", format_name); @@ -19,12 +19,12 @@ pub fn render_image(image_layout: &LayoutConfig) -> Result { let mut context = Context::new(); context.insert("image_regions", image_layout.get_regions()); context.insert("image_size", &image_layout.get_top()); + context.insert("rom_regions", &rom_layout.get_regions()); + context.insert("rom_size", &(rom_layout.get_top() - rom_layout.get_base())); // Image size - metadata pointer offset(0x20) - OVMF GUID table size(0x28) - SEC Core information size(0xC). context.insert("sec_info_offset", &(image_layout.get_top() - 0x54)); - context.insert( - "memory_offset", - &(u32::MAX as usize + 1 - &image_layout.get_top()), - ); + context.insert("sec_info_base", &(rom_layout.get_top() - 0x54)); + context.insert("rom_base", &rom_layout.get_base()); context.insert("entry_type_filter", ENTRY_TYPE_FILTER); tera.render("image.rs", &context) } diff --git a/devtools/td-layout-config/src/template/image.jinja b/devtools/td-layout-config/src/template/image.jinja index 3c598cc8..a85a49a8 100644 --- a/devtools/td-layout-config/src/template/image.jinja +++ b/devtools/td-layout-config/src/template/image.jinja @@ -14,21 +14,26 @@ Image Layout Image size: {{image_size|format_hex}} ({{image_size|filesizeformat}}) */ -// Image Layout Configuration -{%for i in image_regions%} +// Image configuration +pub const TD_SHIM_IMAGE_SIZE: u32 = {{image_size | format_hex }}; +{%-for i in image_regions%} pub const TD_SHIM_{{i.name_screaming_snake_case}}_OFFSET: u32 = {{i.region.start | format_hex }}; -pub const TD_SHIM_{{i.name_screaming_snake_case}}_SIZE: u32 = {{i.region.end - i.region.start | format_hex }}; // {{i.region.end - i.region.start|filesizeformat}} -{%endfor%} -// Offset when Loading into Memory -pub const TD_SHIM_FIRMWARE_BASE: u32 = {{memory_offset | format_hex }}; -pub const TD_SHIM_FIRMWARE_SIZE: u32 = {{image_size | format_hex }}; +{%-endfor%} + +// Size of regions +{%-for i in image_regions%} +pub const TD_SHIM_{{i.name_screaming_snake_case}}_SIZE: u32 = {{i.region.end - i.region.start | format_hex }}; +{%-endfor%} + +pub const TD_SHIM_FIRMWARE_BASE: u32 = {{rom_base | format_hex }}; +pub const TD_SHIM_FIRMWARE_SIZE: u32 = {{rom_size | format_hex }}; + +// ROM configuration +{%-for i in rom_regions%} +pub const TD_SHIM_{{i.name_screaming_snake_case}}_BASE: u32 = {{i.region.start | format_hex }}; +{%-endfor%} // TD_SHIM_SEC_INFO_OFFSET equals to firmware size - metadata pointer offset - // OVMF GUID table size - SEC Core information size. pub const TD_SHIM_SEC_CORE_INFO_OFFSET: u32 = {{sec_info_offset | format_hex }}; -pub const TD_SHIM_SEC_CORE_INFO_BASE: u32 = {{memory_offset + sec_info_offset | format_hex }}; - -// Base Address after Loaded into Memory -{%-for i in image_regions%} -pub const TD_SHIM_{{i.name_screaming_snake_case}}_BASE: u32 = {{memory_offset + i.region.start | format_hex }}; -{%-endfor%} +pub const TD_SHIM_SEC_CORE_INFO_BASE: u32 = {{sec_info_base | format_hex }}; \ No newline at end of file