xref: /freebsd/libexec/rc/rc.d/devmatch (revision 17aec740b0b2112eb1803ca6d5ec8c8161f8dcae)
10696600cSBjoern A. Zeeb#!/bin/sh
20696600cSBjoern A. Zeeb
3f86e6000SWarner Losh# Copyright (c) 2018 M. Warner Losh <imp@FreeBSD.org>
40696600cSBjoern A. Zeeb#
50696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
60696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
70696600cSBjoern A. Zeeb# are met:
80696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
90696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
100696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
110696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
120696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
130696600cSBjoern A. Zeeb#
140696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
150696600cSBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
160696600cSBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
170696600cSBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
180696600cSBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
190696600cSBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
200696600cSBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
210696600cSBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
220696600cSBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
230696600cSBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
240696600cSBjoern A. Zeeb# SUCH DAMAGE.
250696600cSBjoern A. Zeeb#
260696600cSBjoern A. Zeeb#
270696600cSBjoern A. Zeeb# PROVIDE: devmatch
28f68e3ea8SHelge Oldach# REQUIRE: kld
293cc62032SColin Percival# BEFORE: netif
300696600cSBjoern A. Zeeb# KEYWORD: nojail
310696600cSBjoern A. Zeeb
320696600cSBjoern A. Zeeb. /etc/rc.subr
330696600cSBjoern A. Zeeb
340696600cSBjoern A. Zeebname="devmatch"
350696600cSBjoern A. Zeebdesc="Use devmatch(8) to load kernel modules"
360696600cSBjoern A. Zeebrcvar="${name}_enable"
370696600cSBjoern A. Zeeb
380696600cSBjoern A. Zeebstart_cmd="${name}_start"
390696600cSBjoern A. Zeebstop_cmd=':'
400696600cSBjoern A. Zeebone_nomatch="$2"
410696600cSBjoern A. Zeeb
420696600cSBjoern A. Zeebdevmatch_start()
430696600cSBjoern A. Zeeb{
44*17aec740SKyle Evans	local x m list boot_safe
45*17aec740SKyle Evans
46*17aec740SKyle Evans	boot_safe=$(kenv -q boot_safe || echo "NO")
47*17aec740SKyle Evans	checkyesno boot_safe && return
480696600cSBjoern A. Zeeb
490696600cSBjoern A. Zeeb	if [ -n "$one_nomatch" ]; then
500696600cSBjoern A. Zeeb		list=$(devmatch -p "${one_nomatch}" | sort -u)
510696600cSBjoern A. Zeeb	else
5262775aebSJessica Clarke		sysctl hw.bus.devctl_nomatch_enabled=1 > /dev/null
530696600cSBjoern A. Zeeb		list=$(devmatch | sort -u)
540696600cSBjoern A. Zeeb	fi
550696600cSBjoern A. Zeeb
560696600cSBjoern A. Zeeb	[ -n "$list" ] || return
570696600cSBjoern A. Zeeb
58b29ebb9cSWarner Losh	# While kldload can accept multiple modules on the line at once, we loop
59b29ebb9cSWarner Losh	# here in case there's some weird error with one of them.  We also
60b29ebb9cSWarner Losh	# optimize against the false positives or drivers that have symbolic
61b29ebb9cSWarner Losh	# links that confuse devmatch by running it -n.  Finally, we filter out
62b29ebb9cSWarner Losh	# all items in the devmatch_blocklist.
63b29ebb9cSWarner Losh	#
64b29ebb9cSWarner Losh	# We strip all the .ko suffixes off so that one may specify modules
65b29ebb9cSWarner Losh	# with or without .ko. Prior version documented it was without, while
66b29ebb9cSWarner Losh	# the code required it, so accept both now. devmatch produces module
67b29ebb9cSWarner Losh	# names with .ko
68b29ebb9cSWarner Losh
690696600cSBjoern A. Zeeb	devctl freeze
70a8935083SWarner Losh	x=$(echo "#${devmatch_blocklist:-${devmatch_blacklist}}#$(kenv -q devmatch_blocklist)#" | \
71b29ebb9cSWarner Losh		sed -e "s/ /#/g;s/\.ko#/#/g")
720696600cSBjoern A. Zeeb	for m in ${list}; do
73b29ebb9cSWarner Losh		m="${m%.ko}"
74b29ebb9cSWarner Losh		case "${x}" in
750696600cSBjoern A. Zeeb		*"#${m}#"*) continue ;;
760696600cSBjoern A. Zeeb		esac
775549c6a6SWarner Losh		kldstat -q -n ${m} || \
785549c6a6SWarner Losh		    (echo "Autoloading module: ${m}"; kldload -n ${m})
790696600cSBjoern A. Zeeb	done
800696600cSBjoern A. Zeeb	devctl thaw
810696600cSBjoern A. Zeeb}
820696600cSBjoern A. Zeeb
830696600cSBjoern A. Zeebload_rc_config $name
84f99f0ee1SAlexander Leidinger
85f99f0ee1SAlexander Leidinger# doesn't make sense to run in a svcj: privileged operations
86f99f0ee1SAlexander Leidingerdevmatch_svcj="NO"
87f99f0ee1SAlexander Leidinger
880696600cSBjoern A. Zeebrun_rc_command "$1"
89