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_bogomips(unsigned long *u) {
97 _cleanup_fclose_ FILE *f = NULL;
99 f = fopen("/proc/cpuinfo", "re");
108 if (!fgets(line, sizeof(line), f))
114 if (!startswith(line, "bogomips"))
118 x += strspn(x, WHITESPACE);
122 x += strspn(x, WHITESPACE);
124 if (sscanf(x, "%lu.%lu", &a, &b) != 2)
127 *u = a * 1000000L + b * 10000L;
134 static int read_ticks(sd_id128_t vendor, const char *name, unsigned long speed, usec_t *u) {
135 _cleanup_free_ void *i = NULL;
136 _cleanup_free_ char *j = NULL;
144 r = efi_get_variable(EFI_VENDOR_LOADER, name, NULL, &i, &is);
148 j = utf16_to_utf8(i, is);
152 r = safe_atou64(j, &x);
156 *u = USEC_PER_SEC * x / speed;
160 static int get_boot_usec(usec_t *firmware, usec_t *loader) {
163 unsigned long bogomips;
168 /* Returns the usec after the CPU was turned on. The two
169 * timestamps are: the firmware finished, and the boot loader
172 /* We assume that the kernel's bogomips value is calibrated to
173 * twice the CPU frequency, and use this to convert the TSC
174 * ticks into usec. Of course, bogomips are only vaguely
175 * defined. If this breaks one day we can come up with
176 * something better. However, for now this saves us from doing
177 * a local calibration loop. */
179 r = read_bogomips(&bogomips);
183 r = read_ticks(EFI_VENDOR_LOADER, "LoaderTicksInit", bogomips / 2, &x);
187 r = read_ticks(EFI_VENDOR_LOADER, "LoaderTicksExec", bogomips / 2, &y);
194 if (y > USEC_PER_HOUR)
203 int efi_get_boot_timestamps(const dual_timestamp *n, dual_timestamp *firmware, dual_timestamp *loader) {
212 dual_timestamp_get(&_n);
216 r = get_boot_usec(&x, &y);
220 /* Let's convert this to timestamps where the firmware
221 * began/loader began working. To make this more confusing:
222 * since usec_t is unsigned and the kernel's monotonic clock
223 * begins at kernel initialization we'll actually initialize
224 * the monotonic timestamps here as negative of the actual
227 firmware->monotonic = y;
228 loader->monotonic = y - x;
230 a = n->monotonic + firmware->monotonic;
231 firmware->realtime = n->realtime > a ? n->realtime - a : 0;
233 a = n->monotonic + loader->monotonic;
234 loader->realtime = n->realtime > a ? n->realtime - a : 0;