2 Rewrite the BootOrder NvVar based on QEMU's "bootorder" fw_cfg file.
4 Copyright (C) 2012 - 2013, Red Hat, Inc.
5 Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/QemuFwCfgLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/GenericBdsLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/UefiRuntimeServicesTableLib.h>
22 #include <Library/BaseLib.h>
23 #include <Library/PrintLib.h>
24 #include <Library/DevicePathLib.h>
25 #include <Guid/GlobalVariable.h>
29 OpenFirmware to UEFI device path translation output buffer size in CHAR16's.
31 #define TRANSLATION_OUTPUT_SIZE 0x100
35 Numbers of nodes in OpenFirmware device paths that are required and examined.
37 #define REQUIRED_OFW_NODES 2
38 #define EXAMINED_OFW_NODES 4
42 Simple character classification routines, corresponding to POSIX class names
51 return (('0' <= Chr
&& Chr
<= '9') ||
52 ('A' <= Chr
&& Chr
<= 'Z') ||
53 ('a' <= Chr
&& Chr
<= 'z')
64 return (Chr
== ',' || Chr
== '.' || Chr
== '_' ||
65 Chr
== '+' || Chr
== '-'
76 return (32 <= Chr
&& Chr
<= 126 &&
77 Chr
!= '/' && Chr
!= '@' && Chr
!= ':');
82 Utility types and functions.
85 CONST CHAR8
*Ptr
; // not necessarily NUL-terminated
86 UINTN Len
; // number of non-NUL characters
92 Check if Substring and String have identical contents.
94 The function relies on the restriction that a SUBSTRING cannot have embedded
97 @param[in] Substring The SUBSTRING input to the comparison.
99 @param[in] String The ASCII string input to the comparison.
102 @return Whether the inputs have identical contents.
108 IN SUBSTRING Substring
,
109 IN CONST CHAR8
*String
118 while (Pos
< Substring
.Len
&& Substring
.Ptr
[Pos
] == *Chr
) {
123 return (BOOLEAN
)(Pos
== Substring
.Len
&& *Chr
== '\0');
129 Parse a comma-separated list of hexadecimal integers into the elements of an
132 Whitespace, "0x" prefixes, leading or trailing commas, sequences of commas,
133 or an empty string are not allowed; they are rejected.
135 The function relies on ASCII encoding.
137 @param[in] UnitAddress The substring to parse.
139 @param[out] Result The array, allocated by the caller, to receive
140 the parsed values. This parameter may be NULL if
141 NumResults is zero on input.
143 @param[in out] NumResults On input, the number of elements allocated for
144 Result. On output, the number of elements it has
145 taken (or would have taken) to parse the string
149 @retval RETURN_SUCCESS UnitAddress has been fully parsed.
150 NumResults is set to the number of parsed
151 values; the corresponding elements have
152 been set in Result. The rest of Result's
153 elements are unchanged.
155 @retval RETURN_BUFFER_TOO_SMALL UnitAddress has been fully parsed.
156 NumResults is set to the number of parsed
157 values, but elements have been stored only
158 up to the input value of NumResults, which
159 is less than what has been parsed.
161 @retval RETURN_INVALID_PARAMETER Parse error. The contents of Results is
162 indeterminate. NumResults has not been
168 ParseUnitAddressHexList (
169 IN SUBSTRING UnitAddress
,
171 IN OUT UINTN
*NumResults
174 UINTN Entry
; // number of entry currently being parsed
175 UINT32 EntryVal
; // value being constructed for current entry
176 CHAR8 PrevChr
; // UnitAddress character previously checked
177 UINTN Pos
; // current position within UnitAddress
178 RETURN_STATUS Status
;
184 for (Pos
= 0; Pos
< UnitAddress
.Len
; ++Pos
) {
188 Chr
= UnitAddress
.Ptr
[Pos
];
189 Val
= ('a' <= Chr
&& Chr
<= 'f') ? (Chr
- 'a' + 10) :
190 ('A' <= Chr
&& Chr
<= 'F') ? (Chr
- 'A' + 10) :
191 ('0' <= Chr
&& Chr
<= '9') ? (Chr
- '0' ) :
195 if (EntryVal
> 0xFFFFFFF) {
196 return RETURN_INVALID_PARAMETER
;
198 EntryVal
= (EntryVal
<< 4) | Val
;
199 } else if (Chr
== ',') {
200 if (PrevChr
== ',') {
201 return RETURN_INVALID_PARAMETER
;
203 if (Entry
< *NumResults
) {
204 Result
[Entry
] = EntryVal
;
209 return RETURN_INVALID_PARAMETER
;
215 if (PrevChr
== ',') {
216 return RETURN_INVALID_PARAMETER
;
218 if (Entry
< *NumResults
) {
219 Result
[Entry
] = EntryVal
;
220 Status
= RETURN_SUCCESS
;
222 Status
= RETURN_BUFFER_TOO_SMALL
;
232 A simple array of Boot Option ID's.
242 Array element tracking an enumerated boot option that has the
243 LOAD_OPTION_ACTIVE attribute.
246 CONST BDS_COMMON_OPTION
*BootOption
; // reference only, no ownership
247 BOOLEAN Appended
; // has been added to a BOOT_ORDER?
253 Append an active boot option to BootOrder, reallocating the latter if needed.
255 @param[in out] BootOrder The structure pointing to the array and holding
256 allocation and usage counters.
258 @param[in] ActiveOption The active boot option whose ID should be
259 appended to the array.
262 @retval RETURN_SUCCESS ID of ActiveOption appended.
264 @retval RETURN_OUT_OF_RESOURCES Memory reallocation failed.
270 IN OUT BOOT_ORDER
*BootOrder
,
271 IN OUT ACTIVE_OPTION
*ActiveOption
274 if (BootOrder
->Produced
== BootOrder
->Allocated
) {
278 ASSERT (BootOrder
->Allocated
> 0);
279 AllocatedNew
= BootOrder
->Allocated
* 2;
280 DataNew
= ReallocatePool (
281 BootOrder
->Allocated
* sizeof (*BootOrder
->Data
),
282 AllocatedNew
* sizeof (*DataNew
),
285 if (DataNew
== NULL
) {
286 return RETURN_OUT_OF_RESOURCES
;
288 BootOrder
->Allocated
= AllocatedNew
;
289 BootOrder
->Data
= DataNew
;
292 BootOrder
->Data
[BootOrder
->Produced
++] =
293 ActiveOption
->BootOption
->BootCurrent
;
294 ActiveOption
->Appended
= TRUE
;
295 return RETURN_SUCCESS
;
301 Create an array of ACTIVE_OPTION elements for a boot option list.
303 @param[in] BootOptionList A boot option list, created with
304 BdsLibEnumerateAllBootOption().
306 @param[out] ActiveOption Pointer to the first element in the new array.
307 The caller is responsible for freeing the array
308 with FreePool() after use.
310 @param[out] Count Number of elements in the new array.
313 @retval RETURN_SUCCESS The ActiveOption array has been created.
315 @retval RETURN_NOT_FOUND No active entry has been found in
318 @retval RETURN_OUT_OF_RESOURCES Memory allocation failed.
323 CollectActiveOptions (
324 IN CONST LIST_ENTRY
*BootOptionList
,
325 OUT ACTIVE_OPTION
**ActiveOption
,
331 *ActiveOption
= NULL
;
334 // Scan the list twice:
335 // - count active entries,
336 // - store links to active entries.
338 for (ScanMode
= 0; ScanMode
< 2; ++ScanMode
) {
339 CONST LIST_ENTRY
*Link
;
341 Link
= BootOptionList
->ForwardLink
;
343 while (Link
!= BootOptionList
) {
344 CONST BDS_COMMON_OPTION
*Current
;
346 Current
= CR (Link
, BDS_COMMON_OPTION
, Link
, BDS_LOAD_OPTION_SIGNATURE
);
347 if (IS_LOAD_OPTION_TYPE (Current
->Attribute
, LOAD_OPTION_ACTIVE
)) {
349 (*ActiveOption
)[*Count
].BootOption
= Current
;
350 (*ActiveOption
)[*Count
].Appended
= FALSE
;
354 Link
= Link
->ForwardLink
;
359 return RETURN_NOT_FOUND
;
361 *ActiveOption
= AllocatePool (*Count
* sizeof **ActiveOption
);
362 if (*ActiveOption
== NULL
) {
363 return RETURN_OUT_OF_RESOURCES
;
367 return RETURN_SUCCESS
;
372 OpenFirmware device path node
375 SUBSTRING DriverName
;
376 SUBSTRING UnitAddress
;
377 SUBSTRING DeviceArguments
;
383 Parse an OpenFirmware device path node into the caller-allocated OFW_NODE
384 structure, and advance in the input string.
386 The node format is mostly parsed after IEEE 1275-1994, 3.2.1.1 "Node names"
387 (a leading slash is expected and not returned):
389 /driver-name@unit-address[:device-arguments][<LF>]
391 A single trailing <LF> character is consumed but not returned. A trailing
392 <LF> or NUL character terminates the device path.
394 The function relies on ASCII encoding.
396 @param[in out] Ptr Address of the pointer pointing to the start of the
397 node string. After successful parsing *Ptr is set to
398 the byte immediately following the consumed
399 characters. On error it points to the byte that
400 caused the error. The input string is never modified.
402 @param[out] OfwNode The members of this structure point into the input
403 string, designating components of the node.
404 Separators are never included. If "device-arguments"
405 is missing, then DeviceArguments.Ptr is set to NULL.
406 All components that are present have nonzero length.
408 If the call doesn't succeed, the contents of this
409 structure is indeterminate.
411 @param[out] IsFinal In case of successul parsing, this parameter signals
412 whether the node just parsed is the final node in the
413 device path. The call after a final node will attempt
414 to start parsing the next path. If the call doesn't
415 succeed, then this parameter is not changed.
418 @retval RETURN_SUCCESS Parsing successful.
420 @retval RETURN_NOT_FOUND Parsing terminated. *Ptr was (and is)
421 pointing to an empty string.
423 @retval RETURN_INVALID_PARAMETER Parse error.
429 IN OUT CONST CHAR8
**Ptr
,
430 OUT OFW_NODE
*OfwNode
,
435 // A leading slash is expected. End of string is tolerated.
439 return RETURN_NOT_FOUND
;
446 return RETURN_INVALID_PARAMETER
;
452 OfwNode
->DriverName
.Ptr
= *Ptr
;
453 OfwNode
->DriverName
.Len
= 0;
454 while (OfwNode
->DriverName
.Len
< 32 &&
455 (IsAlnum (**Ptr
) || IsDriverNamePunct (**Ptr
))
458 ++OfwNode
->DriverName
.Len
;
461 if (OfwNode
->DriverName
.Len
== 0 || OfwNode
->DriverName
.Len
== 32) {
462 return RETURN_INVALID_PARAMETER
;
470 return RETURN_INVALID_PARAMETER
;
474 OfwNode
->UnitAddress
.Ptr
= *Ptr
;
475 OfwNode
->UnitAddress
.Len
= 0;
476 while (IsPrintNotDelim (**Ptr
)) {
478 ++OfwNode
->UnitAddress
.Len
;
481 if (OfwNode
->UnitAddress
.Len
== 0) {
482 return RETURN_INVALID_PARAMETER
;
487 // device-arguments, may be omitted
489 OfwNode
->DeviceArguments
.Len
= 0;
492 OfwNode
->DeviceArguments
.Ptr
= *Ptr
;
494 while (IsPrintNotDelim (**Ptr
)) {
496 ++OfwNode
->DeviceArguments
.Len
;
499 if (OfwNode
->DeviceArguments
.Len
== 0) {
500 return RETURN_INVALID_PARAMETER
;
504 OfwNode
->DeviceArguments
.Ptr
= NULL
;
523 return RETURN_INVALID_PARAMETER
;
528 "%a: DriverName=\"%.*a\" UnitAddress=\"%.*a\" DeviceArguments=\"%.*a\"\n",
530 OfwNode
->DriverName
.Len
, OfwNode
->DriverName
.Ptr
,
531 OfwNode
->UnitAddress
.Len
, OfwNode
->UnitAddress
.Ptr
,
532 OfwNode
->DeviceArguments
.Len
,
533 OfwNode
->DeviceArguments
.Ptr
== NULL
? "" : OfwNode
->DeviceArguments
.Ptr
535 return RETURN_SUCCESS
;
541 Translate an array of OpenFirmware device nodes to a UEFI device path
544 @param[in] OfwNode Array of OpenFirmware device nodes to
545 translate, constituting the beginning of an
546 OpenFirmware device path.
548 @param[in] NumNodes Number of elements in OfwNode.
550 @param[out] Translated Destination array receiving the UEFI path
551 fragment, allocated by the caller. If the
552 return value differs from RETURN_SUCCESS, its
553 contents is indeterminate.
555 @param[in out] TranslatedSize On input, the number of CHAR16's in
556 Translated. On RETURN_SUCCESS this parameter
557 is assigned the number of non-NUL CHAR16's
558 written to Translated. In case of other return
559 values, TranslatedSize is indeterminate.
562 @retval RETURN_SUCCESS Translation successful.
564 @retval RETURN_BUFFER_TOO_SMALL The translation does not fit into the number
567 @retval RETURN_UNSUPPORTED The array of OpenFirmware device nodes can't
568 be translated in the current implementation.
574 IN CONST OFW_NODE
*OfwNode
,
576 OUT CHAR16
*Translated
,
577 IN OUT UINTN
*TranslatedSize
585 // Get PCI device and optional PCI function. Assume a single PCI root.
587 if (NumNodes
< REQUIRED_OFW_NODES
||
588 !SubstringEq (OfwNode
[0].DriverName
, "pci")
590 return RETURN_UNSUPPORTED
;
593 NumEntries
= sizeof (PciDevFun
) / sizeof (PciDevFun
[0]);
594 if (ParseUnitAddressHexList (
595 OfwNode
[1].UnitAddress
,
600 return RETURN_UNSUPPORTED
;
604 SubstringEq (OfwNode
[1].DriverName
, "ide") &&
605 SubstringEq (OfwNode
[2].DriverName
, "drive") &&
606 SubstringEq (OfwNode
[3].DriverName
, "disk")
609 // OpenFirmware device path (IDE disk, IDE CD-ROM):
611 // /pci@i0cf8/ide@1,1/drive@0/disk@0
613 // | | | | master or slave
614 // | | | primary or secondary
615 // | PCI slot & function holding IDE controller
616 // PCI root at system bus port, PIO
620 // PciRoot(0x0)/Pci(0x1,0x1)/Ata(Primary,Master,0x0)
628 if (ParseUnitAddressHexList (
629 OfwNode
[2].UnitAddress
,
632 ) != RETURN_SUCCESS
||
634 ParseUnitAddressHexList (
635 OfwNode
[3].UnitAddress
,
637 &NumEntries
// reuse after previous single-element call
638 ) != RETURN_SUCCESS
||
641 return RETURN_UNSUPPORTED
;
644 Written
= UnicodeSPrintAsciiFormat (
646 *TranslatedSize
* sizeof (*Translated
), // BufferSize in bytes
647 "PciRoot(0x0)/Pci(0x%x,0x%x)/Ata(%a,%a,0x0)",
650 Secondary
? "Secondary" : "Primary",
651 Slave
? "Slave" : "Master"
653 } else if (NumNodes
>= 4 &&
654 SubstringEq (OfwNode
[1].DriverName
, "isa") &&
655 SubstringEq (OfwNode
[2].DriverName
, "fdc") &&
656 SubstringEq (OfwNode
[3].DriverName
, "floppy")
659 // OpenFirmware device path (floppy disk):
661 // /pci@i0cf8/isa@1/fdc@03f0/floppy@0
664 // | | ISA controller io-port (hex)
665 // | PCI slot holding ISA controller
666 // PCI root at system bus port, PIO
670 // PciRoot(0x0)/Pci(0x1,0x0)/Floppy(0x0)
677 if (ParseUnitAddressHexList (
678 OfwNode
[3].UnitAddress
,
681 ) != RETURN_SUCCESS
||
684 return RETURN_UNSUPPORTED
;
687 Written
= UnicodeSPrintAsciiFormat (
689 *TranslatedSize
* sizeof (*Translated
), // BufferSize in bytes
690 "PciRoot(0x0)/Pci(0x%x,0x%x)/Floppy(0x%x)",
695 } else if (NumNodes
>= 3 &&
696 SubstringEq (OfwNode
[1].DriverName
, "scsi") &&
697 SubstringEq (OfwNode
[2].DriverName
, "disk")
700 // OpenFirmware device path (virtio-blk disk):
702 // /pci@i0cf8/scsi@6[,3]/disk@0,0
705 // | | PCI function corresponding to disk (optional)
706 // | PCI slot holding disk
707 // PCI root at system bus port, PIO
709 // UEFI device path prefix:
711 // PciRoot(0x0)/Pci(0x6,0x0)/HD( -- if PCI function is 0 or absent
712 // PciRoot(0x0)/Pci(0x6,0x3)/HD( -- if PCI function is present and nonzero
714 Written
= UnicodeSPrintAsciiFormat (
716 *TranslatedSize
* sizeof (*Translated
), // BufferSize in bytes
717 "PciRoot(0x0)/Pci(0x%x,0x%x)/HD(",
721 } else if (NumNodes
>= 4 &&
722 SubstringEq (OfwNode
[1].DriverName
, "scsi") &&
723 SubstringEq (OfwNode
[2].DriverName
, "channel") &&
724 SubstringEq (OfwNode
[3].DriverName
, "disk")
727 // OpenFirmware device path (virtio-scsi disk):
729 // /pci@i0cf8/scsi@7[,3]/channel@0/disk@2,3
733 // | | channel (unused, fixed 0)
734 // | PCI slot[, function] holding SCSI controller
735 // PCI root at system bus port, PIO
737 // UEFI device path prefix:
739 // PciRoot(0x0)/Pci(0x7,0x0)/Scsi(0x2,0x3)
740 // -- if PCI function is 0 or absent
741 // PciRoot(0x0)/Pci(0x7,0x3)/Scsi(0x2,0x3)
742 // -- if PCI function is present and nonzero
747 NumEntries
= sizeof (TargetLun
) / sizeof (TargetLun
[0]);
748 if (ParseUnitAddressHexList (
749 OfwNode
[3].UnitAddress
,
754 return RETURN_UNSUPPORTED
;
757 Written
= UnicodeSPrintAsciiFormat (
759 *TranslatedSize
* sizeof (*Translated
), // BufferSize in bytes
760 "PciRoot(0x0)/Pci(0x%x,0x%x)/Scsi(0x%x,0x%x)",
766 } else if (NumNodes
>= 3 &&
767 SubstringEq (OfwNode
[1].DriverName
, "ethernet") &&
768 SubstringEq (OfwNode
[2].DriverName
, "ethernet-phy")
771 // OpenFirmware device path (Ethernet NIC):
773 // /pci@i0cf8/ethernet@3[,2]/ethernet-phy@0
776 // | PCI slot[, function] holding Ethernet card
777 // PCI root at system bus port, PIO
779 // UEFI device path prefix (dependent on presence of nonzero PCI function):
781 // PciRoot(0x0)/Pci(0x3,0x0)/MAC(525400E15EEF,0x1)
782 // PciRoot(0x0)/Pci(0x3,0x2)/MAC(525400E15EEF,0x1)
784 // MAC address IfType (1 == Ethernet)
786 // (Some UEFI NIC drivers don't set 0x1 for IfType.)
788 Written
= UnicodeSPrintAsciiFormat (
790 *TranslatedSize
* sizeof (*Translated
), // BufferSize in bytes
791 "PciRoot(0x0)/Pci(0x%x,0x%x)/MAC",
796 return RETURN_UNSUPPORTED
;
800 // There's no way to differentiate between "completely used up without
801 // truncation" and "truncated", so treat the former as the latter, and return
802 // success only for "some room left unused".
804 if (Written
+ 1 < *TranslatedSize
) {
805 *TranslatedSize
= Written
;
806 return RETURN_SUCCESS
;
809 return RETURN_BUFFER_TOO_SMALL
;
815 Translate an OpenFirmware device path fragment to a UEFI device path
816 fragment, and advance in the input string.
818 @param[in out] Ptr Address of the pointer pointing to the start
819 of the path string. After successful
820 translation (RETURN_SUCCESS) or at least
821 successful parsing (RETURN_UNSUPPORTED,
822 RETURN_BUFFER_TOO_SMALL), *Ptr is set to the
823 byte immediately following the consumed
824 characters. In other error cases, it points to
825 the byte that caused the error.
827 @param[out] Translated Destination array receiving the UEFI path
828 fragment, allocated by the caller. If the
829 return value differs from RETURN_SUCCESS, its
830 contents is indeterminate.
832 @param[in out] TranslatedSize On input, the number of CHAR16's in
833 Translated. On RETURN_SUCCESS this parameter
834 is assigned the number of non-NUL CHAR16's
835 written to Translated. In case of other return
836 values, TranslatedSize is indeterminate.
839 @retval RETURN_SUCCESS Translation successful.
841 @retval RETURN_BUFFER_TOO_SMALL The OpenFirmware device path was parsed
842 successfully, but its translation did not
843 fit into the number of bytes provided.
844 Further calls to this function are
847 @retval RETURN_UNSUPPORTED The OpenFirmware device path was parsed
848 successfully, but it can't be translated in
849 the current implementation. Further calls
850 to this function are possible.
852 @retval RETURN_NOT_FOUND Translation terminated. On input, *Ptr was
853 pointing to the empty string or "HALT". On
854 output, *Ptr points to the empty string
855 (ie. "HALT" is consumed transparently when
858 @retval RETURN_INVALID_PARAMETER Parse error. This is a permanent error.
864 IN OUT CONST CHAR8
**Ptr
,
865 OUT CHAR16
*Translated
,
866 IN OUT UINTN
*TranslatedSize
870 RETURN_STATUS Status
;
871 OFW_NODE Node
[EXAMINED_OFW_NODES
];
877 if (AsciiStrCmp (*Ptr
, "HALT") == 0) {
879 Status
= RETURN_NOT_FOUND
;
881 Status
= ParseOfwNode (Ptr
, &Node
[NumNodes
], &IsFinal
);
884 if (Status
== RETURN_NOT_FOUND
) {
885 DEBUG ((DEBUG_VERBOSE
, "%a: no more nodes\n", __FUNCTION__
));
886 return RETURN_NOT_FOUND
;
889 while (Status
== RETURN_SUCCESS
&& !IsFinal
) {
891 Status
= ParseOfwNode (
893 (NumNodes
< EXAMINED_OFW_NODES
) ? &Node
[NumNodes
] : &Skip
,
903 case RETURN_INVALID_PARAMETER
:
904 DEBUG ((DEBUG_VERBOSE
, "%a: parse error\n", __FUNCTION__
));
905 return RETURN_INVALID_PARAMETER
;
911 Status
= TranslateOfwNodes (
913 NumNodes
< EXAMINED_OFW_NODES
? NumNodes
: EXAMINED_OFW_NODES
,
918 DEBUG ((DEBUG_VERBOSE
, "%a: success: \"%s\"\n", __FUNCTION__
, Translated
));
921 case RETURN_BUFFER_TOO_SMALL
:
922 DEBUG ((DEBUG_VERBOSE
, "%a: buffer too small\n", __FUNCTION__
));
925 case RETURN_UNSUPPORTED
:
926 DEBUG ((DEBUG_VERBOSE
, "%a: unsupported\n", __FUNCTION__
));
938 Convert the UEFI DevicePath to full text representation with DevPathToText,
939 then match the UEFI device path fragment in Translated against it.
941 @param[in] Translated UEFI device path fragment, translated from
942 OpenFirmware format, to search for.
944 @param[in] TranslatedLength The length of Translated in CHAR16's.
946 @param[in] DevicePath Boot option device path whose textual rendering
949 @param[in] DevPathToText Binary-to-text conversion protocol for DevicePath.
952 @retval TRUE If Translated was found at the beginning of DevicePath after
953 converting the latter to text.
955 @retval FALSE If DevicePath was NULL, or it could not be converted, or there
962 IN CONST CHAR16
*Translated
,
963 IN UINTN TranslatedLength
,
964 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
970 Converted
= ConvertDevicePathToText (
972 FALSE
, // DisplayOnly
973 FALSE
// AllowShortcuts
975 if (Converted
== NULL
) {
980 // Attempt to expand any relative UEFI device path starting with HD() to an
981 // absolute device path first. The logic imitates BdsLibBootViaBootOption().
982 // We don't have to free the absolute device path,
983 // BdsExpandPartitionPartialDevicePathToFull() has internal caching.
986 if (DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
&&
987 DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
) {
988 EFI_DEVICE_PATH_PROTOCOL
*AbsDevicePath
;
989 CHAR16
*AbsConverted
;
991 AbsDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
992 (HARDDRIVE_DEVICE_PATH
*) DevicePath
);
993 if (AbsDevicePath
== NULL
) {
996 AbsConverted
= ConvertDevicePathToText (AbsDevicePath
, FALSE
, FALSE
);
997 if (AbsConverted
== NULL
) {
1000 DEBUG ((DEBUG_VERBOSE
,
1001 "%a: expanded relative device path \"%s\" for prefix matching\n",
1002 __FUNCTION__
, Converted
));
1003 FreePool (Converted
);
1004 Converted
= AbsConverted
;
1008 // Is Translated a prefix of Converted?
1010 Result
= (BOOLEAN
)(StrnCmp (Converted
, Translated
, TranslatedLength
) == 0);
1013 "%a: against \"%s\": %a\n",
1016 Result
? "match" : "no match"
1019 FreePool (Converted
);
1025 Append some of the unselected active boot options to the boot order.
1027 This function should accommodate any further policy changes in "boot option
1028 survival". Currently we're adding back everything that starts with neither
1031 @param[in,out] BootOrder The structure holding the boot order to
1032 complete. The caller is responsible for
1033 initializing (and potentially populating) it
1034 before calling this function.
1036 @param[in,out] ActiveOption The array of active boot options to scan.
1037 Entries marked as Appended will be skipped.
1038 Those of the rest that satisfy the survival
1039 policy will be added to BootOrder with
1042 @param[in] ActiveCount Number of elements in ActiveOption.
1045 @retval RETURN_SUCCESS BootOrder has been extended with any eligible boot
1048 @return Error codes returned by BootOrderAppend().
1053 IN OUT BOOT_ORDER
*BootOrder
,
1054 IN OUT ACTIVE_OPTION
*ActiveOption
,
1055 IN UINTN ActiveCount
1058 RETURN_STATUS Status
;
1061 Status
= RETURN_SUCCESS
;
1063 while (!RETURN_ERROR (Status
) && Idx
< ActiveCount
) {
1064 if (!ActiveOption
[Idx
].Appended
) {
1065 CONST BDS_COMMON_OPTION
*Current
;
1066 CONST EFI_DEVICE_PATH_PROTOCOL
*FirstNode
;
1068 Current
= ActiveOption
[Idx
].BootOption
;
1069 FirstNode
= Current
->DevicePath
;
1070 if (FirstNode
!= NULL
) {
1072 STATIC CHAR16 ConvFallBack
[] = L
"<unable to convert>";
1075 Converted
= ConvertDevicePathToText (FirstNode
, FALSE
, FALSE
);
1076 if (Converted
== NULL
) {
1077 Converted
= ConvFallBack
;
1081 if (DevicePathType(FirstNode
) == MEDIA_DEVICE_PATH
&&
1082 DevicePathSubType(FirstNode
) == MEDIA_HARDDRIVE_DP
) {
1087 } else if (DevicePathType(FirstNode
) == ACPI_DEVICE_PATH
&&
1088 DevicePathSubType(FirstNode
) == ACPI_DP
) {
1089 ACPI_HID_DEVICE_PATH
*Acpi
;
1091 Acpi
= (ACPI_HID_DEVICE_PATH
*) FirstNode
;
1092 if ((Acpi
->HID
& PNP_EISA_ID_MASK
) == PNP_EISA_ID_CONST
&&
1093 EISA_ID_TO_NUM (Acpi
->HID
) == 0x0a03) {
1102 Status
= BootOrderAppend (BootOrder
, &ActiveOption
[Idx
]);
1103 if (!RETURN_ERROR (Status
)) {
1104 DEBUG ((DEBUG_VERBOSE
, "%a: keeping \"%s\"\n", __FUNCTION__
,
1108 DEBUG ((DEBUG_VERBOSE
, "%a: dropping \"%s\"\n", __FUNCTION__
,
1112 if (Converted
!= ConvFallBack
) {
1113 FreePool (Converted
);
1125 Set the boot order based on configuration retrieved from QEMU.
1127 Attempt to retrieve the "bootorder" fw_cfg file from QEMU. Translate the
1128 OpenFirmware device paths therein to UEFI device path fragments. Match the
1129 translated fragments against BootOptionList, and rewrite the BootOrder NvVar
1130 so that it corresponds to the order described in fw_cfg.
1132 @param[in] BootOptionList A boot option list, created with
1133 BdsLibEnumerateAllBootOption ().
1136 @retval RETURN_SUCCESS BootOrder NvVar rewritten.
1138 @retval RETURN_UNSUPPORTED QEMU's fw_cfg is not supported.
1140 @retval RETURN_NOT_FOUND Empty or nonexistent "bootorder" fw_cfg
1141 file, or no match found between the
1142 "bootorder" fw_cfg file and BootOptionList.
1144 @retval RETURN_INVALID_PARAMETER Parse error in the "bootorder" fw_cfg file.
1146 @retval RETURN_OUT_OF_RESOURCES Memory allocation failed.
1148 @return Values returned by gBS->LocateProtocol ()
1149 or gRT->SetVariable ().
1153 SetBootOrderFromQemu (
1154 IN CONST LIST_ENTRY
*BootOptionList
1157 RETURN_STATUS Status
;
1158 FIRMWARE_CONFIG_ITEM FwCfgItem
;
1161 CONST CHAR8
*FwCfgPtr
;
1163 BOOT_ORDER BootOrder
;
1164 ACTIVE_OPTION
*ActiveOption
;
1167 UINTN TranslatedSize
;
1168 CHAR16 Translated
[TRANSLATION_OUTPUT_SIZE
];
1170 Status
= QemuFwCfgFindFile ("bootorder", &FwCfgItem
, &FwCfgSize
);
1171 if (Status
!= RETURN_SUCCESS
) {
1175 if (FwCfgSize
== 0) {
1176 return RETURN_NOT_FOUND
;
1179 FwCfg
= AllocatePool (FwCfgSize
);
1180 if (FwCfg
== NULL
) {
1181 return RETURN_OUT_OF_RESOURCES
;
1184 QemuFwCfgSelectItem (FwCfgItem
);
1185 QemuFwCfgReadBytes (FwCfgSize
, FwCfg
);
1186 if (FwCfg
[FwCfgSize
- 1] != '\0') {
1187 Status
= RETURN_INVALID_PARAMETER
;
1188 goto ErrorFreeFwCfg
;
1191 DEBUG ((DEBUG_VERBOSE
, "%a: FwCfg:\n", __FUNCTION__
));
1192 DEBUG ((DEBUG_VERBOSE
, "%a\n", FwCfg
));
1193 DEBUG ((DEBUG_VERBOSE
, "%a: FwCfg: <end>\n", __FUNCTION__
));
1196 BootOrder
.Produced
= 0;
1197 BootOrder
.Allocated
= 1;
1198 BootOrder
.Data
= AllocatePool (
1199 BootOrder
.Allocated
* sizeof (*BootOrder
.Data
)
1201 if (BootOrder
.Data
== NULL
) {
1202 Status
= RETURN_OUT_OF_RESOURCES
;
1203 goto ErrorFreeFwCfg
;
1206 Status
= CollectActiveOptions (BootOptionList
, &ActiveOption
, &ActiveCount
);
1207 if (RETURN_ERROR (Status
)) {
1208 goto ErrorFreeBootOrder
;
1212 // translate each OpenFirmware path
1214 TranslatedSize
= sizeof (Translated
) / sizeof (Translated
[0]);
1215 Status
= TranslateOfwPath (&FwCfgPtr
, Translated
, &TranslatedSize
);
1216 while (Status
== RETURN_SUCCESS
||
1217 Status
== RETURN_UNSUPPORTED
||
1218 Status
== RETURN_BUFFER_TOO_SMALL
) {
1219 if (Status
== RETURN_SUCCESS
) {
1223 // match translated OpenFirmware path against all active boot options
1225 for (Idx
= 0; Idx
< ActiveCount
; ++Idx
) {
1228 TranslatedSize
, // contains length, not size, in CHAR16's here
1229 ActiveOption
[Idx
].BootOption
->DevicePath
1233 // match found, store ID and continue with next OpenFirmware path
1235 Status
= BootOrderAppend (&BootOrder
, &ActiveOption
[Idx
]);
1236 if (Status
!= RETURN_SUCCESS
) {
1237 goto ErrorFreeActiveOption
;
1241 } // scanned all active boot options
1242 } // translation successful
1244 TranslatedSize
= sizeof (Translated
) / sizeof (Translated
[0]);
1245 Status
= TranslateOfwPath (&FwCfgPtr
, Translated
, &TranslatedSize
);
1246 } // scanning of OpenFirmware paths done
1248 if (Status
== RETURN_NOT_FOUND
&& BootOrder
.Produced
> 0) {
1250 // No more OpenFirmware paths, some matches found: rewrite BootOrder NvVar.
1251 // Some of the active boot options that have not been selected over fw_cfg
1252 // should be preserved at the end of the boot order.
1254 Status
= BootOrderComplete (&BootOrder
, ActiveOption
, ActiveCount
);
1255 if (RETURN_ERROR (Status
)) {
1256 goto ErrorFreeActiveOption
;
1260 // See Table 10 in the UEFI Spec 2.3.1 with Errata C for the required
1263 Status
= gRT
->SetVariable (
1265 &gEfiGlobalVariableGuid
,
1266 EFI_VARIABLE_NON_VOLATILE
|
1267 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
1268 EFI_VARIABLE_RUNTIME_ACCESS
,
1269 BootOrder
.Produced
* sizeof (*BootOrder
.Data
),
1274 "%a: setting BootOrder: %a\n",
1276 Status
== EFI_SUCCESS
? "success" : "error"
1280 ErrorFreeActiveOption
:
1281 FreePool (ActiveOption
);
1284 FreePool (BootOrder
.Data
);