Files
logpile/internal/parser/vendors/pciids/pciids.go
T
Mikhail ChusavitinandClaude Opus 5.5 b4b15e58a2 feat(parser): linux-generic content-driven parser and RAR archive support
Field bundles from ad-hoc service scripts (first sample: Giga G894-SD3 .rar
with dmidecode/lscpu/lspci/nvme/lsblk output, no fixed layout). ADL-078:
- linux-generic: recognizes utility output by content, merges records by
  stable key, low detect confidence (20-30) below all vendor parsers
- pciids.LookupByDescription: resolve a textual lspci description back to
  vendor/device IDs and manufacturer/model
- archive: accept .rar via the in-repo internal/rar5 reader (RAR 5 / RAR 7,
  single volume, solid; encrypted, multi-volume, SFX and RAR 4.x rejected),
  same per-file and total limits as tar/zip
- UI: .rar in the upload accept list

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 15:00:38 +03:00

247 lines
5.8 KiB
Go

package pciids
import (
"bufio"
_ "embed"
"fmt"
"os"
"sort"
"strconv"
"strings"
"sync"
)
var (
//go:embed pci.ids
embeddedPCIIDs string
loadOnce sync.Once
vendors map[int]string
devices map[string]string
)
// VendorName returns vendor name by PCI Vendor ID
func VendorName(vendorID int) string {
loadPCIIDs()
if name, ok := vendors[vendorID]; ok {
return name
}
return ""
}
// DeviceName returns device name by Vendor ID and Device ID
func DeviceName(vendorID, deviceID int) string {
loadPCIIDs()
key := fmt.Sprintf("%04x:%04x", vendorID, deviceID)
if name, ok := devices[key]; ok {
return name
}
return ""
}
// DeviceInfo returns both vendor and device name
func DeviceInfo(vendorID, deviceID int) (vendor, device string) {
vendor = VendorName(vendorID)
device = DeviceName(vendorID, deviceID)
return
}
// VendorNameFromString tries to parse vendor ID from string (hex) and return name
func VendorNameFromString(s string) string {
s = strings.TrimSpace(s)
if s == "" {
return ""
}
// Try to parse as hex (with or without 0x prefix)
s = strings.TrimPrefix(strings.ToLower(s), "0x")
var id int
for _, c := range s {
if c >= '0' && c <= '9' {
id = id*16 + int(c-'0')
} else if c >= 'a' && c <= 'f' {
id = id*16 + int(c-'a'+10)
} else {
return ""
}
}
return VendorName(id)
}
func loadPCIIDs() {
loadOnce.Do(func() {
vendors = make(map[int]string)
devices = make(map[string]string)
parsePCIIDs(strings.NewReader(embeddedPCIIDs), vendors, devices)
for _, path := range candidatePCIIDsPaths() {
f, err := os.Open(path)
if err != nil {
continue
}
parsePCIIDs(f, vendors, devices)
_ = f.Close()
}
})
}
func candidatePCIIDsPaths() []string {
paths := []string{
"pci.ids",
"/usr/share/hwdata/pci.ids",
"/usr/share/misc/pci.ids",
"/opt/homebrew/share/pciids/pci.ids",
}
// Env paths have highest priority, so they are applied last.
if env := strings.TrimSpace(os.Getenv("LOGPILE_PCI_IDS_PATH")); env != "" {
for _, p := range strings.Split(env, string(os.PathListSeparator)) {
p = strings.TrimSpace(p)
if p != "" {
paths = append(paths, p)
}
}
}
return paths
}
func parsePCIIDs(r interface{ Read([]byte) (int, error) }, outVendors map[int]string, outDevices map[string]string) {
scanner := bufio.NewScanner(r)
currentVendor := -1
for scanner.Scan() {
line := scanner.Text()
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Subdevice line (tab-tab) - ignored for now
if strings.HasPrefix(line, "\t\t") {
continue
}
// Device line
if strings.HasPrefix(line, "\t") {
if currentVendor < 0 {
continue
}
trimmed := strings.TrimLeft(line, "\t")
fields := strings.Fields(trimmed)
if len(fields) < 2 {
continue
}
deviceID, err := strconv.ParseInt(fields[0], 16, 32)
if err != nil {
continue
}
name := strings.TrimSpace(trimmed[len(fields[0]):])
if name == "" {
continue
}
key := fmt.Sprintf("%04x:%04x", currentVendor, int(deviceID))
outDevices[key] = name
continue
}
// Vendor line
fields := strings.Fields(line)
if len(fields) < 2 {
currentVendor = -1
continue
}
vendorID, err := strconv.ParseInt(fields[0], 16, 32)
if err != nil {
currentVendor = -1
continue
}
name := strings.TrimSpace(line[len(fields[0]):])
if name == "" {
currentVendor = -1
continue
}
currentVendor = int(vendorID)
outVendors[currentVendor] = name
}
}
var (
reverseOnce sync.Once
vendorIDsByName map[string][]int
deviceIDsByName map[int]map[string]int
ambiguousDeviceID = -1
)
func loadReverseIndex() {
loadPCIIDs()
reverseOnce.Do(func() {
vendorIDsByName = make(map[string][]int, len(vendors))
for id, name := range vendors {
vendorIDsByName[name] = append(vendorIDsByName[name], id)
}
for _, ids := range vendorIDsByName {
sort.Ints(ids)
}
deviceIDsByName = make(map[int]map[string]int)
for key, name := range devices {
vendorID, err1 := strconv.ParseInt(key[:4], 16, 32)
deviceID, err2 := strconv.ParseInt(key[5:], 16, 32)
if err1 != nil || err2 != nil {
continue
}
byName := deviceIDsByName[int(vendorID)]
if byName == nil {
byName = make(map[string]int)
deviceIDsByName[int(vendorID)] = byName
}
if prev, seen := byName[name]; seen && prev != int(deviceID) {
byName[name] = ambiguousDeviceID
continue
}
byName[name] = int(deviceID)
}
})
}
// LookupByDescription resolves a textual `lspci` device description
// ("<vendor name> <device name>", printed without numeric IDs) back to PCI
// IDs. The vendor is the longest pci.ids vendor name that prefixes the
// description; deviceName is the remainder. deviceID is resolved from an
// `lspci` "Device 1a2b" placeholder or from a device name that is unique
// within the vendor, and is 0 otherwise. ok is false when no vendor matches.
func LookupByDescription(description string) (vendorID, deviceID int, vendorName, deviceName string, ok bool) {
loadReverseIndex()
description = strings.TrimSpace(description)
for name := range vendorIDsByName {
if len(name) <= len(vendorName) || !strings.HasPrefix(description, name) {
continue
}
if len(description) > len(name) && description[len(name)] != ' ' {
continue
}
vendorName = name
}
if vendorName == "" {
return 0, 0, "", description, false
}
deviceName = strings.TrimSpace(description[len(vendorName):])
candidates := vendorIDsByName[vendorName]
vendorID = candidates[0]
if hex, found := strings.CutPrefix(deviceName, "Device "); found && len(hex) == 4 {
if id, err := strconv.ParseInt(hex, 16, 32); err == nil {
return vendorID, int(id), vendorName, deviceName, true
}
}
for _, candidate := range candidates {
if id, found := deviceIDsByName[candidate][deviceName]; found && id != ambiguousDeviceID {
return candidate, id, vendorName, deviceName, true
}
}
return vendorID, 0, vendorName, deviceName, true
}