diff mbox series

[1/1] sandbox: keep time offset when resetting

Message ID 20201230170748.108499-1-xypron.glpk@gmx.de
State Accepted
Delegated to: Simon Glass
Headers show
Series [1/1] sandbox: keep time offset when resetting | expand

Commit Message

Heinrich Schuchardt Dec. 30, 2020, 5:07 p.m. UTC
The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 arch/sandbox/cpu/os.c      | 25 +++++++++++++++++++++++++
 doc/arch/sandbox.rst       |  7 +++++++
 drivers/rtc/i2c_rtc_emul.c |  4 +++-
 include/os.h               | 18 ++++++++++++++++++
 4 files changed, 53 insertions(+), 1 deletion(-)

--
2.29.2

Comments

Simon Glass Jan. 7, 2021, 12:35 p.m. UTC | #1
Hi Heinrich,

On Wed, 30 Dec 2020 at 10:07, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> The UEFI Self Certification Test (SCT) checks the SetTime() service with
> the following steps:
>
> * set date
> * reset
> * check date matches
>
> To be compliant the sandbox should keep the offset to the host RTC during
> resets. The implementation uses the environment variable
> UBOOT_SB_TIME_OFFSET to persist the offset.
>
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> ---
>  arch/sandbox/cpu/os.c      | 25 +++++++++++++++++++++++++
>  doc/arch/sandbox.rst       |  7 +++++++
>  drivers/rtc/i2c_rtc_emul.c |  4 +++-
>  include/os.h               | 18 ++++++++++++++++++
>  4 files changed, 53 insertions(+), 1 deletion(-)

Sandbox writes driver settings to a state file that can be read on
start-up. Is that suitable here?

Regards,
Simon
Heinrich Schuchardt Jan. 21, 2021, 9:19 a.m. UTC | #2
On 07.01.21 13:35, Simon Glass wrote:
> Hi Heinrich,
>
> On Wed, 30 Dec 2020 at 10:07, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>>
>> The UEFI Self Certification Test (SCT) checks the SetTime() service with
>> the following steps:
>>
>> * set date
>> * reset
>> * check date matches
>>
>> To be compliant the sandbox should keep the offset to the host RTC during
>> resets. The implementation uses the environment variable
>> UBOOT_SB_TIME_OFFSET to persist the offset.
>>
>> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
>> ---
>>  arch/sandbox/cpu/os.c      | 25 +++++++++++++++++++++++++
>>  doc/arch/sandbox.rst       |  7 +++++++
>>  drivers/rtc/i2c_rtc_emul.c |  4 +++-
>>  include/os.h               | 18 ++++++++++++++++++
>>  4 files changed, 53 insertions(+), 1 deletion(-)
>
> Sandbox writes driver settings to a state file that can be read on
> start-up. Is that suitable here?

The sandbox only reads the state from file when using the -r and only
writes the state to while when using the -w option.

This should be documented in
https://u-boot.readthedocs.io/en/latest/arch/sandbox.html?highlight=sandbox#command-line-options

The RTC offset needs to be kept irrespective of command line arguments
during resets. I could not find a simpler way then using an environment
variable.

We could additionally persist the offset in the state file. Is this what
you ask for? For my testing purposes I don't need it.

Best regards

Heinrich
Simon Glass Jan. 21, 2021, 3:48 p.m. UTC | #3
Hi Heinrich,

On Thu, 21 Jan 2021 at 02:19, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 07.01.21 13:35, Simon Glass wrote:
> > Hi Heinrich,
> >
> > On Wed, 30 Dec 2020 at 10:07, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
> >>
> >> The UEFI Self Certification Test (SCT) checks the SetTime() service with
> >> the following steps:
> >>
> >> * set date
> >> * reset
> >> * check date matches
> >>
> >> To be compliant the sandbox should keep the offset to the host RTC during
> >> resets. The implementation uses the environment variable
> >> UBOOT_SB_TIME_OFFSET to persist the offset.
> >>
> >> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> >> ---
> >>  arch/sandbox/cpu/os.c      | 25 +++++++++++++++++++++++++
> >>  doc/arch/sandbox.rst       |  7 +++++++
> >>  drivers/rtc/i2c_rtc_emul.c |  4 +++-
> >>  include/os.h               | 18 ++++++++++++++++++
> >>  4 files changed, 53 insertions(+), 1 deletion(-)
> >
> > Sandbox writes driver settings to a state file that can be read on
> > start-up. Is that suitable here?
>
> The sandbox only reads the state from file when using the -r and only
> writes the state to while when using the -w option.
>
> This should be documented in
> https://u-boot.readthedocs.io/en/latest/arch/sandbox.html?highlight=sandbox#command-line-options
>
> The RTC offset needs to be kept irrespective of command line arguments
> during resets. I could not find a simpler way then using an environment
> variable.

Given the way that reset works (relaunching the ELF app) I don't know
of a better way either. I suppose you could launch it with the RTC
offset as a command-line parameter?

>
> We could additionally persist the offset in the state file. Is this what
> you ask for? For my testing purposes I don't need it.

I don't think so. It is designed for passing driver / state
information between U-Boot phases. I suppose it should be used for
persisting state across reset too, since in principle the emulated
hardware does not change just because sandbox has reset. But in that
case we don't really need to write a file, just keep it in memory
somewhere. Or at most we could write a temporary file.

Reviewed-by: Simon Glass <sjg@chromium.org>

Regards,
Simon
Simon Glass Jan. 30, 2021, 9:27 p.m. UTC | #4
Hi Heinrich,

