Add WinNtGop driver into Nt32Pkg
[mirror_edk2.git] / Nt32Pkg / WinNtGopDxe / WinNtGopDriver.c
1 /** @file
2
3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 WinNtGopDriver.c
15
16 Abstract:
17
18 This file implements the UEFI Device Driver model requirements for GOP
19
20 GOP is short hand for Graphics Output Protocol.
21
22
23 **/
24
25 //
26 // The package level header files this module uses
27 //
28 #include <Uefi.h>
29 #include <WinNtDxe.h>
30 //
31 // The protocols, PPI and GUID defintions for this module
32 //
33 #include <Guid/EventGroup.h>
34 #include <Protocol/WinNtIo.h>
35 #include <Protocol/ComponentName.h>
36 #include <Protocol/SimpleTextIn.h>
37 #include <Protocol/DriverBinding.h>
38 #include <Protocol/GraphicsOutput.h>
39 //
40 // The Library classes this module consumes
41 //
42 #include <Library/DebugLib.h>
43 #include <Library/BaseLib.h>
44 #include <Library/UefiDriverEntryPoint.h>
45 #include <Library/UefiLib.h>
46 #include <Library/BaseMemoryLib.h>
47 #include <Library/UefiBootServicesTableLib.h>
48 #include <Library/MemoryAllocationLib.h>
49
50 #include "WinNtGop.h"
51
52 EFI_DRIVER_BINDING_PROTOCOL gWinNtGopDriverBinding = {
53 WinNtGopDriverBindingSupported,
54 WinNtGopDriverBindingStart,
55 WinNtGopDriverBindingStop,
56 0xa,
57 NULL,
58 NULL
59 };
60
61 /**
62 The user Entry Point for module WinNtGop. The user code starts with this function.
63
64 @param[in] ImageHandle The firmware allocated handle for the EFI image.
65 @param[in] SystemTable A pointer to the EFI System Table.
66
67 @retval EFI_SUCCESS The entry point is executed successfully.
68 @retval other Some error occurs when executing this entry point.
69
70 **/
71 EFI_STATUS
72 EFIAPI
73 InitializeWinNtGop(
74 IN EFI_HANDLE ImageHandle,
75 IN EFI_SYSTEM_TABLE *SystemTable
76 )
77 {
78 EFI_STATUS Status;
79
80 //
81 // Install driver model protocol(s).
82 //
83 Status = EfiLibInstallAllDriverProtocols (
84 ImageHandle,
85 SystemTable,
86 &gWinNtGopDriverBinding,
87 ImageHandle,
88 &gWinNtGopComponentName,
89 NULL,
90 NULL
91 );
92 ASSERT_EFI_ERROR (Status);
93
94
95 return Status;
96 }
97
98 /**
99
100
101 @return None
102
103 **/
104 // TODO: This - add argument and description to function comment
105 // TODO: Handle - add argument and description to function comment
106 // TODO: RemainingDevicePath - add argument and description to function comment
107 EFI_STATUS
108 EFIAPI
109 WinNtGopDriverBindingSupported (
110 IN EFI_DRIVER_BINDING_PROTOCOL *This,
111 IN EFI_HANDLE Handle,
112 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
113 )
114 {
115 EFI_STATUS Status;
116 EFI_WIN_NT_IO_PROTOCOL *WinNtIo;
117
118 //
119 // Open the IO Abstraction(s) needed to perform the supported test
120 //
121 Status = gBS->OpenProtocol (
122 Handle,
123 &gEfiWinNtIoProtocolGuid,
124 &WinNtIo,
125 This->DriverBindingHandle,
126 Handle,
127 EFI_OPEN_PROTOCOL_BY_DRIVER
128 );
129 if (EFI_ERROR (Status)) {
130 return Status;
131 }
132
133 Status = WinNtGopSupported (WinNtIo);
134
135 //
136 // Close the I/O Abstraction(s) used to perform the supported test
137 //
138 gBS->CloseProtocol (
139 Handle,
140 &gEfiWinNtIoProtocolGuid,
141 This->DriverBindingHandle,
142 Handle
143 );
144
145 return Status;
146 }
147
148
149 /**
150
151
152 @return None
153
154 **/
155 // TODO: This - add argument and description to function comment
156 // TODO: Handle - add argument and description to function comment
157 // TODO: RemainingDevicePath - add argument and description to function comment
158 // TODO: EFI_UNSUPPORTED - add return value to function comment
159 EFI_STATUS
160 EFIAPI
161 WinNtGopDriverBindingStart (
162 IN EFI_DRIVER_BINDING_PROTOCOL *This,
163 IN EFI_HANDLE Handle,
164 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
165 )
166 {
167 EFI_WIN_NT_IO_PROTOCOL *WinNtIo;
168 EFI_STATUS Status;
169 GOP_PRIVATE_DATA *Private;
170
171 //
172 // Grab the protocols we need
173 //
174 Status = gBS->OpenProtocol (
175 Handle,
176 &gEfiWinNtIoProtocolGuid,
177 &WinNtIo,
178 This->DriverBindingHandle,
179 Handle,
180 EFI_OPEN_PROTOCOL_BY_DRIVER
181 );
182 if (EFI_ERROR (Status)) {
183 return EFI_UNSUPPORTED;
184 }
185
186 //
187 // Allocate Private context data for SGO inteface.
188 //
189 Private = NULL;
190 Private = AllocatePool (sizeof (GOP_PRIVATE_DATA));
191 if (Private == NULL) {
192 goto Done;
193 }
194 //
195 // Set up context record
196 //
197 Private->Signature = GOP_PRIVATE_DATA_SIGNATURE;
198 Private->Handle = Handle;
199 Private->WinNtThunk = WinNtIo->WinNtThunk;
200
201 Private->ControllerNameTable = NULL;
202
203 AddUnicodeString (
204 "eng",
205 gWinNtGopComponentName.SupportedLanguages,
206 &Private->ControllerNameTable,
207 WinNtIo->EnvString
208 );
209
210 Private->WindowName = WinNtIo->EnvString;
211
212 Status = WinNtGopConstructor (Private);
213 if (EFI_ERROR (Status)) {
214 goto Done;
215 }
216 //
217 // Publish the Gop interface to the world
218 //
219 Status = gBS->InstallMultipleProtocolInterfaces (
220 &Private->Handle,
221 &gEfiGraphicsOutputProtocolGuid,
222 &Private->GraphicsOutput,
223 &gEfiSimpleTextInProtocolGuid,
224 &Private->SimpleTextIn,
225 NULL
226 );
227
228 Done:
229 if (EFI_ERROR (Status)) {
230
231 gBS->CloseProtocol (
232 Handle,
233 &gEfiWinNtIoProtocolGuid,
234 This->DriverBindingHandle,
235 Handle
236 );
237
238 if (Private != NULL) {
239 //
240 // On Error Free back private data
241 //
242 if (Private->ControllerNameTable != NULL) {
243 FreeUnicodeStringTable (Private->ControllerNameTable);
244 }
245
246 FreePool (Private);
247 }
248 }
249
250 return Status;
251 }
252
253
254 /**
255
256
257 @return None
258
259 **/
260 // TODO: This - add argument and description to function comment
261 // TODO: Handle - add argument and description to function comment
262 // TODO: NumberOfChildren - add argument and description to function comment
263 // TODO: ChildHandleBuffer - add argument and description to function comment
264 // TODO: EFI_NOT_STARTED - add return value to function comment
265 // TODO: EFI_DEVICE_ERROR - add return value to function comment
266 EFI_STATUS
267 EFIAPI
268 WinNtGopDriverBindingStop (
269 IN EFI_DRIVER_BINDING_PROTOCOL *This,
270 IN EFI_HANDLE Handle,
271 IN UINTN NumberOfChildren,
272 IN EFI_HANDLE *ChildHandleBuffer
273 )
274 {
275 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
276 EFI_STATUS Status;
277 GOP_PRIVATE_DATA *Private;
278
279 Status = gBS->OpenProtocol (
280 Handle,
281 &gEfiGraphicsOutputProtocolGuid,
282 &GraphicsOutput,
283 This->DriverBindingHandle,
284 Handle,
285 EFI_OPEN_PROTOCOL_GET_PROTOCOL
286 );
287 if (EFI_ERROR (Status)) {
288 //
289 // If the GOP interface does not exist the driver is not started
290 //
291 return EFI_NOT_STARTED;
292 }
293
294 //
295 // Get our private context information
296 //
297 Private = GOP_PRIVATE_DATA_FROM_THIS (GraphicsOutput);
298
299 //
300 // Remove the SGO interface from the system
301 //
302 Status = gBS->UninstallMultipleProtocolInterfaces (
303 Private->Handle,
304 &gEfiGraphicsOutputProtocolGuid,
305 &Private->GraphicsOutput,
306 &gEfiSimpleTextInProtocolGuid,
307 &Private->SimpleTextIn,
308 NULL
309 );
310 if (!EFI_ERROR (Status)) {
311 //
312 // Shutdown the hardware
313 //
314 Status = WinNtGopDestructor (Private);
315 if (EFI_ERROR (Status)) {
316 return EFI_DEVICE_ERROR;
317 }
318
319 gBS->CloseProtocol (
320 Handle,
321 &gEfiWinNtIoProtocolGuid,
322 This->DriverBindingHandle,
323 Handle
324 );
325
326 //
327 // Free our instance data
328 //
329 FreeUnicodeStringTable (Private->ControllerNameTable);
330
331 FreePool (Private);
332
333 }
334
335 return Status;
336 }
337
338
339 /**
340 Convert a unicode string to a UINTN
341
342 @param String Unicode string.
343
344 @return UINTN of the number represented by String.
345
346 **/
347 UINTN
348 Atoi (
349 CHAR16 *String
350 )
351 {
352 UINTN Number;
353 CHAR16 *Str;
354
355 //
356 // skip preceeding white space
357 //
358 Str = String;
359 while ((*Str) && (*Str == ' ' || *Str == '"')) {
360 Str++;
361 }
362
363 //
364 // Convert ot a Number
365 //
366 Number = 0;
367 while (*Str != '\0') {
368 if ((*Str >= '0') && (*Str <= '9')) {
369 Number = (Number * 10) +*Str - '0';
370 } else {
371 break;
372 }
373
374 Str++;
375 }
376
377 return Number;
378 }