-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmds: implement pci device enumeration tool
Adds lspci tool which enumerates available PCI devices. JIRA: RTOS-595
- Loading branch information
Showing
3 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Phoenix-RTOS | ||
* | ||
* Operating system loader | ||
* | ||
* Enumerate PCI devices | ||
* | ||
* Copyright 2023 Phoenix Systems | ||
* Author: Gerard Swiderski | ||
* | ||
* This file is part of Phoenix-RTOS. | ||
* | ||
* %LICENSE% | ||
*/ | ||
|
||
|
||
#include <hal/ia32/pci.h> | ||
#include <lib/lib.h> | ||
|
||
#include "cmd.h" | ||
|
||
|
||
struct PCIDevice { | ||
u16 vendorID; | ||
u16 deviceID; | ||
u8 classCode; | ||
u8 subclass; | ||
}; | ||
|
||
|
||
static void cmd_lspciInfo(void) | ||
{ | ||
lib_printf("enumerates PCI devices"); | ||
} | ||
|
||
|
||
static void readPCIDevice(u8 bus, u8 dev, u8 func, struct PCIDevice *pciDevice) | ||
{ | ||
u32 cfg = hal_pciRead32(bus, dev, func, 0); | ||
pciDevice->vendorID = (u16)cfg; | ||
pciDevice->deviceID = (u16)(cfg >> 16); | ||
|
||
cfg = hal_pciRead32(bus, dev, func, 2); | ||
pciDevice->subclass = (u8)(cfg >> 16); | ||
pciDevice->classCode = (u8)(cfg >> 24); | ||
} | ||
|
||
|
||
static int cmd_lspciMain(int argc, char *argv[]) | ||
{ | ||
struct PCIDevice pciDevice; | ||
unsigned int bus; | ||
unsigned int dev; | ||
unsigned int func; | ||
const char *name; | ||
|
||
if (argc != 1) { | ||
log_error("\n%s: Command does not accept arguments", argv[0]); | ||
return CMD_EXIT_FAILURE; | ||
} | ||
|
||
if (hal_pciDetect() < 0) { | ||
log_error("\n%s: PCI bus is unavailable", argv[0]); | ||
return CMD_EXIT_FAILURE; | ||
} | ||
|
||
lib_printf("\n\033[1mBUS\tDEVICE\tFUNC\tVID\tDID\tCLASS\tDESCRIPTION\033[0m\n"); | ||
|
||
for (bus = 0; bus < 256u; ++bus) { | ||
for (dev = 0; dev < 32u; ++dev) { | ||
for (func = 0; func < 8u; ++func) { | ||
readPCIDevice(bus, dev, func, &pciDevice); | ||
|
||
if (pciDevice.vendorID == 0xffff) { | ||
continue; | ||
} | ||
|
||
lib_printf("0x%02x\t0x%02x\t0x%x\t0x%04x\t0x%04x\t0x%02x%02x\t", | ||
bus, dev, func, pciDevice.vendorID, pciDevice.deviceID, pciDevice.classCode, pciDevice.subclass); | ||
|
||
name = hal_pciClassName(pciDevice.classCode); | ||
lib_printf("%.*s\n", 30, (name != NULL) ? name : "Unknown device class"); | ||
} | ||
} | ||
} | ||
|
||
return CMD_EXIT_SUCCESS; | ||
} | ||
|
||
|
||
__attribute__((constructor)) static void cmd_lspciReg(void) | ||
{ | ||
const static cmd_t app_cmd = { | ||
.name = "lspci", .run = cmd_lspciMain, .info = cmd_lspciInfo | ||
}; | ||
|
||
cmd_reg(&app_cmd); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters