]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/PlatformBdsLib/QemuBootOrder.c
OvmfPkg: QemuBootOrder: initialize IsFinal variable to make MSVC happy
[mirror_edk2.git] / OvmfPkg / Library / PlatformBdsLib / QemuBootOrder.c
CommitLineData
2cd086a6 1/** @file\r
2 Rewrite the BootOrder NvVar based on QEMU's "bootorder" fw_cfg file.\r
3\r
863986b3 4 Copyright (C) 2012 - 2013, Red Hat, Inc.\r
32a22f09 5 Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
2cd086a6 6\r
7 This program and the accompanying materials are licensed and made available\r
8 under the terms and conditions of the BSD License which accompanies this\r
9 distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14**/\r
15\r
16#include <Library/QemuFwCfgLib.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/MemoryAllocationLib.h>\r
19#include <Library/GenericBdsLib.h>\r
20#include <Library/UefiBootServicesTableLib.h>\r
21#include <Library/UefiRuntimeServicesTableLib.h>\r
22#include <Library/BaseLib.h>\r
23#include <Library/PrintLib.h>\r
863986b3 24#include <Library/DevicePathLib.h>\r
2cd086a6 25#include <Guid/GlobalVariable.h>\r
26\r
27\r
28/**\r
29 OpenFirmware to UEFI device path translation output buffer size in CHAR16's.\r
30**/\r
31#define TRANSLATION_OUTPUT_SIZE 0x100\r
32\r
33\r
34/**\r
ec01afdb 35 Numbers of nodes in OpenFirmware device paths that are required and examined.\r
2cd086a6 36**/\r
ec01afdb 37#define REQUIRED_OFW_NODES 2\r
38#define EXAMINED_OFW_NODES 4\r
2cd086a6 39\r
40\r
41/**\r
42 Simple character classification routines, corresponding to POSIX class names\r
43 and ASCII encoding.\r
44**/\r
45STATIC\r
46BOOLEAN\r
47IsAlnum (\r
48 IN CHAR8 Chr\r
49 )\r
50{\r
51 return (('0' <= Chr && Chr <= '9') ||\r
52 ('A' <= Chr && Chr <= 'Z') ||\r
53 ('a' <= Chr && Chr <= 'z')\r
54 );\r
55}\r
56\r
57\r
58STATIC\r
59BOOLEAN\r
60IsDriverNamePunct (\r
61 IN CHAR8 Chr\r
62 )\r
63{\r
64 return (Chr == ',' || Chr == '.' || Chr == '_' ||\r
65 Chr == '+' || Chr == '-'\r
66 );\r
67}\r
68\r
69\r
70STATIC\r
71BOOLEAN\r
72IsPrintNotDelim (\r
73 IN CHAR8 Chr\r
74 )\r
75{\r
76 return (32 <= Chr && Chr <= 126 &&\r
77 Chr != '/' && Chr != '@' && Chr != ':');\r
78}\r
79\r
80\r
81/**\r
82 Utility types and functions.\r
83**/\r
84typedef struct {\r
85 CONST CHAR8 *Ptr; // not necessarily NUL-terminated\r
86 UINTN Len; // number of non-NUL characters\r
87} SUBSTRING;\r
88\r
89\r
90/**\r
91\r
92 Check if Substring and String have identical contents.\r
93\r
94 The function relies on the restriction that a SUBSTRING cannot have embedded\r
95 NULs either.\r
96\r
97 @param[in] Substring The SUBSTRING input to the comparison.\r
98\r
99 @param[in] String The ASCII string input to the comparison.\r
100\r
101\r
102 @return Whether the inputs have identical contents.\r
103\r
104**/\r
105STATIC\r
106BOOLEAN\r
107SubstringEq (\r
108 IN SUBSTRING Substring,\r
109 IN CONST CHAR8 *String\r
110 )\r
111{\r
112 UINTN Pos;\r
113 CONST CHAR8 *Chr;\r
114\r
115 Pos = 0;\r
116 Chr = String;\r
117\r
118 while (Pos < Substring.Len && Substring.Ptr[Pos] == *Chr) {\r
119 ++Pos;\r
120 ++Chr;\r
121 }\r
122\r
dccf7678 123 return (BOOLEAN)(Pos == Substring.Len && *Chr == '\0');\r
2cd086a6 124}\r
125\r
126\r
127/**\r
128\r
129 Parse a comma-separated list of hexadecimal integers into the elements of an\r
130 UINT32 array.\r
131\r
132 Whitespace, "0x" prefixes, leading or trailing commas, sequences of commas,\r
133 or an empty string are not allowed; they are rejected.\r
134\r
135 The function relies on ASCII encoding.\r
136\r
137 @param[in] UnitAddress The substring to parse.\r
138\r
139 @param[out] Result The array, allocated by the caller, to receive\r
140 the parsed values. This parameter may be NULL if\r
141 NumResults is zero on input.\r
142\r
143 @param[in out] NumResults On input, the number of elements allocated for\r
144 Result. On output, the number of elements it has\r
145 taken (or would have taken) to parse the string\r
146 fully.\r
147\r
148\r
149 @retval RETURN_SUCCESS UnitAddress has been fully parsed.\r
150 NumResults is set to the number of parsed\r
151 values; the corresponding elements have\r
152 been set in Result. The rest of Result's\r
153 elements are unchanged.\r
154\r
155 @retval RETURN_BUFFER_TOO_SMALL UnitAddress has been fully parsed.\r
156 NumResults is set to the number of parsed\r
157 values, but elements have been stored only\r
158 up to the input value of NumResults, which\r
159 is less than what has been parsed.\r
160\r
161 @retval RETURN_INVALID_PARAMETER Parse error. The contents of Results is\r
162 indeterminate. NumResults has not been\r
163 changed.\r
164\r
165**/\r
166STATIC\r
167RETURN_STATUS\r
168ParseUnitAddressHexList (\r
169 IN SUBSTRING UnitAddress,\r
170 OUT UINT32 *Result,\r
171 IN OUT UINTN *NumResults\r
172 )\r
173{\r
174 UINTN Entry; // number of entry currently being parsed\r
175 UINT32 EntryVal; // value being constructed for current entry\r
176 CHAR8 PrevChr; // UnitAddress character previously checked\r
177 UINTN Pos; // current position within UnitAddress\r
178 RETURN_STATUS Status;\r
179\r
180 Entry = 0;\r
181 EntryVal = 0;\r
182 PrevChr = ',';\r
183\r
184 for (Pos = 0; Pos < UnitAddress.Len; ++Pos) {\r
185 CHAR8 Chr;\r
186 INT8 Val;\r
187\r
188 Chr = UnitAddress.Ptr[Pos];\r
189 Val = ('a' <= Chr && Chr <= 'f') ? (Chr - 'a' + 10) :\r
190 ('A' <= Chr && Chr <= 'F') ? (Chr - 'A' + 10) :\r
191 ('0' <= Chr && Chr <= '9') ? (Chr - '0' ) :\r
192 -1;\r
193\r
194 if (Val >= 0) {\r
195 if (EntryVal > 0xFFFFFFF) {\r
196 return RETURN_INVALID_PARAMETER;\r
197 }\r
198 EntryVal = (EntryVal << 4) | Val;\r
199 } else if (Chr == ',') {\r
200 if (PrevChr == ',') {\r
201 return RETURN_INVALID_PARAMETER;\r
202 }\r
203 if (Entry < *NumResults) {\r
204 Result[Entry] = EntryVal;\r
205 }\r
206 ++Entry;\r
207 EntryVal = 0;\r
208 } else {\r
209 return RETURN_INVALID_PARAMETER;\r
210 }\r
211\r
212 PrevChr = Chr;\r
213 }\r
214\r
215 if (PrevChr == ',') {\r
216 return RETURN_INVALID_PARAMETER;\r
217 }\r
218 if (Entry < *NumResults) {\r
219 Result[Entry] = EntryVal;\r
220 Status = RETURN_SUCCESS;\r
221 } else {\r
222 Status = RETURN_BUFFER_TOO_SMALL;\r
223 }\r
224 ++Entry;\r
225\r
226 *NumResults = Entry;\r
227 return Status;\r
228}\r
229\r
230\r
231/**\r
232 A simple array of Boot Option ID's.\r
233**/\r
234typedef struct {\r
235 UINT16 *Data;\r
236 UINTN Allocated;\r
237 UINTN Produced;\r
238} BOOT_ORDER;\r
239\r
240\r
32a22f09
LE
241/**\r
242 Array element tracking an enumerated boot option that has the\r
243 LOAD_OPTION_ACTIVE attribute.\r
244**/\r
245typedef struct {\r
246 CONST BDS_COMMON_OPTION *BootOption; // reference only, no ownership\r
e13be08e 247 BOOLEAN Appended; // has been added to a BOOT_ORDER?\r
32a22f09
LE
248} ACTIVE_OPTION;\r
249\r
250\r
2cd086a6 251/**\r
252\r
e13be08e 253 Append an active boot option to BootOrder, reallocating the latter if needed.\r
2cd086a6 254\r
255 @param[in out] BootOrder The structure pointing to the array and holding\r
256 allocation and usage counters.\r
257\r
e13be08e
LE
258 @param[in] ActiveOption The active boot option whose ID should be\r
259 appended to the array.\r
2cd086a6 260\r
261\r
e13be08e 262 @retval RETURN_SUCCESS ID of ActiveOption appended.\r
2cd086a6 263\r
264 @retval RETURN_OUT_OF_RESOURCES Memory reallocation failed.\r
265\r
266**/\r
267STATIC\r
268RETURN_STATUS\r
269BootOrderAppend (\r
e13be08e
LE
270 IN OUT BOOT_ORDER *BootOrder,\r
271 IN OUT ACTIVE_OPTION *ActiveOption\r
2cd086a6 272 )\r
273{\r
274 if (BootOrder->Produced == BootOrder->Allocated) {\r
275 UINTN AllocatedNew;\r
276 UINT16 *DataNew;\r
277\r
278 ASSERT (BootOrder->Allocated > 0);\r
279 AllocatedNew = BootOrder->Allocated * 2;\r
280 DataNew = ReallocatePool (\r
281 BootOrder->Allocated * sizeof (*BootOrder->Data),\r
282 AllocatedNew * sizeof (*DataNew),\r
283 BootOrder->Data\r
284 );\r
285 if (DataNew == NULL) {\r
286 return RETURN_OUT_OF_RESOURCES;\r
287 }\r
288 BootOrder->Allocated = AllocatedNew;\r
289 BootOrder->Data = DataNew;\r
290 }\r
291\r
e13be08e
LE
292 BootOrder->Data[BootOrder->Produced++] =\r
293 ActiveOption->BootOption->BootCurrent;\r
294 ActiveOption->Appended = TRUE;\r
2cd086a6 295 return RETURN_SUCCESS;\r
296}\r
297\r
298\r
32a22f09
LE
299/**\r
300\r
301 Create an array of ACTIVE_OPTION elements for a boot option list.\r
302\r
303 @param[in] BootOptionList A boot option list, created with\r
304 BdsLibEnumerateAllBootOption().\r
305\r
306 @param[out] ActiveOption Pointer to the first element in the new array.\r
307 The caller is responsible for freeing the array\r
308 with FreePool() after use.\r
309\r
310 @param[out] Count Number of elements in the new array.\r
311\r
312\r
313 @retval RETURN_SUCCESS The ActiveOption array has been created.\r
314\r
315 @retval RETURN_NOT_FOUND No active entry has been found in\r
316 BootOptionList.\r
317\r
318 @retval RETURN_OUT_OF_RESOURCES Memory allocation failed.\r
319\r
320**/\r
321STATIC\r
322RETURN_STATUS\r
323CollectActiveOptions (\r
324 IN CONST LIST_ENTRY *BootOptionList,\r
325 OUT ACTIVE_OPTION **ActiveOption,\r
326 OUT UINTN *Count\r
327 )\r
328{\r
329 UINTN ScanMode;\r
330\r
331 *ActiveOption = NULL;\r
332\r
333 //\r
334 // Scan the list twice:\r
335 // - count active entries,\r
336 // - store links to active entries.\r
337 //\r
338 for (ScanMode = 0; ScanMode < 2; ++ScanMode) {\r
339 CONST LIST_ENTRY *Link;\r
340\r
341 Link = BootOptionList->ForwardLink;\r
342 *Count = 0;\r
343 while (Link != BootOptionList) {\r
344 CONST BDS_COMMON_OPTION *Current;\r
345\r
346 Current = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);\r
347 if (IS_LOAD_OPTION_TYPE (Current->Attribute, LOAD_OPTION_ACTIVE)) {\r
348 if (ScanMode == 1) {\r
349 (*ActiveOption)[*Count].BootOption = Current;\r
e13be08e 350 (*ActiveOption)[*Count].Appended = FALSE;\r
32a22f09
LE
351 }\r
352 ++*Count;\r
353 }\r
354 Link = Link->ForwardLink;\r
355 }\r
356\r
357 if (ScanMode == 0) {\r
358 if (*Count == 0) {\r
359 return RETURN_NOT_FOUND;\r
360 }\r
361 *ActiveOption = AllocatePool (*Count * sizeof **ActiveOption);\r
362 if (*ActiveOption == NULL) {\r
363 return RETURN_OUT_OF_RESOURCES;\r
364 }\r
365 }\r
366 }\r
367 return RETURN_SUCCESS;\r
368}\r
369\r
370\r
2cd086a6 371/**\r
372 OpenFirmware device path node\r
373**/\r
374typedef struct {\r
375 SUBSTRING DriverName;\r
376 SUBSTRING UnitAddress;\r
377 SUBSTRING DeviceArguments;\r
378} OFW_NODE;\r
379\r
380\r
381/**\r
382\r
383 Parse an OpenFirmware device path node into the caller-allocated OFW_NODE\r
384 structure, and advance in the input string.\r
385\r
386 The node format is mostly parsed after IEEE 1275-1994, 3.2.1.1 "Node names"\r
387 (a leading slash is expected and not returned):\r
388\r
389 /driver-name@unit-address[:device-arguments][<LF>]\r
390\r
391 A single trailing <LF> character is consumed but not returned. A trailing\r
392 <LF> or NUL character terminates the device path.\r
393\r
394 The function relies on ASCII encoding.\r
395\r
396 @param[in out] Ptr Address of the pointer pointing to the start of the\r
397 node string. After successful parsing *Ptr is set to\r
398 the byte immediately following the consumed\r
399 characters. On error it points to the byte that\r
400 caused the error. The input string is never modified.\r
401\r
402 @param[out] OfwNode The members of this structure point into the input\r
403 string, designating components of the node.\r
404 Separators are never included. If "device-arguments"\r
405 is missing, then DeviceArguments.Ptr is set to NULL.\r
406 All components that are present have nonzero length.\r
407\r
408 If the call doesn't succeed, the contents of this\r
409 structure is indeterminate.\r
410\r
411 @param[out] IsFinal In case of successul parsing, this parameter signals\r
412 whether the node just parsed is the final node in the\r
413 device path. The call after a final node will attempt\r
414 to start parsing the next path. If the call doesn't\r
415 succeed, then this parameter is not changed.\r
416\r
417\r
418 @retval RETURN_SUCCESS Parsing successful.\r
419\r
420 @retval RETURN_NOT_FOUND Parsing terminated. *Ptr was (and is)\r
421 pointing to an empty string.\r
422\r
423 @retval RETURN_INVALID_PARAMETER Parse error.\r
424\r
425**/\r
426STATIC\r
427RETURN_STATUS\r
428ParseOfwNode (\r
429 IN OUT CONST CHAR8 **Ptr,\r
430 OUT OFW_NODE *OfwNode,\r
431 OUT BOOLEAN *IsFinal\r
432 )\r
433{\r
434 //\r
435 // A leading slash is expected. End of string is tolerated.\r
436 //\r
437 switch (**Ptr) {\r
438 case '\0':\r
439 return RETURN_NOT_FOUND;\r
440\r
441 case '/':\r
442 ++*Ptr;\r
443 break;\r
444\r
445 default:\r
446 return RETURN_INVALID_PARAMETER;\r
447 }\r
448\r
449 //\r
450 // driver-name\r
451 //\r
452 OfwNode->DriverName.Ptr = *Ptr;\r
453 OfwNode->DriverName.Len = 0;\r
454 while (OfwNode->DriverName.Len < 32 &&\r
455 (IsAlnum (**Ptr) || IsDriverNamePunct (**Ptr))\r
456 ) {\r
457 ++*Ptr;\r
458 ++OfwNode->DriverName.Len;\r
459 }\r
460\r
461 if (OfwNode->DriverName.Len == 0 || OfwNode->DriverName.Len == 32) {\r
462 return RETURN_INVALID_PARAMETER;\r
463 }\r
464\r
465\r
466 //\r
467 // unit-address\r
468 //\r
469 if (**Ptr != '@') {\r
470 return RETURN_INVALID_PARAMETER;\r
471 }\r
472 ++*Ptr;\r
473\r
474 OfwNode->UnitAddress.Ptr = *Ptr;\r
475 OfwNode->UnitAddress.Len = 0;\r
476 while (IsPrintNotDelim (**Ptr)) {\r
477 ++*Ptr;\r
478 ++OfwNode->UnitAddress.Len;\r
479 }\r
480\r
481 if (OfwNode->UnitAddress.Len == 0) {\r
482 return RETURN_INVALID_PARAMETER;\r
483 }\r
484\r
485\r
486 //\r
487 // device-arguments, may be omitted\r
488 //\r
489 OfwNode->DeviceArguments.Len = 0;\r
490 if (**Ptr == ':') {\r
491 ++*Ptr;\r
492 OfwNode->DeviceArguments.Ptr = *Ptr;\r
493\r
494 while (IsPrintNotDelim (**Ptr)) {\r
495 ++*Ptr;\r
496 ++OfwNode->DeviceArguments.Len;\r
497 }\r
498\r
499 if (OfwNode->DeviceArguments.Len == 0) {\r
500 return RETURN_INVALID_PARAMETER;\r
501 }\r
502 }\r
503 else {\r
504 OfwNode->DeviceArguments.Ptr = NULL;\r
505 }\r
506\r
507 switch (**Ptr) {\r
508 case '\n':\r
509 ++*Ptr;\r
510 //\r
511 // fall through\r
512 //\r
513\r
514 case '\0':\r
515 *IsFinal = TRUE;\r
516 break;\r
517\r
518 case '/':\r
519 *IsFinal = FALSE;\r
520 break;\r
521\r
522 default:\r
523 return RETURN_INVALID_PARAMETER;\r
524 }\r
525\r
526 DEBUG ((\r
527 DEBUG_VERBOSE,\r
528 "%a: DriverName=\"%.*a\" UnitAddress=\"%.*a\" DeviceArguments=\"%.*a\"\n",\r
529 __FUNCTION__,\r
530 OfwNode->DriverName.Len, OfwNode->DriverName.Ptr,\r
531 OfwNode->UnitAddress.Len, OfwNode->UnitAddress.Ptr,\r
532 OfwNode->DeviceArguments.Len,\r
533 OfwNode->DeviceArguments.Ptr == NULL ? "" : OfwNode->DeviceArguments.Ptr\r
534 ));\r
535 return RETURN_SUCCESS;\r
536}\r
537\r
538\r
539/**\r
540\r
541 Translate an array of OpenFirmware device nodes to a UEFI device path\r
542 fragment.\r
543\r
544 @param[in] OfwNode Array of OpenFirmware device nodes to\r
545 translate, constituting the beginning of an\r
546 OpenFirmware device path.\r
547\r
548 @param[in] NumNodes Number of elements in OfwNode.\r
549\r
550 @param[out] Translated Destination array receiving the UEFI path\r
551 fragment, allocated by the caller. If the\r
552 return value differs from RETURN_SUCCESS, its\r
553 contents is indeterminate.\r
554\r
555 @param[in out] TranslatedSize On input, the number of CHAR16's in\r
556 Translated. On RETURN_SUCCESS this parameter\r
557 is assigned the number of non-NUL CHAR16's\r
558 written to Translated. In case of other return\r
559 values, TranslatedSize is indeterminate.\r
560\r
561\r
562 @retval RETURN_SUCCESS Translation successful.\r
563\r
564 @retval RETURN_BUFFER_TOO_SMALL The translation does not fit into the number\r
565 of bytes provided.\r
566\r
567 @retval RETURN_UNSUPPORTED The array of OpenFirmware device nodes can't\r
568 be translated in the current implementation.\r
569\r
570**/\r
571STATIC\r
572RETURN_STATUS\r
573TranslateOfwNodes (\r
574 IN CONST OFW_NODE *OfwNode,\r
575 IN UINTN NumNodes,\r
576 OUT CHAR16 *Translated,\r
577 IN OUT UINTN *TranslatedSize\r
578 )\r
579{\r
580 UINT32 PciDevFun[2];\r
581 UINTN NumEntries;\r
582 UINTN Written;\r
583\r
584 //\r
585 // Get PCI device and optional PCI function. Assume a single PCI root.\r
586 //\r
ec01afdb 587 if (NumNodes < REQUIRED_OFW_NODES ||\r
2cd086a6 588 !SubstringEq (OfwNode[0].DriverName, "pci")\r
589 ) {\r
590 return RETURN_UNSUPPORTED;\r
591 }\r
592 PciDevFun[1] = 0;\r
593 NumEntries = sizeof (PciDevFun) / sizeof (PciDevFun[0]);\r
594 if (ParseUnitAddressHexList (\r
595 OfwNode[1].UnitAddress,\r
596 PciDevFun,\r
597 &NumEntries\r
598 ) != RETURN_SUCCESS\r
599 ) {\r
600 return RETURN_UNSUPPORTED;\r
601 }\r
602\r
ec01afdb 603 if (NumNodes >= 4 &&\r
604 SubstringEq (OfwNode[1].DriverName, "ide") &&\r
2cd086a6 605 SubstringEq (OfwNode[2].DriverName, "drive") &&\r
606 SubstringEq (OfwNode[3].DriverName, "disk")\r
607 ) {\r
608 //\r
609 // OpenFirmware device path (IDE disk, IDE CD-ROM):\r
610 //\r
611 // /pci@i0cf8/ide@1,1/drive@0/disk@0\r
612 // ^ ^ ^ ^ ^\r
613 // | | | | master or slave\r
614 // | | | primary or secondary\r
615 // | PCI slot & function holding IDE controller\r
616 // PCI root at system bus port, PIO\r
617 //\r
618 // UEFI device path:\r
619 //\r
620 // PciRoot(0x0)/Pci(0x1,0x1)/Ata(Primary,Master,0x0)\r
621 // ^\r
622 // fixed LUN\r
623 //\r
624 UINT32 Secondary;\r
625 UINT32 Slave;\r
626\r
627 NumEntries = 1;\r
628 if (ParseUnitAddressHexList (\r
629 OfwNode[2].UnitAddress,\r
630 &Secondary,\r
631 &NumEntries\r
632 ) != RETURN_SUCCESS ||\r
633 Secondary > 1 ||\r
634 ParseUnitAddressHexList (\r
635 OfwNode[3].UnitAddress,\r
636 &Slave,\r
637 &NumEntries // reuse after previous single-element call\r
638 ) != RETURN_SUCCESS ||\r
639 Slave > 1\r
640 ) {\r
641 return RETURN_UNSUPPORTED;\r
642 }\r
643\r
644 Written = UnicodeSPrintAsciiFormat (\r
645 Translated,\r
646 *TranslatedSize * sizeof (*Translated), // BufferSize in bytes\r
647 "PciRoot(0x0)/Pci(0x%x,0x%x)/Ata(%a,%a,0x0)",\r
648 PciDevFun[0],\r
649 PciDevFun[1],\r
650 Secondary ? "Secondary" : "Primary",\r
651 Slave ? "Slave" : "Master"\r
652 );\r
ec01afdb 653 } else if (NumNodes >= 4 &&\r
654 SubstringEq (OfwNode[1].DriverName, "isa") &&\r
2cd086a6 655 SubstringEq (OfwNode[2].DriverName, "fdc") &&\r
656 SubstringEq (OfwNode[3].DriverName, "floppy")\r
657 ) {\r
658 //\r
659 // OpenFirmware device path (floppy disk):\r
660 //\r
661 // /pci@i0cf8/isa@1/fdc@03f0/floppy@0\r
662 // ^ ^ ^ ^\r
663 // | | | A: or B:\r
664 // | | ISA controller io-port (hex)\r
665 // | PCI slot holding ISA controller\r
666 // PCI root at system bus port, PIO\r
667 //\r
668 // UEFI device path:\r
669 //\r
670 // PciRoot(0x0)/Pci(0x1,0x0)/Floppy(0x0)\r
671 // ^\r
672 // ACPI UID\r
673 //\r
674 UINT32 AcpiUid;\r
675\r
676 NumEntries = 1;\r
677 if (ParseUnitAddressHexList (\r
678 OfwNode[3].UnitAddress,\r
679 &AcpiUid,\r
680 &NumEntries\r
681 ) != RETURN_SUCCESS ||\r
682 AcpiUid > 1\r
683 ) {\r
684 return RETURN_UNSUPPORTED;\r
685 }\r
686\r
687 Written = UnicodeSPrintAsciiFormat (\r
688 Translated,\r
689 *TranslatedSize * sizeof (*Translated), // BufferSize in bytes\r
690 "PciRoot(0x0)/Pci(0x%x,0x%x)/Floppy(0x%x)",\r
691 PciDevFun[0],\r
692 PciDevFun[1],\r
693 AcpiUid\r
694 );\r
e06a4cd1 695 } else if (NumNodes >= 3 &&\r
696 SubstringEq (OfwNode[1].DriverName, "scsi") &&\r
697 SubstringEq (OfwNode[2].DriverName, "disk")\r
698 ) {\r
699 //\r
700 // OpenFirmware device path (virtio-blk disk):\r
701 //\r
702 // /pci@i0cf8/scsi@6[,3]/disk@0,0\r
703 // ^ ^ ^ ^ ^\r
704 // | | | fixed\r
705 // | | PCI function corresponding to disk (optional)\r
706 // | PCI slot holding disk\r
707 // PCI root at system bus port, PIO\r
708 //\r
709 // UEFI device path prefix:\r
710 //\r
711 // PciRoot(0x0)/Pci(0x6,0x0)/HD( -- if PCI function is 0 or absent\r
712 // PciRoot(0x0)/Pci(0x6,0x3)/HD( -- if PCI function is present and nonzero\r
713 //\r
714 Written = UnicodeSPrintAsciiFormat (\r
715 Translated,\r
716 *TranslatedSize * sizeof (*Translated), // BufferSize in bytes\r
717 "PciRoot(0x0)/Pci(0x%x,0x%x)/HD(",\r
718 PciDevFun[0],\r
719 PciDevFun[1]\r
720 );\r
d2bf9913 721 } else if (NumNodes >= 4 &&\r
722 SubstringEq (OfwNode[1].DriverName, "scsi") &&\r
723 SubstringEq (OfwNode[2].DriverName, "channel") &&\r
724 SubstringEq (OfwNode[3].DriverName, "disk")\r
725 ) {\r
726 //\r
727 // OpenFirmware device path (virtio-scsi disk):\r
728 //\r
729 // /pci@i0cf8/scsi@7[,3]/channel@0/disk@2,3\r
730 // ^ ^ ^ ^ ^\r
731 // | | | | LUN\r
732 // | | | target\r
733 // | | channel (unused, fixed 0)\r
734 // | PCI slot[, function] holding SCSI controller\r
735 // PCI root at system bus port, PIO\r
736 //\r
737 // UEFI device path prefix:\r
738 //\r
739 // PciRoot(0x0)/Pci(0x7,0x0)/Scsi(0x2,0x3)\r
740 // -- if PCI function is 0 or absent\r
741 // PciRoot(0x0)/Pci(0x7,0x3)/Scsi(0x2,0x3)\r
742 // -- if PCI function is present and nonzero\r
743 //\r
744 UINT32 TargetLun[2];\r
745\r
746 TargetLun[1] = 0;\r
747 NumEntries = sizeof (TargetLun) / sizeof (TargetLun[0]);\r
748 if (ParseUnitAddressHexList (\r
749 OfwNode[3].UnitAddress,\r
750 TargetLun,\r
751 &NumEntries\r
752 ) != RETURN_SUCCESS\r
753 ) {\r
754 return RETURN_UNSUPPORTED;\r
755 }\r
756\r
757 Written = UnicodeSPrintAsciiFormat (\r
758 Translated,\r
759 *TranslatedSize * sizeof (*Translated), // BufferSize in bytes\r
760 "PciRoot(0x0)/Pci(0x%x,0x%x)/Scsi(0x%x,0x%x)",\r
761 PciDevFun[0],\r
762 PciDevFun[1],\r
763 TargetLun[0],\r
764 TargetLun[1]\r
765 );\r
e7a7e480 766 } else if (NumNodes >= 3 &&\r
767 SubstringEq (OfwNode[1].DriverName, "ethernet") &&\r
768 SubstringEq (OfwNode[2].DriverName, "ethernet-phy")\r
769 ) {\r
770 //\r
771 // OpenFirmware device path (Ethernet NIC):\r
772 //\r
773 // /pci@i0cf8/ethernet@3[,2]/ethernet-phy@0\r
774 // ^ ^ ^\r
775 // | | fixed\r
776 // | PCI slot[, function] holding Ethernet card\r
777 // PCI root at system bus port, PIO\r
778 //\r
779 // UEFI device path prefix (dependent on presence of nonzero PCI function):\r
780 //\r
781 // PciRoot(0x0)/Pci(0x3,0x0)/MAC(525400E15EEF,0x1)\r
782 // PciRoot(0x0)/Pci(0x3,0x2)/MAC(525400E15EEF,0x1)\r
783 // ^ ^\r
784 // MAC address IfType (1 == Ethernet)\r
785 //\r
786 // (Some UEFI NIC drivers don't set 0x1 for IfType.)\r
787 //\r
788 Written = UnicodeSPrintAsciiFormat (\r
789 Translated,\r
790 *TranslatedSize * sizeof (*Translated), // BufferSize in bytes\r
791 "PciRoot(0x0)/Pci(0x%x,0x%x)/MAC",\r
792 PciDevFun[0],\r
793 PciDevFun[1]\r
794 );\r
cdde6ddf 795 } else {\r
796 return RETURN_UNSUPPORTED;\r
797 }\r
2cd086a6 798\r
799 //\r
800 // There's no way to differentiate between "completely used up without\r
801 // truncation" and "truncated", so treat the former as the latter, and return\r
802 // success only for "some room left unused".\r
803 //\r
804 if (Written + 1 < *TranslatedSize) {\r
805 *TranslatedSize = Written;\r
806 return RETURN_SUCCESS;\r
807 }\r
808\r
809 return RETURN_BUFFER_TOO_SMALL;\r
810}\r
811\r
812\r
813/**\r
814\r
815 Translate an OpenFirmware device path fragment to a UEFI device path\r
816 fragment, and advance in the input string.\r
817\r
818 @param[in out] Ptr Address of the pointer pointing to the start\r
819 of the path string. After successful\r
820 translation (RETURN_SUCCESS) or at least\r
821 successful parsing (RETURN_UNSUPPORTED,\r
822 RETURN_BUFFER_TOO_SMALL), *Ptr is set to the\r
823 byte immediately following the consumed\r
824 characters. In other error cases, it points to\r
825 the byte that caused the error.\r
826\r
827 @param[out] Translated Destination array receiving the UEFI path\r
828 fragment, allocated by the caller. If the\r
829 return value differs from RETURN_SUCCESS, its\r
830 contents is indeterminate.\r
831\r
832 @param[in out] TranslatedSize On input, the number of CHAR16's in\r
833 Translated. On RETURN_SUCCESS this parameter\r
834 is assigned the number of non-NUL CHAR16's\r
835 written to Translated. In case of other return\r
836 values, TranslatedSize is indeterminate.\r
837\r
838\r
839 @retval RETURN_SUCCESS Translation successful.\r
840\r
841 @retval RETURN_BUFFER_TOO_SMALL The OpenFirmware device path was parsed\r
842 successfully, but its translation did not\r
843 fit into the number of bytes provided.\r
844 Further calls to this function are\r
845 possible.\r
846\r
847 @retval RETURN_UNSUPPORTED The OpenFirmware device path was parsed\r
848 successfully, but it can't be translated in\r
849 the current implementation. Further calls\r
850 to this function are possible.\r
851\r
c3cf8daa
LE
852 @retval RETURN_NOT_FOUND Translation terminated. On input, *Ptr was\r
853 pointing to the empty string or "HALT". On\r
854 output, *Ptr points to the empty string\r
855 (ie. "HALT" is consumed transparently when\r
856 present).\r
2cd086a6 857\r
858 @retval RETURN_INVALID_PARAMETER Parse error. This is a permanent error.\r
859\r
860**/\r
861STATIC\r
862RETURN_STATUS\r
863TranslateOfwPath (\r
864 IN OUT CONST CHAR8 **Ptr,\r
865 OUT CHAR16 *Translated,\r
866 IN OUT UINTN *TranslatedSize\r
867 )\r
868{\r
869 UINTN NumNodes;\r
870 RETURN_STATUS Status;\r
ec01afdb 871 OFW_NODE Node[EXAMINED_OFW_NODES];\r
2cd086a6 872 BOOLEAN IsFinal;\r
873 OFW_NODE Skip;\r
874\r
489c3142 875 IsFinal = FALSE;\r
2cd086a6 876 NumNodes = 0;\r
c3cf8daa
LE
877 if (AsciiStrCmp (*Ptr, "HALT") == 0) {\r
878 *Ptr += 4;\r
879 Status = RETURN_NOT_FOUND;\r
880 } else {\r
881 Status = ParseOfwNode (Ptr, &Node[NumNodes], &IsFinal);\r
882 }\r
2cd086a6 883\r
884 if (Status == RETURN_NOT_FOUND) {\r
885 DEBUG ((DEBUG_VERBOSE, "%a: no more nodes\n", __FUNCTION__));\r
886 return RETURN_NOT_FOUND;\r
887 }\r
888\r
889 while (Status == RETURN_SUCCESS && !IsFinal) {\r
890 ++NumNodes;\r
891 Status = ParseOfwNode (\r
892 Ptr,\r
ec01afdb 893 (NumNodes < EXAMINED_OFW_NODES) ? &Node[NumNodes] : &Skip,\r
2cd086a6 894 &IsFinal\r
895 );\r
896 }\r
897\r
898 switch (Status) {\r
899 case RETURN_SUCCESS:\r
900 ++NumNodes;\r
901 break;\r
902\r
903 case RETURN_INVALID_PARAMETER:\r
904 DEBUG ((DEBUG_VERBOSE, "%a: parse error\n", __FUNCTION__));\r
905 return RETURN_INVALID_PARAMETER;\r
906\r
907 default:\r
908 ASSERT (0);\r
909 }\r
910\r
911 Status = TranslateOfwNodes (\r
912 Node,\r
ec01afdb 913 NumNodes < EXAMINED_OFW_NODES ? NumNodes : EXAMINED_OFW_NODES,\r
2cd086a6 914 Translated,\r
915 TranslatedSize);\r
916 switch (Status) {\r
917 case RETURN_SUCCESS:\r
918 DEBUG ((DEBUG_VERBOSE, "%a: success: \"%s\"\n", __FUNCTION__, Translated));\r
919 break;\r
920\r
921 case RETURN_BUFFER_TOO_SMALL:\r
922 DEBUG ((DEBUG_VERBOSE, "%a: buffer too small\n", __FUNCTION__));\r
923 break;\r
924\r
925 case RETURN_UNSUPPORTED:\r
926 DEBUG ((DEBUG_VERBOSE, "%a: unsupported\n", __FUNCTION__));\r
927 break;\r
928\r
929 default:\r
930 ASSERT (0);\r
931 }\r
932 return Status;\r
933}\r
934\r
935\r
936/**\r
937\r
938 Convert the UEFI DevicePath to full text representation with DevPathToText,\r
939 then match the UEFI device path fragment in Translated against it.\r
940\r
941 @param[in] Translated UEFI device path fragment, translated from\r
942 OpenFirmware format, to search for.\r
943\r
944 @param[in] TranslatedLength The length of Translated in CHAR16's.\r
945\r
946 @param[in] DevicePath Boot option device path whose textual rendering\r
947 to search in.\r
948\r
949 @param[in] DevPathToText Binary-to-text conversion protocol for DevicePath.\r
950\r
951\r
952 @retval TRUE If Translated was found at the beginning of DevicePath after\r
953 converting the latter to text.\r
954\r
955 @retval FALSE If DevicePath was NULL, or it could not be converted, or there\r
956 was no match.\r
957\r
958**/\r
959STATIC\r
960BOOLEAN\r
961Match (\r
962 IN CONST CHAR16 *Translated,\r
963 IN UINTN TranslatedLength,\r
863986b3 964 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
2cd086a6 965 )\r
966{\r
967 CHAR16 *Converted;\r
968 BOOLEAN Result;\r
969\r
863986b3
RN
970 Converted = ConvertDevicePathToText (\r
971 DevicePath,\r
972 FALSE, // DisplayOnly\r
973 FALSE // AllowShortcuts\r
974 );\r
2cd086a6 975 if (Converted == NULL) {\r
976 return FALSE;\r
977 }\r
978\r
64378ce1
LE
979 //\r
980 // Attempt to expand any relative UEFI device path starting with HD() to an\r
981 // absolute device path first. The logic imitates BdsLibBootViaBootOption().\r
982 // We don't have to free the absolute device path,\r
983 // BdsExpandPartitionPartialDevicePathToFull() has internal caching.\r
984 //\r
985 Result = FALSE;\r
986 if (DevicePathType (DevicePath) == MEDIA_DEVICE_PATH &&\r
987 DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP) {\r
988 EFI_DEVICE_PATH_PROTOCOL *AbsDevicePath;\r
989 CHAR16 *AbsConverted;\r
990\r
991 AbsDevicePath = BdsExpandPartitionPartialDevicePathToFull (\r
992 (HARDDRIVE_DEVICE_PATH *) DevicePath);\r
993 if (AbsDevicePath == NULL) {\r
994 goto Exit;\r
995 }\r
996 AbsConverted = ConvertDevicePathToText (AbsDevicePath, FALSE, FALSE);\r
997 if (AbsConverted == NULL) {\r
998 goto Exit;\r
999 }\r
1000 DEBUG ((DEBUG_VERBOSE,\r
1001 "%a: expanded relative device path \"%s\" for prefix matching\n",\r
1002 __FUNCTION__, Converted));\r
1003 FreePool (Converted);\r
1004 Converted = AbsConverted;\r
1005 }\r
1006\r
2cd086a6 1007 //\r
1008 // Is Translated a prefix of Converted?\r
1009 //\r
dccf7678 1010 Result = (BOOLEAN)(StrnCmp (Converted, Translated, TranslatedLength) == 0);\r
2cd086a6 1011 DEBUG ((\r
1012 DEBUG_VERBOSE,\r
1013 "%a: against \"%s\": %a\n",\r
1014 __FUNCTION__,\r
1015 Converted,\r
1016 Result ? "match" : "no match"\r
1017 ));\r
64378ce1 1018Exit:\r
2cd086a6 1019 FreePool (Converted);\r
1020 return Result;\r
1021}\r
1022\r
1023\r
838b5b00
LE
1024/**\r
1025 Append some of the unselected active boot options to the boot order.\r
1026\r
1027 This function should accommodate any further policy changes in "boot option\r
1028 survival". Currently we're adding back everything that starts with neither\r
1029 PciRoot() nor HD().\r
1030\r
1031 @param[in,out] BootOrder The structure holding the boot order to\r
1032 complete. The caller is responsible for\r
1033 initializing (and potentially populating) it\r
1034 before calling this function.\r
1035\r
1036 @param[in,out] ActiveOption The array of active boot options to scan.\r
1037 Entries marked as Appended will be skipped.\r
1038 Those of the rest that satisfy the survival\r
1039 policy will be added to BootOrder with\r
1040 BootOrderAppend().\r
1041\r
1042 @param[in] ActiveCount Number of elements in ActiveOption.\r
1043\r
1044\r
1045 @retval RETURN_SUCCESS BootOrder has been extended with any eligible boot\r
1046 options.\r
1047\r
1048 @return Error codes returned by BootOrderAppend().\r
1049**/\r
1050STATIC\r
1051RETURN_STATUS\r
1052BootOrderComplete (\r
1053 IN OUT BOOT_ORDER *BootOrder,\r
1054 IN OUT ACTIVE_OPTION *ActiveOption,\r
1055 IN UINTN ActiveCount\r
1056 )\r
1057{\r
1058 RETURN_STATUS Status;\r
1059 UINTN Idx;\r
1060\r
1061 Status = RETURN_SUCCESS;\r
1062 Idx = 0;\r
1063 while (!RETURN_ERROR (Status) && Idx < ActiveCount) {\r
1064 if (!ActiveOption[Idx].Appended) {\r
1065 CONST BDS_COMMON_OPTION *Current;\r
1066 CONST EFI_DEVICE_PATH_PROTOCOL *FirstNode;\r
1067\r
1068 Current = ActiveOption[Idx].BootOption;\r
1069 FirstNode = Current->DevicePath;\r
1070 if (FirstNode != NULL) {\r
1071 CHAR16 *Converted;\r
1072 STATIC CHAR16 ConvFallBack[] = L"<unable to convert>";\r
1073 BOOLEAN Keep;\r
1074\r
1075 Converted = ConvertDevicePathToText (FirstNode, FALSE, FALSE);\r
1076 if (Converted == NULL) {\r
1077 Converted = ConvFallBack;\r
1078 }\r
1079\r
1080 Keep = TRUE;\r
1081 if (DevicePathType(FirstNode) == MEDIA_DEVICE_PATH &&\r
1082 DevicePathSubType(FirstNode) == MEDIA_HARDDRIVE_DP) {\r
1083 //\r
1084 // drop HD()\r
1085 //\r
1086 Keep = FALSE;\r
1087 } else if (DevicePathType(FirstNode) == ACPI_DEVICE_PATH &&\r
1088 DevicePathSubType(FirstNode) == ACPI_DP) {\r
1089 ACPI_HID_DEVICE_PATH *Acpi;\r
1090\r
1091 Acpi = (ACPI_HID_DEVICE_PATH *) FirstNode;\r
1092 if ((Acpi->HID & PNP_EISA_ID_MASK) == PNP_EISA_ID_CONST &&\r
1093 EISA_ID_TO_NUM (Acpi->HID) == 0x0a03) {\r
1094 //\r
1095 // drop PciRoot()\r
1096 //\r
1097 Keep = FALSE;\r
1098 }\r
1099 }\r
1100\r
1101 if (Keep) {\r
1102 Status = BootOrderAppend (BootOrder, &ActiveOption[Idx]);\r
1103 if (!RETURN_ERROR (Status)) {\r
1104 DEBUG ((DEBUG_VERBOSE, "%a: keeping \"%s\"\n", __FUNCTION__,\r
1105 Converted));\r
1106 }\r
1107 } else {\r
1108 DEBUG ((DEBUG_VERBOSE, "%a: dropping \"%s\"\n", __FUNCTION__,\r
1109 Converted));\r
1110 }\r
1111\r
1112 if (Converted != ConvFallBack) {\r
1113 FreePool (Converted);\r
1114 }\r
1115 }\r
1116 }\r
1117 ++Idx;\r
1118 }\r
1119 return Status;\r
1120}\r
1121\r
1122\r
2cd086a6 1123/**\r
1124\r
1125 Set the boot order based on configuration retrieved from QEMU.\r
1126\r
1127 Attempt to retrieve the "bootorder" fw_cfg file from QEMU. Translate the\r
1128 OpenFirmware device paths therein to UEFI device path fragments. Match the\r
1129 translated fragments against BootOptionList, and rewrite the BootOrder NvVar\r
1130 so that it corresponds to the order described in fw_cfg.\r
1131\r
1132 @param[in] BootOptionList A boot option list, created with\r
1133 BdsLibEnumerateAllBootOption ().\r
1134\r
1135\r
1136 @retval RETURN_SUCCESS BootOrder NvVar rewritten.\r
1137\r
1138 @retval RETURN_UNSUPPORTED QEMU's fw_cfg is not supported.\r
1139\r
1140 @retval RETURN_NOT_FOUND Empty or nonexistent "bootorder" fw_cfg\r
1141 file, or no match found between the\r
1142 "bootorder" fw_cfg file and BootOptionList.\r
1143\r
1144 @retval RETURN_INVALID_PARAMETER Parse error in the "bootorder" fw_cfg file.\r
1145\r
1146 @retval RETURN_OUT_OF_RESOURCES Memory allocation failed.\r
1147\r
1148 @return Values returned by gBS->LocateProtocol ()\r
1149 or gRT->SetVariable ().\r
1150\r
1151**/\r
1152RETURN_STATUS\r
1153SetBootOrderFromQemu (\r
1154 IN CONST LIST_ENTRY *BootOptionList\r
1155 )\r
1156{\r
1157 RETURN_STATUS Status;\r
2cd086a6 1158 FIRMWARE_CONFIG_ITEM FwCfgItem;\r
1159 UINTN FwCfgSize;\r
1160 CHAR8 *FwCfg;\r
1161 CONST CHAR8 *FwCfgPtr;\r
1162\r
1163 BOOT_ORDER BootOrder;\r
32a22f09
LE
1164 ACTIVE_OPTION *ActiveOption;\r
1165 UINTN ActiveCount;\r
2cd086a6 1166\r
1167 UINTN TranslatedSize;\r
1168 CHAR16 Translated[TRANSLATION_OUTPUT_SIZE];\r
1169\r
2cd086a6 1170 Status = QemuFwCfgFindFile ("bootorder", &FwCfgItem, &FwCfgSize);\r
1171 if (Status != RETURN_SUCCESS) {\r
1172 return Status;\r
1173 }\r
1174\r
1175 if (FwCfgSize == 0) {\r
1176 return RETURN_NOT_FOUND;\r
1177 }\r
1178\r
1179 FwCfg = AllocatePool (FwCfgSize);\r
1180 if (FwCfg == NULL) {\r
1181 return RETURN_OUT_OF_RESOURCES;\r
1182 }\r
1183\r
1184 QemuFwCfgSelectItem (FwCfgItem);\r
1185 QemuFwCfgReadBytes (FwCfgSize, FwCfg);\r
1186 if (FwCfg[FwCfgSize - 1] != '\0') {\r
1187 Status = RETURN_INVALID_PARAMETER;\r
1188 goto ErrorFreeFwCfg;\r
1189 }\r
1190\r
1191 DEBUG ((DEBUG_VERBOSE, "%a: FwCfg:\n", __FUNCTION__));\r
1192 DEBUG ((DEBUG_VERBOSE, "%a\n", FwCfg));\r
1193 DEBUG ((DEBUG_VERBOSE, "%a: FwCfg: <end>\n", __FUNCTION__));\r
1194 FwCfgPtr = FwCfg;\r
1195\r
1196 BootOrder.Produced = 0;\r
1197 BootOrder.Allocated = 1;\r
1198 BootOrder.Data = AllocatePool (\r
1199 BootOrder.Allocated * sizeof (*BootOrder.Data)\r
1200 );\r
1201 if (BootOrder.Data == NULL) {\r
1202 Status = RETURN_OUT_OF_RESOURCES;\r
1203 goto ErrorFreeFwCfg;\r
1204 }\r
1205\r
32a22f09
LE
1206 Status = CollectActiveOptions (BootOptionList, &ActiveOption, &ActiveCount);\r
1207 if (RETURN_ERROR (Status)) {\r
1208 goto ErrorFreeBootOrder;\r
1209 }\r
1210\r
2cd086a6 1211 //\r
1212 // translate each OpenFirmware path\r
1213 //\r
1214 TranslatedSize = sizeof (Translated) / sizeof (Translated[0]);\r
1215 Status = TranslateOfwPath (&FwCfgPtr, Translated, &TranslatedSize);\r
1216 while (Status == RETURN_SUCCESS ||\r
1217 Status == RETURN_UNSUPPORTED ||\r
1218 Status == RETURN_BUFFER_TOO_SMALL) {\r
1219 if (Status == RETURN_SUCCESS) {\r
32a22f09 1220 UINTN Idx;\r
2cd086a6 1221\r
1222 //\r
32a22f09 1223 // match translated OpenFirmware path against all active boot options\r
2cd086a6 1224 //\r
32a22f09
LE
1225 for (Idx = 0; Idx < ActiveCount; ++Idx) {\r
1226 if (Match (\r
2cd086a6 1227 Translated,\r
1228 TranslatedSize, // contains length, not size, in CHAR16's here\r
32a22f09 1229 ActiveOption[Idx].BootOption->DevicePath\r
2cd086a6 1230 )\r
1231 ) {\r
1232 //\r
1233 // match found, store ID and continue with next OpenFirmware path\r
1234 //\r
e13be08e 1235 Status = BootOrderAppend (&BootOrder, &ActiveOption[Idx]);\r
2cd086a6 1236 if (Status != RETURN_SUCCESS) {\r
32a22f09 1237 goto ErrorFreeActiveOption;\r
2cd086a6 1238 }\r
1239 break;\r
1240 }\r
32a22f09 1241 } // scanned all active boot options\r
2cd086a6 1242 } // translation successful\r
1243\r
1244 TranslatedSize = sizeof (Translated) / sizeof (Translated[0]);\r
1245 Status = TranslateOfwPath (&FwCfgPtr, Translated, &TranslatedSize);\r
1246 } // scanning of OpenFirmware paths done\r
1247\r
1248 if (Status == RETURN_NOT_FOUND && BootOrder.Produced > 0) {\r
1249 //\r
1250 // No more OpenFirmware paths, some matches found: rewrite BootOrder NvVar.\r
838b5b00
LE
1251 // Some of the active boot options that have not been selected over fw_cfg\r
1252 // should be preserved at the end of the boot order.\r
1253 //\r
1254 Status = BootOrderComplete (&BootOrder, ActiveOption, ActiveCount);\r
1255 if (RETURN_ERROR (Status)) {\r
1256 goto ErrorFreeActiveOption;\r
1257 }\r
1258\r
1259 //\r
2cd086a6 1260 // See Table 10 in the UEFI Spec 2.3.1 with Errata C for the required\r
1261 // attributes.\r
1262 //\r
1263 Status = gRT->SetVariable (\r
1264 L"BootOrder",\r
1265 &gEfiGlobalVariableGuid,\r
1266 EFI_VARIABLE_NON_VOLATILE |\r
1267 EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
1268 EFI_VARIABLE_RUNTIME_ACCESS,\r
1269 BootOrder.Produced * sizeof (*BootOrder.Data),\r
1270 BootOrder.Data\r
1271 );\r
1272 DEBUG ((\r
1273 DEBUG_INFO,\r
1274 "%a: setting BootOrder: %a\n",\r
1275 __FUNCTION__,\r
1276 Status == EFI_SUCCESS ? "success" : "error"\r
1277 ));\r
1278 }\r
1279\r
32a22f09
LE
1280ErrorFreeActiveOption:\r
1281 FreePool (ActiveOption);\r
1282\r
2cd086a6 1283ErrorFreeBootOrder:\r
1284 FreePool (BootOrder.Data);\r
1285\r
1286ErrorFreeFwCfg:\r
1287 FreePool (FwCfg);\r
1288\r
1289 return Status;\r
1290}\r