xref: /haiku/data/bin/install-wifi-firmwares.sh (revision 529cd177b573aaba391c8adc9c9f5ad76a14bf81)
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 UnlinkDriver()
93{
94	# remove the driver's symlink
95	#rm -f "${driversDir}/dev/net/${driver}"
96	echo "TODO: UnlinkDriver"
97}
98
99
100function SymlinkDriver()
101{
102	# restore the driver's symlink
103	#cd "${driversDir}/dev/net/"
104	#ln -sf "../../bin/${driver}" "${driver}"
105	echo "TODO: SymlinkDriver"
106}
107
108
109function DownloadFileIfNotCached()
110{
111	# DownloadFileIfNotCached <url> <filename> <destination dir>
112	local url=$1
113	local file=$2
114	local dir=$3
115
116	mkdir -p "$dir"
117	if [ ! -e $dir/$file ] ; then
118		echo "Downloading $url ..."
119		wget -nv -O $dir/$file $url
120	fi
121	result=$?
122	if [ $result -gt 0 ]; then
123		local error="Failed to download $url."
124		local msg="As a result, ${driver}'s firmware will not be installed."
125		alert --warning "$error $msg"
126	fi
127}
128
129
130function SetFirmwarePermissions()
131{
132	cd ${tempFirmwareDir}/${driver}/
133	for file in * ; do
134		if [ "$file" != "$driver" ] && [ -f "$file" ] ; then
135			chmod a=r $file
136		fi
137	done
138}
139
140
141function CleanTemporaryFiles()
142{
143	rm -rf "$tempDir"
144	mkdir -p "$tempDir"
145}
146
147
148function PreFirmwareInstallation()
149{
150	echo "Acquiring firmware for ${driver} ..."
151	mkdir -p "${tempFirmwareDir}/${driver}"
152	UnlinkDriver
153}
154
155
156function PostFirmwareInstallation()
157{
158	SetFirmwarePermissions
159	SymlinkDriver
160	CleanTemporaryFiles
161	echo "... firmware for ${driver} will be installed."
162}
163
164
165function InstallIpw2100()
166{
167	driver='iprowifi2100'
168	PreFirmwareInstallation
169
170	# Prepare firmware archive for extraction.
171	local file='ipw2100-fw-1.3.tgz'
172	local url="${baseURL}/intel/${file}"
173	local dir="${tempFirmwareDir}/${driver}"
174	cp "${firmwareDir}/${driver}/${file}" "${dir}"
175	DownloadFileIfNotCached $url $file $dir
176
177	# Extract the firmware & license file in place.
178	cd "${tempFirmwareDir}/${driver}"
179	gunzip < "$file" | tar xf -
180
181	rm "${tempFirmwareDir}/${driver}/${file}"
182	PostFirmwareInstallation
183}
184
185
186function InstallIprowifi2200()
187{
188	driver='iprowifi2200'
189	PreFirmwareInstallation
190
191	# Prepare firmware archive for extraction.
192	local file='ipw2200-fw-3.1.tgz'
193	local url="${baseURL}/intel/${file}"
194	local dir="${tempFirmwareDir}/${driver}"
195	cp "${firmwareDir}/${driver}/${file}" "${dir}"
196	DownloadFileIfNotCached $url $file $dir
197
198	# Extract the firmware & license file.
199	cd "$tempDir"
200	gunzip < "${tempFirmwareDir}/${driver}/$file" | tar xf -
201	cd "${tempDir}/ipw2200-fw-3.1"
202	mv LICENSE.ipw2200-fw "${tempFirmwareDir}/${driver}/"
203	mv ipw2200-ibss.fw "${tempFirmwareDir}/${driver}/"
204	mv ipw2200-sniffer.fw "${tempFirmwareDir}/${driver}/"
205	mv ipw2200-bss.fw "${tempFirmwareDir}/${driver}/"
206
207	rm "${tempFirmwareDir}/${driver}/${file}"
208	PostFirmwareInstallation
209}
210
211
212function InstallBroadcom43xx()
213{
214	driver='broadcom43xx'
215	PreFirmwareInstallation
216
217	BuildBroadcomFWCutter
218	returnCode=$?
219	if [ $returnCode -gt 0 ] ; then
220		echo "...failed. ${driver}'s firmware will not be installed."
221		return $returnCode
222	fi
223
224	CutAndInstallBroadcomFirmware
225	returnCode=$?
226	if [ $returnCode -gt 0 ] ; then
227		echo "...failed. ${driver}'s firmware will not be installed."
228		return $returnCode
229	fi
230
231	PostFirmwareInstallation
232}
233
234
235function InstallMarvell88w8335()
236{
237	driver='marvell88w8335'
238	PreFirmwareInstallation
239
240	# Download firmware archive.
241	local file="malo-firmware-1.4.tgz"
242	local url="${baseURL}/marvell/${file}"
243	local dir="${tempFirmwareDir}/${driver}"
244	DownloadFileIfNotCached $url $file "$dir"
245	if [ $result -gt 0 ]; then
246		echo "...failed. ${driver}'s firmware will not be installed."
247		return $result
248	fi
249
250	# Extract archive.
251	cd "$tempDir"
252	tar xf "${tempFirmwareDir}/${driver}/$file"
253
254	# Move firmware files to destination.
255	local sourceDir="${tempDir}/share/examples/malo-firmware"
256	mv ${sourceDir}/malo8335-h "${tempFirmwareDir}/${driver}"
257	mv ${sourceDir}/malo8335-m "${tempFirmwareDir}/${driver}"
258
259	rm "${tempFirmwareDir}/${driver}/${file}"
260	PostFirmwareInstallation
261}
262
263
264function BuildBroadcomFWCutter()
265{
266	# Download & extract b43-fwcutter.
267	local file="b43-fwcutter-012.tar.bz2"
268	local dir="${tempFirmwareDir}/${driver}/b43-fwcutter"
269	local url="${baseURL}/b43/fwcutter/${file}"
270	DownloadFileIfNotCached $url $file $dir
271	if [ $result -gt 0 ]; then
272		return $result
273	fi
274
275	# Extract archive.
276	cd "$tempDir"
277	tar xjf "$dir/$file"
278
279	# Download additonal files for building b43-fwcutter.
280	cd b43-fwcutter-012
281	local baseURL='http://cgit.haiku-os.org/haiku/plain/src/system/libroot/posix/glibc'
282	DownloadFileIfNotCached ${baseURL}/string/byteswap.h byteswap.h $dir
283	if [ $result -gt 0 ]; then
284		return $result
285	fi
286	DownloadFileIfNotCached ${baseURL}/include/arch/x86/bits/byteswap.h byteswap.h $dir/bits
287	if [ $result -gt 0 ]; then
288		return $result
289	fi
290
291	# Copy those files to working directory.
292	mkdir -p bits
293	cp $dir/byteswap.h .
294	cp $dir/bits/byteswap.h bits/
295
296	# Build b43-fwcutter.
297	echo "Compiling b43-fwcutter for installing Broadcom's firmware ..."
298	make PREFIX=/boot/system CFLAGS="-I. -Wall -D_BSD_SOURCE" > /dev/null 2>&1
299	result=$?
300	if [ $result -gt 0 ]; then
301		echo "... failed to compile b43-fwcutter."
302	else
303		echo "... successfully compiled b43-fwcutter."
304	fi
305	if [ ! -e b43-fwcutter ] ; then
306		return 1
307	fi
308	mv b43-fwcutter "$tempDir"
309
310	cd "${tempFirmwareDir}/${driver}/b43-fwcutter"
311	rm b43-fwcutter-012.tar.bz2
312	rm byteswap.h
313	rm bits/byteswap.h
314	rmdir bits
315	cd ..
316	rmdir b43-fwcutter
317
318	return 0
319}
320
321
322function CutAndInstallBroadcomFirmware()
323{
324	# Download firmware.
325	local file="wl_apsta-3.130.20.0.o"
326	local dir="${tempFirmwareDir}/${driver}"
327	local url="${baseURL}/b43/${file}"
328	DownloadFileIfNotCached $url $file $dir
329	if [ $result -gt 0 ]; then
330		return $result
331	fi
332
333	# Cut firmware in pieces.
334	cp "$dir/$file" "$tempDir"
335	cd "$tempDir"
336	b43-fwcutter $file > /dev/null 2>&1
337
338	# Rename the pieces.
339	cd b43legacy
340	for i in $(ls -1); do
341		newFileName=$(echo $i | sed "s/\(.*\)\.fw$/bwi_v3_\1/g")
342		mv $i $newFileName
343	done
344	touch bwi_v3_ucode
345
346	# Install files.
347	mv * ${tempFirmwareDir}/${driver}/
348
349	rm 	"${tempFirmwareDir}/${driver}/$file"
350	return 0
351}
352
353
354function makePackageInfo()
355{
356	cat << EOF > .PackageInfo
357name			wifi_firmwares
358version			2013_10_06-1
359architecture	any
360summary			"Firmwares for various wireless network cards"
361description		"Installs firmwares for the following wireless network cards:
362Broadcom 43xx, Intel ipw2100, Intel ipw2200, and Marvell 88W8335.
363Firmware for broadcom43xx is under the Copyright of Broadcom Corporation(R).
364Firmware for marvell88w8335 is under the Copyright of Marvell Technology(R).
365ipw2100,iprowifi2200 firmware is covered by the Intel(R) license located in
366/boot/system/data/licenses/Intel (2xxx firmware). The user is not granted a
367license to use the package unless these terms are agreed to."
368
369packager	"me"
370vendor		"myself"
371copyrights {
372	"Copyright of Broadcom Corporation(R)"
373	"Copyright of Intel(R) Corporation"
374	"Copyright of Marvell Technology(R)"
375}
376licenses	"Intel (2xxx firmware)"
377flags {
378	"approve_license"
379	"system_package"
380}
381provides {
382	wifi_firmwares = 2013_10_06
383}
384requires {
385	haiku >= R1~alpha4
386}
387
388EOF
389
390}
391
392
393function MakeHPKG()
394{
395	cd "$tempFirmwareDir/../../.."
396	makePackageInfo
397	package create -C system -i .PackageInfo wifi_firmwares-1-any.hpkg
398	mv wifi_firmwares-1-any.hpkg `finddir B_SYSTEM_PACKAGES_DIRECTORY`
399	rm -rf "`finddir B_SYSTEM_TEMP_DIRECTORY`/package_me"
400	rm -rf "$tempDir"
401}
402
403
404mkdir -p "$tempDir"
405mkdir -p "$tempFirmwareDir"
406DisplayAlert
407