1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Kay Sievers
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.
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.
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/>.
28 #include "acpi-fpdt.h"
29 #include "alloc-util.h"
32 #include "time-util.h"
34 struct acpi_table_header
{
41 uint32_t oem_revision
;
42 char asl_compiler_id
[4];
43 uint32_t asl_compiler_revision
;
47 ACPI_FPDT_TYPE_BOOT
= 0,
48 ACPI_FPDT_TYPE_S3PERF
= 1,
51 struct acpi_fpdt_header
{
59 struct acpi_fpdt_boot_header
{
65 ACPI_FPDT_S3PERF_RESUME_REC
= 0,
66 ACPI_FPDT_S3PERF_SUSPEND_REC
= 1,
67 ACPI_FPDT_BOOT_REC
= 2,
70 struct acpi_fpdt_boot
{
77 uint64_t startup_start
;
78 uint64_t exit_services_entry
;
79 uint64_t exit_services_exit
;
82 int acpi_get_boot_usec(usec_t
*loader_start
, usec_t
*loader_exit
) {
83 _cleanup_free_
char *buf
= NULL
;
84 struct acpi_table_header
*tbl
;
86 struct acpi_fpdt_header
*rec
;
89 _cleanup_close_
int fd
= -1;
90 struct acpi_fpdt_boot_header hbrec
;
91 struct acpi_fpdt_boot brec
;
93 r
= read_full_file("/sys/firmware/acpi/tables/FPDT", &buf
, &l
);
97 if (l
< sizeof(struct acpi_table_header
) + sizeof(struct acpi_fpdt_header
))
100 tbl
= (struct acpi_table_header
*)buf
;
101 if (l
!= tbl
->length
)
104 if (memcmp(tbl
->signature
, "FPDT", 4) != 0)
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
)) {
111 if (rec
->length
<= 0)
113 if (rec
->type
!= ACPI_FPDT_TYPE_BOOT
)
115 if (rec
->length
!= sizeof(struct acpi_fpdt_header
))
125 /* read Firmware Basic Boot Performance Data Record */
126 fd
= open("/dev/mem", O_CLOEXEC
|O_RDONLY
);
130 l
= pread(fd
, &hbrec
, sizeof(struct acpi_fpdt_boot_header
), ptr
);
131 if (l
!= sizeof(struct acpi_fpdt_boot_header
))
134 if (memcmp(hbrec
.signature
, "FBPT", 4) != 0)
137 if (hbrec
.length
< sizeof(struct acpi_fpdt_boot_header
) + sizeof(struct acpi_fpdt_boot
))
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
))
144 if (brec
.length
!= sizeof(struct acpi_fpdt_boot
))
147 if (brec
.type
!= ACPI_FPDT_BOOT_REC
)
150 if (brec
.exit_services_exit
== 0)
151 /* Non-UEFI compatible boot. */
154 if (brec
.startup_start
== 0 || brec
.exit_services_exit
< brec
.startup_start
)
156 if (brec
.exit_services_exit
> NSEC_PER_HOUR
)
160 *loader_start
= brec
.startup_start
/ 1000;
162 *loader_exit
= brec
.exit_services_exit
/ 1000;