LPC Checksum

3.0.0 · maintenance · verified Sun Apr 12

A Python script designed to calculate LPC firmware checksums, based on the C version by Roel Verdult. It functions both as a standalone application and as a Python module that can be integrated into build environments. The current version is 3.0.0, but its release cadence is stalled, with the last release approximately three years ago.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import `lpc_checksum` and use its `checksum` function to calculate and optionally inject the correct checksum into a dummy binary firmware file. The `checksum` function modifies the input file in place by default.

import lpc_checksum
import os

# Create a dummy binary file for demonstration
dummy_firmware_path = 'firmware.bin'
with open(dummy_firmware_path, 'wb') as f:
    f.write(b'\x00\x00\x00\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')

# Calculate and inject the checksum
try:
    # The checksum function modifies the file in place by default
    # or returns the checksum if read_only=True
    print(f"Calculating checksum for {dummy_firmware_path}")
    checksum_value = lpc_checksum.checksum(dummy_firmware_path, read_only=True)
    print(f"Calculated checksum (read-only): 0x{checksum_value:08x}")

    # To actually write the checksum to the file:
    lpc_checksum.checksum(dummy_firmware_path)
    print(f"Checksum injected into {dummy_firmware_path}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up the dummy file
    if os.path.exists(dummy_firmware_path):
        os.remove(dummy_firmware_path)
        print(f"Cleaned up {dummy_firmware_path}")

view raw JSON →