]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Library functions which relates with booting.\r | |
3 | \r | |
4 | Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>\r | |
5 | (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r | |
6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
7 | \r | |
8 | **/\r | |
9 | \r | |
10 | #include "InternalBm.h"\r | |
11 | \r | |
12 | EFI_RAM_DISK_PROTOCOL *mRamDisk = NULL;\r | |
13 | \r | |
14 | EFI_BOOT_MANAGER_REFRESH_LEGACY_BOOT_OPTION mBmRefreshLegacyBootOption = NULL;\r | |
15 | EFI_BOOT_MANAGER_LEGACY_BOOT mBmLegacyBoot = NULL;\r | |
16 | \r | |
17 | ///\r | |
18 | /// This GUID is used for an EFI Variable that stores the front device pathes\r | |
19 | /// for a partial device path that starts with the HD node.\r | |
20 | ///\r | |
21 | EFI_GUID mBmHardDriveBootVariableGuid = { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x08, 0xe2, 0x0e, 0x90, 0x6c, 0xb6, 0xde } };\r | |
22 | EFI_GUID mBmAutoCreateBootOptionGuid = { 0x8108ac4e, 0x9f11, 0x4d59, { 0x85, 0x0e, 0xe2, 0x1a, 0x52, 0x2c, 0x59, 0xb2 } };\r | |
23 | \r | |
24 | /**\r | |
25 | \r | |
26 | End Perf entry of BDS\r | |
27 | \r | |
28 | @param Event The triggered event.\r | |
29 | @param Context Context for this event.\r | |
30 | \r | |
31 | **/\r | |
32 | VOID\r | |
33 | EFIAPI\r | |
34 | BmEndOfBdsPerfCode (\r | |
35 | IN EFI_EVENT Event,\r | |
36 | IN VOID *Context\r | |
37 | )\r | |
38 | {\r | |
39 | //\r | |
40 | // Record the performance data for End of BDS\r | |
41 | //\r | |
42 | PERF_CROSSMODULE_END("BDS");\r | |
43 | \r | |
44 | return ;\r | |
45 | }\r | |
46 | \r | |
47 | /**\r | |
48 | The function registers the legacy boot support capabilities.\r | |
49 | \r | |
50 | @param RefreshLegacyBootOption The function pointer to create all the legacy boot options.\r | |
51 | @param LegacyBoot The function pointer to boot the legacy boot option.\r | |
52 | **/\r | |
53 | VOID\r | |
54 | EFIAPI\r | |
55 | EfiBootManagerRegisterLegacyBootSupport (\r | |
56 | EFI_BOOT_MANAGER_REFRESH_LEGACY_BOOT_OPTION RefreshLegacyBootOption,\r | |
57 | EFI_BOOT_MANAGER_LEGACY_BOOT LegacyBoot\r | |
58 | )\r | |
59 | {\r | |
60 | mBmRefreshLegacyBootOption = RefreshLegacyBootOption;\r | |
61 | mBmLegacyBoot = LegacyBoot;\r | |
62 | }\r | |
63 | \r | |
64 | /**\r | |
65 | Return TRUE when the boot option is auto-created instead of manually added.\r | |
66 | \r | |
67 | @param BootOption Pointer to the boot option to check.\r | |
68 | \r | |
69 | @retval TRUE The boot option is auto-created.\r | |
70 | @retval FALSE The boot option is manually added.\r | |
71 | **/\r | |
72 | BOOLEAN\r | |
73 | BmIsAutoCreateBootOption (\r | |
74 | EFI_BOOT_MANAGER_LOAD_OPTION *BootOption\r | |
75 | )\r | |
76 | {\r | |
77 | if ((BootOption->OptionalDataSize == sizeof (EFI_GUID)) &&\r | |
78 | CompareGuid ((EFI_GUID *) BootOption->OptionalData, &mBmAutoCreateBootOptionGuid)\r | |
79 | ) {\r | |
80 | return TRUE;\r | |
81 | } else {\r | |
82 | return FALSE;\r | |
83 | }\r | |
84 | }\r | |
85 | \r | |
86 | /**\r | |
87 | Find the boot option in the NV storage and return the option number.\r | |
88 | \r | |
89 | @param OptionToFind Boot option to be checked.\r | |
90 | \r | |
91 | @return The option number of the found boot option.\r | |
92 | \r | |
93 | **/\r | |
94 | UINTN\r | |
95 | BmFindBootOptionInVariable (\r | |
96 | IN EFI_BOOT_MANAGER_LOAD_OPTION *OptionToFind\r | |
97 | )\r | |
98 | {\r | |
99 | EFI_STATUS Status;\r | |
100 | EFI_BOOT_MANAGER_LOAD_OPTION BootOption;\r | |
101 | UINTN OptionNumber;\r | |
102 | CHAR16 OptionName[BM_OPTION_NAME_LEN];\r | |
103 | EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;\r | |
104 | UINTN BootOptionCount;\r | |
105 | UINTN Index;\r | |
106 | \r | |
107 | OptionNumber = LoadOptionNumberUnassigned;\r | |
108 | \r | |
109 | //\r | |
110 | // Try to match the variable exactly if the option number is assigned\r | |
111 | //\r | |
112 | if (OptionToFind->OptionNumber != LoadOptionNumberUnassigned) {\r | |
113 | UnicodeSPrint (\r | |
114 | OptionName, sizeof (OptionName), L"%s%04x",\r | |
115 | mBmLoadOptionName[OptionToFind->OptionType], OptionToFind->OptionNumber\r | |
116 | );\r | |
117 | Status = EfiBootManagerVariableToLoadOption (OptionName, &BootOption);\r | |
118 | \r | |
119 | if (!EFI_ERROR (Status)) {\r | |
120 | ASSERT (OptionToFind->OptionNumber == BootOption.OptionNumber);\r | |
121 | if ((OptionToFind->Attributes == BootOption.Attributes) &&\r | |
122 | (StrCmp (OptionToFind->Description, BootOption.Description) == 0) &&\r | |
123 | (CompareMem (OptionToFind->FilePath, BootOption.FilePath, GetDevicePathSize (OptionToFind->FilePath)) == 0) &&\r | |
124 | (OptionToFind->OptionalDataSize == BootOption.OptionalDataSize) &&\r | |
125 | (CompareMem (OptionToFind->OptionalData, BootOption.OptionalData, OptionToFind->OptionalDataSize) == 0)\r | |
126 | ) {\r | |
127 | OptionNumber = OptionToFind->OptionNumber;\r | |
128 | }\r | |
129 | EfiBootManagerFreeLoadOption (&BootOption);\r | |
130 | }\r | |
131 | }\r | |
132 | \r | |
133 | //\r | |
134 | // The option number assigned is either incorrect or unassigned.\r | |
135 | //\r | |
136 | if (OptionNumber == LoadOptionNumberUnassigned) {\r | |
137 | BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);\r | |
138 | \r | |
139 | Index = EfiBootManagerFindLoadOption (OptionToFind, BootOptions, BootOptionCount);\r | |
140 | if (Index != -1) {\r | |
141 | OptionNumber = BootOptions[Index].OptionNumber;\r | |
142 | }\r | |
143 | \r | |
144 | EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r | |
145 | }\r | |
146 | \r | |
147 | return OptionNumber;\r | |
148 | }\r | |
149 | \r | |
150 | /**\r | |
151 | Return the correct FV file path.\r | |
152 | FV address may change across reboot. This routine promises the FV file device path is right.\r | |
153 | \r | |
154 | @param FilePath The Memory Mapped Device Path to get the file buffer.\r | |
155 | \r | |
156 | @return The updated FV Device Path pointint to the file.\r | |
157 | **/\r | |
158 | EFI_DEVICE_PATH_PROTOCOL *\r | |
159 | BmAdjustFvFilePath (\r | |
160 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r | |
161 | )\r | |
162 | {\r | |
163 | EFI_STATUS Status;\r | |
164 | UINTN Index;\r | |
165 | EFI_DEVICE_PATH_PROTOCOL *FvFileNode;\r | |
166 | EFI_HANDLE FvHandle;\r | |
167 | EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r | |
168 | UINTN FvHandleCount;\r | |
169 | EFI_HANDLE *FvHandles;\r | |
170 | EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r | |
171 | EFI_DEVICE_PATH_PROTOCOL *FullPath;\r | |
172 | \r | |
173 | //\r | |
174 | // Get the file buffer by using the exactly FilePath.\r | |
175 | //\r | |
176 | FvFileNode = FilePath;\r | |
177 | Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &FvFileNode, &FvHandle);\r | |
178 | if (!EFI_ERROR (Status)) {\r | |
179 | return DuplicateDevicePath (FilePath);\r | |
180 | }\r | |
181 | \r | |
182 | //\r | |
183 | // Only wide match other FVs if it's a memory mapped FV file path.\r | |
184 | //\r | |
185 | if ((DevicePathType (FilePath) != HARDWARE_DEVICE_PATH) || (DevicePathSubType (FilePath) != HW_MEMMAP_DP)) {\r | |
186 | return NULL;\r | |
187 | }\r | |
188 | \r | |
189 | FvFileNode = NextDevicePathNode (FilePath);\r | |
190 | \r | |
191 | //\r | |
192 | // Firstly find the FV file in current FV\r | |
193 | //\r | |
194 | gBS->HandleProtocol (\r | |
195 | gImageHandle,\r | |
196 | &gEfiLoadedImageProtocolGuid,\r | |
197 | (VOID **) &LoadedImage\r | |
198 | );\r | |
199 | NewDevicePath = AppendDevicePathNode (DevicePathFromHandle (LoadedImage->DeviceHandle), FvFileNode);\r | |
200 | FullPath = BmAdjustFvFilePath (NewDevicePath);\r | |
201 | FreePool (NewDevicePath);\r | |
202 | if (FullPath != NULL) {\r | |
203 | return FullPath;\r | |
204 | }\r | |
205 | \r | |
206 | //\r | |
207 | // Secondly find the FV file in all other FVs\r | |
208 | //\r | |
209 | gBS->LocateHandleBuffer (\r | |
210 | ByProtocol,\r | |
211 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
212 | NULL,\r | |
213 | &FvHandleCount,\r | |
214 | &FvHandles\r | |
215 | );\r | |
216 | for (Index = 0; Index < FvHandleCount; Index++) {\r | |
217 | if (FvHandles[Index] == LoadedImage->DeviceHandle) {\r | |
218 | //\r | |
219 | // Skip current FV, it was handed in first step.\r | |
220 | //\r | |
221 | continue;\r | |
222 | }\r | |
223 | NewDevicePath = AppendDevicePathNode (DevicePathFromHandle (FvHandles[Index]), FvFileNode);\r | |
224 | FullPath = BmAdjustFvFilePath (NewDevicePath);\r | |
225 | FreePool (NewDevicePath);\r | |
226 | if (FullPath != NULL) {\r | |
227 | break;\r | |
228 | }\r | |
229 | }\r | |
230 | \r | |
231 | if (FvHandles != NULL) {\r | |
232 | FreePool (FvHandles);\r | |
233 | }\r | |
234 | return FullPath;\r | |
235 | }\r | |
236 | \r | |
237 | /**\r | |
238 | Check if it's a Device Path pointing to FV file.\r | |
239 | \r | |
240 | The function doesn't garentee the device path points to existing FV file.\r | |
241 | \r | |
242 | @param DevicePath Input device path.\r | |
243 | \r | |
244 | @retval TRUE The device path is a FV File Device Path.\r | |
245 | @retval FALSE The device path is NOT a FV File Device Path.\r | |
246 | **/\r | |
247 | BOOLEAN\r | |
248 | BmIsFvFilePath (\r | |
249 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r | |
250 | )\r | |
251 | {\r | |
252 | EFI_STATUS Status;\r | |
253 | EFI_HANDLE Handle;\r | |
254 | EFI_DEVICE_PATH_PROTOCOL *Node;\r | |
255 | \r | |
256 | Node = DevicePath;\r | |
257 | Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &Node, &Handle);\r | |
258 | if (!EFI_ERROR (Status)) {\r | |
259 | return TRUE;\r | |
260 | }\r | |
261 | \r | |
262 | if ((DevicePathType (DevicePath) == HARDWARE_DEVICE_PATH) && (DevicePathSubType (DevicePath) == HW_MEMMAP_DP)) {\r | |
263 | DevicePath = NextDevicePathNode (DevicePath);\r | |
264 | if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) && (DevicePathSubType (DevicePath) == MEDIA_PIWG_FW_FILE_DP)) {\r | |
265 | return IsDevicePathEnd (NextDevicePathNode (DevicePath));\r | |
266 | }\r | |
267 | }\r | |
268 | return FALSE;\r | |
269 | }\r | |
270 | \r | |
271 | /**\r | |
272 | Check whether a USB device match the specified USB Class device path. This\r | |
273 | function follows "Load Option Processing" behavior in UEFI specification.\r | |
274 | \r | |
275 | @param UsbIo USB I/O protocol associated with the USB device.\r | |
276 | @param UsbClass The USB Class device path to match.\r | |
277 | \r | |
278 | @retval TRUE The USB device match the USB Class device path.\r | |
279 | @retval FALSE The USB device does not match the USB Class device path.\r | |
280 | \r | |
281 | **/\r | |
282 | BOOLEAN\r | |
283 | BmMatchUsbClass (\r | |
284 | IN EFI_USB_IO_PROTOCOL *UsbIo,\r | |
285 | IN USB_CLASS_DEVICE_PATH *UsbClass\r | |
286 | )\r | |
287 | {\r | |
288 | EFI_STATUS Status;\r | |
289 | EFI_USB_DEVICE_DESCRIPTOR DevDesc;\r | |
290 | EFI_USB_INTERFACE_DESCRIPTOR IfDesc;\r | |
291 | UINT8 DeviceClass;\r | |
292 | UINT8 DeviceSubClass;\r | |
293 | UINT8 DeviceProtocol;\r | |
294 | \r | |
295 | if ((DevicePathType (UsbClass) != MESSAGING_DEVICE_PATH) ||\r | |
296 | (DevicePathSubType (UsbClass) != MSG_USB_CLASS_DP)){\r | |
297 | return FALSE;\r | |
298 | }\r | |
299 | \r | |
300 | //\r | |
301 | // Check Vendor Id and Product Id.\r | |
302 | //\r | |
303 | Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);\r | |
304 | if (EFI_ERROR (Status)) {\r | |
305 | return FALSE;\r | |
306 | }\r | |
307 | \r | |
308 | if ((UsbClass->VendorId != 0xffff) &&\r | |
309 | (UsbClass->VendorId != DevDesc.IdVendor)) {\r | |
310 | return FALSE;\r | |
311 | }\r | |
312 | \r | |
313 | if ((UsbClass->ProductId != 0xffff) &&\r | |
314 | (UsbClass->ProductId != DevDesc.IdProduct)) {\r | |
315 | return FALSE;\r | |
316 | }\r | |
317 | \r | |
318 | DeviceClass = DevDesc.DeviceClass;\r | |
319 | DeviceSubClass = DevDesc.DeviceSubClass;\r | |
320 | DeviceProtocol = DevDesc.DeviceProtocol;\r | |
321 | if (DeviceClass == 0) {\r | |
322 | //\r | |
323 | // If Class in Device Descriptor is set to 0, use the Class, SubClass and\r | |
324 | // Protocol in Interface Descriptor instead.\r | |
325 | //\r | |
326 | Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);\r | |
327 | if (EFI_ERROR (Status)) {\r | |
328 | return FALSE;\r | |
329 | }\r | |
330 | \r | |
331 | DeviceClass = IfDesc.InterfaceClass;\r | |
332 | DeviceSubClass = IfDesc.InterfaceSubClass;\r | |
333 | DeviceProtocol = IfDesc.InterfaceProtocol;\r | |
334 | }\r | |
335 | \r | |
336 | //\r | |
337 | // Check Class, SubClass and Protocol.\r | |
338 | //\r | |
339 | if ((UsbClass->DeviceClass != 0xff) &&\r | |
340 | (UsbClass->DeviceClass != DeviceClass)) {\r | |
341 | return FALSE;\r | |
342 | }\r | |
343 | \r | |
344 | if ((UsbClass->DeviceSubClass != 0xff) &&\r | |
345 | (UsbClass->DeviceSubClass != DeviceSubClass)) {\r | |
346 | return FALSE;\r | |
347 | }\r | |
348 | \r | |
349 | if ((UsbClass->DeviceProtocol != 0xff) &&\r | |
350 | (UsbClass->DeviceProtocol != DeviceProtocol)) {\r | |
351 | return FALSE;\r | |
352 | }\r | |
353 | \r | |
354 | return TRUE;\r | |
355 | }\r | |
356 | \r | |
357 | /**\r | |
358 | Check whether a USB device match the specified USB WWID device path. This\r | |
359 | function follows "Load Option Processing" behavior in UEFI specification.\r | |
360 | \r | |
361 | @param UsbIo USB I/O protocol associated with the USB device.\r | |
362 | @param UsbWwid The USB WWID device path to match.\r | |
363 | \r | |
364 | @retval TRUE The USB device match the USB WWID device path.\r | |
365 | @retval FALSE The USB device does not match the USB WWID device path.\r | |
366 | \r | |
367 | **/\r | |
368 | BOOLEAN\r | |
369 | BmMatchUsbWwid (\r | |
370 | IN EFI_USB_IO_PROTOCOL *UsbIo,\r | |
371 | IN USB_WWID_DEVICE_PATH *UsbWwid\r | |
372 | )\r | |
373 | {\r | |
374 | EFI_STATUS Status;\r | |
375 | EFI_USB_DEVICE_DESCRIPTOR DevDesc;\r | |
376 | EFI_USB_INTERFACE_DESCRIPTOR IfDesc;\r | |
377 | UINT16 *LangIdTable;\r | |
378 | UINT16 TableSize;\r | |
379 | UINT16 Index;\r | |
380 | CHAR16 *CompareStr;\r | |
381 | UINTN CompareLen;\r | |
382 | CHAR16 *SerialNumberStr;\r | |
383 | UINTN Length;\r | |
384 | \r | |
385 | if ((DevicePathType (UsbWwid) != MESSAGING_DEVICE_PATH) ||\r | |
386 | (DevicePathSubType (UsbWwid) != MSG_USB_WWID_DP)) {\r | |
387 | return FALSE;\r | |
388 | }\r | |
389 | \r | |
390 | //\r | |
391 | // Check Vendor Id and Product Id.\r | |
392 | //\r | |
393 | Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);\r | |
394 | if (EFI_ERROR (Status)) {\r | |
395 | return FALSE;\r | |
396 | }\r | |
397 | if ((DevDesc.IdVendor != UsbWwid->VendorId) ||\r | |
398 | (DevDesc.IdProduct != UsbWwid->ProductId)) {\r | |
399 | return FALSE;\r | |
400 | }\r | |
401 | \r | |
402 | //\r | |
403 | // Check Interface Number.\r | |
404 | //\r | |
405 | Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);\r | |
406 | if (EFI_ERROR (Status)) {\r | |
407 | return FALSE;\r | |
408 | }\r | |
409 | if (IfDesc.InterfaceNumber != UsbWwid->InterfaceNumber) {\r | |
410 | return FALSE;\r | |
411 | }\r | |
412 | \r | |
413 | //\r | |
414 | // Check Serial Number.\r | |
415 | //\r | |
416 | if (DevDesc.StrSerialNumber == 0) {\r | |
417 | return FALSE;\r | |
418 | }\r | |
419 | \r | |
420 | //\r | |
421 | // Get all supported languages.\r | |
422 | //\r | |
423 | TableSize = 0;\r | |
424 | LangIdTable = NULL;\r | |
425 | Status = UsbIo->UsbGetSupportedLanguages (UsbIo, &LangIdTable, &TableSize);\r | |
426 | if (EFI_ERROR (Status) || (TableSize == 0) || (LangIdTable == NULL)) {\r | |
427 | return FALSE;\r | |
428 | }\r | |
429 | \r | |
430 | //\r | |
431 | // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.\r | |
432 | //\r | |
433 | CompareStr = (CHAR16 *) (UINTN) (UsbWwid + 1);\r | |
434 | CompareLen = (DevicePathNodeLength (UsbWwid) - sizeof (USB_WWID_DEVICE_PATH)) / sizeof (CHAR16);\r | |
435 | if (CompareStr[CompareLen - 1] == L'\0') {\r | |
436 | CompareLen--;\r | |
437 | }\r | |
438 | \r | |
439 | //\r | |
440 | // Compare serial number in each supported language.\r | |
441 | //\r | |
442 | for (Index = 0; Index < TableSize / sizeof (UINT16); Index++) {\r | |
443 | SerialNumberStr = NULL;\r | |
444 | Status = UsbIo->UsbGetStringDescriptor (\r | |
445 | UsbIo,\r | |
446 | LangIdTable[Index],\r | |
447 | DevDesc.StrSerialNumber,\r | |
448 | &SerialNumberStr\r | |
449 | );\r | |
450 | if (EFI_ERROR (Status) || (SerialNumberStr == NULL)) {\r | |
451 | continue;\r | |
452 | }\r | |
453 | \r | |
454 | Length = StrLen (SerialNumberStr);\r | |
455 | if ((Length >= CompareLen) &&\r | |
456 | (CompareMem (SerialNumberStr + Length - CompareLen, CompareStr, CompareLen * sizeof (CHAR16)) == 0)) {\r | |
457 | FreePool (SerialNumberStr);\r | |
458 | return TRUE;\r | |
459 | }\r | |
460 | \r | |
461 | FreePool (SerialNumberStr);\r | |
462 | }\r | |
463 | \r | |
464 | return FALSE;\r | |
465 | }\r | |
466 | \r | |
467 | /**\r | |
468 | Find a USB device which match the specified short-form device path start with\r | |
469 | USB Class or USB WWID device path. If ParentDevicePath is NULL, this function\r | |
470 | will search in all USB devices of the platform. If ParentDevicePath is not NULL,\r | |
471 | this function will only search in its child devices.\r | |
472 | \r | |
473 | @param DevicePath The device path that contains USB Class or USB WWID device path.\r | |
474 | @param ParentDevicePathSize The length of the device path before the USB Class or\r | |
475 | USB WWID device path.\r | |
476 | @param UsbIoHandleCount A pointer to the count of the returned USB IO handles.\r | |
477 | \r | |
478 | @retval NULL The matched USB IO handles cannot be found.\r | |
479 | @retval other The matched USB IO handles.\r | |
480 | \r | |
481 | **/\r | |
482 | EFI_HANDLE *\r | |
483 | BmFindUsbDevice (\r | |
484 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r | |
485 | IN UINTN ParentDevicePathSize,\r | |
486 | OUT UINTN *UsbIoHandleCount\r | |
487 | )\r | |
488 | {\r | |
489 | EFI_STATUS Status;\r | |
490 | EFI_HANDLE *UsbIoHandles;\r | |
491 | EFI_DEVICE_PATH_PROTOCOL *UsbIoDevicePath;\r | |
492 | EFI_USB_IO_PROTOCOL *UsbIo;\r | |
493 | UINTN Index;\r | |
494 | BOOLEAN Matched;\r | |
495 | \r | |
496 | ASSERT (UsbIoHandleCount != NULL);\r | |
497 | \r | |
498 | //\r | |
499 | // Get all UsbIo Handles.\r | |
500 | //\r | |
501 | Status = gBS->LocateHandleBuffer (\r | |
502 | ByProtocol,\r | |
503 | &gEfiUsbIoProtocolGuid,\r | |
504 | NULL,\r | |
505 | UsbIoHandleCount,\r | |
506 | &UsbIoHandles\r | |
507 | );\r | |
508 | if (EFI_ERROR (Status)) {\r | |
509 | *UsbIoHandleCount = 0;\r | |
510 | UsbIoHandles = NULL;\r | |
511 | }\r | |
512 | \r | |
513 | for (Index = 0; Index < *UsbIoHandleCount; ) {\r | |
514 | //\r | |
515 | // Get the Usb IO interface.\r | |
516 | //\r | |
517 | Status = gBS->HandleProtocol(\r | |
518 | UsbIoHandles[Index],\r | |
519 | &gEfiUsbIoProtocolGuid,\r | |
520 | (VOID **) &UsbIo\r | |
521 | );\r | |
522 | UsbIoDevicePath = DevicePathFromHandle (UsbIoHandles[Index]);\r | |
523 | Matched = FALSE;\r | |
524 | if (!EFI_ERROR (Status) && (UsbIoDevicePath != NULL)) {\r | |
525 | \r | |
526 | //\r | |
527 | // Compare starting part of UsbIoHandle's device path with ParentDevicePath.\r | |
528 | //\r | |
529 | if (CompareMem (UsbIoDevicePath, DevicePath, ParentDevicePathSize) == 0) {\r | |
530 | if (BmMatchUsbClass (UsbIo, (USB_CLASS_DEVICE_PATH *) ((UINTN) DevicePath + ParentDevicePathSize)) ||\r | |
531 | BmMatchUsbWwid (UsbIo, (USB_WWID_DEVICE_PATH *) ((UINTN) DevicePath + ParentDevicePathSize))) {\r | |
532 | Matched = TRUE;\r | |
533 | }\r | |
534 | }\r | |
535 | }\r | |
536 | \r | |
537 | if (!Matched) {\r | |
538 | (*UsbIoHandleCount) --;\r | |
539 | CopyMem (&UsbIoHandles[Index], &UsbIoHandles[Index + 1], (*UsbIoHandleCount - Index) * sizeof (EFI_HANDLE));\r | |
540 | } else {\r | |
541 | Index++;\r | |
542 | }\r | |
543 | }\r | |
544 | \r | |
545 | return UsbIoHandles;\r | |
546 | }\r | |
547 | \r | |
548 | /**\r | |
549 | Expand USB Class or USB WWID device path node to be full device path of a USB\r | |
550 | device in platform.\r | |
551 | \r | |
552 | This function support following 4 cases:\r | |
553 | 1) Boot Option device path starts with a USB Class or USB WWID device path,\r | |
554 | and there is no Media FilePath device path in the end.\r | |
555 | In this case, it will follow Removable Media Boot Behavior.\r | |
556 | 2) Boot Option device path starts with a USB Class or USB WWID device path,\r | |
557 | and ended with Media FilePath device path.\r | |
558 | 3) Boot Option device path starts with a full device path to a USB Host Controller,\r | |
559 | contains a USB Class or USB WWID device path node, while not ended with Media\r | |
560 | FilePath device path. In this case, it will follow Removable Media Boot Behavior.\r | |
561 | 4) Boot Option device path starts with a full device path to a USB Host Controller,\r | |
562 | contains a USB Class or USB WWID device path node, and ended with Media\r | |
563 | FilePath device path.\r | |
564 | \r | |
565 | @param FilePath The device path pointing to a load option.\r | |
566 | It could be a short-form device path.\r | |
567 | @param FullPath The full path returned by the routine in last call.\r | |
568 | Set to NULL in first call.\r | |
569 | @param ShortformNode Pointer to the USB short-form device path node in the FilePath buffer.\r | |
570 | \r | |
571 | @return The next possible full path pointing to the load option.\r | |
572 | Caller is responsible to free the memory.\r | |
573 | **/\r | |
574 | EFI_DEVICE_PATH_PROTOCOL *\r | |
575 | BmExpandUsbDevicePath (\r | |
576 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r | |
577 | IN EFI_DEVICE_PATH_PROTOCOL *FullPath,\r | |
578 | IN EFI_DEVICE_PATH_PROTOCOL *ShortformNode\r | |
579 | )\r | |
580 | {\r | |
581 | UINTN ParentDevicePathSize;\r | |
582 | EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;\r | |
583 | EFI_DEVICE_PATH_PROTOCOL *NextFullPath;\r | |
584 | EFI_HANDLE *Handles;\r | |
585 | UINTN HandleCount;\r | |
586 | UINTN Index;\r | |
587 | BOOLEAN GetNext;\r | |
588 | \r | |
589 | NextFullPath = NULL;\r | |
590 | GetNext = (BOOLEAN)(FullPath == NULL);\r | |
591 | ParentDevicePathSize = (UINTN) ShortformNode - (UINTN) FilePath;\r | |
592 | RemainingDevicePath = NextDevicePathNode (ShortformNode);\r | |
593 | Handles = BmFindUsbDevice (FilePath, ParentDevicePathSize, &HandleCount);\r | |
594 | \r | |
595 | for (Index = 0; Index < HandleCount; Index++) {\r | |
596 | FilePath = AppendDevicePath (DevicePathFromHandle (Handles[Index]), RemainingDevicePath);\r | |
597 | if (FilePath == NULL) {\r | |
598 | //\r | |
599 | // Out of memory.\r | |
600 | //\r | |
601 | continue;\r | |
602 | }\r | |
603 | NextFullPath = BmGetNextLoadOptionDevicePath (FilePath, NULL);\r | |
604 | FreePool (FilePath);\r | |
605 | if (NextFullPath == NULL) {\r | |
606 | //\r | |
607 | // No BlockIo or SimpleFileSystem under FilePath.\r | |
608 | //\r | |
609 | continue;\r | |
610 | }\r | |
611 | if (GetNext) {\r | |
612 | break;\r | |
613 | } else {\r | |
614 | GetNext = (BOOLEAN)(CompareMem (NextFullPath, FullPath, GetDevicePathSize (NextFullPath)) == 0);\r | |
615 | FreePool (NextFullPath);\r | |
616 | NextFullPath = NULL;\r | |
617 | }\r | |
618 | }\r | |
619 | \r | |
620 | if (Handles != NULL) {\r | |
621 | FreePool (Handles);\r | |
622 | }\r | |
623 | \r | |
624 | return NextFullPath;\r | |
625 | }\r | |
626 | \r | |
627 | /**\r | |
628 | Expand File-path device path node to be full device path in platform.\r | |
629 | \r | |
630 | @param FilePath The device path pointing to a load option.\r | |
631 | It could be a short-form device path.\r | |
632 | @param FullPath The full path returned by the routine in last call.\r | |
633 | Set to NULL in first call.\r | |
634 | \r | |
635 | @return The next possible full path pointing to the load option.\r | |
636 | Caller is responsible to free the memory.\r | |
637 | **/\r | |
638 | EFI_DEVICE_PATH_PROTOCOL *\r | |
639 | BmExpandFileDevicePath (\r | |
640 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r | |
641 | IN EFI_DEVICE_PATH_PROTOCOL *FullPath\r | |
642 | )\r | |
643 | {\r | |
644 | EFI_STATUS Status;\r | |
645 | UINTN Index;\r | |
646 | UINTN HandleCount;\r | |
647 | EFI_HANDLE *Handles;\r | |
648 | EFI_BLOCK_IO_PROTOCOL *BlockIo;\r | |
649 | UINTN MediaType;\r | |
650 | EFI_DEVICE_PATH_PROTOCOL *NextFullPath;\r | |
651 | BOOLEAN GetNext;\r | |
652 | \r | |
653 | EfiBootManagerConnectAll ();\r | |
654 | Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiSimpleFileSystemProtocolGuid, NULL, &HandleCount, &Handles);\r | |
655 | if (EFI_ERROR (Status)) {\r | |
656 | HandleCount = 0;\r | |
657 | Handles = NULL;\r | |
658 | }\r | |
659 | \r | |
660 | GetNext = (BOOLEAN)(FullPath == NULL);\r | |
661 | NextFullPath = NULL;\r | |
662 | //\r | |
663 | // Enumerate all removable media devices followed by all fixed media devices,\r | |
664 | // followed by media devices which don't layer on block io.\r | |
665 | //\r | |
666 | for (MediaType = 0; MediaType < 3; MediaType++) {\r | |
667 | for (Index = 0; Index < HandleCount; Index++) {\r | |
668 | Status = gBS->HandleProtocol (Handles[Index], &gEfiBlockIoProtocolGuid, (VOID *) &BlockIo);\r | |
669 | if (EFI_ERROR (Status)) {\r | |
670 | BlockIo = NULL;\r | |
671 | }\r | |
672 | if ((MediaType == 0 && BlockIo != NULL && BlockIo->Media->RemovableMedia) ||\r | |
673 | (MediaType == 1 && BlockIo != NULL && !BlockIo->Media->RemovableMedia) ||\r | |
674 | (MediaType == 2 && BlockIo == NULL)\r | |
675 | ) {\r | |
676 | NextFullPath = AppendDevicePath (DevicePathFromHandle (Handles[Index]), FilePath);\r | |
677 | if (GetNext) {\r | |
678 | break;\r | |
679 | } else {\r | |
680 | GetNext = (BOOLEAN)(CompareMem (NextFullPath, FullPath, GetDevicePathSize (NextFullPath)) == 0);\r | |
681 | FreePool (NextFullPath);\r | |
682 | NextFullPath = NULL;\r | |
683 | }\r | |
684 | }\r | |
685 | }\r | |
686 | if (NextFullPath != NULL) {\r | |
687 | break;\r | |
688 | }\r | |
689 | }\r | |
690 | \r | |
691 | if (Handles != NULL) {\r | |
692 | FreePool (Handles);\r | |
693 | }\r | |
694 | \r | |
695 | return NextFullPath;\r | |
696 | }\r | |
697 | \r | |
698 | /**\r | |
699 | Expand URI device path node to be full device path in platform.\r | |
700 | \r | |
701 | @param FilePath The device path pointing to a load option.\r | |
702 | It could be a short-form device path.\r | |
703 | @param FullPath The full path returned by the routine in last call.\r | |
704 | Set to NULL in first call.\r | |
705 | \r | |
706 | @return The next possible full path pointing to the load option.\r | |
707 | Caller is responsible to free the memory.\r | |
708 | **/\r | |
709 | EFI_DEVICE_PATH_PROTOCOL *\r | |
710 | BmExpandUriDevicePath (\r | |
711 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r | |
712 | IN EFI_DEVICE_PATH_PROTOCOL *FullPath\r | |
713 | )\r | |
714 | {\r | |
715 | EFI_STATUS Status;\r | |
716 | UINTN Index;\r | |
717 | UINTN HandleCount;\r | |
718 | EFI_HANDLE *Handles;\r | |
719 | EFI_DEVICE_PATH_PROTOCOL *NextFullPath;\r | |
720 | EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath;\r | |
721 | BOOLEAN GetNext;\r | |
722 | \r | |
723 | EfiBootManagerConnectAll ();\r | |
724 | Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiLoadFileProtocolGuid, NULL, &HandleCount, &Handles);\r | |
725 | if (EFI_ERROR (Status)) {\r | |
726 | HandleCount = 0;\r | |
727 | Handles = NULL;\r | |
728 | }\r | |
729 | \r | |
730 | NextFullPath = NULL;\r | |
731 | GetNext = (BOOLEAN)(FullPath == NULL);\r | |
732 | for (Index = 0; Index < HandleCount; Index++) {\r | |
733 | NextFullPath = BmExpandLoadFile (Handles[Index], FilePath);\r | |
734 | \r | |
735 | if (NextFullPath == NULL) {\r | |
736 | continue;\r | |
737 | }\r | |
738 | \r | |
739 | if (GetNext) {\r | |
740 | break;\r | |
741 | } else {\r | |
742 | GetNext = (BOOLEAN)(CompareMem (NextFullPath, FullPath, GetDevicePathSize (NextFullPath)) == 0);\r | |
743 | //\r | |
744 | // Free the resource occupied by the RAM disk.\r | |
745 | //\r | |
746 | RamDiskDevicePath = BmGetRamDiskDevicePath (NextFullPath);\r | |
747 | if (RamDiskDevicePath != NULL) {\r | |
748 | BmDestroyRamDisk (RamDiskDevicePath);\r | |
749 | FreePool (RamDiskDevicePath);\r | |
750 | }\r | |
751 | FreePool (NextFullPath);\r | |
752 | NextFullPath = NULL;\r | |
753 | }\r | |
754 | }\r | |
755 | \r | |
756 | if (Handles != NULL) {\r | |
757 | FreePool (Handles);\r | |
758 | }\r | |
759 | \r | |
760 | return NextFullPath;\r | |
761 | }\r | |
762 | \r | |
763 | /**\r | |
764 | Save the partition DevicePath to the CachedDevicePath as the first instance.\r | |
765 | \r | |
766 | @param CachedDevicePath The device path cache.\r | |
767 | @param DevicePath The partition device path to be cached.\r | |
768 | **/\r | |
769 | VOID\r | |
770 | BmCachePartitionDevicePath (\r | |
771 | IN OUT EFI_DEVICE_PATH_PROTOCOL **CachedDevicePath,\r | |
772 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r | |
773 | )\r | |
774 | {\r | |
775 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r | |
776 | UINTN Count;\r | |
777 | \r | |
778 | if (BmMatchDevicePaths (*CachedDevicePath, DevicePath)) {\r | |
779 | TempDevicePath = *CachedDevicePath;\r | |
780 | *CachedDevicePath = BmDelPartMatchInstance (*CachedDevicePath, DevicePath);\r | |
781 | FreePool (TempDevicePath);\r | |
782 | }\r | |
783 | \r | |
784 | if (*CachedDevicePath == NULL) {\r | |
785 | *CachedDevicePath = DuplicateDevicePath (DevicePath);\r | |
786 | return;\r | |
787 | }\r | |
788 | \r | |
789 | TempDevicePath = *CachedDevicePath;\r | |
790 | *CachedDevicePath = AppendDevicePathInstance (DevicePath, *CachedDevicePath);\r | |
791 | if (TempDevicePath != NULL) {\r | |
792 | FreePool (TempDevicePath);\r | |
793 | }\r | |
794 | \r | |
795 | //\r | |
796 | // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller\r | |
797 | // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.\r | |
798 | //\r | |
799 | Count = 0;\r | |
800 | TempDevicePath = *CachedDevicePath;\r | |
801 | while (!IsDevicePathEnd (TempDevicePath)) {\r | |
802 | TempDevicePath = NextDevicePathNode (TempDevicePath);\r | |
803 | //\r | |
804 | // Parse one instance\r | |
805 | //\r | |
806 | while (!IsDevicePathEndType (TempDevicePath)) {\r | |
807 | TempDevicePath = NextDevicePathNode (TempDevicePath);\r | |
808 | }\r | |
809 | Count++;\r | |
810 | //\r | |
811 | // If the CachedDevicePath variable contain too much instance, only remain 12 instances.\r | |
812 | //\r | |
813 | if (Count == 12) {\r | |
814 | SetDevicePathEndNode (TempDevicePath);\r | |
815 | break;\r | |
816 | }\r | |
817 | }\r | |
818 | }\r | |
819 | \r | |
820 | /**\r | |
821 | Expand a device path that starts with a hard drive media device path node to be a\r | |
822 | full device path that includes the full hardware path to the device. We need\r | |
823 | to do this so it can be booted. As an optimization the front match (the part point\r | |
824 | to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable\r | |
825 | so a connect all is not required on every boot. All successful history device path\r | |
826 | which point to partition node (the front part) will be saved.\r | |
827 | \r | |
828 | @param FilePath The device path pointing to a load option.\r | |
829 | It could be a short-form device path.\r | |
830 | \r | |
831 | @return The full device path pointing to the load option.\r | |
832 | **/\r | |
833 | EFI_DEVICE_PATH_PROTOCOL *\r | |
834 | BmExpandPartitionDevicePath (\r | |
835 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r | |
836 | )\r | |
837 | {\r | |
838 | EFI_STATUS Status;\r | |
839 | UINTN BlockIoHandleCount;\r | |
840 | EFI_HANDLE *BlockIoBuffer;\r | |
841 | EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;\r | |
842 | UINTN Index;\r | |
843 | EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath;\r | |
844 | EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r | |
845 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r | |
846 | EFI_DEVICE_PATH_PROTOCOL *FullPath;\r | |
847 | UINTN CachedDevicePathSize;\r | |
848 | BOOLEAN NeedAdjust;\r | |
849 | EFI_DEVICE_PATH_PROTOCOL *Instance;\r | |
850 | UINTN Size;\r | |
851 | \r | |
852 | //\r | |
853 | // Check if there is prestore 'HDDP' variable.\r | |
854 | // If exist, search the front path which point to partition node in the variable instants.\r | |
855 | // If fail to find or 'HDDP' not exist, reconnect all and search in all system\r | |
856 | //\r | |
857 | GetVariable2 (L"HDDP", &mBmHardDriveBootVariableGuid, (VOID **) &CachedDevicePath, &CachedDevicePathSize);\r | |
858 | \r | |
859 | //\r | |
860 | // Delete the invalid 'HDDP' variable.\r | |
861 | //\r | |
862 | if ((CachedDevicePath != NULL) && !IsDevicePathValid (CachedDevicePath, CachedDevicePathSize)) {\r | |
863 | FreePool (CachedDevicePath);\r | |
864 | CachedDevicePath = NULL;\r | |
865 | Status = gRT->SetVariable (\r | |
866 | L"HDDP",\r | |
867 | &mBmHardDriveBootVariableGuid,\r | |
868 | 0,\r | |
869 | 0,\r | |
870 | NULL\r | |
871 | );\r | |
872 | ASSERT_EFI_ERROR (Status);\r | |
873 | }\r | |
874 | \r | |
875 | FullPath = NULL;\r | |
876 | if (CachedDevicePath != NULL) {\r | |
877 | TempNewDevicePath = CachedDevicePath;\r | |
878 | NeedAdjust = FALSE;\r | |
879 | do {\r | |
880 | //\r | |
881 | // Check every instance of the variable\r | |
882 | // First, check whether the instance contain the partition node, which is needed for distinguishing multi\r | |
883 | // partial partition boot option. Second, check whether the instance could be connected.\r | |
884 | //\r | |
885 | Instance = GetNextDevicePathInstance (&TempNewDevicePath, &Size);\r | |
886 | if (BmMatchPartitionDevicePathNode (Instance, (HARDDRIVE_DEVICE_PATH *) FilePath)) {\r | |
887 | //\r | |
888 | // Connect the device path instance, the device path point to hard drive media device path node\r | |
889 | // e.g. ACPI() /PCI()/ATA()/Partition()\r | |
890 | //\r | |
891 | Status = EfiBootManagerConnectDevicePath (Instance, NULL);\r | |
892 | if (!EFI_ERROR (Status)) {\r | |
893 | TempDevicePath = AppendDevicePath (Instance, NextDevicePathNode (FilePath));\r | |
894 | //\r | |
895 | // TempDevicePath = ACPI()/PCI()/ATA()/Partition()\r | |
896 | // or = ACPI()/PCI()/ATA()/Partition()/.../A.EFI\r | |
897 | //\r | |
898 | // When TempDevicePath = ACPI()/PCI()/ATA()/Partition(),\r | |
899 | // it may expand to two potienal full paths (nested partition, rarely happen):\r | |
900 | // 1. ACPI()/PCI()/ATA()/Partition()/Partition(A1)/EFI/BootX64.EFI\r | |
901 | // 2. ACPI()/PCI()/ATA()/Partition()/Partition(A2)/EFI/BootX64.EFI\r | |
902 | // For simplicity, only #1 is returned.\r | |
903 | //\r | |
904 | FullPath = BmGetNextLoadOptionDevicePath (TempDevicePath, NULL);\r | |
905 | FreePool (TempDevicePath);\r | |
906 | \r | |
907 | if (FullPath != NULL) {\r | |
908 | //\r | |
909 | // Adjust the 'HDDP' instances sequence if the matched one is not first one.\r | |
910 | //\r | |
911 | if (NeedAdjust) {\r | |
912 | BmCachePartitionDevicePath (&CachedDevicePath, Instance);\r | |
913 | //\r | |
914 | // Save the matching Device Path so we don't need to do a connect all next time\r | |
915 | // Failing to save only impacts performance next time expanding the short-form device path\r | |
916 | //\r | |
917 | Status = gRT->SetVariable (\r | |
918 | L"HDDP",\r | |
919 | &mBmHardDriveBootVariableGuid,\r | |
920 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r | |
921 | GetDevicePathSize (CachedDevicePath),\r | |
922 | CachedDevicePath\r | |
923 | );\r | |
924 | }\r | |
925 | \r | |
926 | FreePool (Instance);\r | |
927 | FreePool (CachedDevicePath);\r | |
928 | return FullPath;\r | |
929 | }\r | |
930 | }\r | |
931 | }\r | |
932 | //\r | |
933 | // Come here means the first instance is not matched\r | |
934 | //\r | |
935 | NeedAdjust = TRUE;\r | |
936 | FreePool(Instance);\r | |
937 | } while (TempNewDevicePath != NULL);\r | |
938 | }\r | |
939 | \r | |
940 | //\r | |
941 | // If we get here we fail to find or 'HDDP' not exist, and now we need\r | |
942 | // to search all devices in the system for a matched partition\r | |
943 | //\r | |
944 | EfiBootManagerConnectAll ();\r | |
945 | Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);\r | |
946 | if (EFI_ERROR (Status)) {\r | |
947 | BlockIoHandleCount = 0;\r | |
948 | BlockIoBuffer = NULL;\r | |
949 | }\r | |
950 | //\r | |
951 | // Loop through all the device handles that support the BLOCK_IO Protocol\r | |
952 | //\r | |
953 | for (Index = 0; Index < BlockIoHandleCount; Index++) {\r | |
954 | BlockIoDevicePath = DevicePathFromHandle (BlockIoBuffer[Index]);\r | |
955 | if (BlockIoDevicePath == NULL) {\r | |
956 | continue;\r | |
957 | }\r | |
958 | \r | |
959 | if (BmMatchPartitionDevicePathNode (BlockIoDevicePath, (HARDDRIVE_DEVICE_PATH *) FilePath)) {\r | |
960 | //\r | |
961 | // Find the matched partition device path\r | |
962 | //\r | |
963 | TempDevicePath = AppendDevicePath (BlockIoDevicePath, NextDevicePathNode (FilePath));\r | |
964 | FullPath = BmGetNextLoadOptionDevicePath (TempDevicePath, NULL);\r | |
965 | FreePool (TempDevicePath);\r | |
966 | \r | |
967 | if (FullPath != NULL) {\r | |
968 | BmCachePartitionDevicePath (&CachedDevicePath, BlockIoDevicePath);\r | |
969 | \r | |
970 | //\r | |
971 | // Save the matching Device Path so we don't need to do a connect all next time\r | |
972 | // Failing to save only impacts performance next time expanding the short-form device path\r | |
973 | //\r | |
974 | Status = gRT->SetVariable (\r | |
975 | L"HDDP",\r | |
976 | &mBmHardDriveBootVariableGuid,\r | |
977 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r | |
978 | GetDevicePathSize (CachedDevicePath),\r | |
979 | CachedDevicePath\r | |
980 | );\r | |
981 | \r | |
982 | break;\r | |
983 | }\r | |
984 | }\r | |
985 | }\r | |
986 | \r | |
987 | if (CachedDevicePath != NULL) {\r | |
988 | FreePool (CachedDevicePath);\r | |
989 | }\r | |
990 | if (BlockIoBuffer != NULL) {\r | |
991 | FreePool (BlockIoBuffer);\r | |
992 | }\r | |
993 | return FullPath;\r | |
994 | }\r | |
995 | \r | |
996 | /**\r | |
997 | Expand the media device path which points to a BlockIo or SimpleFileSystem instance\r | |
998 | by appending EFI_REMOVABLE_MEDIA_FILE_NAME.\r | |
999 | \r | |
1000 | @param DevicePath The media device path pointing to a BlockIo or SimpleFileSystem instance.\r | |
1001 | @param FullPath The full path returned by the routine in last call.\r | |
1002 | Set to NULL in first call.\r | |
1003 | \r | |
1004 | @return The next possible full path pointing to the load option.\r | |
1005 | Caller is responsible to free the memory.\r | |
1006 | **/\r | |
1007 | EFI_DEVICE_PATH_PROTOCOL *\r | |
1008 | BmExpandMediaDevicePath (\r | |
1009 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r | |
1010 | IN EFI_DEVICE_PATH_PROTOCOL *FullPath\r | |
1011 | )\r | |
1012 | {\r | |
1013 | EFI_STATUS Status;\r | |
1014 | EFI_HANDLE Handle;\r | |
1015 | EFI_BLOCK_IO_PROTOCOL *BlockIo;\r | |
1016 | VOID *Buffer;\r | |
1017 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r | |
1018 | EFI_DEVICE_PATH_PROTOCOL *NextFullPath;\r | |
1019 | UINTN Size;\r | |
1020 | UINTN TempSize;\r | |
1021 | EFI_HANDLE *SimpleFileSystemHandles;\r | |
1022 | UINTN NumberSimpleFileSystemHandles;\r | |
1023 | UINTN Index;\r | |
1024 | BOOLEAN GetNext;\r | |
1025 | \r | |
1026 | GetNext = (BOOLEAN)(FullPath == NULL);\r | |
1027 | //\r | |
1028 | // Check whether the device is connected\r | |
1029 | //\r | |
1030 | TempDevicePath = DevicePath;\r | |
1031 | Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);\r | |
1032 | if (!EFI_ERROR (Status)) {\r | |
1033 | ASSERT (IsDevicePathEnd (TempDevicePath));\r | |
1034 | \r | |
1035 | NextFullPath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r | |
1036 | //\r | |
1037 | // For device path pointing to simple file system, it only expands to one full path.\r | |
1038 | //\r | |
1039 | if (GetNext) {\r | |
1040 | return NextFullPath;\r | |
1041 | } else {\r | |
1042 | FreePool (NextFullPath);\r | |
1043 | return NULL;\r | |
1044 | }\r | |
1045 | }\r | |
1046 | \r | |
1047 | Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r | |
1048 | ASSERT_EFI_ERROR (Status);\r | |
1049 | \r | |
1050 | //\r | |
1051 | // For device boot option only pointing to the removable device handle,\r | |
1052 | // should make sure all its children handles (its child partion or media handles)\r | |
1053 | // are created and connected.\r | |
1054 | //\r | |
1055 | gBS->ConnectController (Handle, NULL, NULL, TRUE);\r | |
1056 | \r | |
1057 | //\r | |
1058 | // Issue a dummy read to the device to check for media change.\r | |
1059 | // When the removable media is changed, any Block IO read/write will\r | |
1060 | // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is\r | |
1061 | // returned. After the Block IO protocol is reinstalled, subsequent\r | |
1062 | // Block IO read/write will success.\r | |
1063 | //\r | |
1064 | Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **) &BlockIo);\r | |
1065 | ASSERT_EFI_ERROR (Status);\r | |
1066 | if (EFI_ERROR (Status)) {\r | |
1067 | return NULL;\r | |
1068 | }\r | |
1069 | Buffer = AllocatePool (BlockIo->Media->BlockSize);\r | |
1070 | if (Buffer != NULL) {\r | |
1071 | BlockIo->ReadBlocks (\r | |
1072 | BlockIo,\r | |
1073 | BlockIo->Media->MediaId,\r | |
1074 | 0,\r | |
1075 | BlockIo->Media->BlockSize,\r | |
1076 | Buffer\r | |
1077 | );\r | |
1078 | FreePool (Buffer);\r | |
1079 | }\r | |
1080 | \r | |
1081 | //\r | |
1082 | // Detect the the default boot file from removable Media\r | |
1083 | //\r | |
1084 | NextFullPath = NULL;\r | |
1085 | Size = GetDevicePathSize (DevicePath) - END_DEVICE_PATH_LENGTH;\r | |
1086 | gBS->LocateHandleBuffer (\r | |
1087 | ByProtocol,\r | |
1088 | &gEfiSimpleFileSystemProtocolGuid,\r | |
1089 | NULL,\r | |
1090 | &NumberSimpleFileSystemHandles,\r | |
1091 | &SimpleFileSystemHandles\r | |
1092 | );\r | |
1093 | for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {\r | |
1094 | //\r | |
1095 | // Get the device path size of SimpleFileSystem handle\r | |
1096 | //\r | |
1097 | TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);\r | |
1098 | TempSize = GetDevicePathSize (TempDevicePath) - END_DEVICE_PATH_LENGTH;\r | |
1099 | //\r | |
1100 | // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path\r | |
1101 | //\r | |
1102 | if ((Size <= TempSize) && (CompareMem (TempDevicePath, DevicePath, Size) == 0)) {\r | |
1103 | NextFullPath = FileDevicePath (SimpleFileSystemHandles[Index], EFI_REMOVABLE_MEDIA_FILE_NAME);\r | |
1104 | if (GetNext) {\r | |
1105 | break;\r | |
1106 | } else {\r | |
1107 | GetNext = (BOOLEAN)(CompareMem (NextFullPath, FullPath, GetDevicePathSize (NextFullPath)) == 0);\r | |
1108 | FreePool (NextFullPath);\r | |
1109 | NextFullPath = NULL;\r | |
1110 | }\r | |
1111 | }\r | |
1112 | }\r | |
1113 | \r | |
1114 | if (SimpleFileSystemHandles != NULL) {\r | |
1115 | FreePool (SimpleFileSystemHandles);\r | |
1116 | }\r | |
1117 | \r | |
1118 | return NextFullPath;\r | |
1119 | }\r | |
1120 | \r | |
1121 | /**\r | |
1122 | Check whether Left and Right are the same without matching the specific\r | |
1123 | device path data in IP device path and URI device path node.\r | |
1124 | \r | |
1125 | @retval TRUE Left and Right are the same.\r | |
1126 | @retval FALSE Left and Right are the different.\r | |
1127 | **/\r | |
1128 | BOOLEAN\r | |
1129 | BmMatchHttpBootDevicePath (\r | |
1130 | IN EFI_DEVICE_PATH_PROTOCOL *Left,\r | |
1131 | IN EFI_DEVICE_PATH_PROTOCOL *Right\r | |
1132 | )\r | |
1133 | {\r | |
1134 | for (; !IsDevicePathEnd (Left) && !IsDevicePathEnd (Right)\r | |
1135 | ; Left = NextDevicePathNode (Left), Right = NextDevicePathNode (Right)\r | |
1136 | ) {\r | |
1137 | if (CompareMem (Left, Right, DevicePathNodeLength (Left)) != 0) {\r | |
1138 | if ((DevicePathType (Left) != MESSAGING_DEVICE_PATH) || (DevicePathType (Right) != MESSAGING_DEVICE_PATH)) {\r | |
1139 | return FALSE;\r | |
1140 | }\r | |
1141 | \r | |
1142 | if (DevicePathSubType (Left) == MSG_DNS_DP) {\r | |
1143 | Left = NextDevicePathNode (Left);\r | |
1144 | }\r | |
1145 | \r | |
1146 | if (DevicePathSubType (Right) == MSG_DNS_DP) {\r | |
1147 | Right = NextDevicePathNode (Right);\r | |
1148 | }\r | |
1149 | \r | |
1150 | if (((DevicePathSubType (Left) != MSG_IPv4_DP) || (DevicePathSubType (Right) != MSG_IPv4_DP)) &&\r | |
1151 | ((DevicePathSubType (Left) != MSG_IPv6_DP) || (DevicePathSubType (Right) != MSG_IPv6_DP)) &&\r | |
1152 | ((DevicePathSubType (Left) != MSG_URI_DP) || (DevicePathSubType (Right) != MSG_URI_DP))\r | |
1153 | ) {\r | |
1154 | return FALSE;\r | |
1155 | }\r | |
1156 | }\r | |
1157 | }\r | |
1158 | return (BOOLEAN) (IsDevicePathEnd (Left) && IsDevicePathEnd (Right));\r | |
1159 | }\r | |
1160 | \r | |
1161 | /**\r | |
1162 | Get the file buffer from the file system produced by Load File instance.\r | |
1163 | \r | |
1164 | @param LoadFileHandle The handle of LoadFile instance.\r | |
1165 | @param RamDiskHandle Return the RAM Disk handle.\r | |
1166 | \r | |
1167 | @return The next possible full path pointing to the load option.\r | |
1168 | Caller is responsible to free the memory.\r | |
1169 | **/\r | |
1170 | EFI_DEVICE_PATH_PROTOCOL *\r | |
1171 | BmExpandNetworkFileSystem (\r | |
1172 | IN EFI_HANDLE LoadFileHandle,\r | |
1173 | OUT EFI_HANDLE *RamDiskHandle\r | |
1174 | )\r | |
1175 | {\r | |
1176 | EFI_STATUS Status;\r | |
1177 | EFI_HANDLE Handle;\r | |
1178 | EFI_HANDLE *Handles;\r | |
1179 | UINTN HandleCount;\r | |
1180 | UINTN Index;\r | |
1181 | EFI_DEVICE_PATH_PROTOCOL *Node;\r | |
1182 | \r | |
1183 | Status = gBS->LocateHandleBuffer (\r | |
1184 | ByProtocol,\r | |
1185 | &gEfiBlockIoProtocolGuid,\r | |
1186 | NULL,\r | |
1187 | &HandleCount,\r | |
1188 | &Handles\r | |
1189 | );\r | |
1190 | if (EFI_ERROR (Status)) {\r | |
1191 | Handles = NULL;\r | |
1192 | HandleCount = 0;\r | |
1193 | }\r | |
1194 | \r | |
1195 | Handle = NULL;\r | |
1196 | for (Index = 0; Index < HandleCount; Index++) {\r | |
1197 | Node = DevicePathFromHandle (Handles[Index]);\r | |
1198 | Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle);\r | |
1199 | if (!EFI_ERROR (Status) &&\r | |
1200 | (Handle == LoadFileHandle) &&\r | |
1201 | (DevicePathType (Node) == MEDIA_DEVICE_PATH) && (DevicePathSubType (Node) == MEDIA_RAM_DISK_DP)) {\r | |
1202 | //\r | |
1203 | // Find the BlockIo instance populated from the LoadFile.\r | |
1204 | //\r | |
1205 | Handle = Handles[Index];\r | |
1206 | break;\r | |
1207 | }\r | |
1208 | }\r | |
1209 | \r | |
1210 | if (Handles != NULL) {\r | |
1211 | FreePool (Handles);\r | |
1212 | }\r | |
1213 | \r | |
1214 | if (Index == HandleCount) {\r | |
1215 | Handle = NULL;\r | |
1216 | }\r | |
1217 | \r | |
1218 | *RamDiskHandle = Handle;\r | |
1219 | \r | |
1220 | if (Handle != NULL) {\r | |
1221 | //\r | |
1222 | // Re-use BmExpandMediaDevicePath() to get the full device path of load option.\r | |
1223 | // But assume only one SimpleFileSystem can be found under the BlockIo.\r | |
1224 | //\r | |
1225 | return BmExpandMediaDevicePath (DevicePathFromHandle (Handle), NULL);\r | |
1226 | } else {\r | |
1227 | return NULL;\r | |
1228 | }\r | |
1229 | }\r | |
1230 | \r | |
1231 | /**\r | |
1232 | Return the RAM Disk device path created by LoadFile.\r | |
1233 | \r | |
1234 | @param FilePath The source file path.\r | |
1235 | \r | |
1236 | @return Callee-to-free RAM Disk device path\r | |
1237 | **/\r | |
1238 | EFI_DEVICE_PATH_PROTOCOL *\r | |
1239 | BmGetRamDiskDevicePath (\r | |
1240 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r | |
1241 | )\r | |
1242 | {\r | |
1243 | EFI_STATUS Status;\r | |
1244 | EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath;\r | |
1245 | EFI_DEVICE_PATH_PROTOCOL *Node;\r | |
1246 | EFI_HANDLE Handle;\r | |
1247 | \r | |
1248 | Node = FilePath;\r | |
1249 | Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle);\r | |
1250 | if (!EFI_ERROR (Status) &&\r | |
1251 | (DevicePathType (Node) == MEDIA_DEVICE_PATH) &&\r | |
1252 | (DevicePathSubType (Node) == MEDIA_RAM_DISK_DP)\r | |
1253 | ) {\r | |
1254 | \r | |
1255 | //\r | |
1256 | // Construct the device path pointing to RAM Disk\r | |
1257 | //\r | |
1258 | Node = NextDevicePathNode (Node);\r | |
1259 | RamDiskDevicePath = DuplicateDevicePath (FilePath);\r | |
1260 | ASSERT (RamDiskDevicePath != NULL);\r | |
1261 | SetDevicePathEndNode ((VOID *) ((UINTN) RamDiskDevicePath + ((UINTN) Node - (UINTN) FilePath)));\r | |
1262 | return RamDiskDevicePath;\r | |
1263 | }\r | |
1264 | \r | |
1265 | return NULL;\r | |
1266 | }\r | |
1267 | \r | |
1268 | /**\r | |
1269 | Return the buffer and buffer size occupied by the RAM Disk.\r | |
1270 | \r | |
1271 | @param RamDiskDevicePath RAM Disk device path.\r | |
1272 | @param RamDiskSizeInPages Return RAM Disk size in pages.\r | |
1273 | \r | |
1274 | @retval RAM Disk buffer.\r | |
1275 | **/\r | |
1276 | VOID *\r | |
1277 | BmGetRamDiskMemoryInfo (\r | |
1278 | IN EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath,\r | |
1279 | OUT UINTN *RamDiskSizeInPages\r | |
1280 | )\r | |
1281 | {\r | |
1282 | \r | |
1283 | EFI_STATUS Status;\r | |
1284 | EFI_HANDLE Handle;\r | |
1285 | UINT64 StartingAddr;\r | |
1286 | UINT64 EndingAddr;\r | |
1287 | \r | |
1288 | ASSERT (RamDiskDevicePath != NULL);\r | |
1289 | \r | |
1290 | *RamDiskSizeInPages = 0;\r | |
1291 | \r | |
1292 | //\r | |
1293 | // Get the buffer occupied by RAM Disk.\r | |
1294 | //\r | |
1295 | Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &RamDiskDevicePath, &Handle);\r | |
1296 | ASSERT_EFI_ERROR (Status);\r | |
1297 | ASSERT ((DevicePathType (RamDiskDevicePath) == MEDIA_DEVICE_PATH) &&\r | |
1298 | (DevicePathSubType (RamDiskDevicePath) == MEDIA_RAM_DISK_DP));\r | |
1299 | StartingAddr = ReadUnaligned64 ((UINT64 *) ((MEDIA_RAM_DISK_DEVICE_PATH *) RamDiskDevicePath)->StartingAddr);\r | |
1300 | EndingAddr = ReadUnaligned64 ((UINT64 *) ((MEDIA_RAM_DISK_DEVICE_PATH *) RamDiskDevicePath)->EndingAddr);\r | |
1301 | *RamDiskSizeInPages = EFI_SIZE_TO_PAGES ((UINTN) (EndingAddr - StartingAddr + 1));\r | |
1302 | return (VOID *) (UINTN) StartingAddr;\r | |
1303 | }\r | |
1304 | \r | |
1305 | /**\r | |
1306 | Destroy the RAM Disk.\r | |
1307 | \r | |
1308 | The destroy operation includes to call RamDisk.Unregister to\r | |
1309 | unregister the RAM DISK from RAM DISK driver, free the memory\r | |
1310 | allocated for the RAM Disk.\r | |
1311 | \r | |
1312 | @param RamDiskDevicePath RAM Disk device path.\r | |
1313 | **/\r | |
1314 | VOID\r | |
1315 | BmDestroyRamDisk (\r | |
1316 | IN EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath\r | |
1317 | )\r | |
1318 | {\r | |
1319 | EFI_STATUS Status;\r | |
1320 | VOID *RamDiskBuffer;\r | |
1321 | UINTN RamDiskSizeInPages;\r | |
1322 | \r | |
1323 | ASSERT (RamDiskDevicePath != NULL);\r | |
1324 | \r | |
1325 | RamDiskBuffer = BmGetRamDiskMemoryInfo (RamDiskDevicePath, &RamDiskSizeInPages);\r | |
1326 | \r | |
1327 | //\r | |
1328 | // Destroy RAM Disk.\r | |
1329 | //\r | |
1330 | if (mRamDisk == NULL) {\r | |
1331 | Status = gBS->LocateProtocol (&gEfiRamDiskProtocolGuid, NULL, (VOID *) &mRamDisk);\r | |
1332 | ASSERT_EFI_ERROR (Status);\r | |
1333 | }\r | |
1334 | Status = mRamDisk->Unregister (RamDiskDevicePath);\r | |
1335 | ASSERT_EFI_ERROR (Status);\r | |
1336 | FreePages (RamDiskBuffer, RamDiskSizeInPages);\r | |
1337 | }\r | |
1338 | \r | |
1339 | /**\r | |
1340 | Get the file buffer from the specified Load File instance.\r | |
1341 | \r | |
1342 | @param LoadFileHandle The specified Load File instance.\r | |
1343 | @param FilePath The file path which will pass to LoadFile().\r | |
1344 | \r | |
1345 | @return The full device path pointing to the load option buffer.\r | |
1346 | **/\r | |
1347 | EFI_DEVICE_PATH_PROTOCOL *\r | |
1348 | BmExpandLoadFile (\r | |
1349 | IN EFI_HANDLE LoadFileHandle,\r | |
1350 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r | |
1351 | )\r | |
1352 | {\r | |
1353 | EFI_STATUS Status;\r | |
1354 | EFI_LOAD_FILE_PROTOCOL *LoadFile;\r | |
1355 | VOID *FileBuffer;\r | |
1356 | EFI_HANDLE RamDiskHandle;\r | |
1357 | UINTN BufferSize;\r | |
1358 | EFI_DEVICE_PATH_PROTOCOL *FullPath;\r | |
1359 | \r | |
1360 | Status = gBS->OpenProtocol (\r | |
1361 | LoadFileHandle,\r | |
1362 | &gEfiLoadFileProtocolGuid,\r | |
1363 | (VOID **) &LoadFile,\r | |
1364 | gImageHandle,\r | |
1365 | NULL,\r | |
1366 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
1367 | );\r | |
1368 | ASSERT_EFI_ERROR (Status);\r | |
1369 | \r | |
1370 | FileBuffer = NULL;\r | |
1371 | BufferSize = 0;\r | |
1372 | Status = LoadFile->LoadFile (LoadFile, FilePath, TRUE, &BufferSize, FileBuffer);\r | |
1373 | if ((Status != EFI_WARN_FILE_SYSTEM) && (Status != EFI_BUFFER_TOO_SMALL)) {\r | |
1374 | return NULL;\r | |
1375 | }\r | |
1376 | \r | |
1377 | if (Status == EFI_BUFFER_TOO_SMALL) {\r | |
1378 | //\r | |
1379 | // The load option buffer is directly returned by LoadFile.\r | |
1380 | //\r | |
1381 | return DuplicateDevicePath (DevicePathFromHandle (LoadFileHandle));\r | |
1382 | }\r | |
1383 | \r | |
1384 | //\r | |
1385 | // The load option resides in a RAM disk.\r | |
1386 | //\r | |
1387 | FileBuffer = AllocateReservedPages (EFI_SIZE_TO_PAGES (BufferSize));\r | |
1388 | if (FileBuffer == NULL) {\r | |
1389 | return NULL;\r | |
1390 | }\r | |
1391 | \r | |
1392 | Status = LoadFile->LoadFile (LoadFile, FilePath, TRUE, &BufferSize, FileBuffer);\r | |
1393 | if (EFI_ERROR (Status)) {\r | |
1394 | FreePages (FileBuffer, EFI_SIZE_TO_PAGES (BufferSize));\r | |
1395 | return NULL;\r | |
1396 | }\r | |
1397 | \r | |
1398 | FullPath = BmExpandNetworkFileSystem (LoadFileHandle, &RamDiskHandle);\r | |
1399 | if (FullPath == NULL) {\r | |
1400 | //\r | |
1401 | // Free the memory occupied by the RAM disk if there is no BlockIo or SimpleFileSystem instance.\r | |
1402 | //\r | |
1403 | BmDestroyRamDisk (DevicePathFromHandle (RamDiskHandle));\r | |
1404 | }\r | |
1405 | \r | |
1406 | return FullPath;\r | |
1407 | }\r | |
1408 | \r | |
1409 | /**\r | |
1410 | Return the full device path pointing to the load option.\r | |
1411 | \r | |
1412 | FilePath may:\r | |
1413 | 1. Exactly matches to a LoadFile instance.\r | |
1414 | 2. Cannot match to any LoadFile instance. Wide match is required.\r | |
1415 | In either case, the routine may return:\r | |
1416 | 1. A copy of FilePath when FilePath matches to a LoadFile instance and\r | |
1417 | the LoadFile returns a load option buffer.\r | |
1418 | 2. A new device path with IP and URI information updated when wide match\r | |
1419 | happens.\r | |
1420 | 3. A new device path pointing to a load option in RAM disk.\r | |
1421 | In either case, only one full device path is returned for a specified\r | |
1422 | FilePath.\r | |
1423 | \r | |
1424 | @param FilePath The media device path pointing to a LoadFile instance.\r | |
1425 | \r | |
1426 | @return The load option buffer.\r | |
1427 | **/\r | |
1428 | EFI_DEVICE_PATH_PROTOCOL *\r | |
1429 | BmExpandLoadFiles (\r | |
1430 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r | |
1431 | )\r | |
1432 | {\r | |
1433 | EFI_STATUS Status;\r | |
1434 | EFI_HANDLE Handle;\r | |
1435 | EFI_HANDLE *Handles;\r | |
1436 | UINTN HandleCount;\r | |
1437 | UINTN Index;\r | |
1438 | EFI_DEVICE_PATH_PROTOCOL *Node;\r | |
1439 | \r | |
1440 | //\r | |
1441 | // Get file buffer from load file instance.\r | |
1442 | //\r | |
1443 | Node = FilePath;\r | |
1444 | Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle);\r | |
1445 | if (!EFI_ERROR (Status) && IsDevicePathEnd (Node)) {\r | |
1446 | //\r | |
1447 | // When wide match happens, pass full device path to LoadFile (),\r | |
1448 | // otherwise, pass remaining device path to LoadFile ().\r | |
1449 | //\r | |
1450 | FilePath = Node;\r | |
1451 | } else {\r | |
1452 | Handle = NULL;\r | |
1453 | //\r | |
1454 | // Use wide match algorithm to find one when\r | |
1455 | // cannot find a LoadFile instance to exactly match the FilePath\r | |
1456 | //\r | |
1457 | Status = gBS->LocateHandleBuffer (\r | |
1458 | ByProtocol,\r | |
1459 | &gEfiLoadFileProtocolGuid,\r | |
1460 | NULL,\r | |
1461 | &HandleCount,\r | |
1462 | &Handles\r | |
1463 | );\r | |
1464 | if (EFI_ERROR (Status)) {\r | |
1465 | Handles = NULL;\r | |
1466 | HandleCount = 0;\r | |
1467 | }\r | |
1468 | for (Index = 0; Index < HandleCount; Index++) {\r | |
1469 | if (BmMatchHttpBootDevicePath (DevicePathFromHandle (Handles[Index]), FilePath)) {\r | |
1470 | Handle = Handles[Index];\r | |
1471 | break;\r | |
1472 | }\r | |
1473 | }\r | |
1474 | if (Handles != NULL) {\r | |
1475 | FreePool (Handles);\r | |
1476 | }\r | |
1477 | }\r | |
1478 | \r | |
1479 | if (Handle == NULL) {\r | |
1480 | return NULL;\r | |
1481 | }\r | |
1482 | \r | |
1483 | return BmExpandLoadFile (Handle, FilePath);\r | |
1484 | }\r | |
1485 | \r | |
1486 | /**\r | |
1487 | Get the load option by its device path.\r | |
1488 | \r | |
1489 | @param FilePath The device path pointing to a load option.\r | |
1490 | It could be a short-form device path.\r | |
1491 | @param FullPath Return the full device path of the load option after\r | |
1492 | short-form device path expanding.\r | |
1493 | Caller is responsible to free it.\r | |
1494 | @param FileSize Return the load option size.\r | |
1495 | \r | |
1496 | @return The load option buffer. Caller is responsible to free the memory.\r | |
1497 | **/\r | |
1498 | VOID *\r | |
1499 | EFIAPI\r | |
1500 | EfiBootManagerGetLoadOptionBuffer (\r | |
1501 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r | |
1502 | OUT EFI_DEVICE_PATH_PROTOCOL **FullPath,\r | |
1503 | OUT UINTN *FileSize\r | |
1504 | )\r | |
1505 | {\r | |
1506 | *FullPath = NULL;\r | |
1507 | \r | |
1508 | EfiBootManagerConnectDevicePath (FilePath, NULL);\r | |
1509 | return BmGetNextLoadOptionBuffer (LoadOptionTypeMax, FilePath, FullPath, FileSize);\r | |
1510 | }\r | |
1511 | \r | |
1512 | /**\r | |
1513 | Get the next possible full path pointing to the load option.\r | |
1514 | The routine doesn't guarantee the returned full path points to an existing\r | |
1515 | file, and it also doesn't guarantee the existing file is a valid load option.\r | |
1516 | BmGetNextLoadOptionBuffer() guarantees.\r | |
1517 | \r | |
1518 | @param FilePath The device path pointing to a load option.\r | |
1519 | It could be a short-form device path.\r | |
1520 | @param FullPath The full path returned by the routine in last call.\r | |
1521 | Set to NULL in first call.\r | |
1522 | \r | |
1523 | @return The next possible full path pointing to the load option.\r | |
1524 | Caller is responsible to free the memory.\r | |
1525 | **/\r | |
1526 | EFI_DEVICE_PATH_PROTOCOL *\r | |
1527 | BmGetNextLoadOptionDevicePath (\r | |
1528 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r | |
1529 | IN EFI_DEVICE_PATH_PROTOCOL *FullPath\r | |
1530 | )\r | |
1531 | {\r | |
1532 | EFI_HANDLE Handle;\r | |
1533 | EFI_DEVICE_PATH_PROTOCOL *Node;\r | |
1534 | EFI_STATUS Status;\r | |
1535 | \r | |
1536 | ASSERT (FilePath != NULL);\r | |
1537 | \r | |
1538 | //\r | |
1539 | // Boot from media device by adding a default file name \EFI\BOOT\BOOT{machine type short-name}.EFI\r | |
1540 | //\r | |
1541 | Node = FilePath;\r | |
1542 | Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &Node, &Handle);\r | |
1543 | if (EFI_ERROR (Status)) {\r | |
1544 | Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &Node, &Handle);\r | |
1545 | }\r | |
1546 | \r | |
1547 | if (!EFI_ERROR (Status) && IsDevicePathEnd (Node)) {\r | |
1548 | return BmExpandMediaDevicePath (FilePath, FullPath);\r | |
1549 | }\r | |
1550 | \r | |
1551 | //\r | |
1552 | // Expand the short-form device path to full device path\r | |
1553 | //\r | |
1554 | if ((DevicePathType (FilePath) == MEDIA_DEVICE_PATH) &&\r | |
1555 | (DevicePathSubType (FilePath) == MEDIA_HARDDRIVE_DP)) {\r | |
1556 | //\r | |
1557 | // Expand the Harddrive device path\r | |
1558 | //\r | |
1559 | if (FullPath == NULL) {\r | |
1560 | return BmExpandPartitionDevicePath (FilePath);\r | |
1561 | } else {\r | |
1562 | return NULL;\r | |
1563 | }\r | |
1564 | } else if ((DevicePathType (FilePath) == MEDIA_DEVICE_PATH) &&\r | |
1565 | (DevicePathSubType (FilePath) == MEDIA_FILEPATH_DP)) {\r | |
1566 | //\r | |
1567 | // Expand the File-path device path\r | |
1568 | //\r | |
1569 | return BmExpandFileDevicePath (FilePath, FullPath);\r | |
1570 | } else if ((DevicePathType (FilePath) == MESSAGING_DEVICE_PATH) &&\r | |
1571 | (DevicePathSubType (FilePath) == MSG_URI_DP)) {\r | |
1572 | //\r | |
1573 | // Expand the URI device path\r | |
1574 | //\r | |
1575 | return BmExpandUriDevicePath (FilePath, FullPath);\r | |
1576 | } else {\r | |
1577 | Node = FilePath;\r | |
1578 | Status = gBS->LocateDevicePath (&gEfiUsbIoProtocolGuid, &Node, &Handle);\r | |
1579 | if (EFI_ERROR (Status)) {\r | |
1580 | //\r | |
1581 | // Only expand the USB WWID/Class device path\r | |
1582 | // when FilePath doesn't point to a physical UsbIo controller.\r | |
1583 | // Otherwise, infinite recursion will happen.\r | |
1584 | //\r | |
1585 | for (Node = FilePath; !IsDevicePathEnd (Node); Node = NextDevicePathNode (Node)) {\r | |
1586 | if ((DevicePathType (Node) == MESSAGING_DEVICE_PATH) &&\r | |
1587 | ((DevicePathSubType (Node) == MSG_USB_CLASS_DP) || (DevicePathSubType (Node) == MSG_USB_WWID_DP))) {\r | |
1588 | break;\r | |
1589 | }\r | |
1590 | }\r | |
1591 | \r | |
1592 | //\r | |
1593 | // Expand the USB WWID/Class device path\r | |
1594 | //\r | |
1595 | if (!IsDevicePathEnd (Node)) {\r | |
1596 | if (FilePath == Node) {\r | |
1597 | //\r | |
1598 | // Boot Option device path starts with USB Class or USB WWID device path.\r | |
1599 | // For Boot Option device path which doesn't begin with the USB Class or\r | |
1600 | // USB WWID device path, it's not needed to connect again here.\r | |
1601 | //\r | |
1602 | BmConnectUsbShortFormDevicePath (FilePath);\r | |
1603 | }\r | |
1604 | return BmExpandUsbDevicePath (FilePath, FullPath, Node);\r | |
1605 | }\r | |
1606 | }\r | |
1607 | }\r | |
1608 | \r | |
1609 | //\r | |
1610 | // For the below cases, FilePath only expands to one Full path.\r | |
1611 | // So just handle the case when FullPath == NULL.\r | |
1612 | //\r | |
1613 | if (FullPath != NULL) {\r | |
1614 | return NULL;\r | |
1615 | }\r | |
1616 | \r | |
1617 | //\r | |
1618 | // Load option resides in FV.\r | |
1619 | //\r | |
1620 | if (BmIsFvFilePath (FilePath)) {\r | |
1621 | return BmAdjustFvFilePath (FilePath);\r | |
1622 | }\r | |
1623 | \r | |
1624 | //\r | |
1625 | // Load option resides in Simple File System.\r | |
1626 | //\r | |
1627 | Node = FilePath;\r | |
1628 | Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &Node, &Handle);\r | |
1629 | if (!EFI_ERROR (Status)) {\r | |
1630 | return DuplicateDevicePath (FilePath);\r | |
1631 | }\r | |
1632 | \r | |
1633 | //\r | |
1634 | // Last chance to try: Load option may be loaded through LoadFile.\r | |
1635 | //\r | |
1636 | return BmExpandLoadFiles (FilePath);\r | |
1637 | }\r | |
1638 | \r | |
1639 | /**\r | |
1640 | Check if it's a Device Path pointing to BootManagerMenu.\r | |
1641 | \r | |
1642 | @param DevicePath Input device path.\r | |
1643 | \r | |
1644 | @retval TRUE The device path is BootManagerMenu File Device Path.\r | |
1645 | @retval FALSE The device path is NOT BootManagerMenu File Device Path.\r | |
1646 | **/\r | |
1647 | BOOLEAN\r | |
1648 | BmIsBootManagerMenuFilePath (\r | |
1649 | EFI_DEVICE_PATH_PROTOCOL *DevicePath\r | |
1650 | )\r | |
1651 | {\r | |
1652 | EFI_HANDLE FvHandle;\r | |
1653 | VOID *NameGuid;\r | |
1654 | EFI_STATUS Status;\r | |
1655 | \r | |
1656 | Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePath, &FvHandle);\r | |
1657 | if (!EFI_ERROR (Status)) {\r | |
1658 | NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePath);\r | |
1659 | if (NameGuid != NULL) {\r | |
1660 | return CompareGuid (NameGuid, PcdGetPtr (PcdBootManagerMenuFile));\r | |
1661 | }\r | |
1662 | }\r | |
1663 | \r | |
1664 | return FALSE;\r | |
1665 | }\r | |
1666 | \r | |
1667 | /**\r | |
1668 | Report status code with EFI_RETURN_STATUS_EXTENDED_DATA about LoadImage() or\r | |
1669 | StartImage() failure.\r | |
1670 | \r | |
1671 | @param[in] ErrorCode An Error Code in the Software Class, DXE Boot\r | |
1672 | Service Driver Subclass. ErrorCode will be used to\r | |
1673 | compose the Value parameter for status code\r | |
1674 | reporting. Must be one of\r | |
1675 | EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR and\r | |
1676 | EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED.\r | |
1677 | \r | |
1678 | @param[in] FailureStatus The failure status returned by the boot service\r | |
1679 | that should be reported.\r | |
1680 | **/\r | |
1681 | VOID\r | |
1682 | BmReportLoadFailure (\r | |
1683 | IN UINT32 ErrorCode,\r | |
1684 | IN EFI_STATUS FailureStatus\r | |
1685 | )\r | |
1686 | {\r | |
1687 | EFI_RETURN_STATUS_EXTENDED_DATA ExtendedData;\r | |
1688 | \r | |
1689 | if (!ReportErrorCodeEnabled ()) {\r | |
1690 | return;\r | |
1691 | }\r | |
1692 | \r | |
1693 | ASSERT (\r | |
1694 | (ErrorCode == EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR) ||\r | |
1695 | (ErrorCode == EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED)\r | |
1696 | );\r | |
1697 | \r | |
1698 | ZeroMem (&ExtendedData, sizeof (ExtendedData));\r | |
1699 | ExtendedData.ReturnStatus = FailureStatus;\r | |
1700 | \r | |
1701 | REPORT_STATUS_CODE_EX (\r | |
1702 | (EFI_ERROR_CODE | EFI_ERROR_MINOR),\r | |
1703 | (EFI_SOFTWARE_DXE_BS_DRIVER | ErrorCode),\r | |
1704 | 0,\r | |
1705 | NULL,\r | |
1706 | NULL,\r | |
1707 | &ExtendedData.DataHeader + 1,\r | |
1708 | sizeof (ExtendedData) - sizeof (ExtendedData.DataHeader)\r | |
1709 | );\r | |
1710 | }\r | |
1711 | \r | |
1712 | /**\r | |
1713 | Attempt to boot the EFI boot option. This routine sets L"BootCurent" and\r | |
1714 | also signals the EFI ready to boot event. If the device path for the option\r | |
1715 | starts with a BBS device path a legacy boot is attempted via the registered\r | |
1716 | gLegacyBoot function. Short form device paths are also supported via this\r | |
1717 | rountine. A device path starting with MEDIA_HARDDRIVE_DP, MSG_USB_WWID_DP,\r | |
1718 | MSG_USB_CLASS_DP gets expaned out to find the first device that matches.\r | |
1719 | If the BootOption Device Path fails the removable media boot algorithm\r | |
1720 | is attempted (\EFI\BOOTIA32.EFI, \EFI\BOOTX64.EFI,... only one file type\r | |
1721 | is tried per processor type)\r | |
1722 | \r | |
1723 | @param BootOption Boot Option to try and boot.\r | |
1724 | On return, BootOption->Status contains the boot status.\r | |
1725 | EFI_SUCCESS BootOption was booted\r | |
1726 | EFI_UNSUPPORTED A BBS device path was found with no valid callback\r | |
1727 | registered via EfiBootManagerInitialize().\r | |
1728 | EFI_NOT_FOUND The BootOption was not found on the system\r | |
1729 | !EFI_SUCCESS BootOption failed with this error status\r | |
1730 | \r | |
1731 | **/\r | |
1732 | VOID\r | |
1733 | EFIAPI\r | |
1734 | EfiBootManagerBoot (\r | |
1735 | IN EFI_BOOT_MANAGER_LOAD_OPTION *BootOption\r | |
1736 | )\r | |
1737 | {\r | |
1738 | EFI_STATUS Status;\r | |
1739 | EFI_HANDLE ImageHandle;\r | |
1740 | EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r | |
1741 | UINT16 Uint16;\r | |
1742 | UINTN OptionNumber;\r | |
1743 | UINTN OriginalOptionNumber;\r | |
1744 | EFI_DEVICE_PATH_PROTOCOL *FilePath;\r | |
1745 | EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath;\r | |
1746 | VOID *FileBuffer;\r | |
1747 | UINTN FileSize;\r | |
1748 | EFI_BOOT_LOGO_PROTOCOL *BootLogo;\r | |
1749 | EFI_EVENT LegacyBootEvent;\r | |
1750 | \r | |
1751 | if (BootOption == NULL) {\r | |
1752 | return;\r | |
1753 | }\r | |
1754 | \r | |
1755 | if (BootOption->FilePath == NULL || BootOption->OptionType != LoadOptionTypeBoot) {\r | |
1756 | BootOption->Status = EFI_INVALID_PARAMETER;\r | |
1757 | return;\r | |
1758 | }\r | |
1759 | \r | |
1760 | //\r | |
1761 | // 1. Create Boot#### for a temporary boot if there is no match Boot#### (i.e. a boot by selected a EFI Shell using "Boot From File")\r | |
1762 | //\r | |
1763 | OptionNumber = BmFindBootOptionInVariable (BootOption);\r | |
1764 | if (OptionNumber == LoadOptionNumberUnassigned) {\r | |
1765 | Status = BmGetFreeOptionNumber (LoadOptionTypeBoot, &Uint16);\r | |
1766 | if (!EFI_ERROR (Status)) {\r | |
1767 | //\r | |
1768 | // Save the BootOption->OptionNumber to restore later\r | |
1769 | //\r | |
1770 | OptionNumber = Uint16;\r | |
1771 | OriginalOptionNumber = BootOption->OptionNumber;\r | |
1772 | BootOption->OptionNumber = OptionNumber;\r | |
1773 | Status = EfiBootManagerLoadOptionToVariable (BootOption);\r | |
1774 | BootOption->OptionNumber = OriginalOptionNumber;\r | |
1775 | }\r | |
1776 | \r | |
1777 | if (EFI_ERROR (Status)) {\r | |
1778 | DEBUG ((EFI_D_ERROR, "[Bds] Failed to create Boot#### for a temporary boot - %r!\n", Status));\r | |
1779 | BootOption->Status = Status;\r | |
1780 | return ;\r | |
1781 | }\r | |
1782 | }\r | |
1783 | \r | |
1784 | //\r | |
1785 | // 2. Set BootCurrent\r | |
1786 | //\r | |
1787 | Uint16 = (UINT16) OptionNumber;\r | |
1788 | BmSetVariableAndReportStatusCodeOnError (\r | |
1789 | L"BootCurrent",\r | |
1790 | &gEfiGlobalVariableGuid,\r | |
1791 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
1792 | sizeof (UINT16),\r | |
1793 | &Uint16\r | |
1794 | );\r | |
1795 | \r | |
1796 | //\r | |
1797 | // 3. Signal the EVT_SIGNAL_READY_TO_BOOT event when we are about to load and execute\r | |
1798 | // the boot option.\r | |
1799 | //\r | |
1800 | if (BmIsBootManagerMenuFilePath (BootOption->FilePath)) {\r | |
1801 | DEBUG ((EFI_D_INFO, "[Bds] Booting Boot Manager Menu.\n"));\r | |
1802 | BmStopHotkeyService (NULL, NULL);\r | |
1803 | } else {\r | |
1804 | EfiSignalEventReadyToBoot();\r | |
1805 | //\r | |
1806 | // Report Status Code to indicate ReadyToBoot was signalled\r | |
1807 | //\r | |
1808 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT));\r | |
1809 | //\r | |
1810 | // 4. Repair system through DriverHealth protocol\r | |
1811 | //\r | |
1812 | BmRepairAllControllers (0);\r | |
1813 | }\r | |
1814 | \r | |
1815 | PERF_START_EX (gImageHandle, "BdsAttempt", NULL, 0, (UINT32) OptionNumber);\r | |
1816 | \r | |
1817 | //\r | |
1818 | // 5. Adjust the different type memory page number just before booting\r | |
1819 | // and save the updated info into the variable for next boot to use\r | |
1820 | //\r | |
1821 | BmSetMemoryTypeInformationVariable (\r | |
1822 | (BOOLEAN) ((BootOption->Attributes & LOAD_OPTION_CATEGORY) == LOAD_OPTION_CATEGORY_BOOT)\r | |
1823 | );\r | |
1824 | \r | |
1825 | //\r | |
1826 | // 6. Load EFI boot option to ImageHandle\r | |
1827 | //\r | |
1828 | DEBUG_CODE_BEGIN ();\r | |
1829 | if (BootOption->Description == NULL) {\r | |
1830 | DEBUG ((DEBUG_INFO | DEBUG_LOAD, "[Bds]Booting from unknown device path\n"));\r | |
1831 | } else {\r | |
1832 | DEBUG ((DEBUG_INFO | DEBUG_LOAD, "[Bds]Booting %s\n", BootOption->Description));\r | |
1833 | }\r | |
1834 | DEBUG_CODE_END ();\r | |
1835 | \r | |
1836 | ImageHandle = NULL;\r | |
1837 | RamDiskDevicePath = NULL;\r | |
1838 | if (DevicePathType (BootOption->FilePath) != BBS_DEVICE_PATH) {\r | |
1839 | Status = EFI_NOT_FOUND;\r | |
1840 | FilePath = NULL;\r | |
1841 | EfiBootManagerConnectDevicePath (BootOption->FilePath, NULL);\r | |
1842 | FileBuffer = BmGetNextLoadOptionBuffer (LoadOptionTypeBoot, BootOption->FilePath, &FilePath, &FileSize);\r | |
1843 | if (FileBuffer != NULL) {\r | |
1844 | RamDiskDevicePath = BmGetRamDiskDevicePath (FilePath);\r | |
1845 | \r | |
1846 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r | |
1847 | Status = gBS->LoadImage (\r | |
1848 | TRUE,\r | |
1849 | gImageHandle,\r | |
1850 | FilePath,\r | |
1851 | FileBuffer,\r | |
1852 | FileSize,\r | |
1853 | &ImageHandle\r | |
1854 | );\r | |
1855 | }\r | |
1856 | if (FileBuffer != NULL) {\r | |
1857 | FreePool (FileBuffer);\r | |
1858 | }\r | |
1859 | if (FilePath != NULL) {\r | |
1860 | FreePool (FilePath);\r | |
1861 | }\r | |
1862 | \r | |
1863 | if (EFI_ERROR (Status)) {\r | |
1864 | //\r | |
1865 | // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created\r | |
1866 | // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.\r | |
1867 | // If the caller doesn't have the option to defer the execution of an image, we should\r | |
1868 | // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.\r | |
1869 | //\r | |
1870 | if (Status == EFI_SECURITY_VIOLATION) {\r | |
1871 | gBS->UnloadImage (ImageHandle);\r | |
1872 | }\r | |
1873 | //\r | |
1874 | // Report Status Code with the failure status to indicate that the failure to load boot option\r | |
1875 | //\r | |
1876 | BmReportLoadFailure (EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR, Status);\r | |
1877 | BootOption->Status = Status;\r | |
1878 | //\r | |
1879 | // Destroy the RAM disk\r | |
1880 | //\r | |
1881 | if (RamDiskDevicePath != NULL) {\r | |
1882 | BmDestroyRamDisk (RamDiskDevicePath);\r | |
1883 | FreePool (RamDiskDevicePath);\r | |
1884 | }\r | |
1885 | return;\r | |
1886 | }\r | |
1887 | }\r | |
1888 | \r | |
1889 | //\r | |
1890 | // Check to see if we should legacy BOOT. If yes then do the legacy boot\r | |
1891 | // Write boot to OS performance data for Legacy boot\r | |
1892 | //\r | |
1893 | if ((DevicePathType (BootOption->FilePath) == BBS_DEVICE_PATH) && (DevicePathSubType (BootOption->FilePath) == BBS_BBS_DP)) {\r | |
1894 | if (mBmLegacyBoot != NULL) {\r | |
1895 | //\r | |
1896 | // Write boot to OS performance data for legacy boot.\r | |
1897 | //\r | |
1898 | PERF_CODE (\r | |
1899 | //\r | |
1900 | // Create an event to be signalled when Legacy Boot occurs to write performance data.\r | |
1901 | //\r | |
1902 | Status = EfiCreateEventLegacyBootEx(\r | |
1903 | TPL_NOTIFY,\r | |
1904 | BmEndOfBdsPerfCode,\r | |
1905 | NULL,\r | |
1906 | &LegacyBootEvent\r | |
1907 | );\r | |
1908 | ASSERT_EFI_ERROR (Status);\r | |
1909 | );\r | |
1910 | \r | |
1911 | mBmLegacyBoot (BootOption);\r | |
1912 | } else {\r | |
1913 | BootOption->Status = EFI_UNSUPPORTED;\r | |
1914 | }\r | |
1915 | \r | |
1916 | PERF_END_EX (gImageHandle, "BdsAttempt", NULL, 0, (UINT32) OptionNumber);\r | |
1917 | return;\r | |
1918 | }\r | |
1919 | \r | |
1920 | //\r | |
1921 | // Provide the image with its load options\r | |
1922 | //\r | |
1923 | Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r | |
1924 | ASSERT_EFI_ERROR (Status);\r | |
1925 | \r | |
1926 | if (!BmIsAutoCreateBootOption (BootOption)) {\r | |
1927 | ImageInfo->LoadOptionsSize = BootOption->OptionalDataSize;\r | |
1928 | ImageInfo->LoadOptions = BootOption->OptionalData;\r | |
1929 | }\r | |
1930 | \r | |
1931 | //\r | |
1932 | // Clean to NULL because the image is loaded directly from the firmwares boot manager.\r | |
1933 | //\r | |
1934 | ImageInfo->ParentHandle = NULL;\r | |
1935 | \r | |
1936 | //\r | |
1937 | // Before calling the image, enable the Watchdog Timer for 5 minutes period\r | |
1938 | //\r | |
1939 | gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r | |
1940 | \r | |
1941 | //\r | |
1942 | // Write boot to OS performance data for UEFI boot\r | |
1943 | //\r | |
1944 | PERF_CODE (\r | |
1945 | BmEndOfBdsPerfCode (NULL, NULL);\r | |
1946 | );\r | |
1947 | \r | |
1948 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderStart));\r | |
1949 | \r | |
1950 | Status = gBS->StartImage (ImageHandle, &BootOption->ExitDataSize, &BootOption->ExitData);\r | |
1951 | DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));\r | |
1952 | BootOption->Status = Status;\r | |
1953 | if (EFI_ERROR (Status)) {\r | |
1954 | //\r | |
1955 | // Report Status Code with the failure status to indicate that boot failure\r | |
1956 | //\r | |
1957 | BmReportLoadFailure (EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED, Status);\r | |
1958 | }\r | |
1959 | PERF_END_EX (gImageHandle, "BdsAttempt", NULL, 0, (UINT32) OptionNumber);\r | |
1960 | \r | |
1961 | //\r | |
1962 | // Destroy the RAM disk\r | |
1963 | //\r | |
1964 | if (RamDiskDevicePath != NULL) {\r | |
1965 | BmDestroyRamDisk (RamDiskDevicePath);\r | |
1966 | FreePool (RamDiskDevicePath);\r | |
1967 | }\r | |
1968 | \r | |
1969 | //\r | |
1970 | // Clear the Watchdog Timer after the image returns\r | |
1971 | //\r | |
1972 | gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r | |
1973 | \r | |
1974 | //\r | |
1975 | // Set Logo status invalid after trying one boot option\r | |
1976 | //\r | |
1977 | BootLogo = NULL;\r | |
1978 | Status = gBS->LocateProtocol (&gEfiBootLogoProtocolGuid, NULL, (VOID **) &BootLogo);\r | |
1979 | if (!EFI_ERROR (Status) && (BootLogo != NULL)) {\r | |
1980 | Status = BootLogo->SetBootLogo (BootLogo, NULL, 0, 0, 0, 0);\r | |
1981 | ASSERT_EFI_ERROR (Status);\r | |
1982 | }\r | |
1983 | \r | |
1984 | //\r | |
1985 | // Clear Boot Current\r | |
1986 | //\r | |
1987 | Status = gRT->SetVariable (\r | |
1988 | L"BootCurrent",\r | |
1989 | &gEfiGlobalVariableGuid,\r | |
1990 | 0,\r | |
1991 | 0,\r | |
1992 | NULL\r | |
1993 | );\r | |
1994 | //\r | |
1995 | // Deleting variable with current variable implementation shouldn't fail.\r | |
1996 | // When BootXXXX (e.g.: BootManagerMenu) boots BootYYYY, exiting BootYYYY causes BootCurrent deleted,\r | |
1997 | // exiting BootXXXX causes deleting BootCurrent returns EFI_NOT_FOUND.\r | |
1998 | //\r | |
1999 | ASSERT (Status == EFI_SUCCESS || Status == EFI_NOT_FOUND);\r | |
2000 | }\r | |
2001 | \r | |
2002 | /**\r | |
2003 | Check whether there is a instance in BlockIoDevicePath, which contain multi device path\r | |
2004 | instances, has the same partition node with HardDriveDevicePath device path\r | |
2005 | \r | |
2006 | @param BlockIoDevicePath Multi device path instances which need to check\r | |
2007 | @param HardDriveDevicePath A device path which starts with a hard drive media\r | |
2008 | device path.\r | |
2009 | \r | |
2010 | @retval TRUE There is a matched device path instance.\r | |
2011 | @retval FALSE There is no matched device path instance.\r | |
2012 | \r | |
2013 | **/\r | |
2014 | BOOLEAN\r | |
2015 | BmMatchPartitionDevicePathNode (\r | |
2016 | IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,\r | |
2017 | IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r | |
2018 | )\r | |
2019 | {\r | |
2020 | HARDDRIVE_DEVICE_PATH *Node;\r | |
2021 | \r | |
2022 | if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {\r | |
2023 | return FALSE;\r | |
2024 | }\r | |
2025 | \r | |
2026 | //\r | |
2027 | // Match all the partition device path nodes including the nested partition nodes\r | |
2028 | //\r | |
2029 | while (!IsDevicePathEnd (BlockIoDevicePath)) {\r | |
2030 | if ((DevicePathType (BlockIoDevicePath) == MEDIA_DEVICE_PATH) &&\r | |
2031 | (DevicePathSubType (BlockIoDevicePath) == MEDIA_HARDDRIVE_DP)\r | |
2032 | ) {\r | |
2033 | //\r | |
2034 | // See if the harddrive device path in blockio matches the orig Hard Drive Node\r | |
2035 | //\r | |
2036 | Node = (HARDDRIVE_DEVICE_PATH *) BlockIoDevicePath;\r | |
2037 | \r | |
2038 | //\r | |
2039 | // Match Signature and PartitionNumber.\r | |
2040 | // Unused bytes in Signature are initiaized with zeros.\r | |
2041 | //\r | |
2042 | if ((Node->PartitionNumber == HardDriveDevicePath->PartitionNumber) &&\r | |
2043 | (Node->MBRType == HardDriveDevicePath->MBRType) &&\r | |
2044 | (Node->SignatureType == HardDriveDevicePath->SignatureType) &&\r | |
2045 | (CompareMem (Node->Signature, HardDriveDevicePath->Signature, sizeof (Node->Signature)) == 0)) {\r | |
2046 | return TRUE;\r | |
2047 | }\r | |
2048 | }\r | |
2049 | \r | |
2050 | BlockIoDevicePath = NextDevicePathNode (BlockIoDevicePath);\r | |
2051 | }\r | |
2052 | \r | |
2053 | return FALSE;\r | |
2054 | }\r | |
2055 | \r | |
2056 | /**\r | |
2057 | Emuerate all possible bootable medias in the following order:\r | |
2058 | 1. Removable BlockIo - The boot option only points to the removable media\r | |
2059 | device, like USB key, DVD, Floppy etc.\r | |
2060 | 2. Fixed BlockIo - The boot option only points to a Fixed blockIo device,\r | |
2061 | like HardDisk.\r | |
2062 | 3. Non-BlockIo SimpleFileSystem - The boot option points to a device supporting\r | |
2063 | SimpleFileSystem Protocol, but not supporting BlockIo\r | |
2064 | protocol.\r | |
2065 | 4. LoadFile - The boot option points to the media supporting\r | |
2066 | LoadFile protocol.\r | |
2067 | Reference: UEFI Spec chapter 3.3 Boot Option Variables Default Boot Behavior\r | |
2068 | \r | |
2069 | @param BootOptionCount Return the boot option count which has been found.\r | |
2070 | \r | |
2071 | @retval Pointer to the boot option array.\r | |
2072 | **/\r | |
2073 | EFI_BOOT_MANAGER_LOAD_OPTION *\r | |
2074 | BmEnumerateBootOptions (\r | |
2075 | UINTN *BootOptionCount\r | |
2076 | )\r | |
2077 | {\r | |
2078 | EFI_STATUS Status;\r | |
2079 | EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;\r | |
2080 | UINTN HandleCount;\r | |
2081 | EFI_HANDLE *Handles;\r | |
2082 | EFI_BLOCK_IO_PROTOCOL *BlkIo;\r | |
2083 | UINTN Removable;\r | |
2084 | UINTN Index;\r | |
2085 | CHAR16 *Description;\r | |
2086 | \r | |
2087 | ASSERT (BootOptionCount != NULL);\r | |
2088 | \r | |
2089 | *BootOptionCount = 0;\r | |
2090 | BootOptions = NULL;\r | |
2091 | \r | |
2092 | //\r | |
2093 | // Parse removable block io followed by fixed block io\r | |
2094 | //\r | |
2095 | gBS->LocateHandleBuffer (\r | |
2096 | ByProtocol,\r | |
2097 | &gEfiBlockIoProtocolGuid,\r | |
2098 | NULL,\r | |
2099 | &HandleCount,\r | |
2100 | &Handles\r | |
2101 | );\r | |
2102 | \r | |
2103 | for (Removable = 0; Removable < 2; Removable++) {\r | |
2104 | for (Index = 0; Index < HandleCount; Index++) {\r | |
2105 | Status = gBS->HandleProtocol (\r | |
2106 | Handles[Index],\r | |
2107 | &gEfiBlockIoProtocolGuid,\r | |
2108 | (VOID **) &BlkIo\r | |
2109 | );\r | |
2110 | if (EFI_ERROR (Status)) {\r | |
2111 | continue;\r | |
2112 | }\r | |
2113 | \r | |
2114 | //\r | |
2115 | // Skip the logical partitions\r | |
2116 | //\r | |
2117 | if (BlkIo->Media->LogicalPartition) {\r | |
2118 | continue;\r | |
2119 | }\r | |
2120 | \r | |
2121 | //\r | |
2122 | // Skip the fixed block io then the removable block io\r | |
2123 | //\r | |
2124 | if (BlkIo->Media->RemovableMedia == ((Removable == 0) ? FALSE : TRUE)) {\r | |
2125 | continue;\r | |
2126 | }\r | |
2127 | \r | |
2128 | Description = BmGetBootDescription (Handles[Index]);\r | |
2129 | BootOptions = ReallocatePool (\r | |
2130 | sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount),\r | |
2131 | sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount + 1),\r | |
2132 | BootOptions\r | |
2133 | );\r | |
2134 | ASSERT (BootOptions != NULL);\r | |
2135 | \r | |
2136 | Status = EfiBootManagerInitializeLoadOption (\r | |
2137 | &BootOptions[(*BootOptionCount)++],\r | |
2138 | LoadOptionNumberUnassigned,\r | |
2139 | LoadOptionTypeBoot,\r | |
2140 | LOAD_OPTION_ACTIVE,\r | |
2141 | Description,\r | |
2142 | DevicePathFromHandle (Handles[Index]),\r | |
2143 | NULL,\r | |
2144 | 0\r | |
2145 | );\r | |
2146 | ASSERT_EFI_ERROR (Status);\r | |
2147 | \r | |
2148 | FreePool (Description);\r | |
2149 | }\r | |
2150 | }\r | |
2151 | \r | |
2152 | if (HandleCount != 0) {\r | |
2153 | FreePool (Handles);\r | |
2154 | }\r | |
2155 | \r | |
2156 | //\r | |
2157 | // Parse simple file system not based on block io\r | |
2158 | //\r | |
2159 | gBS->LocateHandleBuffer (\r | |
2160 | ByProtocol,\r | |
2161 | &gEfiSimpleFileSystemProtocolGuid,\r | |
2162 | NULL,\r | |
2163 | &HandleCount,\r | |
2164 | &Handles\r | |
2165 | );\r | |
2166 | for (Index = 0; Index < HandleCount; Index++) {\r | |
2167 | Status = gBS->HandleProtocol (\r | |
2168 | Handles[Index],\r | |
2169 | &gEfiBlockIoProtocolGuid,\r | |
2170 | (VOID **) &BlkIo\r | |
2171 | );\r | |
2172 | if (!EFI_ERROR (Status)) {\r | |
2173 | //\r | |
2174 | // Skip if the file system handle supports a BlkIo protocol, which we've handled in above\r | |
2175 | //\r | |
2176 | continue;\r | |
2177 | }\r | |
2178 | Description = BmGetBootDescription (Handles[Index]);\r | |
2179 | BootOptions = ReallocatePool (\r | |
2180 | sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount),\r | |
2181 | sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount + 1),\r | |
2182 | BootOptions\r | |
2183 | );\r | |
2184 | ASSERT (BootOptions != NULL);\r | |
2185 | \r | |
2186 | Status = EfiBootManagerInitializeLoadOption (\r | |
2187 | &BootOptions[(*BootOptionCount)++],\r | |
2188 | LoadOptionNumberUnassigned,\r | |
2189 | LoadOptionTypeBoot,\r | |
2190 | LOAD_OPTION_ACTIVE,\r | |
2191 | Description,\r | |
2192 | DevicePathFromHandle (Handles[Index]),\r | |
2193 | NULL,\r | |
2194 | 0\r | |
2195 | );\r | |
2196 | ASSERT_EFI_ERROR (Status);\r | |
2197 | FreePool (Description);\r | |
2198 | }\r | |
2199 | \r | |
2200 | if (HandleCount != 0) {\r | |
2201 | FreePool (Handles);\r | |
2202 | }\r | |
2203 | \r | |
2204 | //\r | |
2205 | // Parse load file protocol\r | |
2206 | //\r | |
2207 | gBS->LocateHandleBuffer (\r | |
2208 | ByProtocol,\r | |
2209 | &gEfiLoadFileProtocolGuid,\r | |
2210 | NULL,\r | |
2211 | &HandleCount,\r | |
2212 | &Handles\r | |
2213 | );\r | |
2214 | for (Index = 0; Index < HandleCount; Index++) {\r | |
2215 | //\r | |
2216 | // Ignore BootManagerMenu. its boot option will be created by EfiBootManagerGetBootManagerMenu().\r | |
2217 | //\r | |
2218 | if (BmIsBootManagerMenuFilePath (DevicePathFromHandle (Handles[Index]))) {\r | |
2219 | continue;\r | |
2220 | }\r | |
2221 | \r | |
2222 | Description = BmGetBootDescription (Handles[Index]);\r | |
2223 | BootOptions = ReallocatePool (\r | |
2224 | sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount),\r | |
2225 | sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount + 1),\r | |
2226 | BootOptions\r | |
2227 | );\r | |
2228 | ASSERT (BootOptions != NULL);\r | |
2229 | \r | |
2230 | Status = EfiBootManagerInitializeLoadOption (\r | |
2231 | &BootOptions[(*BootOptionCount)++],\r | |
2232 | LoadOptionNumberUnassigned,\r | |
2233 | LoadOptionTypeBoot,\r | |
2234 | LOAD_OPTION_ACTIVE,\r | |
2235 | Description,\r | |
2236 | DevicePathFromHandle (Handles[Index]),\r | |
2237 | NULL,\r | |
2238 | 0\r | |
2239 | );\r | |
2240 | ASSERT_EFI_ERROR (Status);\r | |
2241 | FreePool (Description);\r | |
2242 | }\r | |
2243 | \r | |
2244 | if (HandleCount != 0) {\r | |
2245 | FreePool (Handles);\r | |
2246 | }\r | |
2247 | \r | |
2248 | BmMakeBootOptionDescriptionUnique (BootOptions, *BootOptionCount);\r | |
2249 | return BootOptions;\r | |
2250 | }\r | |
2251 | \r | |
2252 | /**\r | |
2253 | The function enumerates all boot options, creates them and registers them in the BootOrder variable.\r | |
2254 | **/\r | |
2255 | VOID\r | |
2256 | EFIAPI\r | |
2257 | EfiBootManagerRefreshAllBootOption (\r | |
2258 | VOID\r | |
2259 | )\r | |
2260 | {\r | |
2261 | EFI_STATUS Status;\r | |
2262 | EFI_BOOT_MANAGER_LOAD_OPTION *NvBootOptions;\r | |
2263 | UINTN NvBootOptionCount;\r | |
2264 | EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;\r | |
2265 | UINTN BootOptionCount;\r | |
2266 | UINTN Index;\r | |
2267 | \r | |
2268 | //\r | |
2269 | // Optionally refresh the legacy boot option\r | |
2270 | //\r | |
2271 | if (mBmRefreshLegacyBootOption != NULL) {\r | |
2272 | mBmRefreshLegacyBootOption ();\r | |
2273 | }\r | |
2274 | \r | |
2275 | BootOptions = BmEnumerateBootOptions (&BootOptionCount);\r | |
2276 | NvBootOptions = EfiBootManagerGetLoadOptions (&NvBootOptionCount, LoadOptionTypeBoot);\r | |
2277 | \r | |
2278 | //\r | |
2279 | // Mark the boot option as added by BDS by setting OptionalData to a special GUID\r | |
2280 | //\r | |
2281 | for (Index = 0; Index < BootOptionCount; Index++) {\r | |
2282 | BootOptions[Index].OptionalData = AllocateCopyPool (sizeof (EFI_GUID), &mBmAutoCreateBootOptionGuid);\r | |
2283 | BootOptions[Index].OptionalDataSize = sizeof (EFI_GUID);\r | |
2284 | }\r | |
2285 | \r | |
2286 | //\r | |
2287 | // Remove invalid EFI boot options from NV\r | |
2288 | //\r | |
2289 | for (Index = 0; Index < NvBootOptionCount; Index++) {\r | |
2290 | if (((DevicePathType (NvBootOptions[Index].FilePath) != BBS_DEVICE_PATH) ||\r | |
2291 | (DevicePathSubType (NvBootOptions[Index].FilePath) != BBS_BBS_DP)\r | |
2292 | ) && BmIsAutoCreateBootOption (&NvBootOptions[Index])\r | |
2293 | ) {\r | |
2294 | //\r | |
2295 | // Only check those added by BDS\r | |
2296 | // so that the boot options added by end-user or OS installer won't be deleted\r | |
2297 | //\r | |
2298 | if (EfiBootManagerFindLoadOption (&NvBootOptions[Index], BootOptions, BootOptionCount) == -1) {\r | |
2299 | Status = EfiBootManagerDeleteLoadOptionVariable (NvBootOptions[Index].OptionNumber, LoadOptionTypeBoot);\r | |
2300 | //\r | |
2301 | // Deleting variable with current variable implementation shouldn't fail.\r | |
2302 | //\r | |
2303 | ASSERT_EFI_ERROR (Status);\r | |
2304 | }\r | |
2305 | }\r | |
2306 | }\r | |
2307 | \r | |
2308 | //\r | |
2309 | // Add new EFI boot options to NV\r | |
2310 | //\r | |
2311 | for (Index = 0; Index < BootOptionCount; Index++) {\r | |
2312 | if (EfiBootManagerFindLoadOption (&BootOptions[Index], NvBootOptions, NvBootOptionCount) == -1) {\r | |
2313 | EfiBootManagerAddLoadOptionVariable (&BootOptions[Index], (UINTN) -1);\r | |
2314 | //\r | |
2315 | // Try best to add the boot options so continue upon failure.\r | |
2316 | //\r | |
2317 | }\r | |
2318 | }\r | |
2319 | \r | |
2320 | EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r | |
2321 | EfiBootManagerFreeLoadOptions (NvBootOptions, NvBootOptionCount);\r | |
2322 | }\r | |
2323 | \r | |
2324 | /**\r | |
2325 | This function is called to get or create the boot option for the Boot Manager Menu.\r | |
2326 | \r | |
2327 | The Boot Manager Menu is shown after successfully booting a boot option.\r | |
2328 | Assume the BootManagerMenuFile is in the same FV as the module links to this library.\r | |
2329 | \r | |
2330 | @param BootOption Return the boot option of the Boot Manager Menu\r | |
2331 | \r | |
2332 | @retval EFI_SUCCESS Successfully register the Boot Manager Menu.\r | |
2333 | @retval EFI_NOT_FOUND The Boot Manager Menu cannot be found.\r | |
2334 | @retval others Return status of gRT->SetVariable (). BootOption still points\r | |
2335 | to the Boot Manager Menu even the Status is not EFI_SUCCESS\r | |
2336 | and EFI_NOT_FOUND.\r | |
2337 | **/\r | |
2338 | EFI_STATUS\r | |
2339 | BmRegisterBootManagerMenu (\r | |
2340 | OUT EFI_BOOT_MANAGER_LOAD_OPTION *BootOption\r | |
2341 | )\r | |
2342 | {\r | |
2343 | EFI_STATUS Status;\r | |
2344 | CHAR16 *Description;\r | |
2345 | UINTN DescriptionLength;\r | |
2346 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
2347 | EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r | |
2348 | MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;\r | |
2349 | UINTN HandleCount;\r | |
2350 | EFI_HANDLE *Handles;\r | |
2351 | UINTN Index;\r | |
2352 | VOID *Data;\r | |
2353 | UINTN DataSize;\r | |
2354 | \r | |
2355 | DevicePath = NULL;\r | |
2356 | Description = NULL;\r | |
2357 | //\r | |
2358 | // Try to find BootManagerMenu from LoadFile protocol\r | |
2359 | //\r | |
2360 | gBS->LocateHandleBuffer (\r | |
2361 | ByProtocol,\r | |
2362 | &gEfiLoadFileProtocolGuid,\r | |
2363 | NULL,\r | |
2364 | &HandleCount,\r | |
2365 | &Handles\r | |
2366 | );\r | |
2367 | for (Index = 0; Index < HandleCount; Index++) {\r | |
2368 | if (BmIsBootManagerMenuFilePath (DevicePathFromHandle (Handles[Index]))) {\r | |
2369 | DevicePath = DuplicateDevicePath (DevicePathFromHandle (Handles[Index]));\r | |
2370 | Description = BmGetBootDescription (Handles[Index]);\r | |
2371 | break;\r | |
2372 | }\r | |
2373 | }\r | |
2374 | if (HandleCount != 0) {\r | |
2375 | FreePool (Handles);\r | |
2376 | }\r | |
2377 | \r | |
2378 | if (DevicePath == NULL) {\r | |
2379 | Data = NULL;\r | |
2380 | Status = GetSectionFromFv (\r | |
2381 | PcdGetPtr (PcdBootManagerMenuFile),\r | |
2382 | EFI_SECTION_PE32,\r | |
2383 | 0,\r | |
2384 | (VOID **) &Data,\r | |
2385 | &DataSize\r | |
2386 | );\r | |
2387 | if (Data != NULL) {\r | |
2388 | FreePool (Data);\r | |
2389 | }\r | |
2390 | if (EFI_ERROR (Status)) {\r | |
2391 | DEBUG ((EFI_D_WARN, "[Bds]BootManagerMenu FFS section can not be found, skip its boot option registration\n"));\r | |
2392 | return EFI_NOT_FOUND;\r | |
2393 | }\r | |
2394 | \r | |
2395 | //\r | |
2396 | // Get BootManagerMenu application's description from EFI User Interface Section.\r | |
2397 | //\r | |
2398 | Status = GetSectionFromFv (\r | |
2399 | PcdGetPtr (PcdBootManagerMenuFile),\r | |
2400 | EFI_SECTION_USER_INTERFACE,\r | |
2401 | 0,\r | |
2402 | (VOID **) &Description,\r | |
2403 | &DescriptionLength\r | |
2404 | );\r | |
2405 | if (EFI_ERROR (Status)) {\r | |
2406 | Description = NULL;\r | |
2407 | }\r | |
2408 | \r | |
2409 | EfiInitializeFwVolDevicepathNode (&FileNode, PcdGetPtr (PcdBootManagerMenuFile));\r | |
2410 | Status = gBS->HandleProtocol (\r | |
2411 | gImageHandle,\r | |
2412 | &gEfiLoadedImageProtocolGuid,\r | |
2413 | (VOID **) &LoadedImage\r | |
2414 | );\r | |
2415 | ASSERT_EFI_ERROR (Status);\r | |
2416 | DevicePath = AppendDevicePathNode (\r | |
2417 | DevicePathFromHandle (LoadedImage->DeviceHandle),\r | |
2418 | (EFI_DEVICE_PATH_PROTOCOL *) &FileNode\r | |
2419 | );\r | |
2420 | ASSERT (DevicePath != NULL);\r | |
2421 | }\r | |
2422 | \r | |
2423 | Status = EfiBootManagerInitializeLoadOption (\r | |
2424 | BootOption,\r | |
2425 | LoadOptionNumberUnassigned,\r | |
2426 | LoadOptionTypeBoot,\r | |
2427 | LOAD_OPTION_CATEGORY_APP | LOAD_OPTION_ACTIVE | LOAD_OPTION_HIDDEN,\r | |
2428 | (Description != NULL) ? Description : L"Boot Manager Menu",\r | |
2429 | DevicePath,\r | |
2430 | NULL,\r | |
2431 | 0\r | |
2432 | );\r | |
2433 | ASSERT_EFI_ERROR (Status);\r | |
2434 | FreePool (DevicePath);\r | |
2435 | if (Description != NULL) {\r | |
2436 | FreePool (Description);\r | |
2437 | }\r | |
2438 | \r | |
2439 | DEBUG_CODE (\r | |
2440 | EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;\r | |
2441 | UINTN BootOptionCount;\r | |
2442 | \r | |
2443 | BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);\r | |
2444 | ASSERT (EfiBootManagerFindLoadOption (BootOption, BootOptions, BootOptionCount) == -1);\r | |
2445 | EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r | |
2446 | );\r | |
2447 | \r | |
2448 | return EfiBootManagerAddLoadOptionVariable (BootOption, 0);\r | |
2449 | }\r | |
2450 | \r | |
2451 | /**\r | |
2452 | Return the boot option corresponding to the Boot Manager Menu.\r | |
2453 | It may automatically create one if the boot option hasn't been created yet.\r | |
2454 | \r | |
2455 | @param BootOption Return the Boot Manager Menu.\r | |
2456 | \r | |
2457 | @retval EFI_SUCCESS The Boot Manager Menu is successfully returned.\r | |
2458 | @retval EFI_NOT_FOUND The Boot Manager Menu cannot be found.\r | |
2459 | @retval others Return status of gRT->SetVariable (). BootOption still points\r | |
2460 | to the Boot Manager Menu even the Status is not EFI_SUCCESS\r | |
2461 | and EFI_NOT_FOUND.\r | |
2462 | **/\r | |
2463 | EFI_STATUS\r | |
2464 | EFIAPI\r | |
2465 | EfiBootManagerGetBootManagerMenu (\r | |
2466 | EFI_BOOT_MANAGER_LOAD_OPTION *BootOption\r | |
2467 | )\r | |
2468 | {\r | |
2469 | EFI_STATUS Status;\r | |
2470 | UINTN BootOptionCount;\r | |
2471 | EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;\r | |
2472 | UINTN Index;\r | |
2473 | \r | |
2474 | BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);\r | |
2475 | \r | |
2476 | for (Index = 0; Index < BootOptionCount; Index++) {\r | |
2477 | if (BmIsBootManagerMenuFilePath (BootOptions[Index].FilePath)) {\r | |
2478 | Status = EfiBootManagerInitializeLoadOption (\r | |
2479 | BootOption,\r | |
2480 | BootOptions[Index].OptionNumber,\r | |
2481 | BootOptions[Index].OptionType,\r | |
2482 | BootOptions[Index].Attributes,\r | |
2483 | BootOptions[Index].Description,\r | |
2484 | BootOptions[Index].FilePath,\r | |
2485 | BootOptions[Index].OptionalData,\r | |
2486 | BootOptions[Index].OptionalDataSize\r | |
2487 | );\r | |
2488 | ASSERT_EFI_ERROR (Status);\r | |
2489 | break;\r | |
2490 | }\r | |
2491 | }\r | |
2492 | \r | |
2493 | EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r | |
2494 | \r | |
2495 | //\r | |
2496 | // Automatically create the Boot#### for Boot Manager Menu when not found.\r | |
2497 | //\r | |
2498 | if (Index == BootOptionCount) {\r | |
2499 | return BmRegisterBootManagerMenu (BootOption);\r | |
2500 | } else {\r | |
2501 | return EFI_SUCCESS;\r | |
2502 | }\r | |
2503 | }\r | |
2504 | \r | |
2505 | /**\r | |
2506 | Get the next possible full path pointing to the load option.\r | |
2507 | The routine doesn't guarantee the returned full path points to an existing\r | |
2508 | file, and it also doesn't guarantee the existing file is a valid load option.\r | |
2509 | BmGetNextLoadOptionBuffer() guarantees.\r | |
2510 | \r | |
2511 | @param FilePath The device path pointing to a load option.\r | |
2512 | It could be a short-form device path.\r | |
2513 | @param FullPath The full path returned by the routine in last call.\r | |
2514 | Set to NULL in first call.\r | |
2515 | \r | |
2516 | @return The next possible full path pointing to the load option.\r | |
2517 | Caller is responsible to free the memory.\r | |
2518 | **/\r | |
2519 | EFI_DEVICE_PATH_PROTOCOL *\r | |
2520 | EFIAPI\r | |
2521 | EfiBootManagerGetNextLoadOptionDevicePath (\r | |
2522 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r | |
2523 | IN EFI_DEVICE_PATH_PROTOCOL *FullPath\r | |
2524 | )\r | |
2525 | {\r | |
2526 | return BmGetNextLoadOptionDevicePath(FilePath, FullPath);\r | |
2527 | }\r |