]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Universal/DriverSampleDxe/DriverSample.c
Modules cleanup.
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / DriverSampleDxe / DriverSample.c
CommitLineData
103b6520 1/*++\r
ececc2eb 2Copyright (c) 2006 - 2007, Intel Corporation\r
3All rights reserved. This program and the accompanying materials\r
4are licensed and made available under the terms and conditions of the BSD License\r
5which accompanies this distribution. The full text of the license may be found at\r
6http://opensource.org/licenses/bsd-license.php\r
7\r
8THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
9WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
103b6520 10\r
11Module Name:\r
12 DriverSample.c\r
13\r
14Abstract:\r
15\r
ececc2eb 16 This is an example of how a driver might export data to the HII protocol to be\r
103b6520 17 later utilized by the Setup Protocol\r
18\r
19--*/\r
20\r
103b6520 21#include "DriverSample.h"\r
22\r
23#define DISPLAY_ONLY_MY_ITEM 0x0001\r
24\r
25#define STRING_PACK_GUID \\r
26 { \\r
27 0x8160a85f, 0x934d, 0x468b, { 0xa2, 0x35, 0x72, 0x89, 0x59, 0x14, 0xf6, 0xfc } \\r
28 }\r
29\r
30EFI_GUID mFormSetGuid = FORMSET_GUID;\r
ececc2eb 31EFI_GUID mStringPackGuid = STRING_PACK_GUID;\r
103b6520 32\r
33STATIC\r
34EFI_STATUS\r
35EFIAPI\r
36DriverCallback (\r
37 IN EFI_FORM_CALLBACK_PROTOCOL *This,\r
38 IN UINT16 KeyValue,\r
39 IN EFI_IFR_DATA_ARRAY *Data,\r
40 OUT EFI_HII_CALLBACK_PACKET **Packet\r
41 )\r
42/*++\r
43\r
44Routine Description:\r
45\r
46 This is the function that is called to provide results data to the driver. This data\r
47 consists of a unique key which is used to identify what data is either being passed back\r
ececc2eb 48 or being asked for.\r
103b6520 49\r
50Arguments:\r
51\r
52 KeyValue - A unique value which is sent to the original exporting driver so that it\r
53 can identify the type of data to expect. The format of the data tends to\r
54 vary based on the op-code that geerated the callback.\r
55\r
56 Data - A pointer to the data being sent to the original exporting driver.\r
57\r
ececc2eb 58Returns:\r
103b6520 59\r
60--*/\r
61{\r
62 EFI_CALLBACK_INFO *Private;\r
63 EFI_HII_UPDATE_DATA *UpdateData;\r
64 UINT8 *Location;\r
65 EFI_HII_CALLBACK_PACKET *DataPacket;\r
66 UINT16 Value;\r
67 CHAR16 VariableName[40];\r
68 STATIC UINT16 QuestionId = 0;\r
69 IFR_OPTION *OptionList;\r
70 UINTN Index;\r
71 MyIfrNVData NVStruc;\r
72\r
73 Private = EFI_CALLBACK_INFO_FROM_THIS (This);\r
74\r
75 //\r
76 // This should tell me the first offset AFTER the end of the compiled NV map\r
77 // If op-code results are not going to be saved to NV locations ensure the QuestionId\r
78 // is beyond the end of the NVRAM mapping.\r
79 //\r
80 if (QuestionId == 0) {\r
81 QuestionId = sizeof (MyIfrNVData);\r
82 }\r
83\r
84 ZeroMem (VariableName, (sizeof (CHAR16) * 40));\r
85\r
86 switch (KeyValue) {\r
87 case 0x0001:\r
88 //\r
89 // Create a small boot order list\r
90 //\r
91 QuestionId = (UINT16) ((UINTN) (&NVStruc.BootOrder) - (UINTN) (&NVStruc));\r
92\r
93 //\r
94 // Need some memory for OptionList. Allow for up to 8 options.\r
95 //\r
96 OptionList = AllocateZeroPool (sizeof (IFR_OPTION) * 8);\r
97 ASSERT (OptionList != NULL);\r
98\r
99 //\r
100 // Allocate space for creation of Buffer\r
101 //\r
102 UpdateData = AllocateZeroPool (0x1000);\r
103 ASSERT (UpdateData != NULL);\r
104\r
105 //\r
106 // Remove all the op-codes starting with Label 0x2222 to next Label (second label is for convenience\r
107 // so we don't have to keep track of how many op-codes we added or subtracted. The rules for removal\r
108 // of op-codes are simply that the removal will always stop as soon as a label or the end of a form is\r
109 // encountered. Therefore, giving a large obnoxious count such as below takes care of other complexities.\r
110 //\r
111 UpdateData->DataCount = 0xFF;\r
112\r
113 //\r
114 // Delete set of op-codes\r
115 //\r
116 Private->Hii->UpdateForm (\r
117 Private->Hii,\r
118 Private->RegisteredHandle,\r
119 (EFI_FORM_LABEL) 0x2222,\r
120 FALSE, // If we aren't adding, we are deleting\r
121 UpdateData\r
122 );\r
123\r
124 //\r
125 // Create 3 options\r
126 //\r
127 for (Index = 0; Index < 3; Index++) {\r
128 OptionList[Index].StringToken = (UINT16) (STR_BOOT_OPTION1 + Index);\r
129 OptionList[Index].Value = (UINT16) (Index + 1);\r
130 OptionList[Index].Flags = RESET_REQUIRED;\r
131 }\r
132\r
133 CreateOrderedListOpCode (\r
134 QuestionId, // Question ID\r
135 8, // Max Entries\r
136 (UINT16) STRING_TOKEN (STR_BOOT_OPTIONS), // Token value for the Prompt\r
137 (UINT16) STRING_TOKEN (STR_NULL_STRING), // Token value for the Help\r
138 OptionList,\r
139 3,\r
140 &UpdateData->Data // Buffer location to place op-codes\r
141 );\r
142\r
143 //\r
144 // For one-of/ordered lists commands, they really consist of 2 op-codes (a header and a footer)\r
145 // Each option within a one-of/ordered list is also an op-code\r
146 // So this example has 5 op-codes it is adding since we have a one-of header + 3 options + one-of footer\r
147 //\r
148 UpdateData->DataCount = 0x5;\r
149\r
150 //\r
151 // Add one op-code\r
152 //\r
153 Private->Hii->UpdateForm (\r
154 Private->Hii,\r
155 Private->RegisteredHandle,\r
156 (EFI_FORM_LABEL) 0x2222,\r
157 TRUE,\r
158 UpdateData\r
159 );\r
160\r
161 FreePool (UpdateData);\r
162 FreePool (OptionList);\r
163 break;\r
164\r
165 case 0x0002:\r
166 //\r
167 // Create a large boot order list\r
168 //\r
169 QuestionId = (UINT16) ((UINTN) (&NVStruc.BootOrder) - (UINTN) (&NVStruc));\r
170\r
171 //\r
172 // Need some memory for OptionList. Allow for up to 8 options.\r
173 //\r
174 OptionList = AllocateZeroPool (sizeof (IFR_OPTION) * 8);\r
175 ASSERT (OptionList != NULL);\r
176\r
177 //\r
178 // Allocate space for creation of Buffer\r
179 //\r
180 UpdateData = AllocateZeroPool (0x1000);\r
181 ASSERT (UpdateData != NULL);\r
182\r
183 //\r
184 // Remove all the op-codes starting with Label 0x2222 to next Label (second label is for convenience\r
185 // so we don't have to keep track of how many op-codes we added or subtracted\r
186 //\r
187 UpdateData->DataCount = 0xFF;\r
188\r
189 //\r
190 // Delete one op-code\r
191 //\r
192 Private->Hii->UpdateForm (\r
193 Private->Hii,\r
194 Private->RegisteredHandle,\r
195 (EFI_FORM_LABEL) 0x2222,\r
196 FALSE,\r
197 UpdateData\r
198 );\r
199\r
200 //\r
201 // Create 4 options\r
202 //\r
203 for (Index = 0; Index < 4; Index++) {\r
204 OptionList[Index].StringToken = (UINT16) (STR_BOOT_OPTION1 + Index);\r
205 OptionList[Index].Value = (UINT16) (Index + 1);\r
206 OptionList[Index].Flags = RESET_REQUIRED;\r
207 }\r
208\r
209 CreateOrderedListOpCode (\r
210 QuestionId, // Question ID\r
211 8, // Max Entries\r
212 (UINT16) STRING_TOKEN (STR_BOOT_OPTIONS), // Token value for the Prompt\r
213 (UINT16) STRING_TOKEN (STR_NULL_STRING), // Token value for the Help\r
214 OptionList,\r
215 4,\r
216 &UpdateData->Data // Buffer location to place op-codes\r
217 );\r
218\r
219 //\r
220 // For one-of commands, they really consist of 2 op-codes (a header and a footer)\r
221 // Each option within a one-of is also an op-code\r
222 // So this example has 6 op-codes it is adding since we have a one-of header + 4 options + one-of footer\r
223 //\r
224 UpdateData->DataCount = 0x6;\r
225\r
226 //\r
227 // Add one op-code\r
228 //\r
229 Private->Hii->UpdateForm (\r
230 Private->Hii,\r
231 Private->RegisteredHandle,\r
232 (EFI_FORM_LABEL) 0x2222,\r
233 TRUE,\r
234 UpdateData\r
235 );\r
236\r
237 FreePool (UpdateData);\r
238 FreePool (OptionList);\r
239 break;\r
240\r
241 case 0x1234:\r
242 //\r
243 // Allocate space for creation of Buffer\r
244 //\r
245 QuestionId = (UINT16) ((UINTN) (&NVStruc.DynamicCheck) - (UINTN) (&NVStruc));\r
246 UpdateData = AllocateZeroPool (0x1000);\r
247 ASSERT (UpdateData != NULL);\r
248\r
249 Location = (UINT8 *) &UpdateData->Data;\r
250\r
251 UpdateData->FormSetUpdate = TRUE;\r
252 UpdateData->FormCallbackHandle = (EFI_PHYSICAL_ADDRESS) (UINTN) Private->CallbackHandle;\r
253 UpdateData->FormUpdate = FALSE;\r
254 UpdateData->FormTitle = 0;\r
255 UpdateData->DataCount = 2;\r
256\r
257 CreateGotoOpCode (\r
258 1,\r
259 STR_GOTO_FORM1, // Token value for the Prompt\r
260 0, // Goto Help\r
261 0, // Flags\r
262 0, // Key\r
263 &UpdateData->Data // Buffer location to place op-codes\r
264 );\r
265\r
266 Location = Location + ((EFI_IFR_OP_HEADER *) &UpdateData->Data)->Length;\r
267\r
268 CreateCheckBoxOpCode (\r
269 QuestionId, // Question ID\r
270 1, // Data width (BOOLEAN = 1)\r
271 (UINT16) STRING_TOKEN (STR_CHECK_DYNAMIC_PROMPT), // Token value for the Prompt\r
272 (UINT16) STRING_TOKEN (STR_CHECK_DYNAMIC_HELP), // Token value for the Help\r
273 EFI_IFR_FLAG_INTERACTIVE, // Flags\r
274 0x1236, // Key\r
275 Location // Buffer location to place op-codes\r
276 );\r
277\r
278 Private->Hii->UpdateForm (\r
279 Private->Hii,\r
280 Private->RegisteredHandle,\r
281 (EFI_FORM_LABEL) 0x1234,\r
282 TRUE,\r
283 UpdateData\r
284 );\r
285\r
286 FreePool (UpdateData);\r
287 QuestionId++;\r
288 break;\r
289\r
290 case 0x1235:\r
291 //\r
292 // Allocate space for creation of Buffer\r
293 //\r
294 UpdateData = AllocateZeroPool (0x1000);\r
295 ASSERT (UpdateData != NULL);\r
296\r
297 //\r
298 // Initialize DataPacket with information intended to remove all\r
299 // previously created op-codes in the dynamic page\r
300 //\r
301 UpdateData->FormSetUpdate = FALSE;\r
302 UpdateData->FormCallbackHandle = 0;\r
303 UpdateData->FormUpdate = FALSE;\r
304 UpdateData->FormTitle = 0;\r
305 //\r
306 // Unlikely to be more than 0xff op-codes in the dynamic page to remove\r
307 //\r
308 UpdateData->DataCount = 0xff;\r
309 UpdateData->Data = NULL;\r
310\r
311 //\r
312 // Remove all op-codes from dynamic page\r
313 //\r
314 Private->Hii->UpdateForm (\r
315 Private->Hii,\r
316 Private->RegisteredHandle,\r
317 (EFI_FORM_LABEL) 0x1234, // Label 0x1234\r
318 FALSE, // Remove Op-codes (will never remove form/endform)\r
319 UpdateData // Significant value is UpdateData->DataCount\r
320 );\r
321\r
322 UpdateData->FormSetUpdate = FALSE;\r
323 UpdateData->FormCallbackHandle = 0;\r
324 UpdateData->FormUpdate = FALSE;\r
325 UpdateData->FormTitle = 0;\r
326 UpdateData->DataCount = 1;\r
327\r
328 CreateGotoOpCode (\r
329 1,\r
330 STR_GOTO_FORM1, // Token value for the Prompt\r
331 0, // Goto Help\r
332 0, // Flags\r
333 0, // Key\r
334 &UpdateData->Data // Buffer location to place op-codes\r
335 );\r
336\r
337 Private->Hii->UpdateForm (\r
338 Private->Hii,\r
339 Private->RegisteredHandle,\r
340 (EFI_FORM_LABEL) 0x1234,\r
341 TRUE,\r
342 UpdateData\r
343 );\r
344\r
345 FreePool (UpdateData);\r
346 break;\r
347\r
348 case 0x1236:\r
349 //\r
350 // If I hit the checkbox, I enter this case statement...\r
351 //\r
352 //\r
353 // Since I am returning an error (for test purposes) I need to pass in the string for the error\r
354 // I will allocate space for the return value. If an error occurs (which is the case) I can simply return\r
355 // an error and fill in the string parameter, otherwise, I will return information in the DataArray structure.\r
356 // The browser will free this packet structure\r
357 //\r
358 *Packet = AllocateZeroPool (sizeof (EFI_HII_CALLBACK_PACKET) + sizeof (SAMPLE_STRING) + 2);\r
359 ASSERT (*Packet != NULL);\r
360\r
361 //\r
362 // Assign the buffer address to DataPacket\r
363 //\r
364 DataPacket = *Packet;\r
365\r
366 StrCpy (DataPacket->String, (CHAR16 *) SAMPLE_STRING);\r
367 return EFI_DEVICE_ERROR;\r
368\r
369 case 0x1237:\r
370\r
371 *Packet = AllocateZeroPool (sizeof (EFI_HII_CALLBACK_PACKET) + 2);\r
372 ASSERT (*Packet != NULL);\r
373\r
374 //\r
375 // Assign the buffer address to DataPacket\r
376 //\r
377 DataPacket = *Packet;\r
378\r
379 DataPacket->DataArray.EntryCount = 1;\r
380 DataPacket->DataArray.NvRamMap = NULL;\r
381 ((EFI_IFR_DATA_ENTRY *) (&DataPacket->DataArray + 1))->Flags = EXIT_REQUIRED;\r
382 break;\r
383\r
384 case 0x1555:\r
385 Value = 0x0001;\r
386 UnicodeSPrint (VariableName, 0x80, (CHAR16 *) L"%d", VAR_EQ_TEST_NAME);\r
387\r
388 gRT->SetVariable (\r
389 VariableName,\r
390 &mFormSetGuid,\r
391 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
392 2,\r
393 (VOID *) &Value\r
394 );\r
395 break;\r
396\r
397 case 0x1556:\r
398 Value = 0x1000;\r
399 UnicodeSPrint (VariableName, 0x80, (CHAR16 *) L"%d", VAR_EQ_TEST_NAME);\r
400\r
401 gRT->SetVariable (\r
402 VariableName,\r
403 &mFormSetGuid,\r
404 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
405 2,\r
406 (VOID *) &Value\r
407 );\r
408 break;\r
409\r
410 case 0x1557:\r
411 Value = 0x0000;\r
412 UnicodeSPrint (VariableName, 0x80, (CHAR16 *) L"%d", VAR_EQ_TEST_NAME);\r
413\r
414 gRT->SetVariable (\r
415 VariableName,\r
416 &mFormSetGuid,\r
417 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
418 2,\r
419 (VOID *) &Value\r
420 );\r
421 break;\r
422\r
423 default:\r
424 break;\r
425 }\r
426\r
427 return EFI_SUCCESS;\r
428}\r
429\r
430EFI_STATUS\r
431EFIAPI\r
432DriverSampleInit (\r
433 IN EFI_HANDLE ImageHandle,\r
434 IN EFI_SYSTEM_TABLE *SystemTable\r
435 )\r
436{\r
437 EFI_STATUS Status;\r
438 EFI_HII_PROTOCOL *Hii;\r
439 //\r
440 // EFI_FORM_BROWSER_PROTOCOL *FormConfig;\r
441 //\r
442 EFI_HII_PACKAGES *PackageList;\r
443 EFI_HII_HANDLE HiiHandle;\r
444 STRING_REF TokenToUpdate;\r
445 STRING_REF TokenToUpdate2;\r
446 STRING_REF TokenToUpdate3;\r
447 CHAR16 *NewString;\r
448 EFI_HII_UPDATE_DATA *UpdateData;\r
449 EFI_CALLBACK_INFO *CallbackInfo;\r
450 EFI_HANDLE Handle;\r
451 EFI_SCREEN_DESCRIPTOR Screen;\r
452\r
453 ZeroMem (&Screen, sizeof (EFI_SCREEN_DESCRIPTOR));\r
454\r
455 gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &Screen.RightColumn, &Screen.BottomRow);\r
456\r
457 //\r
458 // Remove 3 characters from top and bottom\r
459 //\r
460 Screen.TopRow = 3;\r
461 Screen.BottomRow = Screen.BottomRow - 3;\r
462\r
463 //\r
464 // There should only be one HII protocol\r
465 //\r
466 Status = gBS->LocateProtocol (\r
467 &gEfiHiiProtocolGuid,\r
468 NULL,\r
469 (VOID **) &Hii\r
470 );\r
471 if (EFI_ERROR (Status)) {\r
472 return Status;;\r
473 }\r
474\r
475 CallbackInfo = AllocatePool (sizeof (EFI_CALLBACK_INFO));\r
476 if (CallbackInfo == NULL) {\r
477 return EFI_OUT_OF_RESOURCES;\r
478 }\r
479\r
480 CallbackInfo->Signature = EFI_CALLBACK_INFO_SIGNATURE;\r
481 CallbackInfo->Hii = Hii;\r
482\r
483 //\r
484 // This example does not implement worker functions for the NV accessor functions. Only a callback evaluator\r
485 //\r
486 CallbackInfo->DriverCallback.NvRead = NULL;\r
487 CallbackInfo->DriverCallback.NvWrite = NULL;\r
488 CallbackInfo->DriverCallback.Callback = DriverCallback;\r
489\r
490 //\r
491 // Install protocol interface\r
492 //\r
493 Handle = NULL;\r
494 Status = gBS->InstallProtocolInterface (\r
495 &Handle,\r
496 &gEfiFormCallbackProtocolGuid,\r
497 EFI_NATIVE_INTERFACE,\r
498 &CallbackInfo->DriverCallback\r
499 );\r
500\r
501 ASSERT_EFI_ERROR (Status);\r
502\r
503 CallbackInfo->CallbackHandle = Handle;\r
504\r
ececc2eb 505 PackageList = PreparePackages (1, &mStringPackGuid, DriverSampleDxeStrings);\r
103b6520 506 Status = Hii->NewPack (Hii, PackageList, &HiiHandle);\r
507 FreePool (PackageList);\r
508\r
509 PackageList = PreparePackages (1, &mStringPackGuid, InventoryBin);\r
510 Status = Hii->NewPack (Hii, PackageList, &HiiHandle);\r
511 FreePool (PackageList);\r
512\r
513 PackageList = PreparePackages (1, &mStringPackGuid, VfrBin);\r
514 Status = Hii->NewPack (Hii, PackageList, &HiiHandle);\r
515 FreePool (PackageList);\r
516\r
517 CallbackInfo->RegisteredHandle = HiiHandle;\r
518\r
519 //\r
520 // Very simple example of how one would update a string that is already\r
521 // in the HII database\r
522 //\r
523 TokenToUpdate = (STRING_REF) STR_CPU_STRING2;\r
524 NewString = (CHAR16 *) L"700 Mhz";\r
525\r
526 Hii->NewString (Hii, NULL, HiiHandle, &TokenToUpdate, NewString);\r
527\r
528 //\r
529 // Add a string - if 0 will be updated with new Token number\r
530 //\r
531 TokenToUpdate = (STRING_REF) 0;\r
532\r
533 //\r
534 // Add a string - if 0 will be updated with new Token number\r
535 //\r
536 TokenToUpdate2 = (STRING_REF) 0;\r
537\r
538 //\r
539 // Add a string - if 0 will be updated with new Token number\r
540 //\r
541 TokenToUpdate3 = (STRING_REF) 0;\r
542\r
543 Hii->NewString (Hii, NULL, HiiHandle, &TokenToUpdate, (CHAR16 *) L"Desired Speed");\r
544 Hii->NewString (Hii, NULL, HiiHandle, &TokenToUpdate2, (CHAR16 *) L"5 Thz");\r
545 Hii->NewString (Hii, NULL, HiiHandle, &TokenToUpdate3, (CHAR16 *) L"This is next year's desired speed - right?");\r
546\r
547 //\r
548 // Allocate space for creation of Buffer\r
549 //\r
550 UpdateData = AllocateZeroPool (0x1000);\r
551 ASSERT (UpdateData != NULL);\r
552\r
553 //\r
554 // Flag update pending in FormSet\r
555 //\r
556 UpdateData->FormSetUpdate = TRUE;\r
557 //\r
558 // Register CallbackHandle data for FormSet\r
559 //\r
560 UpdateData->FormCallbackHandle = (EFI_PHYSICAL_ADDRESS) (UINTN) CallbackInfo->CallbackHandle;\r
561 UpdateData->FormUpdate = FALSE;\r
562 UpdateData->FormTitle = 0;\r
563 UpdateData->DataCount = 1;\r
564\r
565 CreateTextOpCode (TokenToUpdate, TokenToUpdate2, TokenToUpdate3, 0, 0, &UpdateData->Data);\r
566\r
567 Hii->UpdateForm (Hii, HiiHandle, (EFI_FORM_LABEL) 100, TRUE, UpdateData);\r
568\r
569 FreePool (UpdateData);\r
570\r
571 //\r
572 // Example of how to display only the item we sent to HII\r
573 //\r
574 if (DISPLAY_ONLY_MY_ITEM == 0x0001) {\r
575 //\r
576 // Have the browser pull out our copy of the data, and only display our data\r
577 //\r
578 // Status = FormConfig->SendForm (FormConfig, TRUE, HiiHandle, NULL, NULL, NULL, &Screen, NULL);\r
579 //\r
580 } else {\r
581 //\r
582 // Have the browser pull out all the data in the HII Database and display it.\r
583 //\r
584 // Status = FormConfig->SendForm (FormConfig, TRUE, 0, NULL, NULL, NULL, NULL, NULL);\r
585 //\r
586 }\r
587\r
588 if (EFI_ERROR (Status)) {\r
589 return Status;\r
590 }\r
591\r
592 return EFI_SUCCESS;\r
593}\r