#!/usr/bin/perl use perlfunc; sub firmware_list_gen { @fw_images = (); chomp($fw_version = `cat /etc/mesh-release`); foreach(`cat /tmp/web/software.list 2>/dev/null`) { my($md5, $fw, $tag) = /^(\S+) (\S+) (.*)/; next unless $tag; next if $tag eq "none"; next unless ($tag eq "all") or ($tag eq "dev" and $parms{dev}) or ($fw_version =~ /$tag/); push @fw_images, $fw; $fw_md5{$fw} = $md5; } } $debug = 0; $| = 1; read_postdata(); read_query_string(); $node = nvram_get("node"); $tmpdir = "/tmp/web/admin"; system "mkdir -p $tmpdir"; # make developer mode stick system "touch /tmp/developer_mode" if $parms{dev}; $parms{dev} = 1 if -e "/tmp/developer_mode"; # set the wget command options $wget = "wget -U 'node: $node'"; # # handle firmware updates # $fw_install = 0; $patch_install = 0; @fw_output = (); @fw_images = (); %fw_md5 = (); @serverpaths = ( "http://broadband-hamnet.org/download/wrt54g", "http://ad5oo.ham-radio-op.net/download/wrt54g" ); # refresh fw if($parms{button_refresh_fw}) { if(get_default_gw() ne "none") { push @fw_output, "Downloading firmware list...\n"; unlink "/tmp/web/software.list"; $ok = 0; foreach $serverpath (@serverpaths) { system "$wget -O /tmp/web/software.list $serverpath/software.list >/dev/null 2>>$tmpdir/wget.err"; unless($?) { $ok = 1; last } } if($ok) { push @fw_output, "Done.\n" } else { push @fw_output, `cat $tmpdir/wget.err` } unlink "$tmpdir/wget.err"; } else { push @fw_output, "Error: no route to the Internet\n"; unlink "/tmp/web/software.list"; } } # generate data structures # and set $fw_version firmware_list_gen(); # upload fw if($parms{button_ul_fw} and -f "/tmp/web/upload/file") { system "mv -f /tmp/web/upload/file $tmpdir/firmware"; if($parms{firmfile} =~ /\.trx$/) # full firmware { $fw_install = 1; # check firmware header my $header = `hexdump -C -n 8 $tmpdir/firmware`; my ($b0, $b1, $b2, $b3) = $header =~ /^00000000 48 44 52 30 (\w+) (\w+) (\w+) (\w+)/; if(not defined $b0) { push @fw_output, "Firmware CANNOT be updated\n"; push @fw_output, "firmware file is not valid\n"; $fw_install = 0; } else { my $size = hex($b0) + (hex($b1) << 8) + (hex($b2) << 16) + (hex($b3) << 24); # I would use 'stat' here but it seems not to work in microperl my @parts = split /\s+/, `ls -l $tmpdir/firmware`; if($size ne $parts[4]) { push @fw_output, "Firmware CANNOT be updated\n"; push @fw_output, "firmware file size is not valid\n"; $fw_install = 0; } } } elsif($parms{firmfile} =~ /^patch\S+\.tgz$/) # firmware patch { $patch_install = 1; } else { push @fw_output, "Firmware CANNOT be updated\n"; push @fw_output, "the uploaded file is not recognized\n"; } } # download fw if($parms{button_dl_fw} and $parms{dl_fw} ne "default") { if(get_default_gw() ne "none") { unlink "$tmpdir/firmware"; $ok = 0; foreach $serverpath (@serverpaths) { system "$wget -O $tmpdir/firmware $serverpath/$parms{dl_fw} >/dev/null 2>>$tmpdir/wget.err"; unless($?) { $ok = 1; last } } if($parms{dl_fw} =~ /\.trx$/) # full firmware { $fw_install = 1; unless($ok) { push @fw_output, "Downloading firmware image...\n"; push @fw_output, `cat $tmpdir/wget.err`; } unlink "$tmpdir/wget.err"; # check md5sum $fw = $parms{dl_fw}; chdir $tmpdir; if(system "echo '$fw_md5{$fw} firmware' | md5sum -cs") { push @fw_output, "Firmware CANNOT be updated\n"; push @fw_output, "firmware file is not valid\n"; $fw_install = 0; } } elsif($parms{dl_fw} =~ /^patch\S+\.tgz$/) # firmware patch { $patch_install = 1; unless($ok) { push @fw_output, "Downloading patch file...\n"; push @fw_output, `cat $tmpdir/wget.err`; } unlink "$tmpdir/wget.err"; # check md5sum $fw = $parms{dl_fw}; chdir $tmpdir; if(system "echo '$fw_md5{$fw} firmware' | md5sum -cs") { push @fw_output, "Firmware CANNOT be updated\n"; push @fw_output, "patch file is not valid\n"; $patch_install = 0; } } else { push @fw_output, "Firmware CANNOT be updated\n"; push @fw_output, "the downloaded file is not recognized\n"; } } else { push @fw_output, "Error: no route to the Internet\n"; unlink "/tmp/web/software.list"; } } # install fw if($fw_install and -f "$tmpdir/firmware") { my $junk; http_header(); html_header("FIRMWARE UPDATE IN PROGRESS", 1); print "