]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.c
ArmVirtualizationPkg: VirtFdtDxe: use dedicated VIRTIO_MMIO_TRANSPORT_GUID
[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
2f9c55cc
LE
38#define REQUIRED_PCI_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
2f9c55cc 542 Translate a PCI-like array of OpenFirmware device nodes to a UEFI device path\r
2cd086a6 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
2f9c55cc 574TranslatePciOfwNodes (\r
2cd086a6 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
2f9c55cc 588 if (NumNodes < REQUIRED_PCI_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
2f9c55cc
LE
804/**\r
805\r
806 Translate an array of OpenFirmware device nodes to a UEFI device path\r
807 fragment.\r
808\r
809 @param[in] OfwNode Array of OpenFirmware device nodes to\r
810 translate, constituting the beginning of an\r
811 OpenFirmware device path.\r
812\r
813 @param[in] NumNodes Number of elements in OfwNode.\r
814\r
815 @param[out] Translated Destination array receiving the UEFI path\r
816 fragment, allocated by the caller. If the\r
817 return value differs from RETURN_SUCCESS, its\r
818 contents is indeterminate.\r
819\r
820 @param[in out] TranslatedSize On input, the number of CHAR16's in\r
821 Translated. On RETURN_SUCCESS this parameter\r
822 is assigned the number of non-NUL CHAR16's\r
823 written to Translated. In case of other return\r
824 values, TranslatedSize is indeterminate.\r
825\r
826\r
827 @retval RETURN_SUCCESS Translation successful.\r
828\r
829 @retval RETURN_BUFFER_TOO_SMALL The translation does not fit into the number\r
830 of bytes provided.\r
831\r
832 @retval RETURN_UNSUPPORTED The array of OpenFirmware device nodes can't\r
833 be translated in the current implementation.\r
834\r
835**/\r
836STATIC\r
837RETURN_STATUS\r
838TranslateOfwNodes (\r
839 IN CONST OFW_NODE *OfwNode,\r
840 IN UINTN NumNodes,\r
841 OUT CHAR16 *Translated,\r
842 IN OUT UINTN *TranslatedSize\r
843 )\r
844{\r
845 RETURN_STATUS Status;\r
846\r
847 Status = RETURN_UNSUPPORTED;\r
848\r
849 if (FeaturePcdGet (PcdQemuBootOrderPciTranslation)) {\r
850 Status = TranslatePciOfwNodes (OfwNode, NumNodes, Translated,\r
851 TranslatedSize);\r
852 }\r
853 return Status;\r
854}\r
855\r
2cd086a6 856/**\r
857\r
858 Translate an OpenFirmware device path fragment to a UEFI device path\r
859 fragment, and advance in the input string.\r
860\r
861 @param[in out] Ptr Address of the pointer pointing to the start\r
862 of the path string. After successful\r
863 translation (RETURN_SUCCESS) or at least\r
864 successful parsing (RETURN_UNSUPPORTED,\r
865 RETURN_BUFFER_TOO_SMALL), *Ptr is set to the\r
866 byte immediately following the consumed\r
867 characters. In other error cases, it points to\r
868 the byte that caused the error.\r
869\r
870 @param[out] Translated Destination array receiving the UEFI path\r
871 fragment, allocated by the caller. If the\r
872 return value differs from RETURN_SUCCESS, its\r
873 contents is indeterminate.\r
874\r
875 @param[in out] TranslatedSize On input, the number of CHAR16's in\r
876 Translated. On RETURN_SUCCESS this parameter\r
877 is assigned the number of non-NUL CHAR16's\r
878 written to Translated. In case of other return\r
879 values, TranslatedSize is indeterminate.\r
880\r
881\r
882 @retval RETURN_SUCCESS Translation successful.\r
883\r
884 @retval RETURN_BUFFER_TOO_SMALL The OpenFirmware device path was parsed\r
885 successfully, but its translation did not\r
886 fit into the number of bytes provided.\r
887 Further calls to this function are\r
888 possible.\r
889\r
890 @retval RETURN_UNSUPPORTED The OpenFirmware device path was parsed\r
891 successfully, but it can't be translated in\r
892 the current implementation. Further calls\r
893 to this function are possible.\r
894\r
c3cf8daa
LE
895 @retval RETURN_NOT_FOUND Translation terminated. On input, *Ptr was\r
896 pointing to the empty string or "HALT". On\r
897 output, *Ptr points to the empty string\r
898 (ie. "HALT" is consumed transparently when\r
899 present).\r
2cd086a6 900\r
901 @retval RETURN_INVALID_PARAMETER Parse error. This is a permanent error.\r
902\r
903**/\r
904STATIC\r
905RETURN_STATUS\r
906TranslateOfwPath (\r
907 IN OUT CONST CHAR8 **Ptr,\r
908 OUT CHAR16 *Translated,\r
909 IN OUT UINTN *TranslatedSize\r
910 )\r
911{\r
912 UINTN NumNodes;\r
913 RETURN_STATUS Status;\r
ec01afdb 914 OFW_NODE Node[EXAMINED_OFW_NODES];\r
2cd086a6 915 BOOLEAN IsFinal;\r
916 OFW_NODE Skip;\r
917\r
489c3142 918 IsFinal = FALSE;\r
2cd086a6 919 NumNodes = 0;\r
c3cf8daa
LE
920 if (AsciiStrCmp (*Ptr, "HALT") == 0) {\r
921 *Ptr += 4;\r
922 Status = RETURN_NOT_FOUND;\r
923 } else {\r
924 Status = ParseOfwNode (Ptr, &Node[NumNodes], &IsFinal);\r
925 }\r
2cd086a6 926\r
927 if (Status == RETURN_NOT_FOUND) {\r
928 DEBUG ((DEBUG_VERBOSE, "%a: no more nodes\n", __FUNCTION__));\r
929 return RETURN_NOT_FOUND;\r
930 }\r
931\r
932 while (Status == RETURN_SUCCESS && !IsFinal) {\r
933 ++NumNodes;\r
934 Status = ParseOfwNode (\r
935 Ptr,\r
ec01afdb 936 (NumNodes < EXAMINED_OFW_NODES) ? &Node[NumNodes] : &Skip,\r
2cd086a6 937 &IsFinal\r
938 );\r
939 }\r
940\r
941 switch (Status) {\r
942 case RETURN_SUCCESS:\r
943 ++NumNodes;\r
944 break;\r
945\r
946 case RETURN_INVALID_PARAMETER:\r
947 DEBUG ((DEBUG_VERBOSE, "%a: parse error\n", __FUNCTION__));\r
948 return RETURN_INVALID_PARAMETER;\r
949\r
950 default:\r
951 ASSERT (0);\r
952 }\r
953\r
954 Status = TranslateOfwNodes (\r
955 Node,\r
ec01afdb 956 NumNodes < EXAMINED_OFW_NODES ? NumNodes : EXAMINED_OFW_NODES,\r
2cd086a6 957 Translated,\r
958 TranslatedSize);\r
959 switch (Status) {\r
960 case RETURN_SUCCESS:\r
961 DEBUG ((DEBUG_VERBOSE, "%a: success: \"%s\"\n", __FUNCTION__, Translated));\r
962 break;\r
963\r
964 case RETURN_BUFFER_TOO_SMALL:\r
965 DEBUG ((DEBUG_VERBOSE, "%a: buffer too small\n", __FUNCTION__));\r
966 break;\r
967\r
968 case RETURN_UNSUPPORTED:\r
969 DEBUG ((DEBUG_VERBOSE, "%a: unsupported\n", __FUNCTION__));\r
970 break;\r
971\r
972 default:\r
973 ASSERT (0);\r
974 }\r
975 return Status;\r
976}\r
977\r
978\r
979/**\r
980\r
981 Convert the UEFI DevicePath to full text representation with DevPathToText,\r
982 then match the UEFI device path fragment in Translated against it.\r
983\r
984 @param[in] Translated UEFI device path fragment, translated from\r
985 OpenFirmware format, to search for.\r
986\r
987 @param[in] TranslatedLength The length of Translated in CHAR16's.\r
988\r
989 @param[in] DevicePath Boot option device path whose textual rendering\r
990 to search in.\r
991\r
992 @param[in] DevPathToText Binary-to-text conversion protocol for DevicePath.\r
993\r
994\r
995 @retval TRUE If Translated was found at the beginning of DevicePath after\r
996 converting the latter to text.\r
997\r
998 @retval FALSE If DevicePath was NULL, or it could not be converted, or there\r
999 was no match.\r
1000\r
1001**/\r
1002STATIC\r
1003BOOLEAN\r
1004Match (\r
1005 IN CONST CHAR16 *Translated,\r
1006 IN UINTN TranslatedLength,\r
863986b3 1007 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
2cd086a6 1008 )\r
1009{\r
1010 CHAR16 *Converted;\r
1011 BOOLEAN Result;\r
1012\r
863986b3
RN
1013 Converted = ConvertDevicePathToText (\r
1014 DevicePath,\r
1015 FALSE, // DisplayOnly\r
1016 FALSE // AllowShortcuts\r
1017 );\r
2cd086a6 1018 if (Converted == NULL) {\r
1019 return FALSE;\r
1020 }\r
1021\r
64378ce1
LE
1022 //\r
1023 // Attempt to expand any relative UEFI device path starting with HD() to an\r
1024 // absolute device path first. The logic imitates BdsLibBootViaBootOption().\r
1025 // We don't have to free the absolute device path,\r
1026 // BdsExpandPartitionPartialDevicePathToFull() has internal caching.\r
1027 //\r
1028 Result = FALSE;\r
1029 if (DevicePathType (DevicePath) == MEDIA_DEVICE_PATH &&\r
1030 DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP) {\r
1031 EFI_DEVICE_PATH_PROTOCOL *AbsDevicePath;\r
1032 CHAR16 *AbsConverted;\r
1033\r
1034 AbsDevicePath = BdsExpandPartitionPartialDevicePathToFull (\r
1035 (HARDDRIVE_DEVICE_PATH *) DevicePath);\r
1036 if (AbsDevicePath == NULL) {\r
1037 goto Exit;\r
1038 }\r
1039 AbsConverted = ConvertDevicePathToText (AbsDevicePath, FALSE, FALSE);\r
1040 if (AbsConverted == NULL) {\r
1041 goto Exit;\r
1042 }\r
1043 DEBUG ((DEBUG_VERBOSE,\r
1044 "%a: expanded relative device path \"%s\" for prefix matching\n",\r
1045 __FUNCTION__, Converted));\r
1046 FreePool (Converted);\r
1047 Converted = AbsConverted;\r
1048 }\r
1049\r
2cd086a6 1050 //\r
1051 // Is Translated a prefix of Converted?\r
1052 //\r
dccf7678 1053 Result = (BOOLEAN)(StrnCmp (Converted, Translated, TranslatedLength) == 0);\r
2cd086a6 1054 DEBUG ((\r
1055 DEBUG_VERBOSE,\r
1056 "%a: against \"%s\": %a\n",\r
1057 __FUNCTION__,\r
1058 Converted,\r
1059 Result ? "match" : "no match"\r
1060 ));\r
64378ce1 1061Exit:\r
2cd086a6 1062 FreePool (Converted);\r
1063 return Result;\r
1064}\r
1065\r
1066\r
838b5b00
LE
1067/**\r
1068 Append some of the unselected active boot options to the boot order.\r
1069\r
1070 This function should accommodate any further policy changes in "boot option\r
1071 survival". Currently we're adding back everything that starts with neither\r
1072 PciRoot() nor HD().\r
1073\r
1074 @param[in,out] BootOrder The structure holding the boot order to\r
1075 complete. The caller is responsible for\r
1076 initializing (and potentially populating) it\r
1077 before calling this function.\r
1078\r
1079 @param[in,out] ActiveOption The array of active boot options to scan.\r
1080 Entries marked as Appended will be skipped.\r
1081 Those of the rest that satisfy the survival\r
1082 policy will be added to BootOrder with\r
1083 BootOrderAppend().\r
1084\r
1085 @param[in] ActiveCount Number of elements in ActiveOption.\r
1086\r
1087\r
1088 @retval RETURN_SUCCESS BootOrder has been extended with any eligible boot\r
1089 options.\r
1090\r
1091 @return Error codes returned by BootOrderAppend().\r
1092**/\r
1093STATIC\r
1094RETURN_STATUS\r
1095BootOrderComplete (\r
1096 IN OUT BOOT_ORDER *BootOrder,\r
1097 IN OUT ACTIVE_OPTION *ActiveOption,\r
1098 IN UINTN ActiveCount\r
1099 )\r
1100{\r
1101 RETURN_STATUS Status;\r
1102 UINTN Idx;\r
1103\r
1104 Status = RETURN_SUCCESS;\r
1105 Idx = 0;\r
1106 while (!RETURN_ERROR (Status) && Idx < ActiveCount) {\r
1107 if (!ActiveOption[Idx].Appended) {\r
1108 CONST BDS_COMMON_OPTION *Current;\r
1109 CONST EFI_DEVICE_PATH_PROTOCOL *FirstNode;\r
1110\r
1111 Current = ActiveOption[Idx].BootOption;\r
1112 FirstNode = Current->DevicePath;\r
1113 if (FirstNode != NULL) {\r
1114 CHAR16 *Converted;\r
1115 STATIC CHAR16 ConvFallBack[] = L"<unable to convert>";\r
1116 BOOLEAN Keep;\r
1117\r
1118 Converted = ConvertDevicePathToText (FirstNode, FALSE, FALSE);\r
1119 if (Converted == NULL) {\r
1120 Converted = ConvFallBack;\r
1121 }\r
1122\r
1123 Keep = TRUE;\r
1124 if (DevicePathType(FirstNode) == MEDIA_DEVICE_PATH &&\r
1125 DevicePathSubType(FirstNode) == MEDIA_HARDDRIVE_DP) {\r
1126 //\r
1127 // drop HD()\r
1128 //\r
1129 Keep = FALSE;\r
1130 } else if (DevicePathType(FirstNode) == ACPI_DEVICE_PATH &&\r
1131 DevicePathSubType(FirstNode) == ACPI_DP) {\r
1132 ACPI_HID_DEVICE_PATH *Acpi;\r
1133\r
1134 Acpi = (ACPI_HID_DEVICE_PATH *) FirstNode;\r
1135 if ((Acpi->HID & PNP_EISA_ID_MASK) == PNP_EISA_ID_CONST &&\r
1136 EISA_ID_TO_NUM (Acpi->HID) == 0x0a03) {\r
1137 //\r
2f9c55cc
LE
1138 // drop PciRoot() if we enabled the user to select PCI-like boot\r
1139 // options, by providing translation for such OFW device path\r
1140 // fragments\r
838b5b00 1141 //\r
2f9c55cc 1142 Keep = !FeaturePcdGet (PcdQemuBootOrderPciTranslation);\r
838b5b00
LE
1143 }\r
1144 }\r
1145\r
1146 if (Keep) {\r
1147 Status = BootOrderAppend (BootOrder, &ActiveOption[Idx]);\r
1148 if (!RETURN_ERROR (Status)) {\r
1149 DEBUG ((DEBUG_VERBOSE, "%a: keeping \"%s\"\n", __FUNCTION__,\r
1150 Converted));\r
1151 }\r
1152 } else {\r
1153 DEBUG ((DEBUG_VERBOSE, "%a: dropping \"%s\"\n", __FUNCTION__,\r
1154 Converted));\r
1155 }\r
1156\r
1157 if (Converted != ConvFallBack) {\r
1158 FreePool (Converted);\r
1159 }\r
1160 }\r
1161 }\r
1162 ++Idx;\r
1163 }\r
1164 return Status;\r
1165}\r
1166\r
1167\r
1c9135a2
LE
1168/**\r
1169 Delete Boot#### variables that stand for such active boot options that have\r
1170 been dropped (ie. have not been selected by either matching or "survival\r
1171 policy").\r
1172\r
1173 @param[in] ActiveOption The array of active boot options to scan. Each\r
1174 entry not marked as appended will trigger the\r
1175 deletion of the matching Boot#### variable.\r
1176\r
1177 @param[in] ActiveCount Number of elements in ActiveOption.\r
1178**/\r
1179STATIC\r
1180VOID\r
1181PruneBootVariables (\r
1182 IN CONST ACTIVE_OPTION *ActiveOption,\r
1183 IN UINTN ActiveCount\r
1184 )\r
1185{\r
1186 UINTN Idx;\r
1187\r
1188 for (Idx = 0; Idx < ActiveCount; ++Idx) {\r
1189 if (!ActiveOption[Idx].Appended) {\r
1190 CHAR16 VariableName[9];\r
1191\r
1192 UnicodeSPrintAsciiFormat (VariableName, sizeof VariableName, "Boot%04x",\r
1193 ActiveOption[Idx].BootOption->BootCurrent);\r
1194\r
1195 //\r
1196 // "The space consumed by the deleted variable may not be available until\r
1197 // the next power cycle", but that's good enough.\r
1198 //\r
1199 gRT->SetVariable (VariableName, &gEfiGlobalVariableGuid,\r
1200 0, // Attributes, 0 means deletion\r
1201 0, // DataSize, 0 means deletion\r
1202 NULL // Data\r
1203 );\r
1204 }\r
1205 }\r
1206}\r
1207\r
1208\r
2cd086a6 1209/**\r
1210\r
1211 Set the boot order based on configuration retrieved from QEMU.\r
1212\r
1213 Attempt to retrieve the "bootorder" fw_cfg file from QEMU. Translate the\r
1214 OpenFirmware device paths therein to UEFI device path fragments. Match the\r
1215 translated fragments against BootOptionList, and rewrite the BootOrder NvVar\r
1216 so that it corresponds to the order described in fw_cfg.\r
1217\r
1218 @param[in] BootOptionList A boot option list, created with\r
1219 BdsLibEnumerateAllBootOption ().\r
1220\r
1221\r
1222 @retval RETURN_SUCCESS BootOrder NvVar rewritten.\r
1223\r
1224 @retval RETURN_UNSUPPORTED QEMU's fw_cfg is not supported.\r
1225\r
1226 @retval RETURN_NOT_FOUND Empty or nonexistent "bootorder" fw_cfg\r
1227 file, or no match found between the\r
1228 "bootorder" fw_cfg file and BootOptionList.\r
1229\r
1230 @retval RETURN_INVALID_PARAMETER Parse error in the "bootorder" fw_cfg file.\r
1231\r
1232 @retval RETURN_OUT_OF_RESOURCES Memory allocation failed.\r
1233\r
1234 @return Values returned by gBS->LocateProtocol ()\r
1235 or gRT->SetVariable ().\r
1236\r
1237**/\r
1238RETURN_STATUS\r
1239SetBootOrderFromQemu (\r
1240 IN CONST LIST_ENTRY *BootOptionList\r
1241 )\r
1242{\r
1243 RETURN_STATUS Status;\r
2cd086a6 1244 FIRMWARE_CONFIG_ITEM FwCfgItem;\r
1245 UINTN FwCfgSize;\r
1246 CHAR8 *FwCfg;\r
1247 CONST CHAR8 *FwCfgPtr;\r
1248\r
1249 BOOT_ORDER BootOrder;\r
32a22f09
LE
1250 ACTIVE_OPTION *ActiveOption;\r
1251 UINTN ActiveCount;\r
2cd086a6 1252\r
1253 UINTN TranslatedSize;\r
1254 CHAR16 Translated[TRANSLATION_OUTPUT_SIZE];\r
1255\r
2cd086a6 1256 Status = QemuFwCfgFindFile ("bootorder", &FwCfgItem, &FwCfgSize);\r
1257 if (Status != RETURN_SUCCESS) {\r
1258 return Status;\r
1259 }\r
1260\r
1261 if (FwCfgSize == 0) {\r
1262 return RETURN_NOT_FOUND;\r
1263 }\r
1264\r
1265 FwCfg = AllocatePool (FwCfgSize);\r
1266 if (FwCfg == NULL) {\r
1267 return RETURN_OUT_OF_RESOURCES;\r
1268 }\r
1269\r
1270 QemuFwCfgSelectItem (FwCfgItem);\r
1271 QemuFwCfgReadBytes (FwCfgSize, FwCfg);\r
1272 if (FwCfg[FwCfgSize - 1] != '\0') {\r
1273 Status = RETURN_INVALID_PARAMETER;\r
1274 goto ErrorFreeFwCfg;\r
1275 }\r
1276\r
1277 DEBUG ((DEBUG_VERBOSE, "%a: FwCfg:\n", __FUNCTION__));\r
1278 DEBUG ((DEBUG_VERBOSE, "%a\n", FwCfg));\r
1279 DEBUG ((DEBUG_VERBOSE, "%a: FwCfg: <end>\n", __FUNCTION__));\r
1280 FwCfgPtr = FwCfg;\r
1281\r
1282 BootOrder.Produced = 0;\r
1283 BootOrder.Allocated = 1;\r
1284 BootOrder.Data = AllocatePool (\r
1285 BootOrder.Allocated * sizeof (*BootOrder.Data)\r
1286 );\r
1287 if (BootOrder.Data == NULL) {\r
1288 Status = RETURN_OUT_OF_RESOURCES;\r
1289 goto ErrorFreeFwCfg;\r
1290 }\r
1291\r
32a22f09
LE
1292 Status = CollectActiveOptions (BootOptionList, &ActiveOption, &ActiveCount);\r
1293 if (RETURN_ERROR (Status)) {\r
1294 goto ErrorFreeBootOrder;\r
1295 }\r
1296\r
2cd086a6 1297 //\r
1298 // translate each OpenFirmware path\r
1299 //\r
1300 TranslatedSize = sizeof (Translated) / sizeof (Translated[0]);\r
1301 Status = TranslateOfwPath (&FwCfgPtr, Translated, &TranslatedSize);\r
1302 while (Status == RETURN_SUCCESS ||\r
1303 Status == RETURN_UNSUPPORTED ||\r
1304 Status == RETURN_BUFFER_TOO_SMALL) {\r
1305 if (Status == RETURN_SUCCESS) {\r
32a22f09 1306 UINTN Idx;\r
2cd086a6 1307\r
1308 //\r
32a22f09 1309 // match translated OpenFirmware path against all active boot options\r
2cd086a6 1310 //\r
32a22f09
LE
1311 for (Idx = 0; Idx < ActiveCount; ++Idx) {\r
1312 if (Match (\r
2cd086a6 1313 Translated,\r
1314 TranslatedSize, // contains length, not size, in CHAR16's here\r
32a22f09 1315 ActiveOption[Idx].BootOption->DevicePath\r
2cd086a6 1316 )\r
1317 ) {\r
1318 //\r
1319 // match found, store ID and continue with next OpenFirmware path\r
1320 //\r
e13be08e 1321 Status = BootOrderAppend (&BootOrder, &ActiveOption[Idx]);\r
2cd086a6 1322 if (Status != RETURN_SUCCESS) {\r
32a22f09 1323 goto ErrorFreeActiveOption;\r
2cd086a6 1324 }\r
1325 break;\r
1326 }\r
32a22f09 1327 } // scanned all active boot options\r
2cd086a6 1328 } // translation successful\r
1329\r
1330 TranslatedSize = sizeof (Translated) / sizeof (Translated[0]);\r
1331 Status = TranslateOfwPath (&FwCfgPtr, Translated, &TranslatedSize);\r
1332 } // scanning of OpenFirmware paths done\r
1333\r
1334 if (Status == RETURN_NOT_FOUND && BootOrder.Produced > 0) {\r
1335 //\r
1336 // No more OpenFirmware paths, some matches found: rewrite BootOrder NvVar.\r
838b5b00
LE
1337 // Some of the active boot options that have not been selected over fw_cfg\r
1338 // should be preserved at the end of the boot order.\r
1339 //\r
1340 Status = BootOrderComplete (&BootOrder, ActiveOption, ActiveCount);\r
1341 if (RETURN_ERROR (Status)) {\r
1342 goto ErrorFreeActiveOption;\r
1343 }\r
1344\r
1345 //\r
2cd086a6 1346 // See Table 10 in the UEFI Spec 2.3.1 with Errata C for the required\r
1347 // attributes.\r
1348 //\r
1349 Status = gRT->SetVariable (\r
1350 L"BootOrder",\r
1351 &gEfiGlobalVariableGuid,\r
1352 EFI_VARIABLE_NON_VOLATILE |\r
1353 EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
1354 EFI_VARIABLE_RUNTIME_ACCESS,\r
1355 BootOrder.Produced * sizeof (*BootOrder.Data),\r
1356 BootOrder.Data\r
1357 );\r
1c9135a2
LE
1358 if (EFI_ERROR (Status)) {\r
1359 DEBUG ((DEBUG_ERROR, "%a: setting BootOrder: %r\n", __FUNCTION__, Status));\r
1360 goto ErrorFreeActiveOption;\r
1361 }\r
1362\r
1363 DEBUG ((DEBUG_INFO, "%a: setting BootOrder: success\n", __FUNCTION__));\r
1364 PruneBootVariables (ActiveOption, ActiveCount);\r
2cd086a6 1365 }\r
1366\r
32a22f09
LE
1367ErrorFreeActiveOption:\r
1368 FreePool (ActiveOption);\r
1369\r
2cd086a6 1370ErrorFreeBootOrder:\r
1371 FreePool (BootOrder.Data);\r
1372\r
1373ErrorFreeFwCfg:\r
1374 FreePool (FwCfg);\r
1375\r
1376 return Status;\r
1377}\r