Import Usb/UsbBusDxe and Usb/UsbMassStorageDxe into MdeModulePkg.
[mirror_edk2.git] / EdkNt32Pkg / Library / EdkGenericBdsLib / BdsConsole.c
1 /*++
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 BdsConsole.c
15
16 Abstract:
17
18 BDS Lib functions which contain all the code to connect console device
19
20 --*/
21
22 BOOLEAN
23 IsNvNeed (
24 IN CHAR16 *ConVarName
25 )
26 {
27 CHAR16 *Ptr;
28
29 Ptr = ConVarName;
30
31 //
32 // If the variable includes "Dev" at last, we consider
33 // it does not support NV attribute.
34 //
35 while (*Ptr) {
36 Ptr++;
37 }
38
39 if ((*(Ptr - 3) == 'D') && (*(Ptr - 2) == 'e') && (*(Ptr - 1) == 'v')) {
40 return FALSE;
41 } else {
42 return TRUE;
43 }
44 }
45
46 EFI_STATUS
47 BdsLibUpdateConsoleVariable (
48 IN CHAR16 *ConVarName,
49 IN EFI_DEVICE_PATH_PROTOCOL *CustomizedConDevicePath,
50 IN EFI_DEVICE_PATH_PROTOCOL *ExclusiveDevicePath
51 )
52 /*++
53
54 Routine Description:
55
56 This function update console variable based on ConVarName, it can
57 add or remove one specific console device path from the variable
58
59 Arguments:
60
61 ConVarName - Console related variable name, ConIn, ConOut, ErrOut.
62
63 CustomizedConDevicePath - The console device path which will be added to
64 the console variable ConVarName, this parameter
65 can not be multi-instance.
66
67 ExclusiveDevicePath - The console device path which will be removed
68 from the console variable ConVarName, this
69 parameter can not be multi-instance.
70
71 Returns:
72
73 EFI_UNSUPPORTED - Add or remove the same device path.
74
75 EFI_SUCCESS - Success add or remove the device path from
76 the console variable.
77
78 --*/
79 {
80 EFI_STATUS Status;
81 EFI_DEVICE_PATH_PROTOCOL *VarConsole;
82 UINTN DevicePathSize;
83 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
84 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;
85 UINT32 Attributes;
86
87 VarConsole = NULL;
88 DevicePathSize = 0;
89 Status = EFI_UNSUPPORTED;
90
91 //
92 // Notes: check the device path point, here should check
93 // with compare memory
94 //
95 if (CustomizedConDevicePath == ExclusiveDevicePath) {
96 return EFI_UNSUPPORTED;
97 }
98 //
99 // Delete the ExclusiveDevicePath from current default console
100 //
101 VarConsole = BdsLibGetVariableAndSize (
102 ConVarName,
103 &gEfiGlobalVariableGuid,
104 &DevicePathSize
105 );
106
107 //
108 // Initialize NewDevicePath
109 //
110 NewDevicePath = VarConsole;
111
112 //
113 // If ExclusiveDevicePath is even the part of the instance in VarConsole, delete it.
114 // In the end, NewDevicePath is the final device path.
115 //
116 if (ExclusiveDevicePath != NULL && VarConsole != NULL) {
117 NewDevicePath = BdsLibDelPartMatchInstance (VarConsole, ExclusiveDevicePath);
118 }
119 //
120 // Try to append customized device path to NewDevicePath.
121 //
122 if (CustomizedConDevicePath != NULL) {
123 if (!BdsLibMatchDevicePaths (NewDevicePath, CustomizedConDevicePath)) {
124 //
125 // Check if there is part of CustomizedConDevicePath in NewDevicePath, delete it.
126 //
127 NewDevicePath = BdsLibDelPartMatchInstance (NewDevicePath, CustomizedConDevicePath);
128 //
129 // In the first check, the default console variable will be null,
130 // just append current customized device path
131 //
132 TempNewDevicePath = NewDevicePath;
133 NewDevicePath = AppendDevicePathInstance (NewDevicePath, CustomizedConDevicePath);
134 BdsLibSafeFreePool(TempNewDevicePath);
135 }
136 }
137
138 //
139 // The attribute for ConInDev, ConOutDev and ErrOutDev does not include NV.
140 //
141 if (IsNvNeed(ConVarName)) {
142 //
143 // ConVarName has NV attribute.
144 //
145 Attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE;
146 } else {
147 //
148 // ConVarName does not have NV attribute.
149 //
150 Attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
151 }
152
153 //
154 // Finally, Update the variable of the default console by NewDevicePath
155 //
156 gRT->SetVariable (
157 ConVarName,
158 &gEfiGlobalVariableGuid,
159 Attributes,
160 GetDevicePathSize (NewDevicePath),
161 NewDevicePath
162 );
163
164 if (VarConsole == NewDevicePath) {
165 BdsLibSafeFreePool(VarConsole);
166 } else {
167 BdsLibSafeFreePool(VarConsole);
168 BdsLibSafeFreePool(NewDevicePath);
169 }
170
171 return EFI_SUCCESS;
172
173 }
174
175 EFI_STATUS
176 BdsLibConnectConsoleVariable (
177 IN CHAR16 *ConVarName
178 )
179 /*++
180
181 Routine Description:
182
183 Connect the console device base on the variable ConVarName, if
184 device path of the ConVarName is multi-instance device path, if
185 anyone of the instances is connected success, then this function
186 will return success.
187
188 Arguments:
189
190 ConVarName - Console related variable name, ConIn, ConOut, ErrOut.
191
192 Returns:
193
194 EFI_NOT_FOUND - There is not any console devices connected success
195
196 EFI_SUCCESS - Success connect any one instance of the console
197 device path base on the variable ConVarName.
198
199 --*/
200 {
201 EFI_STATUS Status;
202 EFI_DEVICE_PATH_PROTOCOL *StartDevicePath;
203 UINTN VariableSize;
204 EFI_DEVICE_PATH_PROTOCOL *Instance;
205 EFI_DEVICE_PATH_PROTOCOL *Next;
206 EFI_DEVICE_PATH_PROTOCOL *CopyOfDevicePath;
207 UINTN Size;
208 BOOLEAN DeviceExist;
209
210 Status = EFI_SUCCESS;
211 DeviceExist = FALSE;
212
213 //
214 // Check if the console variable exist
215 //
216 StartDevicePath = BdsLibGetVariableAndSize (
217 ConVarName,
218 &gEfiGlobalVariableGuid,
219 &VariableSize
220 );
221 if (StartDevicePath == NULL) {
222 return EFI_UNSUPPORTED;
223 }
224
225 CopyOfDevicePath = StartDevicePath;
226 do {
227 //
228 // Check every instance of the console variable
229 //
230 Instance = GetNextDevicePathInstance (&CopyOfDevicePath, &Size);
231 Next = Instance;
232 while (!IsDevicePathEndType (Next)) {
233 Next = NextDevicePathNode (Next);
234 }
235
236 SetDevicePathEndNode (Next);
237
238 //
239 // Connect the instance device path
240 //
241 Status = BdsLibConnectDevicePath (Instance);
242 if (EFI_ERROR (Status)) {
243 //
244 // Delete the instance from the console varialbe
245 //
246 BdsLibUpdateConsoleVariable (ConVarName, NULL, Instance);
247 } else {
248 DeviceExist = TRUE;
249 }
250 BdsLibSafeFreePool(Instance);
251 } while (CopyOfDevicePath != NULL);
252
253 FreePool (StartDevicePath);
254
255 if (DeviceExist == FALSE) {
256 return EFI_NOT_FOUND;
257 }
258
259 return EFI_SUCCESS;
260 }
261
262 VOID
263 BdsLibConnectAllConsoles (
264 VOID
265 )
266 /*++
267
268 Routine Description:
269
270 This function will search every simpletxt devive in current system,
271 and make every simpletxt device as pertantial console device.
272
273 Arguments:
274
275 None
276
277 Returns:
278
279 None
280
281 --*/
282 {
283 EFI_STATUS Status;
284 UINTN Index;
285 EFI_DEVICE_PATH_PROTOCOL *ConDevicePath;
286 UINTN HandleCount;
287 EFI_HANDLE *HandleBuffer;
288
289 Index = 0;
290 HandleCount = 0;
291 HandleBuffer = NULL;
292 ConDevicePath = NULL;
293
294 //
295 // Update all the console varables
296 //
297 Status = gBS->LocateHandleBuffer (
298 ByProtocol,
299 &gEfiSimpleTextInProtocolGuid,
300 NULL,
301 &HandleCount,
302 &HandleBuffer
303 );
304 for (Index = 0; Index < HandleCount; Index++) {
305 Status = gBS->HandleProtocol (
306 HandleBuffer[Index],
307 &gEfiDevicePathProtocolGuid,
308 (VOID **) &ConDevicePath
309 );
310 BdsLibUpdateConsoleVariable (L"ConIn", ConDevicePath, NULL);
311 }
312
313 BdsLibSafeFreePool(HandleBuffer);
314
315 Status = gBS->LocateHandleBuffer (
316 ByProtocol,
317 &gEfiSimpleTextOutProtocolGuid,
318 NULL,
319 &HandleCount,
320 &HandleBuffer
321 );
322 for (Index = 0; Index < HandleCount; Index++) {
323 Status = gBS->HandleProtocol (
324 HandleBuffer[Index],
325 &gEfiDevicePathProtocolGuid,
326 (VOID **) &ConDevicePath
327 );
328 BdsLibUpdateConsoleVariable (L"ConOut", ConDevicePath, NULL);
329 BdsLibUpdateConsoleVariable (L"ErrOut", ConDevicePath, NULL);
330 }
331
332 BdsLibSafeFreePool(HandleBuffer);
333
334 //
335 // Connect all console variables
336 //
337 BdsLibConnectAllDefaultConsoles ();
338
339 }
340
341 EFI_STATUS
342 BdsLibConnectAllDefaultConsoles (
343 VOID
344 )
345 /*++
346
347 Routine Description:
348
349 This function will connect console device base on the console
350 device variable ConIn, ConOut and ErrOut.
351
352 Arguments:
353
354 None
355
356 Returns:
357
358 EFI_SUCCESS - At least one of the ConIn and ConOut device have
359 been connected success.
360
361 EFI_STATUS - Return the status of BdsLibConnectConsoleVariable ().
362
363 --*/
364 {
365 EFI_STATUS Status;
366
367 //
368 // Connect all default console variables
369 //
370
371 //
372 // Because possibly the platform is legacy free, in such case,
373 // ConIn devices (Serial Port and PS2 Keyboard ) does not exist,
374 // so we need not check the status.
375 //
376 BdsLibConnectConsoleVariable (L"ConIn");
377
378 //
379 // It seems impossible not to have any ConOut device on platform,
380 // so we check the status here.
381 //
382 Status = BdsLibConnectConsoleVariable (L"ConOut");
383 if (EFI_ERROR (Status)) {
384 return Status;
385 }
386 //
387 // Special treat the err out device, becaues the null
388 // err out var is legal.
389 //
390 BdsLibConnectConsoleVariable (L"ErrOut");
391
392 return EFI_SUCCESS;
393
394 }