This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / Documentation / dvb / get_dvb_firmware
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
21 use File::Temp qw/ tempdir /;
22 use IO::Handle;
23
24 @components = ( "sp8870", "sp887x", "tda10045", "tda10046", "av7110", "dec2000t", "dec2540t", "dec3000s", "vp7041", "dibusb" );
25
26 # Check args
27 syntax() if (scalar(@ARGV) != 1);
28 $cid = $ARGV[0];
29
30 # Do it!
31 for($i=0; $i < scalar(@components); $i++) {
32     if ($cid eq $components[$i]) {
33         $outfile = eval($cid);
34         die $@ if $@;
35         print STDERR "Firmware $outfile extracted successfully. Now copy it to either /lib/firmware or /usr/lib/hotplug/firmware/ (depending on your hotplug version).\n";
36         exit(0);
37     }
38 }
39
40 # If we get here, it wasn't found
41 print STDERR "Unknown component \"$cid\"\n";
42 syntax();
43
44
45
46
47 # ---------------------------------------------------------------
48 # Firmware-specific extraction subroutines
49
50 sub sp8870 {
51     my $sourcefile = "tt_Premium_217g.zip";
52     my $url = "http://www.technotrend.de/new/217g/$sourcefile";
53     my $hash = "53970ec17a538945a6d8cb608a7b3899";
54     my $outfile = "dvb-fe-sp8870.fw";
55     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
56
57     checkstandard();
58
59     wgetfile($sourcefile, $url);
60     unzip($sourcefile, $tmpdir);
61     verify("$tmpdir/software/OEM/HE/App/boot/SC_MAIN.MC", $hash);
62     copy("$tmpdir/software/OEM/HE/App/boot/SC_MAIN.MC", $outfile);
63
64     $outfile;
65 }
66
67 sub sp887x {
68     my $sourcefile = "Dvbt1.3.57.6.zip";
69     my $url = "http://www.avermedia.com/software/$sourcefile";
70     my $cabfile = "DVBT Net  Ver1.3.57.6/disk1/data1.cab";
71     my $hash = "237938d53a7f834c05c42b894ca68ac3";
72     my $outfile = "dvb-fe-sp887x.fw";
73     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
74
75     checkstandard();
76     checkunshield();
77
78     wgetfile($sourcefile, $url);
79     unzip($sourcefile, $tmpdir);
80     unshield("$tmpdir/$cabfile", $tmpdir);
81     verify("$tmpdir/sc_main.mc", $hash);
82     copy("$tmpdir/sc_main.mc", $outfile);
83
84     $outfile;
85 }
86
87 sub tda10045 {
88     my $sourcefile = "tt_budget_217g.zip";
89     my $url = "http://www.technotrend.de/new/217g/$sourcefile";
90     my $hash = "2105fd5bf37842fbcdfa4bfd58f3594a";
91     my $outfile = "dvb-fe-tda10045.fw";
92     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
93
94     checkstandard();
95
96     wgetfile($sourcefile, $url);
97     unzip($sourcefile, $tmpdir);
98     extract("$tmpdir/software/OEM/PCI/App/ttlcdacc.dll", 0x37ef9, 30555, "$tmpdir/fwtmp");
99     verify("$tmpdir/fwtmp", $hash);
100     copy("$tmpdir/fwtmp", $outfile);
101
102     $outfile;
103 }
104
105 sub tda10046 {
106     my $sourcefile = "tt_budget_217g.zip";
107     my $url = "http://www.technotrend.de/new/217g/$sourcefile";
108     my $hash = "a25b579e37109af60f4a36c37893957c";
109     my $outfile = "dvb-fe-tda10046.fw";
110     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
111
112     checkstandard();
113
114     wgetfile($sourcefile, $url);
115     unzip($sourcefile, $tmpdir);
116     extract("$tmpdir/software/OEM/PCI/App/ttlcdacc.dll", 0x3f731, 24479, "$tmpdir/fwtmp");
117     verify("$tmpdir/fwtmp", $hash);
118     copy("$tmpdir/fwtmp", $outfile);
119
120     $outfile;
121 }
122
123 sub av7110 {
124     my $sourcefile = "dvb-ttpci-01.fw-261c";
125     my $url = "http://www.linuxtv.org/download/dvb/firmware/$sourcefile";
126     my $hash = "7b263de6b0b92d2347319c65adc7d4fb";
127     my $outfile = "dvb-ttpci-01.fw";
128
129     checkstandard();
130
131     wgetfile($sourcefile, $url);
132     verify($sourcefile, $hash);
133     copy($sourcefile, $outfile);
134
135     $outfile;
136 }
137
138 sub dec2000t {
139     my $sourcefile = "dec217g.exe";
140     my $url = "http://hauppauge.lightpath.net/de/$sourcefile";
141     my $hash = "bd86f458cee4a8f0a8ce2d20c66215a9";
142     my $outfile = "dvb-ttusb-dec-2000t.fw";
143     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
144
145     checkstandard();
146
147     wgetfile($sourcefile, $url);
148     unzip($sourcefile, $tmpdir);
149     verify("$tmpdir/software/OEM/STB/App/Boot/STB_PC_T.bin", $hash);
150     copy("$tmpdir/software/OEM/STB/App/Boot/STB_PC_T.bin", $outfile);
151
152     $outfile;
153 }
154
155 sub dec2540t {
156     my $sourcefile = "dec217g.exe";
157     my $url = "http://hauppauge.lightpath.net/de/$sourcefile";
158     my $hash = "53e58f4f5b5c2930beee74a7681fed92";
159     my $outfile = "dvb-ttusb-dec-2540t.fw";
160     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
161
162     checkstandard();
163
164     wgetfile($sourcefile, $url);
165     unzip($sourcefile, $tmpdir);
166     verify("$tmpdir/software/OEM/STB/App/Boot/STB_PC_X.bin", $hash);
167     copy("$tmpdir/software/OEM/STB/App/Boot/STB_PC_X.bin", $outfile);
168
169     $outfile;
170 }
171
172 sub dec3000s {
173     my $sourcefile = "dec217g.exe";
174     my $url = "http://hauppauge.lightpath.net/de/$sourcefile";
175     my $hash = "b013ececea83f4d6d8d2a29ac7c1b448";
176     my $outfile = "dvb-ttusb-dec-3000s.fw";
177     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
178
179     checkstandard();
180
181     wgetfile($sourcefile, $url);
182     unzip($sourcefile, $tmpdir);
183     verify("$tmpdir/software/OEM/STB/App/Boot/STB_PC_S.bin", $hash);
184     copy("$tmpdir/software/OEM/STB/App/Boot/STB_PC_S.bin", $outfile);
185
186     $outfile;
187 }
188
189 sub vp7041 {
190     my $sourcefile = "2.422.zip";
191     my $url = "http://www.twinhan.com/files/driver/USB-Ter/$sourcefile";
192     my $hash = "e88c9372d1f66609a3e7b072c53fbcfe";
193     my $outfile = "dvb-vp7041-2.422.fw";
194     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
195
196     checkstandard();
197
198     wgetfile($sourcefile, $url);
199     unzip($sourcefile, $tmpdir);
200     extract("$tmpdir/VisionDTV/Drivers/Win2K&XP/UDTTload.sys", 12503, 3036, "$tmpdir/fwtmp1");
201     extract("$tmpdir/VisionDTV/Drivers/Win2K&XP/UDTTload.sys", 2207, 10274, "$tmpdir/fwtmp2");
202
203     my $CMD = "\000\001\000\222\177\000";
204     my $PAD = "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000";
205     my ($FW);
206     open $FW, ">$tmpdir/fwtmp3";
207     print $FW "$CMD\001$PAD";
208     print $FW "$CMD\001$PAD";
209     appendfile($FW, "$tmpdir/fwtmp1");
210     print $FW "$CMD\000$PAD";
211     print $FW "$CMD\001$PAD";
212     appendfile($FW, "$tmpdir/fwtmp2");
213     print $FW "$CMD\001$PAD";
214     print $FW "$CMD\000$PAD";
215     close($FW);
216
217     verify("$tmpdir/fwtmp3", $hash);
218     copy("$tmpdir/fwtmp3", $outfile);
219
220     $outfile;
221 }
222
223 sub dibusb {
224         my $url = "http://linuxtv.org/cgi-bin/cvsweb.cgi/dvb-kernel/firmware/dvb-dibusb-5.0.0.11.fw?rev=1.1&content-type=text/plain";
225         my $outfile = "dvb-dibusb-5.0.0.11.fw";
226         my $hash = "fa490295a527360ca16dcdf3224ca243";
227
228         checkstandard();
229
230         wgetfile($outfile, $url);
231         verify($outfile,$hash);
232
233         $outfile;
234 }
235
236 # ---------------------------------------------------------------
237 # Utilities
238
239 sub checkstandard {
240     if (system("which unzip > /dev/null 2>&1")) {
241         die "This firmware requires the unzip command - see ftp://ftp.info-zip.org/pub/infozip/UnZip.html\n";
242     }
243     if (system("which md5sum > /dev/null 2>&1")) {
244         die "This firmware requires the md5sum command - see http://www.gnu.org/software/coreutils/\n";
245     }
246     if (system("which wget > /dev/null 2>&1")) {
247         die "This firmware requires the wget command - see http://wget.sunsite.dk/\n";
248     }
249 }
250
251 sub checkunshield {
252     if (system("which unshield > /dev/null 2>&1")) {
253         die "This firmware requires the unshield command - see http://sourceforge.net/projects/synce/\n";
254     }
255 }
256
257 sub wgetfile {
258     my ($sourcefile, $url) = @_;
259
260     if (! -f $sourcefile) {
261         system("wget -O \"$sourcefile\" \"$url\"") and die "wget failed - unable to download firmware";
262     }
263 }
264
265 sub unzip {
266     my ($sourcefile, $todir) = @_;
267
268     $status = system("unzip -q -o -d \"$todir\" \"$sourcefile\" 2>/dev/null" );
269     if ((($status >> 8) > 2) || (($status & 0xff) != 0)) {
270         die ("unzip failed - unable to extract firmware");
271     }
272 }
273
274 sub unshield {
275     my ($sourcefile, $todir) = @_;
276
277     system("unshield -d \"$todir\" \"$sourcefile\" > /dev/null" ) and die ("unshield failed - unable to extract firmware");
278 }
279
280 sub verify {
281     my ($filename, $hash) = @_;
282     my ($testhash);
283
284     open(CMD, "md5sum \"$filename\"|");
285     $testhash = <CMD>;
286     $testhash =~ /([a-zA-Z0-9]*)/;
287     $testhash = $1;
288     close CMD;
289     die "Hash of extracted file does not match!\n" if ($testhash ne $hash);
290 }
291
292 sub copy {
293     my ($from, $to) = @_;
294
295     system("cp -f \"$from\" \"$to\"") and die ("cp failed");
296 }
297
298 sub extract {
299     my ($infile, $offset, $length, $outfile) = @_;
300     my ($chunklength, $buf, $rcount);
301
302     open INFILE, "<$infile";
303     open OUTFILE, ">$outfile";
304     sysseek(INFILE, $offset, SEEK_SET);
305     while($length > 0) {
306         # Calc chunk size
307         $chunklength = 2048;
308         $chunklength = $length if ($chunklength > $length);
309
310         $rcount = sysread(INFILE, $buf, $chunklength);
311         die "Ran out of data\n" if ($rcount != $chunklength);
312         syswrite(OUTFILE, $buf);
313         $length -= $rcount;
314     }
315     close INFILE;
316     close OUTFILE;
317 }
318
319 sub appendfile {
320     my ($FH, $infile) = @_;
321     my ($buf);
322
323     open INFILE, "<$infile";
324     while(1) {
325         $rcount = sysread(INFILE, $buf, 2048);
326         last if ($rcount == 0);
327         print $FH $buf;
328     }
329     close(INFILE);
330 }
331
332 sub syntax() {
333     print STDERR "syntax: get_dvb_firmware <component>\n";
334     print STDERR "Supported components:\n";
335     for($i=0; $i < scalar(@components); $i++) {
336         print STDERR "\t" . $components[$i] . "\n";
337     }
338     exit(1);
339 }