-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yonatan Schachter <yonatan.schachter@gmail.com>
- Loading branch information
Showing
10 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import sys | ||
import hashlib | ||
from elftools.elf.elffile import ELFFile | ||
from elftools.elf.constants import SH_FLAGS | ||
|
||
|
||
BINDESC_OFFSETOF_DATA = 4 | ||
HASH_ALGORITHMS = { | ||
"md5": hashlib.md5, | ||
} | ||
|
||
|
||
def set_checksums(filename): | ||
with open(filename, 'rb+') as file_stream: | ||
elffile = ELFFile(file_stream) | ||
|
||
# Variable for storing the raw bytes of the image | ||
image_data = b"" | ||
|
||
# Iterate over the segments to produce the image to calculate the | ||
# checksums over. | ||
for segment in elffile.iter_segments(): | ||
if segment.header.p_type != "PT_LOAD": | ||
continue | ||
|
||
if segment.header.p_filesz == 0: | ||
continue | ||
|
||
for section in elffile.iter_sections(): | ||
if segment.section_in_segment(section): | ||
if not (section.name == "rom_start" and segment.section_in_segment(section)): | ||
# Don't include rom_start in the checksum calculation | ||
image_data += section.data() | ||
|
||
symbol_table = elffile.get_section_by_name(".symtab") | ||
|
||
# The binary descriptors to modify are found in the rom_start section | ||
rom_start = elffile.get_section_by_name("rom_start") | ||
|
||
for symbol in symbol_table.iter_symbols(): | ||
# Iterate over all the symbols, modify the checksums | ||
if symbol.name.startswith("bindesc_entry_checksum"): | ||
# Determine the alorithm and data type | ||
_, _, _, algorithm, data_type = symbol.name.split("_") | ||
|
||
# Calculate the checksum | ||
checksum = HASH_ALGORITHMS[algorithm](image_data) | ||
if data_type == "bytes": | ||
to_write = checksum.digest() | ||
else: | ||
to_write = checksum.hexdigest().encode() | ||
|
||
# Calculate the offset of the ELF file to write the checksum to | ||
# The formula is: offset of rom_start + address of the symbol minus | ||
# the base address of rom_start + offset of the data inside the descriptor | ||
# entry. | ||
offset = rom_start.header.sh_offset + \ | ||
symbol.entry.st_value - rom_start.header.sh_addr + \ | ||
BINDESC_OFFSETOF_DATA | ||
|
||
# Write the hash to the file | ||
elffile.stream.seek(offset) | ||
elffile.stream.write(to_write) | ||
|
||
|
||
def main(): | ||
set_checksums(sys.argv[1]) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright (c) 2023 Yonatan Schachter | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
menuconfig BINDESC_DEFINE_CHECKSUMS | ||
bool "Checksum binary descriptors" | ||
help | ||
Add checksum binary descriptors | ||
|
||
if BINDESC_DEFINE_CHECKSUMS | ||
|
||
config BINDESC_CHECKSUM_MD5_BYTES | ||
bool "MD5 checksum as bytes" | ||
help | ||
MD5 checksum as bytes | ||
|
||
config BINDESC_CHECKSUM_MD5_STRING | ||
bool "MD5 checksum as a string" | ||
help | ||
MD5 checksum as a string | ||
|
||
endif # BINDESC_DEFINE_CHECKSUMS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright (c) 2023 Yonatan Schachter | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/kernel.h> | ||
#include <zephyr/bindesc.h> | ||
|
||
#if IS_ENABLED(CONFIG_BINDESC_CHECKSUM_MD5_BYTES) | ||
BINDESC_BYTES_DEFINE(checksum_md5_bytes, BINDESC_ID_CHECKSUM_MD5_BYTES, | ||
({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})); | ||
#endif /* IS_ENABLED(CONFIG_BINDESC_CHECKSUM_MD5_BYTES) */ | ||
|
||
#if IS_ENABLED(CONFIG_BINDESC_CHECKSUM_MD5_STRING) | ||
BINDESC_STR_DEFINE(checksum_md5_string, BINDESC_ID_CHECKSUM_MD5_STRING, | ||
"00000000000000000000000000000000"); | ||
#endif /* IS_ENABLED(CONFIG_BINDESC_CHECKSUM_MD5_STRING) */ |