From a77be3c28270ede4ea7daac86b40aca933ea1f8b Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Thu, 30 Oct 2025 02:51:52 -0400 Subject: [PATCH] feat(bootloader-interface): measure time in firmware as well --- src/integrations/bootloader_interface.rs | 16 +++++++++++----- src/main.rs | 2 +- src/platform.rs | 2 +- src/platform/timer.rs | 7 ++++++- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/integrations/bootloader_interface.rs b/src/integrations/bootloader_interface.rs index b291d70..0e6cf91 100644 --- a/src/integrations/bootloader_interface.rs +++ b/src/integrations/bootloader_interface.rs @@ -11,15 +11,21 @@ impl BootloaderInterface { const VENDOR: VariableVendor = VariableVendor(guid!("4a67b082-0a4c-41cf-b6c7-440b29bb8c4f")); /// Tell the system that Sprout was initialized at the current time. - pub fn mark_init() -> Result<()> { - Self::set_cstr16("LoaderTimeInitUSec", "0") + pub fn mark_init(timer: &PlatformTimer) -> Result<()> { + Self::mark_time("LoaderTimeInitUSec", timer) } /// Tell the system that Sprout is about to execute the boot entry. pub fn mark_exec(timer: &PlatformTimer) -> Result<()> { - // Measure the elapsed time since the bootloader was started. - let elapsed = timer.elapsed(); - Self::set_cstr16("LoaderTimeExecUSec", &elapsed.as_micros().to_string()) + Self::mark_time("LoaderTimeExecUSec", timer) + } + + /// Tell the system about the current time as measured by the platform timer. + /// Sets the variable specified by `key` to the number of microseconds. + fn mark_time(key: &str, timer: &PlatformTimer) -> Result<()> { + // Measure the elapsed time since the hardware timer was started. + let elapsed = timer.elapsed_since_lifetime(); + Self::set_cstr16(key, &elapsed.as_micros().to_string()) } /// Tell the system what the partition GUID of the ESP Sprout was booted from is. diff --git a/src/main.rs b/src/main.rs index 7a62242..8385dd5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,7 +69,7 @@ fn run() -> Result<()> { let timer = PlatformTimer::start(); // Mark the initialization of Sprout in the bootloader interface. - BootloaderInterface::mark_init() + BootloaderInterface::mark_init(&timer) .context("unable to mark initialization in bootloader interface")?; // Parse the options to the sprout executable. diff --git a/src/platform.rs b/src/platform.rs index 25b5b1f..66d17b4 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -1,2 +1,2 @@ -/// timer: Platform timer support code. +/// timer: Platform timer support. pub mod timer; diff --git a/src/platform/timer.rs b/src/platform/timer.rs index 80e151a..9a2d9be 100644 --- a/src/platform/timer.rs +++ b/src/platform/timer.rs @@ -73,8 +73,13 @@ impl PlatformTimer { } } + /// Measure the elapsed duration since the hardware started ticking upwards. + pub fn elapsed_since_lifetime(&self) -> Duration { + self.frequency.duration(arch_ticks()) + } + /// Measure the elapsed duration since the timer was started. - pub fn elapsed(&self) -> Duration { + pub fn elapsed_since_start(&self) -> Duration { let duration = arch_ticks() - self.start; self.frequency.duration(duration) }