summaryrefslogtreecommitdiff
path: root/EdkNt32Pkg/Dxe/PlatformBds/Generic/DeviceMngr/DeviceManager.c
blob: 3f648b7de6d43eb979b706cd3591758fdb51485b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*++

Copyright (c) 2006, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name: 

  DeviceManager.c

Abstract:

  The platform device manager reference implement

--*/
#include "DeviceManager.h"

STATIC UINT16                     mTokenCount;
EFI_FRONTPAGE_CALLBACK_INFO       FPCallbackInfo;
extern UINTN                      gCallbackKey;
extern EFI_FORM_BROWSER_PROTOCOL  *gBrowser;
extern EFI_GUID                   gBdsStringPackGuid;
extern BOOLEAN                    gConnectAllHappened;

STRING_REF                        gStringTokenTable[] = {
  STR_VIDEO_DEVICE,
  STR_NETWORK_DEVICE,
  STR_INPUT_DEVICE,
  STR_ON_BOARD_DEVICE,
  STR_OTHER_DEVICE,
  STR_EMPTY_STRING,
  0xFFFF
};

EFI_STATUS
EFIAPI
DeviceManagerCallbackRoutine (
  IN EFI_FORM_CALLBACK_PROTOCOL       *This,
  IN UINT16                           KeyValue,
  IN EFI_IFR_DATA_ARRAY               *DataArray,
  OUT EFI_HII_CALLBACK_PACKET         **Packet
  )
/*++

Routine Description:

  This is the function that is called to provide results data to the driver.  This data
  consists of a unique key which is used to identify what data is either being passed back
  or being asked for. 

Arguments:

  KeyValue -        A unique value which is sent to the original exporting driver so that it
                    can identify the type of data to expect.  The format of the data tends to
                    vary based on the op-code that geerated the callback.

  Data -            A pointer to the data being sent to the original exporting driver.

Returns: 

--*/
{
  //
  // The KeyValue corresponds in this case to the handle which was requested to be displayed
  //
  EFI_FRONTPAGE_CALLBACK_INFO *CallbackInfo;

  CallbackInfo = EFI_FP_CALLBACK_DATA_FROM_THIS (This);
  switch (KeyValue) {
  case 0x2000:
    CallbackInfo->Data.VideoBIOS = (UINT8) (UINTN) (((EFI_IFR_DATA_ENTRY *)(DataArray + 1))->Data);
    gRT->SetVariable (
          L"VBIOS",
          &gEfiGlobalVariableGuid,
          EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
          sizeof (UINT8),
          &CallbackInfo->Data.VideoBIOS
          );
    break;

  default:
    break;
  }

  gCallbackKey = KeyValue;
  return EFI_SUCCESS;
}

EFI_STATUS
InitializeDeviceManager (
  VOID
  )
/*++

Routine Description:

  Initialize HII information for the FrontPage

Arguments:
  None
            
Returns:

--*/
{
  EFI_STATUS          Status;
  EFI_HII_PACKAGES    *PackageList;
  EFI_HII_UPDATE_DATA *UpdateData;

  //
  // Allocate space for creation of UpdateData Buffer
  //
  UpdateData = AllocateZeroPool (0x1000);
  ASSERT (UpdateData != NULL);

  PackageList = PreparePackages (1, &gBdsStringPackGuid, DeviceManagerVfrBin);
  Status      = Hii->NewPack (Hii, PackageList, &FPCallbackInfo.DevMgrHiiHandle);
  gBS->FreePool (PackageList);

  //
  // This example does not implement worker functions for the NV accessor functions.  Only a callback evaluator
  //
  FPCallbackInfo.Signature                = EFI_FP_CALLBACK_DATA_SIGNATURE;
  FPCallbackInfo.DevMgrCallback.NvRead    = NULL;
  FPCallbackInfo.DevMgrCallback.NvWrite   = NULL;
  FPCallbackInfo.DevMgrCallback.Callback  = DeviceManagerCallbackRoutine;

  //
  // Install protocol interface
  //
  FPCallbackInfo.CallbackHandle = NULL;

  Status = gBS->InstallProtocolInterface (
                  &FPCallbackInfo.CallbackHandle,
                  &gEfiFormCallbackProtocolGuid,
                  EFI_NATIVE_INTERFACE,
                  &FPCallbackInfo.DevMgrCallback
                  );

  ASSERT_EFI_ERROR (Status);

  //
  // Flag update pending in FormSet
  //
  UpdateData->FormSetUpdate = TRUE;
  //
  // Register CallbackHandle data for FormSet
  //
  UpdateData->FormCallbackHandle = (EFI_PHYSICAL_ADDRESS) (UINTN) FPCallbackInfo.CallbackHandle;
  //
  // Simply registering the callback handle
  //
  Hii->UpdateForm (Hii, FPCallbackInfo.DevMgrHiiHandle, (EFI_FORM_LABEL) 0x0000, TRUE, UpdateData);

  gBS->FreePool (UpdateData);
  return Status;
}

