limine icon indicating copy to clipboard operation
limine copied to clipboard

UEFI boot timestamp ignores the firmware timezone offset

Open pro-utkarshM opened this issue 2 months ago • 1 comments

On trunk 6f618ed039a749538220ee0b3dc0ab98ea6be773, the UEFI time() path converts calendar fields from GetTime() to an epoch timestamp without using EFI_TIME.TimeZone. This value is published through the Limine Date at Boot response.

For example, firmware reporting 1970-01-01 12:00:00 with TimeZone = 480 returns 43200; the correct UTC timestamp is 72000 (20:00 UTC). UEFI time services define Localtime = UTC - TimeZone, with the offset expressed in minutes.

The conversion needs to apply the signed offset before clamping timestamps below zero. Otherwise a local date before 1970 that maps to a positive UTC timestamp is lost. Preserve the current treatment of unspecified timezones, BIOS RTC readings, and firmware failures. Firmware already accounts for daylight adjustment in the reported time and offset.

Reproduction

The following host-only harness includes the actual common/lib/time.c, with a minimal mock for the firmware interface. It makes no firmware calls and needs no boot environment. Create /tmp/limine-time-check, save these two files there, and compile from the Limine source root.

efi.h (mock firmware interface)
#ifndef TEST_EFI_H
#define TEST_EFI_H
#include <stdint.h>
typedef uintptr_t EFI_STATUS;
typedef struct {
    uint16_t Year;
    uint8_t Month, Day, Hour, Minute, Second, Pad1;
    uint32_t Nanosecond;
    int16_t TimeZone;
    uint8_t Daylight, Pad2;
} EFI_TIME;
typedef struct {
    EFI_STATUS (*GetTime)(EFI_TIME *, void *);
} EFI_RUNTIME_SERVICES;
extern EFI_RUNTIME_SERVICES *gRT;
#define EFI_UNSPECIFIED_TIMEZONE 2047
#endif
check.c
#include <stdio.h>
#define LIB__MISC_H__
#define UEFI
#define time limine_test_time
#ifndef TIME_SOURCE
#define TIME_SOURCE "common/lib/time.c"
#endif
#include TIME_SOURCE
#undef time

static EFI_TIME firmware;
static EFI_STATUS error;
static EFI_STATUS mock_get_time(EFI_TIME *out, void *capabilities) {
    (void)capabilities;
    *out = firmware;
    return error;
}
static EFI_RUNTIME_SERVICES runtime = {.GetTime = mock_get_time};
EFI_RUNTIME_SERVICES *gRT = &runtime;

int main(void) {
    const struct {
        const char *name;
        EFI_TIME value;
        EFI_STATUS status;
        uint64_t expected;
    } cases[] = {
        {"UTC", { .Year=1970, .Month=1, .Day=1, .Hour=12 }, 0, 43200},
        {"UTC-08:00", { .Year=1970, .Month=1, .Day=1, .Hour=12, .TimeZone=480 }, 0, 72000},
        {"UTC+05:30", { .Year=1970, .Month=1, .Day=1, .Hour=12, .TimeZone=-330 }, 0, 23400},
        {"unspecified", { .Year=1970, .Month=1, .Day=1, .Hour=12, .TimeZone=2047 }, 0, 43200},
        {"cross into epoch", { .Year=1969, .Month=12, .Day=31, .Hour=23, .TimeZone=120 }, 0, 3600},
        {"cross before epoch", { .Year=1970, .Month=1, .Day=1, .Hour=1, .TimeZone=-120 }, 0, 0},
        {"firmware failure", { .Year=1970, .Month=1, .Day=1, .Hour=12 }, 1, 0},
        {"pre-epoch UTC", { .Year=1969, .Month=12, .Day=31, .Hour=23 }, 0, 0},
        {"maximum offset", { .Year=1970, .Month=1, .Day=1, .TimeZone=1440 }, 0, 86400},
        {"minimum offset", { .Year=1970, .Month=1, .Day=2, .TimeZone=-1440 }, 0, 0},
        {"daylight already applied", { .Year=1970, .Month=1, .Day=1, .Hour=12, .TimeZone=420, .Daylight=3 }, 0, 68400},
    };
    int failures = 0;
    for (unsigned i = 0; i < sizeof(cases)/sizeof(cases[0]); i++) {
        firmware = cases[i].value;
        error = cases[i].status;
        uint64_t got = limine_test_time();
        int failed = got != cases[i].expected;
        failures += failed;
        printf("%s: got=%llu expected=%llu %s\n", cases[i].name,
               (unsigned long long)got, (unsigned long long)cases[i].expected,
               failed ? "FAIL" : "PASS");
    }
    return failures != 0;
}
cc -std=gnu11 -Wall -Wextra -Werror \
  -I/tmp/limine-time-check -I. -Icommon \
  /tmp/limine-time-check/check.c -o /tmp/limine-time-check/check
/tmp/limine-time-check/check

The affected implementation fails seven of eleven cases: both signed offsets, both epoch crossings, the minimum and maximum offsets, and already-adjusted daylight time. UTC, unspecified timezone, firmware failure and pre-epoch UTC controls pass. With the fix, all eleven pass, including with AddressSanitizer and UndefinedBehaviorSanitizer (ASAN_OPTIONS=detect_leaks=0; LeakSanitizer is unavailable under this environment's tracing).

The full ./bootstrap && ./configure --enable-werror --enable-all && make all -j8 build also passes. This is host regression and cross-target compilation coverage; no physical firmware boot test is claimed.

The prepared fix is pushed; I will open the corresponding PR immediately.

pro-utkarshM avatar Sep 22 '26 03:09 pro-utkarshM

Fixed by #668

Mintsuki avatar Sep 26 '26 21:09 Mintsuki