]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
c51d84dc | 2 | |
3ffd4af2 | 3 | #include <fcntl.h> |
c51d84dc KS |
4 | #include <string.h> |
5 | #include <unistd.h> | |
c51d84dc | 6 | |
3ffd4af2 | 7 | #include "acpi-fpdt.h" |
cf0fbc49 | 8 | #include "alloc-util.h" |
3ffd4af2 LP |
9 | #include "fd-util.h" |
10 | #include "fileio.h" | |
11 | #include "time-util.h" | |
c51d84dc KS |
12 | |
13 | struct acpi_table_header { | |
14 | char signature[4]; | |
15 | uint32_t length; | |
16 | uint8_t revision; | |
17 | uint8_t checksum; | |
18 | char oem_id[6]; | |
19 | char oem_table_id[8]; | |
20 | uint32_t oem_revision; | |
21 | char asl_compiler_id[4]; | |
22 | uint32_t asl_compiler_revision; | |
49490c1d | 23 | } _packed_; |
c51d84dc KS |
24 | |
25 | enum { | |
26 | ACPI_FPDT_TYPE_BOOT = 0, | |
27 | ACPI_FPDT_TYPE_S3PERF = 1, | |
28 | }; | |
29 | ||
30 | struct acpi_fpdt_header { | |
31 | uint16_t type; | |
32 | uint8_t length; | |
33 | uint8_t revision; | |
34 | uint8_t reserved[4]; | |
35 | uint64_t ptr; | |
49490c1d | 36 | } _packed_; |
c51d84dc KS |
37 | |
38 | struct acpi_fpdt_boot_header { | |
39 | char signature[4]; | |
40 | uint32_t length; | |
49490c1d | 41 | } _packed_; |
c51d84dc KS |
42 | |
43 | enum { | |
44 | ACPI_FPDT_S3PERF_RESUME_REC = 0, | |
45 | ACPI_FPDT_S3PERF_SUSPEND_REC = 1, | |
46 | ACPI_FPDT_BOOT_REC = 2, | |
47 | }; | |
48 | ||
49 | struct acpi_fpdt_boot { | |
50 | uint16_t type; | |
51 | uint8_t length; | |
52 | uint8_t revision; | |
53 | uint8_t reserved[4]; | |
54 | uint64_t reset_end; | |
55 | uint64_t load_start; | |
56 | uint64_t startup_start; | |
57 | uint64_t exit_services_entry; | |
58 | uint64_t exit_services_exit; | |
49490c1d | 59 | } _packed; |
c51d84dc | 60 | |
bc09c2d8 JL |
61 | /* /dev/mem is deprecated on many systems, try using /sys/firmware/acpi/fpdt parsing instead. |
62 | * This code requires kernel version 5.12 on x86 based machines or 6.2 for arm64 */ | |
63 | static int acpi_get_boot_usec_kernel_parsed(usec_t *ret_loader_start, usec_t *ret_loader_exit) { | |
64 | usec_t start, end; | |
65 | int r; | |
66 | ||
67 | r = read_timestamp_file("/sys/firmware/acpi/fpdt/boot/exitbootservice_end_ns", &end); | |
68 | if (r < 0) | |
69 | return r; | |
70 | ||
71 | if (end == 0) | |
72 | /* Non-UEFI compatible boot. */ | |
73 | return -ENODATA; | |
74 | ||
75 | r = read_timestamp_file("/sys/firmware/acpi/fpdt/boot/bootloader_launch_ns", &start); | |
76 | if (r < 0) | |
77 | return r; | |
78 | ||
79 | if (start == 0 || end < start) | |
80 | return -EINVAL; | |
81 | if (end > NSEC_PER_HOUR) | |
82 | return -EINVAL; | |
83 | ||
84 | if (ret_loader_start) | |
85 | *ret_loader_start = start / 1000; | |
86 | if (ret_loader_exit) | |
87 | *ret_loader_exit = end / 1000; | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
ab10dfe0 | 92 | int acpi_get_boot_usec(usec_t *ret_loader_start, usec_t *ret_loader_exit) { |
2c64a8d0 | 93 | _cleanup_free_ char *buf = NULL; |
c51d84dc | 94 | struct acpi_table_header *tbl; |
8cee3283 LP |
95 | size_t l; |
96 | ssize_t ll; | |
c51d84dc KS |
97 | struct acpi_fpdt_header *rec; |
98 | int r; | |
99 | uint64_t ptr = 0; | |
254d1313 | 100 | _cleanup_close_ int fd = -EBADF; |
c51d84dc KS |
101 | struct acpi_fpdt_boot_header hbrec; |
102 | struct acpi_fpdt_boot brec; | |
103 | ||
bc09c2d8 JL |
104 | r = acpi_get_boot_usec_kernel_parsed(ret_loader_start, ret_loader_exit); |
105 | if (r != -ENOENT) /* fallback to /dev/mem hack only if kernel doesn't support the new sysfs files */ | |
106 | return r; | |
107 | ||
627055ce | 108 | r = read_full_virtual_file("/sys/firmware/acpi/tables/FPDT", &buf, &l); |
c51d84dc KS |
109 | if (r < 0) |
110 | return r; | |
111 | ||
112 | if (l < sizeof(struct acpi_table_header) + sizeof(struct acpi_fpdt_header)) | |
113 | return -EINVAL; | |
114 | ||
115 | tbl = (struct acpi_table_header *)buf; | |
116 | if (l != tbl->length) | |
117 | return -EINVAL; | |
118 | ||
119 | if (memcmp(tbl->signature, "FPDT", 4) != 0) | |
120 | return -EINVAL; | |
121 | ||
122 | /* find Firmware Basic Boot Performance Pointer Record */ | |
123 | for (rec = (struct acpi_fpdt_header *)(buf + sizeof(struct acpi_table_header)); | |
31edf440 | 124 | (char *)rec + offsetof(struct acpi_fpdt_header, revision) <= buf + l; |
c51d84dc | 125 | rec = (struct acpi_fpdt_header *)((char *)rec + rec->length)) { |
f576cd20 PH |
126 | if (rec->length <= 0) |
127 | break; | |
c51d84dc KS |
128 | if (rec->type != ACPI_FPDT_TYPE_BOOT) |
129 | continue; | |
130 | if (rec->length != sizeof(struct acpi_fpdt_header)) | |
131 | continue; | |
132 | ||
133 | ptr = rec->ptr; | |
134 | break; | |
135 | } | |
136 | ||
137 | if (ptr == 0) | |
67a47c60 | 138 | return -ENODATA; |
c51d84dc KS |
139 | |
140 | /* read Firmware Basic Boot Performance Data Record */ | |
141 | fd = open("/dev/mem", O_CLOEXEC|O_RDONLY); | |
142 | if (fd < 0) | |
143 | return -errno; | |
144 | ||
8cee3283 LP |
145 | ll = pread(fd, &hbrec, sizeof(struct acpi_fpdt_boot_header), ptr); |
146 | if (ll < 0) | |
147 | return -errno; | |
148 | if ((size_t) ll != sizeof(struct acpi_fpdt_boot_header)) | |
c51d84dc KS |
149 | return -EINVAL; |
150 | ||
151 | if (memcmp(hbrec.signature, "FBPT", 4) != 0) | |
152 | return -EINVAL; | |
153 | ||
154 | if (hbrec.length < sizeof(struct acpi_fpdt_boot_header) + sizeof(struct acpi_fpdt_boot)) | |
155 | return -EINVAL; | |
156 | ||
8cee3283 LP |
157 | ll = pread(fd, &brec, sizeof(struct acpi_fpdt_boot), ptr + sizeof(struct acpi_fpdt_boot_header)); |
158 | if (ll < 0) | |
159 | return -errno; | |
160 | if ((size_t) ll != sizeof(struct acpi_fpdt_boot)) | |
c51d84dc KS |
161 | return -EINVAL; |
162 | ||
163 | if (brec.length != sizeof(struct acpi_fpdt_boot)) | |
164 | return -EINVAL; | |
165 | ||
166 | if (brec.type != ACPI_FPDT_BOOT_REC) | |
167 | return -EINVAL; | |
168 | ||
67a47c60 ZJS |
169 | if (brec.exit_services_exit == 0) |
170 | /* Non-UEFI compatible boot. */ | |
171 | return -ENODATA; | |
172 | ||
6c798009 KS |
173 | if (brec.startup_start == 0 || brec.exit_services_exit < brec.startup_start) |
174 | return -EINVAL; | |
175 | if (brec.exit_services_exit > NSEC_PER_HOUR) | |
176 | return -EINVAL; | |
177 | ||
ab10dfe0 LP |
178 | if (ret_loader_start) |
179 | *ret_loader_start = brec.startup_start / 1000; | |
180 | if (ret_loader_exit) | |
181 | *ret_loader_exit = brec.exit_services_exit / 1000; | |
c51d84dc KS |
182 | |
183 | return 0; | |
184 | } |