2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2010 Free Software Foundation, Inc.
5
* GRUB is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* GRUB is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
26
#define grub_dprintf(cond, args...) printf ( args )
27
#define grub_printf printf
28
typedef uint64_t grub_uint64_t;
29
typedef uint32_t grub_uint32_t;
30
typedef uint16_t grub_uint16_t;
31
typedef uint8_t grub_uint8_t;
35
#include <grub/acpi.h>
37
#ifndef GRUB_DSDT_TEST
38
#include <grub/misc.h>
39
#include <grub/cpu/io.h>
42
static inline grub_uint32_t
43
decode_length (const grub_uint8_t *ptr, int *numlen)
53
num_bytes = *ptr >> 6;
55
*numlen = num_bytes + 1;
58
for (i = 0; i < num_bytes; i++)
60
ret |= *ptr << (8 * i + 4);
66
static inline grub_uint32_t
67
skip_name_string (const grub_uint8_t *ptr, const grub_uint8_t *end)
69
const grub_uint8_t *ptr0 = ptr;
71
while (ptr < end && (*ptr == '^' || *ptr == '\\'))
81
ptr += 1 + (*ptr) * 4;
93
static inline grub_uint32_t
94
skip_data_ref_object (const grub_uint8_t *ptr, const grub_uint8_t *end)
96
grub_dprintf ("acpi", "data type = 0x%x\n", *ptr);
99
case GRUB_ACPI_OPCODE_PACKAGE:
100
return 1 + decode_length (ptr + 1, 0);
101
case GRUB_ACPI_OPCODE_ZERO:
102
case GRUB_ACPI_OPCODE_ONES:
103
case GRUB_ACPI_OPCODE_ONE:
105
case GRUB_ACPI_OPCODE_BYTE_CONST:
107
case GRUB_ACPI_OPCODE_WORD_CONST:
109
case GRUB_ACPI_OPCODE_DWORD_CONST:
112
if (*ptr == '^' || *ptr == '\\' || *ptr == '_'
113
|| (*ptr >= 'A' && *ptr <= 'Z'))
114
return skip_name_string (ptr, end);
115
grub_printf ("Unknown opcode 0x%x\n", *ptr);
120
static inline grub_uint32_t
121
skip_ext_op (const grub_uint8_t *ptr, const grub_uint8_t *end)
123
const grub_uint8_t *ptr0 = ptr;
125
grub_dprintf ("acpi", "Extended opcode: 0x%x\n", *ptr);
128
case GRUB_ACPI_EXTOPCODE_MUTEX:
130
ptr += skip_name_string (ptr, end);
133
case GRUB_ACPI_EXTOPCODE_OPERATION_REGION:
135
ptr += skip_name_string (ptr, end);
137
ptr += add = skip_data_ref_object (ptr, end);
140
ptr += add = skip_data_ref_object (ptr, end);
144
case GRUB_ACPI_EXTOPCODE_FIELD_OP:
145
case GRUB_ACPI_EXTOPCODE_INDEX_FIELD_OP:
147
ptr += decode_length (ptr, 0);
150
grub_printf ("Unexpected extended opcode: 0x%x\n", *ptr);
157
get_sleep_type (grub_uint8_t *table, grub_uint8_t *end)
159
grub_uint8_t *ptr, *prev = table;
162
ptr = table + sizeof (struct grub_acpi_table_header);
163
while (ptr < end && prev < ptr)
167
grub_dprintf ("acpi", "Opcode 0x%x\n", *ptr);
168
grub_dprintf ("acpi", "Tell %x\n", (unsigned) (ptr - table));
171
case GRUB_ACPI_OPCODE_EXTOP:
173
ptr += add = skip_ext_op (ptr, end);
177
case GRUB_ACPI_OPCODE_NAME:
179
if (memcmp (ptr, "_S5_", 4) == 0 || memcmp (ptr, "\\_S5_", 4) == 0)
182
grub_uint8_t *ptr2 = ptr;
183
grub_dprintf ("acpi", "S5 found\n");
184
ptr2 += skip_name_string (ptr, end);
187
grub_printf ("Unknown opcode in _S5: 0x%x\n", *ptr2);
191
decode_length (ptr2, &ll);
196
case GRUB_ACPI_OPCODE_ZERO:
199
case GRUB_ACPI_OPCODE_ONE:
202
case GRUB_ACPI_OPCODE_BYTE_CONST:
203
sleep_type = ptr2[1];
206
grub_printf ("Unknown data type in _S5: 0x%x\n", *ptr2);
210
ptr += add = skip_name_string (ptr, end);
213
ptr += add = skip_data_ref_object (ptr, end);
217
case GRUB_ACPI_OPCODE_SCOPE:
218
case GRUB_ACPI_OPCODE_IF:
219
case GRUB_ACPI_OPCODE_METHOD:
222
ptr += decode_length (ptr, 0);
228
grub_dprintf ("acpi", "TYP = %d\n", sleep_type);
232
#ifdef GRUB_DSDT_TEST
234
main (int argc, char **argv)
240
printf ("Usage: %s FILE\n", argv[0]);
241
f = fopen (argv[1], "rb");
244
printf ("Couldn't open file\n");
247
fseek (f, 0, SEEK_END);
249
fseek (f, 0, SEEK_SET);
253
printf ("Couldn't malloc buffer\n");
257
if (fread (buf, 1, len, f) != len)
259
printf ("Read failed\n");
265
printf ("Sleep type = %d\n", get_sleep_type (buf, buf + len));
274
grub_acpi_halt (void)
276
struct grub_acpi_rsdp_v20 *rsdp2;
277
struct grub_acpi_rsdp_v10 *rsdp1;
278
struct grub_acpi_table_header *rsdt;
279
grub_uint32_t *entry_ptr;
281
rsdp2 = grub_acpi_get_rsdpv2 ();
283
rsdp1 = &(rsdp2->rsdpv1);
285
rsdp1 = grub_acpi_get_rsdpv1 ();
286
grub_dprintf ("acpi", "rsdp1=%p\n", rsdp1);
290
rsdt = (struct grub_acpi_table_header *) (grub_addr_t) rsdp1->rsdt_addr;
291
for (entry_ptr = (grub_uint32_t *) (rsdt + 1);
292
entry_ptr < (grub_uint32_t *) (((grub_uint8_t *) rsdt)
296
if (grub_memcmp ((void *) (grub_addr_t) *entry_ptr, "FACP", 4) == 0)
299
struct grub_acpi_fadt *fadt
300
= ((struct grub_acpi_fadt *) (grub_addr_t) *entry_ptr);
301
struct grub_acpi_table_header *dsdt
302
= (struct grub_acpi_table_header *) (grub_addr_t) fadt->dsdt_addr;
307
grub_dprintf ("acpi", "PM1a port=%x\n", port);
309
if (grub_memcmp (dsdt->signature, "DSDT",
310
sizeof (dsdt->signature)) != 0)
313
sleep_type = get_sleep_type ((grub_uint8_t *) dsdt,
314
(grub_uint8_t *) dsdt + dsdt->length);
316
if (sleep_type < 0 || sleep_type >= 8)
319
grub_dprintf ("acpi", "SLP_TYP = %d, port = 0x%x\n",
322
grub_outw (GRUB_ACPI_SLP_EN
323
| (sleep_type << GRUB_ACPI_SLP_TYP_OFFSET), port & 0xffff);
327
grub_printf ("ACPI shutdown failed\n");