#!/usr/bin/env bash # Install ipu-bridge with the Dell XPS 16 (DA16250) upside-down camera quirk # for Fedora kernels that don't ship it yet, plus a kernel-install hook that # rebuilds it for every newly installed kernel. # # sudo ./install.sh running kernel + hook # sudo ./install.sh --kver KVER one specific kernel (used by the hook) # sudo ./install.sh --uninstall remove all overrides and the hook # # Once a kernel's stock ipu-bridge contains the quirk (it is in mainline), # the override for that kernel is removed instead of rebuilt. set -euo pipefail NAME=ipu-bridge-da16250 QUIRK='XPS 16 (Dell 16 Premium) DA16250' SHARE=/usr/local/src/$NAME HOOK=/etc/kernel/install.d/90-$NAME.install SELF_DIR=$(dirname "$(readlink -f "$0")") SRC=$SELF_DIR/ipu-bridge WORK=$(mktemp -d) trap 'rm -rf "$WORK"' EXIT die() { echo "$NAME: $*" >&2; exit 1; } # String literals of a module's .rodata, minus __FILE__ paths, so a signed # distro build and our unsigned one compare equal when the source matches. ro_strings() { local ko sec ko=$(mktemp -p "$WORK") # unique: callers run this in parallel via <(...) case $1 in *.xz) xz -dc "$1" >"$ko" ;; *.zst) zstd -qdc "$1" >"$ko" ;; *) cp "$1" "$ko" ;; esac for sec in $(readelf -SW "$ko" | grep -oE '\.rodata[^ ]*' | sort -u); do objcopy -O binary --only-section="$sec" "$ko" /dev/stdout 2>/dev/null done | strings -n4 | grep -vE '\.[ch]$' | sort -u } remove_for() { if [ -e "/lib/modules/$1/updates/ipu-bridge.ko" ]; then rm -f "/lib/modules/$1/updates/ipu-bridge.ko" depmod -a "$1" echo "$1: removed patched ipu-bridge" fi } install_for() { local kver=$1 stock ko=$WORK/build/ipu-bridge.ko stock=$(ls "/lib/modules/$kver/kernel/drivers/media/pci/intel/"ipu-bridge.ko* 2>/dev/null | head -1) [ -n "$stock" ] || die "$kver: no stock ipu-bridge module" if ro_strings "$stock" | grep -xF "$QUIRK" >/dev/null; then echo "$kver: stock ipu-bridge already has the quirk, no override needed" remove_for "$kver" return fi [ -e "/lib/modules/$kver/build/Makefile" ] || die "$kver: kernel-devel-$kver is not installed" rm -rf "$WORK/build" && mkdir "$WORK/build" cp "$SRC/ipu-bridge.c" "$SRC/Makefile" "$WORK/build/" make -s -C "/lib/modules/$kver/build" M="$WORK/build" modules >"$WORK/make.log" 2>&1 || { cat "$WORK/make.log" >&2; die "$kver: build failed"; } strip --strip-debug "$ko" [ "$(modinfo -F vermagic "$ko" | cut -d' ' -f1)" = "$kver" ] || die "$kver: vermagic mismatch" # Never replace a newer distro module with our older copy of the source. diff <(ro_strings "$stock") <(ro_strings "$ko" | grep -vxF "$QUIRK") >/dev/null || die "$kver: stock ipu-bridge differs from ipu-bridge/ipu-bridge.c beyond the quirk; refresh the source" install -Dm644 "$ko" "/lib/modules/$kver/updates/ipu-bridge.ko" depmod -a "$kver" echo "$kver: installed patched ipu-bridge" } [ "$(id -u)" = 0 ] || die "must run as root" [ "$(cat /sys/class/dmi/id/product_name)" = "$QUIRK" ] || die "this is not a $QUIRK" case ${1:-} in --uninstall) for m in /lib/modules/*/updates/ipu-bridge.ko; do [ -e "$m" ] && remove_for "$(basename "$(dirname "$(dirname "$m")")")" done rm -f "$HOOK" rm -rf "$SHARE" echo "Uninstalled. Reboot to go back to the stock module." ;; --kver) install_for "${2:?--kver needs a kernel version}" ;; "") install_for "$(uname -r)" # Root-owned copy for the hook, so it never runs code from a user's home. install -d "$SHARE/ipu-bridge" install -m644 "$SRC/ipu-bridge.c" "$SRC/Makefile" "$SHARE/ipu-bridge/" install -m755 "$SELF_DIR/install.sh" "$SHARE/install.sh" cat >"$HOOK" <>/var/log/$NAME.log 2>&1 || echo "$NAME: failed for \$2, see /var/log/$NAME.log" >&2 exit 0 EOF chmod 755 "$HOOK" echo "Hook installed: $HOOK" echo "Reboot to load the patched module." ;; *) die "unknown argument: $1" ;; esac