]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
c51d84dc KS |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2013 Kay Sievers | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
a8fbdf54 | 21 | #include <errno.h> |
3ffd4af2 | 22 | #include <fcntl.h> |
a8fbdf54 | 23 | #include <stddef.h> |
c51d84dc KS |
24 | #include <stdint.h> |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
c51d84dc | 27 | |
3ffd4af2 | 28 | #include "acpi-fpdt.h" |
cf0fbc49 | 29 | #include "alloc-util.h" |
3ffd4af2 LP |
30 | #include "fd-util.h" |
31 | #include "fileio.h" | |
32 | #include "time-util.h" | |
c51d84dc KS |
33 | |
34 | struct acpi_table_header { | |
35 | char signature[4]; | |
36 | uint32_t length; | |
37 | uint8_t revision; | |
38 | uint8_t checksum; | |
39 | char oem_id[6]; | |
40 | char oem_table_id[8]; | |
41 | uint32_t oem_revision; | |
42 | char asl_compiler_id[4]; | |
43 | uint32_t asl_compiler_revision; | |
44 | }; | |
45 | ||
46 | enum { | |
47 | ACPI_FPDT_TYPE_BOOT = 0, | |
48 | ACPI_FPDT_TYPE_S3PERF = 1, | |
49 | }; | |
50 | ||
51 | struct acpi_fpdt_header { | |
52 | uint16_t type; | |
53 | uint8_t length; | |
54 | uint8_t revision; | |
55 | uint8_t reserved[4]; | |
56 | uint64_t ptr; | |
57 | }; | |
58 | ||
59 | struct acpi_fpdt_boot_header { | |
60 | char signature[4]; | |
61 | uint32_t length; | |
62 | }; | |
63 | ||
64 | enum { | |
65 | ACPI_FPDT_S3PERF_RESUME_REC = 0, | |
66 | ACPI_FPDT_S3PERF_SUSPEND_REC = 1, | |
67 | ACPI_FPDT_BOOT_REC = 2, | |
68 | }; | |
69 | ||
70 | struct acpi_fpdt_boot { | |
71 | uint16_t type; | |
72 | uint8_t length; | |
73 | uint8_t revision; | |
74 | uint8_t reserved[4]; | |
75 | uint64_t reset_end; | |
76 | uint64_t load_start; | |
77 | uint64_t startup_start; | |
78 | uint64_t exit_services_entry; | |
79 | uint64_t exit_services_exit; | |
80 | }; | |
81 | ||
82 | int acpi_get_boot_usec(usec_t *loader_start, usec_t *loader_exit) { | |
2c64a8d0 | 83 | _cleanup_free_ char *buf = NULL; |
c51d84dc | 84 | struct acpi_table_header *tbl; |
39883f62 | 85 | size_t l = 0; |
c51d84dc KS |
86 | struct acpi_fpdt_header *rec; |
87 | int r; | |
88 | uint64_t ptr = 0; | |
89 | _cleanup_close_ int fd = -1; | |
90 | struct acpi_fpdt_boot_header hbrec; | |
91 | struct acpi_fpdt_boot brec; | |
92 | ||
93 | r = read_full_file("/sys/firmware/acpi/tables/FPDT", &buf, &l); | |
94 | if (r < 0) | |
95 | return r; | |
96 | ||
97 | if (l < sizeof(struct acpi_table_header) + sizeof(struct acpi_fpdt_header)) | |
98 | return -EINVAL; | |
99 | ||
100 | tbl = (struct acpi_table_header *)buf; | |
101 | if (l != tbl->length) | |
102 | return -EINVAL; | |
103 | ||
104 | if (memcmp(tbl->signature, "FPDT", 4) != 0) | |
105 | return -EINVAL; | |
106 | ||
107 | /* find Firmware Basic Boot Performance Pointer Record */ | |
108 | for (rec = (struct acpi_fpdt_header *)(buf + sizeof(struct acpi_table_header)); | |
109 | (char *)rec < buf + l; | |
110 | rec = (struct acpi_fpdt_header *)((char *)rec + rec->length)) { | |
f576cd20 PH |
111 | if (rec->length <= 0) |
112 | break; | |
c51d84dc KS |
113 | if (rec->type != ACPI_FPDT_TYPE_BOOT) |
114 | continue; | |
115 | if (rec->length != sizeof(struct acpi_fpdt_header)) | |
116 | continue; | |
117 | ||
118 | ptr = rec->ptr; | |
119 | break; | |
120 | } | |
121 | ||
122 | if (ptr == 0) | |
67a47c60 | 123 | return -ENODATA; |
c51d84dc KS |
124 | |
125 | /* read Firmware Basic Boot Performance Data Record */ | |
126 | fd = open("/dev/mem", O_CLOEXEC|O_RDONLY); | |
127 | if (fd < 0) | |
128 | return -errno; | |
129 | ||
130 | l = pread(fd, &hbrec, sizeof(struct acpi_fpdt_boot_header), ptr); | |
131 | if (l != sizeof(struct acpi_fpdt_boot_header)) | |
132 | return -EINVAL; | |
133 | ||
134 | if (memcmp(hbrec.signature, "FBPT", 4) != 0) | |
135 | return -EINVAL; | |
136 | ||
137 | if (hbrec.length < sizeof(struct acpi_fpdt_boot_header) + sizeof(struct acpi_fpdt_boot)) | |
138 | return -EINVAL; | |
139 | ||
140 | l = pread(fd, &brec, sizeof(struct acpi_fpdt_boot), ptr + sizeof(struct acpi_fpdt_boot_header)); | |
141 | if (l != sizeof(struct acpi_fpdt_boot)) | |
142 | return -EINVAL; | |
143 | ||
144 | if (brec.length != sizeof(struct acpi_fpdt_boot)) | |
145 | return -EINVAL; | |
146 | ||
147 | if (brec.type != ACPI_FPDT_BOOT_REC) | |
148 | return -EINVAL; | |
149 | ||
67a47c60 ZJS |
150 | if (brec.exit_services_exit == 0) |
151 | /* Non-UEFI compatible boot. */ | |
152 | return -ENODATA; | |
153 | ||
6c798009 KS |
154 | if (brec.startup_start == 0 || brec.exit_services_exit < brec.startup_start) |
155 | return -EINVAL; | |
156 | if (brec.exit_services_exit > NSEC_PER_HOUR) | |
157 | return -EINVAL; | |
158 | ||
c51d84dc KS |
159 | if (loader_start) |
160 | *loader_start = brec.startup_start / 1000; | |
161 | if (loader_exit) | |
162 | *loader_exit = brec.exit_services_exit / 1000; | |
163 | ||
164 | return 0; | |
165 | } |