Debian Codegraph @

grub-core/commands/i386/pc/halt.c

raw
1 /* halt.c - command to halt the computer. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2009 Free Software Foundation, Inc.
5 *
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <grub/dl.h>
21 #include <grub/misc.h>
22 #include <grub/extcmd.h>
23 #include <grub/i18n.h>
24 #include <grub/machine/int.h>
25 #include <grub/acpi.h>
26
27 GRUB_MOD_LICENSE ("GPLv3+");
28
29 static const struct grub_arg_option options[] =
30 {
31 {"no-apm", 'n', 0, N_("Do not use APM to halt the computer."), 0, 0},
32 {0, 0, 0, 0, 0, 0}
33 };
34
35 static inline void __attribute__ ((noreturn))
36 stop (void)
37 {
38 while (1)
39 {
40 asm volatile ("hlt");
41 }
42 }
43 /*
44 * Halt the system, using APM if possible. If NO_APM is true, don't use
45 * APM even if it is available.
46 */
47 void __attribute__ ((noreturn))
48 grub_halt (int no_apm)
49 {
50 struct grub_bios_int_registers regs;
51
52 grub_acpi_halt ();
53
54 if (no_apm)
55 stop ();
56
57 /* detect APM */
58 regs.eax = 0x5300;
59 regs.ebx = 0;
60 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
61 grub_bios_interrupt (0x15, &regs);
62
63 if (regs.flags & GRUB_CPU_INT_FLAGS_CARRY)
64 stop ();
65
66 /* disconnect APM first */
67 regs.eax = 0x5304;
68 regs.ebx = 0;
69 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
70 grub_bios_interrupt (0x15, &regs);
71
72 /* connect APM */
73 regs.eax = 0x5301;
74 regs.ebx = 0;
75 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
76 grub_bios_interrupt (0x15, &regs);
77 if (regs.flags & GRUB_CPU_INT_FLAGS_CARRY)
78 stop ();
79
80 /* set APM protocol level - 1.1 or bust. (this covers APM 1.2 also) */
81 regs.eax = 0x530E;
82 regs.ebx = 0;
83 regs.ecx = 0x0101;
84 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
85 grub_bios_interrupt (0x15, &regs);
86 if (regs.flags & GRUB_CPU_INT_FLAGS_CARRY)
87 stop ();
88
89 /* set the power state to off */
90 regs.eax = 0x5307;
91 regs.ebx = 1;
92 regs.ecx = 3;
93 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
94 grub_bios_interrupt (0x15, &regs);
95
96 /* shouldn't reach here */
97 stop ();
98 }
99
100 static grub_err_t __attribute__ ((noreturn))
101 grub_cmd_halt (grub_extcmd_context_t ctxt,
102 int argc __attribute__ ((unused)),
103 char **args __attribute__ ((unused)))
104
105 {
106 struct grub_arg_list *state = ctxt->state;
107 int no_apm = 0;
108
109 if (state[0].set)
110 no_apm = 1;
111 grub_halt (no_apm);
112 }
113
114 static grub_extcmd_t cmd;
115
116 GRUB_MOD_INIT(halt)
117 {
118 cmd = grub_register_extcmd ("halt", grub_cmd_halt, 0, "[-n]",
119 N_("Halt the system, if possible using APM."),
120 options);
121 }
122
123 GRUB_MOD_FINI(halt)
124 {
125 grub_unregister_extcmd (cmd);
126 }