summaryrefslogtreecommitdiff
path: root/acpi/acpi.c
blob: 63066aaf31ae272e94c6aa73df1983b7c686e8f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
   Copyright (C) 2018 Free Software Foundation, Inc.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd.  If not, see <<a rel="nofollow" href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>>.
*/

#include <sys/mman.h>
#include <sys/io.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>

#include "acpi.h"

int
mmap_phys_acpi_header(uintptr_t base_addr, struct acpi_header **ptr_to_header,
                      void **virt_addr, int fd)
{
  /* The memory mapping must be done aligned to page size
   * but we have a known physical address we want to inspect,
   * therefore we must compute offsets.
   */
  uintptr_t pa_acpi = base_addr & ~(sysconf(_SC_PAGE_SIZE) - 1);
  uintptr_t pa_start = base_addr - pa_acpi;

  /* Map the ACPI table at the nearest page (rounded down) */
  *virt_addr = 0;
  *virt_addr = mmap(NULL, ESCD_SIZE, PROT_READ, MAP_SHARED | MAP_FIXED,
                    fd, (off_t) pa_acpi);

  if (*virt_addr == MAP_FAILED)
    return errno;

  /* Fabricate a pointer to our magic address */
  *ptr_to_header = (struct acpi_header *)(*virt_addr + pa_start);

  return 0;
}

int
acpi_get_num_tables(size_t *num_tables)
{
  int fd_mem;
  int err;
  void *virt_addr, *virt_addr2;
  bool found = false;
  struct rsdp_descr2 rsdp = { 0 };
  uintptr_t sdt_base = (uintptr_t)0;
  bool is_64bit = false;
  unsigned char *buf;
  struct acpi_header *root_sdt;
  struct acpi_header *next;

  if ((fd_mem = open("/dev/mem", O_RDWR)) < 0)
    return EPERM;

  virt_addr = mmap(NULL, ESCD_SIZE, PROT_READ,
                   MAP_SHARED | MAP_FIXED, fd_mem, ESCD);
  if (virt_addr == MAP_FAILED)
    return errno;

  buf = (unsigned char *)virt_addr;
  found = false;

  /* RSDP magic string is 16 byte aligned */
  for (int i = 0; i < ESCD_SIZE; i += 16)
    {
      if (!memcmp(&buf[i], RSDP_MAGIC, 8)) {
        rsdp = *((struct rsdp_descr2 *)(&buf[i]));
        found = true;
        break;
      }
    }

  if (!found) {
    munmap(virt_addr, ESCD_SIZE);
    return ENODEV;
  }

  if (rsdp.v1.revision == 0) {
    // ACPI 1.0
    sdt_base = rsdp.v1.rsdt_addr;
    is_64bit = false;
  } else if (rsdp.v1.revision == 2) {
    // ACPI >= 2.0
    sdt_base = rsdp.xsdt_addr;
    is_64bit = true;
  } else {
    munmap(virt_addr, ESCD_SIZE);
    return ENODEV;
  }

  munmap(virt_addr, ESCD_SIZE);

  /* Now we have the sdt_base address and knowledge of 32/64 bit ACPI */

  err = mmap_phys_acpi_header(sdt_base, &root_sdt, &virt_addr, fd_mem);
  if (err) {
    munmap(virt_addr, ESCD_SIZE);
    return err;
  }

  /* Get total tables */
  uint32_t ntables;
  uint8_t sz_ptr;
  sz_ptr = is_64bit ? 8 : 4;
  ntables = (root_sdt->length - sizeof(*root_sdt)) / sz_ptr;

  /* Get pointer to first ACPI table */
  uintptr_t acpi_ptr = (uintptr_t)root_sdt + sizeof(*root_sdt);

  /* Get number of readable tables */
  *num_tables = 0;
  for (int i = 0; i < ntables; i++)
    {
      uintptr_t acpi_ptr32 = (uintptr_t)*((uint32_t *)(acpi_ptr + i*sz_ptr));
      uintptr_t acpi_ptr64 = (uintptr_t)*((uint64_t *)(acpi_ptr + i*sz_ptr));
      if (is_64bit) {
        err = mmap_phys_acpi_header(acpi_ptr64, &next, &virt_addr2, fd_mem);
      } else {
        err = mmap_phys_acpi_header(acpi_ptr32, &next, &virt_addr2, fd_mem);
      }

      char name[5] = { 0 };
      snprintf(name, 5, "%s", &next->signature[0]);
      if (next->signature[0] == '\0' || next->length == 0) {
        munmap(virt_addr2, ESCD_SIZE);
        continue;
      }
      *num_tables += 1;
      munmap(virt_addr2, ESCD_SIZE);
    }

  munmap(virt_addr, ESCD_SIZE);

  return 0;
}

