Skip to content
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

Add md5 checksum for binary file #924

Merged
merged 5 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ set(STLINK_SOURCE
src/sg.c
src/logging.c
src/flash_loader.c
src/md5.c
)

if (WIN32 OR MSYS OR MINGW)
Expand Down
36 changes: 35 additions & 1 deletion src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "stlink.h"
#include "stlink/mmap.h"
#include "stlink/logging.h"
#include "md5.h"

#ifndef O_BINARY
#define O_BINARY 0
Expand Down Expand Up @@ -1365,6 +1366,30 @@ static int check_file(stlink_t* sl, mapped_file_t* mf, stm32_addr_t addr) {
return 0;
}

static void md5_calculate(mapped_file_t *mf, const char *path) {
/* calculate md5 checksum of given binary file */
Md5Context md5Context;
MD5_HASH md5Hash;
Md5Initialise(&md5Context);
Md5Update(&md5Context, mf->base, (uint32_t)mf->len);
Md5Finalise(&md5Context, &md5Hash);
printf("file %s md5 checksum: ",path);
for(int i = 0; i < (int) sizeof(md5Hash); i++) {
printf("%x", md5Hash.bytes[i]);
}
printf("\n");
}

static void stlink_checksum(mapped_file_t *mp, const char* path) {
/* checksum that backward compatible with official ST tools */
uint32_t sum = 0;
uint8_t *mp_byte = (uint8_t *)mp->base;
for (size_t i = 0; i < mp->len; ++i) {
sum += mp_byte[i];
}
printf("file %s stlink checksum: 0x%08x\n", path, sum);
}

static void stlink_fwrite_finalize(stlink_t *sl, stm32_addr_t addr) {
unsigned int val;
/* set stack*/
Expand Down Expand Up @@ -1446,6 +1471,9 @@ int stlink_fwrite_sram(stlink_t * sl, const char* path, stm32_addr_t addr) {
return -1;
}

md5_calculate(&mf, path);
stlink_checksum(&mf, path);

/* check addr range is inside the sram */
if (addr < sl->sram_base) {
fprintf(stderr, "addr too low\n");
Expand Down Expand Up @@ -2669,7 +2697,10 @@ int stlink_fwrite_flash(stlink_t *sl, const char* path, stm32_addr_t addr) {
ELOG("map_file() == -1\n");
return -1;
}


md5_calculate(&mf, path);
stlink_checksum(&mf, path);

if (sl->opt) {
idx = (unsigned int) mf.len;
for(num_empty = 0; num_empty != mf.len; ++num_empty) {
Expand Down Expand Up @@ -3234,6 +3265,9 @@ int stlink_fwrite_option_bytes(stlink_t *sl, const char* path, stm32_addr_t addr
return -1;
}

md5_calculate(&mf, path);
stlink_checksum(&mf, path);

err = stlink_write_option_bytes(sl, addr, mf.base, (uint32_t) mf.len);
stlink_fwrite_finalize(sl, addr);
unmap_file(&mf);
Expand Down
Loading