]> git.proxmox.com Git - mirror_edk2.git/blob - Nt32Pkg/PlatformBdsDxe/Generic/DeviceMngr/DeviceManager.c
ef72e79309da104e454e16c31cdaab5bf60588ed
[mirror_edk2.git] / Nt32Pkg / PlatformBdsDxe / Generic / DeviceMngr / DeviceManager.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 DeviceManager.c
15
16 Abstract:
17
18 The platform device manager reference implement
19
20 --*/
21 //
22 // Include common header file for this module.
23 //
24 #include "CommonHeader.h"
25 #include "DeviceManager.h"
26
27 STATIC UINT16 mTokenCount;
28 EFI_FRONTPAGE_CALLBACK_INFO FPCallbackInfo;
29 extern UINTN gCallbackKey;
30 extern EFI_FORM_BROWSER_PROTOCOL *gBrowser;
31 extern EFI_GUID gBdsStringPackGuid;
32 extern BOOLEAN gConnectAllHappened;
33
34 STRING_REF gStringTokenTable[] = {
35 STR_VIDEO_DEVICE,
36 STR_NETWORK_DEVICE,
37 STR_INPUT_DEVICE,
38 STR_ON_BOARD_DEVICE,
39 STR_OTHER_DEVICE,
40 STR_EMPTY_STRING,
41 0xFFFF
42 };
43
44 EFI_STATUS
45 EFIAPI
46 DeviceManagerCallbackRoutine (
47 IN EFI_FORM_CALLBACK_PROTOCOL *This,
48 IN UINT16 KeyValue,
49 IN EFI_IFR_DATA_ARRAY *DataArray,
50 OUT EFI_HII_CALLBACK_PACKET **Packet
51 )
52 /*++
53
54 Routine Description:
55
56 This is the function that is called to provide results data to the driver. This data
57 consists of a unique key which is used to identify what data is either being passed back
58 or being asked for.
59
60 Arguments:
61
62 KeyValue - A unique value which is sent to the original exporting driver so that it
63 can identify the type of data to expect. The format of the data tends to
64 vary based on the op-code that geerated the callback.
65
66 Data - A pointer to the data being sent to the original exporting driver.
67
68 Returns:
69
70 --*/
71 {
72 //
73 // The KeyValue corresponds in this case to the handle which was requested to be displayed
74 //
75 EFI_FRONTPAGE_CALLBACK_INFO *CallbackInfo;
76
77 CallbackInfo = EFI_FP_CALLBACK_DATA_FROM_THIS (This);
78 switch (KeyValue) {
79 case 0x2000:
80 CallbackInfo->Data.VideoBIOS = (UINT8) (UINTN) (((EFI_IFR_DATA_ENTRY *)(DataArray + 1))->Data);
81 gRT->SetVariable (
82 L"VBIOS",
83 &gEfiGenericPlatformVariableGuid,
84 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
85 sizeof (UINT8),
86 &CallbackInfo->Data.VideoBIOS
87 );
88 break;
89
90 default:
91 break;
92 }
93
94 gCallbackKey = KeyValue;
95 return EFI_SUCCESS;
96 }
97
98 EFI_STATUS
99 InitializeDeviceManager (
100 VOID
101 )
102 /*++
103
104 Routine Description:
105
106 Initialize HII information for the FrontPage
107
108 Arguments:
109 None
110
111 Returns:
112
113 --*/
114 {
115 EFI_STATUS Status;
116 EFI_HII_PACKAGES *PackageList;
117 EFI_HII_UPDATE_DATA *UpdateData;
118
119 //
120 // Allocate space for creation of UpdateData Buffer
121 //
122 UpdateData = AllocateZeroPool (0x1000);
123 ASSERT (UpdateData != NULL);
124
125 PackageList = PreparePackages (1, &gBdsStringPackGuid, DeviceManagerVfrBin);
126 Status = Hii->NewPack (Hii, PackageList, &FPCallbackInfo.DevMgrHiiHandle);
127 FreePool (PackageList);
128
129 //
130 // This example does not implement worker functions for the NV accessor functions. Only a callback evaluator
131 //
132 FPCallbackInfo.Signature = EFI_FP_CALLBACK_DATA_SIGNATURE;
133 FPCallbackInfo.DevMgrCallback.NvRead = NULL;
134 FPCallbackInfo.DevMgrCallback.NvWrite = NULL;
135 FPCallbackInfo.DevMgrCallback.Callback = DeviceManagerCallbackRoutine;
136
137 //
138 // Install protocol interface
139 //
140 FPCallbackInfo.CallbackHandle = NULL;
141
142 Status = gBS->InstallProtocolInterface (
143 &FPCallbackInfo.CallbackHandle,
144 &gEfiFormCallbackProtocolGuid,
145 EFI_NATIVE_INTERFACE,
146 &FPCallbackInfo.DevMgrCallback
147 );
148
149 ASSERT_EFI_ERROR (Status);
150
151 //
152 // Flag update pending in FormSet
153 //
154 UpdateData->FormSetUpdate = TRUE;
155 //
156 // Register CallbackHandle data for FormSet
157 //
158 UpdateData->FormCallbackHandle = (EFI_PHYSICAL_ADDRESS) (UINTN) FPCallbackInfo.CallbackHandle;
159 //
160 // Simply registering the callback handle
161 //
162 Hii->UpdateForm (Hii, FPCallbackInfo.DevMgrHiiHandle, (EFI_FORM_LABEL) 0x0000, TRUE, UpdateData);
163
164 FreePool (UpdateData);
165 return Status;
166 }
167
168 EFI_STATUS
169 CallDeviceManager (
170 VOID
171 )
172 /*++
173
174 Routine Description:
175
176 Call the browser and display the device manager
177
178 Arguments:
179
180 None
181
182 Returns:
183 EFI_SUCCESS - Operation is successful.
184 EFI_INVALID_PARAMETER - If the inputs to SendForm function is not valid.
185
186 --*/
187 {
188 EFI_STATUS Status;
189 UINTN BufferSize;
190 UINTN Count;
191 EFI_HII_HANDLE Index;
192 UINT8 *Buffer;
193 EFI_IFR_FORM_SET *FormSetData;
194 CHAR16 *String;
195 UINTN StringLength;
196 EFI_HII_UPDATE_DATA *UpdateData;
197 STRING_REF Token;
198 STRING_REF TokenHelp;
199 IFR_OPTION *IfrOptionList;
200 UINT8 *VideoOption;
201 UINTN VideoOptionSize;
202 EFI_HII_HANDLE *HiiHandles;
203 UINT16 HandleBufferLength;
204 BOOLEAN BootDeviceMngrMenuResetRequired;
205
206 IfrOptionList = NULL;
207 VideoOption = NULL;
208 HiiHandles = NULL;
209 HandleBufferLength = 0;
210
211 //
212 // Connect all prior to entering the platform setup menu.
213 //
214 if (!gConnectAllHappened) {
215 BdsLibConnectAllDriversToAllControllers ();
216 gConnectAllHappened = TRUE;
217 }
218 //
219 // Allocate space for creation of UpdateData Buffer
220 //
221 UpdateData = AllocateZeroPool (0x1000);
222 ASSERT (UpdateData != NULL);
223
224 Status = EFI_SUCCESS;
225 Buffer = NULL;
226 FormSetData = NULL;
227 gCallbackKey = 0;
228 if (mTokenCount == 0) {
229 Hii->NewString (Hii, NULL, FPCallbackInfo.DevMgrHiiHandle, &mTokenCount, L" ");
230 }
231
232 Token = mTokenCount;
233 TokenHelp = (UINT16) (Token + 1);
234
235 //
236 // Reset the menu
237 //
238 for (Index = 0, Count = 1; Count < 0x10000; Count <<= 1, Index++) {
239 //
240 // We will strip off all previous menu entries
241 //
242 UpdateData->DataCount = 0xFF;
243
244 //
245 // Erase entries on this label
246 //
247 Hii->UpdateForm (Hii, FPCallbackInfo.DevMgrHiiHandle, (EFI_FORM_LABEL) Count, FALSE, UpdateData);
248
249 //
250 // Did we reach the end of the Token Table?
251 //
252 if (gStringTokenTable[Index] == 0xFFFF) {
253 break;
254 }
255
256 CreateSubTitleOpCode (gStringTokenTable[Index], &UpdateData->Data);
257 //
258 // Add a single menu item - in this case a subtitle for the device type
259 //
260 UpdateData->DataCount = 1;
261
262 //
263 // Add default title for this label
264 //
265 Hii->UpdateForm (Hii, FPCallbackInfo.DevMgrHiiHandle, (EFI_FORM_LABEL) Count, TRUE, UpdateData);
266 }
267 //
268 // Add a space and an exit string. Remember since we add things at the label and push other things beyond the
269 // label down, we add this in reverse order
270 //
271 CreateSubTitleOpCode (STRING_TOKEN (STR_EXIT_STRING), &UpdateData->Data);
272 Hii->UpdateForm (Hii, FPCallbackInfo.DevMgrHiiHandle, (EFI_FORM_LABEL) Count, TRUE, UpdateData);
273 CreateSubTitleOpCode (STR_EMPTY_STRING, &UpdateData->Data);
274 Hii->UpdateForm (Hii, FPCallbackInfo.DevMgrHiiHandle, (EFI_FORM_LABEL) Count, TRUE, UpdateData);
275
276 //
277 // Get all the Hii handles
278 //
279 Status = BdsLibGetHiiHandles (Hii, &HandleBufferLength, &HiiHandles);
280 ASSERT_EFI_ERROR (Status);
281
282 for (Index = 1, BufferSize = 0; Index < HandleBufferLength; Index++) {
283 //
284 // Am not initializing Buffer since the first thing checked is the size
285 // this way I can get the real buffersize in the smallest code size
286 //
287 Status = Hii->GetForms (Hii, Index, 0, &BufferSize, Buffer);
288
289 if (Status != EFI_NOT_FOUND) {
290 //
291 // BufferSize should have the real size of the forms now
292 //
293 Buffer = AllocateZeroPool (BufferSize);
294 ASSERT (Buffer != NULL);
295
296 //
297 // Am not initializing Buffer since the first thing checked is the size
298 // this way I can get the real buffersize in the smallest code size
299 //
300 Status = Hii->GetForms (Hii, Index, 0, &BufferSize, Buffer);
301
302 //
303 // Skip EFI_HII_PACK_HEADER, advance to EFI_IFR_FORM_SET data.
304 //
305 FormSetData = (EFI_IFR_FORM_SET *) (Buffer + sizeof (EFI_HII_PACK_HEADER));
306
307 //
308 // If this formset belongs in the device manager, add it to the menu
309 //
310 if (FormSetData->Class != EFI_NON_DEVICE_CLASS) {
311
312 StringLength = 0x1000;
313 String = AllocateZeroPool (StringLength);
314 ASSERT (String != NULL);
315
316 Status = Hii->GetString (Hii, Index, FormSetData->FormSetTitle, TRUE, NULL, &StringLength, String);
317 Status = Hii->NewString (Hii, NULL, FPCallbackInfo.DevMgrHiiHandle, &Token, String);
318
319 //
320 // If token value exceeded real token value - we need to add a new token values
321 //
322 if (Status == EFI_INVALID_PARAMETER) {
323 Token = 0;
324 TokenHelp = 0;
325 Status = Hii->NewString (Hii, NULL, FPCallbackInfo.DevMgrHiiHandle, &Token, String);
326 }
327
328 StringLength = 0x1000;
329 if (FormSetData->Help == 0) {
330 TokenHelp = 0;
331 } else {
332 Status = Hii->GetString (Hii, Index, FormSetData->Help, TRUE, NULL, &StringLength, String);
333 if (StringLength == 0x02) {
334 TokenHelp = 0;
335 } else {
336 Status = Hii->NewString (Hii, NULL, FPCallbackInfo.DevMgrHiiHandle, &TokenHelp, String);
337 if (Status == EFI_INVALID_PARAMETER) {
338 TokenHelp = 0;
339 Status = Hii->NewString (Hii, NULL, FPCallbackInfo.DevMgrHiiHandle, &TokenHelp, String);
340 }
341 }
342 }
343
344 FreePool (String);
345
346 CreateGotoOpCode (
347 0x1000, // Device Manager Page
348 Token, // Description String Token
349 TokenHelp, // Description Help String Token
350 EFI_IFR_FLAG_INTERACTIVE | EFI_IFR_FLAG_NV_ACCESS, // Flag designating callback is active
351 (UINT16) Index, // Callback key value
352 &UpdateData->Data // Buffer to fill with op-code
353 );
354
355 //
356 // In the off-chance that we have lots of extra tokens allocated to the DeviceManager
357 // this ensures we are fairly re-using the tokens instead of constantly growing the token
358 // storage for this one handle. If we incremented the token value beyond what it normally
359 // would use, we will fall back into the error path which seeds the token value with a 0
360 // so that we can correctly add a token value.
361 //
362 if (TokenHelp == 0) {
363 //
364 // Since we didn't add help, only advance Token by 1
365 //
366 Token++;
367 } else {
368 Token = (UINT16) (Token + 2);
369 TokenHelp = (UINT16) (TokenHelp + 2);
370 }
371 //
372 // This for loop basically will take the Class value which is a bitmask and
373 // update the form for every active bit. There will be a label at each bit
374 // location. So if someone had a device which a class of EFI_DISK_DEVICE_CLASS |
375 // EFI_ON_BOARD_DEVICE_CLASS, this routine will unwind that mask and drop the menu entry
376 // on each corresponding label.
377 //
378 for (Count = 1; Count < 0x10000; Count <<= 1) {
379 //
380 // This is an active bit, so update the form
381 //
382 if (FormSetData->Class & Count) {
383 Hii->UpdateForm (
384 Hii,
385 FPCallbackInfo.DevMgrHiiHandle,
386 (EFI_FORM_LABEL) (FormSetData->Class & Count),
387 TRUE,
388 UpdateData
389 );
390 }
391 }
392 }
393
394 BufferSize = 0;
395 //
396 // Reset Buffer pointer to original location
397 //
398 FreePool (Buffer);
399 }
400 }
401 //
402 // Add oneof for video BIOS selection
403 //
404 VideoOption = BdsLibGetVariableAndSize (
405 L"VBIOS",
406 &gEfiGenericPlatformVariableGuid,
407 &VideoOptionSize
408 );
409 if (NULL == VideoOption) {
410 FPCallbackInfo.Data.VideoBIOS = 0;
411 } else {
412 FPCallbackInfo.Data.VideoBIOS = VideoOption[0];
413 FreePool (VideoOption);
414 }
415
416 ASSERT (FPCallbackInfo.Data.VideoBIOS <= 1);
417
418 IfrOptionList = AllocatePool (2 * sizeof (IFR_OPTION));
419 if (IfrOptionList != NULL) {
420 IfrOptionList[0].Flags = EFI_IFR_FLAG_INTERACTIVE;
421 IfrOptionList[0].Key = SET_VIDEO_BIOS_TYPE_QUESTION_ID + 0x2000;
422 IfrOptionList[0].StringToken = STRING_TOKEN (STR_ONE_OF_PCI);
423 IfrOptionList[0].Value = 0;
424 IfrOptionList[0].OptionString = NULL;
425 IfrOptionList[1].Flags = EFI_IFR_FLAG_INTERACTIVE;
426 IfrOptionList[1].Key = SET_VIDEO_BIOS_TYPE_QUESTION_ID + 0x2000;
427 IfrOptionList[1].StringToken = STRING_TOKEN (STR_ONE_OF_AGP);
428 IfrOptionList[1].Value = 1;
429 IfrOptionList[1].OptionString = NULL;
430 IfrOptionList[FPCallbackInfo.Data.VideoBIOS].Flags |= EFI_IFR_FLAG_DEFAULT;
431
432 CreateOneOfOpCode (
433 SET_VIDEO_BIOS_TYPE_QUESTION_ID,
434 (UINT8) 1,
435 STRING_TOKEN (STR_ONE_OF_VBIOS),
436 STRING_TOKEN (STR_ONE_OF_VBIOS_HELP),
437 IfrOptionList,
438 2,
439 &UpdateData->Data
440 );
441
442 UpdateData->DataCount = 4;
443 Hii->UpdateForm (Hii, FPCallbackInfo.DevMgrHiiHandle, (EFI_FORM_LABEL) EFI_VBIOS_CLASS, TRUE, UpdateData);
444 FreePool (IfrOptionList);
445 }
446
447 BootDeviceMngrMenuResetRequired = FALSE;
448 Status = gBrowser->SendForm (
449 gBrowser,
450 TRUE, // Use the database
451 &FPCallbackInfo.DevMgrHiiHandle, // The HII Handle
452 1,
453 NULL,
454 FPCallbackInfo.CallbackHandle,
455 (UINT8 *) &FPCallbackInfo.Data,
456 NULL,
457 &BootDeviceMngrMenuResetRequired
458 );
459
460 if (BootDeviceMngrMenuResetRequired) {
461 EnableResetRequired ();
462 }
463
464 Hii->ResetStrings (Hii, FPCallbackInfo.DevMgrHiiHandle);
465
466 //
467 // We will have returned from processing a callback - user either hit ESC to exit, or selected
468 // a target to display
469 //
470 if (gCallbackKey != 0 && gCallbackKey < 0x2000) {
471 BootDeviceMngrMenuResetRequired = FALSE;
472 Status = gBrowser->SendForm (
473 gBrowser,
474 TRUE, // Use the database
475 (EFI_HII_HANDLE *) &gCallbackKey, // The HII Handle
476 1,
477 NULL,
478 NULL, // This is the handle that the interface to the callback was installed on
479 NULL,
480 NULL,
481 &BootDeviceMngrMenuResetRequired
482 );
483
484 if (BootDeviceMngrMenuResetRequired) {
485 EnableResetRequired ();
486 }
487 //
488 // Force return to Device Manager
489 //
490 gCallbackKey = 4;
491 }
492
493 if (gCallbackKey >= 0x2000) {
494 gCallbackKey = 4;
495 }
496
497 FreePool (UpdateData);
498 FreePool (HiiHandles);
499
500 return Status;
501 }