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