hexChecksum.pl 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/perl -w
  2. # Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
  3. #
  4. # This file is part of SCSI2SD.
  5. #
  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.
  10. #
  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.
  15. #
  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/>.
  18. # Calculates the checksum of all flash data within Cypress PSoC 5lp .hex files
  19. # Allows fixing the checksum after manual hacking of the hex files
  20. use strict;
  21. use warnings;
  22. my $sum = 0;
  23. while (my $line = <>)
  24. {
  25. $line =~ s/[\n\r]//g;
  26. if ($line =~ /^:40[0-9A-F]{4}00(.+)[0-9A-F]{2}$/)
  27. {
  28. my $binrec = pack('H*', $1);
  29. $sum += unpack('%16C*', $binrec);
  30. }
  31. elsif ($line eq ":0200000490303A")
  32. {
  33. my $checksumRec = sprintf(":02000000%04X", ($sum & 0xffff));
  34. # create checksum of checksum record.
  35. my $sum2 = unpack('%8C*', pack('H*', substr($checksumRec, 1)));
  36. $checksumRec .= sprintf('%2X', (~$sum2 + 1) & 0xFF);
  37. print("Flash data checksum record = $checksumRec\n");
  38. print("(Replace line below ':0200000490303A'\n");
  39. exit;
  40. }
  41. }