xref: /netbsd-src/share/examples/devpubd/hooks/99-ugen-perms-tigard (revision 4e13543e2b57c2673c3a2f9cf3e103f2f9e69f67)
1*4e13543eSthorpej#!/bin/sh -
2*4e13543eSthorpej#
3*4e13543eSthorpej# $NetBSD: 99-ugen-perms-tigard,v 1.1 2024/03/30 06:42:10 thorpej Exp $
4*4e13543eSthorpej#
5*4e13543eSthorpej# Look for a Tigard (https://github.com/tigard-tools/tigard) debug
6*4e13543eSthorpej# board and change the permissions to 0660.
7*4e13543eSthorpej#
8*4e13543eSthorpej# Written by Jason R. Thorpe, March 2024.  Public domain.
9*4e13543eSthorpej#
10*4e13543eSthorpej
11*4e13543eSthorpejexport LC_ALL=C
12*4e13543eSthorpej
13*4e13543eSthorpejevent="$1"
14*4e13543eSthorpejshift
15*4e13543eSthorpejdevices=$@
16*4e13543eSthorpej
17*4e13543eSthorpejorig_perms=0600
18*4e13543eSthorpejnew_perms=0660
19*4e13543eSthorpej
20*4e13543eSthorpejorig_group=wheel
21*4e13543eSthorpejnew_group=wheel
22*4e13543eSthorpej
23*4e13543eSthorpejdevice_name=tigard
24*4e13543eSthorpej
25*4e13543eSthorpejis_target_device()
26*4e13543eSthorpej{
27*4e13543eSthorpej	local vendor_string
28*4e13543eSthorpej	local product_string
29*4e13543eSthorpej
30*4e13543eSthorpej	vendor_string="$(drvctl -p $1 vendor-string)"
31*4e13543eSthorpej	product_string="$(drvctl -p $1 product-string)"
32*4e13543eSthorpej
33*4e13543eSthorpej	if [ x"$vendor_string" = x"SecuringHardware.com" -a \
34*4e13543eSthorpej	     x"$product_string" = x"Tigard V1.1" ]; then
35*4e13543eSthorpej		echo "yes"
36*4e13543eSthorpej		return
37*4e13543eSthorpej	fi
38*4e13543eSthorpej
39*4e13543eSthorpej	echo "no"
40*4e13543eSthorpej}
41*4e13543eSthorpej
42*4e13543eSthorpejset_permissions()
43*4e13543eSthorpej{
44*4e13543eSthorpej	if [ x$(is_target_device $1) = xyes ]; then
45*4e13543eSthorpej		chgrp $new_group /dev/"${2}".*
46*4e13543eSthorpej		chmod $new_perms /dev/"${2}".*
47*4e13543eSthorpej		#
48*4e13543eSthorpej		# We need to create a symlink here to remember
49*4e13543eSthorpej		# the ugen device node that was used, since we
50*4e13543eSthorpej		# can't recover it from the device name that
51*4e13543eSthorpej		# comes from the kernel later because we get the
52*4e13543eSthorpej		# event *after* the device is gone, and thus
53*4e13543eSthorpej		# cannot query any properties.
54*4e13543eSthorpej		#
55*4e13543eSthorpej		rm -f /dev/${1}-${device_name}
56*4e13543eSthorpej		ln -sf ${2} /dev/${1}-${device_name}
57*4e13543eSthorpej	fi
58*4e13543eSthorpej}
59*4e13543eSthorpej
60*4e13543eSthorpejrestore_permissions()
61*4e13543eSthorpej{
62*4e13543eSthorpej	if [ -h "/dev/${1}-${device_name}" ]; then
63*4e13543eSthorpej		devnode=$(readlink "/dev/${1}-${device_name}")
64*4e13543eSthorpej		if [ x"$devnode" != x ]; then
65*4e13543eSthorpej			chmod $orig_perms /dev/"${devnode}".*
66*4e13543eSthorpej			chgrp $orig_group /dev/"${devnode}".*
67*4e13543eSthorpej		fi
68*4e13543eSthorpej		rm -f "/dev/${1}-${device_name}"
69*4e13543eSthorpej	fi
70*4e13543eSthorpej}
71*4e13543eSthorpej
72*4e13543eSthorpejget_ugen_devnode()
73*4e13543eSthorpej{
74*4e13543eSthorpej	# Because "ugen" and "ugenif" share the same /dev/ugenN.*
75*4e13543eSthorpej	# namespace, we have to query an additional property to
76*4e13543eSthorpej	# determine which one it is.
77*4e13543eSthorpej	local ugen_unit
78*4e13543eSthorpej
79*4e13543eSthorpej	ugen_unit=$(drvctl -p $1 ugen-unit)
80*4e13543eSthorpej	case "$ugen_unit" in
81*4e13543eSthorpej	[0-9]*)
82*4e13543eSthorpej		echo "ugen$ugen_unit"
83*4e13543eSthorpej		;;
84*4e13543eSthorpej	esac
85*4e13543eSthorpej}
86*4e13543eSthorpej
87*4e13543eSthorpejfor device in $devices; do
88*4e13543eSthorpej	case $device in
89*4e13543eSthorpej	ugensa*)
90*4e13543eSthorpej		# Ignore ugensa(4).
91*4e13543eSthorpej		;;
92*4e13543eSthorpej	ugen*)
93*4e13543eSthorpej		case $event in
94*4e13543eSthorpej		device-attach)
95*4e13543eSthorpej			devnode=$(get_ugen_devnode $1)
96*4e13543eSthorpej			if [ x"$devnode" != x ]; then
97*4e13543eSthorpej				set_permissions $device $devnode
98*4e13543eSthorpej			fi
99*4e13543eSthorpej			;;
100*4e13543eSthorpej		device-detach)
101*4e13543eSthorpej			restore_permissions $device
102*4e13543eSthorpej			;;
103*4e13543eSthorpej		esac
104*4e13543eSthorpej	esac
105*4e13543eSthorpejdone
106