xref: /freebsd/usr.sbin/fwget/fwget.sh (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1#!/bin/sh
2
3#-
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright 2023 Beckhoff Automation GmbH & Co. KG
7# Copyright 2023 Bjoern A. Zeeb
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted providing that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29
30: ${LIBEXEC_PATH:="/usr/libexec/fwget"}
31
32usage()
33{
34	cat <<EOF
35Usage: $(basename "$0") [options] [subsystem]
36
37Supported subsystems
38  pci
39
40Options:
41  -n		-- Do not install packages, only print the results
42  -q		-- Quiet mode.  If used with -n only prints a package a line
43  -v		-- More verbose
44EOF
45	exit 1
46}
47
48log()
49{
50	if [ "${QUIET}" != "y" ]; then
51		echo "$@"
52	fi
53}
54
55log_verbose()
56{
57	if [ "${VERBOSE}" = "n" ]; then
58		return
59	fi
60
61	echo "$@"
62}
63
64addpkg()
65{
66	local _p
67
68	_p=$1
69
70	case "${packages}" in
71	"")	packages="${_p}" ;;
72	*)	# Avoid duplicates.
73		case " ${packages} " in
74		*\ ${_p}\ *) ;;	# duplicate
75		*)	packages="${packages} ${_p}" ;;
76		esac
77	esac
78}
79
80DRY_RUN=n
81QUIET=n
82VERBOSE=n
83
84while getopts ":nqv" _arg; do
85	case ${_arg} in
86	n)
87		DRY_RUN=y
88		;;
89	q)
90		QUIET=y
91		;;
92	v)
93		VERBOSE=y
94		;;
95	?)
96		usage
97		;;
98	esac
99done
100shift $(($OPTIND - 1))
101subsystems="$@"
102
103# Default searching PCI subsystem
104if [ -z "${subsystems}" ]; then
105	subsystems="pci"
106fi
107
108# Fail early on unsupported subsystem
109for subsystem in ${subsystems}; do
110	if [ ! -f "${LIBEXEC_PATH}"/"${subsystem}" ]; then
111		usage
112	fi
113	. "${LIBEXEC_PATH}"/"${subsystem}"
114done
115
116packages=""
117for subsystem in ${subsystems}; do
118	"${subsystem}"_search_packages
119done
120
121case "${packages}" in
122""|^[[:space:]]*$)
123	log "No firmware packages to install."
124	exit 0
125	;;
126esac
127
128log "Needed firmware packages: '${packages}'"
129if [ "${DRY_RUN}" = "y" ]; then
130	if [ "${QUIET}" = "y" ]; then
131		for pkg in ${packages}; do
132			case "${pkg}" in
133			""|^[[:space:]]*$) continue ;;
134			esac
135			echo "${pkg}"
136		done
137	fi
138	exit 0
139fi
140
141pkg install -qy ${packages}
142