2 # Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
4 # This file is part of SCSI2SD.
6 # SCSI2SD 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 3 of the License, or
9 # (at your option) any later version.
11 # SCSI2SD 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 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
19 # Calculates the checksum of all flash data within Cypress PSoC 5lp .hex files
20 # Allows fixing the checksum after manual hacking of the hex files
31 if ($line =~ /^:40[0-9A-F]{4}00(.+)[0-9A-F]{2}$/)
33 my $binrec = pack('H*', $1);
34 $sum += unpack('%16C*', $binrec);
36 elsif ($line eq ":0200000490303A")
38 my $checksumRec = sprintf(":02000000%04X", ($sum & 0xffff));
40 # create checksum of checksum record.
41 my $sum2 = unpack('%8C*', pack('H*', substr($checksumRec, 1)));
42 $checksumRec .= sprintf('%2X', (~$sum2 + 1) & 0xFF);
43 print("Flash data checksum record = $checksumRec\n");
44 print("(Replace line below ':0200000490303A'\n");