1#!/usr/bin/perl
2#     DVB firmware extractor
3#
4#     (c) 2004 Andrew de Quincey
5#
6#     This program is free software; you can redistribute it and/or modify
7#       it under the terms of the GNU General Public License as published by
8#       the Free Software Foundation; either version 2 of the License, or
9#       (at your option) any later version.
10#
11#     This program is distributed in the hope that it will be useful,
12#       but WITHOUT ANY WARRANTY; without even the implied warranty of
13#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#
15#     GNU General Public License for more details.
16#
17#     You should have received a copy of the GNU General Public License
18#       along with this program; if not, write to the Free Software
19#       Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21use File::Temp qw/ tempdir /;
22use IO::Handle;
23
24@components = ( "sp8870", "sp887x", "tda10045", "tda10046",
25		"tda10046lifeview", "av7110", "dec2000t", "dec2540t",
26		"dec3000s", "vp7041", "dibusb", "nxt2002", "nxt2004",
27		"or51211", "or51132_qam", "or51132_vsb", "bluebird");
28
29# Check args
30syntax() if (scalar(@ARGV) != 1);
31$cid = $ARGV[0];
32
33# Do it!
34for ($i=0; $i < scalar(@components); $i++) {
35    if ($cid eq $components[$i]) {
36	$outfile = eval($cid);
37	die $@ if $@;
38	print STDERR <<EOF;
39Firmware $outfile extracted successfully.
40Now copy it to either /usr/lib/hotplug/firmware or /lib/firmware
41(depending on configuration of firmware hotplug).
42EOF
43	exit(0);
44    }
45}
46
47# If we get here, it wasn't found
48print STDERR "Unknown component \"$cid\"\n";
49syntax();
50
51
52
53
54# ---------------------------------------------------------------
55# Firmware-specific extraction subroutines
56
57sub sp8870 {
58    my $sourcefile = "tt_Premium_217g.zip";
59    my $url = "http://www.technotrend.de/new/217g/$sourcefile";
60    my $hash = "53970ec17a538945a6d8cb608a7b3899";
61    my $outfile = "dvb-fe-sp8870.fw";
62    my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
63
64    checkstandard();
65
66    wgetfile($sourcefile, $url);
67    unzip($sourcefile, $tmpdir);
68    verify("$tmpdir/software/OEM/HE/App/boot/SC_MAIN.MC", $hash);
69    copy("$tmpdir/software/OEM/HE/App/boot/SC_MAIN.MC", $outfile);
70
71    $outfile;
72}
73
74sub sp887x {
75    my $sourcefile = "Dvbt1.3.57.6.zip";
76    my $url = "http://www.avermedia.com/software/$sourcefile";
77    my $cabfile = "DVBT Net  Ver1.3.57.6/disk1/data1.cab";
78    my $hash = "237938d53a7f834c05c42b894ca68ac3";
79    my $outfile = "dvb-fe-sp887x.fw";
80    my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
81
82    checkstandard();
83    checkunshield();
84
85    wgetfile($sourcefile, $url);
86    unzip($sourcefile, $tmpdir);
87    unshield("$tmpdir/$cabfile", $tmpdir);
88    verify("$tmpdir/ZEnglish/sc_main.mc", $hash);
89    copy("$tmpdir/ZEnglish/sc_main.mc", $outfile);
90
91    $outfile;
92}
93
94sub tda10045 {
95    my $sourcefile = "tt_budget_217g.zip";
96    my $url = "http://www.technotrend.de/new/217g/$sourcefile";
97    my $hash = "2105fd5bf37842fbcdfa4bfd58f3594a";
98    my $outfile = "dvb-fe-tda10045.fw";
99    my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
100
101    checkstandard();
102
103    wgetfile($sourcefile, $url);
104    unzip($sourcefile, $tmpdir);
105    extract("$tmpdir/software/OEM/PCI/App/ttlcdacc.dll", 0x37ef9, 30555, "$tmpdir/fwtmp");
106    verify("$tmpdir/fwtmp", $hash);
107    copy("$tmpdir/fwtmp", $outfile);
108
109    $outfile;
110}
111
112sub tda10046 {
113    my $sourcefile = "tt_budget_217g.zip";
114    my $url = "http://www.technotrend.de/new/217g/$sourcefile";
115    my $hash = "6a7e1e2f2644b162ff0502367553c72d";
116    my $outfile = "dvb-fe-tda10046.fw";
117    my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
118
119    checkstandard();
120
121    wgetfile($sourcefile, $url);
122    unzip($sourcefile, $tmpdir);
123    extract("$tmpdir/software/OEM/PCI/App/ttlcdacc.dll", 0x3f731, 24478, "$tmpdir/fwtmp");
124    verify("$tmpdir/fwtmp", $hash);
125    copy("$tmpdir/fwtmp", $outfile);
126
127    $outfile;
128}
129
130sub tda10046lifeview {
131    my $sourcefile = "Drv_2.11.02.zip";
132    my $url = "http://www.lifeview.com.tw/drivers/pci_card/FlyDVB-T/$sourcefile";
133    my $hash = "1ea24dee4eea8fe971686981f34fd2e0";
134    my $outfile = "dvb-fe-tda10046.fw";
135    my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
136
137    checkstandard();
138
139    wgetfile($sourcefile, $url);
140    unzip($sourcefile, $tmpdir);
141    extract("$tmpdir/LVHybrid.sys", 0x8b088, 24602, "$tmpdir/fwtmp");
142    verify("$tmpdir/fwtmp", $hash);
143    copy("$tmpdir/fwtmp", $outfile);
144
145    $outfile;
146}
147
148sub av7110 {
149    my $sourcefile = "dvb-ttpci-01.fw-261d";
150    my $url = "http://www.linuxtv.org/downloads/firmware/$sourcefile";
151    my $hash = "603431b6259715a8e88f376a53b64e2f";
152    my $outfile = "dvb-ttpci-01.fw";
153
154    checkstandard();
155
156    wgetfile($sourcefile, $url);
157    verify($sourcefile, $hash);
158    copy($sourcefile, $outfile);
159
160    $outfile;
161}
162
163sub dec2000t {
164    my $sourcefile = "dec217g.exe";
165    my $url = "http://hauppauge.lightpath.net/de/$sourcefile";
166    my $hash = "bd86f458cee4a8f0a8ce2d20c66215a9";
167    my $outfile = "dvb-ttusb-dec-2000t.fw";
168    my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
169
170    checkstandard();
171
172    wgetfile($sourcefile, $url);
173    unzip($sourcefile, $tmpdir);
174    verify("$tmpdir/software/OEM/STB/App/Boot/STB_PC_T.bin", $hash);
175    copy("$tmpdir/software/OEM/STB/App/Boot/STB_PC_T.bin", $outfile);
176
177    $outfile;
178}
179
180sub dec2540t {
181    my $sourcefile = "dec217g.exe";
182    my $url = "http://hauppauge.lightpath.net/de/$sourcefile";
183    my $hash = "53e58f4f5b5c2930beee74a7681fed92";
184    my $outfile = "dvb-ttusb-dec-2540t.fw";
185    my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
186
187    checkstandard();
188
189    wgetfile($sourcefile, $url);
190    unzip($sourcefile, $tmpdir);
191    verify("$tmpdir/software/OEM/STB/App/Boot/STB_PC_X.bin", $hash);
192    copy("$tmpdir/software/OEM/STB/App/Boot/STB_PC_X.bin", $outfile);
193
194    $outfile;
195}
196
197sub dec3000s {
198    my $sourcefile = "dec217g.exe";
199    my $url = "http://hauppauge.lightpath.net/de/$sourcefile";
200    my $hash = "b013ececea83f4d6d8d2a29ac7c1b448";
201    my $outfile = "dvb-ttusb-dec-3000s.fw";
202    my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
203
204    checkstandard();
205
206    wgetfile($sourcefile, $url);
207    unzip($sourcefile, $tmpdir);
208    verify("$tmpdir/software/OEM/STB/App/Boot/STB_PC_S.bin", $hash);
209    copy("$tmpdir/software/OEM/STB/App/Boot/STB_PC_S.bin", $outfile);
210
211    $outfile;
212}
213
214sub vp7041 {
215    my $sourcefile = "2.422.zip";
216    my $url = "http://www.twinhan.com/files/driver/USB-Ter/$sourcefile";
217    my $hash = "e88c9372d1f66609a3e7b072c53fbcfe";
218    my $outfile = "dvb-vp7041-2.422.fw";
219    my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
220
221    checkstandard();
222
223    wgetfile($sourcefile, $url);
224    unzip($sourcefile, $tmpdir);
225    extract("$tmpdir/VisionDTV/Drivers/Win2K&XP/UDTTload.sys", 12503, 3036, "$tmpdir/fwtmp1");
226    extract("$tmpdir/VisionDTV/Drivers/Win2K&XP/UDTTload.sys", 2207, 10274, "$tmpdir/fwtmp2");
227
228    my $CMD = "\000\001\000\222\177\000";
229    my $PAD = "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000";
230    my ($FW);
231    open $FW, ">$tmpdir/fwtmp3";
232    print $FW "$CMD\001$PAD";
233    print $FW "$CMD\001$PAD";
234    appendfile($FW, "$tmpdir/fwtmp1");
235    print $FW "$CMD\000$PAD";
236    print $FW "$CMD\001$PAD";
237    appendfile($FW, "$tmpdir/fwtmp2");
238    print $FW "$CMD\001$PAD";
239    print $FW "$CMD\000$PAD";
240    close($FW);
241
242    verify("$tmpdir/fwtmp3", $hash);
243    copy("$tmpdir/fwtmp3", $outfile);
244
245    $outfile;
246}
247
248sub dibusb {
249	my $url = "http://www.linuxtv.org/downloads/firmware/dvb-usb-dibusb-5.0.0.11.fw";
250	my $outfile = "dvb-dibusb-5.0.0.11.fw";
251	my $hash = "fa490295a527360ca16dcdf3224ca243";
252
253	checkstandard();
254
255	wgetfile($outfile, $url);
256	verify($outfile,$hash);
257
258	$outfile;
259}
260
261sub nxt2002 {
262    my $sourcefile = "Technisat_DVB-PC_4_4_COMPACT.zip";
263    my $url = "http://www.bbti.us/download/windows/$sourcefile";
264    my $hash = "476befae8c7c1bb9648954060b1eec1f";
265    my $outfile = "dvb-fe-nxt2002.fw";
266    my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
267
268    checkstandard();
269
270    wgetfile($sourcefile, $url);
271    unzip($sourcefile, $tmpdir);
272    verify("$tmpdir/SkyNET.sys", $hash);
273    extract("$tmpdir/SkyNET.sys", 331624, 5908, $outfile);
274
275    $outfile;
276}
277
278sub nxt2004 {
279    my $sourcefile = "AVerTVHD_MCE_A180_Drv_v1.2.2.16.zip";
280    my $url = "http://www.aver.com/support/Drivers/$sourcefile";
281    my $hash = "111cb885b1e009188346d72acfed024c";
282    my $outfile = "dvb-fe-nxt2004.fw";
283    my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
284
285    checkstandard();
286
287    wgetfile($sourcefile, $url);
288    unzip($sourcefile, $tmpdir);
289    verify("$tmpdir/3xHybrid.sys", $hash);
290    extract("$tmpdir/3xHybrid.sys", 465304, 9584, $outfile);
291
292    $outfile;
293}
294
295sub or51211 {
296    my $fwfile = "dvb-fe-or51211.fw";
297    my $url = "http://linuxtv.org/downloads/firmware/$fwfile";
298    my $hash = "d830949c771a289505bf9eafc225d491";
299
300    checkstandard();
301
302    wgetfile($fwfile, $url);
303    verify($fwfile, $hash);
304
305    $fwfile;
306}
307
308sub or51132_qam {
309    my $fwfile = "dvb-fe-or51132-qam.fw";
310    my $url = "http://linuxtv.org/downloads/firmware/$fwfile";
311    my $hash = "7702e8938612de46ccadfe9b413cb3b5";
312
313    checkstandard();
314
315    wgetfile($fwfile, $url);
316    verify($fwfile, $hash);
317
318    $fwfile;
319}
320
321sub or51132_vsb {
322    my $fwfile = "dvb-fe-or51132-vsb.fw";
323    my $url = "http://linuxtv.org/downloads/firmware/$fwfile";
324    my $hash = "c16208e02f36fc439a557ad4c613364a";
325
326    checkstandard();
327
328    wgetfile($fwfile, $url);
329    verify($fwfile, $hash);
330
331    $fwfile;
332}
333
334sub bluebird {
335	my $url = "http://www.linuxtv.org/download/dvb/firmware/dvb-usb-bluebird-01.fw";
336	my $outfile = "dvb-usb-bluebird-01.fw";
337	my $hash = "658397cb9eba9101af9031302671f49d";
338
339	checkstandard();
340
341	wgetfile($outfile, $url);
342	verify($outfile,$hash);
343
344	$outfile;
345}
346
347# ---------------------------------------------------------------
348# Utilities
349
350sub checkstandard {
351    if (system("which unzip > /dev/null 2>&1")) {
352	die "This firmware requires the unzip command - see ftp://ftp.info-zip.org/pub/infozip/UnZip.html\n";
353    }
354    if (system("which md5sum > /dev/null 2>&1")) {
355	die "This firmware requires the md5sum command - see http://www.gnu.org/software/coreutils/\n";
356    }
357    if (system("which wget > /dev/null 2>&1")) {
358	die "This firmware requires the wget command - see http://wget.sunsite.dk/\n";
359    }
360}
361
362sub checkunshield {
363    if (system("which unshield > /dev/null 2>&1")) {
364	die "This firmware requires the unshield command - see http://sourceforge.net/projects/synce/\n";
365    }
366}
367
368sub wgetfile {
369    my ($sourcefile, $url) = @_;
370
371    if (! -f $sourcefile) {
372	system("wget -O \"$sourcefile\" \"$url\"") and die "wget failed - unable to download firmware";
373    }
374}
375
376sub unzip {
377    my ($sourcefile, $todir) = @_;
378
379    $status = system("unzip -q -o -d \"$todir\" \"$sourcefile\" 2>/dev/null" );
380    if ((($status >> 8) > 2) || (($status & 0xff) != 0)) {
381	die ("unzip failed - unable to extract firmware");
382    }
383}
384
385sub unshield {
386    my ($sourcefile, $todir) = @_;
387
388    system("unshield x -d \"$todir\" \"$sourcefile\" > /dev/null" ) and die ("unshield failed - unable to extract firmware");
389}
390
391sub verify {
392    my ($filename, $hash) = @_;
393    my ($testhash);
394
395    open(CMD, "md5sum \"$filename\"|");
396    $testhash = <CMD>;
397    $testhash =~ /([a-zA-Z0-9]*)/;
398    $testhash = $1;
399    close CMD;
400    die "Hash of extracted file does not match!\n" if ($testhash ne $hash);
401}
402
403sub copy {
404    my ($from, $to) = @_;
405
406    system("cp -f \"$from\" \"$to\"") and die ("cp failed");
407}
408
409sub extract {
410    my ($infile, $offset, $length, $outfile) = @_;
411    my ($chunklength, $buf, $rcount);
412
413    open INFILE, "<$infile";
414    open OUTFILE, ">$outfile";
415    sysseek(INFILE, $offset, SEEK_SET);
416    while($length > 0) {
417	# Calc chunk size
418	$chunklength = 2048;
419	$chunklength = $length if ($chunklength > $length);
420
421	$rcount = sysread(INFILE, $buf, $chunklength);
422	die "Ran out of data\n" if ($rcount != $chunklength);
423	syswrite(OUTFILE, $buf);
424	$length -= $rcount;
425    }
426    close INFILE;
427    close OUTFILE;
428}
429
430sub appendfile {
431    my ($FH, $infile) = @_;
432    my ($buf);
433
434    open INFILE, "<$infile";
435    while(1) {
436	$rcount = sysread(INFILE, $buf, 2048);
437	last if ($rcount == 0);
438	print $FH $buf;
439    }
440    close(INFILE);
441}
442
443sub syntax() {
444    print STDERR "syntax: get_dvb_firmware <component>\n";
445    print STDERR "Supported components:\n";
446    for($i=0; $i < scalar(@components); $i++) {
447	print STDERR "\t" . $components[$i] . "\n";
448    }
449    exit(1);
450}
451