Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include <sys/types.h>
4 : #include <unistd.h>
5 :
6 : #include "acpi-fpdt.h"
7 : #include "boot-timestamps.h"
8 : #include "efivars.h"
9 : #include "log.h"
10 : #include "tests.h"
11 : #include "util.h"
12 :
13 1 : static int test_acpi_fpdt(void) {
14 : char ts_start[FORMAT_TIMESPAN_MAX], ts_exit[FORMAT_TIMESPAN_MAX], ts_span[FORMAT_TIMESPAN_MAX];
15 : usec_t loader_start, loader_exit;
16 : int r;
17 :
18 1 : r = acpi_get_boot_usec(&loader_start, &loader_exit);
19 1 : if (r < 0) {
20 1 : bool ok = r == -ENOENT || (getuid() != 0 && r == -EACCES) || r == -ENODATA;
21 :
22 1 : log_full_errno(ok ? LOG_DEBUG : LOG_ERR, r, "Failed to read ACPI FPDT: %m");
23 1 : return ok ? 0 : r;
24 : }
25 :
26 0 : log_info("ACPI FPDT: loader start=%s exit=%s duration=%s",
27 : format_timespan(ts_start, sizeof(ts_start), loader_start, USEC_PER_MSEC),
28 : format_timespan(ts_exit, sizeof(ts_exit), loader_exit, USEC_PER_MSEC),
29 : format_timespan(ts_span, sizeof(ts_span), loader_exit - loader_start, USEC_PER_MSEC));
30 0 : return 1;
31 : }
32 :
33 1 : static int test_efi_loader(void) {
34 : char ts_start[FORMAT_TIMESPAN_MAX], ts_exit[FORMAT_TIMESPAN_MAX], ts_span[FORMAT_TIMESPAN_MAX];
35 : usec_t loader_start, loader_exit;
36 : int r;
37 :
38 1 : r = efi_loader_get_boot_usec(&loader_start, &loader_exit);
39 1 : if (r < 0) {
40 1 : bool ok = r == -ENOENT || (getuid() != 0 && r == -EACCES) || r == -EOPNOTSUPP;
41 :
42 1 : log_full_errno(ok ? LOG_DEBUG : LOG_ERR, r, "Failed to read EFI loader data: %m");
43 1 : return ok ? 0 : r;
44 : }
45 :
46 0 : log_info("EFI Loader: start=%s exit=%s duration=%s",
47 : format_timespan(ts_start, sizeof(ts_start), loader_start, USEC_PER_MSEC),
48 : format_timespan(ts_exit, sizeof(ts_exit), loader_exit, USEC_PER_MSEC),
49 : format_timespan(ts_span, sizeof(ts_span), loader_exit - loader_start, USEC_PER_MSEC));
50 0 : return 1;
51 : }
52 :
53 1 : static int test_boot_timestamps(void) {
54 1 : char s[MAX(FORMAT_TIMESPAN_MAX, FORMAT_TIMESTAMP_MAX)];
55 : dual_timestamp fw, l, k;
56 : int r;
57 :
58 1 : dual_timestamp_from_monotonic(&k, 0);
59 :
60 1 : r = boot_timestamps(NULL, &fw, &l);
61 1 : if (r < 0) {
62 1 : bool ok = r == -ENOENT || (getuid() != 0 && r == -EACCES) || r == -EOPNOTSUPP;
63 :
64 1 : log_full_errno(ok ? LOG_DEBUG : LOG_ERR, r, "Failed to read variables: %m");
65 1 : return ok ? 0 : r;
66 : }
67 :
68 0 : log_info("Firmware began %s before kernel.", format_timespan(s, sizeof(s), fw.monotonic, 0));
69 0 : log_info("Loader began %s before kernel.", format_timespan(s, sizeof(s), l.monotonic, 0));
70 0 : log_info("Firmware began %s.", format_timestamp(s, sizeof(s), fw.realtime));
71 0 : log_info("Loader began %s.", format_timestamp(s, sizeof(s), l.realtime));
72 0 : log_info("Kernel began %s.", format_timestamp(s, sizeof(s), k.realtime));
73 0 : return 1;
74 : }
75 :
76 1 : int main(int argc, char* argv[]) {
77 : int p, q, r;
78 :
79 1 : test_setup_logging(LOG_DEBUG);
80 :
81 1 : p = test_acpi_fpdt();
82 1 : assert(p >= 0);
83 1 : q = test_efi_loader();
84 1 : assert(q >= 0);
85 1 : r = test_boot_timestamps();
86 1 : assert(r >= 0);
87 :
88 1 : if (p == 0 && q == 0 && r == 0)
89 1 : return log_tests_skipped("access to firmware variables not possible");
90 :
91 0 : return EXIT_SUCCESS;
92 : }
|