Skip to content

Commit

Permalink
cmds: implement pci device enumeration tool
Browse files Browse the repository at this point in the history
Adds lspci tool which enumerates available PCI devices.

JIRA: RTOS-595
  • Loading branch information
gerard5 committed Sep 7, 2023
1 parent e295b7b commit aea9e6e
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmds/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#

PLO_ALLCOMMANDS = alias app bankswitch bitstream bootcm4 bootrom call console copy dump \
echo erase go help jffs2 kernel kernelimg map mem mpu otp phfs reboot script test-dev \
test-ddr wait
echo erase go help jffs2 kernel kernelimg lspci map mem mpu otp phfs reboot script \
test-dev test-ddr wait

PLO_COMMANDS ?= $(PLO_ALLCOMMANDS)
PLO_APPLETS = $(filter $(PLO_ALLCOMMANDS), $(PLO_COMMANDS))
Expand Down
98 changes: 98 additions & 0 deletions cmds/lspci.c
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);
}
2 changes: 1 addition & 1 deletion hal/ia32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
CFLAGS += -DVADDR_KERNEL_BASE=$(VADDR_KERNEL_BASE)
CFLAGS += -Ihal/ia32

PLO_COMMANDS := alias app call console copy dump echo go help kernel map mem phfs script reboot syspage wait
PLO_COMMANDS := alias app call console copy dump echo go help kernel lspci map mem phfs script reboot syspage wait

include devices/disk-bios/Makefile
include devices/tty-bios/Makefile
Expand Down

0 comments on commit aea9e6e

Please sign in to comment.