int
acpi_get_tables(struct acpi_table **tables)
{
  int err;
  int fd_mem;
  void *virt_addr, *virt_addr2;
  uint32_t phys_addr = ESCD;
  bool found = false;
  struct rsdp_descr2 rsdp = { 0 };
  uintptr_t sdt_base = (uintptr_t)0;
  bool is_64bit = false;
  unsigned char *buf;
  struct acpi_header *root_sdt;
  struct acpi_header *next;
  size_t ntables_actual;
  int cur_tab = 0;

  err = acpi_get_num_tables(&ntables_actual);
  if (err)
    return err;

  *tables = malloc(ntables_actual * sizeof(**tables));
  if (!*tables)
    return ENOMEM;

  if ((fd_mem = open("/dev/mem", O_RDWR)) < 0)
    return EPERM;

  virt_addr = mmap(NULL, ESCD_SIZE, PROT_READ, MAP_SHARED | MAP_FIXED,
                   fd_mem, (off_t) phys_addr);

  if (virt_addr == MAP_FAILED)
    return errno;

  buf = (unsigned char *)virt_addr;
  found = false;

  /* RSDP magic string is 16 byte aligned */
  for (int i = 0; i < ESCD_SIZE; i += 16)
    {
      if (!memcmp(&buf[i], RSDP_MAGIC, 8)) {
        rsdp = *((struct rsdp_descr2 *)(&buf[i]));
        found = true;
        break;
      }
    }

  if (!found) {
    munmap(virt_addr, ESCD_SIZE);
    return ENODEV;
  }

  if (rsdp.v1.revision == 0) {
    // ACPI 1.0
    sdt_base = rsdp.v1.rsdt_addr;
    is_64bit = false;
  } else if (rsdp.v1.revision == 2) {
    // ACPI >= 2.0
    sdt_base = rsdp.xsdt_addr;
    is_64bit = true;
  } else {
    munmap(virt_addr, ESCD_SIZE);
    return ENODEV;
  }

  munmap(virt_addr, ESCD_SIZE);

  /* Now we have the sdt_base address and knowledge of 32/64 bit ACPI */

  err = mmap_phys_acpi_header(sdt_base, &root_sdt, &virt_addr, fd_mem);
  if (err) {
    munmap(virt_addr, ESCD_SIZE);
    return err;
  }

  /* Get total tables */
  uint32_t ntables;
  uint8_t sz_ptr;
  sz_ptr = is_64bit ? 8 : 4;
  ntables = (root_sdt->length - sizeof(*root_sdt)) / sz_ptr;

  /* Get pointer to first ACPI table */
  uintptr_t acpi_ptr = (uintptr_t)root_sdt + sizeof(*root_sdt);

  /* Get all tables and data */
  for (int i = 0; i < ntables; i++)
    {
      uintptr_t acpi_ptr32 = (uintptr_t)*((uint32_t *)(acpi_ptr + i*sz_ptr));
      uintptr_t acpi_ptr64 = (uintptr_t)*((uint64_t *)(acpi_ptr + i*sz_ptr));
      if (is_64bit) {
        err = mmap_phys_acpi_header(acpi_ptr64, &next, &virt_addr2, fd_mem);
        if (err) {
          munmap(virt_addr, ESCD_SIZE);
          return err;
        }
      } else {
        err = mmap_phys_acpi_header(acpi_ptr32, &next, &virt_addr2, fd_mem);
        if (err) {
          munmap(virt_addr, ESCD_SIZE);
          return err;
        }
      }

      char name[5] = { 0 };
      snprintf(name, 5, "%s", &next->signature[0]);
      if (next->signature[0] == '\0' || next->length == 0) {
        munmap(virt_addr2, ESCD_SIZE);
        continue;
      }
      uint32_t datalen = next->length - sizeof(*next);
      void *data = (void *)((uintptr_t)next + sizeof(*next));

      /* We now have a pointer to the data,
       * its length and header.
       */
      struct acpi_table *t = *tables + cur_tab;
      memcpy(&t->h, next, sizeof(*next));
      t->datalen = 0;
      t->data = malloc(datalen);
      if (!t->data) {
        munmap(virt_addr2, ESCD_SIZE);
        munmap(virt_addr, ESCD_SIZE);
        return ENOMEM;
      }
      t->datalen = datalen;
      memcpy(t->data, data, datalen);
      cur_tab++;
      munmap(virt_addr2, ESCD_SIZE);
    }

  munmap(virt_addr, ESCD_SIZE);

  return 0;
}