4df7996d87
* grub-core/Makefile.core.def (halt): Add commands/acpihalt.c on i386-pc. * grub-core/commands/acpihalt.c: New file. * grub-core/commands/i386/pc/halt.c (grub_cmd_halt): Call grub_acpi_halt. * include/grub/acpi.h (grub_acpi_fadt): New member pm1a. (grub_acpi_halt): New proto. (GRUB_ACPI_SLP_EN): New const. (GRUB_ACPI_SLP_TYP_OFFSET): Likewise. (GRUB_ACPI_OPCODE_*): New enum. (GRUB_ACPI_EXTOPCODE_*): Likewise.
126 lines
3 KiB
C
126 lines
3 KiB
C
/* halt.c - command to halt the computer. */
|
||
/*
|
||
* GRUB -- GRand Unified Bootloader
|
||
* Copyright (C) 2005,2007,2009 Free Software Foundation, Inc.
|
||
*
|
||
* GRUB 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 3 of the License, or
|
||
* (at your option) any later version.
|
||
*
|
||
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
#include <grub/dl.h>
|
||
#include <grub/misc.h>
|
||
#include <grub/extcmd.h>
|
||
#include <grub/i18n.h>
|
||
#include <grub/machine/int.h>
|
||
#include <grub/acpi.h>
|
||
|
||
static const struct grub_arg_option options[] =
|
||
{
|
||
{"no-apm", 'n', 0, N_("Do not use APM to halt the computer."), 0, 0},
|
||
{0, 0, 0, 0, 0, 0}
|
||
};
|
||
|
||
static inline void __attribute__ ((noreturn))
|
||
stop (void)
|
||
{
|
||
while (1)
|
||
{
|
||
asm volatile ("hlt");
|
||
}
|
||
}
|
||
/*
|
||
* Halt the system, using APM if possible. If NO_APM is true, don't use
|
||
* APM even if it is available.
|
||
*/
|
||
void
|
||
grub_halt (int no_apm)
|
||
{
|
||
struct grub_bios_int_registers regs;
|
||
|
||
if (no_apm)
|
||
stop ();
|
||
|
||
/* detect APM */
|
||
regs.eax = 0x5300;
|
||
regs.ebx = 0;
|
||
regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
|
||
grub_bios_interrupt (0x15, ®s);
|
||
|
||
if (regs.flags & GRUB_CPU_INT_FLAGS_CARRY)
|
||
stop ();
|
||
|
||
/* disconnect APM first */
|
||
regs.eax = 0x5304;
|
||
regs.ebx = 0;
|
||
regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
|
||
grub_bios_interrupt (0x15, ®s);
|
||
|
||
/* connect APM */
|
||
regs.eax = 0x5301;
|
||
regs.ebx = 0;
|
||
regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
|
||
grub_bios_interrupt (0x15, ®s);
|
||
if (regs.flags & GRUB_CPU_INT_FLAGS_CARRY)
|
||
stop ();
|
||
|
||
/* set APM protocol level - 1.1 or bust. (this covers APM 1.2 also) */
|
||
regs.eax = 0x530E;
|
||
regs.ebx = 0;
|
||
regs.ecx = 0x0101;
|
||
regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
|
||
grub_bios_interrupt (0x15, ®s);
|
||
if (regs.flags & GRUB_CPU_INT_FLAGS_CARRY)
|
||
stop ();
|
||
|
||
/* set the power state to off */
|
||
regs.eax = 0x5307;
|
||
regs.ebx = 1;
|
||
regs.ecx = 3;
|
||
regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
|
||
grub_bios_interrupt (0x15, ®s);
|
||
|
||
/* shouldn't reach here */
|
||
stop ();
|
||
}
|
||
|
||
static grub_err_t
|
||
grub_cmd_halt (grub_extcmd_context_t ctxt,
|
||
int argc __attribute__ ((unused)),
|
||
char **args __attribute__ ((unused)))
|
||
|
||
{
|
||
struct grub_arg_list *state = ctxt->state;
|
||
int no_apm = 0;
|
||
|
||
grub_acpi_halt ();
|
||
|
||
if (state[0].set)
|
||
no_apm = 1;
|
||
grub_halt (no_apm);
|
||
return 0;
|
||
}
|
||
|
||
static grub_extcmd_t cmd;
|
||
|
||
GRUB_MOD_INIT(halt)
|
||
{
|
||
cmd = grub_register_extcmd ("halt", grub_cmd_halt, GRUB_COMMAND_FLAG_BOTH,
|
||
"[-n]",
|
||
N_("Halt the system, if possible using APM."),
|
||
options);
|
||
}
|
||
|
||
GRUB_MOD_FINI(halt)
|
||
{
|
||
grub_unregister_extcmd (cmd);
|
||
}
|