1#!/bin/sh
2#
3# Copyright (c) 2010-2012 Haiku, Inc.
4# Distributed under the terms of the MIT License.
5#
6# Authors:
7#		Matt Madia, mattmadia@gmail.com
8#
9# Synopsis:
10#	Provide a mechanism for end-users to install various firmwares for wireless
11#	network cards in a manner that complies with their individual licenses.
12#
13# Supported chipsets:
14# 	Intel ipw2100
15#	Intel ipw2200/2225/2915
16#	Broadcom 43xx
17#	Marvell 88W8335
18
19
20MESSAGE="This script will install firmware for various wireless network cards.
21 The Broadcom 43xx and Marvell 88W8335 require an active network connection
22 to download additional files before installation. In the absence of internet
23 access, only Intel's ipw2100 and ipw2200 will be installed.
24
25 If you do not have internet access and need to install the other firmwares,
26 goto https://www.haiku-os.org/docs/userguide/en/workshop-wlan.html. This page
27 has instructions on which files to manually download and where to copy them
28 into this OS. It also has different script that can be run on another OS and
29 will prepare a zip archive for easy install. After that, re-run this script."
30VIEW='View licenses'
31ABORT='Abort installation'
32OK='I agree to the licenses. Install firmwares.'
33
34baseURL='https://raw.githubusercontent.com/haiku/firmware/master/wifi/'
35firmwareDir=`finddir B_SYSTEM_DATA_DIRECTORY`/firmware
36tempDir=`finddir B_SYSTEM_TEMP_DIRECTORY`/wifi-firmwares
37driversDir=`finddir B_SYSTEM_ADDONS_DIRECTORY`/kernel/drivers
38tempFirmwareDir=`finddir B_SYSTEM_TEMP_DIRECTORY`/package_me"$firmwareDir"
39intelLicense='/boot/system/data/licenses/Intel (2xxx firmware)'
40
41
42function DisplayAlert()
43{
44	local result=`alert --stop "$MESSAGE" "$VIEW" "$ABORT" "$OK"`
45	case "${result}" in
46		"$VIEW")
47			ViewLicenses ;
48			DisplayAlert ;
49			;;
50		"$ABORT")
51			exit 0 ;;
52		"$OK")
53			InstallAllFirmwares ;
54			exit 0 ;
55			;;
56	esac
57}
58
59
60function ViewLicenses()
61{
62	license="$tempDir/Wifi_Firmware_Licenses"
63	cat << EOF > $license
64
65+-----------------------------------------------------------------------------+
66|                                                                             |
67|   Copyright and licensing information of the various wireless firmwares     |
68|                                                                             |
69| Firmware for broadcom43xx is under the Copyright of Broadcom Corporation(R) |
70| Firmware for marvell88w8335 is under the Copyright of Marvell Technology(R) |
71| ipw2100,iprowifi2200 firmware is covered by the following Intel(R) license: |
72|                                                                             |
73+-----------------------------------------------------------------------------+
74
75EOF
76	cat "$intelLicense" >> $license
77
78	open $license
79}
80
81
82function InstallAllFirmwares()
83{
84	InstallIpw2100
85	InstallIprowifi2200
86	InstallBroadcom43xx
87	InstallMarvell88w8335
88	MakeHPKG
89}
90
91
92function DownloadFileIfNotCached()
93{
94	# DownloadFileIfNotCached <url> <filename> <destination dir>
95	local url=$1
96	local file=$2
97	local dir=$3
98
99	mkdir -p "$dir"
100	if [ ! -e $dir/$file ] ; then
101		echo "Downloading $url ..."
102		wget -nv -O $dir/$file $url
103	fi
104	result=$?
105	if [ $result -gt 0 ]; then
106		local error="Failed to download $url."
107		local msg="As a result, ${driver}'s firmware will not be installed."
108		alert --warning "$error $msg"
109	fi
110}
111
112
113function SetFirmwarePermissions()
114{
115	cd ${tempFirmwareDir}/${driver}/
116	for file in * ; do
117		if [ "$file" != "$driver" ] && [ -f "$file" ] ; then
118			chmod a=r $file
119		fi
120	done
121}
122
123
124function CleanTemporaryFiles()
125{
126	rm -rf "$tempDir"
127	mkdir -p "$tempDir"
128}
129
130
131function PreFirmwareInstallation()
132{
133	echo "Acquiring firmware for ${driver} ..."
134	mkdir -p "${tempFirmwareDir}/${driver}"
135}
136
137
138function PostFirmwareInstallation()
139{
140	SetFirmwarePermissions
141	CleanTemporaryFiles
142	echo "... firmware for ${driver} will be installed."
143}
144
145
146function InstallIpw2100()
147{
148	driver='iprowifi2100'
149	PreFirmwareInstallation
150
151	# Prepare firmware archive for extraction.
152	local file='ipw2100-fw-1.3.tgz'
153	local url="${baseURL}/intel/${file}"
154	local dir="${tempFirmwareDir}/${driver}"
155	cp "${firmwareDir}/${driver}/${file}" "${dir}"
156	DownloadFileIfNotCached $url $file $dir
157
158	# Extract the firmware & license file in place.
159	cd "${tempFirmwareDir}/${driver}"
160	gunzip < "$file" | tar xf -
161
162	rm "${tempFirmwareDir}/${driver}/${file}"
163	PostFirmwareInstallation
164}
165
166
167function InstallIprowifi2200()
168{
169	driver='iprowifi2200'
170	PreFirmwareInstallation
171
172	# Prepare firmware archive for extraction.
173	local file='ipw2200-fw-3.1.tgz'
174	local url="${baseURL}/intel/${file}"
175	local dir="${tempFirmwareDir}/${driver}"
176	cp "${firmwareDir}/${driver}/${file}" "${dir}"
177	DownloadFileIfNotCached $url $file $dir
178
179	# Extract the firmware & license file.
180	cd "$tempDir"
181	gunzip < "${tempFirmwareDir}/${driver}/$file" | tar xf -
182	cd "${tempDir}/ipw2200-fw-3.1"
183	mv LICENSE.ipw2200-fw "${tempFirmwareDir}/${driver}/"
184	mv ipw2200-ibss.fw "${tempFirmwareDir}/${driver}/"
185	mv ipw2200-sniffer.fw "${tempFirmwareDir}/${driver}/"
186	mv ipw2200-bss.fw "${tempFirmwareDir}/${driver}/"
187
188	rm "${tempFirmwareDir}/${driver}/${file}"
189	PostFirmwareInstallation
190}
191
192
193function InstallBroadcom43xx()
194{
195	driver='broadcom43xx'
196	PreFirmwareInstallation
197
198	pkgman install -y cmd:b43_fwcutter
199	returnCode=$?
200	if [ $returnCode -gt 0 ] ; then
201		echo "...failed. ${driver}'s firmware will not be installed."
202		return $returnCode
203	fi
204
205	CutAndInstallBroadcomFirmware
206	returnCode=$?
207	if [ $returnCode -gt 0 ] ; then
208		echo "...failed. ${driver}'s firmware will not be installed."
209		return $returnCode
210	fi
211
212	PostFirmwareInstallation
213}
214
215
216function InstallMarvell88w8335()
217{
218	driver='marvell88w8335'
219	PreFirmwareInstallation
220
221	# Download firmware archive.
222	local file="malo-firmware-1.4.tgz"
223	local url="${baseURL}/marvell/${file}"
224	local dir="${tempFirmwareDir}/${driver}"
225	DownloadFileIfNotCached $url $file "$dir"
226	if [ $result -gt 0 ]; then
227		echo "...failed. ${driver}'s firmware will not be installed."
228		return $result
229	fi
230
231	# Extract archive.
232	cd "$tempDir"
233	tar xf "${tempFirmwareDir}/${driver}/$file"
234
235	# Move firmware files to destination.
236	local sourceDir="${tempDir}/share/examples/malo-firmware"
237	mv ${sourceDir}/malo8335-h "${tempFirmwareDir}/${driver}"
238	mv ${sourceDir}/malo8335-m "${tempFirmwareDir}/${driver}"
239
240	rm "${tempFirmwareDir}/${driver}/${file}"
241	PostFirmwareInstallation
242}
243
244
245function CutAndInstallBroadcomFirmware()
246{
247	# Download firmware.
248	local file="wl_apsta-3.130.20.0.o"
249	local dir="${tempFirmwareDir}/${driver}"
250	local url="${baseURL}/b43/${file}"
251	DownloadFileIfNotCached $url $file $dir
252	if [ $result -gt 0 ]; then
253		return $result
254	fi
255
256	# Cut firmware in pieces.
257	cp "$dir/$file" "$tempDir"
258	cd "$tempDir"
259	b43-fwcutter $file > /dev/null 2>&1
260
261	# Rename the pieces.
262	cd b43legacy
263	for i in $(ls -1); do
264		newFileName=$(echo $i | sed "s/\(.*\)\.fw$/bwi_v3_\1/g")
265		mv $i $newFileName
266	done
267	touch bwi_v3_ucode
268
269	# Install files.
270	mv * ${tempFirmwareDir}/${driver}/
271
272	rm 	"${tempFirmwareDir}/${driver}/$file"
273	return 0
274}
275
276
277function makePackageInfo()
278{
279	cat << EOF > .PackageInfo
280name			wifi_firmwares
281version			2013_10_06-1
282architecture	any
283summary			"Firmwares for various wireless network cards"
284description		"Installs firmwares for the following wireless network cards:
285Broadcom 43xx, Intel ipw2100, Intel ipw2200, and Marvell 88W8335.
286Firmware for broadcom43xx is under the Copyright of Broadcom Corporation(R).
287Firmware for marvell88w8335 is under the Copyright of Marvell Technology(R).
288ipw2100,iprowifi2200 firmware is covered by the Intel(R) license located in
289/boot/system/data/licenses/Intel (2xxx firmware). The user is not granted a
290license to use the package unless these terms are agreed to."
291
292packager	"me"
293vendor		"myself"
294copyrights {
295	"Copyright of Broadcom Corporation(R)"
296	"Copyright of Intel(R) Corporation"
297	"Copyright of Marvell Technology(R)"
298}
299licenses	"Intel (2xxx firmware)"
300flags {
301	"approve_license"
302	"system_package"
303}
304provides {
305	wifi_firmwares = 2013_10_06
306}
307requires {
308}
309
310EOF
311
312}
313
314
315function MakeHPKG()
316{
317	cd "$tempFirmwareDir/../../.."
318	makePackageInfo
319	package create -C system -i .PackageInfo wifi_firmwares-1-any.hpkg
320	mv wifi_firmwares-1-any.hpkg `finddir B_SYSTEM_PACKAGES_DIRECTORY`
321	rm -rf "`finddir B_SYSTEM_TEMP_DIRECTORY`/package_me"
322	rm -rf "$tempDir"
323}
324
325
326mkdir -p "$tempDir"
327mkdir -p "$tempFirmwareDir"
328DisplayAlert
329