1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Kay Sievers
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.
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.
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/>.
28 #include <sys/types.h>
32 #include <time-util.h>
33 #include <acpi-fpdt.h>
35 struct acpi_table_header {
42 uint32_t oem_revision;
43 char asl_compiler_id[4];
44 uint32_t asl_compiler_revision;
48 ACPI_FPDT_TYPE_BOOT = 0,
49 ACPI_FPDT_TYPE_S3PERF = 1,
52 struct acpi_fpdt_header {
60 struct acpi_fpdt_boot_header {
66 ACPI_FPDT_S3PERF_RESUME_REC = 0,
67 ACPI_FPDT_S3PERF_SUSPEND_REC = 1,
68 ACPI_FPDT_BOOT_REC = 2,
71 struct acpi_fpdt_boot {
78 uint64_t startup_start;
79 uint64_t exit_services_entry;
80 uint64_t exit_services_exit;
83 int acpi_get_boot_usec(usec_t *loader_start, usec_t *loader_exit) {
84 _cleanup_free_ char *buf = NULL;
85 struct acpi_table_header *tbl;
87 struct acpi_fpdt_header *rec;
90 _cleanup_close_ int fd = -1;
91 struct acpi_fpdt_boot_header hbrec;
92 struct acpi_fpdt_boot brec;
94 r = read_full_file("/sys/firmware/acpi/tables/FPDT", &buf, &l);
98 if (l < sizeof(struct acpi_table_header) + sizeof(struct acpi_fpdt_header))
101 tbl = (struct acpi_table_header *)buf;
102 if (l != tbl->length)
105 if (memcmp(tbl->signature, "FPDT", 4) != 0)
108 /* find Firmware Basic Boot Performance Pointer Record */
109 for (rec = (struct acpi_fpdt_header *)(buf + sizeof(struct acpi_table_header));
110 (char *)rec < buf + l;
111 rec = (struct acpi_fpdt_header *)((char *)rec + rec->length)) {
112 if (rec->length <= 0)
114 if (rec->type != ACPI_FPDT_TYPE_BOOT)
116 if (rec->length != sizeof(struct acpi_fpdt_header))
126 /* read Firmware Basic Boot Performance Data Record */
127 fd = open("/dev/mem", O_CLOEXEC|O_RDONLY);
131 l = pread(fd, &hbrec, sizeof(struct acpi_fpdt_boot_header), ptr);
132 if (l != sizeof(struct acpi_fpdt_boot_header))
135 if (memcmp(hbrec.signature, "FBPT", 4) != 0)
138 if (hbrec.length < sizeof(struct acpi_fpdt_boot_header) + sizeof(struct acpi_fpdt_boot))
141 l = pread(fd, &brec, sizeof(struct acpi_fpdt_boot), ptr + sizeof(struct acpi_fpdt_boot_header));
142 if (l != sizeof(struct acpi_fpdt_boot))
145 if (brec.length != sizeof(struct acpi_fpdt_boot))
148 if (brec.type != ACPI_FPDT_BOOT_REC)
151 if (brec.startup_start == 0 || brec.exit_services_exit < brec.startup_start)
153 if (brec.exit_services_exit > NSEC_PER_HOUR)
157 *loader_start = brec.startup_start / 1000;
159 *loader_exit = brec.exit_services_exit / 1000;