rtl8127-omega
aien-dev/rtl8127-omega
Brought by drakestapleton
rtl8127-omega / install.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # rtl8127-omega installer. |
| 4 | # |
| 5 | # curl -fsSL https://raw.githubusercontent.com/aien-dev/rtl8127-omega/main/install.sh | sh |
| 6 | # |
| 7 | # EXPERIMENTAL. It has never run against a real chip. Realtek publishes no register document for the RTL8127. |
| 8 | # |
| 9 | # What the one-liner does (and ONLY this): |
| 10 | # 1. looks for an RTL8127 network card (PCI 10ec:8127) and tells you what it found; |
| 11 | # 2. downloads ONE file (rtl8127-probe, an Omega program) from this repository at a pinned commit and checks its sha256; |
| 12 | # 3. installs it under your home folder (no root needed for this); |
| 13 | # 4. runs it once, read-only, with sudo, to show the chip id, MAC address and link state. |
| 14 | # It does NOT change your network, unbind your network driver, or load anything into the kernel. Your network keeps working. |
| 15 | # |
| 16 | # Other commands (sh -s -- COMMAND): check | install | takeover | restore | uninstall | help |
| 17 | # check read-only report, changes nothing |
| 18 | # takeover hands the card to the Omega driver, with an automatic timed rollback. NOT AVAILABLE in this release: |
| 19 | # the Omega driver cannot carry network traffic yet (see docs/GAPS.md). The safety rules below are built and tested. |
| 20 | # restore gives the card back to the stock driver |
| 21 | # uninstall removes what install put in your home folder (and restores the card if takeover was used) |
| 22 | # |
| 23 | # Safety rules (all on by default): |
| 24 | # - never takes the card if it carries your default route, or if you are on ssh and have no other network; |
| 25 | # - takeover arms a rollback timer first; unless you confirm in time, the stock driver is put back by itself; |
| 26 | # - everything downloaded is pinned to a commit and checked against a sha256 before it is used; |
| 27 | # - one download, no other network traffic, nothing is sent anywhere. |
| 28 | set -eu |
| 29 | |
| 30 | VERSION=0.1.0 |
| 31 | REPO=aien-dev/rtl8127-omega |
| 32 | PIN_COMMIT=45becc6a90086ce754a256245cff64d6e62a33a7 |
| 33 | PIN_SHA256=81bd506adcb2ef85d2a42f6eb66b05e2f1c372a34ed9bc8b77ee0fe401493267 |
| 34 | ASSET=dist/rtl8127-probe-linux-aarch64 |
| 35 | BASE_URL=${RTL_BASE_URL:-https://raw.githubusercontent.com/$REPO/$PIN_COMMIT} |
| 36 | SYSFS=${RTL_SYSFS:-/sys} |
| 37 | PROCFS=${RTL_PROC:-/proc} |
| 38 | PREFIX=${RTL_PREFIX:-$HOME/.local} |
| 39 | STATE=${RTL_STATE:-$HOME/.local/share/rtl8127-omega} |
| 40 | ARCH=${RTL_ARCH:-$(uname -m)} |
| 41 | ROLLBACK_S=${RTL_ROLLBACK_S:-120} |
| 42 | # A fake /sys means a test. Test-only switches are honoured ONLY then, never against the real /sys. |
| 43 | TEST=0 |
| 44 | [ "$SYSFS" = /sys ] || TEST=1 |
| 45 | |
| 46 | say() { printf '%s\n' "$*"; } |
| 47 | die() { printf 'rtl8127-omega: %s\n' "$*" >&2; exit "${2:-1}"; } |
| 48 | note() { printf 'rtl8127-omega: %s\n' "$*"; } |
| 49 | |
| 50 | sha256_of() { |
| 51 | if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 |
| 52 | elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1 |
| 53 | else die "no sha256sum or shasum on this machine, so I cannot check the download. Stopping." 4; fi |
| 54 | } |
| 55 | |
| 56 | # ---- looking around (read-only) ---- |
| 57 | find_cards() { # prints the PCI address of each RTL8127 |
| 58 | for d in "$SYSFS"/bus/pci/devices/*; do |
| 59 | [ -f "$d/vendor" ] && [ -f "$d/device" ] || continue |
| 60 | [ "$(cat "$d/vendor")" = 0x10ec ] && [ "$(cat "$d/device")" = 0x8127 ] && basename "$d" |
| 61 | done |
| 62 | return 0 |
| 63 | } |
| 64 | card_driver() { [ -L "$SYSFS/bus/pci/devices/$1/driver" ] && basename "$(readlink "$SYSFS/bus/pci/devices/$1/driver")" || true; } |
| 65 | card_netdev() { for n in "$SYSFS/bus/pci/devices/$1"/net/*; do [ -e "$n" ] && basename "$n" && return 0; done; return 0; } |
| 66 | default_route_ifaces() { awk 'NR>1 && $2=="00000000" {print $1}' "$PROCFS/net/route" 2>/dev/null | sort -u; } |
| 67 | other_links_up() { # count of other network interfaces (not lo, not $1) that are up |
| 68 | n=0 |
| 69 | for i in "$SYSFS"/class/net/*; do |
| 70 | [ -e "$i" ] || continue |
| 71 | b=$(basename "$i") |
| 72 | [ "$b" = lo ] && continue |
| 73 | [ "$b" = "${1:-}" ] && continue |
| 74 | [ "$(cat "$i/operstate" 2>/dev/null || echo down)" = up ] && n=$((n + 1)) |
| 75 | done |
| 76 | echo "$n" |
| 77 | } |
| 78 | on_ssh() { [ -n "${SSH_CONNECTION:-}${SSH_TTY:-}" ]; } |
| 79 | |
| 80 | # Returns 0 and says why when taking this card would be risky. |
| 81 | risky() { # risky BDF |
| 82 | nd=$(card_netdev "$1") |
| 83 | if [ -n "$nd" ] && default_route_ifaces | grep -qx "$nd"; then |
| 84 | say "it carries your default route ($nd): taking it would cut your internet"; return 0 |
| 85 | fi |
| 86 | if on_ssh && [ "$(other_links_up "$nd")" = 0 ]; then |
| 87 | say "you are connected over ssh and I see no other network that is up: you could lock yourself out"; return 0 |
| 88 | fi |
| 89 | return 1 |
| 90 | } |
| 91 | |
| 92 | cmd_check() { |
| 93 | say "rtl8127-omega $VERSION (experimental, never run on a real chip)" |
| 94 | say "cpu: $ARCH" |
| 95 | cards=$(find_cards) |
| 96 | if [ -z "$cards" ]; then say "found: no RTL8127 network card (PCI 10ec:8127) on this machine"; return 3; fi |
| 97 | for c in $cards; do |
| 98 | nd=$(card_netdev "$c"); dr=$(card_driver "$c") |
| 99 | say "found: RTL8127 at $c (interface ${nd:-none}, driver ${dr:-none})" |
| 100 | if r=$(risky "$c"); then say "takeover: would be REFUSED, $r"; else say "takeover: the safety checks pass (but takeover is not available in this release)"; fi |
| 101 | done |
| 102 | return 0 |
| 103 | } |
| 104 | |
| 105 | # ---- the one download ---- |
| 106 | fetch_probe() { # fetch_probe DEST |
| 107 | [ "$ARCH" = aarch64 ] || [ "$ARCH" = arm64 ] || die "this release only has a 64-bit Arm (aarch64) build, and this machine is $ARCH. Nothing was changed." 5 |
| 108 | command -v curl >/dev/null 2>&1 || die "curl is not installed. Nothing was changed." 4 |
| 109 | case "$BASE_URL" in |
| 110 | https://*) proto="--proto =https --tlsv1.2" ;; |
| 111 | file://*) [ "$TEST" = 1 ] || die "a file:// source is only allowed in tests" 4; proto="" ;; |
| 112 | *) die "the download address must be https" 4 ;; |
| 113 | esac |
| 114 | tmp="$1.part" |
| 115 | # shellcheck disable=SC2086 |
| 116 | curl -fsSL $proto -o "$tmp" "$BASE_URL/$ASSET" || { rm -f "$tmp"; die "could not download $ASSET. Nothing was changed." 6; } |
| 117 | got=$(sha256_of "$tmp") |
| 118 | if [ "$got" != "$PIN_SHA256" ]; then |
| 119 | rm -f "$tmp" |
| 120 | die "the download does not match its pinned checksum (wanted $PIN_SHA256, got $got). I deleted it. Nothing was changed." 7 |
| 121 | fi |
| 122 | chmod 755 "$tmp"; mv "$tmp" "$1" |
| 123 | } |
| 124 | |
| 125 | run_probe() { # run_probe BDF (read-only) |
| 126 | res="$SYSFS/bus/pci/devices/$1/resource2" |
| 127 | [ -e "$res" ] || { note "no $res to read; skipping the read-only look"; return 0; } |
| 128 | if [ "$(id -u)" = 0 ]; then "$PREFIX/bin/rtl8127-probe" "$res" |
| 129 | elif command -v sudo >/dev/null 2>&1; then say "Next: one sudo step to read the chip (read-only)."; sudo "$PREFIX/bin/rtl8127-probe" "$res" |
| 130 | else note "no sudo here; run this as root to read the chip: $PREFIX/bin/rtl8127-probe $res"; fi |
| 131 | } |
| 132 | |
| 133 | cmd_install() { |
| 134 | dry=${1:-} |
| 135 | cards=$(find_cards) |
| 136 | if [ -z "$cards" ]; then say "No RTL8127 network card (PCI 10ec:8127) found on this machine. Nothing to do, nothing changed."; return 3; fi |
| 137 | for c in $cards; do say "Found an RTL8127 at $c (interface $(card_netdev "$c"), driver $(card_driver "$c"))."; done |
| 138 | say "This will: download rtl8127-probe (pinned to commit $PIN_COMMIT, sha256 checked), put it in $PREFIX/bin, and run it once, read-only." |
| 139 | say "It will NOT change your network or your network driver." |
| 140 | [ "$dry" = --dry-run ] && { say "(dry run: stopping here, nothing changed)"; return 0; } |
| 141 | mkdir -p "$PREFIX/bin" "$STATE" |
| 142 | fetch_probe "$PREFIX/bin/rtl8127-probe" |
| 143 | say "Downloaded and checked: sha256 $PIN_SHA256" |
| 144 | for c in $cards; do run_probe "$c"; done |
| 145 | say "" |
| 146 | say "Done. Your network was not touched." |
| 147 | say "This release can READ the chip. It cannot run your network yet. Why, and what is missing: docs/GAPS.md in the repository." |
| 148 | say "To remove everything: curl -fsSL https://raw.githubusercontent.com/$REPO/main/install.sh | sh -s -- uninstall" |
| 149 | } |
| 150 | |
| 151 | # ---- takeover: built, tested, and switched off in this release ---- |
| 152 | write_restore() { # write_restore BDF DRIVER : a self-contained script that gives the card back |
| 153 | cat > "$STATE/restore.sh" <<EOS |
| 154 | #!/bin/sh |
| 155 | # puts the stock driver ($2) back on $1 |
| 156 | d="$SYSFS/bus/pci/devices/$1" |
| 157 | [ -e "\$d/driver/unbind" ] && echo "$1" > "\$d/driver/unbind" 2>/dev/null |
| 158 | echo > "\$d/driver_override" 2>/dev/null |
| 159 | echo "$1" > "$SYSFS/bus/pci/drivers/$2/bind" 2>/dev/null || echo "$1" > "$SYSFS/bus/pci/drivers_probe" 2>/dev/null |
| 160 | echo restored > "$STATE/restored" |
| 161 | EOS |
| 162 | chmod 755 "$STATE/restore.sh" |
| 163 | } |
| 164 | |
| 165 | caps_ok() { # the driver says it can carry traffic |
| 166 | [ -f "$STATE/CAPS" ] && grep -qx 'dma=yes' "$STATE/CAPS" && return 0 |
| 167 | [ "$TEST" = 1 ] && [ -n "${RTL_TEST_CAPS:-}" ] && return 0 |
| 168 | return 1 |
| 169 | } |
| 170 | |
| 171 | cmd_takeover() { |
| 172 | cards=$(find_cards); c=$(printf '%s\n' "$cards" | head -n 1) |
| 173 | [ -n "$c" ] || die "no RTL8127 found. Nothing changed." 3 |
| 174 | if ! caps_ok; then |
| 175 | die "takeover is not available in this release: the Omega driver cannot carry network traffic yet (see docs/GAPS.md). Nothing was changed." 8 |
| 176 | fi |
| 177 | if r=$(risky "$c"); then die "REFUSED: $r. Nothing was changed." 9; fi |
| 178 | dr=$(card_driver "$c"); [ -n "$dr" ] || die "the card has no stock driver bound; nothing to hand over. Nothing changed." 3 |
| 179 | mkdir -p "$STATE"; rm -f "$STATE/confirm" "$STATE/restored" |
| 180 | write_restore "$c" "$dr" |
| 181 | # 1. arm the rollback FIRST. It puts the stock driver back unless you confirm in time. |
| 182 | ( sleep "$ROLLBACK_S"; [ -e "$STATE/confirm" ] || sh "$STATE/restore.sh" ) >/dev/null 2>&1 & |
| 183 | echo $! > "$STATE/watchdog.pid" |
| 184 | say "Rollback armed: in $ROLLBACK_S seconds the stock driver comes back by itself unless you run: touch $STATE/confirm" |
| 185 | # 2. only now hand the card over. |
| 186 | echo "$c" > "$SYSFS/bus/pci/devices/$c/driver/unbind" |
| 187 | echo "vfio-pci" > "$SYSFS/bus/pci/devices/$c/driver_override" |
| 188 | echo "$c" > "$SYSFS/bus/pci/drivers_probe" |
| 189 | echo "$c $dr" > "$STATE/taken" |
| 190 | say "Card $c handed over." |
| 191 | } |
| 192 | |
| 193 | cmd_restore() { |
| 194 | [ -f "$STATE/restore.sh" ] || { say "nothing to restore."; return 0; } |
| 195 | sh "$STATE/restore.sh"; rm -f "$STATE/taken" |
| 196 | say "The stock driver is back." |
| 197 | } |
| 198 | |
| 199 | cmd_uninstall() { |
| 200 | [ -f "$STATE/taken" ] && cmd_restore |
| 201 | [ -f "$STATE/watchdog.pid" ] && kill "$(cat "$STATE/watchdog.pid")" 2>/dev/null || true |
| 202 | rm -f "$PREFIX/bin/rtl8127-probe" |
| 203 | rm -rf "$STATE" |
| 204 | say "Removed rtl8127-probe and its state folder. Your network driver was not changed." |
| 205 | } |
| 206 | |
| 207 | cmd=${1:-install} |
| 208 | case "$cmd" in |
| 209 | check) cmd_check ;; |
| 210 | install) cmd_install "${2:-}" ;; |
| 211 | --dry-run|dry-run) cmd_install --dry-run ;; |
| 212 | takeover) cmd_takeover ;; |
| 213 | restore) cmd_restore ;; |
| 214 | uninstall) cmd_uninstall ;; |
| 215 | help|-h|--help) sed -n '2,32p' "$0" 2>/dev/null || say "see $REPO" ;; |
| 216 | *) die "unknown command '$cmd' (try: check, install, uninstall)" 2 ;; |
| 217 | esac |