xref: /haiku/data/bin/install-wifi-firmwares.sh (revision 2897df967633aab846ff4917b53e2af7d1e54eeb)
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 http://www.haiku-os.org/guides/dailytasks/wireless. This page has
27 instructions on which files to manually download and where to copy them into
28 this OS. It also has different script that can be run on another OS and will
29 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='http://www.haiku-files.org/files/wifi-firmwares'
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	BuildBroadcomFWCutter
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 BuildBroadcomFWCutter()
246{
247	# Download & extract b43-fwcutter.
248	local file="b43-fwcutter-012.tar.bz2"
249	local dir="${tempFirmwareDir}/${driver}/b43-fwcutter"
250	local url="${baseURL}/b43/fwcutter/${file}"
251	DownloadFileIfNotCached $url $file $dir
252	if [ $result -gt 0 ]; then
253		return $result
254	fi
255
256	# Extract archive.
257	cd "$tempDir"
258	tar xjf "$dir/$file"
259
260	# Download additonal files for building b43-fwcutter.
261	cd b43-fwcutter-012
262	local baseURL='https://git.haiku-os.org/haiku/plain/src/system/libroot/posix/glibc'
263	DownloadFileIfNotCached ${baseURL}/string/byteswap.h byteswap.h $dir
264	if [ $result -gt 0 ]; then
265		return $result
266	fi
267	DownloadFileIfNotCached ${baseURL}/include/arch/x86/bits/byteswap.h byteswap.h $dir/bits
268	if [ $result -gt 0 ]; then
269		return $result
270	fi
271
272	# Copy those files to working directory.
273	mkdir -p bits
274	cp $dir/byteswap.h .
275	cp $dir/bits/byteswap.h bits/
276
277	# Build b43-fwcutter.
278	echo "Compiling b43-fwcutter for installing Broadcom's firmware ..."
279	make PREFIX=/boot/system CFLAGS="-I. -Wall -D_BSD_SOURCE" >/dev/null 2>&1
280	result=$?
281	if [ $result -gt 0 ]; then
282		echo "... failed to compile b43-fwcutter."
283	else
284		echo "... successfully compiled b43-fwcutter."
285	fi
286	if [ ! -e b43-fwcutter ] ; then
287		return 1
288	fi
289	mv b43-fwcutter "$tempDir"
290
291	cd "${tempFirmwareDir}/${driver}/b43-fwcutter"
292	rm b43-fwcutter-012.tar.bz2
293	rm byteswap.h
294	rm bits/byteswap.h
295	rmdir bits
296	cd ..
297	rmdir b43-fwcutter
298
299	return 0
300}
301
302
303function CutAndInstallBroadcomFirmware()
304{
305	# Download firmware.
306	local file="wl_apsta-3.130.20.0.o"
307	local dir="${tempFirmwareDir}/${driver}"
308	local url="${baseURL}/b43/${file}"
309	DownloadFileIfNotCached $url $file $dir
310	if [ $result -gt 0 ]; then
311		return $result
312	fi
313
314	# Cut firmware in pieces.
315	cp "$dir/$file" "$tempDir"
316	cd "$tempDir"
317	b43-fwcutter $file > /dev/null 2>&1
318
319	# Rename the pieces.
320	cd b43legacy
321	for i in $(ls -1); do
322		newFileName=$(echo $i | sed "s/\(.*\)\.fw$/bwi_v3_\1/g")
323		mv $i $newFileName
324	done
325	touch bwi_v3_ucode
326
327	# Install files.
328	mv * ${tempFirmwareDir}/${driver}/
329
330	rm 	"${tempFirmwareDir}/${driver}/$file"
331	return 0
332}
333
334
335function makePackageInfo()
336{
337	cat << EOF > .PackageInfo
338name			wifi_firmwares
339version			2013_10_06-1
340architecture	any
341summary			"Firmwares for various wireless network cards"
342description		"Installs firmwares for the following wireless network cards:
343Broadcom 43xx, Intel ipw2100, Intel ipw2200, and Marvell 88W8335.
344Firmware for broadcom43xx is under the Copyright of Broadcom Corporation(R).
345Firmware for marvell88w8335 is under the Copyright of Marvell Technology(R).
346ipw2100,iprowifi2200 firmware is covered by the Intel(R) license located in
347/boot/system/data/licenses/Intel (2xxx firmware). The user is not granted a
348license to use the package unless these terms are agreed to."
349
350packager	"me"
351vendor		"myself"
352copyrights {
353	"Copyright of Broadcom Corporation(R)"
354	"Copyright of Intel(R) Corporation"
355	"Copyright of Marvell Technology(R)"
356}
357licenses	"Intel (2xxx firmware)"
358flags {
359	"approve_license"
360	"system_package"
361}
362provides {
363	wifi_firmwares = 2013_10_06
364}
365requires {
366}
367
368EOF
369
370}
371
372
373function MakeHPKG()
374{
375	cd "$tempFirmwareDir/../../.."
376	makePackageInfo
377	package create -C system -i .PackageInfo wifi_firmwares-1-any.hpkg
378	mv wifi_firmwares-1-any.hpkg `finddir B_SYSTEM_PACKAGES_DIRECTORY`
379	rm -rf "`finddir B_SYSTEM_TEMP_DIRECTORY`/package_me"
380	rm -rf "$tempDir"
381}
382
383
384mkdir -p "$tempDir"
385mkdir -p "$tempFirmwareDir"
386DisplayAlert
387