]>
Commit | Line | Data |
---|---|---|
5c08e117 | 1 | /** @file\r |
2 | BDS Lib functions which relate with create or process the boot option.\r | |
3 | \r | |
2df686c6 | 4 | Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.<BR>\r |
180a5a35 | 5 | This program and the accompanying materials\r |
5c08e117 | 6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "InternalBdsLib.h"\r | |
9aa7ba01 | 16 | #include "String.h"\r |
5c08e117 | 17 | \r |
18 | BOOLEAN mEnumBootDevice = FALSE;\r | |
9aa7ba01 | 19 | EFI_HII_HANDLE gBdsLibStringPackHandle = NULL;\r |
5c08e117 | 20 | \r |
9aa7ba01 | 21 | /**\r |
22 | The constructor function register UNI strings into imageHandle.\r | |
23 | \r | |
24 | It will ASSERT() if that operation fails and it will always return EFI_SUCCESS. \r | |
25 | \r | |
26 | @param ImageHandle The firmware allocated handle for the EFI image.\r | |
27 | @param SystemTable A pointer to the EFI System Table.\r | |
28 | \r | |
29 | @retval EFI_SUCCESS The constructor successfully added string package.\r | |
30 | @retval Other value The constructor can't add string package.\r | |
31 | \r | |
32 | **/\r | |
33 | EFI_STATUS\r | |
34 | EFIAPI\r | |
35 | GenericBdsLibConstructor (\r | |
36 | IN EFI_HANDLE ImageHandle,\r | |
37 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
38 | )\r | |
39 | {\r | |
40 | \r | |
41 | gBdsLibStringPackHandle = HiiAddPackages (\r | |
e24fc103 | 42 | &gBdsLibStringPackageGuid,\r |
9aa7ba01 | 43 | &ImageHandle,\r |
44 | GenericBdsLibStrings,\r | |
45 | NULL\r | |
46 | );\r | |
47 | \r | |
48 | ASSERT (gBdsLibStringPackHandle != NULL);\r | |
49 | \r | |
50 | return EFI_SUCCESS;\r | |
51 | }\r | |
52 | \r | |
5c08e117 | 53 | \r |
54 | \r | |
55 | /**\r | |
56 | Boot the legacy system with the boot option\r | |
57 | \r | |
58 | @param Option The legacy boot option which have BBS device path\r | |
59 | \r | |
60 | @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support\r | |
61 | legacy boot.\r | |
62 | @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().\r | |
63 | \r | |
64 | **/\r | |
65 | EFI_STATUS\r | |
66 | BdsLibDoLegacyBoot (\r | |
67 | IN BDS_COMMON_OPTION *Option\r | |
68 | )\r | |
69 | {\r | |
70 | EFI_STATUS Status;\r | |
71 | EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;\r | |
72 | \r | |
73 | Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);\r | |
74 | if (EFI_ERROR (Status)) {\r | |
75 | //\r | |
76 | // If no LegacyBios protocol we do not support legacy boot\r | |
77 | //\r | |
78 | return EFI_UNSUPPORTED;\r | |
79 | }\r | |
80 | //\r | |
81 | // Notes: if we separate the int 19, then we don't need to refresh BBS\r | |
82 | //\r | |
83 | BdsRefreshBbsTableForBoot (Option);\r | |
84 | \r | |
85 | //\r | |
cd6a3b15 | 86 | // Write boot to OS performance data for legacy boot.\r |
5c08e117 | 87 | //\r |
88 | PERF_CODE (\r | |
89 | WriteBootToOsPerformanceData ();\r | |
90 | );\r | |
91 | \r | |
92 | DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Legacy Boot: %S\n", Option->Description));\r | |
93 | return LegacyBios->LegacyBoot (\r | |
94 | LegacyBios,\r | |
95 | (BBS_BBS_DEVICE_PATH *) Option->DevicePath,\r | |
96 | Option->LoadOptionsSize,\r | |
97 | Option->LoadOptions\r | |
98 | );\r | |
99 | }\r | |
100 | \r | |
3d7decbc | 101 | /**\r |
102 | Internal function to check if the input boot option is a valid EFI NV Boot####.\r | |
103 | \r | |
104 | @param OptionToCheck Boot option to be checked.\r | |
105 | \r | |
106 | @retval TRUE This boot option matches a valid EFI NV Boot####.\r | |
107 | @retval FALSE If not.\r | |
128efbbc | 108 | \r |
3d7decbc | 109 | **/\r |
3d7decbc | 110 | BOOLEAN\r |
111 | IsBootOptionValidNVVarialbe (\r | |
112 | IN BDS_COMMON_OPTION *OptionToCheck\r | |
113 | )\r | |
114 | {\r | |
115 | LIST_ENTRY TempList;\r | |
116 | BDS_COMMON_OPTION *BootOption;\r | |
117 | BOOLEAN Valid;\r | |
118 | CHAR16 OptionName[20];\r | |
119 | \r | |
120 | Valid = FALSE;\r | |
121 | \r | |
122 | InitializeListHead (&TempList);\r | |
123 | UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionToCheck->BootCurrent);\r | |
5c08e117 | 124 | \r |
3d7decbc | 125 | BootOption = BdsLibVariableToOption (&TempList, OptionName);\r |
126 | if (BootOption == NULL) {\r | |
127 | return FALSE;\r | |
128 | }\r | |
129 | \r | |
130 | //\r | |
128efbbc | 131 | // If the Boot Option Number and Device Path matches, OptionToCheck matches a\r |
3d7decbc | 132 | // valid EFI NV Boot####.\r |
133 | //\r | |
134 | if ((OptionToCheck->BootCurrent == BootOption->BootCurrent) &&\r | |
135 | (CompareMem (OptionToCheck->DevicePath, BootOption->DevicePath, GetDevicePathSize (OptionToCheck->DevicePath)) == 0))\r | |
136 | {\r | |
137 | Valid = TRUE;\r | |
138 | }\r | |
139 | \r | |
140 | FreePool (BootOption);\r | |
128efbbc | 141 | \r |
3d7decbc | 142 | return Valid;\r |
143 | }\r | |
7389fdd0 | 144 | \r |
145 | /**\r | |
146 | Check whether a USB device match the specified USB Class device path. This\r | |
147 | function follows "Load Option Processing" behavior in UEFI specification.\r | |
148 | \r | |
149 | @param UsbIo USB I/O protocol associated with the USB device.\r | |
150 | @param UsbClass The USB Class device path to match.\r | |
151 | \r | |
152 | @retval TRUE The USB device match the USB Class device path.\r | |
153 | @retval FALSE The USB device does not match the USB Class device path.\r | |
154 | \r | |
155 | **/\r | |
156 | BOOLEAN\r | |
157 | BdsMatchUsbClass (\r | |
158 | IN EFI_USB_IO_PROTOCOL *UsbIo,\r | |
159 | IN USB_CLASS_DEVICE_PATH *UsbClass\r | |
160 | )\r | |
161 | {\r | |
162 | EFI_STATUS Status;\r | |
163 | EFI_USB_DEVICE_DESCRIPTOR DevDesc;\r | |
164 | EFI_USB_INTERFACE_DESCRIPTOR IfDesc;\r | |
165 | UINT8 DeviceClass;\r | |
166 | UINT8 DeviceSubClass;\r | |
167 | UINT8 DeviceProtocol;\r | |
168 | \r | |
169 | if ((DevicePathType (UsbClass) != MESSAGING_DEVICE_PATH) ||\r | |
170 | (DevicePathSubType (UsbClass) != MSG_USB_CLASS_DP)){\r | |
171 | return FALSE;\r | |
172 | }\r | |
173 | \r | |
174 | //\r | |
175 | // Check Vendor Id and Product Id.\r | |
176 | //\r | |
177 | Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);\r | |
178 | if (EFI_ERROR (Status)) {\r | |
179 | return FALSE;\r | |
180 | }\r | |
181 | \r | |
182 | if ((UsbClass->VendorId != 0xffff) &&\r | |
183 | (UsbClass->VendorId != DevDesc.IdVendor)) {\r | |
184 | return FALSE;\r | |
185 | }\r | |
186 | \r | |
187 | if ((UsbClass->ProductId != 0xffff) &&\r | |
188 | (UsbClass->ProductId != DevDesc.IdProduct)) {\r | |
189 | return FALSE;\r | |
190 | }\r | |
191 | \r | |
192 | DeviceClass = DevDesc.DeviceClass;\r | |
193 | DeviceSubClass = DevDesc.DeviceSubClass;\r | |
194 | DeviceProtocol = DevDesc.DeviceProtocol;\r | |
195 | if (DeviceClass == 0) {\r | |
196 | //\r | |
197 | // If Class in Device Descriptor is set to 0, use the Class, SubClass and\r | |
198 | // Protocol in Interface Descriptor instead.\r | |
199 | //\r | |
200 | Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);\r | |
201 | if (EFI_ERROR (Status)) {\r | |
202 | return FALSE;\r | |
203 | }\r | |
204 | \r | |
205 | DeviceClass = IfDesc.InterfaceClass;\r | |
206 | DeviceSubClass = IfDesc.InterfaceSubClass;\r | |
207 | DeviceProtocol = IfDesc.InterfaceProtocol;\r | |
208 | }\r | |
209 | \r | |
210 | //\r | |
211 | // Check Class, SubClass and Protocol.\r | |
212 | //\r | |
213 | if ((UsbClass->DeviceClass != 0xff) &&\r | |
214 | (UsbClass->DeviceClass != DeviceClass)) {\r | |
215 | return FALSE;\r | |
216 | }\r | |
217 | \r | |
218 | if ((UsbClass->DeviceSubClass != 0xff) &&\r | |
219 | (UsbClass->DeviceSubClass != DeviceSubClass)) {\r | |
220 | return FALSE;\r | |
221 | }\r | |
222 | \r | |
223 | if ((UsbClass->DeviceProtocol != 0xff) &&\r | |
224 | (UsbClass->DeviceProtocol != DeviceProtocol)) {\r | |
225 | return FALSE;\r | |
226 | }\r | |
227 | \r | |
228 | return TRUE;\r | |
229 | }\r | |
230 | \r | |
231 | /**\r | |
232 | Check whether a USB device match the specified USB WWID device path. This\r | |
233 | function follows "Load Option Processing" behavior in UEFI specification.\r | |
234 | \r | |
235 | @param UsbIo USB I/O protocol associated with the USB device.\r | |
236 | @param UsbWwid The USB WWID device path to match.\r | |
237 | \r | |
238 | @retval TRUE The USB device match the USB WWID device path.\r | |
239 | @retval FALSE The USB device does not match the USB WWID device path.\r | |
240 | \r | |
241 | **/\r | |
242 | BOOLEAN\r | |
243 | BdsMatchUsbWwid (\r | |
244 | IN EFI_USB_IO_PROTOCOL *UsbIo,\r | |
245 | IN USB_WWID_DEVICE_PATH *UsbWwid\r | |
246 | )\r | |
247 | {\r | |
248 | EFI_STATUS Status;\r | |
249 | EFI_USB_DEVICE_DESCRIPTOR DevDesc;\r | |
250 | EFI_USB_INTERFACE_DESCRIPTOR IfDesc;\r | |
251 | UINT16 *LangIdTable;\r | |
252 | UINT16 TableSize;\r | |
253 | UINT16 Index;\r | |
254 | CHAR16 *CompareStr;\r | |
255 | UINTN CompareLen;\r | |
256 | CHAR16 *SerialNumberStr;\r | |
257 | UINTN Length;\r | |
258 | \r | |
259 | if ((DevicePathType (UsbWwid) != MESSAGING_DEVICE_PATH) ||\r | |
260 | (DevicePathSubType (UsbWwid) != MSG_USB_WWID_DP )){\r | |
261 | return FALSE;\r | |
262 | }\r | |
263 | \r | |
264 | //\r | |
265 | // Check Vendor Id and Product Id.\r | |
266 | //\r | |
267 | Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);\r | |
268 | if (EFI_ERROR (Status)) {\r | |
269 | return FALSE;\r | |
270 | }\r | |
271 | if ((DevDesc.IdVendor != UsbWwid->VendorId) ||\r | |
272 | (DevDesc.IdProduct != UsbWwid->ProductId)) {\r | |
273 | return FALSE;\r | |
274 | }\r | |
275 | \r | |
276 | //\r | |
277 | // Check Interface Number.\r | |
278 | //\r | |
279 | Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);\r | |
280 | if (EFI_ERROR (Status)) {\r | |
281 | return FALSE;\r | |
282 | }\r | |
283 | if (IfDesc.InterfaceNumber != UsbWwid->InterfaceNumber) {\r | |
284 | return FALSE;\r | |
285 | }\r | |
286 | \r | |
287 | //\r | |
288 | // Check Serial Number.\r | |
289 | //\r | |
290 | if (DevDesc.StrSerialNumber == 0) {\r | |
291 | return FALSE;\r | |
292 | }\r | |
293 | \r | |
294 | //\r | |
295 | // Get all supported languages.\r | |
296 | //\r | |
297 | TableSize = 0;\r | |
298 | LangIdTable = NULL;\r | |
299 | Status = UsbIo->UsbGetSupportedLanguages (UsbIo, &LangIdTable, &TableSize);\r | |
300 | if (EFI_ERROR (Status) || (TableSize == 0) || (LangIdTable == NULL)) {\r | |
301 | return FALSE;\r | |
302 | }\r | |
303 | \r | |
304 | //\r | |
305 | // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.\r | |
306 | //\r | |
307 | CompareStr = (CHAR16 *) (UINTN) (UsbWwid + 1);\r | |
308 | CompareLen = (DevicePathNodeLength (UsbWwid) - sizeof (USB_WWID_DEVICE_PATH)) / sizeof (CHAR16);\r | |
309 | if (CompareStr[CompareLen - 1] == L'\0') {\r | |
310 | CompareLen--;\r | |
311 | }\r | |
312 | \r | |
313 | //\r | |
314 | // Compare serial number in each supported language.\r | |
315 | //\r | |
316 | for (Index = 0; Index < TableSize / sizeof (UINT16); Index++) {\r | |
317 | SerialNumberStr = NULL;\r | |
318 | Status = UsbIo->UsbGetStringDescriptor (\r | |
319 | UsbIo,\r | |
320 | LangIdTable[Index],\r | |
321 | DevDesc.StrSerialNumber,\r | |
322 | &SerialNumberStr\r | |
323 | );\r | |
324 | if (EFI_ERROR (Status) || (SerialNumberStr == NULL)) {\r | |
325 | continue;\r | |
326 | }\r | |
327 | \r | |
328 | Length = StrLen (SerialNumberStr);\r | |
329 | if ((Length >= CompareLen) &&\r | |
330 | (CompareMem (SerialNumberStr + Length - CompareLen, CompareStr, CompareLen * sizeof (CHAR16)) == 0)) {\r | |
331 | FreePool (SerialNumberStr);\r | |
332 | return TRUE;\r | |
333 | }\r | |
334 | \r | |
335 | FreePool (SerialNumberStr);\r | |
336 | }\r | |
337 | \r | |
338 | return FALSE;\r | |
339 | }\r | |
340 | \r | |
341 | /**\r | |
9972247d | 342 | Find a USB device path which match the specified short-form device path start\r |
343 | with USB Class or USB WWID device path and load the boot file then return the \r | |
344 | image handle. If ParentDevicePath is NULL, this function will search in all USB\r | |
345 | devices of the platform. If ParentDevicePath is not NULL,this function will only\r | |
346 | search in its child devices.\r | |
7389fdd0 | 347 | \r |
348 | @param ParentDevicePath The device path of the parent.\r | |
349 | @param ShortFormDevicePath The USB Class or USB WWID device path to match.\r | |
350 | \r | |
9972247d | 351 | @return The image Handle if find load file from specified short-form device path\r |
352 | or NULL if not found.\r | |
7389fdd0 | 353 | \r |
354 | **/\r | |
355 | EFI_HANDLE *\r | |
356 | BdsFindUsbDevice (\r | |
357 | IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,\r | |
358 | IN EFI_DEVICE_PATH_PROTOCOL *ShortFormDevicePath\r | |
359 | )\r | |
360 | {\r | |
361 | EFI_STATUS Status;\r | |
362 | UINTN UsbIoHandleCount;\r | |
363 | EFI_HANDLE *UsbIoHandleBuffer;\r | |
364 | EFI_DEVICE_PATH_PROTOCOL *UsbIoDevicePath;\r | |
365 | EFI_USB_IO_PROTOCOL *UsbIo;\r | |
366 | UINTN Index;\r | |
367 | UINTN ParentSize;\r | |
368 | UINTN Size;\r | |
9972247d | 369 | EFI_HANDLE ImageHandle;\r |
370 | EFI_HANDLE Handle;\r | |
371 | EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;\r | |
372 | EFI_DEVICE_PATH_PROTOCOL *NextDevicePath;\r | |
373 | \r | |
374 | FullDevicePath = NULL;\r | |
375 | ImageHandle = NULL;\r | |
7389fdd0 | 376 | \r |
377 | //\r | |
378 | // Get all UsbIo Handles.\r | |
379 | //\r | |
380 | UsbIoHandleCount = 0;\r | |
381 | UsbIoHandleBuffer = NULL;\r | |
382 | Status = gBS->LocateHandleBuffer (\r | |
383 | ByProtocol,\r | |
384 | &gEfiUsbIoProtocolGuid,\r | |
385 | NULL,\r | |
386 | &UsbIoHandleCount,\r | |
387 | &UsbIoHandleBuffer\r | |
388 | );\r | |
389 | if (EFI_ERROR (Status) || (UsbIoHandleCount == 0) || (UsbIoHandleBuffer == NULL)) {\r | |
390 | return NULL;\r | |
391 | }\r | |
392 | \r | |
7389fdd0 | 393 | ParentSize = (ParentDevicePath == NULL) ? 0 : GetDevicePathSize (ParentDevicePath);\r |
394 | for (Index = 0; Index < UsbIoHandleCount; Index++) {\r | |
395 | //\r | |
396 | // Get the Usb IO interface.\r | |
397 | //\r | |
398 | Status = gBS->HandleProtocol(\r | |
399 | UsbIoHandleBuffer[Index],\r | |
400 | &gEfiUsbIoProtocolGuid,\r | |
401 | (VOID **) &UsbIo\r | |
402 | );\r | |
403 | if (EFI_ERROR (Status)) {\r | |
404 | continue;\r | |
405 | }\r | |
406 | \r | |
9972247d | 407 | UsbIoDevicePath = DevicePathFromHandle (UsbIoHandleBuffer[Index]);\r |
408 | if (UsbIoDevicePath == NULL) {\r | |
409 | continue;\r | |
410 | }\r | |
411 | \r | |
7389fdd0 | 412 | if (ParentDevicePath != NULL) {\r |
413 | //\r | |
414 | // Compare starting part of UsbIoHandle's device path with ParentDevicePath.\r | |
415 | //\r | |
7389fdd0 | 416 | Size = GetDevicePathSize (UsbIoDevicePath);\r |
417 | if ((Size < ParentSize) ||\r | |
418 | (CompareMem (UsbIoDevicePath, ParentDevicePath, ParentSize - END_DEVICE_PATH_LENGTH) != 0)) {\r | |
419 | continue;\r | |
420 | }\r | |
421 | }\r | |
422 | \r | |
423 | if (BdsMatchUsbClass (UsbIo, (USB_CLASS_DEVICE_PATH *) ShortFormDevicePath) ||\r | |
424 | BdsMatchUsbWwid (UsbIo, (USB_WWID_DEVICE_PATH *) ShortFormDevicePath)) {\r | |
9972247d | 425 | //\r |
426 | // Try to find if there is the boot file in this DevicePath\r | |
427 | //\r | |
428 | NextDevicePath = NextDevicePathNode (ShortFormDevicePath);\r | |
429 | if (!IsDevicePathEnd (NextDevicePath)) {\r | |
430 | FullDevicePath = AppendDevicePath (UsbIoDevicePath, NextDevicePath);\r | |
431 | //\r | |
432 | // Connect the full device path, so that Simple File System protocol\r | |
433 | // could be installed for this USB device.\r | |
434 | //\r | |
435 | BdsLibConnectDevicePath (FullDevicePath);\r | |
79b7a6a1 | 436 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r |
9972247d | 437 | Status = gBS->LoadImage (\r |
438 | TRUE,\r | |
439 | gImageHandle,\r | |
440 | FullDevicePath,\r | |
441 | NULL,\r | |
442 | 0,\r | |
443 | &ImageHandle\r | |
444 | );\r | |
445 | FreePool (FullDevicePath);\r | |
446 | } else {\r | |
447 | FullDevicePath = UsbIoDevicePath;\r | |
448 | Status = EFI_NOT_FOUND;\r | |
449 | }\r | |
450 | \r | |
451 | //\r | |
452 | // If we didn't find an image directly, we need to try as if it is a removable device boot option\r | |
453 | // and load the image according to the default boot behavior for removable device.\r | |
454 | //\r | |
455 | if (EFI_ERROR (Status)) {\r | |
456 | //\r | |
457 | // check if there is a bootable removable media could be found in this device path ,\r | |
458 | // and get the bootable media handle\r | |
459 | //\r | |
460 | Handle = BdsLibGetBootableHandle(UsbIoDevicePath);\r | |
461 | if (Handle == NULL) {\r | |
462 | continue;\r | |
463 | }\r | |
464 | //\r | |
465 | // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r | |
466 | // machinename is ia32, ia64, x64, ...\r | |
467 | //\r | |
468 | FullDevicePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r | |
469 | if (FullDevicePath != NULL) {\r | |
79b7a6a1 | 470 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r |
9972247d | 471 | Status = gBS->LoadImage (\r |
472 | TRUE,\r | |
473 | gImageHandle,\r | |
474 | FullDevicePath,\r | |
475 | NULL,\r | |
476 | 0,\r | |
477 | &ImageHandle\r | |
478 | );\r | |
479 | if (EFI_ERROR (Status)) {\r | |
480 | //\r | |
481 | // The DevicePath failed, and it's not a valid\r | |
482 | // removable media device.\r | |
483 | //\r | |
484 | continue;\r | |
485 | }\r | |
486 | } else {\r | |
487 | continue;\r | |
488 | }\r | |
489 | }\r | |
7389fdd0 | 490 | break;\r |
491 | }\r | |
492 | }\r | |
493 | \r | |
494 | FreePool (UsbIoHandleBuffer);\r | |
9972247d | 495 | return ImageHandle;\r |
7389fdd0 | 496 | }\r |
497 | \r | |
498 | /**\r | |
499 | Expand USB Class or USB WWID device path node to be full device path of a USB\r | |
9972247d | 500 | device in platform then load the boot file on this full device path and return the \r |
501 | image handle.\r | |
7389fdd0 | 502 | \r |
503 | This function support following 4 cases:\r | |
504 | 1) Boot Option device path starts with a USB Class or USB WWID device path,\r | |
505 | and there is no Media FilePath device path in the end.\r | |
506 | In this case, it will follow Removable Media Boot Behavior.\r | |
507 | 2) Boot Option device path starts with a USB Class or USB WWID device path,\r | |
508 | and ended with Media FilePath device path.\r | |
509 | 3) Boot Option device path starts with a full device path to a USB Host Controller,\r | |
510 | contains a USB Class or USB WWID device path node, while not ended with Media\r | |
511 | FilePath device path. In this case, it will follow Removable Media Boot Behavior.\r | |
512 | 4) Boot Option device path starts with a full device path to a USB Host Controller,\r | |
513 | contains a USB Class or USB WWID device path node, and ended with Media\r | |
514 | FilePath device path.\r | |
515 | \r | |
516 | @param DevicePath The Boot Option device path.\r | |
517 | \r | |
9972247d | 518 | @return The image handle of boot file, or NULL if there is no boot file found in\r |
519 | the specified USB Class or USB WWID device path.\r | |
7389fdd0 | 520 | \r |
521 | **/\r | |
9972247d | 522 | EFI_HANDLE *\r |
7389fdd0 | 523 | BdsExpandUsbShortFormDevicePath (\r |
524 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r | |
525 | )\r | |
526 | {\r | |
9972247d | 527 | EFI_HANDLE *ImageHandle;\r |
7389fdd0 | 528 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r |
7389fdd0 | 529 | EFI_DEVICE_PATH_PROTOCOL *ShortFormDevicePath;\r |
530 | \r | |
531 | //\r | |
532 | // Search for USB Class or USB WWID device path node.\r | |
533 | //\r | |
534 | ShortFormDevicePath = NULL;\r | |
9972247d | 535 | ImageHandle = NULL;\r |
536 | TempDevicePath = DevicePath;\r | |
7389fdd0 | 537 | while (!IsDevicePathEnd (TempDevicePath)) {\r |
538 | if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&\r | |
539 | ((DevicePathSubType (TempDevicePath) == MSG_USB_CLASS_DP) ||\r | |
540 | (DevicePathSubType (TempDevicePath) == MSG_USB_WWID_DP))) {\r | |
541 | ShortFormDevicePath = TempDevicePath;\r | |
542 | break;\r | |
543 | }\r | |
7389fdd0 | 544 | TempDevicePath = NextDevicePathNode (TempDevicePath);\r |
545 | }\r | |
546 | \r | |
547 | if (ShortFormDevicePath == NULL) {\r | |
548 | //\r | |
549 | // No USB Class or USB WWID device path node found, do nothing.\r | |
550 | //\r | |
551 | return NULL;\r | |
552 | }\r | |
553 | \r | |
554 | if (ShortFormDevicePath == DevicePath) {\r | |
555 | //\r | |
556 | // Boot Option device path starts with USB Class or USB WWID device path.\r | |
557 | //\r | |
9972247d | 558 | ImageHandle = BdsFindUsbDevice (NULL, ShortFormDevicePath);\r |
559 | if (ImageHandle == NULL) {\r | |
7389fdd0 | 560 | //\r |
561 | // Failed to find a match in existing devices, connect the short form USB\r | |
562 | // device path and try again.\r | |
563 | //\r | |
564 | BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath);\r | |
9972247d | 565 | ImageHandle = BdsFindUsbDevice (NULL, ShortFormDevicePath);\r |
7389fdd0 | 566 | }\r |
567 | } else {\r | |
568 | //\r | |
569 | // Boot Option device path contains USB Class or USB WWID device path node.\r | |
570 | //\r | |
571 | \r | |
572 | //\r | |
573 | // Prepare the parent device path for search.\r | |
574 | //\r | |
575 | TempDevicePath = DuplicateDevicePath (DevicePath);\r | |
576 | ASSERT (TempDevicePath != NULL);\r | |
577 | SetDevicePathEndNode (((UINT8 *) TempDevicePath) + ((UINTN) ShortFormDevicePath - (UINTN) DevicePath));\r | |
578 | \r | |
579 | //\r | |
9972247d | 580 | // The USB Host Controller device path is already in Boot Option device path\r |
7389fdd0 | 581 | // and USB Bus driver already support RemainingDevicePath starts with USB\r |
582 | // Class or USB WWID device path, so just search in existing USB devices and\r | |
583 | // doesn't perform ConnectController here.\r | |
584 | //\r | |
9972247d | 585 | ImageHandle = BdsFindUsbDevice (TempDevicePath, ShortFormDevicePath);\r |
7389fdd0 | 586 | FreePool (TempDevicePath);\r |
587 | }\r | |
588 | \r | |
9972247d | 589 | return ImageHandle;\r |
7389fdd0 | 590 | }\r |
591 | \r | |
5c08e117 | 592 | /**\r |
593 | Process the boot option follow the UEFI specification and\r | |
594 | special treat the legacy boot option with BBS_DEVICE_PATH.\r | |
595 | \r | |
596 | @param Option The boot option need to be processed\r | |
597 | @param DevicePath The device path which describe where to load the\r | |
598 | boot image or the legacy BBS device path to boot\r | |
599 | the legacy OS\r | |
600 | @param ExitDataSize The size of exit data.\r | |
601 | @param ExitData Data returned when Boot image failed.\r | |
602 | \r | |
603 | @retval EFI_SUCCESS Boot from the input boot option successfully.\r | |
604 | @retval EFI_NOT_FOUND If the Device Path is not found in the system\r | |
605 | \r | |
606 | **/\r | |
607 | EFI_STATUS\r | |
608 | EFIAPI\r | |
609 | BdsLibBootViaBootOption (\r | |
610 | IN BDS_COMMON_OPTION *Option,\r | |
611 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r | |
612 | OUT UINTN *ExitDataSize,\r | |
613 | OUT CHAR16 **ExitData OPTIONAL\r | |
614 | )\r | |
615 | {\r | |
616 | EFI_STATUS Status;\r | |
2df686c6 | 617 | EFI_STATUS StatusLogo;\r |
5c08e117 | 618 | EFI_HANDLE Handle;\r |
619 | EFI_HANDLE ImageHandle;\r | |
620 | EFI_DEVICE_PATH_PROTOCOL *FilePath;\r | |
621 | EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r | |
622 | EFI_DEVICE_PATH_PROTOCOL *WorkingDevicePath;\r | |
623 | EFI_ACPI_S3_SAVE_PROTOCOL *AcpiS3Save;\r | |
624 | LIST_ENTRY TempBootLists;\r | |
2df686c6 | 625 | EFI_BOOT_LOGO_PROTOCOL *BootLogo;\r |
5c08e117 | 626 | \r |
627 | //\r | |
628 | // Record the performance data for End of BDS\r | |
629 | //\r | |
128efbbc | 630 | PERF_END(NULL, "BDS", NULL, 0);\r |
5c08e117 | 631 | \r |
632 | *ExitDataSize = 0;\r | |
633 | *ExitData = NULL;\r | |
634 | \r | |
5c08e117 | 635 | //\r |
636 | // Notes: this code can be remove after the s3 script table\r | |
637 | // hook on the event EVT_SIGNAL_READY_TO_BOOT or\r | |
638 | // EVT_SIGNAL_LEGACY_BOOT\r | |
639 | //\r | |
640 | Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL, (VOID **) &AcpiS3Save);\r | |
641 | if (!EFI_ERROR (Status)) {\r | |
642 | AcpiS3Save->S3Save (AcpiS3Save, NULL);\r | |
643 | }\r | |
644 | //\r | |
645 | // If it's Device Path that starts with a hard drive path, append it with the front part to compose a\r | |
646 | // full device path\r | |
647 | //\r | |
648 | WorkingDevicePath = NULL;\r | |
649 | if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r | |
650 | (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) {\r | |
651 | WorkingDevicePath = BdsExpandPartitionPartialDevicePathToFull (\r | |
652 | (HARDDRIVE_DEVICE_PATH *)DevicePath\r | |
653 | );\r | |
654 | if (WorkingDevicePath != NULL) {\r | |
655 | DevicePath = WorkingDevicePath;\r | |
656 | }\r | |
657 | }\r | |
7389fdd0 | 658 | \r |
5c08e117 | 659 | //\r |
660 | // Set Boot Current\r | |
661 | //\r | |
3d7decbc | 662 | if (IsBootOptionValidNVVarialbe (Option)) {\r |
663 | //\r | |
664 | // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.\r | |
665 | // In this case, "BootCurrent" is not created.\r | |
666 | // Only create the BootCurrent variable when it points to a valid Boot#### variable.\r | |
667 | //\r | |
668 | gRT->SetVariable (\r | |
669 | L"BootCurrent",\r | |
670 | &gEfiGlobalVariableGuid,\r | |
671 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
672 | sizeof (UINT16),\r | |
673 | &Option->BootCurrent\r | |
674 | );\r | |
675 | }\r | |
5c08e117 | 676 | \r |
72861c22 | 677 | //\r |
678 | // Signal the EVT_SIGNAL_READY_TO_BOOT event\r | |
679 | //\r | |
680 | EfiSignalEventReadyToBoot();\r | |
681 | \r | |
682 | //\r | |
683 | // Expand USB Class or USB WWID device path node to be full device path of a USB\r | |
684 | // device in platform then load the boot file on this full device path and get the\r | |
685 | // image handle.\r | |
686 | //\r | |
687 | ImageHandle = BdsExpandUsbShortFormDevicePath (DevicePath);\r | |
688 | \r | |
689 | //\r | |
690 | // Adjust the different type memory page number just before booting\r | |
691 | // and save the updated info into the variable for next boot to use\r | |
692 | //\r | |
693 | BdsSetMemoryTypeInformationVariable ();\r | |
694 | \r | |
5c08e117 | 695 | //\r |
9972247d | 696 | // By expanding the USB Class or WWID device path, the ImageHandle has returnned.\r |
697 | // Here get the ImageHandle for the non USB class or WWID device path.\r | |
5c08e117 | 698 | //\r |
9972247d | 699 | if (ImageHandle == NULL) {\r |
700 | ASSERT (Option->DevicePath != NULL);\r | |
701 | if ((DevicePathType (Option->DevicePath) == BBS_DEVICE_PATH) &&\r | |
702 | (DevicePathSubType (Option->DevicePath) == BBS_BBS_DP)\r | |
703 | ) {\r | |
704 | //\r | |
705 | // Check to see if we should legacy BOOT. If yes then do the legacy boot\r | |
706 | //\r | |
707 | return BdsLibDoLegacyBoot (Option);\r | |
5c08e117 | 708 | }\r |
128efbbc | 709 | \r |
5c08e117 | 710 | //\r |
9972247d | 711 | // If the boot option point to Internal FV shell, make sure it is valid\r |
5c08e117 | 712 | //\r |
9972247d | 713 | Status = BdsLibUpdateFvFileDevicePath (&DevicePath, PcdGetPtr(PcdShellFile));\r |
714 | if (!EFI_ERROR(Status)) {\r | |
715 | if (Option->DevicePath != NULL) {\r | |
716 | FreePool(Option->DevicePath);\r | |
717 | }\r | |
718 | Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));\r | |
719 | ASSERT(Option->DevicePath != NULL);\r | |
720 | CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));\r | |
721 | //\r | |
722 | // Update the shell boot option\r | |
723 | //\r | |
724 | InitializeListHead (&TempBootLists);\r | |
725 | BdsLibRegisterNewOption (&TempBootLists, DevicePath, L"EFI Internal Shell", L"BootOrder");\r | |
726 | \r | |
727 | //\r | |
728 | // free the temporary device path created by BdsLibUpdateFvFileDevicePath()\r | |
729 | //\r | |
730 | FreePool (DevicePath);\r | |
731 | DevicePath = Option->DevicePath;\r | |
732 | }\r | |
5c08e117 | 733 | \r |
9972247d | 734 | DEBUG_CODE_BEGIN();\r |
5c08e117 | 735 | \r |
9aa7ba01 | 736 | if (Option->Description == NULL) {\r |
737 | DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting from unknown device path\n"));\r | |
738 | } else {\r | |
739 | DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting %S\n", Option->Description));\r | |
ab8cc80b | 740 | }\r |
9aa7ba01 | 741 | \r |
9972247d | 742 | DEBUG_CODE_END();\r |
ab8cc80b | 743 | \r |
79b7a6a1 | 744 | //\r |
745 | // Report status code for OS Loader LoadImage.\r | |
746 | //\r | |
747 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r | |
9972247d | 748 | Status = gBS->LoadImage (\r |
749 | TRUE,\r | |
750 | gImageHandle,\r | |
751 | DevicePath,\r | |
752 | NULL,\r | |
753 | 0,\r | |
754 | &ImageHandle\r | |
755 | );\r | |
5c08e117 | 756 | \r |
5c08e117 | 757 | //\r |
9972247d | 758 | // If we didn't find an image directly, we need to try as if it is a removable device boot option\r |
759 | // and load the image according to the default boot behavior for removable device.\r | |
5c08e117 | 760 | //\r |
9972247d | 761 | if (EFI_ERROR (Status)) {\r |
762 | //\r | |
763 | // check if there is a bootable removable media could be found in this device path ,\r | |
764 | // and get the bootable media handle\r | |
765 | //\r | |
766 | Handle = BdsLibGetBootableHandle(DevicePath);\r | |
767 | if (Handle == NULL) {\r | |
5c08e117 | 768 | goto Done;\r |
769 | }\r | |
9972247d | 770 | //\r |
771 | // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r | |
772 | // machinename is ia32, ia64, x64, ...\r | |
773 | //\r | |
774 | FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r | |
775 | if (FilePath != NULL) {\r | |
79b7a6a1 | 776 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r |
9972247d | 777 | Status = gBS->LoadImage (\r |
778 | TRUE,\r | |
779 | gImageHandle,\r | |
780 | FilePath,\r | |
781 | NULL,\r | |
782 | 0,\r | |
783 | &ImageHandle\r | |
784 | );\r | |
785 | if (EFI_ERROR (Status)) {\r | |
786 | //\r | |
787 | // The DevicePath failed, and it's not a valid\r | |
788 | // removable media device.\r | |
789 | //\r | |
790 | goto Done;\r | |
791 | }\r | |
792 | }\r | |
5c08e117 | 793 | }\r |
5c08e117 | 794 | \r |
9972247d | 795 | if (EFI_ERROR (Status)) {\r |
796 | //\r | |
797 | // It there is any error from the Boot attempt exit now.\r | |
798 | //\r | |
799 | goto Done;\r | |
800 | }\r | |
5c08e117 | 801 | }\r |
802 | //\r | |
803 | // Provide the image with it's load options\r | |
804 | //\r | |
9972247d | 805 | if (ImageHandle == NULL) {\r |
806 | goto Done;\r | |
807 | }\r | |
5c08e117 | 808 | Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r |
809 | ASSERT_EFI_ERROR (Status);\r | |
810 | \r | |
811 | if (Option->LoadOptionsSize != 0) {\r | |
812 | ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;\r | |
813 | ImageInfo->LoadOptions = Option->LoadOptions;\r | |
814 | }\r | |
bf0712ae ED |
815 | \r |
816 | //\r | |
817 | // Clean to NULL because the image is loaded directly from the firmwares boot manager.\r | |
818 | //\r | |
819 | ImageInfo->ParentHandle = NULL;\r | |
820 | \r | |
5c08e117 | 821 | //\r |
822 | // Before calling the image, enable the Watchdog Timer for\r | |
823 | // the 5 Minute period\r | |
824 | //\r | |
825 | gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r | |
826 | \r | |
cd6a3b15 | 827 | //\r |
828 | // Write boot to OS performance data for UEFI boot\r | |
829 | //\r | |
830 | PERF_CODE (\r | |
831 | WriteBootToOsPerformanceData ();\r | |
832 | );\r | |
833 | \r | |
79b7a6a1 | 834 | //\r |
835 | // Report status code for OS Loader StartImage.\r | |
836 | //\r | |
837 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderStart));\r | |
838 | \r | |
5c08e117 | 839 | Status = gBS->StartImage (ImageHandle, ExitDataSize, ExitData);\r |
840 | DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));\r | |
841 | \r | |
842 | //\r | |
843 | // Clear the Watchdog Timer after the image returns\r | |
844 | //\r | |
845 | gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r | |
846 | \r | |
847 | Done:\r | |
2df686c6 | 848 | //\r |
849 | // Set Logo status invalid after trying one boot option\r | |
850 | //\r | |
851 | BootLogo = NULL;\r | |
852 | StatusLogo = gBS->LocateProtocol (&gEfiBootLogoProtocolGuid, NULL, (VOID **) &BootLogo);\r | |
853 | if (!EFI_ERROR (StatusLogo) && (BootLogo != NULL)) {\r | |
854 | BootLogo->SetBootLogo (BootLogo, NULL, 0, 0, 0, 0);\r | |
855 | }\r | |
856 | \r | |
5c08e117 | 857 | //\r |
858 | // Clear Boot Current\r | |
859 | //\r | |
860 | gRT->SetVariable (\r | |
861 | L"BootCurrent",\r | |
862 | &gEfiGlobalVariableGuid,\r | |
863 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
864 | 0,\r | |
865 | &Option->BootCurrent\r | |
866 | );\r | |
867 | \r | |
868 | return Status;\r | |
869 | }\r | |
870 | \r | |
871 | \r | |
872 | /**\r | |
873 | Expand a device path that starts with a hard drive media device path node to be a\r | |
874 | full device path that includes the full hardware path to the device. We need\r | |
875 | to do this so it can be booted. As an optimization the front match (the part point\r | |
876 | to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable\r | |
877 | so a connect all is not required on every boot. All successful history device path\r | |
878 | which point to partition node (the front part) will be saved.\r | |
879 | \r | |
880 | @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard\r | |
881 | drive media device path.\r | |
882 | @return A Pointer to the full device path or NULL if a valid Hard Drive devic path\r | |
883 | cannot be found.\r | |
884 | \r | |
885 | **/\r | |
886 | EFI_DEVICE_PATH_PROTOCOL *\r | |
887 | EFIAPI\r | |
888 | BdsExpandPartitionPartialDevicePathToFull (\r | |
889 | IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r | |
890 | )\r | |
891 | {\r | |
892 | EFI_STATUS Status;\r | |
893 | UINTN BlockIoHandleCount;\r | |
894 | EFI_HANDLE *BlockIoBuffer;\r | |
895 | EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;\r | |
896 | EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;\r | |
897 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
898 | UINTN Index;\r | |
899 | UINTN InstanceNum;\r | |
900 | EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath;\r | |
901 | EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r | |
902 | UINTN CachedDevicePathSize;\r | |
903 | BOOLEAN DeviceExist;\r | |
904 | BOOLEAN NeedAdjust;\r | |
905 | EFI_DEVICE_PATH_PROTOCOL *Instance;\r | |
906 | UINTN Size;\r | |
907 | \r | |
908 | FullDevicePath = NULL;\r | |
909 | //\r | |
e24fc103 | 910 | // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.\r |
5c08e117 | 911 | // If exist, search the front path which point to partition node in the variable instants.\r |
e24fc103 | 912 | // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system\r |
5c08e117 | 913 | //\r |
914 | CachedDevicePath = BdsLibGetVariableAndSize (\r | |
e24fc103 LG |
915 | HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r |
916 | &gHdBootDevicePathVariablGuid,\r | |
5c08e117 | 917 | &CachedDevicePathSize\r |
918 | );\r | |
128efbbc | 919 | \r |
5c08e117 | 920 | if (CachedDevicePath != NULL) {\r |
921 | TempNewDevicePath = CachedDevicePath;\r | |
922 | DeviceExist = FALSE;\r | |
923 | NeedAdjust = FALSE;\r | |
924 | do {\r | |
925 | //\r | |
926 | // Check every instance of the variable\r | |
927 | // First, check whether the instance contain the partition node, which is needed for distinguishing multi\r | |
928 | // partial partition boot option. Second, check whether the instance could be connected.\r | |
929 | //\r | |
930 | Instance = GetNextDevicePathInstance (&TempNewDevicePath, &Size);\r | |
931 | if (MatchPartitionDevicePathNode (Instance, HardDriveDevicePath)) {\r | |
932 | //\r | |
933 | // Connect the device path instance, the device path point to hard drive media device path node\r | |
934 | // e.g. ACPI() /PCI()/ATA()/Partition()\r | |
935 | //\r | |
936 | Status = BdsLibConnectDevicePath (Instance);\r | |
937 | if (!EFI_ERROR (Status)) {\r | |
938 | DeviceExist = TRUE;\r | |
939 | break;\r | |
940 | }\r | |
941 | }\r | |
942 | //\r | |
943 | // Come here means the first instance is not matched\r | |
944 | //\r | |
945 | NeedAdjust = TRUE;\r | |
946 | FreePool(Instance);\r | |
947 | } while (TempNewDevicePath != NULL);\r | |
948 | \r | |
949 | if (DeviceExist) {\r | |
950 | //\r | |
951 | // Find the matched device path.\r | |
952 | // Append the file path information from the boot option and return the fully expanded device path.\r | |
953 | //\r | |
954 | DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r | |
955 | FullDevicePath = AppendDevicePath (Instance, DevicePath);\r | |
956 | \r | |
957 | //\r | |
e24fc103 | 958 | // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.\r |
5c08e117 | 959 | //\r |
960 | if (NeedAdjust) {\r | |
961 | //\r | |
962 | // First delete the matched instance.\r | |
963 | //\r | |
964 | TempNewDevicePath = CachedDevicePath;\r | |
965 | CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );\r | |
966 | FreePool (TempNewDevicePath);\r | |
128efbbc | 967 | \r |
5c08e117 | 968 | //\r |
969 | // Second, append the remaining path after the matched instance\r | |
970 | //\r | |
971 | TempNewDevicePath = CachedDevicePath;\r | |
972 | CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );\r | |
973 | FreePool (TempNewDevicePath);\r | |
974 | //\r | |
975 | // Save the matching Device Path so we don't need to do a connect all next time\r | |
976 | //\r | |
977 | Status = gRT->SetVariable (\r | |
e24fc103 LG |
978 | HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r |
979 | &gHdBootDevicePathVariablGuid,\r | |
5c08e117 | 980 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r |
981 | GetDevicePathSize (CachedDevicePath),\r | |
982 | CachedDevicePath\r | |
983 | );\r | |
984 | }\r | |
128efbbc | 985 | \r |
5c08e117 | 986 | FreePool (Instance);\r |
987 | FreePool (CachedDevicePath);\r | |
988 | return FullDevicePath;\r | |
989 | }\r | |
990 | }\r | |
991 | \r | |
992 | //\r | |
e24fc103 | 993 | // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need\r |
5c08e117 | 994 | // to search all devices in the system for a matched partition\r |
995 | //\r | |
996 | BdsLibConnectAllDriversToAllControllers ();\r | |
997 | Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);\r | |
998 | if (EFI_ERROR (Status) || BlockIoHandleCount == 0 || BlockIoBuffer == NULL) {\r | |
999 | //\r | |
1000 | // If there was an error or there are no device handles that support\r | |
1001 | // the BLOCK_IO Protocol, then return.\r | |
1002 | //\r | |
1003 | return NULL;\r | |
1004 | }\r | |
1005 | //\r | |
1006 | // Loop through all the device handles that support the BLOCK_IO Protocol\r | |
1007 | //\r | |
1008 | for (Index = 0; Index < BlockIoHandleCount; Index++) {\r | |
1009 | \r | |
1010 | Status = gBS->HandleProtocol (BlockIoBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID *) &BlockIoDevicePath);\r | |
1011 | if (EFI_ERROR (Status) || BlockIoDevicePath == NULL) {\r | |
1012 | continue;\r | |
1013 | }\r | |
1014 | \r | |
1015 | if (MatchPartitionDevicePathNode (BlockIoDevicePath, HardDriveDevicePath)) {\r | |
1016 | //\r | |
1017 | // Find the matched partition device path\r | |
1018 | //\r | |
1019 | DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r | |
1020 | FullDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);\r | |
1021 | \r | |
1022 | //\r | |
e24fc103 | 1023 | // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable\r |
5c08e117 | 1024 | //\r |
1025 | if (CachedDevicePath != NULL) {\r | |
1026 | //\r | |
e24fc103 | 1027 | // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable\r |
5c08e117 | 1028 | //\r |
1029 | if (BdsLibMatchDevicePaths (CachedDevicePath, BlockIoDevicePath)) {\r | |
1030 | TempNewDevicePath = CachedDevicePath;\r | |
1031 | CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);\r | |
1032 | FreePool(TempNewDevicePath);\r | |
1033 | \r | |
1034 | TempNewDevicePath = CachedDevicePath;\r | |
1035 | CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r | |
cd730ec0 | 1036 | if (TempNewDevicePath != NULL) {\r |
1037 | FreePool(TempNewDevicePath);\r | |
1038 | }\r | |
5c08e117 | 1039 | } else {\r |
1040 | TempNewDevicePath = CachedDevicePath;\r | |
1041 | CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r | |
1042 | FreePool(TempNewDevicePath);\r | |
1043 | }\r | |
1044 | //\r | |
1045 | // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller\r | |
e24fc103 LG |
1046 | // If the user try to boot many OS in different HDs or partitions, in theory, \r |
1047 | // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.\r | |
5c08e117 | 1048 | //\r |
1049 | InstanceNum = 0;\r | |
1050 | ASSERT (CachedDevicePath != NULL);\r | |
1051 | TempNewDevicePath = CachedDevicePath;\r | |
1052 | while (!IsDevicePathEnd (TempNewDevicePath)) {\r | |
1053 | TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);\r | |
1054 | //\r | |
1055 | // Parse one instance\r | |
1056 | //\r | |
1057 | while (!IsDevicePathEndType (TempNewDevicePath)) {\r | |
1058 | TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);\r | |
1059 | }\r | |
1060 | InstanceNum++;\r | |
1061 | //\r | |
1062 | // If the CachedDevicePath variable contain too much instance, only remain 12 instances.\r | |
1063 | //\r | |
1064 | if (InstanceNum >= 12) {\r | |
1065 | SetDevicePathEndNode (TempNewDevicePath);\r | |
1066 | break;\r | |
1067 | }\r | |
1068 | }\r | |
1069 | } else {\r | |
1070 | CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);\r | |
1071 | }\r | |
1072 | \r | |
1073 | //\r | |
1074 | // Save the matching Device Path so we don't need to do a connect all next time\r | |
1075 | //\r | |
1076 | Status = gRT->SetVariable (\r | |
e24fc103 LG |
1077 | HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r |
1078 | &gHdBootDevicePathVariablGuid,\r | |
5c08e117 | 1079 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r |
1080 | GetDevicePathSize (CachedDevicePath),\r | |
1081 | CachedDevicePath\r | |
1082 | );\r | |
1083 | \r | |
1084 | break;\r | |
1085 | }\r | |
1086 | }\r | |
128efbbc | 1087 | \r |
cd730ec0 | 1088 | if (CachedDevicePath != NULL) {\r |
1089 | FreePool (CachedDevicePath);\r | |
1090 | }\r | |
5c08e117 | 1091 | if (BlockIoBuffer != NULL) {\r |
1092 | FreePool (BlockIoBuffer);\r | |
1093 | }\r | |
1094 | return FullDevicePath;\r | |
1095 | }\r | |
1096 | \r | |
1097 | /**\r | |
1098 | Check whether there is a instance in BlockIoDevicePath, which contain multi device path\r | |
1099 | instances, has the same partition node with HardDriveDevicePath device path\r | |
1100 | \r | |
1101 | @param BlockIoDevicePath Multi device path instances which need to check\r | |
1102 | @param HardDriveDevicePath A device path which starts with a hard drive media\r | |
1103 | device path.\r | |
1104 | \r | |
1105 | @retval TRUE There is a matched device path instance.\r | |
1106 | @retval FALSE There is no matched device path instance.\r | |
1107 | \r | |
1108 | **/\r | |
1109 | BOOLEAN\r | |
1110 | EFIAPI\r | |
1111 | MatchPartitionDevicePathNode (\r | |
1112 | IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,\r | |
1113 | IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r | |
1114 | )\r | |
1115 | {\r | |
1116 | HARDDRIVE_DEVICE_PATH *TmpHdPath;\r | |
1117 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
1118 | BOOLEAN Match;\r | |
1119 | EFI_DEVICE_PATH_PROTOCOL *BlockIoHdDevicePathNode;\r | |
1120 | \r | |
1121 | if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {\r | |
1122 | return FALSE;\r | |
1123 | }\r | |
128efbbc | 1124 | \r |
5c08e117 | 1125 | //\r |
1126 | // Make PreviousDevicePath == the device path node before the end node\r | |
1127 | //\r | |
1128 | DevicePath = BlockIoDevicePath;\r | |
1129 | BlockIoHdDevicePathNode = NULL;\r | |
1130 | \r | |
1131 | //\r | |
1132 | // find the partition device path node\r | |
1133 | //\r | |
1134 | while (!IsDevicePathEnd (DevicePath)) {\r | |
1135 | if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r | |
1136 | (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)\r | |
1137 | ) {\r | |
1138 | BlockIoHdDevicePathNode = DevicePath;\r | |
1139 | break;\r | |
1140 | }\r | |
1141 | \r | |
1142 | DevicePath = NextDevicePathNode (DevicePath);\r | |
1143 | }\r | |
1144 | \r | |
1145 | if (BlockIoHdDevicePathNode == NULL) {\r | |
1146 | return FALSE;\r | |
1147 | }\r | |
1148 | //\r | |
1149 | // See if the harddrive device path in blockio matches the orig Hard Drive Node\r | |
1150 | //\r | |
1151 | TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePathNode;\r | |
1152 | Match = FALSE;\r | |
128efbbc | 1153 | \r |
5c08e117 | 1154 | //\r |
1155 | // Check for the match\r | |
1156 | //\r | |
1157 | if ((TmpHdPath->MBRType == HardDriveDevicePath->MBRType) &&\r | |
1158 | (TmpHdPath->SignatureType == HardDriveDevicePath->SignatureType)) {\r | |
1159 | switch (TmpHdPath->SignatureType) {\r | |
1160 | case SIGNATURE_TYPE_GUID:\r | |
1161 | Match = CompareGuid ((EFI_GUID *)TmpHdPath->Signature, (EFI_GUID *)HardDriveDevicePath->Signature);\r | |
1162 | break;\r | |
1163 | case SIGNATURE_TYPE_MBR:\r | |
1164 | Match = (BOOLEAN)(*((UINT32 *)(&(TmpHdPath->Signature[0]))) == ReadUnaligned32((UINT32 *)(&(HardDriveDevicePath->Signature[0]))));\r | |
1165 | break;\r | |
1166 | default:\r | |
1167 | Match = FALSE;\r | |
1168 | break;\r | |
1169 | }\r | |
1170 | }\r | |
1171 | \r | |
1172 | return Match;\r | |
1173 | }\r | |
1174 | \r | |
1175 | /**\r | |
1176 | Delete the boot option associated with the handle passed in.\r | |
1177 | \r | |
1178 | @param Handle The handle which present the device path to create\r | |
1179 | boot option\r | |
1180 | \r | |
1181 | @retval EFI_SUCCESS Delete the boot option success\r | |
1182 | @retval EFI_NOT_FOUND If the Device Path is not found in the system\r | |
1183 | @retval EFI_OUT_OF_RESOURCES Lack of memory resource\r | |
1184 | @retval Other Error return value from SetVariable()\r | |
1185 | \r | |
1186 | **/\r | |
1187 | EFI_STATUS\r | |
1188 | BdsLibDeleteOptionFromHandle (\r | |
1189 | IN EFI_HANDLE Handle\r | |
1190 | )\r | |
1191 | {\r | |
1192 | UINT16 *BootOrder;\r | |
1193 | UINT8 *BootOptionVar;\r | |
1194 | UINTN BootOrderSize;\r | |
1195 | UINTN BootOptionSize;\r | |
1196 | EFI_STATUS Status;\r | |
1197 | UINTN Index;\r | |
1198 | UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r | |
1199 | UINTN DevicePathSize;\r | |
1200 | UINTN OptionDevicePathSize;\r | |
1201 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
1202 | EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r | |
1203 | UINT8 *TempPtr;\r | |
1204 | \r | |
1205 | Status = EFI_SUCCESS;\r | |
1206 | BootOrder = NULL;\r | |
1207 | BootOrderSize = 0;\r | |
1208 | \r | |
1209 | //\r | |
1210 | // Check "BootOrder" variable, if no, means there is no any boot order.\r | |
1211 | //\r | |
1212 | BootOrder = BdsLibGetVariableAndSize (\r | |
1213 | L"BootOrder",\r | |
1214 | &gEfiGlobalVariableGuid,\r | |
1215 | &BootOrderSize\r | |
1216 | );\r | |
1217 | if (BootOrder == NULL) {\r | |
1218 | return EFI_NOT_FOUND;\r | |
1219 | }\r | |
1220 | \r | |
1221 | //\r | |
1222 | // Convert device handle to device path protocol instance\r | |
1223 | //\r | |
1224 | DevicePath = DevicePathFromHandle (Handle);\r | |
1225 | if (DevicePath == NULL) {\r | |
1226 | return EFI_NOT_FOUND;\r | |
1227 | }\r | |
1228 | DevicePathSize = GetDevicePathSize (DevicePath);\r | |
1229 | \r | |
1230 | //\r | |
1231 | // Loop all boot order variable and find the matching device path\r | |
1232 | //\r | |
1233 | Index = 0;\r | |
1234 | while (Index < BootOrderSize / sizeof (UINT16)) {\r | |
1235 | UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r | |
1236 | BootOptionVar = BdsLibGetVariableAndSize (\r | |
1237 | BootOption,\r | |
1238 | &gEfiGlobalVariableGuid,\r | |
1239 | &BootOptionSize\r | |
1240 | );\r | |
128efbbc | 1241 | \r |
5c08e117 | 1242 | if (BootOptionVar == NULL) {\r |
1243 | FreePool (BootOrder);\r | |
1244 | return EFI_OUT_OF_RESOURCES;\r | |
1245 | }\r | |
1246 | \r | |
8c08a567 ED |
1247 | if (!ValidateOption(BootOptionVar, BootOptionSize)) {\r |
1248 | BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r | |
1249 | FreePool (BootOptionVar);\r | |
1250 | Index++;\r | |
1251 | continue;\r | |
1252 | }\r | |
1253 | \r | |
5c08e117 | 1254 | TempPtr = BootOptionVar;\r |
1255 | TempPtr += sizeof (UINT32) + sizeof (UINT16);\r | |
1256 | TempPtr += StrSize ((CHAR16 *) TempPtr);\r | |
1257 | OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r | |
1258 | OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);\r | |
1259 | \r | |
1260 | //\r | |
1261 | // Check whether the device path match\r | |
1262 | //\r | |
1263 | if ((OptionDevicePathSize == DevicePathSize) &&\r | |
1264 | (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {\r | |
1265 | BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r | |
1266 | FreePool (BootOptionVar);\r | |
1267 | break;\r | |
1268 | }\r | |
1269 | \r | |
1270 | FreePool (BootOptionVar);\r | |
1271 | Index++;\r | |
1272 | }\r | |
1273 | \r | |
1274 | //\r | |
1275 | // Adjust number of boot option for "BootOrder" variable.\r | |
1276 | //\r | |
1277 | Status = gRT->SetVariable (\r | |
1278 | L"BootOrder",\r | |
1279 | &gEfiGlobalVariableGuid,\r | |
1280 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r | |
1281 | BootOrderSize,\r | |
1282 | BootOrder\r | |
1283 | );\r | |
1284 | \r | |
1285 | FreePool (BootOrder);\r | |
1286 | \r | |
1287 | return Status;\r | |
1288 | }\r | |
1289 | \r | |
1290 | \r | |
1291 | /**\r | |
3384a9bc | 1292 | Delete all invalid EFI boot options.\r |
5c08e117 | 1293 | \r |
1294 | @retval EFI_SUCCESS Delete all invalid boot option success\r | |
1295 | @retval EFI_NOT_FOUND Variable "BootOrder" is not found\r | |
1296 | @retval EFI_OUT_OF_RESOURCES Lack of memory resource\r | |
1297 | @retval Other Error return value from SetVariable()\r | |
1298 | \r | |
1299 | **/\r | |
1300 | EFI_STATUS\r | |
1301 | BdsDeleteAllInvalidEfiBootOption (\r | |
1302 | VOID\r | |
1303 | )\r | |
1304 | {\r | |
1305 | UINT16 *BootOrder;\r | |
1306 | UINT8 *BootOptionVar;\r | |
1307 | UINTN BootOrderSize;\r | |
1308 | UINTN BootOptionSize;\r | |
1309 | EFI_STATUS Status;\r | |
1310 | UINTN Index;\r | |
1311 | UINTN Index2;\r | |
1312 | UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r | |
1313 | EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r | |
1314 | UINT8 *TempPtr;\r | |
3384a9bc | 1315 | CHAR16 *Description;\r |
8c08a567 | 1316 | BOOLEAN Corrupted;\r |
5c08e117 | 1317 | \r |
8c08a567 ED |
1318 | Status = EFI_SUCCESS;\r |
1319 | BootOrder = NULL;\r | |
1320 | Description = NULL;\r | |
1321 | OptionDevicePath = NULL;\r | |
1322 | BootOrderSize = 0;\r | |
1323 | Corrupted = FALSE;\r | |
5c08e117 | 1324 | \r |
1325 | //\r | |
1326 | // Check "BootOrder" variable firstly, this variable hold the number of boot options\r | |
1327 | //\r | |
1328 | BootOrder = BdsLibGetVariableAndSize (\r | |
1329 | L"BootOrder",\r | |
1330 | &gEfiGlobalVariableGuid,\r | |
1331 | &BootOrderSize\r | |
1332 | );\r | |
1333 | if (NULL == BootOrder) {\r | |
1334 | return EFI_NOT_FOUND;\r | |
1335 | }\r | |
1336 | \r | |
1337 | Index = 0;\r | |
1338 | while (Index < BootOrderSize / sizeof (UINT16)) {\r | |
1339 | UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r | |
1340 | BootOptionVar = BdsLibGetVariableAndSize (\r | |
1341 | BootOption,\r | |
1342 | &gEfiGlobalVariableGuid,\r | |
1343 | &BootOptionSize\r | |
1344 | );\r | |
1345 | if (NULL == BootOptionVar) {\r | |
1346 | FreePool (BootOrder);\r | |
1347 | return EFI_OUT_OF_RESOURCES;\r | |
1348 | }\r | |
1349 | \r | |
8c08a567 ED |
1350 | if (!ValidateOption(BootOptionVar, BootOptionSize)) {\r |
1351 | Corrupted = TRUE;\r | |
1352 | } else {\r | |
1353 | TempPtr = BootOptionVar;\r | |
1354 | TempPtr += sizeof (UINT32) + sizeof (UINT16);\r | |
1355 | Description = (CHAR16 *) TempPtr;\r | |
1356 | TempPtr += StrSize ((CHAR16 *) TempPtr);\r | |
1357 | OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r | |
5c08e117 | 1358 | \r |
8c08a567 ED |
1359 | //\r |
1360 | // Skip legacy boot option (BBS boot device)\r | |
1361 | //\r | |
1362 | if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&\r | |
1363 | (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {\r | |
1364 | FreePool (BootOptionVar);\r | |
1365 | Index++;\r | |
1366 | continue;\r | |
1367 | }\r | |
5c08e117 | 1368 | }\r |
1369 | \r | |
8c08a567 | 1370 | if (Corrupted || !BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath, FALSE, Description)) {\r |
5c08e117 | 1371 | //\r |
1372 | // Delete this invalid boot option "Boot####"\r | |
1373 | //\r | |
1374 | Status = gRT->SetVariable (\r | |
1375 | BootOption,\r | |
1376 | &gEfiGlobalVariableGuid,\r | |
1377 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r | |
1378 | 0,\r | |
1379 | NULL\r | |
1380 | );\r | |
1381 | //\r | |
1382 | // Mark this boot option in boot order as deleted\r | |
1383 | //\r | |
1384 | BootOrder[Index] = 0xffff;\r | |
8c08a567 | 1385 | Corrupted = FALSE;\r |
5c08e117 | 1386 | }\r |
1387 | \r | |
1388 | FreePool (BootOptionVar);\r | |
1389 | Index++;\r | |
1390 | }\r | |
1391 | \r | |
1392 | //\r | |
1393 | // Adjust boot order array\r | |
1394 | //\r | |
1395 | Index2 = 0;\r | |
1396 | for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {\r | |
1397 | if (BootOrder[Index] != 0xffff) {\r | |
1398 | BootOrder[Index2] = BootOrder[Index];\r | |
1399 | Index2 ++;\r | |
1400 | }\r | |
1401 | }\r | |
1402 | Status = gRT->SetVariable (\r | |
1403 | L"BootOrder",\r | |
1404 | &gEfiGlobalVariableGuid,\r | |
1405 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r | |
1406 | Index2 * sizeof (UINT16),\r | |
1407 | BootOrder\r | |
1408 | );\r | |
1409 | \r | |
1410 | FreePool (BootOrder);\r | |
1411 | \r | |
1412 | return Status;\r | |
1413 | }\r | |
1414 | \r | |
1415 | \r | |
1416 | /**\r | |
3384a9bc | 1417 | For EFI boot option, BDS separate them as six types:\r |
128efbbc | 1418 | 1. Network - The boot option points to the SimpleNetworkProtocol device.\r |
3384a9bc | 1419 | Bds will try to automatically create this type boot option when enumerate.\r |
128efbbc | 1420 | 2. Shell - The boot option points to internal flash shell.\r |
3384a9bc | 1421 | Bds will try to automatically create this type boot option when enumerate.\r |
1422 | 3. Removable BlockIo - The boot option only points to the removable media\r | |
1423 | device, like USB flash disk, DVD, Floppy etc.\r | |
1424 | These device should contain a *removable* blockIo\r | |
1425 | protocol in their device handle.\r | |
128efbbc | 1426 | Bds will try to automatically create this type boot option\r |
3384a9bc | 1427 | when enumerate.\r |
128efbbc | 1428 | 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,\r |
3384a9bc | 1429 | like HardDisk.\r |
1430 | These device should contain a *fixed* blockIo\r | |
1431 | protocol in their device handle.\r | |
1432 | BDS will skip fixed blockIo devices, and NOT\r | |
128efbbc | 1433 | automatically create boot option for them. But BDS\r |
1434 | will help to delete those fixed blockIo boot option,\r | |
3384a9bc | 1435 | whose description rule conflict with other auto-created\r |
1436 | boot options.\r | |
128efbbc | 1437 | 5. Non-BlockIo Simplefile - The boot option points to a device whose handle\r |
3384a9bc | 1438 | has SimpleFileSystem Protocol, but has no blockio\r |
1439 | protocol. These devices do not offer blockIo\r | |
128efbbc | 1440 | protocol, but BDS still can get the\r |
3384a9bc | 1441 | \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem\r |
1442 | Protocol.\r | |
128efbbc | 1443 | 6. File - The boot option points to a file. These boot options are usually\r |
3384a9bc | 1444 | created by user manually or OS loader. BDS will not delete or modify\r |
128efbbc | 1445 | these boot options.\r |
1446 | \r | |
3384a9bc | 1447 | This function will enumerate all possible boot device in the system, and\r |
128efbbc | 1448 | automatically create boot options for Network, Shell, Removable BlockIo,\r |
3384a9bc | 1449 | and Non-BlockIo Simplefile devices.\r |
8d3b5aff | 1450 | It will only execute once of every boot.\r |
128efbbc | 1451 | \r |
5c08e117 | 1452 | @param BdsBootOptionList The header of the link list which indexed all\r |
1453 | current boot options\r | |
1454 | \r | |
1455 | @retval EFI_SUCCESS Finished all the boot device enumerate and create\r | |
1456 | the boot option base on that boot device\r | |
1457 | \r | |
e83c9064 | 1458 | @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list\r |
5c08e117 | 1459 | **/\r |
1460 | EFI_STATUS\r | |
1461 | EFIAPI\r | |
1462 | BdsLibEnumerateAllBootOption (\r | |
1463 | IN OUT LIST_ENTRY *BdsBootOptionList\r | |
1464 | )\r | |
1465 | {\r | |
1466 | EFI_STATUS Status;\r | |
1467 | UINT16 FloppyNumber;\r | |
889a4bc2 | 1468 | UINT16 HarddriveNumber;\r |
5c08e117 | 1469 | UINT16 CdromNumber;\r |
1470 | UINT16 UsbNumber;\r | |
1471 | UINT16 MiscNumber;\r | |
8d3b5aff | 1472 | UINT16 ScsiNumber;\r |
5c08e117 | 1473 | UINT16 NonBlockNumber;\r |
1474 | UINTN NumberBlockIoHandles;\r | |
1475 | EFI_HANDLE *BlockIoHandles;\r | |
1476 | EFI_BLOCK_IO_PROTOCOL *BlkIo;\r | |
35bc0e9f RN |
1477 | BOOLEAN Removable[2];\r |
1478 | UINTN RemovableIndex;\r | |
5c08e117 | 1479 | UINTN Index;\r |
a7a523e0 | 1480 | UINTN NumOfLoadFileHandles;\r |
1481 | EFI_HANDLE *LoadFileHandles;\r | |
5c08e117 | 1482 | UINTN FvHandleCount;\r |
1483 | EFI_HANDLE *FvHandleBuffer;\r | |
1484 | EFI_FV_FILETYPE Type;\r | |
1485 | UINTN Size;\r | |
1486 | EFI_FV_FILE_ATTRIBUTES Attributes;\r | |
1487 | UINT32 AuthenticationStatus;\r | |
8d3b5aff | 1488 | EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r |
1489 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
5c08e117 | 1490 | UINTN DevicePathType;\r |
1491 | CHAR16 Buffer[40];\r | |
1492 | EFI_HANDLE *FileSystemHandles;\r | |
1493 | UINTN NumberFileSystemHandles;\r | |
1494 | BOOLEAN NeedDelete;\r | |
1495 | EFI_IMAGE_DOS_HEADER DosHeader;\r | |
9aa7ba01 | 1496 | CHAR8 *PlatLang;\r |
1497 | CHAR8 *LastLang;\r | |
5c08e117 | 1498 | EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r |
1499 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
1500 | \r | |
889a4bc2 RN |
1501 | FloppyNumber = 0;\r |
1502 | HarddriveNumber = 0;\r | |
1503 | CdromNumber = 0;\r | |
1504 | UsbNumber = 0;\r | |
1505 | MiscNumber = 0;\r | |
1506 | ScsiNumber = 0;\r | |
1507 | PlatLang = NULL;\r | |
1508 | LastLang = NULL;\r | |
5c08e117 | 1509 | ZeroMem (Buffer, sizeof (Buffer));\r |
128efbbc | 1510 | \r |
5c08e117 | 1511 | //\r |
1512 | // If the boot device enumerate happened, just get the boot\r | |
1513 | // device from the boot order variable\r | |
1514 | //\r | |
1515 | if (mEnumBootDevice) {\r | |
bf4a3dbd ED |
1516 | GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME, &gLastEnumLangGuid, &LastLang, NULL);\r |
1517 | GetEfiGlobalVariable2 (L"PlatformLang", &PlatLang, NULL);\r | |
0fa3ac1b RN |
1518 | ASSERT (PlatLang != NULL);\r |
1519 | if ((LastLang != NULL) && (AsciiStrCmp (LastLang, PlatLang) == 0)) {\r | |
9aa7ba01 | 1520 | Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r |
0fa3ac1b RN |
1521 | FreePool (LastLang);\r |
1522 | FreePool (PlatLang);\r | |
9aa7ba01 | 1523 | return Status;\r |
1524 | } else {\r | |
1525 | Status = gRT->SetVariable (\r | |
e24fc103 LG |
1526 | LAST_ENUM_LANGUAGE_VARIABLE_NAME,\r |
1527 | &gLastEnumLangGuid,\r | |
9aa7ba01 | 1528 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r |
0fa3ac1b | 1529 | AsciiStrSize (PlatLang),\r |
9aa7ba01 | 1530 | PlatLang\r |
1531 | );\r | |
1532 | ASSERT_EFI_ERROR (Status);\r | |
0fa3ac1b RN |
1533 | \r |
1534 | if (LastLang != NULL) {\r | |
1535 | FreePool (LastLang);\r | |
1536 | }\r | |
1537 | FreePool (PlatLang);\r | |
9aa7ba01 | 1538 | }\r |
5c08e117 | 1539 | }\r |
128efbbc | 1540 | \r |
5c08e117 | 1541 | //\r |
1542 | // Notes: this dirty code is to get the legacy boot option from the\r | |
1543 | // BBS table and create to variable as the EFI boot option, it should\r | |
1544 | // be removed after the CSM can provide legacy boot option directly\r | |
1545 | //\r | |
1546 | REFRESH_LEGACY_BOOT_OPTIONS;\r | |
1547 | \r | |
1548 | //\r | |
1549 | // Delete invalid boot option\r | |
1550 | //\r | |
1551 | BdsDeleteAllInvalidEfiBootOption ();\r | |
128efbbc | 1552 | \r |
5c08e117 | 1553 | //\r |
701e17e5 RN |
1554 | // Parse removable media followed by fixed media.\r |
1555 | // The Removable[] array is used by the for-loop below to create removable media boot options \r | |
1556 | // at first, and then to create fixed media boot options.\r | |
5c08e117 | 1557 | //\r |
701e17e5 RN |
1558 | Removable[0] = FALSE;\r |
1559 | Removable[1] = TRUE;\r | |
1560 | \r | |
5c08e117 | 1561 | gBS->LocateHandleBuffer (\r |
1562 | ByProtocol,\r | |
1563 | &gEfiBlockIoProtocolGuid,\r | |
1564 | NULL,\r | |
1565 | &NumberBlockIoHandles,\r | |
1566 | &BlockIoHandles\r | |
1567 | );\r | |
128efbbc | 1568 | \r |
35bc0e9f RN |
1569 | for (RemovableIndex = 0; RemovableIndex < 2; RemovableIndex++) {\r |
1570 | for (Index = 0; Index < NumberBlockIoHandles; Index++) {\r | |
1571 | Status = gBS->HandleProtocol (\r | |
1572 | BlockIoHandles[Index],\r | |
1573 | &gEfiBlockIoProtocolGuid,\r | |
1574 | (VOID **) &BlkIo\r | |
1575 | );\r | |
1576 | //\r | |
1577 | // skip the fixed block io then the removable block io\r | |
1578 | //\r | |
1579 | if (EFI_ERROR (Status) || (BlkIo->Media->RemovableMedia == Removable[RemovableIndex])) {\r | |
5c08e117 | 1580 | continue;\r |
1581 | }\r | |
35bc0e9f RN |
1582 | DevicePath = DevicePathFromHandle (BlockIoHandles[Index]);\r |
1583 | DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);\r | |
5c08e117 | 1584 | \r |
35bc0e9f RN |
1585 | switch (DevicePathType) {\r |
1586 | case BDS_EFI_ACPI_FLOPPY_BOOT:\r | |
1587 | if (FloppyNumber != 0) {\r | |
1588 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)), FloppyNumber);\r | |
1589 | } else {\r | |
1590 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)));\r | |
1591 | }\r | |
1592 | BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r | |
1593 | FloppyNumber++;\r | |
1594 | break;\r | |
128efbbc | 1595 | \r |
35bc0e9f | 1596 | //\r |
889a4bc2 | 1597 | // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.\r |
35bc0e9f RN |
1598 | //\r |
1599 | case BDS_EFI_MESSAGE_ATAPI_BOOT:\r | |
1600 | case BDS_EFI_MESSAGE_SATA_BOOT:\r | |
889a4bc2 RN |
1601 | if (BlkIo->Media->RemovableMedia) {\r |
1602 | if (CdromNumber != 0) {\r | |
1603 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)), CdromNumber);\r | |
1604 | } else {\r | |
1605 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)));\r | |
1606 | }\r | |
1607 | CdromNumber++;\r | |
35bc0e9f | 1608 | } else {\r |
889a4bc2 RN |
1609 | if (HarddriveNumber != 0) {\r |
1610 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)), HarddriveNumber);\r | |
1611 | } else {\r | |
1612 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)));\r | |
1613 | }\r | |
1614 | HarddriveNumber++;\r | |
35bc0e9f RN |
1615 | }\r |
1616 | DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Buffer: %S\n", Buffer));\r | |
1617 | BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r | |
35bc0e9f RN |
1618 | break;\r |
1619 | \r | |
1620 | case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:\r | |
1621 | if (UsbNumber != 0) {\r | |
1622 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)), UsbNumber);\r | |
1623 | } else {\r | |
1624 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)));\r | |
1625 | }\r | |
1626 | BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r | |
1627 | UsbNumber++;\r | |
1628 | break;\r | |
5c08e117 | 1629 | \r |
35bc0e9f RN |
1630 | case BDS_EFI_MESSAGE_SCSI_BOOT:\r |
1631 | if (ScsiNumber != 0) {\r | |
1632 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)), ScsiNumber);\r | |
1633 | } else {\r | |
1634 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)));\r | |
1635 | }\r | |
1636 | BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r | |
1637 | ScsiNumber++;\r | |
1638 | break;\r | |
5c08e117 | 1639 | \r |
35bc0e9f RN |
1640 | case BDS_EFI_MESSAGE_MISC_BOOT:\r |
1641 | if (MiscNumber != 0) {\r | |
1642 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)), MiscNumber);\r | |
1643 | } else {\r | |
1644 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)));\r | |
1645 | }\r | |
1646 | BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r | |
1647 | MiscNumber++;\r | |
1648 | break;\r | |
5c08e117 | 1649 | \r |
35bc0e9f RN |
1650 | default:\r |
1651 | break;\r | |
9aa7ba01 | 1652 | }\r |
5c08e117 | 1653 | }\r |
1654 | }\r | |
1655 | \r | |
1656 | if (NumberBlockIoHandles != 0) {\r | |
1657 | FreePool (BlockIoHandles);\r | |
1658 | }\r | |
1659 | \r | |
1660 | //\r | |
1661 | // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.\r | |
1662 | //\r | |
1663 | NonBlockNumber = 0;\r | |
1664 | gBS->LocateHandleBuffer (\r | |
1665 | ByProtocol,\r | |
1666 | &gEfiSimpleFileSystemProtocolGuid,\r | |
1667 | NULL,\r | |
1668 | &NumberFileSystemHandles,\r | |
1669 | &FileSystemHandles\r | |
1670 | );\r | |
1671 | for (Index = 0; Index < NumberFileSystemHandles; Index++) {\r | |
1672 | Status = gBS->HandleProtocol (\r | |
1673 | FileSystemHandles[Index],\r | |
1674 | &gEfiBlockIoProtocolGuid,\r | |
1675 | (VOID **) &BlkIo\r | |
1676 | );\r | |
1677 | if (!EFI_ERROR (Status)) {\r | |
1678 | //\r | |
1679 | // Skip if the file system handle supports a BlkIo protocol,\r | |
1680 | //\r | |
1681 | continue;\r | |
1682 | }\r | |
1683 | \r | |
1684 | //\r | |
1685 | // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI\r | |
1686 | // machinename is ia32, ia64, x64, ...\r | |
1687 | //\r | |
35bc0e9f | 1688 | Hdr.Union = &HdrData;\r |
5c08e117 | 1689 | NeedDelete = TRUE;\r |
1690 | Status = BdsLibGetImageHeader (\r | |
1691 | FileSystemHandles[Index],\r | |
c62dbf31 | 1692 | EFI_REMOVABLE_MEDIA_FILE_NAME,\r |
5c08e117 | 1693 | &DosHeader,\r |
1694 | Hdr\r | |
1695 | );\r | |
1696 | if (!EFI_ERROR (Status) &&\r | |
1697 | EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r | |
1698 | Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r | |
1699 | NeedDelete = FALSE;\r | |
1700 | }\r | |
1701 | \r | |
1702 | if (NeedDelete) {\r | |
1703 | //\r | |
1704 | // No such file or the file is not a EFI application, delete this boot option\r | |
1705 | //\r | |
1706 | BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);\r | |
1707 | } else {\r | |
9aa7ba01 | 1708 | if (NonBlockNumber != 0) {\r |
1709 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)), NonBlockNumber);\r | |
1710 | } else {\r | |
1711 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)));\r | |
1712 | }\r | |
5c08e117 | 1713 | BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList, Buffer);\r |
1714 | NonBlockNumber++;\r | |
1715 | }\r | |
1716 | }\r | |
1717 | \r | |
1718 | if (NumberFileSystemHandles != 0) {\r | |
1719 | FreePool (FileSystemHandles);\r | |
1720 | }\r | |
1721 | \r | |
1722 | //\r | |
1723 | // Parse Network Boot Device\r | |
1724 | //\r | |
a7a523e0 | 1725 | NumOfLoadFileHandles = 0;\r |
ff482c56 | 1726 | //\r |
a7a523e0 | 1727 | // Search Load File protocol for PXE boot option.\r |
ff482c56 | 1728 | //\r |
5c08e117 | 1729 | gBS->LocateHandleBuffer (\r |
1730 | ByProtocol,\r | |
a7a523e0 | 1731 | &gEfiLoadFileProtocolGuid,\r |
5c08e117 | 1732 | NULL,\r |
a7a523e0 | 1733 | &NumOfLoadFileHandles,\r |
1734 | &LoadFileHandles\r | |
5c08e117 | 1735 | );\r |
8d3b5aff | 1736 | \r |
a7a523e0 | 1737 | for (Index = 0; Index < NumOfLoadFileHandles; Index++) {\r |
9aa7ba01 | 1738 | if (Index != 0) {\r |
1739 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)), Index);\r | |
1740 | } else {\r | |
1741 | UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)));\r | |
1742 | }\r | |
a7a523e0 | 1743 | BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);\r |
5c08e117 | 1744 | }\r |
1745 | \r | |
a7a523e0 | 1746 | if (NumOfLoadFileHandles != 0) {\r |
1747 | FreePool (LoadFileHandles);\r | |
5c08e117 | 1748 | }\r |
1749 | \r | |
1750 | //\r | |
1751 | // Check if we have on flash shell\r | |
1752 | //\r | |
1753 | gBS->LocateHandleBuffer (\r | |
1754 | ByProtocol,\r | |
1755 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
1756 | NULL,\r | |
1757 | &FvHandleCount,\r | |
1758 | &FvHandleBuffer\r | |
1759 | );\r | |
1760 | for (Index = 0; Index < FvHandleCount; Index++) {\r | |
5c08e117 | 1761 | gBS->HandleProtocol (\r |
1762 | FvHandleBuffer[Index],\r | |
1763 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
1764 | (VOID **) &Fv\r | |
1765 | );\r | |
1766 | \r | |
1767 | Status = Fv->ReadFile (\r | |
1768 | Fv,\r | |
d46f3632 | 1769 | PcdGetPtr(PcdShellFile),\r |
5c08e117 | 1770 | NULL,\r |
1771 | &Size,\r | |
1772 | &Type,\r | |
1773 | &Attributes,\r | |
1774 | &AuthenticationStatus\r | |
1775 | );\r | |
1776 | if (EFI_ERROR (Status)) {\r | |
1777 | //\r | |
1778 | // Skip if no shell file in the FV\r | |
1779 | //\r | |
1780 | continue;\r | |
1781 | }\r | |
1782 | //\r | |
1783 | // Build the shell boot option\r | |
1784 | //\r | |
1785 | BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);\r | |
1786 | }\r | |
1787 | \r | |
1788 | if (FvHandleCount != 0) {\r | |
1789 | FreePool (FvHandleBuffer);\r | |
1790 | }\r | |
1791 | //\r | |
1792 | // Make sure every boot only have one time\r | |
1793 | // boot device enumerate\r | |
1794 | //\r | |
e83c9064 | 1795 | Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r |
5c08e117 | 1796 | mEnumBootDevice = TRUE;\r |
1797 | \r | |
e83c9064 | 1798 | return Status;\r |
5c08e117 | 1799 | }\r |
1800 | \r | |
1801 | /**\r | |
1802 | Build the boot option with the handle parsed in\r | |
1803 | \r | |
1804 | @param Handle The handle which present the device path to create\r | |
1805 | boot option\r | |
1806 | @param BdsBootOptionList The header of the link list which indexed all\r | |
1807 | current boot options\r | |
1808 | @param String The description of the boot option.\r | |
1809 | \r | |
1810 | **/\r | |
1811 | VOID\r | |
1812 | EFIAPI\r | |
1813 | BdsLibBuildOptionFromHandle (\r | |
1814 | IN EFI_HANDLE Handle,\r | |
1815 | IN LIST_ENTRY *BdsBootOptionList,\r | |
1816 | IN CHAR16 *String\r | |
1817 | )\r | |
1818 | {\r | |
1819 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
128efbbc | 1820 | \r |
8d3b5aff | 1821 | DevicePath = DevicePathFromHandle (Handle);\r |
5c08e117 | 1822 | \r |
1823 | //\r | |
1824 | // Create and register new boot option\r | |
1825 | //\r | |
1826 | BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");\r | |
1827 | }\r | |
1828 | \r | |
1829 | \r | |
1830 | /**\r | |
1831 | Build the on flash shell boot option with the handle parsed in.\r | |
1832 | \r | |
1833 | @param Handle The handle which present the device path to create\r | |
1834 | on flash shell boot option\r | |
1835 | @param BdsBootOptionList The header of the link list which indexed all\r | |
1836 | current boot options\r | |
1837 | \r | |
1838 | **/\r | |
1839 | VOID\r | |
1840 | EFIAPI\r | |
1841 | BdsLibBuildOptionFromShell (\r | |
1842 | IN EFI_HANDLE Handle,\r | |
1843 | IN OUT LIST_ENTRY *BdsBootOptionList\r | |
1844 | )\r | |
1845 | {\r | |
1846 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
1847 | MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;\r | |
1848 | \r | |
1849 | DevicePath = DevicePathFromHandle (Handle);\r | |
1850 | \r | |
1851 | //\r | |
1852 | // Build the shell device path\r | |
1853 | //\r | |
d46f3632 | 1854 | EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));\r |
5c08e117 | 1855 | \r |
1856 | DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);\r | |
1857 | \r | |
1858 | //\r | |
1859 | // Create and register the shell boot option\r | |
1860 | //\r | |
1861 | BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");\r | |
1862 | \r | |
1863 | }\r | |
1864 | \r | |
1865 | /**\r | |
1866 | Boot from the UEFI spec defined "BootNext" variable.\r | |
1867 | \r | |
1868 | **/\r | |
1869 | VOID\r | |
1870 | EFIAPI\r | |
1871 | BdsLibBootNext (\r | |
1872 | VOID\r | |
1873 | )\r | |
1874 | {\r | |
1875 | UINT16 *BootNext;\r | |
1876 | UINTN BootNextSize;\r | |
1877 | CHAR16 Buffer[20];\r | |
1878 | BDS_COMMON_OPTION *BootOption;\r | |
1879 | LIST_ENTRY TempList;\r | |
1880 | UINTN ExitDataSize;\r | |
1881 | CHAR16 *ExitData;\r | |
1882 | \r | |
1883 | //\r | |
1884 | // Init the boot option name buffer and temp link list\r | |
1885 | //\r | |
1886 | InitializeListHead (&TempList);\r | |
1887 | ZeroMem (Buffer, sizeof (Buffer));\r | |
1888 | \r | |
1889 | BootNext = BdsLibGetVariableAndSize (\r | |
1890 | L"BootNext",\r | |
1891 | &gEfiGlobalVariableGuid,\r | |
1892 | &BootNextSize\r | |
1893 | );\r | |
1894 | \r | |
1895 | //\r | |
1896 | // Clear the boot next variable first\r | |
1897 | //\r | |
1898 | if (BootNext != NULL) {\r | |
1899 | gRT->SetVariable (\r | |
1900 | L"BootNext",\r | |
1901 | &gEfiGlobalVariableGuid,\r | |
1902 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r | |
1903 | 0,\r | |
1904 | BootNext\r | |
1905 | );\r | |
1906 | \r | |
1907 | //\r | |
1908 | // Start to build the boot option and try to boot\r | |
1909 | //\r | |
1910 | UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);\r | |
1911 | BootOption = BdsLibVariableToOption (&TempList, Buffer);\r | |
1912 | ASSERT (BootOption != NULL);\r | |
1913 | BdsLibConnectDevicePath (BootOption->DevicePath);\r | |
1914 | BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);\r | |
1915 | }\r | |
1916 | \r | |
1917 | }\r | |
1918 | \r | |
1919 | /**\r | |
1920 | Return the bootable media handle.\r | |
1921 | First, check the device is connected\r | |
1922 | Second, check whether the device path point to a device which support SimpleFileSystemProtocol,\r | |
1923 | Third, detect the the default boot file in the Media, and return the removable Media handle.\r | |
1924 | \r | |
e83c9064 | 1925 | @param DevicePath Device Path to a bootable device\r |
5c08e117 | 1926 | \r |
e83c9064 | 1927 | @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.\r |
5c08e117 | 1928 | \r |
1929 | **/\r | |
1930 | EFI_HANDLE\r | |
1931 | EFIAPI\r | |
1932 | BdsLibGetBootableHandle (\r | |
1933 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r | |
1934 | )\r | |
1935 | {\r | |
1936 | EFI_STATUS Status;\r | |
ef949581 | 1937 | EFI_TPL OldTpl;\r |
5c08e117 | 1938 | EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r |
1939 | EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;\r | |
1940 | EFI_HANDLE Handle;\r | |
1941 | EFI_BLOCK_IO_PROTOCOL *BlockIo;\r | |
1942 | VOID *Buffer;\r | |
1943 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r | |
1944 | UINTN Size;\r | |
1945 | UINTN TempSize;\r | |
1946 | EFI_HANDLE ReturnHandle;\r | |
1947 | EFI_HANDLE *SimpleFileSystemHandles;\r | |
1948 | \r | |
1949 | UINTN NumberSimpleFileSystemHandles;\r | |
1950 | UINTN Index;\r | |
1951 | EFI_IMAGE_DOS_HEADER DosHeader;\r | |
1952 | EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r | |
1953 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
1954 | \r | |
1955 | UpdatedDevicePath = DevicePath;\r | |
128efbbc | 1956 | \r |
ef949581 RN |
1957 | //\r |
1958 | // Enter to critical section to protect the acquired BlockIo instance \r | |
1959 | // from getting released due to the USB mass storage hotplug event\r | |
1960 | //\r | |
1961 | OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r | |
1962 | \r | |
5c08e117 | 1963 | //\r |
1964 | // Check whether the device is connected\r | |
1965 | //\r | |
1966 | Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);\r | |
1967 | if (EFI_ERROR (Status)) {\r | |
1968 | //\r | |
1969 | // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,\r | |
1970 | //\r | |
1971 | Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);\r | |
1972 | if (EFI_ERROR (Status)) {\r | |
1973 | //\r | |
1974 | // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly\r | |
1975 | //\r | |
1976 | UpdatedDevicePath = DevicePath;\r | |
1977 | Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r | |
1978 | gBS->ConnectController (Handle, NULL, NULL, TRUE);\r | |
1979 | }\r | |
1980 | } else {\r | |
e74f510b RN |
1981 | //\r |
1982 | // For removable device boot option, its contained device path only point to the removable device handle, \r | |
1983 | // should make sure all its children handles (its child partion or media handles) are created and connected. \r | |
1984 | //\r | |
1985 | gBS->ConnectController (Handle, NULL, NULL, TRUE); \r | |
5c08e117 | 1986 | //\r |
1987 | // Get BlockIo protocol and check removable attribute\r | |
1988 | //\r | |
1989 | Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r | |
ef949581 RN |
1990 | ASSERT_EFI_ERROR (Status);\r |
1991 | \r | |
5c08e117 | 1992 | //\r |
1993 | // Issue a dummy read to the device to check for media change.\r | |
1994 | // When the removable media is changed, any Block IO read/write will\r | |
1995 | // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is\r | |
1996 | // returned. After the Block IO protocol is reinstalled, subsequent\r | |
1997 | // Block IO read/write will success.\r | |
1998 | //\r | |
1999 | Buffer = AllocatePool (BlockIo->Media->BlockSize);\r | |
2000 | if (Buffer != NULL) {\r | |
2001 | BlockIo->ReadBlocks (\r | |
2002 | BlockIo,\r | |
2003 | BlockIo->Media->MediaId,\r | |
2004 | 0,\r | |
2005 | BlockIo->Media->BlockSize,\r | |
2006 | Buffer\r | |
2007 | );\r | |
2008 | FreePool(Buffer);\r | |
2009 | }\r | |
2010 | }\r | |
2011 | \r | |
2012 | //\r | |
2013 | // Detect the the default boot file from removable Media\r | |
2014 | //\r | |
2015 | \r | |
2016 | //\r | |
2017 | // If fail to get bootable handle specified by a USB boot option, the BDS should try to find other bootable device in the same USB bus\r | |
2018 | // Try to locate the USB node device path first, if fail then use its previous PCI node to search\r | |
2019 | //\r | |
2020 | DupDevicePath = DuplicateDevicePath (DevicePath);\r | |
2021 | ASSERT (DupDevicePath != NULL);\r | |
128efbbc | 2022 | \r |
5c08e117 | 2023 | UpdatedDevicePath = DupDevicePath;\r |
2024 | Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r | |
2025 | //\r | |
2026 | // if the resulting device path point to a usb node, and the usb node is a dummy node, should only let device path only point to the previous Pci node\r | |
2027 | // Acpi()/Pci()/Usb() --> Acpi()/Pci()\r | |
2028 | //\r | |
2029 | if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&\r | |
2030 | (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {\r | |
2031 | //\r | |
2032 | // Remove the usb node, let the device path only point to PCI node\r | |
2033 | //\r | |
2034 | SetDevicePathEndNode (UpdatedDevicePath);\r | |
2035 | UpdatedDevicePath = DupDevicePath;\r | |
2036 | } else {\r | |
2037 | UpdatedDevicePath = DevicePath;\r | |
2038 | }\r | |
2039 | \r | |
2040 | //\r | |
2041 | // Get the device path size of boot option\r | |
2042 | //\r | |
2043 | Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r | |
2044 | ReturnHandle = NULL;\r | |
2045 | gBS->LocateHandleBuffer (\r | |
2046 | ByProtocol,\r | |
2047 | &gEfiSimpleFileSystemProtocolGuid,\r | |
2048 | NULL,\r | |
2049 | &NumberSimpleFileSystemHandles,\r | |
2050 | &SimpleFileSystemHandles\r | |
2051 | );\r | |
2052 | for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {\r | |
2053 | //\r | |
2054 | // Get the device path size of SimpleFileSystem handle\r | |
2055 | //\r | |
2056 | TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);\r | |
2057 | TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r | |
2058 | //\r | |
2059 | // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path\r | |
2060 | //\r | |
2061 | if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {\r | |
2062 | //\r | |
2063 | // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r | |
2064 | // machinename is ia32, ia64, x64, ...\r | |
2065 | //\r | |
2066 | Hdr.Union = &HdrData;\r | |
2067 | Status = BdsLibGetImageHeader (\r | |
2068 | SimpleFileSystemHandles[Index],\r | |
c62dbf31 | 2069 | EFI_REMOVABLE_MEDIA_FILE_NAME,\r |
5c08e117 | 2070 | &DosHeader,\r |
2071 | Hdr\r | |
2072 | );\r | |
2073 | if (!EFI_ERROR (Status) &&\r | |
2074 | EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r | |
2075 | Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r | |
2076 | ReturnHandle = SimpleFileSystemHandles[Index];\r | |
2077 | break;\r | |
2078 | }\r | |
2079 | }\r | |
2080 | }\r | |
2081 | \r | |
2082 | FreePool(DupDevicePath);\r | |
2083 | \r | |
2084 | if (SimpleFileSystemHandles != NULL) {\r | |
2085 | FreePool(SimpleFileSystemHandles);\r | |
2086 | }\r | |
2087 | \r | |
ef949581 RN |
2088 | gBS->RestoreTPL (OldTpl);\r |
2089 | \r | |
5c08e117 | 2090 | return ReturnHandle;\r |
2091 | }\r | |
2092 | \r | |
2093 | /**\r | |
2094 | Check to see if the network cable is plugged in. If the DevicePath is not\r | |
2095 | connected it will be connected.\r | |
2096 | \r | |
2097 | @param DevicePath Device Path to check\r | |
2098 | \r | |
2099 | @retval TRUE DevicePath points to an Network that is connected\r | |
2100 | @retval FALSE DevicePath does not point to a bootable network\r | |
2101 | \r | |
2102 | **/\r | |
2103 | BOOLEAN\r | |
2104 | BdsLibNetworkBootWithMediaPresent (\r | |
2105 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r | |
2106 | )\r | |
2107 | {\r | |
2108 | EFI_STATUS Status;\r | |
2109 | EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r | |
2110 | EFI_HANDLE Handle;\r | |
2111 | EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r | |
2112 | BOOLEAN MediaPresent;\r | |
e51e619e | 2113 | UINT32 InterruptStatus;\r |
5c08e117 | 2114 | \r |
2115 | MediaPresent = FALSE;\r | |
2116 | \r | |
2117 | UpdatedDevicePath = DevicePath;\r | |
ff482c56 | 2118 | //\r |
a7a523e0 | 2119 | // Locate Load File Protocol for PXE boot option first\r |
ff482c56 | 2120 | //\r |
a7a523e0 | 2121 | Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r |
5c08e117 | 2122 | if (EFI_ERROR (Status)) {\r |
2123 | //\r | |
2124 | // Device not present so see if we need to connect it\r | |
2125 | //\r | |
2126 | Status = BdsLibConnectDevicePath (DevicePath);\r | |
2127 | if (!EFI_ERROR (Status)) {\r | |
2128 | //\r | |
2129 | // This one should work after we did the connect\r | |
2130 | //\r | |
a7a523e0 | 2131 | Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r |
5c08e117 | 2132 | }\r |
2133 | }\r | |
2134 | \r | |
2135 | if (!EFI_ERROR (Status)) {\r | |
2136 | Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);\r | |
ff482c56 | 2137 | if (EFI_ERROR (Status)) {\r |
2138 | //\r | |
2139 | // Failed to open SNP from this handle, try to get SNP from parent handle\r | |
2140 | //\r | |
2141 | UpdatedDevicePath = DevicePathFromHandle (Handle);\r | |
2142 | if (UpdatedDevicePath != NULL) {\r | |
2143 | Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);\r | |
2144 | if (!EFI_ERROR (Status)) {\r | |
2145 | //\r | |
2146 | // SNP handle found, get SNP from it\r | |
2147 | //\r | |
2148 | Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &Snp);\r | |
2149 | }\r | |
2150 | }\r | |
2151 | }\r | |
2152 | \r | |
5c08e117 | 2153 | if (!EFI_ERROR (Status)) {\r |
2154 | if (Snp->Mode->MediaPresentSupported) {\r | |
2155 | if (Snp->Mode->State == EfiSimpleNetworkInitialized) {\r | |
e51e619e | 2156 | //\r |
2157 | // Invoke Snp->GetStatus() to refresh the media status\r | |
2158 | //\r | |
2159 | Snp->GetStatus (Snp, &InterruptStatus, NULL);\r | |
2160 | \r | |
5c08e117 | 2161 | //\r |
2162 | // In case some one else is using the SNP check to see if it's connected\r | |
2163 | //\r | |
2164 | MediaPresent = Snp->Mode->MediaPresent;\r | |
2165 | } else {\r | |
2166 | //\r | |
2167 | // No one is using SNP so we need to Start and Initialize so\r | |
2168 | // MediaPresent will be valid.\r | |
2169 | //\r | |
2170 | Status = Snp->Start (Snp);\r | |
2171 | if (!EFI_ERROR (Status)) {\r | |
2172 | Status = Snp->Initialize (Snp, 0, 0);\r | |
2173 | if (!EFI_ERROR (Status)) {\r | |
2174 | MediaPresent = Snp->Mode->MediaPresent;\r | |
2175 | Snp->Shutdown (Snp);\r | |
2176 | }\r | |
2177 | Snp->Stop (Snp);\r | |
2178 | }\r | |
2179 | }\r | |
2180 | } else {\r | |
2181 | MediaPresent = TRUE;\r | |
2182 | }\r | |
2183 | }\r | |
2184 | }\r | |
2185 | \r | |
2186 | return MediaPresent;\r | |
2187 | }\r | |
2188 | \r | |
2189 | /**\r | |
2190 | For a bootable Device path, return its boot type.\r | |
2191 | \r | |
2192 | @param DevicePath The bootable device Path to check\r | |
2193 | \r | |
128efbbc | 2194 | @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r |
11c5022d | 2195 | which subtype is MEDIA_HARDDRIVE_DP\r |
2196 | @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r | |
2197 | which subtype is MEDIA_CDROM_DP\r | |
2198 | @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node\r | |
2199 | which HID is floppy device.\r | |
2200 | @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r | |
2201 | and its last device path node's subtype is MSG_ATAPI_DP.\r | |
2202 | @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r | |
2203 | and its last device path node's subtype is MSG_SCSI_DP.\r | |
2204 | @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r | |
2205 | and its last device path node's subtype is MSG_USB_DP.\r | |
5c08e117 | 2206 | @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and\r |
11c5022d | 2207 | its last device path node point to a message device path node.\r |
2208 | @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.\r | |
128efbbc | 2209 | @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,\r |
5c08e117 | 2210 | \r |
2211 | **/\r | |
2212 | UINT32\r | |
2213 | EFIAPI\r | |
2214 | BdsGetBootTypeFromDevicePath (\r | |
2215 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r | |
2216 | )\r | |
2217 | {\r | |
2218 | ACPI_HID_DEVICE_PATH *Acpi;\r | |
2219 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r | |
2220 | EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r | |
ff482c56 | 2221 | UINT32 BootType;\r |
5c08e117 | 2222 | \r |
2223 | if (NULL == DevicePath) {\r | |
2224 | return BDS_EFI_UNSUPPORT;\r | |
2225 | }\r | |
2226 | \r | |
2227 | TempDevicePath = DevicePath;\r | |
2228 | \r | |
2229 | while (!IsDevicePathEndType (TempDevicePath)) {\r | |
2230 | switch (DevicePathType (TempDevicePath)) {\r | |
2231 | case BBS_DEVICE_PATH:\r | |
2232 | return BDS_LEGACY_BBS_BOOT;\r | |
2233 | case MEDIA_DEVICE_PATH:\r | |
2234 | if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {\r | |
2235 | return BDS_EFI_MEDIA_HD_BOOT;\r | |
2236 | } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {\r | |
2237 | return BDS_EFI_MEDIA_CDROM_BOOT;\r | |
128efbbc | 2238 | }\r |
5c08e117 | 2239 | break;\r |
2240 | case ACPI_DEVICE_PATH:\r | |
2241 | Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;\r | |
2242 | if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {\r | |
2243 | return BDS_EFI_ACPI_FLOPPY_BOOT;\r | |
2244 | }\r | |
2245 | break;\r | |
2246 | case MESSAGING_DEVICE_PATH:\r | |
2247 | //\r | |
2248 | // Get the last device path node\r | |
2249 | //\r | |
2250 | LastDeviceNode = NextDevicePathNode (TempDevicePath);\r | |
2251 | if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {\r | |
2252 | //\r | |
2253 | // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),\r | |
ff482c56 | 2254 | // skip it\r |
5c08e117 | 2255 | //\r |
2256 | LastDeviceNode = NextDevicePathNode (LastDeviceNode);\r | |
2257 | }\r | |
2258 | //\r | |
2259 | // if the device path not only point to driver device, it is not a messaging device path,\r | |
2260 | //\r | |
2261 | if (!IsDevicePathEndType (LastDeviceNode)) {\r | |
128efbbc | 2262 | break;\r |
5c08e117 | 2263 | }\r |
2264 | \r | |
ff482c56 | 2265 | switch (DevicePathSubType (TempDevicePath)) {\r |
2266 | case MSG_ATAPI_DP:\r | |
2267 | BootType = BDS_EFI_MESSAGE_ATAPI_BOOT;\r | |
2268 | break;\r | |
2269 | \r | |
2270 | case MSG_USB_DP:\r | |
2271 | BootType = BDS_EFI_MESSAGE_USB_DEVICE_BOOT;\r | |
2272 | break;\r | |
2273 | \r | |
2274 | case MSG_SCSI_DP:\r | |
2275 | BootType = BDS_EFI_MESSAGE_SCSI_BOOT;\r | |
2276 | break;\r | |
2277 | \r | |
2278 | case MSG_SATA_DP:\r | |
2279 | BootType = BDS_EFI_MESSAGE_SATA_BOOT;\r | |
2280 | break;\r | |
2281 | \r | |
2282 | case MSG_MAC_ADDR_DP:\r | |
2283 | case MSG_VLAN_DP:\r | |
a7a523e0 | 2284 | case MSG_IPv4_DP:\r |
2285 | case MSG_IPv6_DP:\r | |
ff482c56 | 2286 | BootType = BDS_EFI_MESSAGE_MAC_BOOT;\r |
2287 | break;\r | |
2288 | \r | |
2289 | default:\r | |
2290 | BootType = BDS_EFI_MESSAGE_MISC_BOOT;\r | |
2291 | break;\r | |
5c08e117 | 2292 | }\r |
ff482c56 | 2293 | return BootType;\r |
2294 | \r | |
5c08e117 | 2295 | default:\r |
2296 | break;\r | |
2297 | }\r | |
2298 | TempDevicePath = NextDevicePathNode (TempDevicePath);\r | |
2299 | }\r | |
2300 | \r | |
2301 | return BDS_EFI_UNSUPPORT;\r | |
2302 | }\r | |
2303 | \r | |
2304 | /**\r | |
2305 | Check whether the Device path in a boot option point to a valid bootable device,\r | |
2306 | And if CheckMedia is true, check the device is ready to boot now.\r | |
2307 | \r | |
2308 | @param DevPath the Device path in a boot option\r | |
2309 | @param CheckMedia if true, check the device is ready to boot now.\r | |
2310 | \r | |
2311 | @retval TRUE the Device path is valid\r | |
2312 | @retval FALSE the Device path is invalid .\r | |
2313 | \r | |
2314 | **/\r | |
2315 | BOOLEAN\r | |
2316 | EFIAPI\r | |
2317 | BdsLibIsValidEFIBootOptDevicePath (\r | |
2318 | IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r | |
2319 | IN BOOLEAN CheckMedia\r | |
2320 | )\r | |
3384a9bc | 2321 | {\r |
2322 | return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);\r | |
2323 | }\r | |
2324 | \r | |
2325 | /**\r | |
2326 | Check whether the Device path in a boot option point to a valid bootable device,\r | |
2327 | And if CheckMedia is true, check the device is ready to boot now.\r | |
2328 | If Description is not NULL and the device path point to a fixed BlockIo\r | |
2329 | device, check the description whether conflict with other auto-created\r | |
2330 | boot options.\r | |
2331 | \r | |
2332 | @param DevPath the Device path in a boot option\r | |
2333 | @param CheckMedia if true, check the device is ready to boot now.\r | |
2334 | @param Description the description in a boot option\r | |
2335 | \r | |
2336 | @retval TRUE the Device path is valid\r | |
2337 | @retval FALSE the Device path is invalid .\r | |
2338 | \r | |
2339 | **/\r | |
2340 | BOOLEAN\r | |
2341 | EFIAPI\r | |
2342 | BdsLibIsValidEFIBootOptDevicePathExt (\r | |
2343 | IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r | |
2344 | IN BOOLEAN CheckMedia,\r | |
2345 | IN CHAR16 *Description\r | |
2346 | )\r | |
5c08e117 | 2347 | {\r |
2348 | EFI_STATUS Status;\r | |
2349 | EFI_HANDLE Handle;\r | |
2350 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r | |
2351 | EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r | |
2352 | EFI_BLOCK_IO_PROTOCOL *BlockIo;\r | |
2353 | \r | |
2354 | TempDevicePath = DevPath;\r | |
2355 | LastDeviceNode = DevPath;\r | |
128efbbc | 2356 | \r |
5c08e117 | 2357 | //\r |
a7a523e0 | 2358 | // Check if it's a valid boot option for network boot device.\r |
2359 | // Check if there is EfiLoadFileProtocol installed. \r | |
2360 | // If yes, that means there is a boot option for network.\r | |
5c08e117 | 2361 | //\r |
2362 | Status = gBS->LocateDevicePath (\r | |
a7a523e0 | 2363 | &gEfiLoadFileProtocolGuid,\r |
5c08e117 | 2364 | &TempDevicePath,\r |
2365 | &Handle\r | |
2366 | );\r | |
2367 | if (EFI_ERROR (Status)) {\r | |
2368 | //\r | |
2369 | // Device not present so see if we need to connect it\r | |
2370 | //\r | |
2371 | TempDevicePath = DevPath;\r | |
2372 | BdsLibConnectDevicePath (TempDevicePath);\r | |
2373 | Status = gBS->LocateDevicePath (\r | |
a7a523e0 | 2374 | &gEfiLoadFileProtocolGuid,\r |
5c08e117 | 2375 | &TempDevicePath,\r |
2376 | &Handle\r | |
2377 | );\r | |
2378 | }\r | |
128efbbc | 2379 | \r |
5c08e117 | 2380 | if (!EFI_ERROR (Status)) {\r |
a7a523e0 | 2381 | if (!IsDevicePathEnd (TempDevicePath)) {\r |
2382 | //\r | |
2383 | // LoadFile protocol is not installed on handle with exactly the same DevPath\r | |
2384 | //\r | |
2385 | return FALSE;\r | |
2386 | }\r | |
ff482c56 | 2387 | \r |
a7a523e0 | 2388 | if (CheckMedia) {\r |
2389 | //\r | |
2390 | // Test if it is ready to boot now\r | |
2391 | //\r | |
2392 | if (BdsLibNetworkBootWithMediaPresent(DevPath)) {\r | |
5c08e117 | 2393 | return TRUE;\r |
2394 | }\r | |
a7a523e0 | 2395 | } else {\r |
2396 | return TRUE;\r | |
2397 | } \r | |
5c08e117 | 2398 | }\r |
2399 | \r | |
2400 | //\r | |
2401 | // If the boot option point to a file, it is a valid EFI boot option,\r | |
2402 | // and assume it is ready to boot now\r | |
2403 | //\r | |
2404 | while (!IsDevicePathEnd (TempDevicePath)) {\r | |
7389fdd0 | 2405 | //\r |
2406 | // If there is USB Class or USB WWID device path node, treat it as valid EFI\r | |
2407 | // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it\r | |
2408 | // to full device path.\r | |
2409 | //\r | |
2410 | if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&\r | |
2411 | ((DevicePathSubType (TempDevicePath) == MSG_USB_CLASS_DP) ||\r | |
2412 | (DevicePathSubType (TempDevicePath) == MSG_USB_WWID_DP))) {\r | |
2413 | return TRUE;\r | |
2414 | }\r | |
2415 | \r | |
2416 | LastDeviceNode = TempDevicePath;\r | |
2417 | TempDevicePath = NextDevicePathNode (TempDevicePath);\r | |
5c08e117 | 2418 | }\r |
2419 | if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r | |
2420 | (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {\r | |
2421 | return TRUE;\r | |
2422 | }\r | |
2423 | \r | |
2424 | //\r | |
6617d838 | 2425 | // Check if it's a valid boot option for internal FV application\r |
5c08e117 | 2426 | //\r |
2427 | if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {\r | |
2428 | //\r | |
6617d838 | 2429 | // If the boot option point to internal FV application, make sure it is valid\r |
5c08e117 | 2430 | //\r |
128efbbc | 2431 | TempDevicePath = DevPath;\r |
6617d838 RN |
2432 | Status = BdsLibUpdateFvFileDevicePath (\r |
2433 | &TempDevicePath,\r | |
2434 | EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode)\r | |
2435 | );\r | |
5c08e117 | 2436 | if (Status == EFI_ALREADY_STARTED) {\r |
2437 | return TRUE;\r | |
2438 | } else {\r | |
2439 | if (Status == EFI_SUCCESS) {\r | |
128efbbc | 2440 | FreePool (TempDevicePath);\r |
5c08e117 | 2441 | }\r |
2442 | return FALSE;\r | |
2443 | }\r | |
2444 | }\r | |
128efbbc | 2445 | \r |
5c08e117 | 2446 | //\r |
3384a9bc | 2447 | // If the boot option point to a blockIO device:\r |
8d3b5aff | 2448 | // if it is a removable blockIo device, it is valid.\r |
128efbbc | 2449 | // if it is a fixed blockIo device, check its description confliction.\r |
5c08e117 | 2450 | //\r |
2451 | TempDevicePath = DevPath;\r | |
2452 | Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r | |
2453 | if (EFI_ERROR (Status)) {\r | |
2454 | //\r | |
2455 | // Device not present so see if we need to connect it\r | |
2456 | //\r | |
2457 | Status = BdsLibConnectDevicePath (DevPath);\r | |
2458 | if (!EFI_ERROR (Status)) {\r | |
2459 | //\r | |
2460 | // Try again to get the Block Io protocol after we did the connect\r | |
2461 | //\r | |
2462 | TempDevicePath = DevPath;\r | |
2463 | Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r | |
2464 | }\r | |
2465 | }\r | |
128efbbc | 2466 | \r |
5c08e117 | 2467 | if (!EFI_ERROR (Status)) {\r |
2468 | Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r | |
2469 | if (!EFI_ERROR (Status)) {\r | |
2470 | if (CheckMedia) {\r | |
2471 | //\r | |
2472 | // Test if it is ready to boot now\r | |
2473 | //\r | |
2474 | if (BdsLibGetBootableHandle (DevPath) != NULL) {\r | |
2475 | return TRUE;\r | |
2476 | }\r | |
2477 | } else {\r | |
2478 | return TRUE;\r | |
2479 | }\r | |
2480 | }\r | |
2481 | } else {\r | |
2482 | //\r | |
2483 | // if the boot option point to a simple file protocol which does not consume block Io protocol, it is also a valid EFI boot option,\r | |
2484 | //\r | |
2485 | Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);\r | |
2486 | if (!EFI_ERROR (Status)) {\r | |
2487 | if (CheckMedia) {\r | |
2488 | //\r | |
2489 | // Test if it is ready to boot now\r | |
2490 | //\r | |
2491 | if (BdsLibGetBootableHandle (DevPath) != NULL) {\r | |
2492 | return TRUE;\r | |
2493 | }\r | |
2494 | } else {\r | |
2495 | return TRUE;\r | |
2496 | }\r | |
2497 | }\r | |
2498 | }\r | |
2499 | \r | |
2500 | return FALSE;\r | |
2501 | }\r | |
2502 | \r | |
2503 | \r | |
2504 | /**\r | |
2505 | According to a file guild, check a Fv file device path is valid. If it is invalid,\r | |
2506 | try to return the valid device path.\r | |
2507 | FV address maybe changes for memory layout adjust from time to time, use this function\r | |
2508 | could promise the Fv file device path is right.\r | |
2509 | \r | |
2510 | @param DevicePath on input, the Fv file device path need to check on\r | |
2511 | output, the updated valid Fv file device path\r | |
2512 | @param FileGuid the Fv file guild\r | |
2513 | \r | |
2514 | @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid\r | |
2515 | parameter\r | |
2516 | @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file\r | |
2517 | guild at all\r | |
2518 | @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is\r | |
2519 | valid\r | |
2520 | @retval EFI_SUCCESS has successfully updated the invalid DevicePath,\r | |
2521 | and return the updated device path in DevicePath\r | |
2522 | \r | |
2523 | **/\r | |
2524 | EFI_STATUS\r | |
2525 | EFIAPI\r | |
2526 | BdsLibUpdateFvFileDevicePath (\r | |
2527 | IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,\r | |
2528 | IN EFI_GUID *FileGuid\r | |
2529 | )\r | |
2530 | {\r | |
2531 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r | |
2532 | EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r | |
2533 | EFI_STATUS Status;\r | |
2534 | EFI_GUID *GuidPoint;\r | |
2535 | UINTN Index;\r | |
2536 | UINTN FvHandleCount;\r | |
2537 | EFI_HANDLE *FvHandleBuffer;\r | |
2538 | EFI_FV_FILETYPE Type;\r | |
2539 | UINTN Size;\r | |
2540 | EFI_FV_FILE_ATTRIBUTES Attributes;\r | |
2541 | UINT32 AuthenticationStatus;\r | |
2542 | BOOLEAN FindFvFile;\r | |
2543 | EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r | |
2544 | EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r | |
2545 | MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;\r | |
2546 | EFI_HANDLE FoundFvHandle;\r | |
2547 | EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r | |
2548 | \r | |
2549 | if ((DevicePath == NULL) || (*DevicePath == NULL)) {\r | |
2550 | return EFI_INVALID_PARAMETER;\r | |
2551 | }\r | |
2552 | if (FileGuid == NULL) {\r | |
2553 | return EFI_INVALID_PARAMETER;\r | |
2554 | }\r | |
128efbbc | 2555 | \r |
5c08e117 | 2556 | //\r |
2557 | // Check whether the device path point to the default the input Fv file\r | |
2558 | //\r | |
2559 | TempDevicePath = *DevicePath;\r | |
2560 | LastDeviceNode = TempDevicePath;\r | |
2561 | while (!IsDevicePathEnd (TempDevicePath)) {\r | |
2562 | LastDeviceNode = TempDevicePath;\r | |
2563 | TempDevicePath = NextDevicePathNode (TempDevicePath);\r | |
2564 | }\r | |
2565 | GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (\r | |
2566 | (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode\r | |
2567 | );\r | |
2568 | if (GuidPoint == NULL) {\r | |
2569 | //\r | |
2570 | // if this option does not points to a Fv file, just return EFI_UNSUPPORTED\r | |
2571 | //\r | |
2572 | return EFI_UNSUPPORTED;\r | |
2573 | }\r | |
2574 | if (!CompareGuid (GuidPoint, FileGuid)) {\r | |
2575 | //\r | |
2576 | // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED\r | |
2577 | //\r | |
2578 | return EFI_UNSUPPORTED;\r | |
2579 | }\r | |
2580 | \r | |
2581 | //\r | |
2582 | // Check whether the input Fv file device path is valid\r | |
2583 | //\r | |
2584 | TempDevicePath = *DevicePath;\r | |
2585 | FoundFvHandle = NULL;\r | |
2586 | Status = gBS->LocateDevicePath (\r | |
2587 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
2588 | &TempDevicePath,\r | |
2589 | &FoundFvHandle\r | |
2590 | );\r | |
2591 | if (!EFI_ERROR (Status)) {\r | |
2592 | Status = gBS->HandleProtocol (\r | |
2593 | FoundFvHandle,\r | |
2594 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
2595 | (VOID **) &Fv\r | |
2596 | );\r | |
2597 | if (!EFI_ERROR (Status)) {\r | |
2598 | //\r | |
2599 | // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there\r | |
2600 | //\r | |
2601 | Status = Fv->ReadFile (\r | |
2602 | Fv,\r | |
2603 | FileGuid,\r | |
2604 | NULL,\r | |
2605 | &Size,\r | |
2606 | &Type,\r | |
2607 | &Attributes,\r | |
2608 | &AuthenticationStatus\r | |
2609 | );\r | |
2610 | if (!EFI_ERROR (Status)) {\r | |
2611 | return EFI_ALREADY_STARTED;\r | |
2612 | }\r | |
2613 | }\r | |
2614 | }\r | |
2615 | \r | |
2616 | //\r | |
2617 | // Look for the input wanted FV file in current FV\r | |
2618 | // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV\r | |
2619 | //\r | |
2620 | FindFvFile = FALSE;\r | |
2621 | FoundFvHandle = NULL;\r | |
2622 | Status = gBS->HandleProtocol (\r | |
fefefa4c | 2623 | gImageHandle,\r |
5c08e117 | 2624 | &gEfiLoadedImageProtocolGuid,\r |
2625 | (VOID **) &LoadedImage\r | |
2626 | );\r | |
2627 | if (!EFI_ERROR (Status)) {\r | |
2628 | Status = gBS->HandleProtocol (\r | |
2629 | LoadedImage->DeviceHandle,\r | |
2630 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
2631 | (VOID **) &Fv\r | |
2632 | );\r | |
2633 | if (!EFI_ERROR (Status)) {\r | |
2634 | Status = Fv->ReadFile (\r | |
2635 | Fv,\r | |
2636 | FileGuid,\r | |
2637 | NULL,\r | |
2638 | &Size,\r | |
2639 | &Type,\r | |
2640 | &Attributes,\r | |
2641 | &AuthenticationStatus\r | |
2642 | );\r | |
2643 | if (!EFI_ERROR (Status)) {\r | |
2644 | FindFvFile = TRUE;\r | |
2645 | FoundFvHandle = LoadedImage->DeviceHandle;\r | |
2646 | }\r | |
2647 | }\r | |
2648 | }\r | |
2649 | //\r | |
2650 | // Second, if fail to find, try to enumerate all FV\r | |
2651 | //\r | |
2652 | if (!FindFvFile) {\r | |
2653 | FvHandleBuffer = NULL;\r | |
2654 | gBS->LocateHandleBuffer (\r | |
2655 | ByProtocol,\r | |
2656 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
2657 | NULL,\r | |
2658 | &FvHandleCount,\r | |
2659 | &FvHandleBuffer\r | |
2660 | );\r | |
2661 | for (Index = 0; Index < FvHandleCount; Index++) {\r | |
2662 | gBS->HandleProtocol (\r | |
2663 | FvHandleBuffer[Index],\r | |
2664 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
2665 | (VOID **) &Fv\r | |
2666 | );\r | |
2667 | \r | |
2668 | Status = Fv->ReadFile (\r | |
2669 | Fv,\r | |
2670 | FileGuid,\r | |
2671 | NULL,\r | |
2672 | &Size,\r | |
2673 | &Type,\r | |
2674 | &Attributes,\r | |
2675 | &AuthenticationStatus\r | |
2676 | );\r | |
2677 | if (EFI_ERROR (Status)) {\r | |
2678 | //\r | |
2679 | // Skip if input Fv file not in the FV\r | |
2680 | //\r | |
2681 | continue;\r | |
2682 | }\r | |
2683 | FindFvFile = TRUE;\r | |
2684 | FoundFvHandle = FvHandleBuffer[Index];\r | |
2685 | break;\r | |
2686 | }\r | |
2687 | \r | |
2688 | if (FvHandleBuffer != NULL) {\r | |
128efbbc | 2689 | FreePool (FvHandleBuffer);\r |
5c08e117 | 2690 | }\r |
2691 | }\r | |
2692 | \r | |
2693 | if (FindFvFile) {\r | |
2694 | //\r | |
2695 | // Build the shell device path\r | |
2696 | //\r | |
2697 | NewDevicePath = DevicePathFromHandle (FoundFvHandle);\r | |
2698 | EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);\r | |
2699 | NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);\r | |
2700 | *DevicePath = NewDevicePath;\r | |
2701 | return EFI_SUCCESS;\r | |
2702 | }\r | |
2703 | return EFI_NOT_FOUND;\r | |
2704 | }\r |