Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add efi_time_t declarations and helper functions. #183

Merged
merged 3 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 38 additions & 2 deletions src/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#ifndef COMPILER_H_
#define COMPILER_H_

#include <sys/cdefs.h>

/* GCC version checking borrowed from glibc. */
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
# define GNUC_PREREQ(maj,min) \
Expand Down Expand Up @@ -43,8 +45,42 @@
# define CLANG_PREREQ(maj,min) 0
#endif

#define PASTE(x, y) x ## y
#define PASTE3(x, y, z) x ## y ## z
#define UNUSED __attribute__((__unused__))
#define HIDDEN __attribute__((__visibility__ ("hidden")))
#define PUBLIC __attribute__((__visibility__ ("default")))
#define DESTRUCTOR __attribute__((__destructor__))
#define CONSTRUCTOR __attribute__((__constructor__))
#define ALIAS(x) __attribute__((weak, alias (#x)))
#define NONNULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
#define PRINTF(...) __attribute__((__format__(printf, __VA_ARGS__)))
#define FLATTEN __attribute__((__flatten__))
#define PACKED __attribute__((__packed__))
#if defined(__clang__)
# define VERSION(sym, ver)
#else
# if GNUC_PREREQ(10,0)
# define VERSION(sym, ver) __attribute__ ((symver (# ver)))
# else
# define VERSION(sym, ver) __asm__(".symver " # sym "," # ver)
# endif
#endif
#define NORETURN __attribute__((__noreturn__))
#define ALIGNED(n) __attribute__((__aligned__(n)))
#define CLEANUP_FUNC(x) __attribute__((__cleanup__(x)))

#define __CONCAT3(a, b, c) a ## b ## c
#define CONCATENATE(a, b) __CONCAT(a, b)
#define CAT(a, b) __CONCAT(a, b)
#define CAT3(a, b, c) __CONCAT3(a, b, c)
#define STRING(x) __STRING(x)

#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
#define __ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
#define ALIGN(x, a) __ALIGN((x), (a))
#define ALIGN_DOWN(x, a) __ALIGN((x) - ((a) - 1), (a))

#define ALIGNMENT_PADDING(value, align) ((align - (value % align)) % align)
#define ALIGN_UP(value, align) ((value) + ALIGNMENT_PADDING(value, align))

#endif /* !COMPILER_H_ */
// vim:fenc=utf-8:tw=75:noet
2 changes: 2 additions & 0 deletions src/efivar.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

#include <efivar/efivar.h>

#include "compiler.h"
#include "diag.h"
#include "list.h"
#include "util.h"
#include "safemath.h"
#include "efivar_endian.h"
Expand Down
8 changes: 0 additions & 8 deletions src/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@
#define ATTRS_UNSET 0xa5a5a5a5a5a5a5a5
#define ATTRS_MASK 0xffffffff

struct efi_variable {
uint64_t attrs;
efi_guid_t *guid;
unsigned char *name;
uint8_t *data;
size_t data_size;
};

/* The exported structure is:
* struct {
* uint32_t magic;
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
99 changes: 99 additions & 0 deletions src/include/efivar/efivar-types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// SPDX-License-Identifier: LGPL-2.1
/*
* Copyright 2012-2020 Red Hat, Inc.
* Copyright 2012-2020 Peter M. Jones <pjones@redhat.com>
*
* Author(s): Peter Jones <pjones@redhat.com>
*/
#ifndef EFI_TYPES_H
#define EFI_TYPES_H 1

#include <stdint.h>

typedef struct {
uint32_t a;
uint16_t b;
uint16_t c;
uint16_t d;
uint8_t e[6];
} efi_guid_t __attribute__((__aligned__(1)));

#if BYTE_ORDER == LITTLE_ENDIAN
#define EFI_GUID(a,b,c,d,e0,e1,e2,e3,e4,e5) \
((efi_guid_t) {(a), (b), (c), __builtin_bswap16(d), { (e0), (e1), (e2), (e3), (e4), (e5) }})
#else
#define EFI_GUID(a,b,c,d,e0,e1,e2,e3,e4,e5) \
((efi_guid_t) {(a), (b), (c), (d), { (e0), (e1), (e2), (e3), (e4), (e5) }})
#endif

