-
-
Notifications
You must be signed in to change notification settings - Fork 234
Add runtime warning if the MCU firmware is out of date, based on a hash of the firmware sources #768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add runtime warning if the MCU firmware is out of date, based on a hash of the firmware sources #768
Changes from all commits
b558bdf
c3b0776
6e07535
ea22521
fe4d57d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ out | |
| __pycache__/ | ||
| .config | ||
| .config.old | ||
| .sources | ||
| klippy/.version | ||
| .history/ | ||
| .DS_Store | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,11 @@ | |
| # | ||
| # This file may be distributed under the terms of the GNU GPLv3 license. | ||
| import fcntl | ||
| import hashlib | ||
| import json | ||
| import logging | ||
| import os | ||
| import pathlib | ||
| import pty | ||
| import signal | ||
| import subprocess | ||
|
|
@@ -252,3 +254,24 @@ def get_git_version(from_file=True): | |
| if from_file: | ||
| git_info["version"] = get_version_from_file(klippy_src) | ||
| return git_info | ||
|
|
||
|
|
||
| def get_firmware_hash(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider prefixing the hash with a leader string indicating the version of the hash being used, to allow for changes to either the algo, or the way that it is fed in the future. (See how crypt(5) works for an example of this) |
||
| root_dir = pathlib.Path(__file__).parent.parent | ||
| hash_cache = root_dir / ".sources" | ||
| source_files = sorted( | ||
| file | ||
| for path in (root_dir / "src", root_dir / "lib") | ||
| for file in path.glob("**/*") | ||
| if file.is_file() | ||
| ) | ||
| last_modified = max(file.stat().st_mtime for file in source_files) | ||
| if hash_cache.is_file() and hash_cache.stat().st_mtime >= last_modified: | ||
| return hash_cache.read_text() | ||
| hash = hashlib.blake2b(digest_size=16, usedforsecurity=False) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usedforsecurity is py3.9, which is not present in Debian |
||
| for file in source_files: | ||
| hash.update(b"\x00" + bytes(file.relative_to(root_dir)) + b"\x00") | ||
| hash.update(file.read_bytes()) | ||
| digest = hash.hexdigest() | ||
| hash_cache.write_text(digest) | ||
| return digest | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
msgproto will give back "unknown" if there is no source hash in the firmware (either pre this change, or non-klipper MCUs using the Anchor/Windlass implementation). This means that this will always alert for such MCUs.