xref: /freebsd/usr.sbin/fwget/fwget.sh (revision 1eb3f15c149b9a2e5b6f5e10aed454fc85945bbd)
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	echo "$@"
51}
52
53log_verbose()
54{
55	if [ "${VERBOSE}" = "n" ]; then
56		return
57	fi
58
59	echo "$@"
60}
61
62addpkg()
63{
64	local _p
65
66	_p=$1
67
68	case "${packages}" in
69	"")	packages="${_p}" ;;
70	*)	# Avoid duplicates.
71		case " ${packages} " in
72		*\ ${_p}\ *) ;;	# duplicate
73		*)	packages="${packages} ${_p}" ;;
74		esac
75	esac
76}
77
78DRY_RUN=n
79QUIET=n
80VERBOSE=n
81
82while getopts ":nqv" _arg; do
83	case ${_arg} in
84	n)
85		DRY_RUN=y
86		;;
87	q)
88		QUIET=y
89		;;
90	v)
91		VERBOSE=y
92		;;
93	?)
94		usage
95		;;
96	esac
97done
98shift $(($OPTIND - 1))
99subsystems="$@"
100
101# Default searching PCI subsystem
102if [ -z "${subsystems}" ]; then
103	subsystems="pci"
104fi
105
106# Fail early on unsupported subsystem
107for subsystem in ${subsystems}; do
108	if [ ! -f "${LIBEXEC_PATH}"/"${subsystem}" ]; then
109		usage
110	fi
111	. "${LIBEXEC_PATH}"/"${subsystem}"
112done
113
114packages=""
115for subsystem in ${subsystems}; do
116	"${subsystem}"_search_packages
117done
118
119case "${packages}" in
120""|^[[:space:]]*$)
121	if [ "${QUIET}" != "y" ]; then
122		echo "No firmware packages to install."
123	fi
124	exit 0
125	;;
126esac
127
128if [ "${QUIET}" != "y" ]; then
129	echo "Needed firmware packages: '${packages}'"
130fi
131if [ "${DRY_RUN}" = "y" ]; then
132	if [ "${QUIET}" = "y" ]; then
133		for pkg in ${packages}; do
134			case "${pkg}" in
135			""|^[[:space:]]*$) continue ;;
136			esac
137			echo "${pkg}"
138		done
139	fi
140	exit 0
141fi
142
143pkg install -qy ${packages}
144