UefiBootServicesTableLibUnitTest.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /** @file
  2. This library supports a Boot Services table library implementation that allows code dependent
  3. upon UefiBootServicesTableLib to operate in an isolated execution environment such as within
  4. the context of a host-based unit test framework.
  5. The unit test should initialize the Boot Services database with any required elements
  6. (e.g. protocols, events, handles, etc.) prior to the services being invoked by code under test.
  7. It is strongly recommended to clean any global databases (e.g. protocol, event, handles, etc.) after
  8. every unit test so the tests execute in a predictable manner from a clean state.
  9. Copyright (c) Microsoft Corporation
  10. SPDX-License-Identifier: BSD-2-Clause-Patent
  11. **/
  12. #include "UefiBootServicesTableLibUnitTest.h"
  13. EFI_HANDLE gImageHandle = NULL;
  14. EFI_SYSTEM_TABLE *gST = NULL;
  15. STATIC EFI_BOOT_SERVICES mBootServices = {
  16. {
  17. EFI_BOOT_SERVICES_SIGNATURE, // Signature
  18. EFI_BOOT_SERVICES_REVISION, // Revision
  19. sizeof (EFI_BOOT_SERVICES), // HeaderSize
  20. 0, // CRC32
  21. 0 // Reserved
  22. },
  23. (EFI_RAISE_TPL)UnitTestRaiseTpl, // RaiseTPL
  24. (EFI_RESTORE_TPL)UnitTestRestoreTpl, // RestoreTPL
  25. (EFI_ALLOCATE_PAGES)UnitTestAllocatePages, // AllocatePages
  26. (EFI_FREE_PAGES)UnitTestFreePages, // FreePages
  27. (EFI_GET_MEMORY_MAP)UnitTestGetMemoryMap, // GetMemoryMap
  28. (EFI_ALLOCATE_POOL)UnitTestAllocatePool, // AllocatePool
  29. (EFI_FREE_POOL)UnitTestFreePool, // FreePool
  30. (EFI_CREATE_EVENT)UnitTestCreateEvent, // CreateEvent
  31. (EFI_SET_TIMER)UnitTestSetTimer, // SetTimer
  32. (EFI_WAIT_FOR_EVENT)UnitTestWaitForEvent, // WaitForEvent
  33. (EFI_SIGNAL_EVENT)UnitTestSignalEvent, // SignalEvent
  34. (EFI_CLOSE_EVENT)UnitTestCloseEvent, // CloseEvent
  35. (EFI_CHECK_EVENT)UnitTestCheckEvent, // CheckEvent
  36. (EFI_INSTALL_PROTOCOL_INTERFACE)UnitTestInstallProtocolInterface, // InstallProtocolInterface
  37. (EFI_REINSTALL_PROTOCOL_INTERFACE)UnitTestReinstallProtocolInterface, // ReinstallProtocolInterface
  38. (EFI_UNINSTALL_PROTOCOL_INTERFACE)UnitTestUninstallProtocolInterface, // UninstallProtocolInterface
  39. (EFI_HANDLE_PROTOCOL)UnitTestHandleProtocol, // HandleProtocol
  40. (VOID *)NULL, // Reserved
  41. (EFI_REGISTER_PROTOCOL_NOTIFY)UnitTestRegisterProtocolNotify, // RegisterProtocolNotify
  42. (EFI_LOCATE_HANDLE)UnitTestLocateHandle, // LocateHandle
  43. (EFI_LOCATE_DEVICE_PATH)UnitTestLocateDevicePath, // LocateDevicePath
  44. (EFI_INSTALL_CONFIGURATION_TABLE)UnitTestInstallConfigurationTable, // InstallConfigurationTable
  45. (EFI_IMAGE_LOAD)UnitTestLoadImage, // LoadImage
  46. (EFI_IMAGE_START)UnitTestStartImage, // StartImage
  47. (EFI_EXIT)UnitTestExit, // Exit
  48. (EFI_IMAGE_UNLOAD)UnitTestUnloadImage, // UnloadImage
  49. (EFI_EXIT_BOOT_SERVICES)UnitTestExitBootServices, // ExitBootServices
  50. (EFI_GET_NEXT_MONOTONIC_COUNT)UnitTestGetNextMonotonicCount, // GetNextMonotonicCount
  51. (EFI_STALL)UnitTestStall, // Stall
  52. (EFI_SET_WATCHDOG_TIMER)UnitTestSetWatchdogTimer, // SetWatchdogTimer
  53. (EFI_CONNECT_CONTROLLER)UnitTestConnectController, // ConnectController
  54. (EFI_DISCONNECT_CONTROLLER)UnitTestDisconnectController, // DisconnectController
  55. (EFI_OPEN_PROTOCOL)UnitTestOpenProtocol, // OpenProtocol
  56. (EFI_CLOSE_PROTOCOL)UnitTestCloseProtocol, // CloseProtocol
  57. (EFI_OPEN_PROTOCOL_INFORMATION)UnitTestOpenProtocolInformation, // OpenProtocolInformation
  58. (EFI_PROTOCOLS_PER_HANDLE)UnitTestProtocolsPerHandle, // ProtocolsPerHandle
  59. (EFI_LOCATE_HANDLE_BUFFER)UnitTestLocateHandleBuffer, // LocateHandleBuffer
  60. (EFI_LOCATE_PROTOCOL)UnitTestLocateProtocol, // LocateProtocol
  61. (EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES)UnitTestInstallMultipleProtocolInterfaces, // InstallMultipleProtocolInterfaces
  62. (EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES)UnitTestUninstallMultipleProtocolInterfaces, // UninstallMultipleProtocolInterfaces
  63. (EFI_CALCULATE_CRC32)UnitTestCalculateCrc32, // CalculateCrc32
  64. (EFI_COPY_MEM)CopyMem, // CopyMem
  65. (EFI_SET_MEM)SetMem, // SetMem
  66. (EFI_CREATE_EVENT_EX)UnitTestCreateEventEx // CreateEventEx
  67. };
  68. EFI_BOOT_SERVICES *gBS = &mBootServices;
  69. /**
  70. The constructor function caches the pointer of Boot Services Table.
  71. The constructor function caches the pointer of Boot Services Table through System Table.
  72. It will ASSERT() if the pointer of System Table is NULL.
  73. It will ASSERT() if the pointer of Boot Services Table is NULL.
  74. It will always return EFI_SUCCESS.
  75. @param ImageHandle The firmware allocated handle for the EFI image.
  76. @param SystemTable A pointer to the EFI System Table.
  77. @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
  78. **/
  79. EFI_STATUS
  80. EFIAPI
  81. UefiBootServicesTableLibConstructor (
  82. IN EFI_HANDLE ImageHandle,
  83. IN EFI_SYSTEM_TABLE *SystemTable
  84. )
  85. {
  86. //
  87. // Cache the Image Handle
  88. //
  89. gImageHandle = ImageHandle;
  90. ASSERT (gImageHandle != NULL);
  91. //
  92. // Cache pointer to the EFI System Table
  93. //
  94. // Note: The system table is not implemented
  95. gST = NULL;
  96. //
  97. // Cache pointer to the EFI Boot Services Table
  98. //
  99. gBS = SystemTable->BootServices;
  100. ASSERT (gBS != NULL);
  101. return EFI_SUCCESS;
  102. }