1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #define EFI_VENDOR_LOADER SD_ID128_MAKE(4a,67,b0,82,0a,4c,41,cf,b6,c7,44,0b,29,bb,8c,4f)
31 bool is_efiboot(void) {
32 return access("/sys/firmware/efi", F_OK) >= 0;
35 int efi_get_variable(sd_id128_t vendor, const char *name, uint32_t *attribute, void **value, size_t *size) {
36 _cleanup_close_ int fd = -1;
37 _cleanup_free_ char *p = NULL;
48 "/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
49 name, SD_ID128_FORMAT_VAL(vendor)) < 0)
52 fd = open(p, O_RDONLY|O_NOCTTY|O_CLOEXEC);
56 if (fstat(fd, &st) < 0)
60 if (st.st_size > 4*1024*1024 + 4)
63 n = read(fd, &a, sizeof(a));
69 r = malloc(st.st_size - 4 + 2);
73 n = read(fd, r, (size_t) st.st_size - 4);
78 if (n != (ssize_t) st.st_size - 4) {
83 /* Always NUL terminate (2 bytes, to protect UTF-16) */
84 ((char*) r)[st.st_size - 4] = 0;
85 ((char*) r)[st.st_size - 4 + 1] = 0;
88 *size = (size_t) st.st_size;
96 static int read_usec(sd_id128_t vendor, const char *name, usec_t *u) {
97 _cleanup_free_ void *i = NULL;
98 _cleanup_free_ char *j = NULL;
106 r = efi_get_variable(EFI_VENDOR_LOADER, name, NULL, &i, &is);
110 j = utf16_to_utf8(i, is);
114 r = safe_atou64(j, &x);
122 static int get_boot_usec(usec_t *firmware, usec_t *loader) {
129 r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeInitUSec", &x);
133 r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeExecUSec", &y);
140 if (y > USEC_PER_HOUR)
149 int efi_get_boot_timestamps(const dual_timestamp *n, dual_timestamp *firmware, dual_timestamp *loader) {
158 dual_timestamp_get(&_n);
162 r = get_boot_usec(&x, &y);
166 /* Let's convert this to timestamps where the firmware
167 * began/loader began working. To make this more confusing:
168 * since usec_t is unsigned and the kernel's monotonic clock
169 * begins at kernel initialization we'll actually initialize
170 * the monotonic timestamps here as negative of the actual
173 firmware->monotonic = y;
174 loader->monotonic = y - x;
176 a = n->monotonic + firmware->monotonic;
177 firmware->realtime = n->realtime > a ? n->realtime - a : 0;
179 a = n->monotonic + loader->monotonic;
180 loader->realtime = n->realtime > a ? n->realtime - a : 0;
185 int efi_get_loader_device_part_uuid(sd_id128_t *u) {
186 _cleanup_free_ void *s = NULL;
187 _cleanup_free_ char *p = NULL;
194 r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderDevicePartUUID", NULL, &s, &ss);
198 p = utf16_to_utf8(s, ss);
202 if (sscanf(p, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
203 &parsed[0], &parsed[1], &parsed[2], &parsed[3],
204 &parsed[4], &parsed[5], &parsed[6], &parsed[7],
205 &parsed[8], &parsed[9], &parsed[10], &parsed[11],
206 &parsed[12], &parsed[13], &parsed[14], &parsed[15]) != 16)
209 for (i = 0; i < ELEMENTSOF(parsed); i++)
210 u->bytes[i] = parsed[i];