ef09050a1a708319b48d476bdb003d802c12556f
[mirror_edk2.git] / MdeModulePkg / Library / UefiBootManagerLib / InternalBm.h
1 /** @file
2 BDS library definition, include the file and data structure
3
4 Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #ifndef _INTERNAL_BM_H_
17 #define _INTERNAL_BM_H_
18
19 #include <PiDxe.h>
20
21 #include <IndustryStandard/Pci.h>
22 #include <IndustryStandard/PeImage.h>
23 #include <IndustryStandard/Atapi.h>
24 #include <IndustryStandard/Scsi.h>
25 #include <IndustryStandard/Nvme.h>
26
27 #include <Protocol/PciRootBridgeIo.h>
28 #include <Protocol/BlockIo.h>
29 #include <Protocol/LoadedImage.h>
30 #include <Protocol/SimpleFileSystem.h>
31 #include <Protocol/LoadFile.h>
32 #include <Protocol/DevicePath.h>
33 #include <Protocol/SimpleTextIn.h>
34 #include <Protocol/SimpleTextInEx.h>
35 #include <Protocol/SimpleTextOut.h>
36 #include <Protocol/SimpleNetwork.h>
37 #include <Protocol/FirmwareVolume2.h>
38 #include <Protocol/PciIo.h>
39 #include <Protocol/GraphicsOutput.h>
40 #include <Protocol/UsbIo.h>
41 #include <Protocol/DiskInfo.h>
42 #include <Protocol/NvmExpressPassthru.h>
43 #include <Protocol/IdeControllerInit.h>
44 #include <Protocol/BootLogo.h>
45 #include <Protocol/DriverHealth.h>
46 #include <Protocol/FormBrowser2.h>
47 #include <Protocol/VariableLock.h>
48 #include <Protocol/RamDisk.h>
49 #include <Protocol/DeferredImageLoad.h>
50
51 #include <Guid/MemoryTypeInformation.h>
52 #include <Guid/FileInfo.h>
53 #include <Guid/GlobalVariable.h>
54 #include <Guid/Performance.h>
55 #include <Guid/StatusCodeDataTypeVariable.h>
56
57 #include <Library/PrintLib.h>
58 #include <Library/DebugLib.h>
59 #include <Library/BaseMemoryLib.h>
60 #include <Library/UefiBootServicesTableLib.h>
61 #include <Library/UefiRuntimeServicesTableLib.h>
62 #include <Library/UefiLib.h>
63 #include <Library/MemoryAllocationLib.h>
64 #include <Library/DxeServicesTableLib.h>
65 #include <Library/HobLib.h>
66 #include <Library/BaseLib.h>
67 #include <Library/DevicePathLib.h>
68 #include <Library/PerformanceLib.h>
69 #include <Library/PcdLib.h>
70 #include <Library/PeCoffGetEntryPointLib.h>
71 #include <Library/UefiBootManagerLib.h>
72 #include <Library/TimerLib.h>
73 #include <Library/DxeServicesLib.h>
74 #include <Library/ReportStatusCodeLib.h>
75 #include <Library/CapsuleLib.h>
76 #include <Library/PerformanceLib.h>
77 #include <Library/HiiLib.h>
78
79 #if !defined (EFI_REMOVABLE_MEDIA_FILE_NAME)
80 #if defined (MDE_CPU_EBC)
81 //
82 // Uefi specification only defines the default boot file name for IA32, X64
83 // and IPF processor, so need define boot file name for EBC architecture here.
84 //
85 #define EFI_REMOVABLE_MEDIA_FILE_NAME L"\\EFI\\BOOT\\BOOTEBC.EFI"
86 #else
87 #error "Can not determine the default boot file name for unknown processor type!"
88 #endif
89 #endif
90
91 typedef enum {
92 BmAcpiFloppyBoot,
93 BmHardwareDeviceBoot,
94 BmMessageAtapiBoot,
95 BmMessageSataBoot,
96 BmMessageUsbBoot,
97 BmMessageScsiBoot,
98 BmMiscBoot
99 } BM_BOOT_TYPE;
100
101 typedef
102 CHAR16 *
103 (* BM_GET_BOOT_DESCRIPTION) (
104 IN EFI_HANDLE Handle
105 );
106
107 //
108 // PlatformRecovery#### is the load option with the longest name
109 //
110 #define BM_OPTION_NAME_LEN sizeof ("PlatformRecovery####")
111 extern CHAR16 *mBmLoadOptionName[];
112
113 /**
114 Visitor function to be called by BmForEachVariable for each variable
115 in variable storage.
116
117 @param Name Variable name.
118 @param Guid Variable GUID.
119 @param Context The same context passed to BmForEachVariable.
120 **/
121 typedef
122 VOID
123 (*BM_VARIABLE_VISITOR) (
124 CHAR16 *Name,
125 EFI_GUID *Guid,
126 VOID *Context
127 );
128
129 /**
130 Call Visitor function for each variable in variable storage.
131
132 @param Visitor Visitor function.
133 @param Context The context passed to Visitor function.
134 **/
135 VOID
136 BmForEachVariable (
137 BM_VARIABLE_VISITOR Visitor,
138 VOID *Context
139 );
140
141 #define BM_BOOT_DESCRIPTION_ENTRY_SIGNATURE SIGNATURE_32 ('b', 'm', 'd', 'h')
142 typedef struct {
143 UINT32 Signature;
144 LIST_ENTRY Link;
145 EFI_BOOT_MANAGER_BOOT_DESCRIPTION_HANDLER Handler;
146 } BM_BOOT_DESCRIPTION_ENTRY;
147
148 /**
149 Repair all the controllers according to the Driver Health status queried.
150 **/
151 VOID
152 BmRepairAllControllers (
153 VOID
154 );
155
156 #define BM_HOTKEY_SIGNATURE SIGNATURE_32 ('b', 'm', 'h', 'k')
157 typedef struct {
158 UINT32 Signature;
159 LIST_ENTRY Link;
160
161 BOOLEAN IsContinue;
162 UINT16 BootOption;
163 UINT8 CodeCount;
164 UINT8 WaitingKey;
165 EFI_KEY_DATA KeyData[3];
166 } BM_HOTKEY;
167
168 #define BM_HOTKEY_FROM_LINK(a) CR (a, BM_HOTKEY, Link, BM_HOTKEY_SIGNATURE)
169
170 /**
171 Get the Option Number that wasn't used.
172
173 @param LoadOptionType Load option type.
174 @param FreeOptionNumber To receive the minimal free option number.
175
176 @retval EFI_SUCCESS The option number is found
177 @retval EFI_OUT_OF_RESOURCES There is no free option number that can be used.
178 @retval EFI_INVALID_PARAMETER FreeOptionNumber is NULL
179
180 **/
181 EFI_STATUS
182 BmGetFreeOptionNumber (
183 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType,
184 OUT UINT16 *FreeOptionNumber
185 );
186
187 /**
188
189 Writes performance data of booting into the allocated memory.
190 OS can process these records.
191
192 @param Event The triggered event.
193 @param Context Context for this event.
194
195 **/
196 VOID
197 EFIAPI
198 BmWriteBootToOsPerformanceData (
199 IN EFI_EVENT Event,
200 IN VOID *Context
201 );
202
203 /**
204 This routine adjust the memory information for different memory type and
205 save them into the variables for next boot. It resets the system when
206 memory information is updated and the current boot option belongs to
207 boot category instead of application category. It doesn't count the
208 reserved memory occupied by RAM Disk.
209
210 @param Boot TRUE if current boot option belongs to boot
211 category instead of application category.
212 **/
213 VOID
214 BmSetMemoryTypeInformationVariable (
215 IN BOOLEAN Boot
216 );
217
218 /**
219 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
220 instances, has the same partition node with HardDriveDevicePath device path
221
222 @param BlockIoDevicePath Multi device path instances which need to check
223 @param HardDriveDevicePath A device path which starts with a hard drive media
224 device path.
225
226 @retval TRUE There is a matched device path instance.
227 @retval FALSE There is no matched device path instance.
228
229 **/
230 BOOLEAN
231 BmMatchPartitionDevicePathNode (
232 IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,
233 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath
234 );
235
236 /**
237 Connect the specific Usb device which match the short form device path.
238
239 @param DevicePath A short-form device path that starts with the first
240 element being a USB WWID or a USB Class device
241 path
242
243 @return EFI_INVALID_PARAMETER DevicePath is NULL pointer.
244 DevicePath is not a USB device path.
245
246 @return EFI_SUCCESS Success to connect USB device
247 @return EFI_NOT_FOUND Fail to find handle for USB controller to connect.
248
249 **/
250 EFI_STATUS
251 BmConnectUsbShortFormDevicePath (
252 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
253 );
254
255 /**
256 Stop the hotkey processing.
257
258 @param Event Event pointer related to hotkey service.
259 @param Context Context pass to this function.
260 **/
261 VOID
262 EFIAPI
263 BmStopHotkeyService (
264 IN EFI_EVENT Event,
265 IN VOID *Context
266 );
267
268 /**
269 Set the variable and report the error through status code upon failure.
270
271 @param VariableName A Null-terminated string that is the name of the vendor's variable.
272 Each VariableName is unique for each VendorGuid. VariableName must
273 contain 1 or more characters. If VariableName is an empty string,
274 then EFI_INVALID_PARAMETER is returned.
275 @param VendorGuid A unique identifier for the vendor.
276 @param Attributes Attributes bitmask to set for the variable.
277 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
278 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
279 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
280 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
281 set, then a SetVariable() call with a DataSize of zero will not cause any change to
282 the variable value (the timestamp associated with the variable may be updated however
283 even if no new data value is provided,see the description of the
284 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
285 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
286 @param Data The contents for the variable.
287
288 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
289 defined by the Attributes.
290 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
291 DataSize exceeds the maximum allowed.
292 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
293 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
294 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
295 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
296 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
297 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
298 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
299 does NOT pass the validation check carried out by the firmware.
300
301 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
302 **/
303 EFI_STATUS
304 BmSetVariableAndReportStatusCodeOnError (
305 IN CHAR16 *VariableName,
306 IN EFI_GUID *VendorGuid,
307 IN UINT32 Attributes,
308 IN UINTN DataSize,
309 IN VOID *Data
310 );
311
312 /**
313 Function compares a device path data structure to that of all the nodes of a
314 second device path instance.
315
316 @param Multi A pointer to a multi-instance device path data
317 structure.
318 @param Single A pointer to a single-instance device path data
319 structure.
320
321 @retval TRUE If the Single device path is contained within Multi device path.
322 @retval FALSE The Single device path is not match within Multi device path.
323
324 **/
325 BOOLEAN
326 BmMatchDevicePaths (
327 IN EFI_DEVICE_PATH_PROTOCOL *Multi,
328 IN EFI_DEVICE_PATH_PROTOCOL *Single
329 );
330
331 /**
332 Delete the instance in Multi which matches partly with Single instance
333
334 @param Multi A pointer to a multi-instance device path data
335 structure.
336 @param Single A pointer to a single-instance device path data
337 structure.
338
339 @return This function will remove the device path instances in Multi which partly
340 match with the Single, and return the result device path. If there is no
341 remaining device path as a result, this function will return NULL.
342
343 **/
344 EFI_DEVICE_PATH_PROTOCOL *
345 BmDelPartMatchInstance (
346 IN EFI_DEVICE_PATH_PROTOCOL *Multi,
347 IN EFI_DEVICE_PATH_PROTOCOL *Single
348 );
349
350 /**
351 Repair all the controllers according to the Driver Health status queried.
352 **/
353 VOID
354 BmRepairAllControllers (
355 VOID
356 );
357
358 /**
359 Print the device path info.
360
361 @param DevicePath The device path need to print.
362 **/
363 VOID
364 BmPrintDp (
365 EFI_DEVICE_PATH_PROTOCOL *DevicePath
366 );
367
368 /**
369 Convert a single character to number.
370 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'
371
372 @param Char The input char which need to convert to int.
373
374 @return The converted 8-bit number or (UINTN) -1 if conversion failed.
375 **/
376 UINTN
377 BmCharToUint (
378 IN CHAR16 Char
379 );
380
381 /**
382 Return the boot description for the controller.
383
384 @param Handle Controller handle.
385
386 @return The description string.
387 **/
388 CHAR16 *
389 BmGetBootDescription (
390 IN EFI_HANDLE Handle
391 );
392
393 /**
394 Enumerate all boot option descriptions and append " 2"/" 3"/... to make
395 unique description.
396
397 @param BootOptions Array of boot options.
398 @param BootOptionCount Count of boot options.
399 **/
400 VOID
401 BmMakeBootOptionDescriptionUnique (
402 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions,
403 UINTN BootOptionCount
404 );
405
406 /**
407 Get the file buffer from the specified Load File instance.
408
409 @param LoadFileHandle The specified Load File instance.
410 @param FilePath The file path which will pass to LoadFile().
411
412 @return The full device path pointing to the load option buffer.
413 **/
414 EFI_DEVICE_PATH_PROTOCOL *
415 BmExpandLoadFile (
416 IN EFI_HANDLE LoadFileHandle,
417 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
418 );
419
420 /**
421 Return the RAM Disk device path created by LoadFile.
422
423 @param FilePath The source file path.
424
425 @return Callee-to-free RAM Disk device path
426 **/
427 EFI_DEVICE_PATH_PROTOCOL *
428 BmGetRamDiskDevicePath (
429 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
430 );
431
432 /**
433 Destroy the RAM Disk.
434
435 The destroy operation includes to call RamDisk.Unregister to
436 unregister the RAM DISK from RAM DISK driver, free the memory
437 allocated for the RAM Disk.
438
439 @param RamDiskDevicePath RAM Disk device path.
440 **/
441 VOID
442 BmDestroyRamDisk (
443 IN EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath
444 );
445
446 /**
447 Get the next possible full path pointing to the load option.
448
449 @param FilePath The device path pointing to a load option.
450 It could be a short-form device path.
451 @param FullPath The full path returned by the routine in last call.
452 Set to NULL in first call.
453
454 @return The next possible full path pointing to the load option.
455 Caller is responsible to free the memory.
456 **/
457 EFI_DEVICE_PATH_PROTOCOL *
458 BmGetNextLoadOptionDevicePath (
459 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
460 IN EFI_DEVICE_PATH_PROTOCOL *FullPath
461 );
462
463 /**
464 Return the next matched load option buffer.
465 The routine keeps calling BmGetNextLoadOptionDevicePath() until a valid
466 load option is read.
467
468 @param Type The load option type.
469 It's used to check whether the load option is valid.
470 When it's LoadOptionTypeMax, the routine only guarantees
471 the load option is a valid PE image but doesn't guarantee
472 the PE's subsystem type is valid.
473 @param FilePath The device path pointing to a load option.
474 It could be a short-form device path.
475 @param FullPath Return the next full device path of the load option after
476 short-form device path expanding.
477 Caller is responsible to free it.
478 NULL to return the first matched full device path.
479 @param FileSize Return the load option size.
480
481 @return The load option buffer. Caller is responsible to free the memory.
482 **/
483 VOID *
484 BmGetNextLoadOptionBuffer (
485 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE Type,
486 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
487 OUT EFI_DEVICE_PATH_PROTOCOL **FullPath,
488 OUT UINTN *FileSize
489 );
490 #endif // _INTERNAL_BM_H_