Skip to content

Commit

Permalink
Add efi_time_t and time conversion and formatting utilities
Browse files Browse the repository at this point in the history
This adds the hilariously defective efi_time_t type, as well as
conversion utilities to and from it, and utilities for formatted output
of it.

Optionally, for compatibility with older source trees, if your
application defines EFIVAR_NO_EFI_TIME_T to any value other than 0, it
will *not* do so, though that option may go away in some future efivar
version.  This allows you to turn off declaration of efi_time_t and
related functions in the case where it's declared someplace else, such
as in some local code or another library's headers.

Signed-off-by: Peter Jones <pjones@redhat.com>
  • Loading branch information
vathpela authored and frozencemetery committed Nov 11, 2021
1 parent 8882f9e commit 011f3e5
Show file tree
Hide file tree
Showing 8 changed files with 473 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LIBEFIBOOT_SOURCES = crc32.c creator.c disk.c gpt.c loadopt.c path-helpers.c \
LIBEFIBOOT_OBJECTS = $(patsubst %.c,%.o,$(LIBEFIBOOT_SOURCES))
LIBEFIVAR_SOURCES = crc32.c dp.c dp-acpi.c dp-hw.c dp-media.c dp-message.c \
efivarfs.c error.c export.c guid.c guids.S guid-symbols.c \
lib.c vars.c
lib.c vars.c time.c
LIBEFIVAR_OBJECTS = $(patsubst %.S,%.o,$(patsubst %.c,%.o,$(LIBEFIVAR_SOURCES)))
EFIVAR_SOURCES = efivar.c
EFIVAR_OBJECTS = $(patsubst %.S,%.o,$(patsubst %.c,%.o,$(EFIVAR_SOURCES)))
Expand Down
33 changes: 33 additions & 0 deletions src/include/efivar/efivar-time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: LGPL-2.1
/*
* efivar-time.h
* Copyright 2020 Peter Jones <pjones@redhat.com>
*/

#if defined(EFIVAR_NO_EFI_TIME_T) && EFIVAR_NO_EFI_TIME_T && \
!defined(EFIVAR_TIME_H_)
#define EFIVAR_TIME_H_ 1
#endif

#ifndef EFIVAR_TIME_H_
#define EFIVAR_TIME_H_ 1

#include <stdbool.h>

extern int tm_to_efi_time(const struct tm *const s, efi_time_t *d, bool tzadj);
extern int efi_time_to_tm(const efi_time_t * const s, struct tm *d);

extern char *efi_asctime(const efi_time_t *const time);
extern char *efi_asctime_r(const efi_time_t *const time, char *buf);
extern efi_time_t *efi_gmtime(const time_t *time);
extern efi_time_t *efi_gmtime_r(const time_t *time, efi_time_t *result);
extern efi_time_t *efi_localtime(const time_t *time);
extern efi_time_t *efi_localtime_r(const time_t *time, efi_time_t *result);
extern time_t efi_mktime(const efi_time_t *const time);

extern char *efi_strptime(const char *s, const char *format, efi_time_t *time);
extern size_t efi_strftime(char *s, size_t max, const char *format,
const efi_time_t *time);

#endif /* !EFIVAR_TIME_H_ */
// vim:fenc=utf-8:tw=75:noet
35 changes: 35 additions & 0 deletions src/include/efivar/efivar-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,41 @@ typedef uint16_t efi_char16_t;
typedef unsigned long uintn_t;
typedef long intn_t;

#if !defined(EFIVAR_NO_EFI_TIME_T) || EFIVAR_NO_EFI_TIME_T
#define EFIVAR_HAVE_EFI_TIME_T 1

/*
* This can never be correct, as defined, in the face of leap seconds.
* Because seconds here are defined with a range of [0,59], we can't
* express leap seconds correctly there. Because TimeZone is specified in
* minutes West of UTC, rather than seconds (like struct tm), it can't be
* used to correct when we cross a leap second boundary condition. As a
* result, EFI_TIME can only express UT1, rather than UTC, and there's no
* way when converting to know wether the error has been taken into
* account, nor if it should be.
*
* As I write this, there is a 37 second error.
*/
typedef struct {
uint16_t year; // 1900 - 9999
uint8_t month; // 1 - 12
uint8_t day; // 1 - 31
uint8_t hour; // 0 - 23
uint8_t minute; // 0 - 59
uint8_t second; // 0 - 59 // ha ha only serious
uint8_t pad1; // 0
uint32_t nanosecond; // 0 - 999,999,999
int16_t timezone; // minutes from UTC or EFI_UNSPECIFIED_TIMEZONE
uint8_t daylight; // bitfield
uint8_t pad2; // 0
} efi_time_t __attribute__((__aligned__(1)));

#define EFI_TIME_ADJUST_DAYLIGHT ((uint8_t)0x01)
#define EFI_TIME_IN_DAYLIGHT ((uint8_t)0x02)

#define EFI_UNSPECIFIED_TIMEZONE ((uint16_t)0x07ff)
#endif /* !defined(EFIVAR_NO_EFI_TIME_T) || EFIVAR_NO_EFI_TIME_T */

