aboutsummaryrefslogtreecommitdiffstats
path: root/Lessons/Lesson_30/UefiLessonsPkg/ListPCI
diff options
context:
space:
mode:
authorKonstantin Aladyshev <aladyshev22@gmail.com>2021-07-10 00:04:40 +0300
committerKonstantin Aladyshev <aladyshev22@gmail.com>2021-07-10 00:04:40 +0300
commit6064c1e48b622f53538f4df9bdd402c607a87d51 (patch)
tree93d3c937b9568568307fd2ff7053a30c538ad72a /Lessons/Lesson_30/UefiLessonsPkg/ListPCI
parenta9c375c80c3505be794ec2b5d5bb90de27ef0d42 (diff)
downloadUEFI-Lessons-6064c1e48b622f53538f4df9bdd402c607a87d51.tar.gz
UEFI-Lessons-6064c1e48b622f53538f4df9bdd402c607a87d51.tar.bz2
UEFI-Lessons-6064c1e48b622f53538f4df9bdd402c607a87d51.zip
Move lessons to separate folder
Signed-off-by: Konstantin Aladyshev <aladyshev22@gmail.com>
Diffstat (limited to 'Lessons/Lesson_30/UefiLessonsPkg/ListPCI')
-rw-r--r--Lessons/Lesson_30/UefiLessonsPkg/ListPCI/ListPCI.c116
-rw-r--r--Lessons/Lesson_30/UefiLessonsPkg/ListPCI/ListPCI.inf20
2 files changed, 136 insertions, 0 deletions
diff --git a/Lessons/Lesson_30/UefiLessonsPkg/ListPCI/ListPCI.c b/Lessons/Lesson_30/UefiLessonsPkg/ListPCI/ListPCI.c
new file mode 100644
index 0000000..e8d5842
--- /dev/null
+++ b/Lessons/Lesson_30/UefiLessonsPkg/ListPCI/ListPCI.c
@@ -0,0 +1,116 @@
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiLib.h>
+
+#include <Protocol/PciRootBridgeIo.h>
+#include <Library/MemoryAllocationLib.h>
+#include <IndustryStandard/Pci.h>
+
+
+UINT64 PciConfigurationAddress(UINT8 Bus,
+ UINT8 Device,
+ UINT8 Function,
+ UINT32 Register)
+{
+ UINT64 Address = (((UINT64)Bus) << 24) + (((UINT64)Device) << 16) + (((UINT64)Function) << 8);
+ if (Register & 0xFFFFFF00) {
+ Address += (((UINT64)Register) << 32);
+ } else {
+ Address += (((UINT64)Register) << 0);
+ }
+ return Address;
+}
+
+
+EFI_STATUS PrintRootBridge(EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL* PciRootBridgeIo)
+{
+ EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR* AddressDescriptor;
+ EFI_STATUS Status = PciRootBridgeIo->Configuration(
+ PciRootBridgeIo,
+ (VOID**)&AddressDescriptor
+ );
+ if (EFI_ERROR(Status)) {
+ Print(L"\tError! Can't get EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR: %r\n", Status);
+ return Status;
+ }
+ while (AddressDescriptor->Desc != ACPI_END_TAG_DESCRIPTOR) {
+ if (AddressDescriptor->ResType == ACPI_ADDRESS_SPACE_TYPE_BUS) {
+ for (UINT8 Bus = AddressDescriptor->AddrRangeMin; Bus <= AddressDescriptor->AddrRangeMax; Bus++) {
+ for (UINT8 Device = 0; Device <= PCI_MAX_DEVICE; Device++) {
+ for (UINT8 Func = 0; Func <= PCI_MAX_FUNC; Func++) {
+ UINT64 Address = PciConfigurationAddress(Bus, Device, Func, 0);
+ PCI_DEVICE_INDEPENDENT_REGION PCIConfHdr;
+ Status = PciRootBridgeIo->Pci.Read(
+ PciRootBridgeIo,
+ EfiPciWidthUint8,
+ Address,
+ sizeof(PCI_DEVICE_INDEPENDENT_REGION),
+ &PCIConfHdr
+ );
+ if (!EFI_ERROR(Status)) {
+ if (PCIConfHdr.VendorId != 0xffff) {
+ Print(L"\tBus: %02x, Dev: %02x, Func: %02x - Vendor:%04x, Device:%04x\n",
+ Bus,
+ Device,
+ Func,
+ PCIConfHdr.VendorId,
+ PCIConfHdr.DeviceId);
+ }
+ } else {
+ Print(L"\tError in PCI read: %r\n", Status);
+ }
+ }
+ }
+ }
+ }
+ AddressDescriptor++;
+ }
+ return Status;
+}
+
+EFI_STATUS
+EFIAPI
+UefiMain (
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_SYSTEM_TABLE *SystemTable
+ )
+{
+ EFI_STATUS Status;
+ UINTN HandleCount;
+ EFI_HANDLE *HandleBuffer;
+ Status = gBS->LocateHandleBuffer(
+ ByProtocol,
+ &gEfiPciRootBridgeIoProtocolGuid,
+ NULL,
+ &HandleCount,
+ &HandleBuffer
+ );
+ if (EFI_ERROR (Status)) {
+ Print(L"Can't locate EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL: %r\n", Status);
+ return Status;
+ }
+
+ Print(L"Number of PCI root bridges in the system: %d\n", HandleCount);
+ EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL* PciRootBridgeIo;
+ for (UINTN Index = 0; Index < HandleCount; Index++) {
+ Status = gBS->OpenProtocol (
+ HandleBuffer[Index],
+ &gEfiPciRootBridgeIoProtocolGuid,
+ (VOID **)&PciRootBridgeIo,
+ ImageHandle,
+ NULL,
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL
+ );
+ if (EFI_ERROR(Status)) {
+ Print(L"Can't open protocol: %r\n", Status);
+ return Status;
+ }
+ Print(L"\nPCI Root Bridge %d\n", Index);
+ Status = PrintRootBridge(PciRootBridgeIo);
+ if (EFI_ERROR(Status)) {
+ Print(L"Error in PCI Root Bridge printing\n");
+ }
+ }
+ FreePool(HandleBuffer);
+
+ return EFI_SUCCESS;
+}
diff --git a/Lessons/Lesson_30/UefiLessonsPkg/ListPCI/ListPCI.inf b/Lessons/Lesson_30/UefiLessonsPkg/ListPCI/ListPCI.inf
new file mode 100644
index 0000000..dd32b12
--- /dev/null
+++ b/Lessons/Lesson_30/UefiLessonsPkg/ListPCI/ListPCI.inf
@@ -0,0 +1,20 @@
+[Defines]
+ INF_VERSION = 1.25
+ BASE_NAME = ListPCI
+ FILE_GUID = 07aceb78-97df-4e49-84a8-28997896e42a
+ MODULE_TYPE = UEFI_APPLICATION
+ VERSION_STRING = 1.0
+ ENTRY_POINT = UefiMain
+
+[Sources]
+ ListPCI.c
+
+[Packages]
+ MdePkg/MdePkg.dec
+
+[LibraryClasses]
+ UefiApplicationEntryPoint
+ UefiLib
+
+[Protocols]
+ gEfiPciRootBridgeIoProtocolGuid