Skip to content

Commit

Permalink
Merge pull request #924 from chenguokai/develop
Browse files Browse the repository at this point in the history
Added md5 checksum for binary file
  • Loading branch information
Nightwalker-87 authored Apr 13, 2020
2 parents 99466bf + 1047fdd commit 1d00424
Show file tree
Hide file tree
Showing 5 changed files with 482 additions and 1 deletion.
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
5 changes: 5 additions & 0 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,11 @@ want to disassemble the code at 0x20000000, use:\
(gdb) disassemble 0x20000001
```

Checksum of binary file
-----------------------

When flashing a file, the checksum of which is calculated, both in md5 and the sum algorithm used by ST's official tool. The detail of sum algorithm can be found in [https://www.st.com/resource/en/user_manual/cd00262073-stm32-stlink-utility-software-description-stmicroelectronics.pdf](https://www.st.com/resource/en/user_manual/cd00262073-stm32-stlink-utility-software-description-stmicroelectronics.pdf).

References
==========

Expand Down
38 changes: 37 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) {
/* 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("md5 checksum: ");
for(int i = 0; i < (int) sizeof(md5Hash); i++) {
printf("%x", md5Hash.bytes[i]);
}
printf(", ");
}

static void stlink_checksum(mapped_file_t *mp) {
/* 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("stlink checksum: 0x%08x\n", sum);
}

static void stlink_fwrite_finalize(stlink_t *sl, stm32_addr_t addr) {
unsigned int val;
/* set stack*/
Expand Down Expand Up @@ -1445,6 +1470,9 @@ int stlink_fwrite_sram(stlink_t * sl, const char* path, stm32_addr_t addr) {
fprintf(stderr, "map_file() == -1\n");
return -1;
}
printf("file %s ", path);
md5_calculate(&mf);
stlink_checksum(&mf);

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


printf("file %s ", path);
md5_calculate(&mf);
stlink_checksum(&mf);

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

printf("file %s ", path);
md5_calculate(&mf);
stlink_checksum(&mf);

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

0 comments on commit 1d00424

Please sign in to comment.