Coder Social home page Coder Social logo

lpc_checksum's Introduction

lpc_checksum

Python script to calculate LPC firmware checksums, based on the C version by Roel Verdult. It can be used as a standalone application, or as a Python module that integrates directly in a build environment (e.g. SCons). It does not need to be compiled.

Linting Testing PyPI version

Requirements

The only requirement is Python 3.9 or newer.

Installation

This module can be installed from Pypi via pip install lpc_checksum.

Alternatively, you can install the latest version by cloning this repository and run python setup.py install.

Usage

There are two ways of using lpc_checksum.

Standalone

When installed via Pip or from source, the command lpc_checksum should be available on your PATH. By default, it assumes the input file is a binary file.

lpc_checksum <firmware.bin|hex> [--format=bin] [--read-only]

Program exits with a non-zero error code when it failed.

As a module

import lpc_checksum

checksum = lpc_checksum.checksum(input_file, [read_only=True])

On error, an exception will be raised.

Tests

To run the tests, please clone this repository and run poetry run pytest.

Contributing

See the CONTRIBUTING.md file.

License

See the LICENSE.md file (MIT license).

lpc_checksum's People

Contributors

basilfx avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

lpc_checksum's Issues

Can't run lpc_checksum

sudo pip install lpc_checksum
lpc_checksum mybinary.bin
-> Error: ord() expected string of length 1, but int found

Running on Python 3.7

Tool computes incorrect checksum on internal 32-bit overflow

In your script you attempt to do a 32-bit unsigned add in python.

result = (result + value) % 0xFFFFFFFF

However, when an overflow occurrs, meaning the result is larger than 32 bits, the upper bits are discarded by the CPU and only the lower 32 bits are retained. Thus it's wrong to do modulo, there should be an AND. For reference, see https://stackoverflow.com/a/210747/5296568

Example: The original program and your script compute different values for this firmware.bin.

gcc -o lpcrc lpcrc.c && lpcrc.exe firmware.bin && python lpcrc.py firmware.bin
-- lpcrc.exe --
succesfully updated crc to: 02b6aa66
-- lpcrc.py --
Firmware path: firmware.bin
Wrote checksum 0x2b6aa63 into binary.

When patching it with the python script, openocd complains

** Programming Started **
Warn : Verification will fail since checksum in image (0x02b6aa63) to be written to flash is different from calculated vector checksum (0x02b6aa66).
Warn : To remove this warning modify build tools on developer PC to inject correct LPC vector checksum.
** Programming Finished **
** Verify Started **
Error: checksum mismatch - attempting binary compare
embedded:startup.tcl:449: Error: ** Verify Failed **

The script returns the same checksum as the C program after applying the patch above for & instead of %.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.