2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2010 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#ifdef GRUB_DSDT_TEST
2N/A#include <stdio.h>
2N/A#include <unistd.h>
2N/A#include <stdlib.h>
2N/A#include <stdint.h>
2N/A#include <string.h>
2N/A
2N/A#define grub_dprintf(cond, args...) printf ( args )
2N/A#define grub_printf printf
2N/Atypedef uint64_t grub_uint64_t;
2N/Atypedef uint32_t grub_uint32_t;
2N/Atypedef uint16_t grub_uint16_t;
2N/Atypedef uint8_t grub_uint8_t;
2N/A
2N/A#endif
2N/A
2N/A#include <grub/acpi.h>
2N/A#include <grub/i18n.h>
2N/A
2N/A#ifndef GRUB_DSDT_TEST
2N/A#include <grub/misc.h>
2N/A#include <grub/time.h>
2N/A#include <grub/cpu/io.h>
2N/A#endif
2N/A
2N/Astatic inline grub_uint32_t
2N/Adecode_length (const grub_uint8_t *ptr, int *numlen)
2N/A{
2N/A int num_bytes, i;
2N/A grub_uint32_t ret;
2N/A if (*ptr < 64)
2N/A {
2N/A if (numlen)
2N/A *numlen = 1;
2N/A return *ptr;
2N/A }
2N/A num_bytes = *ptr >> 6;
2N/A if (numlen)
2N/A *numlen = num_bytes + 1;
2N/A ret = *ptr & 0xf;
2N/A ptr++;
2N/A for (i = 0; i < num_bytes; i++)
2N/A {
2N/A ret |= *ptr << (8 * i + 4);
2N/A ptr++;
2N/A }
2N/A return ret;
2N/A}
2N/A
2N/Astatic inline grub_uint32_t
2N/Askip_name_string (const grub_uint8_t *ptr, const grub_uint8_t *end)
2N/A{
2N/A const grub_uint8_t *ptr0 = ptr;
2N/A
2N/A while (ptr < end && (*ptr == '^' || *ptr == '\\'))
2N/A ptr++;
2N/A switch (*ptr)
2N/A {
2N/A case '.':
2N/A ptr++;
2N/A ptr += 8;
2N/A break;
2N/A case '/':
2N/A ptr++;
2N/A ptr += 1 + (*ptr) * 4;
2N/A break;
2N/A case 0:
2N/A ptr++;
2N/A break;
2N/A default:
2N/A ptr += 4;
2N/A break;
2N/A }
2N/A return ptr - ptr0;
2N/A}
2N/A
2N/Astatic inline grub_uint32_t
2N/Askip_data_ref_object (const grub_uint8_t *ptr, const grub_uint8_t *end)
2N/A{
2N/A grub_dprintf ("acpi", "data type = 0x%x\n", *ptr);
2N/A switch (*ptr)
2N/A {
2N/A case GRUB_ACPI_OPCODE_PACKAGE:
2N/A return 1 + decode_length (ptr + 1, 0);
2N/A case GRUB_ACPI_OPCODE_ZERO:
2N/A case GRUB_ACPI_OPCODE_ONES:
2N/A case GRUB_ACPI_OPCODE_ONE:
2N/A return 1;
2N/A case GRUB_ACPI_OPCODE_BYTE_CONST:
2N/A return 2;
2N/A case GRUB_ACPI_OPCODE_WORD_CONST:
2N/A return 3;
2N/A case GRUB_ACPI_OPCODE_DWORD_CONST:
2N/A return 5;
2N/A default:
2N/A if (*ptr == '^' || *ptr == '\\' || *ptr == '_'
2N/A || (*ptr >= 'A' && *ptr <= 'Z'))
2N/A return skip_name_string (ptr, end);
2N/A grub_printf ("Unknown opcode 0x%x\n", *ptr);
2N/A return 0;
2N/A }
2N/A}
2N/A
2N/Astatic inline grub_uint32_t
2N/Askip_ext_op (const grub_uint8_t *ptr, const grub_uint8_t *end)
2N/A{
2N/A const grub_uint8_t *ptr0 = ptr;
2N/A int add;
2N/A grub_dprintf ("acpi", "Extended opcode: 0x%x\n", *ptr);
2N/A switch (*ptr)
2N/A {
2N/A case GRUB_ACPI_EXTOPCODE_MUTEX:
2N/A ptr++;
2N/A ptr += skip_name_string (ptr, end);
2N/A ptr++;
2N/A break;
2N/A case GRUB_ACPI_EXTOPCODE_OPERATION_REGION:
2N/A ptr++;
2N/A ptr += skip_name_string (ptr, end);
2N/A ptr++;
2N/A ptr += add = skip_data_ref_object (ptr, end);
2N/A if (!add)
2N/A return 0;
2N/A ptr += add = skip_data_ref_object (ptr, end);
2N/A if (!add)
2N/A return 0;
2N/A break;
2N/A case GRUB_ACPI_EXTOPCODE_FIELD_OP:
2N/A case GRUB_ACPI_EXTOPCODE_INDEX_FIELD_OP:
2N/A ptr++;
2N/A ptr += decode_length (ptr, 0);
2N/A break;
2N/A default:
2N/A grub_printf ("Unexpected extended opcode: 0x%x\n", *ptr);
2N/A return 0;
2N/A }
2N/A return ptr - ptr0;
2N/A}
2N/A
2N/Astatic int
2N/Aget_sleep_type (grub_uint8_t *table, grub_uint8_t *end)
2N/A{
2N/A grub_uint8_t *ptr, *prev = table;
2N/A int sleep_type = -1;
2N/A
2N/A ptr = table + sizeof (struct grub_acpi_table_header);
2N/A while (ptr < end && prev < ptr)
2N/A {
2N/A int add;
2N/A prev = ptr;
2N/A grub_dprintf ("acpi", "Opcode 0x%x\n", *ptr);
2N/A grub_dprintf ("acpi", "Tell %x\n", (unsigned) (ptr - table));
2N/A switch (*ptr)
2N/A {
2N/A case GRUB_ACPI_OPCODE_EXTOP:
2N/A ptr++;
2N/A ptr += add = skip_ext_op (ptr, end);
2N/A if (!add)
2N/A return -1;
2N/A break;
2N/A case GRUB_ACPI_OPCODE_NAME:
2N/A ptr++;
2N/A if (memcmp (ptr, "_S5_", 4) == 0 || memcmp (ptr, "\\_S5_", 4) == 0)
2N/A {
2N/A int ll;
2N/A grub_uint8_t *ptr2 = ptr;
2N/A grub_dprintf ("acpi", "S5 found\n");
2N/A ptr2 += skip_name_string (ptr, end);
2N/A if (*ptr2 != 0x12)
2N/A {
2N/A grub_printf ("Unknown opcode in _S5: 0x%x\n", *ptr2);
2N/A return -1;
2N/A }
2N/A ptr2++;
2N/A decode_length (ptr2, &ll);
2N/A ptr2 += ll;
2N/A ptr2++;
2N/A switch (*ptr2)
2N/A {
2N/A case GRUB_ACPI_OPCODE_ZERO:
2N/A sleep_type = 0;
2N/A break;
2N/A case GRUB_ACPI_OPCODE_ONE:
2N/A sleep_type = 1;
2N/A break;
2N/A case GRUB_ACPI_OPCODE_BYTE_CONST:
2N/A sleep_type = ptr2[1];
2N/A break;
2N/A default:
2N/A grub_printf ("Unknown data type in _S5: 0x%x\n", *ptr2);
2N/A return -1;
2N/A }
2N/A }
2N/A ptr += add = skip_name_string (ptr, end);
2N/A if (!add)
2N/A return -1;
2N/A ptr += add = skip_data_ref_object (ptr, end);
2N/A if (!add)
2N/A return -1;
2N/A break;
2N/A case GRUB_ACPI_OPCODE_SCOPE:
2N/A case GRUB_ACPI_OPCODE_IF:
2N/A case GRUB_ACPI_OPCODE_METHOD:
2N/A {
2N/A ptr++;
2N/A ptr += decode_length (ptr, 0);
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A
2N/A grub_dprintf ("acpi", "TYP = %d\n", sleep_type);
2N/A return sleep_type;
2N/A}
2N/A
2N/A#ifdef GRUB_DSDT_TEST
2N/Aint
2N/Amain (int argc, char **argv)
2N/A{
2N/A FILE *f;
2N/A size_t len;
2N/A unsigned char *buf;
2N/A if (argc < 2)
2N/A printf ("Usage: %s FILE\n", argv[0]);
2N/A f = fopen (argv[1], "rb");
2N/A if (!f)
2N/A {
2N/A printf ("Couldn't open file\n");
2N/A return 1;
2N/A }
2N/A fseek (f, 0, SEEK_END);
2N/A len = ftell (f);
2N/A fseek (f, 0, SEEK_SET);
2N/A buf = malloc (len);
2N/A if (!buf)
2N/A {
2N/A printf ("Couldn't malloc buffer\n");
2N/A fclose (f);
2N/A return 2;
2N/A }
2N/A if (fread (buf, 1, len, f) != len)
2N/A {
2N/A printf ("Read failed\n");
2N/A free (buf);
2N/A fclose (f);
2N/A return 2;
2N/A }
2N/A
2N/A printf ("Sleep type = %d\n", get_sleep_type (buf, buf + len));
2N/A free (buf);
2N/A fclose (f);
2N/A return 0;
2N/A}
2N/A
2N/A#else
2N/A
2N/Avoid
2N/Agrub_acpi_halt (void)
2N/A{
2N/A struct grub_acpi_rsdp_v20 *rsdp2;
2N/A struct grub_acpi_rsdp_v10 *rsdp1;
2N/A struct grub_acpi_table_header *rsdt;
2N/A grub_uint32_t *entry_ptr;
2N/A
2N/A rsdp2 = grub_acpi_get_rsdpv2 ();
2N/A if (rsdp2)
2N/A rsdp1 = &(rsdp2->rsdpv1);
2N/A else
2N/A rsdp1 = grub_acpi_get_rsdpv1 ();
2N/A grub_dprintf ("acpi", "rsdp1=%p\n", rsdp1);
2N/A if (!rsdp1)
2N/A return;
2N/A
2N/A rsdt = (struct grub_acpi_table_header *) (grub_addr_t) rsdp1->rsdt_addr;
2N/A for (entry_ptr = (grub_uint32_t *) (rsdt + 1);
2N/A entry_ptr < (grub_uint32_t *) (((grub_uint8_t *) rsdt)
2N/A + rsdt->length);
2N/A entry_ptr++)
2N/A {
2N/A if (grub_memcmp ((void *) (grub_addr_t) *entry_ptr, "FACP", 4) == 0)
2N/A {
2N/A grub_uint32_t port;
2N/A struct grub_acpi_fadt *fadt
2N/A = ((struct grub_acpi_fadt *) (grub_addr_t) *entry_ptr);
2N/A struct grub_acpi_table_header *dsdt
2N/A = (struct grub_acpi_table_header *) (grub_addr_t) fadt->dsdt_addr;
2N/A int sleep_type = -1;
2N/A
2N/A port = fadt->pm1a;
2N/A
2N/A grub_dprintf ("acpi", "PM1a port=%x\n", port);
2N/A
2N/A if (grub_memcmp (dsdt->signature, "DSDT",
2N/A sizeof (dsdt->signature)) != 0)
2N/A break;
2N/A
2N/A sleep_type = get_sleep_type ((grub_uint8_t *) dsdt,
2N/A (grub_uint8_t *) dsdt + dsdt->length);
2N/A
2N/A if (sleep_type < 0 || sleep_type >= 8)
2N/A break;
2N/A
2N/A grub_dprintf ("acpi", "SLP_TYP = %d, port = 0x%x\n",
2N/A sleep_type, port);
2N/A
2N/A grub_outw (GRUB_ACPI_SLP_EN
2N/A | (sleep_type << GRUB_ACPI_SLP_TYP_OFFSET), port & 0xffff);
2N/A }
2N/A }
2N/A
2N/A grub_millisleep (1500);
2N/A
2N/A grub_puts_ (N_("ACPI shutdown failed"));
2N/A}
2N/A#endif