[CMAKE]
[reactos.git] / ntoskrnl / po / poshtdwn.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/po/poshtdwn.c
5 * PURPOSE: Power Manager Shutdown Code
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include <ntoskrnl.h>
12 #ifdef NEWCC
13 #include "../cache/newcc.h"
14 #endif
15 #define NDEBUG
16 #include <debug.h>
17
18 /* GLOBALS *******************************************************************/
19
20 ULONG PopShutdownPowerOffPolicy;
21
22 /* PRIVATE FUNCTIONS *********************************************************/
23
24 VOID
25 NTAPI
26 PopShutdownHandler(VOID)
27 {
28 PUCHAR Logo1, Logo2;
29 ULONG i;
30
31 /* Stop all interrupts */
32 KeRaiseIrqlToDpcLevel();
33 _disable();
34
35 /* Do we have boot video */
36 if (InbvIsBootDriverInstalled())
37 {
38 /* Yes we do, cleanup for shutdown screen */
39 if (!InbvCheckDisplayOwnership()) InbvAcquireDisplayOwnership();
40 InbvResetDisplay();
41 InbvSolidColorFill(0, 0, 639, 479, 0);
42 InbvEnableDisplayString(TRUE);
43 InbvSetScrollRegion(0, 0, 639, 479);
44
45 /* Display shutdown logo and message */
46 Logo1 = InbvGetResourceAddress(IDB_SHUTDOWN_LOGO);
47 Logo2 = InbvGetResourceAddress(IDB_LOGO);
48 if ((Logo1) && (Logo2))
49 {
50 InbvBitBlt(Logo1, 215, 352);
51 InbvBitBlt(Logo2, 217, 111);
52 }
53 }
54 else
55 {
56 /* Do it in text-mode */
57 for (i = 0; i < 25; i++) InbvDisplayString("\n");
58 InbvDisplayString(" ");
59 InbvDisplayString("The system may be powered off now.\n");
60 }
61
62 /* Hang the system */
63 for (;;) HalHaltSystem();
64 }
65
66 VOID
67 NTAPI
68 PopShutdownSystem(IN POWER_ACTION SystemAction)
69 {
70 /* Note should notify caller of NtPowerInformation(PowerShutdownNotification) */
71
72 /* Unload symbols */
73 DPRINT1("It's the final countdown...%lx\n", SystemAction);
74 DbgUnLoadImageSymbols(NULL, (PVOID)-1, 0);
75
76 /* Run the thread on the boot processor */
77 KeSetSystemAffinityThread(1);
78
79 /* Now check what the caller wants */
80 switch (SystemAction)
81 {
82 /* Reset */
83 case PowerActionShutdownReset:
84
85 /* Try platform driver first, then legacy */
86 //PopInvokeSystemStateHandler(PowerStateShutdownReset, NULL);
87 HalReturnToFirmware(HalRebootRoutine);
88 break;
89
90 case PowerActionShutdown:
91
92 /* Check for group policy that says to use "it is now safe" screen */
93 if (PopShutdownPowerOffPolicy)
94 {
95 /* FIXFIX: Switch to legacy shutdown handler */
96 //PopPowerStateHandlers[PowerStateShutdownOff].Handler = PopShutdownHandler;
97 }
98
99 case PowerActionShutdownOff:
100
101 /* Call shutdown handler */
102 //PopInvokeSystemStateHandler(PowerStateShutdownOff, NULL);
103
104 /* ReactOS Hack */
105 PopSetSystemPowerState(PowerSystemShutdown);
106 PopShutdownHandler();
107
108 /* If that didn't work, call the HAL */
109 HalReturnToFirmware(HalPowerDownRoutine);
110 break;
111
112 default:
113 break;
114 }
115
116 /* Anything else should not happen */
117 KeBugCheckEx(INTERNAL_POWER_ERROR, 5, 0, 0, 0);
118 }
119
120 VOID
121 NTAPI
122 PopGracefulShutdown(IN PVOID Context)
123 {
124 PEPROCESS Process = NULL;
125
126 /* Loop every process */
127 Process = PsGetNextProcess(Process);
128 while (Process)
129 {
130 /* Make sure this isn't the idle or initial process */
131 if ((Process != PsInitialSystemProcess) && (Process != PsIdleProcess))
132 {
133 /* Print it */
134 DPRINT1("%15s is still RUNNING (%lx)\n", Process->ImageFileName, Process->UniqueProcessId);
135 }
136
137 /* Get the next process */
138 Process = PsGetNextProcess(Process);
139 }
140
141 /* First, the HAL handles any "end of boot" special functionality */
142 DPRINT1("HAL shutting down\n");
143 HalEndOfBoot();
144
145 /* In this step, the I/O manager does first-chance shutdown notification */
146 DPRINT1("I/O manager shutting down in phase 0\n");
147 IoShutdownSystem(0);
148
149 /* In this step, all workers are killed and hives are flushed */
150 DPRINT1("Configuration Manager shutting down\n");
151 CmShutdownSystem();
152
153 /* Note that modified pages should be written here (MiShutdownSystem) */
154
155 /* In this step, the I/O manager does last-chance shutdown notification */
156 DPRINT1("I/O manager shutting down in phase 1\n");
157 IoShutdownSystem(1);
158 CcWaitForCurrentLazyWriterActivity();
159
160 #ifdef NEWCC
161 CcShutdownSystem();
162 #endif
163
164 /* Note that here, we should broadcast the power IRP to devices */
165
166 /* In this step, the HAL disables any wake timers */
167 DPRINT1("Disabling wake timers\n");
168 HalSetWakeEnable(FALSE);
169
170 /* And finally the power request is sent */
171 DPRINT1("Taking the system down\n");
172 PopShutdownSystem(PopAction.Action);
173 }
174
175 VOID
176 NTAPI
177 PopReadShutdownPolicy(VOID)
178 {
179 UNICODE_STRING KeyString;
180 OBJECT_ATTRIBUTES ObjectAttributes;
181 NTSTATUS Status;
182 HANDLE KeyHandle;
183 ULONG Length;
184 UCHAR Buffer[sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG)];
185 PKEY_VALUE_PARTIAL_INFORMATION Info = (PVOID)Buffer;
186
187 /* Setup object attributes */
188 RtlInitUnicodeString(&KeyString,
189 L"\\Registry\\Machine\\Software\\Policies\\Microsoft\\Windows NT");
190 InitializeObjectAttributes(&ObjectAttributes,
191 &KeyString,
192 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
193 NULL,
194 NULL);
195
196 /* Open the key */
197 Status = ZwOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
198 if (NT_SUCCESS(Status))
199 {
200 /* Open the policy value and query it */
201 RtlInitUnicodeString(&KeyString, L"DontPowerOffAfterShutdown");
202 Status = ZwQueryValueKey(KeyHandle,
203 &KeyString,
204 KeyValuePartialInformation,
205 &Info,
206 sizeof(Info),
207 &Length);
208 if ((NT_SUCCESS(Status)) && (Info->Type == REG_DWORD))
209 {
210 /* Read the policy */
211 PopShutdownPowerOffPolicy = *Info->Data == 1;
212 }
213
214 /* Close the key */
215 ZwClose(KeyHandle);
216 }
217 }
218
219 /* PUBLIC FUNCTIONS **********************************************************/
220