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