chiark / gitweb /
core: if the bootloader supports it, determine firmware and boot loader delay
[elogind.git] / src / shared / efivars.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <unistd.h>
23 #include <fcntl.h>
24
25 #include "util.h"
26 #include "utf8.h"
27 #include "efivars.h"
28
29 #define EFI_VENDOR_LOADER SD_ID128_MAKE(4a,67,b0,82,0a,4c,41,cf,b6,c7,44,0b,29,bb,8c,4f)
30
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;
34         uint32_t a;
35         ssize_t n;
36         struct stat st;
37         void *r;
38
39         assert(name);
40         assert(value);
41         assert(size);
42
43         if (asprintf(&p,
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)
46                 return -ENOMEM;
47
48         fd = open(p, O_RDONLY|O_NOCTTY|O_CLOEXEC);
49         if (fd < 0)
50                 return -errno;
51
52         if (fstat(fd, &st) < 0)
53                 return -errno;
54         if (st.st_size < 4)
55                 return -EIO;
56         if (st.st_size > 4*1024*1024 + 4)
57                 return -E2BIG;
58
59         n = read(fd, &a, sizeof(a));
60         if (n < 0)
61                 return (int) n;
62         if (n != sizeof(a))
63                 return -EIO;
64
65         r = malloc(st.st_size - 4 + 2);
66         if (!r)
67                 return -ENOMEM;
68
69         n = read(fd, r, (size_t) st.st_size - 4);
70         if (n < 0) {
71                 free(r);
72                 return (int) -n;
73         }
74         if (n != (ssize_t) st.st_size - 4) {
75                 free(r);
76                 return -EIO;
77         }
78
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;
82
83         *value = r;
84         *size = (size_t) st.st_size;
85
86         if (attribute)
87                 *attribute = a;
88
89         return 0;
90 }
91
92 static int read_bogomips(unsigned long *u) {
93         _cleanup_fclose_ FILE *f = NULL;
94
95         f = fopen("/proc/cpuinfo", "re");
96         if (!f)
97                 return -errno;
98
99         while (!feof(f)) {
100                 char line[LINE_MAX];
101                 char *x;
102                 unsigned long a, b;
103
104                 if (!fgets(line, sizeof(line), f))
105                         return -EIO;
106
107                 char_array_0(line);
108                 truncate_nl(line);
109
110                 if (!startswith(line, "bogomips"))
111                         continue;
112
113                 x = line + 8;
114                 x += strspn(x, WHITESPACE);
115                 if (*x != ':')
116                         continue;
117                 x++;
118                 x += strspn(x, WHITESPACE);
119
120                 if (sscanf(x, "%lu.%lu", &a, &b) != 2)
121                         continue;
122
123                 *u = a * 1000000L + b * 10000L;
124                 return 0;
125         }
126
127         return -EIO;
128 }
129
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;
133         size_t is;
134         int r;
135         uint64_t x;
136
137         assert(name);
138         assert(u);
139
140         r = efi_get_variable(EFI_VENDOR_LOADER, name, NULL, &i, &is);
141         if (r < 0)
142                 return r;
143
144         j = utf16_to_utf8(i, is);
145         if (!j)
146                 return -ENOMEM;
147
148         r = safe_atou64(j, &x);
149         if (r < 0)
150                 return r;
151
152         *u = USEC_PER_SEC * x / speed;
153         return 0;
154 }
155
156 static int get_boot_usec(usec_t *firmware, usec_t *loader) {
157         uint64_t x, y;
158         int r;
159         unsigned long bogomips;
160
161         assert(firmware);
162         assert(loader);
163
164         /* Returns the usec after the CPU was turned on. The two
165          * timestamps are: the firmware finished, and the boot loader
166          * finished. */
167
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. */
174
175         r = read_bogomips(&bogomips);
176         if (r < 0)
177                 return r;
178
179         r = read_ticks(EFI_VENDOR_LOADER, "LoaderTicksInit", bogomips / 2, &x);
180         if (r < 0)
181                 return r;
182
183         r = read_ticks(EFI_VENDOR_LOADER, "LoaderTicksExec", bogomips / 2, &y);
184         if (r < 0)
185                 return r;
186
187         if (y == 0 || y < x)
188                 return -EIO;
189
190         if (y > USEC_PER_HOUR)
191                 return -EIO;
192
193         *firmware = x;
194         *loader = y;
195
196         return 0;
197 }
198
199 int efi_get_boot_timestamps(const dual_timestamp *n, dual_timestamp *firmware, dual_timestamp *loader) {
200         usec_t x, y, a;
201         int r;
202         dual_timestamp _n;
203
204         assert(firmware);
205         assert(loader);
206
207         if (!n) {
208                 dual_timestamp_get(&_n);
209                 n = &_n;
210         }
211
212         r = get_boot_usec(&x, &y);
213         if (r < 0)
214                 return r;
215
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
221          * value. */
222
223         firmware->monotonic = y;
224         loader->monotonic = y - x;
225
226         a = n->monotonic + firmware->monotonic;
227         firmware->realtime = n->realtime > a ? n->realtime - a : 0;
228
229         a = n->monotonic + loader->monotonic;
230         loader->realtime = n->realtime > a ? n->realtime - a : 0;
231
232         return 0;
233 }