-
Notifications
You must be signed in to change notification settings - Fork 126
Wip firmware exclusion refacto #3818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ycardaillac
wants to merge
1
commit into
master
Choose a base branch
from
wip_firmware_exclusion_refacto
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
191 changes: 191 additions & 0 deletions
191
meta-balena-common/classes/balena-firmware-exclusion.bbclass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| # | ||
| # Build non-essential firmware package lists from firmware_metadata.json. | ||
| # Produces: | ||
| # - DEPLOY_DIR_IMAGE/nonessential_firmware.txt (debug with reasons) | ||
| # | ||
|
|
||
| BALENA_FIRMWARE_EXCLUSION_ENABLED ?= "1" | ||
|
|
||
| def _is_firmware_exclusion_enabled(d): | ||
| value = (d.getVar('BALENA_FIRMWARE_EXCLUSION_ENABLED') or "").strip() | ||
| return value == "1" | ||
|
|
||
| # Read firmware metadata produced by balena-firmware-sort, then compute the | ||
| # nonessential package set including interface-incompatible firmware. | ||
| def _compute_nonessential_firmware_from_metadata(d): | ||
| import json | ||
| import os | ||
|
|
||
| deploy_dir = d.getVar('DEPLOY_DIR_IMAGE') | ||
| if not deploy_dir: | ||
| bb.fatal("Could not determine DEPLOY_DIR_IMAGE") | ||
|
|
||
| metadata_path = os.path.join(deploy_dir, 'firmware_metadata.json') | ||
| if not os.path.exists(metadata_path): | ||
| bb.fatal(f"firmware_metadata.json not found at: {metadata_path}") | ||
|
|
||
| packages = {} | ||
| with open(metadata_path, 'r') as f: | ||
| packages = json.load(f) | ||
|
|
||
| machine_features = set((d.getVar('MACHINE_FEATURES') or "").split()) | ||
| nonessential = {} | ||
|
|
||
| for pkg, pkg_meta in packages.items(): | ||
| categories = set(pkg_meta.get('categories', [])) | ||
| interfaces = set(pkg_meta.get('interfaces', [])) | ||
| in_essential_category = bool(pkg_meta.get('in_essential_category', False)) | ||
| reasons = set() | ||
|
|
||
| # Exclude a package when: | ||
| # A) it is NOT in an essential category (Connectivity or Storage), OR | ||
| # B) it declares interfaces and NONE of them match MACHINE_FEATURES. | ||
| # | ||
| # This means: | ||
| # - Nonessential categories are always excluded. | ||
| # - Essential-category packages are excluded only on interface mismatch. | ||
| # - Essential-category packages with matching interfaces are kept. | ||
| if not in_essential_category: | ||
| reasons.update( | ||
| sorted(cat for cat in categories if cat not in {"Connectivity", "Storage"}) | ||
| ) | ||
| if interfaces and not (interfaces & machine_features): | ||
| reasons.add(f"UnsupportedInterfaces({','.join(sorted(interfaces))})") | ||
|
|
||
| if reasons: | ||
| nonessential[pkg] = reasons | ||
|
|
||
| return nonessential | ||
|
|
||
| def _get_allowed_firmware_whitelist(d): | ||
| raw_whitelist = d.getVar('BALENA_ALLOWED_FIRMWARE_PACKAGES') or "" | ||
| return {pkg.strip() for pkg in raw_whitelist.split() if pkg.strip()} | ||
|
|
||
| # Generate DEPLOY_DIR_IMAGE/nonessential_firmware.txt from firmware_metadata.json | ||
| # for the current image/machine context. | ||
| python do_generate_nonessential_firmware_from_metadata() { | ||
| import os | ||
|
|
||
| image = d.getVar('PN') | ||
| machine = d.getVar('MACHINE') | ||
| machine_features = sorted((d.getVar('MACHINE_FEATURES') or "").split()) | ||
| bb.note(f"[fw-meta-excl] IMAGE={image} MACHINE={machine}") | ||
| bb.note(f"[fw-meta-excl] MACHINE_FEATURES(sorted)={','.join(machine_features)}") | ||
|
|
||
| deploy_dir = d.getVar('DEPLOY_DIR_IMAGE') | ||
| if not deploy_dir: | ||
| bb.fatal("Could not determine DEPLOY_DIR_IMAGE") | ||
|
|
||
| nonessential = _compute_nonessential_firmware_from_metadata(d) | ||
| whitelist = _get_allowed_firmware_whitelist(d) | ||
| if whitelist: | ||
| bb.note(f"Allowed firmware whitelist: {repr(sorted(whitelist))}") | ||
|
|
||
| debug_path = os.path.join(deploy_dir, 'nonessential_firmware.txt') | ||
| with open(debug_path, 'w') as f: | ||
| for pkg in sorted(nonessential.keys()): | ||
| line = f"{pkg} : {', '.join(sorted(nonessential[pkg]))}" | ||
| # Keep raw policy visibility while making whitelist effect explicit. | ||
| if pkg in whitelist: | ||
| f.write(f"# {line} # whitelisted\n") | ||
| else: | ||
| f.write(f"{line}\n") | ||
|
|
||
| bb.note(f"Wrote metadata-based nonessential firmware debug list to: {debug_path}") | ||
| } | ||
|
|
||
| do_generate_nonessential_firmware_from_metadata[depends] += "linux-firmware:do_deploy" | ||
| addtask generate_nonessential_firmware_from_metadata before do_rootfs | ||
|
|
||
|
|
||
|
|
||
| def _get_nonessential_firmware_packages(d): | ||
| nonessential_path = get_nonessential_firmware_path(d) | ||
| packages = [] | ||
| with open(nonessential_path, 'r') as f: | ||
| for line in f: | ||
| line = line.strip() | ||
| if not line or line.startswith('#'): | ||
| continue | ||
| pkg = line.split(':', 1)[0].strip() | ||
| if pkg: | ||
| packages.append(pkg) | ||
| return packages | ||
|
|
||
| def get_nonessential_firmware_path(d): | ||
| import os | ||
|
|
||
| deploy_dir = d.getVar('DEPLOY_DIR_IMAGE') | ||
| if not deploy_dir: | ||
| bb.fatal("Could not determine DEPLOY_DIR_IMAGE") | ||
|
|
||
| nonessential_path = os.path.join(deploy_dir, 'nonessential_firmware.txt') | ||
| if not os.path.exists(nonessential_path): | ||
| bb.fatal("nonessential_firmware.txt not found.") | ||
|
|
||
| return nonessential_path | ||
|
|
||
| # Add excluded firmware from nonessential_firmware.txt to BAD_RECOMMENDATIONS | ||
| python do_apply_firmware_exclusion_policy() { | ||
| if not _is_firmware_exclusion_enabled(d): | ||
| bb.note("Firmware exclusion is disabled; skipping BAD_RECOMMENDATIONS updates") | ||
| return | ||
|
|
||
| extra_bad = _get_nonessential_firmware_packages(d) | ||
|
|
||
| if extra_bad: | ||
| bad_str = " ".join(extra_bad) | ||
| # BAD_RECOMMENDATIONS is used to remove packages from RRECOMMENDS | ||
| d.appendVar('BAD_RECOMMENDATIONS', " " + bad_str) | ||
| bb.note(f"Policy applied: Excluded {len(extra_bad)} firmware packages.") | ||
| } | ||
|
|
||
| addtask do_apply_firmware_exclusion_policy after do_generate_nonessential_firmware_from_metadata before do_rootfs | ||
|
|
||
|
|
||
|
|
||
| # Fail the build if any of the excluded packages have been found in the image manifest | ||
| python do_nonessential_firmware_check() { | ||
| import os | ||
|
|
||
| if not _is_firmware_exclusion_enabled(d): | ||
| bb.note("Firmware exclusion is disabled; skipping manifest enforcement") | ||
| return | ||
|
|
||
| # During do_image_complete, this variable points to the manifest in WORKDIR | ||
| manifest_path = d.getVar('IMAGE_MANIFEST') | ||
|
|
||
| if not manifest_path or not os.path.exists(manifest_path): | ||
| # Fallback to check the deploy directory manually | ||
| deploy_dir = d.getVar('DEPLOY_DIR_IMAGE') | ||
| link_name = d.getVar('IMAGE_LINK_NAME') | ||
| manifest_path = os.path.join(deploy_dir, f"{link_name}.manifest") | ||
|
|
||
| if not os.path.exists(manifest_path): | ||
| bb.fatal(f"Firmware policy check failed: Manifest file not found in {manifest_path}") | ||
|
|
||
| effective_excluded = _get_nonessential_firmware_packages(d) | ||
|
|
||
| # Parse image manifest | ||
| installed = [] | ||
| with open(manifest_path, 'r') as f: | ||
| for line in f: | ||
| parts = line.split() | ||
| if parts: | ||
| installed.append(parts[0]) | ||
|
|
||
| # Check if any effective excluded package still landed in final manifest. | ||
| matched_packages = [p for p in effective_excluded if p in installed] | ||
|
|
||
| if matched_packages: | ||
| bb.fatal(f"Non-essential firmware found in manifest: {', '.join(matched_packages)}. " | ||
| f"Please check which categories these packages belong to in firmware_metadata.json/nonessential_firmware.txt " | ||
| f"or add them to BALENA_ALLOWED_FIRMWARE_PACKAGES") | ||
| else: | ||
| bb.plain("Firmware Policy Check: PASSED") | ||
| } | ||
|
|
||
| # Manifest is generated in do_rootfs, | ||
| # we thus need to perform the check after | ||
| # it becomes available. | ||
| addtask nonessential_firmware_check after do_rootfs before do_image_complete |
100 changes: 100 additions & 0 deletions
100
meta-balena-common/classes/balena-firmware-sort-data.bbclass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| python () { | ||
| # Inverted: driver -> category for O(1) lookup. | ||
| # any WHENCE driver used by linux-firmware must be | ||
| # mapped here, otherwise parsing will fail on "Uncategorized driver". | ||
| global firmware_sort_driver_categories | ||
| global firmware_sort_skip_list | ||
| firmware_sort_driver_categories = { | ||
| "amdgpu": "GPU", "isp": "GPU", "tegra-vic": "GPU", "nouveau": "GPU", | ||
| "radeon": "GPU", "i915": "GPU", "xe": "GPU", "adreno": "GPU", | ||
| "amdxdna": "GPU", "intel_vpu": "GPU", "amphion": "GPU", "powervr": "GPU", | ||
| "panthor": "GPU", | ||
| "snd-korg1212": "Audio", "snd-maestro3": "Audio", "snd-ymfpci": "Audio", | ||
| "emi26": "Audio", "emi62": "Audio", "snd-sb16-csp": "Audio", | ||
| "snd-wavefront": "Audio", "snd-hda-codec-ca0132": "Audio", | ||
| "snd_soc_sst_acpi": "Audio", "snd_soc_catpt": "Audio", "snd_soc_avs": "Audio", | ||
| "snd_intel_sst_core": "Audio", "snd-soc-skl": "Audio", "cs35l41": "Audio", | ||
| "cs35l41_hda": "Audio", "cs35l56": "Audio", "cs42l43": "Audio", "cs42l45": "Audio", | ||
| "mtk-sof": "Audio", "qcom-sc8280xp": "Audio", "qcom-qcs6490": "Audio", | ||
| "qcom-qcs8300": "Audio", "qcom-qcs9100": "Audio", "qcom-sm8550": "Audio", | ||
| "qcom-sm8650": "Audio", "qcom-x1e80100": "Audio", "ti-tas2781": "Audio", | ||
| "ti-tas2563": "Audio", "qcm6490": "Audio", "qcs615": "Audio", | ||
| "sm8450": "Audio", "sm8750": "Audio", | ||
| "atomisp": "Video", "ipu3-imgu": "Video", "intel-ipu6-isys": "Video", | ||
| "intel-ipu7-isys": "Video", "mei-vsc-hw": "Video", "dvb-ttusb-budget": "Video", | ||
| "cpia2": "Video", "dabusb": "Video", "vicam": "Video", "cx231xx": "Video", | ||
| "cx23418": "Video", "cx23885": "Video", "cx23840": "Video", "dvb-ttpci": "Video", | ||
| "xc4000": "Video", "xc5000": "Video", "dib0700": "Video", "lgs8gxx": "Video", | ||
| "ti-vpe": "Video", "tlg2300": "Video", "drxk": "Video", "s5p-mfc": "Video", | ||
| "as102": "Video", "it9135": "Video", "smsmdtv": "Video", "mtk-vpu": "Video", | ||
| "venus": "Video", "iris": "Video", "meson-vdec": "Video", "mt8196": "Video", | ||
| "mga": "Video", "r128": "Video", "s2255drv": "Video", "go7007": "Video", | ||
| "rk3399-dptx": "Video", "cdns-mhdp": "Video", "lt9611uxc": "Video", | ||
| "wave5": "Video", "wave6": "Video", "ast": "Video", | ||
| "sdx61": "Connectivity", "sdx35": "Connectivity", "ar9170": "Connectivity", | ||
| "ath9k_htc": "Connectivity", "ath6kl": "Connectivity", "ar5523": "Connectivity", | ||
| "carl9170": "Connectivity", "wil6210": "Connectivity", "ath10k": "Connectivity", | ||
| "ath11k": "Connectivity", "ath12k": "Connectivity", "bnx2x": "Connectivity", | ||
| "bnx2": "Connectivity", "brcmsmac": "Connectivity", "brcmfmac": "Connectivity", | ||
| "cxgb3": "Connectivity", "cxgb4": "Connectivity", "mt7601u": "Connectivity", | ||
| "mt76x0": "Connectivity", "mt76x2e": "Connectivity", "mt76x2u": "Connectivity", | ||
| "mt7615e": "Connectivity", "mt7622": "Connectivity", "mt7663": "Connectivity", | ||
| "mt7915e": "Connectivity", "mt7920": "Connectivity", "mt7921": "Connectivity", | ||
| "mt7922": "Connectivity", "mt7925": "Connectivity", "mt7988": "Connectivity", | ||
| "mtk-2p5ge": "Connectivity", "mt7996e": "Connectivity", "mtk_wed": "Connectivity", | ||
| "mwifiex": "Connectivity", "orinoco": "Connectivity", "slicoss": "Connectivity", | ||
| "sxg": "Connectivity", "e100": "Connectivity", "acenic": "Connectivity", | ||
| "tg3": "Connectivity", "starfire": "Connectivity", "tehuti": "Connectivity", | ||
| "typhoon": "Connectivity", "myri_sbus": "Connectivity", "netxen_nic": "Connectivity", | ||
| "rt61pci": "Connectivity", "as21xxx": "Connectivity", "en8811h": "Connectivity", | ||
| "an8811hb": "Connectivity", "airoha-npu-7581": "Connectivity", | ||
| "airoha-npu-7583": "Connectivity", "vxge": "Connectivity", "myri10ge": "Connectivity", | ||
| "cw1200": "Connectivity", "wilc1000": "Connectivity", "ice": "Connectivity", | ||
| "nfp": "Connectivity", "mlxsw_spectrum": "Connectivity", "prestera": "Connectivity", | ||
| "qla2xxx": "Connectivity", "ib_qib": "Connectivity", "qed": "Connectivity", | ||
| "BFA/BNA": "Connectivity", "rt2800pci": "Connectivity", "rt2860sta": "Connectivity", | ||
| "rt2800usb": "Connectivity", "rt2870sta": "Connectivity", "rtl8192e": "Connectivity", | ||
| "r8712u": "Connectivity", "rtl8192ce": "Connectivity", "rtl8192cu": "Connectivity", | ||
| "rtl8192se": "Connectivity", "rtl8192de": "Connectivity", "rtl8192du": "Connectivity", | ||
| "rtl8723e": "Connectivity", "rtl8723be": "Connectivity", "rtl8723de": "Connectivity", | ||
| "r8723au": "Connectivity", "rtl8188ee": "Connectivity", "rtl8188eu": "Connectivity", | ||
| "rtl8821ae": "Connectivity", "rtl8822be": "Connectivity", "rtw88": "Connectivity", | ||
| "rtw89": "Connectivity", "rtl8192ee": "Connectivity", "rtl8723bs": "Connectivity", | ||
| "rtl8xxxu": "Connectivity", "r8169": "Connectivity", "r8152": "Connectivity", | ||
| "rt1320": "Connectivity", "wl12xx": "Connectivity", "wl18xx": "Connectivity", | ||
| "cc33xx": "Connectivity", "ueagle-atm": "Connectivity", "kaweth": "Connectivity", | ||
| "rt73usb": "Connectivity", "vt6656": "Connectivity", "rsi": "Connectivity", | ||
| "atusb": "Connectivity", "liquidio": "Connectivity", "iwlwifi": "Connectivity", | ||
| "libertas": "Connectivity", "mwl8k": "Connectivity", "mwlwifi": "Connectivity", | ||
| "wl1251": "Connectivity", "hfi1": "Connectivity", "qcom_q6v5_mss": "Connectivity", | ||
| "ixp4xx-npe": "Connectivity", "pcnet_cs": "Connectivity", "3c589_cs": "Connectivity", | ||
| "3c574_cs": "Connectivity", "smc91c92_cs": "Connectivity", "mscc-phy": "Connectivity", | ||
| "wfx": "Connectivity", | ||
| "ath3k": "Bluetooth", "DFU": "Bluetooth", "Atheros": "Bluetooth", | ||
| "btusb": "Bluetooth", "qca": "Bluetooth", "btqca": "Bluetooth", | ||
| "amlogic": "Bluetooth", "BCM-0bb4-0306": "Bluetooth", "btmtk_usb": "Bluetooth", | ||
| "btmtk": "Bluetooth", "TI_ST": "Bluetooth", "btnxpuart": "Bluetooth", | ||
| "isci": "Storage", "qla1280": "Storage", "qlogicpti": "Storage", | ||
| "xhci-tegra": "Storage", "advansys": "Storage", "ene-ub6250": "Storage", | ||
| "xhci-rcar": "Storage", "imx-sdma": "Storage", "microcode_amd": "Storage", | ||
| "qat": "Misc", "ish": "Misc", "qcom_q6v5_pas": "Misc", "qaic": "Misc", | ||
| "qdu100": "Misc", "qcom-geni-se": "Misc", "knav_qmss_queue": "Misc", | ||
| "fsl-mc": "Misc", "dsp56k": "Misc", "cassini": "Misc", "yam": "Misc", | ||
| "serial_cs": "Misc", "usbdux/usbduxfast/usbduxsigma": "Misc", "amd_pmf": "Misc", | ||
| "ccp": "Misc", "nitrox": "Misc", "inside-secure": "Misc", "rvu_cptpf": "Misc", | ||
| "nxp-sr1xx": "Misc", "Mont-TSSE": "Misc", "bmi260": "Misc", | ||
| "pcie-rcar-gen4": "Misc", "keyspan": "Misc", "keyspan_pda": "Misc", | ||
| "ti_usb_3410_5052": "Misc", "whiteheat": "Misc", "io_edgeport": "Misc", | ||
| "io_ti": "Misc", "rp2": "Misc", "mxu11x0": "Misc", "mxuport": "Misc", | ||
| "mtk_scp": "Misc", | ||
| } | ||
|
|
||
| # Skip non-runtime/meta package outputs from categorization. | ||
| # In particular, the generic linux-firmware umbrella package is excluded to | ||
| # keep metadata focused on concrete split firmware packages. | ||
| firmware_sort_skip_list = [ | ||
| "linux-firmware-license", "linux-firmware-dev", "linux-firmware-doc", | ||
| "linux-firmware-locale", "linux-firmware", "linux-firmware-dbg", | ||
| "linux-firmware-staticdev" | ||
| ] | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have these categories been produced by manually parsing a WHENCE file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it's derivated from OpenSuse classification, we probably had to add drivers to it as well.