#define EFI_VARIABLE_NON_VOLATILE ((uint64_t)0x0000000000000001)
#define EFI_VARIABLE_BOOTSERVICE_ACCESS ((uint64_t)0x0000000000000002)
#define EFI_VARIABLE_RUNTIME_ACCESS ((uint64_t)0x0000000000000004)
Expand Down
2 changes: 2 additions & 0 deletions src/include/efivar/efivar.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <byteswap.h>

Expand Down Expand Up @@ -200,6 +201,7 @@ extern uint32_t efi_get_libefivar_version(void)
__attribute__((__visibility__("default")));

#include <efivar/efivar-dp.h>
#include <efivar/efivar-time.h>

#endif /* EFIVAR_H */

Expand Down
14 changes: 7 additions & 7 deletions src/libefiboot.abixml
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@
<parameter type-id='type-id-89'/>
<return type-id='type-id-89'/>
</function-decl>
<function-decl name='efi_error_set' filepath='src/include/efivar/efivar.h' line='128' column='1' visibility='default' binding='global' size-in-bits='64'>
<function-decl name='efi_error_set' filepath='src/include/efivar/efivar.h' line='129' column='1' visibility='default' binding='global' size-in-bits='64'>
<parameter type-id='type-id-98'/>
<parameter type-id='type-id-98'/>
<parameter type-id='type-id-15'/>
Expand All @@ -679,14 +679,14 @@
<parameter is-variadic='yes'/>
<return type-id='type-id-15'/>
</function-decl>
<function-decl name='efi_error_clear' filepath='src/include/efivar/efivar.h' line='136' column='1' visibility='default' binding='global' size-in-bits='64'>
<function-decl name='efi_error_clear' filepath='src/include/efivar/efivar.h' line='137' column='1' visibility='default' binding='global' size-in-bits='64'>
<return type-id='type-id-28'/>
</function-decl>
<function-decl name='efi_set_loglevel' filepath='src/include/efivar/efivar.h' line='138' column='1' visibility='default' binding='global' size-in-bits='64'>
<function-decl name='efi_set_loglevel' filepath='src/include/efivar/efivar.h' line='139' column='1' visibility='default' binding='global' size-in-bits='64'>
<parameter type-id='type-id-15'/>
<return type-id='type-id-28'/>
</function-decl>
<function-decl name='efi_get_logfile' filepath='src/include/efivar/efivar.h' line='196' column='1' visibility='default' binding='global' size-in-bits='64'>
<function-decl name='efi_get_logfile' filepath='src/include/efivar/efivar.h' line='197' column='1' visibility='default' binding='global' size-in-bits='64'>
<return type-id='type-id-91'/>
</function-decl>
<function-decl name='device_get' filepath='src/linux.h' line='142' column='1' visibility='default' binding='global' size-in-bits='64'>
Expand Down Expand Up @@ -1082,7 +1082,7 @@
<parameter type-id='type-id-122'/>
<return type-id='type-id-89'/>
</function-decl>
<function-decl name='efi_str_to_guid' filepath='src/include/efivar/efivar.h' line='56' column='1' visibility='default' binding='global' size-in-bits='64'>
<function-decl name='efi_str_to_guid' filepath='src/include/efivar/efivar.h' line='57' column='1' visibility='default' binding='global' size-in-bits='64'>
<parameter type-id='type-id-98'/>
<parameter type-id='type-id-122'/>
<return type-id='type-id-15'/>
Expand Down Expand Up @@ -1191,7 +1191,7 @@
<parameter type-id='type-id-89'/>
<return type-id='type-id-89'/>
</function-decl>
<function-decl name='efi_error_pop' filepath='src/include/efivar/efivar.h' line='137' column='1' visibility='default' binding='global' size-in-bits='64'>
<function-decl name='efi_error_pop' filepath='src/include/efivar/efivar.h' line='138' column='1' visibility='default' binding='global' size-in-bits='64'>
<return type-id='type-id-28'/>
</function-decl>
<function-decl name='find_path_segment' filepath='src/path-helpers.h' line='12' column='1' visibility='default' binding='global' size-in-bits='64'>
Expand Down Expand Up @@ -2240,7 +2240,7 @@
<pointer-type-def type-id='type-id-32' size-in-bits='64' id='type-id-281'/>
<pointer-type-def type-id='type-id-24' size-in-bits='64' id='type-id-138'/>
<pointer-type-def type-id='type-id-138' size-in-bits='64' id='type-id-282'/>
<function-decl name='efi_get_verbose' filepath='src/include/efivar/efivar.h' line='194' column='1' visibility='default' binding='global' size-in-bits='64'>
<function-decl name='efi_get_verbose' filepath='src/include/efivar/efivar.h' line='195' column='1' visibility='default' binding='global' size-in-bits='64'>
<return type-id='type-id-15'/>
</function-decl>
<function-decl name='efi_loadopt_create' mangled-name='efi_loadopt_create' filepath='src/loadopt.c' line='26' column='1' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='efi_loadopt_create@@libefiboot.so.0'>
Expand Down
Loading

0 comments on commit 011f3e5

Please sign in to comment.