#define EFI_GLOBAL_GUID EFI_GUID(0x8be4df61,0x93ca,0x11d2,0xaa0d,0x00,0xe0,0x98,0x03,0x2b,0x8c)

typedef struct {
uint8_t addr[4];
} efi_ipv4_addr_t;

typedef struct {
uint8_t addr[16];
} efi_ipv6_addr_t;

typedef union {
uint32_t addr[4];
efi_ipv4_addr_t v4;
efi_ipv6_addr_t v6;
} efi_ip_addr_t;

typedef struct {
uint8_t addr[32];
} efi_mac_addr_t;

typedef unsigned long efi_status_t;
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)
#define EFI_VARIABLE_HARDWARE_ERROR_RECORD ((uint64_t)0x0000000000000008)
#define EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS ((uint64_t)0x0000000000000010)
#define EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ((uint64_t)0x0000000000000020)
#define EFI_VARIABLE_APPEND_WRITE ((uint64_t)0x0000000000000040)
#define EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS ((uint64_t)0x0000000000000080)

#endif /* EFI_TYPES_H */
// vim:fenc=utf-8:tw=75:noet
46 changes: 3 additions & 43 deletions src/include/efivar/efivar.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,16 @@
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <byteswap.h>

typedef struct {
uint32_t a;
uint16_t b;
uint16_t c;
uint16_t d;
uint8_t e[6];
} efi_guid_t __attribute__((__aligned__(1)));

typedef struct {
uint8_t addr[4];
} efi_ipv4_addr_t;

typedef struct {
uint8_t addr[16];
} efi_ipv6_addr_t;

typedef union {
uint32_t addr[4];
efi_ipv4_addr_t v4;
efi_ipv6_addr_t v6;
} efi_ip_addr_t;

typedef struct {
uint8_t addr[32];
} efi_mac_addr_t;
#include <efivar/efivar-types.h>

#ifndef EFIVAR_BUILD_ENVIRONMENT
#include <efivar/efivar-guids.h>
#endif

#if BYTE_ORDER == LITTLE_ENDIAN
#define EFI_GUID(a,b,c,d,e0,e1,e2,e3,e4,e5) \
((efi_guid_t) {(a), (b), (c), __builtin_bswap16(d), { (e0), (e1), (e2), (e3), (e4), (e5) }})
#else
#define EFI_GUID(a,b,c,d,e0,e1,e2,e3,e4,e5) \
((efi_guid_t) {(a), (b), (c), (d), { (e0), (e1), (e2), (e3), (e4), (e5) }})
#endif

#define EFI_GLOBAL_GUID EFI_GUID(0x8be4df61,0x93ca,0x11d2,0xaa0d,0x00,0xe0,0x98,0x03,0x2b,0x8c)

#define EFI_VARIABLE_NON_VOLATILE 0x0000000000000001
#define EFI_VARIABLE_BOOTSERVICE_ACCESS 0x0000000000000002
#define EFI_VARIABLE_RUNTIME_ACCESS 0x0000000000000004
#define EFI_VARIABLE_HARDWARE_ERROR_RECORD 0x0000000000000008
#define EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS 0x0000000000000010
#define EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS 0x0000000000000020
#define EFI_VARIABLE_APPEND_WRITE 0x0000000000000040

#define EFI_VARIABLE_HAS_AUTH_HEADER 0x0000000100000000
#define EFI_VARIABLE_HAS_SIGNATURE 0x0000000200000000

Expand Down Expand Up @@ -242,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
8 changes: 8 additions & 0 deletions src/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

#define GUID_FORMAT "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x"

struct efi_variable {
uint64_t attrs;
efi_guid_t *guid;
unsigned char *name;
uint8_t *data;
size_t data_size;
};

struct efi_var_operations {
char name[NAME_MAX];
int (*probe)(void);
Expand Down
Loading