Skip to content

Commit

Permalink
Rewrite printIOMMUgroup to use the new module library
Browse files Browse the repository at this point in the history
  • Loading branch information
HikariKnight committed Mar 6, 2023
1 parent a83041b commit 3b049a9
Showing 1 changed file with 53 additions and 18 deletions.
71 changes: 53 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"
"os"
"strconv"
"regexp"

iommu "github.com/HikariKnight/ls-iommu/lib"
"github.com/akamensky/argparse"
Expand Down Expand Up @@ -58,7 +58,7 @@ func main() {
printoutput(usbs)
printIOMMUgroup(*iommu_group)
} else if *test {
newTest(*iommu_group)
newTest(false, `VGA`)
} else if len(*iommu_group) > 0 {
printIOMMUgroup(*iommu_group)
} else {
Expand All @@ -68,34 +68,69 @@ func main() {
}
}

// Print all devices in IOMMU group
func printIOMMUgroup(groups []int) {
if len(groups) > 0 {
// For each IOMMU group given we will print the devices in each group
for _, group := range groups {
devs := iommu.MatchDEVs(false, `Group ` + strconv.Itoa(group))
printoutput(devs)
}
}
os.Exit(0)
}

// Function to just print out a string array to STDOUT
func printoutput(out []string) {
for _, line := range out {
fmt.Print(line)
}
}

func newTest(groups []int) {
// Function to print everything inside a specific IOMMU group
func printIOMMUgroup(groups []int) {
// As long as we are asked to get devices from any specific IOMMU groups
if len(groups) > 0 {
// Get all IOMMU devices
devs := iommu.NewIOMMU()
// For each IOMMU group given we will print the devices in each group
for _, group := range groups {
//devs := iommu.MatchDEVs(false, `Group ` + strconv.Itoa(group))
fmt.Println(devs.Groups[group])
//printoutput(devs)
// For each device in specified IOMMU group
for _, device := range devs.Groups[group].Devices {
var line string

// If the device has no revision, ommit the (rev ID), in both cases we generate the line with device info
if device.Revision != "0x00" {
line = fmt.Sprintf("IOMMU Group %v: %s %s: %s %s [%s:%s] (rev %s)\n",
group,
device.Address,
device.Subclass.Name,
device.Vendor.Name,
device.Product.Name,
device.Vendor.ID,
device.Product.ID,
device.Revision[len(device.Revision)-2:],
)
} else {
line = fmt.Sprintf("IOMMU Group %v: %s %s: %s %s [%s:%s]\n",
group,
device.Address,
device.Subclass.Name,
device.Vendor.Name,
device.Product.Name,
device.Vendor.ID,
device.Product.ID,
)
}

// Print the device info
fmt.Print(line)
}
}
}
os.Exit(0)
}

func newTest(kernelmodules bool, regex string) []string{
var devs []string

output := iommu.GetAllDevices(kernelmodules)
gpuReg, err := regexp.Compile(regex)
iommu.ErrorCheck(err)

for _, line := range output {
if gpuReg.MatchString(line) {
devs = append(devs, line)
}
}

return devs
}

0 comments on commit 3b049a9

Please sign in to comment.