]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/acpi-fpdt.c
acpi-fpdt: break on zero or negative length read
[thirdparty/systemd.git] / src / shared / acpi-fpdt.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Kay Sievers
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29
30 #include <util.h>
31 #include <fileio.h>
32 #include <time-util.h>
33 #include <acpi-fpdt.h>
34
35 struct acpi_table_header {
36 char signature[4];
37 uint32_t length;
38 uint8_t revision;
39 uint8_t checksum;
40 char oem_id[6];
41 char oem_table_id[8];
42 uint32_t oem_revision;
43 char asl_compiler_id[4];
44 uint32_t asl_compiler_revision;
45 };
46
47 enum {
48 ACPI_FPDT_TYPE_BOOT = 0,
49 ACPI_FPDT_TYPE_S3PERF = 1,
50 };
51
52 struct acpi_fpdt_header {
53 uint16_t type;
54 uint8_t length;
55 uint8_t revision;
56 uint8_t reserved[4];
57 uint64_t ptr;
58 };
59
60 struct acpi_fpdt_boot_header {
61 char signature[4];
62 uint32_t length;
63 };
64
65 enum {
66 ACPI_FPDT_S3PERF_RESUME_REC = 0,
67 ACPI_FPDT_S3PERF_SUSPEND_REC = 1,
68 ACPI_FPDT_BOOT_REC = 2,
69 };
70
71 struct acpi_fpdt_boot {
72 uint16_t type;
73 uint8_t length;
74 uint8_t revision;
75 uint8_t reserved[4];
76 uint64_t reset_end;
77 uint64_t load_start;
78 uint64_t startup_start;
79 uint64_t exit_services_entry;
80 uint64_t exit_services_exit;
81 };
82
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;
86 size_t l;
87 struct acpi_fpdt_header *rec;
88 int r;
89 uint64_t ptr = 0;
90 _cleanup_close_ int fd = -1;
91 struct acpi_fpdt_boot_header hbrec;
92 struct acpi_fpdt_boot brec;
93
94 r = read_full_file("/sys/firmware/acpi/tables/FPDT", &buf, &l);
95 if (r < 0)
96 return r;
97
98 if (l < sizeof(struct acpi_table_header) + sizeof(struct acpi_fpdt_header))
99 return -EINVAL;
100
101 tbl = (struct acpi_table_header *)buf;
102 if (l != tbl->length)
103 return -EINVAL;
104
105 if (memcmp(tbl->signature, "FPDT", 4) != 0)
106 return -EINVAL;
107
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)
113 break;
114 if (rec->type != ACPI_FPDT_TYPE_BOOT)
115 continue;
116 if (rec->length != sizeof(struct acpi_fpdt_header))
117 continue;
118
119 ptr = rec->ptr;
120 break;
121 }
122
123 if (ptr == 0)
124 return -EINVAL;
125
126 /* read Firmware Basic Boot Performance Data Record */
127 fd = open("/dev/mem", O_CLOEXEC|O_RDONLY);
128 if (fd < 0)
129 return -errno;
130
131 l = pread(fd, &hbrec, sizeof(struct acpi_fpdt_boot_header), ptr);
132 if (l != sizeof(struct acpi_fpdt_boot_header))
133 return -EINVAL;
134
135 if (memcmp(hbrec.signature, "FBPT", 4) != 0)
136 return -EINVAL;
137
138 if (hbrec.length < sizeof(struct acpi_fpdt_boot_header) + sizeof(struct acpi_fpdt_boot))
139 return -EINVAL;
140
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))
143 return -EINVAL;
144
145 if (brec.length != sizeof(struct acpi_fpdt_boot))
146 return -EINVAL;
147
148 if (brec.type != ACPI_FPDT_BOOT_REC)
149 return -EINVAL;
150
151 if (brec.startup_start == 0 || brec.exit_services_exit < brec.startup_start)
152 return -EINVAL;
153 if (brec.exit_services_exit > NSEC_PER_HOUR)
154 return -EINVAL;
155
156 if (loader_start)
157 *loader_start = brec.startup_start / 1000;
158 if (loader_exit)
159 *loader_exit = brec.exit_services_exit / 1000;
160
161 return 0;
162 }