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