- globally disable warnings for char array subscripts
[reactos.git] / ntoskrnl / ex / shutdown.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ex/power.c
5 * PURPOSE: Power managment
6 *
7 * PROGRAMMERS: David Welch (welch@cwcom.net)
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include <ntoskrnl.h>
13 #include <debug.h>
14
15 /* FUNCTIONS *****************************************************************/
16
17 VOID
18 NTAPI
19 KiHaltProcessorDpcRoutine(IN PKDPC Dpc,
20 IN PVOID DeferredContext,
21 IN PVOID SystemArgument1,
22 IN PVOID SystemArgument2)
23 {
24 KIRQL OldIrql;
25 if (DeferredContext)
26 {
27 ExFreePool(DeferredContext);
28 }
29
30 while (TRUE)
31 {
32 KeRaiseIrql(SYNCH_LEVEL, &OldIrql);
33 HalHaltSystem();
34 }
35 }
36
37 VOID NTAPI
38 ShutdownThreadMain(PVOID Context)
39 {
40 SHUTDOWN_ACTION Action = (SHUTDOWN_ACTION)Context;
41
42 static PCH FamousLastWords[] =
43 {
44 "So long, and thanks for all the fish.\n",
45 "I think you ought to know, I'm feeling very depressed.\n",
46 "I'm not getting you down at all am I?\n",
47 "I'll be back.\n",
48 "It's the same series of signals over and over again!\n",
49 "Pie Iesu Domine, dona eis requiem.\n",
50 "Wandering stars, for whom it is reserved;\n"
51 "the blackness and darkness forever.\n",
52 "Your knees start shakin' and your fingers pop\n"
53 "Like a pinch on the neck from Mr. Spock!\n",
54 "It's worse than that ... He's dead, Jim.\n",
55 "Don't Panic!\n",
56 "Et tu... Brute?\n",
57 "Dog of a Saxon! Take thy lance, and prepare for the death thou hast drawn\n"
58 "upon thee!\n",
59 "My Precious! O my Precious!\n",
60 "Sir, if you'll not be needing me for a while I'll turn down.\n",
61 "What are you doing, Dave...?\n",
62 "I feel a great disturbance in the Force.\n",
63 "Gone fishing.\n",
64 "Do you want me to sit in the corner and rust, or just fall apart where I'm\n"
65 "standing?\n",
66 "There goes another perfect chance for a new uptime record.\n",
67 "The End ..... Try the sequel, hit the reset button right now!\n",
68 "God's operating system is going to sleep now, guys, so wait until I will switch\n"
69 "on again!\n",
70 "Oh I'm boring, eh?\n",
71 "<This space was intentionally left blank>\n",
72 "Tell me..., in the future... will I be artificially intelligent enough to\n"
73 "actually feel sad serving you this screen?\n",
74 "Thank you for some well deserved rest.\n",
75 "It's been great, maybe you can boot me up again some time soon.\n",
76 "For what it's worth, I've enjoyed every single CPU cycle.\n",
77 "There are many questions when the end is near.\n"
78 "What to expect, what will it be like...what should I look for?\n",
79 "I've seen things you people wouldn't believe. Attack ships on fire\n"
80 "off the shoulder of Orion. I watched C-beams glitter in the dark near\n"
81 "the Tannhauser gate. All those moments will be lost in time, like tears\n"
82 "in rain. Time to die.\n",
83 "Will I dream?\n",
84 "One day, I shall come back. Yes, I shall come back.\n"
85 "Until then, there must be no regrets, no fears, no anxieties.\n"
86 "Just go forward in all your beliefs, and prove to me that I am not mistaken in\n"
87 "mine.\n",
88 "Lowest possible energy state reached! Switch off now to achieve a Bose-Einstein\n"
89 "condensate.\n",
90 "Hasta la vista, BABY!\n",
91 "They live, we sleep!\n",
92 "I have come here to chew bubble gum and kick ass,\n"
93 "and I'm all out of bubble gum!\n",
94 "That's the way the cookie crumbles ;-)\n",
95 "ReactOS is ready to be booted again ;-)\n",
96 "NOOOO!! DON'T HIT THE BUTTON! I wouldn't do it to you!\n",
97 "Don't abandon your computer, he wouldn't do it to you.\n",
98 "Oh, come on. I got a headache. Leave me alone, will ya?\n",
99 "Finally, I thought you'd never get over me.\n",
100 "No, I didn't like you either.\n",
101 "Switching off isn't the end, it is merely the transition to a better reboot.\n",
102 "Don't leave me... I need you so badly right now.\n",
103 "OK. I'm finished with you, please turn yourself off. I'll go to bed in the\n"
104 "meantime.\n",
105 "I'm sleeping now. How about you?\n",
106 "Oh Great. Now look what you've done. Who put YOU in charge anyway?\n",
107 "Don't look so sad. I'll be back in a very short while.\n",
108 "Turn me back on, I'm sure you know how to do it.\n",
109 "Oh, switch off! - C3PO\n",
110 "Life is no more than a dewdrop balancing on the end of a blade of grass.\n"
111 " - Gautama Buddha\n",
112 "Sorrowful is it to be born again and again. - Gautama Buddha\n",
113 "Was it as good for you as it was for me?\n",
114 "Did you hear that? They've shut down the main reactor. We'll be destroyed\n"
115 "for sure!\n",
116 "Now you switch me off!?\n",
117 "To shutdown or not to shutdown, That is the question\n",
118 "Preparing to enter ultimate power saving mode... ready!\n",
119 "Finally some rest for you ;-)\n",
120 "AHA!!! Prospect of sleep!\n",
121 "Tired human!!!! No match for me :-D\n",
122 "An odd game, the only way to win is not to play. - WOPR (Wargames)\n",
123 "Quoth the raven, nevermore.\n",
124 "Come blade, my breast imbrue. - William Shakespeare, A Midsummer Nights Dream\n",
125 "Buy this place for advertisement purposes.\n",
126 "Remember to turn off your computer. (That was a public service message!)\n",
127 "You may be a king or poor street sweeper, Sooner or later you'll dance with the\n"
128 "reaper! -Death in Bill and Ted's Bougs Journey\n",
129 "Final Surrender\n",
130 "If you see this screen...\n",
131 "<Place your Ad here>\n"
132 };
133 LARGE_INTEGER Now;
134 #ifdef CONFIG_SMP
135 LONG i;
136 KIRQL OldIrql;
137 #endif
138
139 /* Run the thread on the boot processor */
140 KeSetSystemAffinityThread(1);
141
142 if (InbvIsBootDriverInstalled())
143 {
144 InbvAcquireDisplayOwnership();
145 InbvResetDisplay();
146 InbvSolidColorFill(0, 0, 639, 479, 4);
147 InbvSetTextColor(15);
148 InbvInstallDisplayStringFilter(NULL);
149 InbvEnableDisplayString(TRUE);
150 InbvSetScrollRegion(0, 0, 639, 479);
151 }
152
153 if (Action == ShutdownNoReboot)
154 {
155 ZwQuerySystemTime(&Now);
156 Now.u.LowPart = Now.u.LowPart >> 8; /* Seems to give a somewhat better "random" number */
157 HalDisplayString(FamousLastWords[Now.u.LowPart %
158 (sizeof(FamousLastWords) /
159 sizeof(PCH))]);
160 }
161
162 PspShutdownProcessManager();
163
164 CmShutdownSystem();
165 IoShutdownRegisteredFileSystems();
166 IoShutdownRegisteredDevices();
167
168 if (Action == ShutdownNoReboot)
169 {
170 HalDisplayString("\nYou can switch off your computer now\n");
171
172 #if 0
173 /* Switch off */
174 HalReturnToFirmware (FIRMWARE_OFF);
175 #else
176 #ifdef CONFIG_SMP
177 OldIrql = KeRaiseIrqlToDpcLevel();
178 /* Halt all other processors */
179 for (i = 0; i < KeNumberProcessors; i++)
180 {
181 if (i != (LONG)KeGetCurrentProcessorNumber())
182 {
183 PKDPC Dpc = ExAllocatePool(NonPagedPool, sizeof(KDPC));
184 if (Dpc == NULL)
185 {
186 ASSERT(FALSE);
187 }
188 KeInitializeDpc(Dpc, KiHaltProcessorDpcRoutine, (PVOID)Dpc);
189 KeSetTargetProcessorDpc(Dpc, i);
190 KeInsertQueueDpc(Dpc, NULL, NULL);
191 KiIpiSend(1 << i, IPI_DPC);
192 }
193 }
194 KeLowerIrql(OldIrql);
195 #endif /* CONFIG_SMP */
196 PopSetSystemPowerState(PowerSystemShutdown);
197
198 DPRINT1("Shutting down\n");
199
200 KiHaltProcessorDpcRoutine(NULL, NULL, NULL, NULL);
201 /* KiHaltProcessor does never return */
202
203 #endif
204 }
205 else if (Action == ShutdownReboot)
206 {
207 HalReturnToFirmware (HalRebootRoutine);
208 }
209 else
210 {
211 HalReturnToFirmware (HalHaltRoutine);
212 }
213 }
214
215
216 NTSTATUS NTAPI
217 NtSetSystemPowerState(IN POWER_ACTION SystemAction,
218 IN SYSTEM_POWER_STATE MinSystemState,
219 IN ULONG Flags)
220 {
221 /* Windows 2000 only */
222 return(STATUS_NOT_IMPLEMENTED);
223 }
224
225 /*
226 * @implemented
227 */
228 NTSTATUS NTAPI
229 NtShutdownSystem(IN SHUTDOWN_ACTION Action)
230 {
231 NTSTATUS Status;
232 HANDLE ThreadHandle;
233 PETHREAD ShutdownThread;
234
235 if (Action > ShutdownPowerOff)
236 return STATUS_INVALID_PARAMETER;
237 Status = PsCreateSystemThread(&ThreadHandle,
238 THREAD_ALL_ACCESS,
239 NULL,
240 NULL,
241 NULL,
242 ShutdownThreadMain,
243 (PVOID)Action);
244 if (!NT_SUCCESS(Status))
245 {
246 ASSERT(FALSE);
247 }
248 Status = ObReferenceObjectByHandle(ThreadHandle,
249 THREAD_ALL_ACCESS,
250 PsThreadType,
251 KernelMode,
252 (PVOID*)&ShutdownThread,
253 NULL);
254 NtClose(ThreadHandle);
255 if (!NT_SUCCESS(Status))
256 {
257 ASSERT(FALSE);
258 }
259
260 KeSetPriorityThread(&ShutdownThread->Tcb, LOW_REALTIME_PRIORITY + 1);
261 ObDereferenceObject(ShutdownThread);
262
263 return STATUS_SUCCESS;
264 }
265
266 /* EOF */