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