5 source_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
6 default_binary="$source_root/build-candidate21-linux/src/friidump"
7 binary="${2:-$default_binary}"
8 expected_version="0.5.3.16-pf1-candidate21"
11 printf 'ERROR: %s\n' "$*" >&2
19 command -v "$name" >/dev/null 2>&1 || {
20 printf 'Missing required command: %s\n' "$name" >&2
21 printf 'Ubuntu package hint: sudo apt-get install -y %s\n' "$package" >&2
27 local resolved help_output help_status
29 [[ -f "$binary" ]] || fail "FriiDump executable does not exist: $binary"
30 [[ -x "$binary" ]] || fail "FriiDump executable is not executable: $binary"
31 [[ ! -L "$binary" ]] || fail "Refusing a symbolic-link executable: $binary"
33 resolved="$(readlink -f -- "$binary")"
34 [[ -n "$resolved" && -f "$resolved" ]] || fail "Unable to resolve executable: $binary"
37 help_output="$("$resolved" --help 2>&1)"
41 [[ "$help_status" -eq 1 ]] || fail "Unexpected --help exit status from $resolved: $help_status"
42 grep -Fq "FriiDump $expected_version" <<<"$help_output" ||
43 fail "Executable version mismatch: $resolved"
45 printf '%s\n' "$resolved"
49 getcap -n -- "$1" 2>/dev/null || true
52 capability_present() {
54 text="$(capability_text "$1")"
55 [[ "$text" =~ cap_sys_rawio(\+|=)(ep|eip|p|ip) ]] ||
56 [[ "$text" == *"cap_sys_rawio=ep"* ]]
61 printf 'Executable: %s\n' "$resolved"
62 printf 'SHA-256: %s\n' "$(sha256sum "$resolved" | awk '{print $1}')"
64 stat -c '%A (%a) owner=%U:%G' "$resolved"
65 printf 'Capability: %s\n' "$(capability_text "$resolved")"
68 install_capability() {
70 local before_hash after_hash
72 before_hash="$(sha256sum "$resolved" | awk '{print $1}')"
74 printf '\nInstalling the minimum Linux authority required for vendor SCSI commands.\n'
75 printf 'Only the exact executable receives CAP_SYS_RAWIO; FriiDump remains a normal-user process.\n'
76 printf 'Do not run FriiDump itself with sudo.\n\n'
78 sudo setcap cap_sys_rawio=ep "$resolved"
80 after_hash="$(sha256sum "$resolved" | awk '{print $1}')"
81 [[ "$after_hash" == "$before_hash" ]] ||
82 fail "Executable bytes changed while applying the file capability."
84 capability_present "$resolved" || {
85 print_identity "$resolved"
86 fail "CAP_SYS_RAWIO verification failed."
89 print_identity "$resolved"
90 printf '\nFRIIDUMP LINUX RAW-I/O CAPABILITY INSTALL: PASS\n'
91 printf 'WARNING: rebuilding, replacing, copying, or re-extracting this executable clears the capability.\n'
97 if ! capability_present "$resolved"; then
98 print_identity "$resolved"
99 printf '\nFRIIDUMP LINUX RAW-I/O CAPABILITY REMOVE: NOT PRESENT\n'
103 sudo setcap -r "$resolved"
105 if capability_present "$resolved"; then
106 print_identity "$resolved"
107 fail "CAP_SYS_RAWIO remains present after removal."
110 print_identity "$resolved"
111 printf '\nFRIIDUMP LINUX RAW-I/O CAPABILITY REMOVE: PASS\n'
114 require_command getcap libcap2-bin
115 require_command sha256sum coreutils
116 require_command readlink coreutils
117 require_command stat coreutils
119 resolved_binary="$(resolve_binary)"
123 print_identity "$resolved_binary"
124 if capability_present "$resolved_binary"; then
125 printf '\nFRIIDUMP LINUX RAW-I/O CAPABILITY STATUS: PRESENT\n'
127 printf '\nFRIIDUMP LINUX RAW-I/O CAPABILITY STATUS: MISSING\n'
128 printf 'Vendor-command operations will be refused before seed retrieval or unlock.\n'
129 printf 'Install with:\n bash %q install %q\n' "$0" "$resolved_binary"
134 require_command setcap libcap2-bin
135 require_command sudo sudo
136 install_capability "$resolved_binary"
139 require_command setcap libcap2-bin
140 require_command sudo sudo
141 remove_capability "$resolved_binary"
144 printf 'Usage: %s {status|install|remove} [friidump-executable]\n' "$0" >&2