On Thu, 21 Jan 2021 at 02:19, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 07.01.21 13:35, Simon Glass wrote:
> > Hi Heinrich,
> >
> > On Wed, 30 Dec 2020 at 10:07, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
> >>
> >> The UEFI Self Certification Test (SCT) checks the SetTime() service with
> >> the following steps:
> >>
> >> * set date
> >> * reset
> >> * check date matches
> >>
> >> To be compliant the sandbox should keep the offset to the host RTC during
> >> resets. The implementation uses the environment variable
> >> UBOOT_SB_TIME_OFFSET to persist the offset.
> >>
> >> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> >> ---
> >>  arch/sandbox/cpu/os.c      | 25 +++++++++++++++++++++++++
> >>  doc/arch/sandbox.rst       |  7 +++++++
> >>  drivers/rtc/i2c_rtc_emul.c |  4 +++-
> >>  include/os.h               | 18 ++++++++++++++++++
> >>  4 files changed, 53 insertions(+), 1 deletion(-)
> >
> > Sandbox writes driver settings to a state file that can be read on
> > start-up. Is that suitable here?
>
> The sandbox only reads the state from file when using the -r and only
> writes the state to while when using the -w option.
>
> This should be documented in
> https://u-boot.readthedocs.io/en/latest/arch/sandbox.html?highlight=sandbox#command-line-options
>
> The RTC offset needs to be kept irrespective of command line arguments
> during resets. I could not find a simpler way then using an environment
> variable.

Given the way that reset works (relaunching the ELF app) I don't know
of a better way either. I suppose you could launch it with the RTC
offset as a command-line parameter?

>
> We could additionally persist the offset in the state file. Is this what
> you ask for? For my testing purposes I don't need it.

I don't think so. It is designed for passing driver / state
information between U-Boot phases. I suppose it should be used for
persisting state across reset too, since in principle the emulated
hardware does not change just because sandbox has reset. But in that
case we don't really need to write a file, just keep it in memory
somewhere. Or at most we could write a temporary file.

Reviewed-by: Simon Glass <sjg@chromium.org>

Regards,
Simon

Applied to u-boot-dm, thanks!
diff mbox series

Patch

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index b56fa04a34..87fce57a36 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -32,6 +32,9 @@ 
 #include <os.h>
 #include <rtc_def.h>

+/* Environment variable for time offset */
+#define ENV_TIME_OFFSET "UBOOT_SB_TIME_OFFSET"
+
 /* Operating System Interface */

 struct os_mem_hdr {
@@ -793,6 +796,28 @@  int os_spl_to_uboot(const char *fname)
 	return os_jump_to_file(fname);
 }

+long os_get_time_offset(void)
+{
+	const char *offset;
+
+	offset = getenv(ENV_TIME_OFFSET);
+	if (offset)
+		return strtol(offset, NULL, 0);
+	return 0;
+}
+
+void os_set_time_offset(long offset)
+{
+	char buf[21];
+	int ret;
+
+	snprintf(buf, sizeof(buf), "%ld", offset);
+	ret = setenv(ENV_TIME_OFFSET, buf, true);
+	if (ret)
+		printf("Could not set environment variable %s\n",
+		       ENV_TIME_OFFSET);
+}
+
 void os_localtime(struct rtc_time *rt)
 {
 	time_t t = time(NULL);
diff --git a/doc/arch/sandbox.rst b/doc/arch/sandbox.rst
index 4674c420ac..ee720c5a09 100644
--- a/doc/arch/sandbox.rst
+++ b/doc/arch/sandbox.rst
@@ -131,6 +131,13 @@  available options. Some of these are described below:
 * -i
   - Go to interactive mode after executing the commands specified by -c.

+Environment Variables
+---------------------
+
+UBOOT_SB_TIME_OFFSET
+    This environment variable stores the offset of the emulated real time clock
+    to the host's real time clock in seconds. The offset defaults to zero.
+
 Memory Emulation
 ----------------

diff --git a/drivers/rtc/i2c_rtc_emul.c b/drivers/rtc/i2c_rtc_emul.c
index 7f78ff83cb..ef6f6a8e1d 100644
--- a/drivers/rtc/i2c_rtc_emul.c
+++ b/drivers/rtc/i2c_rtc_emul.c
@@ -57,6 +57,7 @@  long sandbox_i2c_rtc_set_offset(struct udevice *dev, bool use_system_time,
 	plat->use_system_time = use_system_time;
 	if (offset != -1)
 		plat->offset = offset;
+	os_set_time_offset(plat->offset);

 	return old_offset;
 }
@@ -80,7 +81,7 @@  static void reset_time(struct udevice *dev)

 	os_localtime(&now);
 	plat->base_time = rtc_mktime(&now);
-	plat->offset = 0;
+	plat->offset = os_get_time_offset();
 	plat->use_system_time = true;
 }

@@ -115,6 +116,7 @@  static int sandbox_i2c_rtc_set(struct udevice *dev, const struct rtc_time *time)
 		now = plat->base_time;
 	}
 	plat->offset = rtc_mktime(time) - now;
+	os_set_time_offset(plat->offset);

 	return 0;
 }
diff --git a/include/os.h b/include/os.h
index 0913b47b3a..e192e32d59 100644
--- a/include/os.h
+++ b/include/os.h
@@ -424,4 +424,22 @@  int os_setup_signal_handlers(void);
  */
 void os_signal_action(int sig, unsigned long pc);

+/**
+ * os_get_time_offset() - get time offset
+ *
+ * Get the time offset from environment variable UBOOT_SB_TIME_OFFSET.
+ *
+ * Return:	offset in seconds
+ */
+long os_get_time_offset(void);
+
+/**
+ * os_set_time_offset() - set time offset
+ *
+ * Save the time offset in environment variable UBOOT_SB_TIME_OFFSET.
+ *
+ * @offset:	offset in seconds
+ */
+void os_set_time_offset(long offset);
+
 #endif