MdeModulePkg: Refine the code in BootMaintenanceManagerUiLib
[mirror_edk2.git] / MdeModulePkg / Library / BootMaintenanceManagerUiLib / Variable.c
CommitLineData
4af04335
DB
1/** @file\r
2Variable operation that will be used by bootmaint\r
3\r
2ba36b2f 4Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
4af04335
DB
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "BootMaintenanceManager.h"\r
16\r
17/**\r
18 Delete Boot Option that represent a Deleted state in BootOptionMenu.\r
19 After deleting this boot option, call Var_ChangeBootOrder to\r
20 make sure BootOrder is in valid state.\r
21\r
22 @retval EFI_SUCCESS If all boot load option EFI Variables corresponding to \r
23 BM_LOAD_CONTEXT marked for deletion is deleted.\r
24 @retval EFI_NOT_FOUND If can not find the boot option want to be deleted.\r
25 @return Others If failed to update the "BootOrder" variable after deletion. \r
26\r
27**/\r
28EFI_STATUS\r
29Var_DelBootOption (\r
30 VOID\r
31 )\r
32{\r
33 BM_MENU_ENTRY *NewMenuEntry;\r
34 BM_LOAD_CONTEXT *NewLoadContext;\r
35 UINT16 BootString[10];\r
36 EFI_STATUS Status;\r
37 UINTN Index;\r
38 UINTN Index2;\r
39\r
40 Status = EFI_SUCCESS;\r
41 Index2 = 0;\r
42 for (Index = 0; Index < BootOptionMenu.MenuNumber; Index++) {\r
43 NewMenuEntry = BOpt_GetMenuEntry (&BootOptionMenu, (Index - Index2));\r
44 if (NULL == NewMenuEntry) {\r
45 return EFI_NOT_FOUND;\r
46 }\r
47\r
48 NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext;\r
49 if (!NewLoadContext->Deleted) {\r
50 continue;\r
51 }\r
52\r
53 UnicodeSPrint (\r
54 BootString,\r
55 sizeof (BootString),\r
56 L"Boot%04x",\r
57 NewMenuEntry->OptionNumber\r
58 );\r
59\r
60 EfiLibDeleteVariable (BootString, &gEfiGlobalVariableGuid);\r
61 Index2++;\r
62 //\r
63 // If current Load Option is the same as BootNext,\r
64 // must delete BootNext in order to make sure\r
65 // there will be no panic on next boot\r
66 //\r
67 if (NewLoadContext->IsBootNext) {\r
68 EfiLibDeleteVariable (L"BootNext", &gEfiGlobalVariableGuid);\r
69 }\r
70\r
71 RemoveEntryList (&NewMenuEntry->Link);\r
72 BOpt_DestroyMenuEntry (NewMenuEntry);\r
73 NewMenuEntry = NULL;\r
74 }\r
75\r
76 BootOptionMenu.MenuNumber -= Index2;\r
77\r
78 Status = Var_ChangeBootOrder ();\r
79 return Status;\r
80}\r
81\r
82/**\r
83 After any operation on Boot####, there will be a discrepancy in BootOrder.\r
84 Since some are missing but in BootOrder, while some are present but are\r
85 not reflected by BootOrder. Then a function rebuild BootOrder from\r
86 scratch by content from BootOptionMenu is needed.\r
87\r
88\r
89 \r
90\r
91 @retval EFI_SUCCESS The boot order is updated successfully.\r
92 @return EFI_STATUS other than EFI_SUCCESS if failed to\r
93 Set the "BootOrder" EFI Variable.\r
94\r
95**/\r
96EFI_STATUS\r
97Var_ChangeBootOrder (\r
98 VOID\r
99 )\r
100{\r
101\r
102 EFI_STATUS Status;\r
103 BM_MENU_ENTRY *NewMenuEntry;\r
104 UINT16 *BootOrderList;\r
105 UINT16 *BootOrderListPtr;\r
106 UINTN BootOrderListSize;\r
107 UINTN Index;\r
108\r
109 BootOrderList = NULL;\r
110 BootOrderListSize = 0;\r
111 //\r
112 // First check whether BootOrder is present in current configuration\r
113 //\r
114 GetEfiGlobalVariable2 (L"BootOrder", (VOID **) &BootOrderList, &BootOrderListSize);\r
115\r
116 //\r
117 // If exists, delete it to hold new BootOrder\r
118 //\r
119 if (BootOrderList != NULL) {\r
120 EfiLibDeleteVariable (L"BootOrder", &gEfiGlobalVariableGuid);\r
121 FreePool (BootOrderList);\r
122 BootOrderList = NULL;\r
123 }\r
124 //\r
125 // Maybe here should be some check method to ensure that\r
126 // no new added boot options will be added\r
127 // but the setup engine now will give only one callback\r
128 // that is to say, user are granted only one chance to\r
129 // decide whether the boot option will be added or not\r
130 // there should be no indictor to show whether this\r
131 // is a "new" boot option\r
132 //\r
133 BootOrderListSize = BootOptionMenu.MenuNumber;\r
134\r
135 if (BootOrderListSize > 0) {\r
136 BootOrderList = AllocateZeroPool (BootOrderListSize * sizeof (UINT16));\r
137 ASSERT (BootOrderList != NULL);\r
138 BootOrderListPtr = BootOrderList;\r
139\r
140 //\r
141 // Get all current used Boot#### from BootOptionMenu.\r
142 // OptionNumber in each BM_LOAD_OPTION is really its\r
143 // #### value.\r
144 //\r
145 for (Index = 0; Index < BootOrderListSize; Index++) {\r
146 NewMenuEntry = BOpt_GetMenuEntry (&BootOptionMenu, Index);\r
147 *BootOrderList = (UINT16) NewMenuEntry->OptionNumber;\r
148 BootOrderList++;\r
149 }\r
150\r
151 BootOrderList = BootOrderListPtr;\r
152\r
153 //\r
154 // After building the BootOrderList, write it back\r
155 //\r
156 Status = gRT->SetVariable (\r
157 L"BootOrder",\r
158 &gEfiGlobalVariableGuid,\r
159 VAR_FLAG,\r
160 BootOrderListSize * sizeof (UINT16),\r
161 BootOrderList\r
162 );\r
163 if (EFI_ERROR (Status)) {\r
164 return Status;\r
165 }\r
166 }\r
167 return EFI_SUCCESS;\r
168}\r
169\r
170/**\r
171 Delete Load Option that represent a Deleted state in BootOptionMenu.\r
172 After deleting this Driver option, call Var_ChangeDriverOrder to\r
173 make sure DriverOrder is in valid state.\r
174\r
175 @retval EFI_SUCCESS Load Option is successfully updated.\r
176 @retval EFI_NOT_FOUND Fail to find the driver option want to be deleted.\r
177 @return Other value than EFI_SUCCESS if failed to update "Driver Order" EFI\r
178 Variable.\r
179\r
180**/\r
181EFI_STATUS\r
182Var_DelDriverOption (\r
183 VOID\r
184 )\r
185{\r
186 BM_MENU_ENTRY *NewMenuEntry;\r
187 BM_LOAD_CONTEXT *NewLoadContext;\r
188 UINT16 DriverString[12];\r
189 EFI_STATUS Status;\r
190 UINTN Index;\r
191 UINTN Index2;\r
192\r
193 Status = EFI_SUCCESS;\r
194 Index2 = 0;\r
195 for (Index = 0; Index < DriverOptionMenu.MenuNumber; Index++) {\r
196 NewMenuEntry = BOpt_GetMenuEntry (&DriverOptionMenu, (Index - Index2));\r
197 if (NULL == NewMenuEntry) {\r
198 return EFI_NOT_FOUND;\r
199 }\r
200\r
201 NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext;\r
202 if (!NewLoadContext->Deleted) {\r
203 continue;\r
204 }\r
205\r
206 UnicodeSPrint (\r
207 DriverString,\r
208 sizeof (DriverString),\r
209 L"Driver%04x",\r
210 NewMenuEntry->OptionNumber\r
211 );\r
212\r
213 EfiLibDeleteVariable (DriverString, &gEfiGlobalVariableGuid);\r
214 Index2++;\r
215\r
216 RemoveEntryList (&NewMenuEntry->Link);\r
217 BOpt_DestroyMenuEntry (NewMenuEntry);\r
218 NewMenuEntry = NULL;\r
219 }\r
220\r
221 DriverOptionMenu.MenuNumber -= Index2;\r
222\r
223 Status = Var_ChangeDriverOrder ();\r
224 return Status;\r
225}\r
226\r
227/**\r
228 After any operation on Driver####, there will be a discrepancy in\r
229 DriverOrder. Since some are missing but in DriverOrder, while some\r
230 are present but are not reflected by DriverOrder. Then a function\r
231 rebuild DriverOrder from scratch by content from DriverOptionMenu is\r
232 needed.\r
233\r
234 @retval EFI_SUCCESS The driver order is updated successfully.\r
235 @return Other status than EFI_SUCCESS if failed to set the "DriverOrder" EFI Variable.\r
236\r
237**/\r
238EFI_STATUS\r
239Var_ChangeDriverOrder (\r
240 VOID\r
241 )\r
242{\r
243 EFI_STATUS Status;\r
244 BM_MENU_ENTRY *NewMenuEntry;\r
245 UINT16 *DriverOrderList;\r
246 UINT16 *DriverOrderListPtr;\r
247 UINTN DriverOrderListSize;\r
248 UINTN Index;\r
249\r
250 DriverOrderList = NULL;\r
251 DriverOrderListSize = 0;\r
252\r
253 //\r
254 // First check whether DriverOrder is present in current configuration\r
255 //\r
256 GetEfiGlobalVariable2 (L"DriverOrder", (VOID **) &DriverOrderList, &DriverOrderListSize);\r
257 //\r
258 // If exists, delete it to hold new DriverOrder\r
259 //\r
260 if (DriverOrderList != NULL) {\r
261 EfiLibDeleteVariable (L"DriverOrder", &gEfiGlobalVariableGuid);\r
262 FreePool (DriverOrderList);\r
263 DriverOrderList = NULL;\r
264 }\r
265\r
266 DriverOrderListSize = DriverOptionMenu.MenuNumber;\r
267\r
268 if (DriverOrderListSize > 0) {\r
269 DriverOrderList = AllocateZeroPool (DriverOrderListSize * sizeof (UINT16));\r
270 ASSERT (DriverOrderList != NULL);\r
271 DriverOrderListPtr = DriverOrderList;\r
272\r
273 //\r
274 // Get all current used Driver#### from DriverOptionMenu.\r
275 // OptionNumber in each BM_LOAD_OPTION is really its\r
276 // #### value.\r
277 //\r
278 for (Index = 0; Index < DriverOrderListSize; Index++) {\r
279 NewMenuEntry = BOpt_GetMenuEntry (&DriverOptionMenu, Index);\r
280 *DriverOrderList = (UINT16) NewMenuEntry->OptionNumber;\r
281 DriverOrderList++;\r
282 }\r
283\r
284 DriverOrderList = DriverOrderListPtr;\r
285\r
286 //\r
287 // After building the DriverOrderList, write it back\r
288 //\r
289 Status = gRT->SetVariable (\r
290 L"DriverOrder",\r
291 &gEfiGlobalVariableGuid,\r
292 VAR_FLAG,\r
293 DriverOrderListSize * sizeof (UINT16),\r
294 DriverOrderList\r
295 );\r
296 if (EFI_ERROR (Status)) {\r
297 return Status;\r
298 }\r
299 }\r
300 return EFI_SUCCESS;\r
301}\r
302\r
303/**\r
304 Update the device path of "ConOut", "ConIn" and "ErrOut" \r
305 based on the new BaudRate, Data Bits, parity and Stop Bits\r
306 set.\r
307\r
308**/\r
309VOID\r
310Var_UpdateAllConsoleOption (\r
311 VOID\r
312 )\r
313{\r
314 EFI_DEVICE_PATH_PROTOCOL *OutDevicePath;\r
315 EFI_DEVICE_PATH_PROTOCOL *InpDevicePath;\r
316 EFI_DEVICE_PATH_PROTOCOL *ErrDevicePath;\r
317 EFI_STATUS Status;\r
318\r
319 GetEfiGlobalVariable2 (L"ConOut", (VOID**)&OutDevicePath, NULL);\r
320 GetEfiGlobalVariable2 (L"ConIn", (VOID**)&InpDevicePath, NULL);\r
321 GetEfiGlobalVariable2 (L"ErrOut", (VOID**)&ErrDevicePath, NULL);\r
322 if (OutDevicePath != NULL) {\r
323 ChangeVariableDevicePath (OutDevicePath);\r
324 Status = gRT->SetVariable (\r
325 L"ConOut",\r
326 &gEfiGlobalVariableGuid,\r
327 VAR_FLAG,\r
328 GetDevicePathSize (OutDevicePath),\r
329 OutDevicePath\r
330 );\r
331 ASSERT (!EFI_ERROR (Status));\r
332 }\r
333\r
334 if (InpDevicePath != NULL) {\r
335 ChangeVariableDevicePath (InpDevicePath);\r
336 Status = gRT->SetVariable (\r
337 L"ConIn",\r
338 &gEfiGlobalVariableGuid,\r
339 VAR_FLAG,\r
340 GetDevicePathSize (InpDevicePath),\r
341 InpDevicePath\r
342 );\r
343 ASSERT (!EFI_ERROR (Status));\r
344 }\r
345\r
346 if (ErrDevicePath != NULL) {\r
347 ChangeVariableDevicePath (ErrDevicePath);\r
348 Status = gRT->SetVariable (\r
349 L"ErrOut",\r
350 &gEfiGlobalVariableGuid,\r
351 VAR_FLAG,\r
352 GetDevicePathSize (ErrDevicePath),\r
353 ErrDevicePath\r
354 );\r
355 ASSERT (!EFI_ERROR (Status));\r
356 }\r
357}\r
358\r
359/**\r
360 This function delete and build multi-instance device path for\r
361 specified type of console device.\r
362\r
363 This function clear the EFI variable defined by ConsoleName and\r
364 gEfiGlobalVariableGuid. It then build the multi-instance device\r
365 path by appending the device path of the Console (In/Out/Err) instance \r
366 in ConsoleMenu. Then it scan all corresponding console device by\r
367 scanning Terminal (built from device supporting Serial I/O instances)\r
368 devices in TerminalMenu. At last, it save a EFI variable specifed\r
369 by ConsoleName and gEfiGlobalVariableGuid.\r
370\r
371 @param ConsoleName The name for the console device type. They are\r
372 usually "ConIn", "ConOut" and "ErrOut".\r
373 @param ConsoleMenu The console memu which is a list of console devices.\r
374 @param UpdatePageId The flag specifying which type of console device\r
375 to be processed.\r
376\r
377 @retval EFI_SUCCESS The function complete successfully.\r
378 @return The EFI variable can not be saved. See gRT->SetVariable for detail return information.\r
379\r
380**/\r
381EFI_STATUS\r
382Var_UpdateConsoleOption (\r
383 IN UINT16 *ConsoleName,\r
384 IN BM_MENU_OPTION *ConsoleMenu,\r
385 IN UINT16 UpdatePageId\r
386 )\r
387{\r
388 EFI_DEVICE_PATH_PROTOCOL *ConDevicePath;\r
389 BM_MENU_ENTRY *NewMenuEntry;\r
390 BM_CONSOLE_CONTEXT *NewConsoleContext;\r
391 BM_TERMINAL_CONTEXT *NewTerminalContext;\r
392 EFI_STATUS Status;\r
393 VENDOR_DEVICE_PATH Vendor;\r
394 EFI_DEVICE_PATH_PROTOCOL *TerminalDevicePath;\r
395 UINTN Index;\r
396\r
397 GetEfiGlobalVariable2 (ConsoleName, (VOID**)&ConDevicePath, NULL);\r
398 if (ConDevicePath != NULL) {\r
399 EfiLibDeleteVariable (ConsoleName, &gEfiGlobalVariableGuid);\r
400 FreePool (ConDevicePath);\r
401 ConDevicePath = NULL;\r
402 };\r
403\r
404 //\r
405 // First add all console input device from console input menu\r
406 //\r
407 for (Index = 0; Index < ConsoleMenu->MenuNumber; Index++) {\r
408 NewMenuEntry = BOpt_GetMenuEntry (ConsoleMenu, Index);\r
409\r
410 NewConsoleContext = (BM_CONSOLE_CONTEXT *) NewMenuEntry->VariableContext;\r
411 if (NewConsoleContext->IsActive) {\r
412 ConDevicePath = AppendDevicePathInstance (\r
413 ConDevicePath,\r
414 NewConsoleContext->DevicePath\r
415 );\r
416 }\r
417 }\r
418\r
419 for (Index = 0; Index < TerminalMenu.MenuNumber; Index++) {\r
420 NewMenuEntry = BOpt_GetMenuEntry (&TerminalMenu, Index);\r
421\r
422 NewTerminalContext = (BM_TERMINAL_CONTEXT *) NewMenuEntry->VariableContext;\r
423 if (((NewTerminalContext->IsConIn != 0) && (UpdatePageId == FORM_CON_IN_ID)) ||\r
424 ((NewTerminalContext->IsConOut != 0) && (UpdatePageId == FORM_CON_OUT_ID)) ||\r
425 ((NewTerminalContext->IsStdErr != 0) && (UpdatePageId == FORM_CON_ERR_ID))\r
426 ) {\r
427 Vendor.Header.Type = MESSAGING_DEVICE_PATH;\r
428 Vendor.Header.SubType = MSG_VENDOR_DP;\r
429 \r
430 ASSERT (NewTerminalContext->TerminalType < (sizeof (TerminalTypeGuid) / sizeof (TerminalTypeGuid[0])));\r
431 CopyMem (\r
432 &Vendor.Guid,\r
433 &TerminalTypeGuid[NewTerminalContext->TerminalType],\r
434 sizeof (EFI_GUID)\r
435 );\r
436 SetDevicePathNodeLength (&Vendor.Header, sizeof (VENDOR_DEVICE_PATH));\r
437 TerminalDevicePath = AppendDevicePathNode (\r
438 NewTerminalContext->DevicePath,\r
439 (EFI_DEVICE_PATH_PROTOCOL *) &Vendor\r
440 );\r
441 ASSERT (TerminalDevicePath != NULL);\r
442 ChangeTerminalDevicePath (TerminalDevicePath, TRUE);\r
443 ConDevicePath = AppendDevicePathInstance (\r
444 ConDevicePath,\r
445 TerminalDevicePath\r
446 );\r
447 }\r
448 }\r
449\r
450 if (ConDevicePath != NULL) {\r
451 Status = gRT->SetVariable (\r
452 ConsoleName,\r
453 &gEfiGlobalVariableGuid,\r
454 VAR_FLAG,\r
455 GetDevicePathSize (ConDevicePath),\r
456 ConDevicePath\r
457 );\r
458 if (EFI_ERROR (Status)) {\r
459 return Status;\r
460 }\r
461 }\r
462\r
463 return EFI_SUCCESS;\r
464\r
465}\r
466\r
467/**\r
468 This function delete and build multi-instance device path ConIn\r
469 console device.\r
470\r
471 @retval EFI_SUCCESS The function complete successfully.\r
472 @return The EFI variable can not be saved. See gRT->SetVariable for detail return information.\r
473**/\r
474EFI_STATUS\r
475Var_UpdateConsoleInpOption (\r
476 VOID\r
477 )\r
478{\r
479 return Var_UpdateConsoleOption (L"ConIn", &ConsoleInpMenu, FORM_CON_IN_ID);\r
480}\r
481\r
482/**\r
483 This function delete and build multi-instance device path ConOut\r
484 console device.\r
485\r
486 @retval EFI_SUCCESS The function complete successfully.\r
487 @return The EFI variable can not be saved. See gRT->SetVariable for detail return information.\r
488**/\r
489EFI_STATUS\r
490Var_UpdateConsoleOutOption (\r
491 VOID\r
492 )\r
493{\r
494 return Var_UpdateConsoleOption (L"ConOut", &ConsoleOutMenu, FORM_CON_OUT_ID);\r
495}\r
496\r
497/**\r
498 This function delete and build multi-instance device path ErrOut\r
499 console device.\r
500\r
501 @retval EFI_SUCCESS The function complete successfully.\r
502 @return The EFI variable can not be saved. See gRT->SetVariable for detail return information. \r
503**/\r
504EFI_STATUS\r
505Var_UpdateErrorOutOption (\r
506 VOID\r
507 )\r
508{\r
509 return Var_UpdateConsoleOption (L"ErrOut", &ConsoleErrMenu, FORM_CON_ERR_ID);\r
510}\r
511\r
512/**\r
513 This function create a currently loaded Drive Option from \r
514 the BMM. It then appends this Driver Option to the end of \r
515 the "DriverOrder" list. It append this Driver Opotion to the end\r
516 of DriverOptionMenu.\r
517\r
518 @param CallbackData The BMM context data.\r
519 @param HiiHandle The HII handle associated with the BMM formset.\r
520 @param DescriptionData The description of this driver option.\r
521 @param OptionalData The optional load option.\r
522 @param ForceReconnect If to force reconnect.\r
523\r
524 @retval EFI_OUT_OF_RESOURCES If not enought memory to complete the operation.\r
525 @retval EFI_SUCCESS If function completes successfully.\r
526\r
527**/\r
528EFI_STATUS\r
529Var_UpdateDriverOption (\r
530 IN BMM_CALLBACK_DATA *CallbackData,\r
531 IN EFI_HII_HANDLE HiiHandle,\r
532 IN UINT16 *DescriptionData,\r
533 IN UINT16 *OptionalData,\r
534 IN UINT8 ForceReconnect\r
535 )\r
536{\r
537 UINT16 Index;\r
4af04335 538 UINT16 DriverString[12];\r
4af04335
DB
539 BM_MENU_ENTRY *NewMenuEntry;\r
540 BM_LOAD_CONTEXT *NewLoadContext;\r
541 BOOLEAN OptionalDataExist;\r
542 EFI_STATUS Status;\r
2ba36b2f
DB
543 EFI_BOOT_MANAGER_LOAD_OPTION LoadOption;\r
544 UINT8 *OptionalDesData;\r
545 UINT32 OptionalDataSize;\r
4af04335
DB
546\r
547 OptionalDataExist = FALSE;\r
2ba36b2f
DB
548 OptionalDesData = NULL;\r
549 OptionalDataSize = 0;\r
4af04335
DB
550\r
551 Index = BOpt_GetDriverOptionNumber ();\r
552 UnicodeSPrint (\r
553 DriverString,\r
554 sizeof (DriverString),\r
555 L"Driver%04x",\r
556 Index\r
557 );\r
558\r
559 if (*DescriptionData == 0x0000) {\r
560 StrCpyS (DescriptionData, MAX_MENU_NUMBER, DriverString);\r
561 }\r
562\r
4af04335
DB
563 if (*OptionalData != 0x0000) {\r
564 OptionalDataExist = TRUE;\r
2ba36b2f
DB
565 OptionalDesData = (UINT8 *)OptionalData;\r
566 OptionalDataSize = (UINT32)StrSize (OptionalData);\r
4af04335
DB
567 }\r
568\r
569 NewMenuEntry = BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT);\r
570 if (NULL == NewMenuEntry) {\r
4af04335
DB
571 return EFI_OUT_OF_RESOURCES;\r
572 }\r
573\r
2ba36b2f
DB
574 Status = EfiBootManagerInitializeLoadOption (\r
575 &LoadOption,\r
576 Index,\r
577 LoadOptionTypeDriver,\r
578 LOAD_OPTION_ACTIVE | (ForceReconnect << 1),\r
579 DescriptionData,\r
580 CallbackData->LoadContext->FilePathList,\r
581 OptionalDesData,\r
582 OptionalDataSize\r
583 );\r
584 if (!EFI_ERROR (Status)){\r
585 Status = EfiBootManagerAddLoadOptionVariable (&LoadOption,(UINTN) -1 );\r
586 }\r
587\r
4af04335
DB
588 NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext;\r
589 NewLoadContext->Deleted = FALSE;\r
2ba36b2f
DB
590 NewLoadContext->Attributes = LoadOption.Attributes;\r
591 NewLoadContext->FilePathListLength = (UINT16)GetDevicePathSize (LoadOption.FilePath);\r
4af04335
DB
592\r
593 NewLoadContext->Description = AllocateZeroPool (StrSize (DescriptionData));\r
594 ASSERT (NewLoadContext->Description != NULL);\r
595 NewMenuEntry->DisplayString = NewLoadContext->Description;\r
596 CopyMem (\r
597 NewLoadContext->Description,\r
2ba36b2f 598 LoadOption.Description,\r
4af04335
DB
599 StrSize (DescriptionData)\r
600 );\r
601\r
4af04335
DB
602 NewLoadContext->FilePathList = AllocateZeroPool (GetDevicePathSize (CallbackData->LoadContext->FilePathList));\r
603 ASSERT (NewLoadContext->FilePathList != NULL);\r
4af04335
DB
604 CopyMem (\r
605 NewLoadContext->FilePathList,\r
2ba36b2f 606 LoadOption.FilePath,\r
4af04335
DB
607 GetDevicePathSize (CallbackData->LoadContext->FilePathList)\r
608 );\r
609\r
610 NewMenuEntry->HelpString = UiDevicePathToStr (NewLoadContext->FilePathList);\r
611 NewMenuEntry->OptionNumber = Index;\r
612 NewMenuEntry->DisplayStringToken = HiiSetString (HiiHandle, 0, NewMenuEntry->DisplayString, NULL);\r
613 NewMenuEntry->HelpStringToken = HiiSetString (HiiHandle, 0, NewMenuEntry->HelpString, NULL);\r
614\r
615 if (OptionalDataExist) {\r
2ba36b2f 616 NewLoadContext->OptionalData = AllocateZeroPool (LoadOption.OptionalDataSize);\r
4af04335 617 CopyMem (\r
2ba36b2f
DB
618 NewLoadContext->OptionalData,\r
619 LoadOption.OptionalData,\r
620 LoadOption.OptionalDataSize\r
4af04335
DB
621 );\r
622 }\r
623\r
4af04335
DB
624 InsertTailList (&DriverOptionMenu.Head, &NewMenuEntry->Link);\r
625 DriverOptionMenu.MenuNumber++;\r
626\r
2ba36b2f
DB
627 EfiBootManagerFreeLoadOption(&LoadOption);\r
628\r
4af04335
DB
629 return EFI_SUCCESS;\r
630}\r
631\r
632/**\r
633 This function create a currently loaded Boot Option from \r
634 the BMM. It then appends this Boot Option to the end of \r
635 the "BootOrder" list. It also append this Boot Opotion to the end\r
636 of BootOptionMenu.\r
637\r
638 @param CallbackData The BMM context data.\r
639\r
640 @retval EFI_OUT_OF_RESOURCES If not enought memory to complete the operation.\r
641 @retval EFI_SUCCESS If function completes successfully.\r
642\r
643**/\r
644EFI_STATUS\r
645Var_UpdateBootOption (\r
646 IN BMM_CALLBACK_DATA *CallbackData\r
647 )\r
648{\r
4af04335 649 UINT16 BootString[10];\r
4af04335
DB
650 UINT16 Index;\r
651 BM_MENU_ENTRY *NewMenuEntry;\r
652 BM_LOAD_CONTEXT *NewLoadContext;\r
653 BOOLEAN OptionalDataExist;\r
654 EFI_STATUS Status;\r
655 BMM_FAKE_NV_DATA *NvRamMap;\r
2ba36b2f
DB
656 EFI_BOOT_MANAGER_LOAD_OPTION LoadOption;\r
657 UINT8 *OptionalData;\r
658 UINT32 OptionalDataSize;\r
4af04335
DB
659\r
660 OptionalDataExist = FALSE;\r
661 NvRamMap = &CallbackData->BmmFakeNvData;\r
2ba36b2f
DB
662 OptionalData = NULL;\r
663 OptionalDataSize = 0;\r
4af04335
DB
664\r
665 Index = BOpt_GetBootOptionNumber () ;\r
666 UnicodeSPrint (BootString, sizeof (BootString), L"Boot%04x", Index);\r
667\r
668 if (NvRamMap->BootDescriptionData[0] == 0x0000) {\r
669 StrCpyS (NvRamMap->BootDescriptionData, sizeof (NvRamMap->BootDescriptionData) / sizeof (NvRamMap->BootDescriptionData[0]), BootString);\r
670 }\r
671\r
4af04335
DB
672 if (NvRamMap->BootOptionalData[0] != 0x0000) {\r
673 OptionalDataExist = TRUE;\r
2ba36b2f
DB
674 OptionalData = (UINT8 *)NvRamMap->BootOptionalData;\r
675 OptionalDataSize = (UINT32)StrSize (NvRamMap->BootOptionalData);\r
4af04335
DB
676 }\r
677\r
678 NewMenuEntry = BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT);\r
679 if (NULL == NewMenuEntry) {\r
680 return EFI_OUT_OF_RESOURCES;\r
681 }\r
682\r
2ba36b2f
DB
683 Status = EfiBootManagerInitializeLoadOption (\r
684 &LoadOption,\r
685 Index,\r
686 LoadOptionTypeBoot,\r
687 LOAD_OPTION_ACTIVE,\r
688 NvRamMap->BootDescriptionData,\r
689 CallbackData->LoadContext->FilePathList,\r
690 OptionalData,\r
691 OptionalDataSize\r
692 );\r
693 if (!EFI_ERROR (Status)){\r
694 Status = EfiBootManagerAddLoadOptionVariable (&LoadOption,(UINTN) -1 );\r
695 }\r
696\r
4af04335
DB
697 NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext;\r
698 NewLoadContext->Deleted = FALSE;\r
2ba36b2f
DB
699 NewLoadContext->Attributes = LoadOption.Attributes;\r
700 NewLoadContext->FilePathListLength = (UINT16) GetDevicePathSize (LoadOption.FilePath);\r
4af04335
DB
701\r
702 NewLoadContext->Description = AllocateZeroPool (StrSize (NvRamMap->BootDescriptionData));\r
703 ASSERT (NewLoadContext->Description != NULL);\r
704\r
705 NewMenuEntry->DisplayString = NewLoadContext->Description;\r
2ba36b2f 706\r
4af04335
DB
707 CopyMem (\r
708 NewLoadContext->Description,\r
2ba36b2f 709 LoadOption.Description,\r
4af04335
DB
710 StrSize (NvRamMap->BootDescriptionData)\r
711 );\r
712\r
4af04335
DB
713 NewLoadContext->FilePathList = AllocateZeroPool (GetDevicePathSize (CallbackData->LoadContext->FilePathList));\r
714 ASSERT (NewLoadContext->FilePathList != NULL);\r
4af04335
DB
715 CopyMem (\r
716 NewLoadContext->FilePathList,\r
2ba36b2f 717 LoadOption.FilePath,\r
4af04335
DB
718 GetDevicePathSize (CallbackData->LoadContext->FilePathList)\r
719 );\r
720\r
721 NewMenuEntry->HelpString = UiDevicePathToStr (NewLoadContext->FilePathList);\r
722 NewMenuEntry->OptionNumber = Index;\r
723 NewMenuEntry->DisplayStringToken = HiiSetString (CallbackData->BmmHiiHandle, 0, NewMenuEntry->DisplayString, NULL);\r
724 NewMenuEntry->HelpStringToken = HiiSetString (CallbackData->BmmHiiHandle, 0, NewMenuEntry->HelpString, NULL);\r
725\r
726 if (OptionalDataExist) {\r
2ba36b2f
DB
727 NewLoadContext->OptionalData = AllocateZeroPool (LoadOption.OptionalDataSize);\r
728 CopyMem (\r
729 NewLoadContext->OptionalData,\r
730 LoadOption.OptionalData,\r
731 LoadOption.OptionalDataSize\r
732 );\r
4af04335
DB
733 }\r
734\r
4af04335
DB
735 InsertTailList (&BootOptionMenu.Head, &NewMenuEntry->Link);\r
736 BootOptionMenu.MenuNumber++;\r
737\r
2ba36b2f
DB
738 EfiBootManagerFreeLoadOption(&LoadOption);\r
739\r
4af04335
DB
740 return EFI_SUCCESS;\r
741}\r
742\r
743/**\r
744 This function update the "BootNext" EFI Variable. If there is \r
745 no "BootNext" specified in BMM, this EFI Variable is deleted.\r
746 It also update the BMM context data specified the "BootNext"\r
747 vaule.\r
748\r
749 @param CallbackData The BMM context data.\r
750\r
751 @retval EFI_SUCCESS The function complete successfully.\r
752 @return The EFI variable can be saved. See gRT->SetVariable \r
753 for detail return information.\r
754\r
755**/\r
756EFI_STATUS\r
757Var_UpdateBootNext (\r
758 IN BMM_CALLBACK_DATA *CallbackData\r
759 )\r
760{\r
761 BM_MENU_ENTRY *NewMenuEntry;\r
762 BM_LOAD_CONTEXT *NewLoadContext;\r
763 BMM_FAKE_NV_DATA *CurrentFakeNVMap;\r
764 UINT16 Index;\r
765 EFI_STATUS Status;\r
766\r
767 Status = EFI_SUCCESS;\r
768 CurrentFakeNVMap = &CallbackData->BmmFakeNvData;\r
769 for (Index = 0; Index < BootOptionMenu.MenuNumber; Index++) {\r
770 NewMenuEntry = BOpt_GetMenuEntry (&BootOptionMenu, Index);\r
771 ASSERT (NULL != NewMenuEntry);\r
772\r
773 NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext;\r
774 NewLoadContext->IsBootNext = FALSE;\r
775 }\r
776\r
f7986526 777 if (CurrentFakeNVMap->BootNext == NONE_BOOTNEXT_VALUE) {\r
4af04335
DB
778 EfiLibDeleteVariable (L"BootNext", &gEfiGlobalVariableGuid);\r
779 return EFI_SUCCESS;\r
780 }\r
781\r
782 NewMenuEntry = BOpt_GetMenuEntry (\r
783 &BootOptionMenu,\r
784 CurrentFakeNVMap->BootNext\r
785 );\r
786 ASSERT (NewMenuEntry != NULL);\r
787\r
788 NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext;\r
789 Status = gRT->SetVariable (\r
790 L"BootNext",\r
791 &gEfiGlobalVariableGuid,\r
792 VAR_FLAG,\r
793 sizeof (UINT16),\r
794 &NewMenuEntry->OptionNumber\r
795 );\r
796 NewLoadContext->IsBootNext = TRUE;\r
797 CallbackData->BmmOldFakeNVData.BootNext = CurrentFakeNVMap->BootNext;\r
798 return Status;\r
799}\r
800\r
801/**\r
802 This function update the "BootOrder" EFI Variable based on\r
803 BMM Formset's NV map. It then refresh BootOptionMenu\r
804 with the new "BootOrder" list.\r
805\r
806 @param CallbackData The BMM context data.\r
807\r
808 @retval EFI_SUCCESS The function complete successfully.\r
809 @retval EFI_OUT_OF_RESOURCES Not enough memory to complete the function.\r
810 @return The EFI variable can not be saved. See gRT->SetVariable for detail return information.\r
811\r
812**/\r
813EFI_STATUS\r
814Var_UpdateBootOrder (\r
815 IN BMM_CALLBACK_DATA *CallbackData\r
816 )\r
817{\r
818 EFI_STATUS Status;\r
819 UINT16 Index;\r
820 UINT16 OrderIndex;\r
821 UINT16 *BootOrder;\r
822 UINTN BootOrderSize;\r
823 UINT16 OptionNumber;\r
824\r
825 //\r
826 // First check whether BootOrder is present in current configuration\r
827 //\r
828 GetEfiGlobalVariable2 (L"BootOrder", (VOID **) &BootOrder, &BootOrderSize);\r
829 if (BootOrder == NULL) {\r
830 return EFI_OUT_OF_RESOURCES;\r
831 }\r
832\r
833 ASSERT (BootOptionMenu.MenuNumber <= (sizeof (CallbackData->BmmFakeNvData.BootOptionOrder) / sizeof (CallbackData->BmmFakeNvData.BootOptionOrder[0])));\r
834\r
835 //\r
836 // OptionOrder is subset of BootOrder\r
837 //\r
838 for (OrderIndex = 0; (OrderIndex < BootOptionMenu.MenuNumber) && (CallbackData->BmmFakeNvData.BootOptionOrder[OrderIndex] != 0); OrderIndex++) {\r
839 for (Index = OrderIndex; Index < BootOrderSize / sizeof (UINT16); Index++) {\r
840 if ((BootOrder[Index] == (UINT16) (CallbackData->BmmFakeNvData.BootOptionOrder[OrderIndex] - 1)) && (OrderIndex != Index)) {\r
841 OptionNumber = BootOrder[Index];\r
842 CopyMem (&BootOrder[OrderIndex + 1], &BootOrder[OrderIndex], (Index - OrderIndex) * sizeof (UINT16));\r
843 BootOrder[OrderIndex] = OptionNumber;\r
844 }\r
845 }\r
846 }\r
847\r
848 Status = gRT->SetVariable (\r
849 L"BootOrder",\r
850 &gEfiGlobalVariableGuid,\r
851 VAR_FLAG,\r
852 BootOrderSize,\r
853 BootOrder\r
854 );\r
855 FreePool (BootOrder);\r
856 \r
857 BOpt_FreeMenu (&BootOptionMenu);\r
858 BOpt_GetBootOptions (CallbackData);\r
859\r
860 return Status;\r
861\r
862}\r
863\r
864/**\r
865 This function update the "DriverOrder" EFI Variable based on\r
866 BMM Formset's NV map. It then refresh DriverOptionMenu\r
867 with the new "DriverOrder" list.\r
868\r
869 @param CallbackData The BMM context data.\r
870\r
871 @retval EFI_SUCCESS The function complete successfully.\r
872 @retval EFI_OUT_OF_RESOURCES Not enough memory to complete the function.\r
873 @return The EFI variable can not be saved. See gRT->SetVariable for detail return information.\r
874\r
875**/\r
876EFI_STATUS\r
877Var_UpdateDriverOrder (\r
878 IN BMM_CALLBACK_DATA *CallbackData\r
879 )\r
880{\r
881 EFI_STATUS Status;\r
882 UINT16 Index;\r
883 UINT16 *DriverOrderList;\r
884 UINT16 *NewDriverOrderList;\r
885 UINTN DriverOrderListSize;\r
886\r
887 DriverOrderList = NULL;\r
888 DriverOrderListSize = 0;\r
889\r
890 //\r
891 // First check whether DriverOrder is present in current configuration\r
892 //\r
893 GetEfiGlobalVariable2 (L"DriverOrder", (VOID **) &DriverOrderList, &DriverOrderListSize);\r
894 NewDriverOrderList = AllocateZeroPool (DriverOrderListSize);\r
895\r
896 if (NewDriverOrderList == NULL) {\r
897 return EFI_OUT_OF_RESOURCES;\r
898 }\r
899 //\r
900 // If exists, delete it to hold new DriverOrder\r
901 //\r
902 if (DriverOrderList != NULL) {\r
903 EfiLibDeleteVariable (L"DriverOrder", &gEfiGlobalVariableGuid);\r
904 FreePool (DriverOrderList);\r
905 }\r
906\r
907 ASSERT (DriverOptionMenu.MenuNumber <= (sizeof (CallbackData->BmmFakeNvData.DriverOptionOrder) / sizeof (CallbackData->BmmFakeNvData.DriverOptionOrder[0])));\r
908 for (Index = 0; Index < DriverOptionMenu.MenuNumber; Index++) {\r
909 NewDriverOrderList[Index] = (UINT16) (CallbackData->BmmFakeNvData.DriverOptionOrder[Index] - 1);\r
910 }\r
911\r
912 Status = gRT->SetVariable (\r
913 L"DriverOrder",\r
914 &gEfiGlobalVariableGuid,\r
915 VAR_FLAG,\r
916 DriverOrderListSize,\r
917 NewDriverOrderList\r
918 );\r
919 if (EFI_ERROR (Status)) {\r
920 return Status;\r
921 }\r
922\r
923 BOpt_FreeMenu (&DriverOptionMenu);\r
924 BOpt_GetDriverOptions (CallbackData);\r
925 return EFI_SUCCESS;\r
926}\r
927\r
928/**\r
929 Update the Text Mode of Console.\r
930\r
931 @param CallbackData The context data for BMM.\r
932\r
933 @retval EFI_SUCCSS If the Text Mode of Console is updated.\r
934 @return Other value if the Text Mode of Console is not updated.\r
935\r
936**/\r
937EFI_STATUS\r
938Var_UpdateConMode (\r
939 IN BMM_CALLBACK_DATA *CallbackData\r
940 )\r
941{\r
942 EFI_STATUS Status;\r
943 UINTN Mode;\r
944 CONSOLE_OUT_MODE ModeInfo;\r
945\r
946 Mode = CallbackData->BmmFakeNvData.ConsoleOutMode;\r
947\r
948 Status = gST->ConOut->QueryMode (gST->ConOut, Mode, &(ModeInfo.Column), &(ModeInfo.Row));\r
949 if (!EFI_ERROR(Status)) {\r
950 Status = PcdSet32S (PcdSetupConOutColumn, (UINT32) ModeInfo.Column);\r
951 if (!EFI_ERROR (Status)) {\r
952 Status = PcdSet32S (PcdSetupConOutRow, (UINT32) ModeInfo.Row);\r
953 }\r
954 }\r
955\r
956 return Status;\r
957}\r