Feat/remote repository #31
@@ -0,0 +1,104 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Print pacman NoExtract lines for firmware the ISO's kernel never loads.
|
||||
|
||||
usage: firmware-noextract.py <linux pkg> <linux-firmware-intel pkg> <linux-firmware-nvidia pkg>
|
||||
|
||||
Two drivers ship several versions of the same firmware and load one:
|
||||
- iwlwifi (Intel Wi-Fi) tries, for each chip, its highest supported API
|
||||
version first and goes down until a file exists: every version below the
|
||||
highest one present is never read (~55 MiB).
|
||||
- nouveau (NVIDIA GSP) takes the newest GSP release it supports: older
|
||||
releases are never read when the newest one is present (~38 MiB).
|
||||
The versions a driver supports are its MODULE_FIRMWARE declarations, read from
|
||||
the .modinfo of the very kernel package the ISO installs, so the result
|
||||
follows kernel and firmware updates. Only versions below one that is kept are
|
||||
excluded: a newer kernel would load the same file or a newer one, never an
|
||||
excluded one.
|
||||
"""
|
||||
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
PER_LINE = 12
|
||||
|
||||
|
||||
def package_files(package):
|
||||
return subprocess.run(["bsdtar", "-tf", package], check=True,
|
||||
capture_output=True, text=True).stdout.split()
|
||||
|
||||
|
||||
def module_firmware(linux_package, module):
|
||||
"""MODULE_FIRMWARE entries of `module`, read from its .modinfo strings."""
|
||||
members = [path for path in package_files(linux_package)
|
||||
if re.search(rf"/{module}\.ko(\.[a-z]+)?$", path)]
|
||||
if not members:
|
||||
sys.exit(f"firmware-noextract: {module} not found in {linux_package}")
|
||||
data = subprocess.run(["bsdtar", "-xOf", linux_package, members[0]],
|
||||
check=True, capture_output=True).stdout
|
||||
if members[0].endswith(".zst"):
|
||||
data = subprocess.run(["zstd", "-dcq"], input=data, check=True,
|
||||
capture_output=True).stdout
|
||||
elif members[0].endswith(".xz"):
|
||||
data = subprocess.run(["xz", "-dcq"], input=data, check=True,
|
||||
capture_output=True).stdout
|
||||
return {match.decode() for match in re.findall(rb"firmware=([^\0]+)\0", data)}
|
||||
|
||||
|
||||
def iwlwifi_unused(declared, files):
|
||||
# iwlwifi-<chip>-<api>.ucode, where <api> is a number or c<number>.
|
||||
name = re.compile(r"^usr/lib/firmware/intel/iwlwifi/iwlwifi-(.+)-(c?)(\d+)\.ucode(\.\w+)?$")
|
||||
highest = {}
|
||||
for entry in declared:
|
||||
match = re.match(r"^iwlwifi-(.+)-(c?)(\d+)\.ucode$", entry)
|
||||
if match:
|
||||
highest[match.group(1, 2)] = int(match.group(3))
|
||||
present = {}
|
||||
for path in files:
|
||||
match = name.match(path)
|
||||
# Chips the module does not declare are left alone: their file names
|
||||
# are built at run time and nothing says which version is loaded.
|
||||
if match and match.group(1, 2) in highest:
|
||||
present.setdefault(match.group(1, 2), []).append((int(match.group(3)), path))
|
||||
unused = []
|
||||
for chip, versions in present.items():
|
||||
loadable = [version for version, _ in versions if version <= highest[chip]]
|
||||
if loadable:
|
||||
kept = max(loadable)
|
||||
unused += [path for version, path in versions if version < kept]
|
||||
return unused
|
||||
|
||||
|
||||
def nouveau_unused(declared, files):
|
||||
releases = {match.group(1) for entry in declared
|
||||
if (match := re.search(r"/gsp/[a-z_]+-([0-9.]+)\.bin$", entry))}
|
||||
if len(releases) < 2:
|
||||
return []
|
||||
def key(release):
|
||||
return tuple(int(part) for part in release.split("."))
|
||||
newest = max(releases, key=key)
|
||||
older = [release for release in releases if key(release) < key(newest)]
|
||||
# Per GPU: an older release goes only where the newest one is present.
|
||||
with_newest = {path.split("/gsp/")[0] for path in files
|
||||
if "/gsp/" in path and f"-{newest}.bin" in path}
|
||||
return [path for path in files
|
||||
if "/gsp/" in path and path.split("/gsp/")[0] in with_newest
|
||||
and any(f"-{release}.bin" in path for release in older)]
|
||||
|
||||
|
||||
def main(linux_package, intel_package, nvidia_package):
|
||||
patterns = iwlwifi_unused(module_firmware(linux_package, "iwlwifi"),
|
||||
package_files(intel_package))
|
||||
patterns += nouveau_unused(module_firmware(linux_package, "nouveau"),
|
||||
package_files(nvidia_package))
|
||||
patterns.sort()
|
||||
# pacman reads its configuration in lines of at most 4 KiB: a longer one
|
||||
# is cut and the rest taken for an unknown directive.
|
||||
for start in range(0, len(patterns), PER_LINE):
|
||||
print("NoExtract = " + " ".join(patterns[start:start + PER_LINE]))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 4:
|
||||
sys.exit(__doc__.strip().splitlines()[2])
|
||||
main(*sys.argv[1:])
|
||||
Reference in new issue
Block a user