Add Checksums to Firmware & External App images (#1809)

* Pad image to 1MB and add simple checksum

* Test code to verify firmware checksum

* Comment out unneeded zlib

* Add files via upload

* Print space remaining in ROM

* Append checksum to external apps too

* Check external app checksums when loading

* Is it 2024 already?!

* Validate firmware checksum before flashing

* Add files via upload

* Added flash error warning to nav screen

* Clang

* Replaced some hard-coded values with #defines

* Check FW checksum before USB serial flash too

* Add files via upload
This commit is contained in:
Mark Thompson 2024-01-24 16:37:21 -06:00 committed by GitHub
parent 2d98c5d311
commit 6a6c6d6502
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 179 additions and 13 deletions

View file

@ -104,6 +104,14 @@ for external_image_prefix in sys.argv[4:]:
external_application_image = patch_image(external_application_image, search_address, replace_address)
external_application_image[memory_location_header_position:memory_location_header_position+4] = replace_address.to_bytes(4, byteorder='little')
checksum = 0
for i in range(0, len(external_application_image), 4):
checksum += external_application_image[i] + (external_application_image[i + 1] << 8) + (external_application_image[i + 2] << 16) + (external_application_image[i + 3] << 24)
final_checksum = 0
checksum = (final_checksum - checksum) & 0xFFFFFFFF
external_application_image += checksum.to_bytes(4, 'little')
write_image(external_application_image, "{}/{}.ppma".format(binary_dir, external_image_prefix))
continue
@ -127,5 +135,13 @@ for external_image_prefix in sys.argv[4:]:
print("application {} can not exceed 32kb: {} bytes used".format(external_image_prefix, len(external_application_image)))
sys.exit(-1)
checksum = 0
for i in range(0, len(external_application_image), 4):
checksum += external_application_image[i] + (external_application_image[i + 1] << 8) + (external_application_image[i + 2] << 16) + (external_application_image[i + 3] << 24)
final_checksum = 0
checksum = (final_checksum - checksum) & 0xFFFFFFFF
external_application_image += checksum.to_bytes(4, 'little')
# write .ppma (portapack mayhem application)
write_image(external_application_image, "{}/{}.ppma".format(binary_dir, external_image_prefix))

View file

@ -74,7 +74,24 @@ for image in images:
padded_data = image['data'] + (spi_image_default_byte * pad_size)
spi_image += padded_data
if len(spi_image) > spi_size:
raise RuntimeError('SPI flash image size of %d exceeds device size of %d bytes' % (len(spi_image), spi_size))
if len(spi_image) > spi_size - 4:
raise RuntimeError('SPI flash image size of %d exceeds device size of %d bytes' % (len(spi_image) + 4, spi_size))
pad_size = spi_size - 4 - len(spi_image)
for i in range(pad_size):
spi_image += spi_image_default_byte
# quick "add up the words" checksum:
checksum = 0
for i in range(0, len(spi_image), 4):
checksum += (spi_image[i] + (spi_image[i + 1] << 8) + (spi_image[i + 2] << 16) + (spi_image[i + 3] << 24))
final_checksum = 0
checksum = (final_checksum - checksum) & 0xFFFFFFFF
spi_image += checksum.to_bytes(4, 'little')
write_image(spi_image, output_path)
percent_remaining = round(1000 * pad_size / spi_size) / 10;
print ("Space remaining in flash ROM:", pad_size, "bytes (", percent_remaining, "%)")

View file

@ -0,0 +1,57 @@
#!/usr/bin/env python3
#
# Copyright (C) 2024 Mark Thompson
#
# This file is part of PortaPack.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
import sys
import struct
usage_message = """
PortaPack ROM image checksum checker
Usage: <command> <input-file>
"""
def read_image(path):
f = open(path, 'rb')
data = f.read()
f.close()
return data
def write_image(data, path):
f = open(path, 'wb')
f.write(data)
f.close()
if len(sys.argv) != 2:
print(usage_message)
sys.exit(-1)
image = read_image(sys.argv[1])
image = bytearray(image)
# simple "add up the words" checksum:
checksum = 0
for i in range(0, len(image), 4):
checksum += (image[i] + (image[i + 1] << 8) + (image[i + 2] << 16) + (image[i + 3] << 24))
checksum &= 0xFFFFFFFF
print ("Simple checksum =", checksum)