#!/usr/local/bin/perl -w # Warn about a possibly invalid hex file (does not check checksums) # # Usage: chkhex < file.hex $linecount = 0; $nextaddr = 0; $eof = 0; while (<>) { ++$linecount; chomp; $bytes = ((length) - 1) / 2; # Check line length if ((length) < 11) { print "Line $linecount is too short to be a valid hex record\n"; next; } # Check for : at beginning of line if (substr($_, 0, 1) ne ":") { print "Line $linecount: line does not begin with ':'\n"; next; } # Check record type $record = hex(substr($_, 7, 2)); if (($record != 0) and ($record != 1)) { printf "Line $linecount: Record type is %2.2X, not 00 or 01.\n", $record; } # EOF record is a special case if ($record == 1) { if (lc($_) ne ":00000001ff") { print ("Line $linecount: invalid EOF record.\n"); next; } else { $eof = 1; last; } } # Check length $length = hex(substr($_, 1, 2)); if ($length != $bytes - 5) { printf "Line $linecount: Length field is %2.2X but should be %2.2X\n", $length, ($bytes-5); } # Check address $addr = hex(substr($_, 3, 4)); if ($addr != $nextaddr) { printf "Line $linecount: Address field is %4.4X but expected address was %4.4X\n", $addr, $nextaddr; } # Calculate next address $nextaddr = $addr + $length; } if ($eof != 1) { print "No EOF record in hex file.\n"; } print "Check done.\n";