xref: /freebsd/usr.sbin/fwget/fwget.sh (revision fb98fc4755def2cb8ca145751b0e54485d5e2f4a)
1d198b877SEmmanuel Vadot#!/bin/sh
2d198b877SEmmanuel Vadot
3d198b877SEmmanuel Vadot#-
4611503c6SEmmanuel Vadot# SPDX-License-Identifier: BSD-2-Clause
5d198b877SEmmanuel Vadot#
6d198b877SEmmanuel Vadot# Copyright 2023 Beckhoff Automation GmbH & Co. KG
710aa369aSBjoern A. Zeeb# Copyright 2023 Bjoern A. Zeeb
8d198b877SEmmanuel Vadot#
9d198b877SEmmanuel Vadot# Redistribution and use in source and binary forms, with or without
10d198b877SEmmanuel Vadot# modification, are permitted providing that the following conditions
11d198b877SEmmanuel Vadot# are met:
12d198b877SEmmanuel Vadot# 1. Redistributions of source code must retain the above copyright
13d198b877SEmmanuel Vadot#    notice, this list of conditions and the following disclaimer.
14d198b877SEmmanuel Vadot# 2. Redistributions in binary form must reproduce the above copyright
15d198b877SEmmanuel Vadot#    notice, this list of conditions and the following disclaimer in the
16d198b877SEmmanuel Vadot#    documentation and/or other materials provided with the distribution.
17d198b877SEmmanuel Vadot#
18d198b877SEmmanuel Vadot# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19d198b877SEmmanuel Vadot# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20d198b877SEmmanuel Vadot# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21d198b877SEmmanuel Vadot# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22d198b877SEmmanuel Vadot# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23d198b877SEmmanuel Vadot# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24d198b877SEmmanuel Vadot# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25d198b877SEmmanuel Vadot# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26d198b877SEmmanuel Vadot# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27d198b877SEmmanuel Vadot# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28d198b877SEmmanuel Vadot# POSSIBILITY OF SUCH DAMAGE.
29d198b877SEmmanuel Vadot
30c81495a6SEmmanuel Vadot: ${LIBEXEC_PATH:="/usr/libexec/fwget"}
31d198b877SEmmanuel Vadot
32d198b877SEmmanuel Vadotusage()
33d198b877SEmmanuel Vadot{
34d198b877SEmmanuel Vadot	cat <<EOF
357ad4d94dSBenedict ReuschlingUsage: $(basename "$0") [options] [subsystem]
36d198b877SEmmanuel Vadot
37d198b877SEmmanuel VadotSupported subsystems
38d198b877SEmmanuel Vadot  pci
39d198b877SEmmanuel Vadot
40d198b877SEmmanuel VadotOptions:
411eb3f15cSBjoern A. Zeeb  -n		-- Do not install packages, only print the results
421eb3f15cSBjoern A. Zeeb  -q		-- Quiet mode.  If used with -n only prints a package a line
43d198b877SEmmanuel Vadot  -v		-- More verbose
44d198b877SEmmanuel VadotEOF
45d198b877SEmmanuel Vadot	exit 1
46d198b877SEmmanuel Vadot}
47d198b877SEmmanuel Vadot
48d198b877SEmmanuel Vadotlog()
49d198b877SEmmanuel Vadot{
5019a6bc9fSRenato Botelho	if [ "${QUIET}" != "y" ]; then
517ad4d94dSBenedict Reuschling		echo "$@"
5219a6bc9fSRenato Botelho	fi
53d198b877SEmmanuel Vadot}
54d198b877SEmmanuel Vadot
55d198b877SEmmanuel Vadotlog_verbose()
56d198b877SEmmanuel Vadot{
57d198b877SEmmanuel Vadot	if [ "${VERBOSE}" = "n" ]; then
58d198b877SEmmanuel Vadot		return
59d198b877SEmmanuel Vadot	fi
60d198b877SEmmanuel Vadot
617ad4d94dSBenedict Reuschling	echo "$@"
62d198b877SEmmanuel Vadot}
63d198b877SEmmanuel Vadot
6410aa369aSBjoern A. Zeebaddpkg()
6510aa369aSBjoern A. Zeeb{
6610aa369aSBjoern A. Zeeb	local _p
6710aa369aSBjoern A. Zeeb
6810aa369aSBjoern A. Zeeb	_p=$1
6910aa369aSBjoern A. Zeeb
7010aa369aSBjoern A. Zeeb	case "${packages}" in
7110aa369aSBjoern A. Zeeb	"")	packages="${_p}" ;;
7210aa369aSBjoern A. Zeeb	*)	# Avoid duplicates.
7310aa369aSBjoern A. Zeeb		case " ${packages} " in
7410aa369aSBjoern A. Zeeb		*\ ${_p}\ *) ;;	# duplicate
7510aa369aSBjoern A. Zeeb		*)	packages="${packages} ${_p}" ;;
7610aa369aSBjoern A. Zeeb		esac
7710aa369aSBjoern A. Zeeb	esac
7810aa369aSBjoern A. Zeeb}
7910aa369aSBjoern A. Zeeb
80d198b877SEmmanuel VadotDRY_RUN=n
811eb3f15cSBjoern A. ZeebQUIET=n
82d198b877SEmmanuel VadotVERBOSE=n
83d198b877SEmmanuel Vadot
841eb3f15cSBjoern A. Zeebwhile getopts ":nqv" _arg; do
851eb3f15cSBjoern A. Zeeb	case ${_arg} in
861eb3f15cSBjoern A. Zeeb	n)
87d198b877SEmmanuel Vadot		DRY_RUN=y
88d198b877SEmmanuel Vadot		;;
891eb3f15cSBjoern A. Zeeb	q)
901eb3f15cSBjoern A. Zeeb		QUIET=y
911eb3f15cSBjoern A. Zeeb		;;
921eb3f15cSBjoern A. Zeeb	v)
93d198b877SEmmanuel Vadot		VERBOSE=y
94d198b877SEmmanuel Vadot		;;
951eb3f15cSBjoern A. Zeeb	?)
961eb3f15cSBjoern A. Zeeb		usage
97d198b877SEmmanuel Vadot		;;
98d198b877SEmmanuel Vadot	esac
99d198b877SEmmanuel Vadotdone
1001eb3f15cSBjoern A. Zeebshift $(($OPTIND - 1))
1011eb3f15cSBjoern A. Zeebsubsystems="$@"
102d198b877SEmmanuel Vadot
103d198b877SEmmanuel Vadot# Default searching PCI subsystem
104d198b877SEmmanuel Vadotif [ -z "${subsystems}" ]; then
105d198b877SEmmanuel Vadot	subsystems="pci"
106d198b877SEmmanuel Vadotfi
107d198b877SEmmanuel Vadot
108d198b877SEmmanuel Vadot# Fail early on unsupported subsystem
109d198b877SEmmanuel Vadotfor subsystem in ${subsystems}; do
1107ad4d94dSBenedict Reuschling	if [ ! -f "${LIBEXEC_PATH}"/"${subsystem}" ]; then
111d198b877SEmmanuel Vadot		usage
112d198b877SEmmanuel Vadot	fi
1137ad4d94dSBenedict Reuschling	. "${LIBEXEC_PATH}"/"${subsystem}"
114d198b877SEmmanuel Vadotdone
115d198b877SEmmanuel Vadot
116d198b877SEmmanuel Vadotpackages=""
117d198b877SEmmanuel Vadotfor subsystem in ${subsystems}; do
1187ad4d94dSBenedict Reuschling	"${subsystem}"_search_packages
119d198b877SEmmanuel Vadotdone
120d198b877SEmmanuel Vadot
12110aa369aSBjoern A. Zeebcase "${packages}" in
12210aa369aSBjoern A. Zeeb""|^[[:space:]]*$)
123*fb98fc47SRenato Botelho	log "No firmware packages to install."
12410aa369aSBjoern A. Zeeb	exit 0
12510aa369aSBjoern A. Zeeb	;;
12610aa369aSBjoern A. Zeebesac
12710aa369aSBjoern A. Zeeb
128*fb98fc47SRenato Botelholog "Needed firmware packages: '${packages}'"
129d198b877SEmmanuel Vadotif [ "${DRY_RUN}" = "y" ]; then
1301eb3f15cSBjoern A. Zeeb	if [ "${QUIET}" = "y" ]; then
1311eb3f15cSBjoern A. Zeeb		for pkg in ${packages}; do
1321eb3f15cSBjoern A. Zeeb			case "${pkg}" in
1331eb3f15cSBjoern A. Zeeb			""|^[[:space:]]*$) continue ;;
1341eb3f15cSBjoern A. Zeeb			esac
1351eb3f15cSBjoern A. Zeeb			echo "${pkg}"
1361eb3f15cSBjoern A. Zeeb		done
1371eb3f15cSBjoern A. Zeeb	fi
138d198b877SEmmanuel Vadot	exit 0
139d198b877SEmmanuel Vadotfi
140d198b877SEmmanuel Vadot
141214e3e09SBjoern A. Zeebpkg install -qy ${packages}
142