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 int efi_get_variable(sd_id128_t vendor, const char *name, uint32_t *attribute, void **value, size_t *size) {
32 _cleanup_close_ int fd = -1;
33 _cleanup_free_ char *p = NULL;
44 "/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
45 name, SD_ID128_FORMAT_VAL(vendor)) < 0)
48 fd = open(p, O_RDONLY|O_NOCTTY|O_CLOEXEC);
52 if (fstat(fd, &st) < 0)
56 if (st.st_size > 4*1024*1024 + 4)
59 n = read(fd, &a, sizeof(a));
65 r = malloc(st.st_size - 4 + 2);
69 n = read(fd, r, (size_t) st.st_size - 4);
74 if (n != (ssize_t) st.st_size - 4) {
79 /* Always NUL terminate (2 bytes, to protect UTF-16) */
80 ((char*) r)[st.st_size - 4] = 0;
81 ((char*) r)[st.st_size - 4 + 1] = 0;
84 *size = (size_t) st.st_size;
92 static int read_bogomips(unsigned long *u) {
93 _cleanup_fclose_ FILE *f = NULL;
95 f = fopen("/proc/cpuinfo", "re");
104 if (!fgets(line, sizeof(line), f))
110 if (!startswith(line, "bogomips"))
114 x += strspn(x, WHITESPACE);
118 x += strspn(x, WHITESPACE);
120 if (sscanf(x, "%lu.%lu", &a, &b) != 2)
123 *u = a * 1000000L + b * 10000L;
130 static int read_ticks(sd_id128_t vendor, const char *name, unsigned long speed, usec_t *u) {
131 _cleanup_free_ void *i = NULL;
132 _cleanup_free_ char *j = NULL;
140 r = efi_get_variable(EFI_VENDOR_LOADER, name, NULL, &i, &is);
144 j = utf16_to_utf8(i, is);
148 r = safe_atou64(j, &x);
152 *u = USEC_PER_SEC * x / speed;
156 static int get_boot_usec(usec_t *firmware, usec_t *loader) {
159 unsigned long bogomips;
164 /* Returns the usec after the CPU was turned on. The two
165 * timestamps are: the firmware finished, and the boot loader
168 /* We assume that the kernel's bogomips value is calibrated to
169 * twice the CPU frequency, and use this to convert the TSC
170 * ticks into usec. Of course, bogomips are only vaguely
171 * defined. If this breaks one day we can come up with
172 * something better. However, for now this saves us from doing
173 * a local calibration loop. */
175 r = read_bogomips(&bogomips);
179 r = read_ticks(EFI_VENDOR_LOADER, "LoaderTicksInit", bogomips / 2, &x);
183 r = read_ticks(EFI_VENDOR_LOADER, "LoaderTicksExec", bogomips / 2, &y);
190 if (y > USEC_PER_HOUR)
199 int efi_get_boot_timestamps(const dual_timestamp *n, dual_timestamp *firmware, dual_timestamp *loader) {
208 dual_timestamp_get(&_n);
212 r = get_boot_usec(&x, &y);
216 /* Let's convert this to timestamps where the firmware
217 * began/loader began working. To make this more confusing:
218 * since usec_t is unsigned and the kernel's monotonic clock
219 * begins at kernel initialization we'll actually initialize
220 * the monotonic timestamps here as negative of the actual
223 firmware->monotonic = y;
224 loader->monotonic = y - x;
226 a = n->monotonic + firmware->monotonic;
227 firmware->realtime = n->realtime > a ? n->realtime - a : 0;
229 a = n->monotonic + loader->monotonic;
230 loader->realtime = n->realtime > a ? n->realtime - a : 0;