source: xtideuniversalbios/tags/v2.0.0_beta_3/Tools/checksum.pl @ 515

Last change on this file since 515 was 376, checked in by gregli@…, 12 years ago

WIDE checkin... Added copyright and license information to sorce files, as per the GPL instructions for usage.

File size: 2.0 KB
Line 
1@rem = '--*-Perl-*--
2@echo off
3perl -x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
4goto endofperl
5@rem ';
6#!perl
7#
8# Add checksum byte to ROM image
9#
10# Use a size of 0 to skip this script entirely (file is not modified)
11#
12# On Windows, this file can be renamed to a batch file and invoked directly (for example, "c:\>checksum file size")
13#
14
15#
16# XTIDE Universal BIOS and Associated Tools
17# Copyright (C) 2009-2010 by Tomi Tilli, 2011-2012 by XTIDE Universal BIOS Team.
18#
19# This program is free software; you can redistribute it and/or modify
20# it under the terms of the GNU General Public License as published by
21# the Free Software Foundation; either version 2 of the License, or
22# (at your option) any later version.
23#
24# This program is distributed in the hope that it will be useful,
25# but WITHOUT ANY WARRANTY; without even the implied warranty of
26# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27# GNU General Public License for more details.     
28# Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
29#
30
31($ARGV[0] ne "" && $ARGV[1] ne "") || die "usage: checksum filename size\n";
32
33$desiredSize = int($ARGV[1]);
34
35if( $desiredSize == 0 )
36{
37    exit( 0 );
38}
39
40open FILE, "+<".$ARGV[0] || die "file not found\n";
41binmode FILE;
42$cs = 0;
43$last = 0;
44$bytes = 0;
45while( ($n = read( FILE, $d, 1 )) != 0 )
46{
47    $cs = $cs + ord($d);
48    $cs = $cs % 256;
49    $bytes = $bytes + 1;
50}
51$oldBytes = $bytes;
52
53if( $bytes > $desiredSize - 1 )
54{
55    die "ERROR: image is bigger than ".($desiredSize-1).": $bytes\n";
56}
57
58$fixzero = chr(0); 
59while( $bytes < $desiredSize - 1 )
60{
61    print FILE $fixzero;
62    $bytes++;
63}
64
65$fixl = ($cs == 0 ? 0 : 256 - $cs);
66$fix = chr($fixl);
67print FILE $fix;
68
69close FILE;
70
71open FILE, "<".$ARGV[0];
72binmode FILE;
73$cs = 0;
74$newBytes = 0;
75while( ($n = read( FILE, $d, 1 )) != 0 )
76{
77    $cs = $cs + ord($d);
78    $cs = $cs % 256;
79    $newBytes++;
80}
81$cs == 0 || die "Checksum verification failed\n";
82
83print "checksum: ".$ARGV[0].": $oldBytes bytes before, $newBytes bytes after\n";
84
85__DATA__
86:endofperl
Note: See TracBrowser for help on using the repository browser.