]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/acpi-fpdt.c
tree-wide: make use of new relative time events in sd-event.h
[thirdparty/systemd.git] / src / shared / acpi-fpdt.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c51d84dc 2
a8fbdf54 3#include <errno.h>
3ffd4af2 4#include <fcntl.h>
a8fbdf54 5#include <stddef.h>
c51d84dc
KS
6#include <stdint.h>
7#include <string.h>
8#include <unistd.h>
c51d84dc 9
3ffd4af2 10#include "acpi-fpdt.h"
cf0fbc49 11#include "alloc-util.h"
3ffd4af2
LP
12#include "fd-util.h"
13#include "fileio.h"
14#include "time-util.h"
c51d84dc
KS
15
16struct acpi_table_header {
17 char signature[4];
18 uint32_t length;
19 uint8_t revision;
20 uint8_t checksum;
21 char oem_id[6];
22 char oem_table_id[8];
23 uint32_t oem_revision;
24 char asl_compiler_id[4];
25 uint32_t asl_compiler_revision;
49490c1d 26} _packed_;
c51d84dc
KS
27
28enum {
29 ACPI_FPDT_TYPE_BOOT = 0,
30 ACPI_FPDT_TYPE_S3PERF = 1,
31};
32
33struct acpi_fpdt_header {
34 uint16_t type;
35 uint8_t length;
36 uint8_t revision;
37 uint8_t reserved[4];
38 uint64_t ptr;
49490c1d 39} _packed_;
c51d84dc
KS
40
41struct acpi_fpdt_boot_header {
42 char signature[4];
43 uint32_t length;
49490c1d 44} _packed_;
c51d84dc
KS
45
46enum {
47 ACPI_FPDT_S3PERF_RESUME_REC = 0,
48 ACPI_FPDT_S3PERF_SUSPEND_REC = 1,
49 ACPI_FPDT_BOOT_REC = 2,
50};
51
52struct acpi_fpdt_boot {
53 uint16_t type;
54 uint8_t length;
55 uint8_t revision;
56 uint8_t reserved[4];
57 uint64_t reset_end;
58 uint64_t load_start;
59 uint64_t startup_start;
60 uint64_t exit_services_entry;
61 uint64_t exit_services_exit;
49490c1d 62} _packed;
c51d84dc
KS
63
64int acpi_get_boot_usec(usec_t *loader_start, usec_t *loader_exit) {
2c64a8d0 65 _cleanup_free_ char *buf = NULL;
c51d84dc 66 struct acpi_table_header *tbl;
39883f62 67 size_t l = 0;
c51d84dc
KS
68 struct acpi_fpdt_header *rec;
69 int r;
70 uint64_t ptr = 0;
71 _cleanup_close_ int fd = -1;
72 struct acpi_fpdt_boot_header hbrec;
73 struct acpi_fpdt_boot brec;
74
75 r = read_full_file("/sys/firmware/acpi/tables/FPDT", &buf, &l);
76 if (r < 0)
77 return r;
78
79 if (l < sizeof(struct acpi_table_header) + sizeof(struct acpi_fpdt_header))
80 return -EINVAL;
81
82 tbl = (struct acpi_table_header *)buf;
83 if (l != tbl->length)
84 return -EINVAL;
85
86 if (memcmp(tbl->signature, "FPDT", 4) != 0)
87 return -EINVAL;
88
89 /* find Firmware Basic Boot Performance Pointer Record */
90 for (rec = (struct acpi_fpdt_header *)(buf + sizeof(struct acpi_table_header));
91 (char *)rec < buf + l;
92 rec = (struct acpi_fpdt_header *)((char *)rec + rec->length)) {
f576cd20
PH
93 if (rec->length <= 0)
94 break;
c51d84dc
KS
95 if (rec->type != ACPI_FPDT_TYPE_BOOT)
96 continue;
97 if (rec->length != sizeof(struct acpi_fpdt_header))
98 continue;
99
100 ptr = rec->ptr;
101 break;
102 }
103
104 if (ptr == 0)
67a47c60 105 return -ENODATA;
c51d84dc
KS
106
107 /* read Firmware Basic Boot Performance Data Record */
108 fd = open("/dev/mem", O_CLOEXEC|O_RDONLY);
109 if (fd < 0)
110 return -errno;
111
112 l = pread(fd, &hbrec, sizeof(struct acpi_fpdt_boot_header), ptr);
113 if (l != sizeof(struct acpi_fpdt_boot_header))
114 return -EINVAL;
115
116 if (memcmp(hbrec.signature, "FBPT", 4) != 0)
117 return -EINVAL;
118
119 if (hbrec.length < sizeof(struct acpi_fpdt_boot_header) + sizeof(struct acpi_fpdt_boot))
120 return -EINVAL;
121
122 l = pread(fd, &brec, sizeof(struct acpi_fpdt_boot), ptr + sizeof(struct acpi_fpdt_boot_header));
123 if (l != sizeof(struct acpi_fpdt_boot))
124 return -EINVAL;
125
126 if (brec.length != sizeof(struct acpi_fpdt_boot))
127 return -EINVAL;
128
129 if (brec.type != ACPI_FPDT_BOOT_REC)
130 return -EINVAL;
131
67a47c60
ZJS
132 if (brec.exit_services_exit == 0)
133 /* Non-UEFI compatible boot. */
134 return -ENODATA;
135
6c798009
KS
136 if (brec.startup_start == 0 || brec.exit_services_exit < brec.startup_start)
137 return -EINVAL;
138 if (brec.exit_services_exit > NSEC_PER_HOUR)
139 return -EINVAL;
140
c51d84dc
KS
141 if (loader_start)
142 *loader_start = brec.startup_start / 1000;
143 if (loader_exit)
144 *loader_exit = brec.exit_services_exit / 1000;
145
146 return 0;
147}