180 lines
5.4 KiB
Go
180 lines
5.4 KiB
Go
// Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
|
|
|
package main
|
|
|
|
// Missing drivers and firmware, resolved on the box (docs/hardware-support.md,
|
|
// step 3). Every Cantabile image carries a small hardware database made from the
|
|
// full kernel and firmware packages before an edition removes anything
|
|
// (/usr/lib/cantabile/hwdb, mkosi.postinst.chroot): the kernel's modules.alias and
|
|
// the list of firmware files. With it the box tells, for each device without
|
|
// a driver and each firmware file the kernel did not find, which case it is:
|
|
//
|
|
// device: driver-here a module of this Cantabile matches (blacklisted, or
|
|
// its probe failed)
|
|
// other-edition Cantabile's kernel has a module, this edition not
|
|
// not-in-cantabile no module in Cantabile's kernel (a driver pack with
|
|
// an out-of-tree driver, or a device it can't use)
|
|
// firmware: here the file is on this box (the load failed for
|
|
// another reason, e.g. too early at boot)
|
|
// other-edition in Cantabile's packages, removed in this edition
|
|
// not-in-cantabile not in Cantabile's packages (a firmware pack)
|
|
//
|
|
// And why a device has no driver on this box (the box
|
|
// knows best), from its modules, the modprobe
|
|
// configuration and the kernel log (hwreason.go): reason not-loaded,
|
|
// blacklisted, loaded-not-bound, probe-failed (with the error) or
|
|
// needs-firmware (with the files); a missing firmware file carries the
|
|
// device that asked for it.
|
|
|
|
import (
|
|
"bufio"
|
|
"compress/gzip"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
var hwdbDir = "/usr/lib/cantabile/hwdb"
|
|
|
|
type hwDev struct {
|
|
Bus, ID string
|
|
Modalias []string
|
|
}
|
|
|
|
type hwResolved struct {
|
|
Kind string `json:"kind"` // device | firmware
|
|
Key string `json:"key"` // "pci 8086:0be1" | firmware path
|
|
Modules []string `json:"modules,omitempty"`
|
|
Verdict string `json:"verdict"`
|
|
Modalias []string `json:"modalias,omitempty"`
|
|
// Why a device has no driver (hwreason.go); for a
|
|
// firmware file, the device that asked for it ("usb 0451:3410").
|
|
Reason string `json:"reason,omitempty"`
|
|
Error int `json:"error,omitempty"` // the probe's error number (-2)
|
|
ErrorText string `json:"error_text,omitempty"` // and what it means
|
|
Firmware []string `json:"firmware,omitempty"` // needs-firmware: the files the log names
|
|
Device string `json:"device,omitempty"`
|
|
}
|
|
|
|
// gzLines: the lines of a gzipped text file (nil if it can't be read).
|
|
func gzLines(p string) []string {
|
|
f, err := os.Open(p)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
defer f.Close()
|
|
z, err := gzip.NewReader(f)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var out []string
|
|
sc := bufio.NewScanner(z)
|
|
sc.Buffer(make([]byte, 64*1024), 1024*1024)
|
|
for sc.Scan() {
|
|
out = append(out, sc.Text())
|
|
}
|
|
return out
|
|
}
|
|
|
|
func modName(s string) string { return strings.ReplaceAll(s, "-", "_") }
|
|
|
|
// modulesHere: the modules this box's kernel tree has (modules.dep).
|
|
func modulesHere(root string) map[string]bool {
|
|
here := map[string]bool{}
|
|
b, err := os.ReadFile(filepath.Join(root, "modules.dep"))
|
|
if err != nil {
|
|
return here
|
|
}
|
|
for _, l := range strings.Split(string(b), "\n") {
|
|
f, _, ok := strings.Cut(l, ":")
|
|
if !ok {
|
|
continue
|
|
}
|
|
n := filepath.Base(f)
|
|
if i := strings.Index(n, ".ko"); i > 0 {
|
|
here[modName(n[:i])] = true
|
|
}
|
|
}
|
|
return here
|
|
}
|
|
|
|
// fwName: a firmware path without the compression the kernel adds itself.
|
|
func fwName(s string) string {
|
|
for _, x := range []string{".zst", ".xz"} {
|
|
s = strings.TrimSuffix(s, x)
|
|
}
|
|
return s
|
|
}
|
|
|
|
func resolveHardware(devs []hwDev, missingFirmware []string, kernelRoot, firmwareRoot string) []hwResolved {
|
|
out := []hwResolved{}
|
|
if len(devs) == 0 && len(missingFirmware) == 0 {
|
|
return out
|
|
}
|
|
type alias struct{ pattern, module string }
|
|
var aliases []alias
|
|
for _, l := range gzLines(filepath.Join(hwdbDir, "modules.alias.gz")) {
|
|
f := strings.Fields(l)
|
|
if len(f) == 3 && f[0] == "alias" {
|
|
aliases = append(aliases, alias{f[1], modName(f[2])})
|
|
}
|
|
}
|
|
here := modulesHere(kernelRoot)
|
|
for _, d := range devs {
|
|
mods := map[string]bool{}
|
|
for _, ma := range d.Modalias {
|
|
for _, a := range aliases {
|
|
if ok, _ := path.Match(a.pattern, ma); ok {
|
|
mods[a.module] = true
|
|
}
|
|
}
|
|
}
|
|
r := hwResolved{Kind: "device", Key: d.Bus + " " + d.ID, Modalias: d.Modalias}
|
|
for m := range mods {
|
|
r.Modules = append(r.Modules, m)
|
|
}
|
|
sort.Strings(r.Modules)
|
|
switch {
|
|
case len(aliases) == 0:
|
|
r.Verdict = "unknown" // no database (an image from before it)
|
|
case len(r.Modules) == 0:
|
|
r.Verdict = "not-in-cantabile"
|
|
default:
|
|
r.Verdict = "other-edition"
|
|
for _, m := range r.Modules {
|
|
if here[m] {
|
|
r.Verdict = "driver-here"
|
|
}
|
|
}
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
known := map[string]bool{}
|
|
for _, l := range gzLines(filepath.Join(hwdbDir, "firmware.txt.gz")) {
|
|
known[fwName(l)] = true
|
|
}
|
|
for _, fw := range missingFirmware {
|
|
r := hwResolved{Kind: "firmware", Key: fw}
|
|
onBox := false
|
|
for _, x := range []string{"", ".zst", ".xz"} {
|
|
if _, err := os.Stat(filepath.Join(firmwareRoot, fw+x)); err == nil {
|
|
onBox = true
|
|
}
|
|
}
|
|
switch {
|
|
case len(known) == 0:
|
|
r.Verdict = "unknown"
|
|
case onBox:
|
|
r.Verdict = "here"
|
|
case known[fwName(fw)]:
|
|
r.Verdict = "other-edition"
|
|
default:
|
|
r.Verdict = "not-in-cantabile"
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
return out
|
|
}
|