summaryrefslogtreecommitdiffstats
path: root/IntelFrameworkModulePkg/Universal/DriverSampleDxe/DriverSample.c
blob: 6514aa70d409d5199dccf4b8208109cc75d5ceb8 (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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*++
Copyright (c) 2006 - 2007, 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:
  DriverSample.c

Abstract:

  This is an example of how a driver might export data to the HII protocol to be
  later utilized by the Setup Protocol

--*/

#include "DriverSample.h"

#define DISPLAY_ONLY_MY_ITEM  0x0001

#define STRING_PACK_GUID \
  { \
    0x8160a85f, 0x934d, 0x468b, { 0xa2, 0x35, 0x72, 0x89, 0x59, 0x14, 0xf6, 0xfc } \
  }

EFI_GUID  mFormSetGuid    = FORMSET_GUID;
EFI_GUID  mStringPackGuid = STRING_PACK_GUID;

STATIC
EFI_STATUS
EFIAPI
DriverCallback (
  IN EFI_FORM_CALLBACK_PROTOCOL       *This,
  IN UINT16                           KeyValue,
  IN EFI_IFR_DATA_ARRAY               *Data,
  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:

--*/
{
  EFI_CALLBACK_INFO       *Private;
  EFI_HII_UPDATE_DATA     *UpdateData;
  UINT8                   *Location;
  EFI_HII_CALLBACK_PACKET *DataPacket;
  UINT16                  Value;
  CHAR16                  VariableName[40];
  STATIC UINT16           QuestionId = 0;
  IFR_OPTION              *OptionList;
  UINTN                   Index;
  MyIfrNVData             NVStruc;

  Private     = EFI_CALLBACK_INFO_FROM_THIS (This);

  //
  // This should tell me the first offset AFTER the end of the compiled NV map
  // If op-code results are not going to be saved to NV locations ensure the QuestionId
  // is beyond the end of the NVRAM mapping.
  //
  if (QuestionId == 0) {
    QuestionId = sizeof (MyIfrNVData);
  }

  ZeroMem (VariableName, (sizeof (CHAR16) * 40));

  switch (KeyValue) {
  case 0x0001:
    //
    // Create a small boot order list
    //
    QuestionId = (UINT16) ((UINTN) (&NVStruc.BootOrder) - (UINTN) (&NVStruc));

    //
    // Need some memory for OptionList. Allow for up to 8 options.
    //
    OptionList = AllocateZeroPool (sizeof (IFR_OPTION) * 8);
    ASSERT (OptionList != NULL);

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

    //
    // Remove all the op-codes starting with Label 0x2222 to next Label (second label is for convenience
    // so we don't have to keep track of how many op-codes we added or subtracted.  The rules for removal
    // of op-codes are simply that the removal will always stop as soon as a label or the end of a form is
    // encountered.  Therefore, giving a large obnoxious count such as below takes care of other complexities.
    //
    UpdateData->DataCount = 0xFF;

    //
    // Delete set of op-codes
    //
    Private->Hii->UpdateForm (
                    Private->Hii,
                    Private->RegisteredHandle,
                    (EFI_FORM_LABEL) 0x2222,
                    FALSE,  // If we aren't adding, we are deleting
                    UpdateData
                    );

    //
    // Create 3 options
    //
    for (Index = 0; Index < 3; Index++) {
      OptionList[Index].StringToken = (UINT16) (STR_BOOT_OPTION1 + Index);
      OptionList[Index].Value       = (UINT16) (Index + 1);
      OptionList[Index].Flags       = RESET_REQUIRED;
    }

    CreateOrderedListOpCode (
      QuestionId,                               // Question ID
      8,                                        // Max Entries
      (UINT16) STRING_TOKEN (STR_BOOT_OPTIONS), // Token value for the Prompt
      (UINT16) STRING_TOKEN (STR_NULL_STRING),  // Token value for the Help
      OptionList,
      3,
      &UpdateData->Data                         // Buffer location to place op-codes
      );

    //
    // For one-of/ordered lists commands, they really consist of 2 op-codes (a header and a footer)
    // Each option within a one-of/ordered list is also an op-code
    // So this example has 5 op-codes it is adding since we have a one-of header + 3 options + one-of footer
    //
    UpdateData->DataCount = 0x5;

    //
    // Add one op-code
    //
    Private->Hii->UpdateForm (
                    Private->Hii,
                    Private->RegisteredHandle,
                    (EFI_FORM_LABEL) 0x2222,
                    TRUE,
                    UpdateData
                    );

    FreePool (UpdateData);
    FreePool (OptionList);
    break;

  case 0x0002:
    //
    // Create a large boot order list
    //
    QuestionId = (UINT16) ((UINTN) (&NVStruc.BootOrder) - (UINTN) (&NVStruc));

    //
    // Need some memory for OptionList. Allow for up to 8 options.
    //
    OptionList = AllocateZeroPool (sizeof (IFR_OPTION) * 8);
    ASSERT (OptionList != NULL);

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

    //
    // Remove all the op-codes starting with Label 0x2222 to next Label (second label is for convenience
    // so we don't have to keep track of how many op-codes we added or subtracted
    //
    UpdateData->DataCount = 0xFF;

    //
    // Delete one op-code
    //
    Private->Hii->UpdateForm (
                    Private->Hii,
                    Private->RegisteredHandle,
                    (EFI_FORM_LABEL) 0x2222,
                    FALSE,
                    UpdateData
                    );

    //
    // Create 4 options
    //
    for (Index = 0; Index < 4; Index++) {
      OptionList[Index].StringToken = (UINT16) (STR_BOOT_OPTION1 + Index);
      OptionList[Index].Value       = (UINT16) (Index + 1);
      OptionList[Index].Flags       = RESET_REQUIRED;
    }

    CreateOrderedListOpCode (
      QuestionId,                               // Question ID
      8,                                        // Max Entries
      (UINT16) STRING_TOKEN (STR_BOOT_OPTIONS), // Token value for the Prompt
      (UINT16) STRING_TOKEN (STR_NULL_STRING),  // Token value for the Help
      OptionList,
      4,
      &UpdateData->Data                         // Buffer location to place op-codes
      );

    //
    // For one-of commands, they really consist of 2 op-codes (a header and a footer)
    // Each option within a one-of is also an op-code
    // So this example has 6 op-codes it is adding since we have a one-of header + 4 options + one-of footer
    //
    UpdateData->DataCount = 0x6;

    //
    // Add one op-code
    //
    Private->Hii->UpdateForm (
                    Private->Hii,
                    Private->RegisteredHandle,
                    (EFI_FORM_LABEL) 0x2222,
                    TRUE,
                    UpdateData
                    );

    FreePool (UpdateData);
    FreePool (OptionList);
    break;

  case 0x1234:
    //
    // Allocate space for creation of Buffer
    //
    QuestionId = (UINT16) ((UINTN) (&NVStruc.DynamicCheck) - (UINTN) (&NVStruc));
    UpdateData = AllocateZeroPool (0x1000);
    ASSERT (UpdateData != NULL);

    Location                        = (UINT8 *) &UpdateData->Data;

    UpdateData->FormSetUpdate       = TRUE;
    UpdateData->FormCallbackHandle  = (EFI_PHYSICAL_ADDRESS) (UINTN) Private->CallbackHandle;
    UpdateData->FormUpdate          = FALSE;
    UpdateData->FormTitle           = 0;
    UpdateData->DataCount           = 2;

    CreateGotoOpCode (
      1,
      STR_GOTO_FORM1,                                   // Token value for the Prompt
      0,                                                // Goto Help
      0,                                                // Flags
      0,                                                // Key
      &UpdateData->Data                                 // Buffer location to place op-codes
      );

    Location = Location + ((EFI_IFR_OP_HEADER *) &UpdateData->Data)->Length;

    CreateCheckBoxOpCode (
      QuestionId,                                       // Question ID
      1,                                                // Data width (BOOLEAN = 1)
      (UINT16) STRING_TOKEN (STR_CHECK_DYNAMIC_PROMPT), // Token value for the Prompt
      (UINT16) STRING_TOKEN (STR_CHECK_DYNAMIC_HELP),   // Token value for the Help
      EFI_IFR_FLAG_INTERACTIVE,                         // Flags
      0x1236,   // Key
      Location  // Buffer location to place op-codes
      );

    Private->Hii->UpdateForm (
                    Private->Hii,
                    Private->RegisteredHandle,
                    (EFI_FORM_LABEL) 0x1234,
                    TRUE,
                    UpdateData
                    );

    FreePool (UpdateData);
    QuestionId++;
    break;

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

    //
    // Initialize DataPacket with information intended to remove all
    // previously created op-codes in the dynamic page
    //
    UpdateData->FormSetUpdate       = FALSE;
    UpdateData->FormCallbackHandle  = 0;
    UpdateData->FormUpdate          = FALSE;
    UpdateData->FormTitle           = 0;
    //
    // Unlikely to be more than 0xff op-codes in the dynamic page to remove
    //
    UpdateData->DataCount           = 0xff;
    UpdateData->Data = NULL;

    //
    // Remove all op-codes from dynamic page
    //
    Private->Hii->UpdateForm (
                    Private->Hii,
                    Private->RegisteredHandle,
                    (EFI_FORM_LABEL) 0x1234,  // Label 0x1234
                    FALSE,                    // Remove Op-codes (will never remove form/endform)
                    UpdateData                // Significant value is UpdateData->DataCount
                    );

    UpdateData->FormSetUpdate       = FALSE;
    UpdateData->FormCallbackHandle  = 0;
    UpdateData->FormUpdate          = FALSE;
    UpdateData->FormTitle           = 0;
    UpdateData->DataCount           = 1;

    CreateGotoOpCode (
      1,
      STR_GOTO_FORM1,                         // Token value for the Prompt
      0,                                      // Goto Help
      0,                                      // Flags
      0,                                      // Key
      &UpdateData->Data                       // Buffer location to place op-codes
      );

    Private->Hii->UpdateForm (
                    Private->Hii,
                    Private->RegisteredHandle,
                    (EFI_FORM_LABEL) 0x1234,
                    TRUE,
                    UpdateData
                    );

    FreePool (UpdateData);
    break;

  case 0x1236:
    //
    // If I hit the checkbox, I enter this case statement...
    //
    //
    // Since I am returning an error (for test purposes) I need to pass in the string for the error
    // I will allocate space for the return value.  If an error occurs (which is the case) I can simply return
    // an error and fill in the string parameter, otherwise, I will return information in the DataArray structure.
    // The browser will free this packet structure
    //
    *Packet = AllocateZeroPool (sizeof (EFI_HII_CALLBACK_PACKET) + sizeof (SAMPLE_STRING) + 2);
    ASSERT (*Packet != NULL);

    //
    // Assign the buffer address to DataPacket
    //
    DataPacket = *Packet;

    StrCpy (DataPacket->String, (CHAR16 *) SAMPLE_STRING);
    return EFI_DEVICE_ERROR;

  case 0x1237:

    *Packet = AllocateZeroPool (sizeof (EFI_HII_CALLBACK_PACKET) + 2);
    ASSERT (*Packet != NULL);

    //
    // Assign the buffer address to DataPacket
    //
    DataPacket                        = *Packet;

    DataPacket->DataArray.EntryCount  = 1;
    DataPacket->DataArray.NvRamMap    = NULL;
    ((EFI_IFR_DATA_ENTRY *) (&DataPacket->DataArray + 1))->Flags = EXIT_REQUIRED;
    break;

  case 0x1555:
    Value = 0x0001;
    UnicodeSPrint (VariableName, 0x80, (CHAR16 *) L"%d", VAR_EQ_TEST_NAME);

    gRT->SetVariable (
          VariableName,
          &mFormSetGuid,
          EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
          2,
          (VOID *) &Value
          );
    break;

  case 0x1556:
    Value = 0x1000;
    UnicodeSPrint (VariableName, 0x80, (CHAR16 *) L"%d", VAR_EQ_TEST_NAME);

    gRT->SetVariable (
          VariableName,
          &mFormSetGuid,
          EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
          2,
          (VOID *) &Value
          );
    break;

  case 0x1557:
    Value = 0x0000;
    UnicodeSPrint (VariableName, 0x80, (CHAR16 *) L"%d", VAR_EQ_TEST_NAME);

    gRT->SetVariable (
          VariableName,
          &mFormSetGuid,
          EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
          2,
          (VOID *) &Value
          );
    break;

  default:
    break;
  }

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
DriverSampleInit (
  IN EFI_HANDLE                   ImageHandle,
  IN EFI_SYSTEM_TABLE             *SystemTable
  )
{
  EFI_STATUS          Status;
  EFI_HII_PROTOCOL    *Hii;
  //
  //  EFI_FORM_BROWSER_PROTOCOL       *FormConfig;
  //
  EFI_HII_PACKAGES    *PackageList;
  EFI_HII_HANDLE      HiiHandle;
  STRING_REF          TokenToUpdate;
  STRING_REF          TokenToUpdate2;
  STRING_REF          TokenToUpdate3;
  CHAR16              *NewString;
  EFI_HII_UPDATE_DATA *UpdateData;
  EFI_CALLBACK_INFO   *CallbackInfo;
  EFI_HANDLE          Handle;
  EFI_SCREEN_DESCRIPTOR   Screen;

  ZeroMem (&Screen, sizeof (EFI_SCREEN_DESCRIPTOR));

  gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &Screen.RightColumn, &Screen.BottomRow);

  //
  // Remove 3 characters from top and bottom
  //
  Screen.TopRow     = 3;
  Screen.BottomRow  = Screen.BottomRow - 3;

  //
  // There should only be one HII protocol
  //
  Status = gBS->LocateProtocol (
                  &gEfiHiiProtocolGuid,
                  NULL,
                  (VOID **) &Hii
                  );
  if (EFI_ERROR (Status)) {
    return Status;;
  }

  CallbackInfo = AllocatePool (sizeof (EFI_CALLBACK_INFO));
  if (CallbackInfo == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  CallbackInfo->Signature = EFI_CALLBACK_INFO_SIGNATURE;
  CallbackInfo->Hii       = Hii;

  //
  // This example does not implement worker functions for the NV accessor functions.  Only a callback evaluator
  //
  CallbackInfo->DriverCallback.NvRead   = NULL;
  CallbackInfo->DriverCallback.NvWrite  = NULL;
  CallbackInfo->DriverCallback.Callback = DriverCallback;

  //
  // Install protocol interface
  //
  Handle = NULL;
  Status = gBS->InstallProtocolInterface (
                  &Handle,
                  &gEfiFormCallbackProtocolGuid,
                  EFI_NATIVE_INTERFACE,
                  &CallbackInfo->DriverCallback
                  );

  ASSERT_EFI_ERROR (Status);

  CallbackInfo->CallbackHandle  = Handle;

  PackageList                   = PreparePackages (1, &mStringPackGuid, DriverSampleDxeStrings);
  Status                        = Hii->NewPack (Hii, PackageList, &HiiHandle);
  FreePool (PackageList);

  PackageList = PreparePackages (1, &mStringPackGuid, InventoryBin);
  Status      = Hii->NewPack (Hii, PackageList, &HiiHandle);
  FreePool (PackageList);

  PackageList = PreparePackages (1, &mStringPackGuid, VfrBin);
  Status      = Hii->NewPack (Hii, PackageList, &HiiHandle);
  FreePool (PackageList);

  CallbackInfo->RegisteredHandle = HiiHandle;

  //
  // Very simple example of how one would update a string that is already
  // in the HII database
  //
  TokenToUpdate = (STRING_REF) STR_CPU_STRING2;
  NewString     = (CHAR16 *) L"700 Mhz";

  Hii->NewString (Hii, NULL, HiiHandle, &TokenToUpdate, NewString);

  //
  // Add a string - if 0 will be updated with new Token number
  //
  TokenToUpdate = (STRING_REF) 0;

  //
  // Add a string - if 0 will be updated with new Token number
  //
  TokenToUpdate2 = (STRING_REF) 0;

  //
  // Add a string - if 0 will be updated with new Token number
  //
  TokenToUpdate3 = (STRING_REF) 0;

  Hii->NewString (Hii, NULL, HiiHandle, &TokenToUpdate, (CHAR16 *) L"Desired Speed");
  Hii->NewString (Hii, NULL, HiiHandle, &TokenToUpdate2, (CHAR16 *) L"5 Thz");
  Hii->NewString (Hii, NULL, HiiHandle, &TokenToUpdate3, (CHAR16 *) L"This is next year's desired speed - right?");

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

  //
  // Flag update pending in FormSet
  //
  UpdateData->FormSetUpdate = TRUE;
  //
  // Register CallbackHandle data for FormSet
  //
  UpdateData->FormCallbackHandle = (EFI_PHYSICAL_ADDRESS) (UINTN) CallbackInfo->CallbackHandle;
  UpdateData->FormUpdate  = FALSE;
  UpdateData->FormTitle   = 0;
  UpdateData->DataCount   = 1;

  CreateTextOpCode (TokenToUpdate, TokenToUpdate2, TokenToUpdate3, 0, 0, &UpdateData->Data);

  Hii->UpdateForm (Hii, HiiHandle, (EFI_FORM_LABEL) 100, TRUE, UpdateData);

  FreePool (UpdateData);

  //
  // Example of how to display only the item we sent to HII
  //
  if (DISPLAY_ONLY_MY_ITEM == 0x0001) {
    //
    // Have the browser pull out our copy of the data, and only display our data
    //
    //    Status = FormConfig->SendForm (FormConfig, TRUE, HiiHandle, NULL, NULL, NULL, &Screen, NULL);
    //
  } else {
    //
    // Have the browser pull out all the data in the HII Database and display it.
    //
    //    Status = FormConfig->SendForm (FormConfig, TRUE, 0, NULL, NULL, NULL, NULL, NULL);
    //
  }

  if (EFI_ERROR (Status)) {
    return Status;
  }

  return EFI_SUCCESS;
}