Merge branch 'fixes' of git://git.linaro.org/people/rmk/linux-arm
[linux-flexiantxendom0-3.2.10.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",
25                 "tda10046lifeview", "av7110", "dec2000t", "dec2540t",
26                 "dec3000s", "vp7041", "dibusb", "nxt2002", "nxt2004",
27                 "or51211", "or51132_qam", "or51132_vsb", "bluebird",
28                 "opera1", "cx231xx", "cx18", "cx23885", "pvrusb2", "mpc718",
29                 "af9015", "ngene", "az6027", "lme2510_lg", "lme2510c_s7395",
30                 "lme2510c_s7395_old", "drxk", "drxk_terratec_h5",
31                 "drxk_hauppauge_hvr930c", "tda10071", "it9135", "it9137");
32
33 # Check args
34 syntax() if (scalar(@ARGV) != 1);
35 $cid = $ARGV[0];
36
37 # Do it!
38 for ($i=0; $i < scalar(@components); $i++) {
39     if ($cid eq $components[$i]) {
40         $outfile = eval($cid);
41         die $@ if $@;
42         print STDERR <<EOF;
43 Firmware(s) $outfile extracted successfully.
44 Now copy it(them) to either /usr/lib/hotplug/firmware or /lib/firmware
45 (depending on configuration of firmware hotplug).
46 EOF
47         exit(0);
48     }
49 }
50
51 # If we get here, it wasn't found
52 print STDERR "Unknown component \"$cid\"\n";
53 syntax();
54
55
56
57
58 # ---------------------------------------------------------------
59 # Firmware-specific extraction subroutines
60
61 sub sp8870 {
62     my $sourcefile = "tt_Premium_217g.zip";
63     my $url = "http://www.softwarepatch.pl/9999ccd06a4813cb827dbb0005071c71/$sourcefile";
64     my $hash = "53970ec17a538945a6d8cb608a7b3899";
65     my $outfile = "dvb-fe-sp8870.fw";
66     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
67
68     checkstandard();
69
70     wgetfile($sourcefile, $url);
71     unzip($sourcefile, $tmpdir);
72     verify("$tmpdir/software/OEM/HE/App/boot/SC_MAIN.MC", $hash);
73     copy("$tmpdir/software/OEM/HE/App/boot/SC_MAIN.MC", $outfile);
74
75     $outfile;
76 }
77
78 sub sp887x {
79     my $sourcefile = "Dvbt1.3.57.6.zip";
80     my $url = "http://www.avermedia.com/software/$sourcefile";
81     my $cabfile = "DVBT Net  Ver1.3.57.6/disk1/data1.cab";
82     my $hash = "237938d53a7f834c05c42b894ca68ac3";
83     my $outfile = "dvb-fe-sp887x.fw";
84     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
85
86     checkstandard();
87     checkunshield();
88
89     wgetfile($sourcefile, $url);
90     unzip($sourcefile, $tmpdir);
91     unshield("$tmpdir/$cabfile", $tmpdir);
92     verify("$tmpdir/ZEnglish/sc_main.mc", $hash);
93     copy("$tmpdir/ZEnglish/sc_main.mc", $outfile);
94
95     $outfile;
96 }
97
98 sub tda10045 {
99     my $sourcefile = "tt_budget_217g.zip";
100     my $url = "http://www.technotrend.de/new/217g/$sourcefile";
101     my $hash = "2105fd5bf37842fbcdfa4bfd58f3594a";
102     my $outfile = "dvb-fe-tda10045.fw";
103     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
104
105     checkstandard();
106
107     wgetfile($sourcefile, $url);
108     unzip($sourcefile, $tmpdir);
109     extract("$tmpdir/software/OEM/PCI/App/ttlcdacc.dll", 0x37ef9, 30555, "$tmpdir/fwtmp");
110     verify("$tmpdir/fwtmp", $hash);
111     copy("$tmpdir/fwtmp", $outfile);
112
113     $outfile;
114 }
115
116 sub tda10046 {
117         my $sourcefile = "TT_PCI_2.19h_28_11_2006.zip";
118         my $url = "http://www.tt-download.com/download/updates/219/$sourcefile";
119         my $hash = "6a7e1e2f2644b162ff0502367553c72d";
120         my $outfile = "dvb-fe-tda10046.fw";
121         my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
122
123         checkstandard();
124
125         wgetfile($sourcefile, $url);
126         unzip($sourcefile, $tmpdir);
127         extract("$tmpdir/TT_PCI_2.19h_28_11_2006/software/OEM/PCI/App/ttlcdacc.dll", 0x65389, 24478, "$tmpdir/fwtmp");
128         verify("$tmpdir/fwtmp", $hash);
129         copy("$tmpdir/fwtmp", $outfile);
130
131         $outfile;
132 }
133
134 sub tda10046lifeview {
135     my $sourcefile = "7%5Cdrv_2.11.02.zip";
136     my $url = "http://www.lifeview.hk/dbimages/document/$sourcefile";
137     my $hash = "1ea24dee4eea8fe971686981f34fd2e0";
138     my $outfile = "dvb-fe-tda10046.fw";
139     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
140
141     checkstandard();
142
143     wgetfile($sourcefile, $url);
144     unzip($sourcefile, $tmpdir);
145     extract("$tmpdir/LVHybrid.sys", 0x8b088, 24602, "$tmpdir/fwtmp");
146     verify("$tmpdir/fwtmp", $hash);
147     copy("$tmpdir/fwtmp", $outfile);
148
149     $outfile;
150 }
151
152 sub av7110 {
153     my $sourcefile = "dvb-ttpci-01.fw-261d";
154     my $url = "http://www.linuxtv.org/downloads/firmware/$sourcefile";
155     my $hash = "603431b6259715a8e88f376a53b64e2f";
156     my $outfile = "dvb-ttpci-01.fw";
157
158     checkstandard();
159
160     wgetfile($sourcefile, $url);
161     verify($sourcefile, $hash);
162     copy($sourcefile, $outfile);
163
164     $outfile;
165 }
166
167 sub dec2000t {
168     my $sourcefile = "dec217g.exe";
169     my $url = "http://hauppauge.lightpath.net/de/$sourcefile";
170     my $hash = "bd86f458cee4a8f0a8ce2d20c66215a9";
171     my $outfile = "dvb-ttusb-dec-2000t.fw";
172     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
173
174     checkstandard();
175
176     wgetfile($sourcefile, $url);
177     unzip($sourcefile, $tmpdir);
178     verify("$tmpdir/software/OEM/STB/App/Boot/STB_PC_T.bin", $hash);
179     copy("$tmpdir/software/OEM/STB/App/Boot/STB_PC_T.bin", $outfile);
180
181     $outfile;
182 }
183
184 sub dec2540t {
185     my $sourcefile = "dec217g.exe";
186     my $url = "http://hauppauge.lightpath.net/de/$sourcefile";
187     my $hash = "53e58f4f5b5c2930beee74a7681fed92";
188     my $outfile = "dvb-ttusb-dec-2540t.fw";
189     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
190
191     checkstandard();
192
193     wgetfile($sourcefile, $url);
194     unzip($sourcefile, $tmpdir);
195     verify("$tmpdir/software/OEM/STB/App/Boot/STB_PC_X.bin", $hash);
196     copy("$tmpdir/software/OEM/STB/App/Boot/STB_PC_X.bin", $outfile);
197
198     $outfile;
199 }
200
201 sub dec3000s {
202     my $sourcefile = "dec217g.exe";
203     my $url = "http://hauppauge.lightpath.net/de/$sourcefile";
204     my $hash = "b013ececea83f4d6d8d2a29ac7c1b448";
205     my $outfile = "dvb-ttusb-dec-3000s.fw";
206     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
207
208     checkstandard();
209
210     wgetfile($sourcefile, $url);
211     unzip($sourcefile, $tmpdir);
212     verify("$tmpdir/software/OEM/STB/App/Boot/STB_PC_S.bin", $hash);
213     copy("$tmpdir/software/OEM/STB/App/Boot/STB_PC_S.bin", $outfile);
214
215     $outfile;
216 }
217 sub opera1{
218         my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 0);
219
220         checkstandard();
221         my $fwfile1="dvb-usb-opera1-fpga-01.fw";
222         my $fwfile2="dvb-usb-opera-01.fw";
223         extract("2830SCap2.sys", 0x62e8, 55024, "$tmpdir/opera1-fpga.fw");
224         extract("2830SLoad2.sys",0x3178,0x3685-0x3178,"$tmpdir/fw1part1");
225         extract("2830SLoad2.sys",0x0980,0x3150-0x0980,"$tmpdir/fw1part2");
226         delzero("$tmpdir/fw1part1","$tmpdir/fw1part1-1");
227         delzero("$tmpdir/fw1part2","$tmpdir/fw1part2-1");
228         verify("$tmpdir/fw1part1-1","5e0909858fdf0b5b09ad48b9fe622e70");
229         verify("$tmpdir/fw1part2-1","d6e146f321427e931df2c6fcadac37a1");
230         verify("$tmpdir/opera1-fpga.fw","0f8133f5e9051f5f3c1928f7e5a1b07d");
231
232         my $RES1="\x01\x92\x7f\x00\x01\x00";
233         my $RES0="\x01\x92\x7f\x00\x00\x00";
234         my $DAT1="\x01\x00\xe6\x00\x01\x00";
235         my $DAT0="\x01\x00\xe6\x00\x00\x00";
236         open FW,">$tmpdir/opera.fw";
237         print FW "$RES1";
238         print FW "$DAT1";
239         print FW "$RES1";
240         print FW "$DAT1";
241         appendfile(FW,"$tmpdir/fw1part1-1");
242         print FW "$RES0";
243         print FW "$DAT0";
244         print FW "$RES1";
245         print FW "$DAT1";
246         appendfile(FW,"$tmpdir/fw1part2-1");
247         print FW "$RES1";
248         print FW "$DAT1";
249         print FW "$RES0";
250         print FW "$DAT0";
251         copy ("$tmpdir/opera1-fpga.fw",$fwfile1);
252         copy ("$tmpdir/opera.fw",$fwfile2);
253
254         $fwfile1.",".$fwfile2;
255 }
256
257 sub vp7041 {
258     my $sourcefile = "2.422.zip";
259     my $url = "http://www.twinhan.com/files/driver/USB-Ter/$sourcefile";
260     my $hash = "e88c9372d1f66609a3e7b072c53fbcfe";
261     my $outfile = "dvb-vp7041-2.422.fw";
262     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
263
264     checkstandard();
265
266     wgetfile($sourcefile, $url);
267     unzip($sourcefile, $tmpdir);
268     extract("$tmpdir/VisionDTV/Drivers/Win2K&XP/UDTTload.sys", 12503, 3036, "$tmpdir/fwtmp1");
269     extract("$tmpdir/VisionDTV/Drivers/Win2K&XP/UDTTload.sys", 2207, 10274, "$tmpdir/fwtmp2");
270
271     my $CMD = "\000\001\000\222\177\000";
272     my $PAD = "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000";
273     my ($FW);
274     open $FW, ">$tmpdir/fwtmp3";
275     print $FW "$CMD\001$PAD";
276     print $FW "$CMD\001$PAD";
277     appendfile($FW, "$tmpdir/fwtmp1");
278     print $FW "$CMD\000$PAD";
279     print $FW "$CMD\001$PAD";
280     appendfile($FW, "$tmpdir/fwtmp2");
281     print $FW "$CMD\001$PAD";
282     print $FW "$CMD\000$PAD";
283     close($FW);
284
285     verify("$tmpdir/fwtmp3", $hash);
286     copy("$tmpdir/fwtmp3", $outfile);
287
288     $outfile;
289 }
290
291 sub dibusb {
292         my $url = "http://www.linuxtv.org/downloads/firmware/dvb-usb-dibusb-5.0.0.11.fw";
293         my $outfile = "dvb-dibusb-5.0.0.11.fw";
294         my $hash = "fa490295a527360ca16dcdf3224ca243";
295
296         checkstandard();
297
298         wgetfile($outfile, $url);
299         verify($outfile,$hash);
300
301         $outfile;
302 }
303
304 sub nxt2002 {
305     my $sourcefile = "Technisat_DVB-PC_4_4_COMPACT.zip";
306     my $url = "http://www.bbti.us/download/windows/$sourcefile";
307     my $hash = "476befae8c7c1bb9648954060b1eec1f";
308     my $outfile = "dvb-fe-nxt2002.fw";
309     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
310
311     checkstandard();
312
313     wgetfile($sourcefile, $url);
314     unzip($sourcefile, $tmpdir);
315     verify("$tmpdir/SkyNET.sys", $hash);
316     extract("$tmpdir/SkyNET.sys", 331624, 5908, $outfile);
317
318     $outfile;
319 }
320
321 sub nxt2004 {
322     my $sourcefile = "AVerTVHD_MCE_A180_Drv_v1.2.2.16.zip";
323     my $url = "http://www.avermedia-usa.com/support/Drivers/$sourcefile";
324     my $hash = "111cb885b1e009188346d72acfed024c";
325     my $outfile = "dvb-fe-nxt2004.fw";
326     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
327
328     checkstandard();
329
330     wgetfile($sourcefile, $url);
331     unzip($sourcefile, $tmpdir);
332     verify("$tmpdir/3xHybrid.sys", $hash);
333     extract("$tmpdir/3xHybrid.sys", 465304, 9584, $outfile);
334
335     $outfile;
336 }
337
338 sub or51211 {
339     my $fwfile = "dvb-fe-or51211.fw";
340     my $url = "http://linuxtv.org/downloads/firmware/$fwfile";
341     my $hash = "d830949c771a289505bf9eafc225d491";
342
343     checkstandard();
344
345     wgetfile($fwfile, $url);
346     verify($fwfile, $hash);
347
348     $fwfile;
349 }
350
351 sub cx231xx {
352     my $fwfile = "v4l-cx231xx-avcore-01.fw";
353     my $url = "http://linuxtv.org/downloads/firmware/$fwfile";
354     my $hash = "7d3bb956dc9df0eafded2b56ba57cc42";
355
356     checkstandard();
357
358     wgetfile($fwfile, $url);
359     verify($fwfile, $hash);
360
361     $fwfile;
362 }
363
364 sub cx18 {
365     my $url = "http://linuxtv.org/downloads/firmware/";
366
367     my %files = (
368         'v4l-cx23418-apu.fw' => '588f081b562f5c653a3db1ad8f65939a',
369         'v4l-cx23418-cpu.fw' => 'b6c7ed64bc44b1a6e0840adaeac39d79',
370         'v4l-cx23418-dig.fw' => '95bc688d3e7599fd5800161e9971cc55',
371     );
372
373     checkstandard();
374
375     my $allfiles;
376     foreach my $fwfile (keys %files) {
377         wgetfile($fwfile, "$url/$fwfile");
378         verify($fwfile, $files{$fwfile});
379         $allfiles .= " $fwfile";
380     }
381
382     $allfiles =~ s/^\s//;
383
384     $allfiles;
385 }
386
387 sub mpc718 {
388     my $archive = 'Yuan MPC718 TV Tuner Card 2.13.10.1016.zip';
389     my $url = "ftp://ftp.work.acer-euro.com/desktop/aspire_idea510/vista/Drivers/$archive";
390     my $fwfile = "dvb-cx18-mpc718-mt352.fw";
391     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
392
393     checkstandard();
394     wgetfile($archive, $url);
395     unzip($archive, $tmpdir);
396
397     my $sourcefile = "$tmpdir/Yuan MPC718 TV Tuner Card 2.13.10.1016/mpc718_32bit/yuanrap.sys";
398     my $found = 0;
399
400     open IN, '<', $sourcefile or die "Couldn't open $sourcefile to extract $fwfile data\n";
401     binmode IN;
402     open OUT, '>', $fwfile;
403     binmode OUT;
404     {
405         # Block scope because we change the line terminator variable $/
406         my $prevlen = 0;
407         my $currlen;
408
409         # Buried in the data segment are 3 runs of almost identical
410         # register-value pairs that end in 0x5d 0x01 which is a "TUNER GO"
411         # command for the MT352.
412         # Pull out the middle run (because it's easy) of register-value
413         # pairs to make the "firmware" file.
414
415         local $/ = "\x5d\x01"; # MT352 "TUNER GO"
416
417         while (<IN>) {
418             $currlen = length($_);
419             if ($prevlen == $currlen && $currlen <= 64) {
420                 chop; chop; # Get rid of "TUNER GO"
421                 s/^\0\0//;  # get rid of leading 00 00 if it's there
422                 printf OUT "$_";
423                 $found = 1;
424                 last;
425             }
426             $prevlen = $currlen;
427         }
428     }
429     close OUT;
430     close IN;
431     if (!$found) {
432         unlink $fwfile;
433         die "Couldn't find valid register-value sequence in $sourcefile for $fwfile\n";
434     }
435     $fwfile;
436 }
437
438 sub cx23885 {
439     my $url = "http://linuxtv.org/downloads/firmware/";
440
441     my %files = (
442         'v4l-cx23885-avcore-01.fw' => 'a9f8f5d901a7fb42f552e1ee6384f3bb',
443         'v4l-cx23885-enc.fw'       => 'a9f8f5d901a7fb42f552e1ee6384f3bb',
444     );
445
446     checkstandard();
447
448     my $allfiles;
449     foreach my $fwfile (keys %files) {
450         wgetfile($fwfile, "$url/$fwfile");
451         verify($fwfile, $files{$fwfile});
452         $allfiles .= " $fwfile";
453     }
454
455     $allfiles =~ s/^\s//;
456
457     $allfiles;
458 }
459
460 sub pvrusb2 {
461     my $url = "http://linuxtv.org/downloads/firmware/";
462
463     my %files = (
464         'v4l-cx25840.fw'           => 'dadb79e9904fc8af96e8111d9cb59320',
465     );
466
467     checkstandard();
468
469     my $allfiles;
470     foreach my $fwfile (keys %files) {
471         wgetfile($fwfile, "$url/$fwfile");
472         verify($fwfile, $files{$fwfile});
473         $allfiles .= " $fwfile";
474     }
475
476     $allfiles =~ s/^\s//;
477
478     $allfiles;
479 }
480
481 sub or51132_qam {
482     my $fwfile = "dvb-fe-or51132-qam.fw";
483     my $url = "http://linuxtv.org/downloads/firmware/$fwfile";
484     my $hash = "7702e8938612de46ccadfe9b413cb3b5";
485
486     checkstandard();
487
488     wgetfile($fwfile, $url);
489     verify($fwfile, $hash);
490
491     $fwfile;
492 }
493
494 sub or51132_vsb {
495     my $fwfile = "dvb-fe-or51132-vsb.fw";
496     my $url = "http://linuxtv.org/downloads/firmware/$fwfile";
497     my $hash = "c16208e02f36fc439a557ad4c613364a";
498
499     checkstandard();
500
501     wgetfile($fwfile, $url);
502     verify($fwfile, $hash);
503
504     $fwfile;
505 }
506
507 sub bluebird {
508         my $url = "http://www.linuxtv.org/download/dvb/firmware/dvb-usb-bluebird-01.fw";
509         my $outfile = "dvb-usb-bluebird-01.fw";
510         my $hash = "658397cb9eba9101af9031302671f49d";
511
512         checkstandard();
513
514         wgetfile($outfile, $url);
515         verify($outfile,$hash);
516
517         $outfile;
518 }
519
520 sub af9015 {
521         my $sourcefile = "download.ashx?file=57";
522         my $url = "http://www.ite.com.tw/EN/Services/$sourcefile";
523         my $hash = "e3f08935158038d385ad382442f4bb2d";
524         my $outfile = "dvb-usb-af9015.fw";
525         my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
526         my $fwoffset = 0x25690;
527         my $fwlength = 18725;
528         my ($chunklength, $buf, $rcount);
529
530         checkstandard();
531
532         wgetfile($sourcefile, $url);
533         unzip($sourcefile, $tmpdir);
534         verify("$tmpdir/Driver/Files/AF15BDA.sys", $hash);
535
536         open INFILE, '<', "$tmpdir/Driver/Files/AF15BDA.sys";
537         open OUTFILE, '>', $outfile;
538
539         sysseek(INFILE, $fwoffset, SEEK_SET);
540         while($fwlength > 0) {
541                 $chunklength = 55;
542                 $chunklength = $fwlength if ($chunklength > $fwlength);
543                 $rcount = sysread(INFILE, $buf, $chunklength);
544                 die "Ran out of data\n" if ($rcount != $chunklength);
545                 syswrite(OUTFILE, $buf);
546                 sysread(INFILE, $buf, 8);
547                 $fwlength -= $rcount + 8;
548         }
549
550         close OUTFILE;
551         close INFILE;
552 }
553
554 sub ngene {
555     my $url = "http://www.digitaldevices.de/download/";
556     my $file1 = "ngene_15.fw";
557     my $hash1 = "d798d5a757121174f0dbc5f2833c0c85";
558     my $file2 = "ngene_17.fw";
559     my $hash2 = "26b687136e127b8ac24b81e0eeafc20b";
560     my $url2 = "http://l4m-daten.de/downloads/firmware/dvb-s2/linux/all/";
561     my $file3 = "ngene_18.fw";
562     my $hash3 = "ebce3ea769a53e3e0b0197c3b3f127e3";
563
564     checkstandard();
565
566     wgetfile($file1, $url . $file1);
567     verify($file1, $hash1);
568
569     wgetfile($file2, $url . $file2);
570     verify($file2, $hash2);
571
572     wgetfile($file3, $url2 . $file3);
573     verify($file3, $hash3);
574
575     "$file1, $file2, $file3";
576 }
577
578 sub az6027{
579     my $firmware = "dvb-usb-az6027-03.fw";
580     my $url = "http://linux.terratec.de/files/TERRATEC_S7/$firmware";
581
582     wgetfile($firmware, $url);
583
584     $firmware;
585 }
586
587 sub lme2510_lg {
588     my $sourcefile = "LMEBDA_DVBS.sys";
589     my $hash = "fc6017ad01e79890a97ec53bea157ed2";
590     my $outfile = "dvb-usb-lme2510-lg.fw";
591     my $hasho = "caa065d5fdbd2c09ad57b399bbf55cad";
592
593     checkstandard();
594
595     verify($sourcefile, $hash);
596     extract($sourcefile, 4168, 3841, $outfile);
597     verify($outfile, $hasho);
598     $outfile;
599 }
600
601 sub lme2510c_s7395 {
602     my $sourcefile = "US2A0D.sys";
603     my $hash = "b0155a8083fb822a3bd47bc360e74601";
604     my $outfile = "dvb-usb-lme2510c-s7395.fw";
605     my $hasho = "3a3cf1aeebd17b6ddc04cebe131e94cf";
606
607     checkstandard();
608
609     verify($sourcefile, $hash);
610     extract($sourcefile, 37248, 3720, $outfile);
611     verify($outfile, $hasho);
612     $outfile;
613 }
614
615 sub lme2510c_s7395_old {
616     my $sourcefile = "LMEBDA_DVBS7395C.sys";
617     my $hash = "7572ae0eb9cdf91baabd7c0ba9e09b31";
618     my $outfile = "dvb-usb-lme2510c-s7395.fw";
619     my $hasho = "90430c5b435eb5c6f88fd44a9d950674";
620
621     checkstandard();
622
623     verify($sourcefile, $hash);
624     extract($sourcefile, 4208, 3881, $outfile);
625     verify($outfile, $hasho);
626     $outfile;
627 }
628
629 sub drxk {
630     my $url = "http://l4m-daten.de/files/";
631     my $zipfile = "DDTuner.zip";
632     my $hash = "f5a37b9a20a3534997997c0b1382a3e5";
633     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
634     my $drvfile = "DDTuner.sys";
635     my $fwfile = "drxk_a3.mc";
636
637     checkstandard();
638
639     wgetfile($zipfile, $url . $zipfile);
640     verify($zipfile, $hash);
641     unzip($zipfile, $tmpdir);
642     extract("$tmpdir/$drvfile", 0x14dd8, 15634, "$fwfile");
643
644     "$fwfile"
645 }
646
647 sub drxk_hauppauge_hvr930c {
648     my $url = "http://www.wintvcd.co.uk/drivers/";
649     my $zipfile = "HVR-9x0_5_10_325_28153_SIGNED.zip";
650     my $hash = "83ab82e7e9480ec8bf1ae0155ca63c88";
651     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
652     my $drvfile = "HVR-900/emOEM.sys";
653     my $fwfile = "dvb-usb-hauppauge-hvr930c-drxk.fw";
654
655     checkstandard();
656
657     wgetfile($zipfile, $url . $zipfile);
658     verify($zipfile, $hash);
659     unzip($zipfile, $tmpdir);
660     extract("$tmpdir/$drvfile", 0x117b0, 42692, "$fwfile");
661
662     "$fwfile"
663 }
664
665 sub drxk_terratec_h5 {
666     my $url = "http://www.linuxtv.org/downloads/firmware/";
667     my $hash = "19000dada8e2741162ccc50cc91fa7f1";
668     my $fwfile = "dvb-usb-terratec-h5-drxk.fw";
669
670     checkstandard();
671
672     wgetfile($fwfile, $url . $fwfile);
673     verify($fwfile, $hash);
674
675     "$fwfile"
676 }
677
678 sub it9135 {
679         my $sourcefile = "dvb-usb-it9135.zip";
680         my $url = "http://www.ite.com.tw/uploads/firmware/v3.6.0.0/$sourcefile";
681         my $hash = "1e55f6c8833f1d0ae067c2bb2953e6a9";
682         my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 0);
683         my $outfile = "dvb-usb-it9135.fw";
684         my $fwfile1 = "dvb-usb-it9135-01.fw";
685         my $fwfile2 = "dvb-usb-it9135-02.fw";
686
687         checkstandard();
688
689         wgetfile($sourcefile, $url);
690         unzip($sourcefile, $tmpdir);
691         verify("$tmpdir/$outfile", $hash);
692         extract("$tmpdir/$outfile", 64, 8128, "$fwfile1");
693         extract("$tmpdir/$outfile", 12866, 5817, "$fwfile2");
694
695         "$fwfile1 $fwfile2"
696 }
697
698 sub it9137 {
699     my $url = "http://kworld.server261.com/kworld/CD/ITE_TiVme/V1.00/";
700     my $zipfile = "Driver_V10.323.1.0412.100412.zip";
701     my $hash = "79b597dc648698ed6820845c0c9d0d37";
702     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 0);
703     my $drvfile = "Driver_V10.323.1.0412.100412/Data/x86/IT9135BDA.sys";
704     my $fwfile = "dvb-usb-it9137-01.fw";
705
706     checkstandard();
707
708     wgetfile($zipfile, $url . $zipfile);
709     verify($zipfile, $hash);
710     unzip($zipfile, $tmpdir);
711     extract("$tmpdir/$drvfile", 69632, 5731, "$fwfile");
712
713     "$fwfile"
714 }
715
716 sub tda10071 {
717     my $sourcefile = "PCTV_460e_reference.zip";
718     my $url = "ftp://ftp.pctvsystems.com/TV/driver/PCTV%2070e%2080e%20100e%20320e%20330e%20800e/";
719     my $hash = "4403de903bf2593464c8d74bbc200a57";
720     my $fwfile = "dvb-fe-tda10071.fw";
721     my $tmpdir = tempdir(DIR => "/tmp", CLEANUP => 1);
722
723     checkstandard();
724
725     wgetfile($sourcefile, $url . $sourcefile);
726     verify($sourcefile, $hash);
727     unzip($sourcefile, $tmpdir);
728     extract("$tmpdir/PCTV\ 70e\ 80e\ 100e\ 320e\ 330e\ 800e/32\ bit/emOEM.sys", 0x67d38, 40504, $fwfile);
729
730     "$fwfile";
731 }
732
733 # ---------------------------------------------------------------
734 # Utilities
735
736 sub checkstandard {
737     if (system("which unzip > /dev/null 2>&1")) {
738         die "This firmware requires the unzip command - see ftp://ftp.info-zip.org/pub/infozip/UnZip.html\n";
739     }
740     if (system("which md5sum > /dev/null 2>&1")) {
741         die "This firmware requires the md5sum command - see http://www.gnu.org/software/coreutils/\n";
742     }
743     if (system("which wget > /dev/null 2>&1")) {
744         die "This firmware requires the wget command - see http://wget.sunsite.dk/\n";
745     }
746 }
747
748 sub checkunshield {
749     if (system("which unshield > /dev/null 2>&1")) {
750         die "This firmware requires the unshield command - see http://sourceforge.net/projects/synce/\n";
751     }
752 }
753
754 sub wgetfile {
755     my ($sourcefile, $url) = @_;
756
757     if (! -f $sourcefile) {
758         system("wget -O \"$sourcefile\" \"$url\"") and die "wget failed - unable to download firmware";
759     }
760 }
761
762 sub unzip {
763     my ($sourcefile, $todir) = @_;
764
765     $status = system("unzip -q -o -d \"$todir\" \"$sourcefile\" 2>/dev/null" );
766     if ((($status >> 8) > 2) || (($status & 0xff) != 0)) {
767         die ("unzip failed - unable to extract firmware");
768     }
769 }
770
771 sub unshield {
772     my ($sourcefile, $todir) = @_;
773
774     system("unshield x -d \"$todir\" \"$sourcefile\" > /dev/null" ) and die ("unshield failed - unable to extract firmware");
775 }
776
777 sub verify {
778     my ($filename, $hash) = @_;
779     my ($testhash);
780
781     open(CMD, "md5sum \"$filename\"|");
782     $testhash = <CMD>;
783     $testhash =~ /([a-zA-Z0-9]*)/;
784     $testhash = $1;
785     close CMD;
786     die "Hash of extracted file does not match!\n" if ($testhash ne $hash);
787 }
788
789 sub copy {
790     my ($from, $to) = @_;
791
792     system("cp -f \"$from\" \"$to\"") and die ("cp failed");
793 }
794
795 sub extract {
796     my ($infile, $offset, $length, $outfile) = @_;
797     my ($chunklength, $buf, $rcount);
798
799     open INFILE, "<$infile";
800     open OUTFILE, ">$outfile";
801     sysseek(INFILE, $offset, SEEK_SET);
802     while($length > 0) {
803         # Calc chunk size
804         $chunklength = 2048;
805         $chunklength = $length if ($chunklength > $length);
806
807         $rcount = sysread(INFILE, $buf, $chunklength);
808         die "Ran out of data\n" if ($rcount != $chunklength);
809         syswrite(OUTFILE, $buf);
810         $length -= $rcount;
811     }
812     close INFILE;
813     close OUTFILE;
814 }
815
816 sub appendfile {
817     my ($FH, $infile) = @_;
818     my ($buf);
819
820     open INFILE, "<$infile";
821     while(1) {
822         $rcount = sysread(INFILE, $buf, 2048);
823         last if ($rcount == 0);
824         print $FH $buf;
825     }
826     close(INFILE);
827 }
828
829 sub delzero{
830         my ($infile,$outfile) =@_;
831
832         open INFILE,"<$infile";
833         open OUTFILE,">$outfile";
834         while (1){
835                 $rcount=sysread(INFILE,$buf,22);
836                 $len=ord(substr($buf,0,1));
837                 print OUTFILE substr($buf,0,1);
838                 print OUTFILE substr($buf,2,$len+3);
839         last if ($rcount<1);
840         printf OUTFILE "%c",0;
841 #print $len." ".length($buf)."\n";
842
843         }
844         close(INFILE);
845         close(OUTFILE);
846 }
847
848 sub syntax() {
849     print STDERR "syntax: get_dvb_firmware <component>\n";
850     print STDERR "Supported components:\n";
851     @components = sort @components;
852     for($i=0; $i < scalar(@components); $i++) {
853         print STDERR "\t" . $components[$i] . "\n";
854     }
855     exit(1);
856 }