799d90be8dd277def35e380a143f21b84f7c280f
[reactos.git] / boot / environ / lib / misc / debug.c
1 /*
2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/lib/misc/debug.c
5 * PURPOSE: Boot Library Debug Routines
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include "bl.h"
12
13 /* DATA VARIABLES ************************************************************/
14
15 CHAR AnsiBuffer[1024];
16 BOOLEAN BdDebuggerNotPresent;
17 BOOLEAN BdSubsystemInitialized;
18 BOOLEAN BdArchBlockDebuggerOperation;
19 BOOLEAN BlpStatusErrorInProgress;
20 PBL_STATUS_ERROR_HANDLER BlpStatusErrorHandler;
21
22 /* FUNCTIONS *****************************************************************/
23
24 BOOLEAN
25 BdDebuggerInitialized (
26 VOID
27 )
28 {
29 /* Check if BD was initialized, and is currently usable */
30 return BdSubsystemInitialized && !BdArchBlockDebuggerOperation;
31 }
32
33 NTSTATUS
34 BlBdPullRemoteFile (
35 _In_ PWCHAR FilePath,
36 _Out_ PVOID BaseAddress,
37 _Out_ PULONGLONG FileSize
38 )
39 {
40 /* Is the boot debugger enabled? */
41 if (!BlBdDebuggerEnabled())
42 {
43 /* Nothing to pull */
44 return STATUS_DEBUGGER_INACTIVE;
45 }
46
47 /* TODO */
48 EfiPrintf(L"Todo\r\n");
49 return STATUS_NOT_IMPLEMENTED;
50 }
51
52 BOOLEAN
53 BlBdDebuggerEnabled (
54 VOID
55 )
56 {
57 BOOLEAN Initialized, Enabled;
58
59 /* Check if the debugger is initialized */
60 Initialized = BdDebuggerInitialized();
61
62 /* Check if it's currently active */
63 Enabled = FALSE;
64 if ((Initialized) && !(BdDebuggerNotPresent))
65 {
66 /* Yep! */
67 Enabled = TRUE;
68 }
69
70 /* Return enabled state */
71 return Enabled;
72 }
73
74 VOID
75 BlStatusPrint (
76 _In_ PCWCH Format,
77 ...
78 )
79 {
80 ANSI_STRING AnsiString;
81 UNICODE_STRING UnicodeString;
82 va_list va;
83 NTSTATUS Status;
84
85 va_start(va, Format);
86
87 /* Check if the boot debugger is enabled */
88 if (BlBdDebuggerEnabled()
89 #if DBG
90 || TRUE
91 #endif
92 )
93 {
94 /* Print the string out into a buffer */
95 if (vswprintf(BlScratchBuffer, Format, va) > 0)
96 {
97 #if DBG
98 EfiPrintf(BlScratchBuffer);
99 EfiPrintf(L"\r\n");
100 #endif
101 /* Make it a UNICODE_STRING */
102 RtlInitUnicodeString(&UnicodeString, BlScratchBuffer);
103
104 /* Then convert it into an ANSI_STRING */
105 AnsiString.Length = 0;
106 AnsiString.MaximumLength = sizeof(AnsiBuffer);
107 AnsiString.Buffer = AnsiBuffer;
108 Status = RtlUnicodeStringToAnsiString(&AnsiString, &UnicodeString, FALSE);
109 if (NT_SUCCESS(Status))
110 {
111 /* Print it out to the debugger if that worked */
112 DbgPrint(AnsiString.Buffer);
113 }
114 }
115 }
116
117 va_end(va);
118 }
119
120 VOID
121 BlStatusError (
122 _In_ ULONG ErrorCode,
123 _In_ ULONG Parameter1,
124 _In_ ULONG_PTR Parameter2,
125 _In_ ULONG_PTR Parameter3,
126 _In_ ULONG_PTR Parameter4
127 )
128 {
129 NTSTATUS Status;
130
131 /* Is this a non-boot error? */
132 if (ErrorCode != 1)
133 {
134 /* Is one already ongoing? */
135 if (!BlpStatusErrorInProgress)
136 {
137 /* Do we have a custom error handler registered? */
138 if (BlpStatusErrorHandler)
139 {
140 /* Call it, making sure to avoid recursion */
141 BlpStatusErrorInProgress = TRUE;
142 Status = BlpStatusErrorHandler(ErrorCode,
143 Parameter1,
144 Parameter2,
145 Parameter3,
146 Parameter4);
147 BlpStatusErrorInProgress = FALSE;
148
149 /* If the error handler consumed the error, we're done */
150 if (NT_SUCCESS(Status))
151 {
152 return;
153 }
154 }
155 }
156 }
157
158 /* Check if the boot debugger is enabled */
159 if (BlBdDebuggerEnabled())
160 {
161 /* Print out the fatal error */
162 BlStatusPrint(L"\n"
163 L"*** Fatal Error 0x%08x :\n"
164 L" (0x%p, 0x%p, 0x%p, 0x%p)\n"
165 L"\n",
166 ErrorCode,
167 Parameter1,
168 Parameter2,
169 Parameter3,
170 Parameter4);
171
172 /* Issue a breakpoint */
173 __debugbreak();
174 }
175 }
176