EFI_STATUS
CallDeviceManager (
  VOID
  )
/*++

Routine Description:
  
  Call the browser and display the device manager

Arguments:
  
  None
  
Returns:
  EFI_SUCCESS            - Operation is successful.
  EFI_INVALID_PARAMETER  - If the inputs to SendForm function is not valid.
  
--*/
{
  EFI_STATUS          Status;
  UINTN               BufferSize;
  UINTN               Count;
  EFI_HII_HANDLE      Index;
  UINT8               *Buffer;
  EFI_IFR_FORM_SET    *FormSetData;
  CHAR16              *String;
  UINTN               StringLength;
  EFI_HII_UPDATE_DATA *UpdateData;
  STRING_REF          Token;
  STRING_REF          TokenHelp;
  IFR_OPTION          *IfrOptionList;
  UINT8               *VideoOption;
  UINTN               VideoOptionSize;
  EFI_HII_HANDLE      *HiiHandles;
  UINT16              HandleBufferLength;
  BOOLEAN	          BootDeviceMngrMenuResetRequired;

  IfrOptionList       = NULL;
  VideoOption         = NULL;
  HandleBufferLength  = 0;

  //
  // Connect all prior to entering the platform setup menu.
  //
  if (!gConnectAllHappened) {
    BdsLibConnectAllDriversToAllControllers ();
    gConnectAllHappened = TRUE;
  }
  //
  // Allocate space for creation of UpdateData Buffer
  //
  UpdateData = AllocateZeroPool (0x1000);
  ASSERT (UpdateData != NULL);

  Status        = EFI_SUCCESS;
  Buffer        = NULL;
  FormSetData   = NULL;
  gCallbackKey  = 0;
  if (mTokenCount == 0) {
    Hii->NewString (Hii, NULL, FPCallbackInfo.DevMgrHiiHandle, &mTokenCount, L" ");
  }

  Token     = mTokenCount;
  TokenHelp = (UINT16) (Token + 1);

  //
  // Reset the menu
  //
  for (Index = 0, Count = 1; Count < 0x10000; Count <<= 1, Index++) {
    //
    // We will strip off all previous menu entries
    //
    UpdateData->DataCount = 0xFF;

    //
    // Erase entries on this label
    //
    Hii->UpdateForm (Hii, FPCallbackInfo.DevMgrHiiHandle, (EFI_FORM_LABEL) Count, FALSE, UpdateData);

    //
    // Did we reach the end of the Token Table?
    //
    if (gStringTokenTable[Index] == 0xFFFF) {
      break;
    }

    CreateSubTitleOpCode (gStringTokenTable[Index], &UpdateData->Data);
    //
    // Add a single menu item - in this case a subtitle for the device type
    //
    UpdateData->DataCount = 1;

    //
    // Add default title for this label
    //
    Hii->UpdateForm (Hii, FPCallbackInfo.DevMgrHiiHandle, (EFI_FORM_LABEL) Count, TRUE, UpdateData);
  }
  //
  // Add a space and an exit string.  Remember since we add things at the label and push other things beyond the
  // label down, we add this in reverse order
  //
  CreateSubTitleOpCode (STRING_TOKEN (STR_EXIT_STRING), &UpdateData->Data);
  Hii->UpdateForm (Hii, FPCallbackInfo.DevMgrHiiHandle, (EFI_FORM_LABEL) Count, TRUE, UpdateData);
  CreateSubTitleOpCode (STR_EMPTY_STRING, &UpdateData->Data);
  Hii->UpdateForm (Hii, FPCallbackInfo.DevMgrHiiHandle, (EFI_FORM_LABEL) Count, TRUE, UpdateData);

  HiiHandles = AllocateZeroPool (HandleBufferLength);
  Hii->FindHandles (Hii, &HandleBufferLength, HiiHandles);

  for (Index = 1, BufferSize = 0; Index < HandleBufferLength; Index++) {
    //
    // Am not initializing Buffer since the first thing checked is the size
    // this way I can get the real buffersize in the smallest code size
    //
    Status = Hii->GetForms (Hii, Index, 0, &BufferSize, Buffer);

    if (Status != EFI_NOT_FOUND) {
      //
      // BufferSize should have the real size of the forms now
      //
      Buffer = AllocateZeroPool (BufferSize);
      ASSERT (Buffer != NULL);

      //
      // Am not initializing Buffer since the first thing checked is the size
      // this way I can get the real buffersize in the smallest code size
      //
      Status = Hii->GetForms (Hii, Index, 0, &BufferSize, Buffer);

      //
      // Skip EFI_HII_PACK_HEADER, advance to EFI_IFR_FORM_SET data.
      //
      FormSetData = (EFI_IFR_FORM_SET *) (Buffer + sizeof (EFI_HII_PACK_HEADER));

      //
      // If this formset belongs in the device manager, add it to the menu
      //
      if (FormSetData->Class != EFI_NON_DEVICE_CLASS) {

        StringLength  = 0x1000;
        String        = AllocateZeroPool (StringLength);
        ASSERT (String != NULL);

        Status  = Hii->GetString (Hii, Index, FormSetData->FormSetTitle, TRUE, NULL, &StringLength, String);
        Status  = Hii->NewString (Hii, NULL, FPCallbackInfo.DevMgrHiiHandle, &Token, String);

        //
        // If token value exceeded real token value - we need to add a new token values
        //
        if (Status == EFI_INVALID_PARAMETER) {
          Token     = 0;
          TokenHelp = 0;
          Status    = Hii->NewString (Hii, NULL, FPCallbackInfo.DevMgrHiiHandle, &Token, String);
        }

        StringLength = 0x1000;
        if (FormSetData->Help == 0) {
          TokenHelp = 0;
        } else {
          Status = Hii->GetString (Hii, Index, FormSetData->Help, TRUE, NULL, &StringLength, String);
          if (StringLength == 0x02) {
            TokenHelp = 0;
          } else {
            Status = Hii->NewString (Hii, NULL, FPCallbackInfo.DevMgrHiiHandle, &TokenHelp, String);
            if (Status == EFI_INVALID_PARAMETER) {
              TokenHelp = 0;
              Status    = Hii->NewString (Hii, NULL, FPCallbackInfo.DevMgrHiiHandle, &TokenHelp, String);
            }
          }
        }

        gBS->FreePool (String);

        CreateGotoOpCode (
          0x1000,     // Device Manager Page
          Token,      // Description String Token
          TokenHelp,  // Description Help String Token
          EFI_IFR_FLAG_INTERACTIVE | EFI_IFR_FLAG_NV_ACCESS,  // Flag designating callback is active
          (UINT16) Index,                                     // Callback key value
          &UpdateData->Data                                   // Buffer to fill with op-code
          );

        //
        // In the off-chance that we have lots of extra tokens allocated to the DeviceManager
        // this ensures we are fairly re-using the tokens instead of constantly growing the token
        // storage for this one handle.  If we incremented the token value beyond what it normally
        // would use, we will fall back into the error path which seeds the token value with a 0
        // so that we can correctly add a token value.
        //
        if (TokenHelp == 0) {
          //
          // Since we didn't add help, only advance Token by 1
          //
          Token++;
        } else {
          Token     = (UINT16) (Token + 2);
          TokenHelp = (UINT16) (TokenHelp + 2);
        }
        //
        // This for loop basically will take the Class value which is a bitmask and
        // update the form for every active bit.  There will be a label at each bit
        // location.  So if someone had a device which a class of EFI_DISK_DEVICE_CLASS |
        // EFI_ON_BOARD_DEVICE_CLASS, this routine will unwind that mask and drop the menu entry
        // on each corresponding label.
        //
        for (Count = 1; Count < 0x10000; Count <<= 1) {
          //
          // This is an active bit, so update the form
          //
          if (FormSetData->Class & Count) {
            Hii->UpdateForm (
                  Hii,
                  FPCallbackInfo.DevMgrHiiHandle,
                  (EFI_FORM_LABEL) (FormSetData->Class & Count),
                  TRUE,
                  UpdateData
                  );
          }
        }
      }

      BufferSize = 0;
      //
      // Reset Buffer pointer to original location
      //
      gBS->FreePool (Buffer);
    }
  }
  //
  // Add oneof for video BIOS selection
  //
  VideoOption = BdsLibGetVariableAndSize (
                  L"VBIOS",
                  &gEfiGlobalVariableGuid,
                  &VideoOptionSize
                  );
  if (NULL == VideoOption) {
    FPCallbackInfo.Data.VideoBIOS = 0;
  } else {
    FPCallbackInfo.Data.VideoBIOS = VideoOption[0];
    gBS->FreePool (VideoOption);
  }

  ASSERT (FPCallbackInfo.Data.VideoBIOS <= 1);

  Status = gBS->AllocatePool (EfiBootServicesData, 2 * sizeof (IFR_OPTION), &IfrOptionList);
  if (IfrOptionList != NULL) {
    IfrOptionList[0].Flags        = EFI_IFR_FLAG_INTERACTIVE;
    IfrOptionList[0].Key          = SET_VIDEO_BIOS_TYPE_QUESTION_ID + 0x2000;
    IfrOptionList[0].StringToken  = STRING_TOKEN (STR_ONE_OF_PCI);
    IfrOptionList[0].Value        = 0;
    IfrOptionList[0].OptionString = NULL;
    IfrOptionList[1].Flags        = EFI_IFR_FLAG_INTERACTIVE;
    IfrOptionList[1].Key          = SET_VIDEO_BIOS_TYPE_QUESTION_ID + 0x2000;
    IfrOptionList[1].StringToken  = STRING_TOKEN (STR_ONE_OF_AGP);
    IfrOptionList[1].Value        = 1;
    IfrOptionList[1].OptionString = NULL;
    IfrOptionList[FPCallbackInfo.Data.VideoBIOS].Flags |= EFI_IFR_FLAG_DEFAULT;

    CreateOneOfOpCode (
      SET_VIDEO_BIOS_TYPE_QUESTION_ID,
      (UINT8) 1,
      STRING_TOKEN (STR_ONE_OF_VBIOS),
      STRING_TOKEN (STR_ONE_OF_VBIOS_HELP),
      IfrOptionList,
      2,
      &UpdateData->Data
      );

    UpdateData->DataCount = 4;
    Hii->UpdateForm (Hii, FPCallbackInfo.DevMgrHiiHandle, (EFI_FORM_LABEL) EFI_VBIOS_CLASS, TRUE, UpdateData);
    gBS->FreePool (IfrOptionList);
  }

  BootDeviceMngrMenuResetRequired = FALSE;
  Status = gBrowser->SendForm (
                      gBrowser,
                      TRUE,                             // Use the database
                      &FPCallbackInfo.DevMgrHiiHandle,  // The HII Handle
                      1,
                      NULL,
                      FPCallbackInfo.CallbackHandle,
                      (UINT8 *) &FPCallbackInfo.Data,
                      NULL,
                      &BootDeviceMngrMenuResetRequired
                      );

  if (BootDeviceMngrMenuResetRequired) {
    EnableResetRequired ();
  }

  Hii->ResetStrings (Hii, FPCallbackInfo.DevMgrHiiHandle);

  //
  // We will have returned from processing a callback - user either hit ESC to exit, or selected
  // a target to display
  //
  if (gCallbackKey != 0 && gCallbackKey < 0x2000) {
    BootDeviceMngrMenuResetRequired = FALSE;
    Status = gBrowser->SendForm (
                        gBrowser,
                        TRUE,                             // Use the database
                        (EFI_HII_HANDLE *) &gCallbackKey, // The HII Handle
                        1,
                        NULL,
                        NULL,                             // This is the handle that the interface to the callback was installed on
                        NULL,
                        NULL,
                        &BootDeviceMngrMenuResetRequired
                        );

    if (BootDeviceMngrMenuResetRequired) {
      EnableResetRequired ();
    }
    //
    // Force return to Device Manager
    //
    gCallbackKey = 4;
  }

  if (gCallbackKey >= 0x2000) {
    gCallbackKey = 4;
  }

  gBS->FreePool (UpdateData);

  return Status;
}