[refactoring] Clean-up for stlink-lib

- Ensure proper function declaration
- Moved some functions to related modules
- Checked & revised header includes
- Renamed "md5" to "lib_md5"
- New source file "md5"
pull/1315/head
nightwalker-87 2023-06-09 01:28:22 +02:00
rodzic 67ae7a12d8
commit be2e7e3883
30 zmienionych plików z 557 dodań i 528 usunięć

Wyświetl plik

@ -172,10 +172,10 @@ set(STLINK_HEADERS
src/stlink-lib/chipid.h
src/stlink-lib/commands.h
src/stlink-lib/common_flash.h
src/stlink-lib/common.h
src/stlink-lib/flash_loader.h
src/stlink-lib/helper.h
src/stlink-lib/libusb_settings.h
src/stlink-lib/lib_md5.h
src/stlink-lib/logging.h
src/stlink-lib/map_file.h
src/stlink-lib/md5.h
@ -194,6 +194,7 @@ set(STLINK_SOURCE
src/stlink-lib/helper.c
src/stlink-lib/logging.c
src/stlink-lib/map_file.c
src/stlink-lib/lib_md5.c
src/stlink-lib/md5.c
src/stlink-lib/option_bytes.c
src/stlink-lib/read_write.c

Wyświetl plik

@ -11,8 +11,8 @@
#include <stddef.h>
#include <stdbool.h>
#include "stm32.h"
#include "stm32flash.h"
#include <stm32.h>
#include <stm32flash.h>
#ifdef __cplusplus
extern "C" {
@ -263,24 +263,13 @@ int32_t stlink_set_swdclk(stlink_t *sl, int32_t freq_khz);
int32_t stlink_trace_enable(stlink_t* sl, uint32_t frequency);
int32_t stlink_trace_disable(stlink_t* sl);
int32_t stlink_trace_read(stlink_t* sl, uint8_t* buf, size_t size);
int32_t stlink_erase_flash_mass(stlink_t* sl);
int32_t stlink_erase_flash_section(stlink_t *sl, stm32_addr_t base_addr, size_t size, bool align_size);
int32_t stlink_write_flash(stlink_t* sl, stm32_addr_t address, uint8_t* data, uint32_t length, uint8_t eraseonly);
int32_t stlink_parse_ihex(const char* path, uint8_t erased_pattern, uint8_t * * mem, size_t * size, uint32_t * begin);
uint8_t stlink_get_erased_pattern(stlink_t *sl);
int32_t stlink_mwrite_flash(stlink_t *sl, uint8_t* data, uint32_t length, stm32_addr_t addr);
int32_t stlink_fwrite_flash(stlink_t *sl, const char* path, stm32_addr_t addr);
int32_t stlink_mwrite_sram(stlink_t *sl, uint8_t* data, uint32_t length, stm32_addr_t addr);
int32_t stlink_fwrite_sram(stlink_t *sl, const char* path, stm32_addr_t addr);
int32_t stlink_verify_write_flash(stlink_t *sl, stm32_addr_t address, uint8_t *data, uint32_t length);
//int32_t stlink_chip_id(stlink_t *sl, uint32_t *chip_id);
int32_t stlink_cpu_id(stlink_t *sl, cortex_m3_cpuid_t *cpuid);
int32_t stlink_erase_flash_page(stlink_t* sl, stm32_addr_t flashaddr);
uint32_t stlink_calculate_pagesize(stlink_t *sl, uint32_t flashaddr);
int32_t stlink_check_address_range_validity(stlink_t *sl, stm32_addr_t addr, size_t size);
int32_t stlink_check_address_alignment(stlink_t *sl, stm32_addr_t addr);
uint16_t read_uint16(const unsigned char *c, const int32_t pt);
//void stlink_core_stat(stlink_t *sl);
void stlink_print_data(stlink_t *sl);
@ -293,7 +282,6 @@ int32_t write_buffer_to_sram(stlink_t *sl, flash_loader_t* fl, const uint8_t* bu
int32_t write_loader_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t* size);
int32_t stlink_fread(stlink_t* sl, const char* path, bool is_ihex, stm32_addr_t addr, size_t size);
int32_t stlink_load_device_params(stlink_t *sl);
int32_t stlink_target_connect(stlink_t *sl, enum connect_type connect);
#include <sg.h>
@ -301,8 +289,8 @@ int32_t stlink_target_connect(stlink_t *sl, enum connect_type connect);
#include <register.h>
#include <commands.h>
#include <chipid.h>
#include <flash_loader.h>
#include <version.h>
#include <flash_loader.h>
#include <logging.h>
#ifdef __cplusplus

Wyświetl plik

@ -29,7 +29,8 @@
#include <stlink.h>
#include <helper.h>
#include <logging.h>
#include "flash_loader.h"
#include <flash_loader.h>
#include <common_flash.h>
#include "gdb-remote.h"
#include "gdb-server.h"
#include "semihosting.h"

Wyświetl plik

@ -5,6 +5,7 @@
#include <gtk/gtk.h>
#include <stlink.h>
#include <common_flash.h>
#include "gui.h"
#define MEM_READ_SIZE 1024

Wyświetl plik

@ -8,6 +8,7 @@
#include <stlink.h>
#include "calculate.h"
#include "common_flash.h"
uint32_t calculate_F4_sectornum(uint32_t flashaddr) {

Wyświetl plik

@ -4,16 +4,19 @@
* Chip-ID parametres
*/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "chipid.h"
#include <stlink.h>
#include <stm32.h>
#include <stlink.h>
#include "chipid.h"
#include "logging.h"
// #include <ctype.h> // TODO: Check use
// #include <errno.h> // TODO: Check use
static struct stlink_chipid_params *devicelist;

Wyświetl plik

@ -9,9 +9,6 @@
#include <stdint.h>
#include <stm32.h>
#include <stlink.h>
/* Chipid parametres */
struct stlink_chipid_params {
char *dev_type;

Wyświetl plik

@ -13,18 +13,20 @@
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <helper.h>
#include <sys/stat.h>
#include <sys/types.h>
// #include <sys/stat.h> // TODO: Check use
// #include <sys/types.h> // TODO: Check use
#include <stlink.h>
#include <stm32.h>
#include "calculate.h"
#include "chipid.h"
#include "common_flash.h"
#include "helper.h"
#include "logging.h"
#include "map_file.h"
#include "md5.h"
#include "common.h"
#include "register.h"
#include "usb.h"
#ifndef O_BINARY
#define O_BINARY 0
@ -1213,78 +1215,6 @@ void stlink_run_at(stlink_t *sl, stm32_addr_t addr) {
}
}
/* Limit the block size to compare to 0x1800 as anything larger will stall the
* STLINK2 Maybe STLINK V1 needs smaller value!
*/
int32_t check_file(stlink_t *sl, mapped_file_t *mf, stm32_addr_t addr) {
size_t off;
size_t n_cmp = sl->flash_pgsz;
if (n_cmp > 0x1800) {
n_cmp = 0x1800;
}
for (off = 0; off < mf->len; off += n_cmp) {
size_t aligned_size;
size_t cmp_size = n_cmp; // adjust last page size
if ((off + n_cmp) > mf->len) {
cmp_size = mf->len - off;
}
aligned_size = cmp_size;
if (aligned_size & (4 - 1)) {
aligned_size = (cmp_size + 4) & ~(4 - 1);
}
stlink_read_mem32(sl, addr + (uint32_t)off, (uint16_t)aligned_size);
if (memcmp(sl->q_buf, mf->base + off, cmp_size)) {
return (-1);
}
}
return (0);
}
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 (int32_t i = 0; i < (int32_t)sizeof(md5Hash); i++) {
printf("%x", md5Hash.bytes[i]);
}
printf(", ");
}
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);
}
void stlink_fwrite_finalize(stlink_t *sl, stm32_addr_t addr) {
uint32_t val;
// set PC to the reset routine
stlink_read_debug32(sl, addr + 4, &val);
stlink_write_reg(sl, val, 15);
stlink_run(sl, RUN_NORMAL);
}
static int32_t stlink_read(stlink_t *sl, stm32_addr_t addr, size_t size,
save_block_fn fn, void *fn_arg) {

Wyświetl plik

@ -1,23 +0,0 @@
/* == nightwalker-87: TODO: CONTENT AND USE OF THIS SOURCE FILE IS TO BE VERIFIED (07.06.2023) == */
/* TODO: This file should be split up into new or existing modules. */
/*
* File: common.h
*
*
*/
#ifndef COMMON_H
#define COMMON_H
#include <stdint.h>
#include "map_file.h"
#include "md5.h"
int32_t check_file(stlink_t *, mapped_file_t *, stm32_addr_t);
void md5_calculate(mapped_file_t *);
void stlink_checksum(mapped_file_t *);
void stlink_fwrite_finalize(stlink_t *, stm32_addr_t);
#endif // COMMON_H

Wyświetl plik

@ -5,16 +5,19 @@
*/
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <unistd.h>
#include <stlink.h>
#include "common_flash.h"
#include "calculate.h"
#include "flash_loader.h"
#include "common_flash.h"
#include "logging.h"
#include "map_file.h"
#include "common.h"
#include "md5.h"
#define DEBUG_FLASH 0
@ -742,6 +745,7 @@ void clear_flash_cr_pg(stlink_t *sl, uint32_t bank) {
n = read_flash_cr(sl, bank) & ~(1 << bit);
stlink_write_debug32(sl, cr_reg, n);
}
/* ------------------------------------------------------------------------ */
static void wait_flash_busy_progress(stlink_t *sl) {
@ -1170,7 +1174,7 @@ int32_t stlink_erase_flash_section(stlink_t *sl, stm32_addr_t base_addr, size_t
return (-1);
}
fprintf(stdout, "-> Flash page at %#x erased (size: %#x)\n", addr, page_size);
fprintf(stdout, "-> Flash page at %#x erased (size: %#x)\r", addr, page_size);
fflush(stdout);
// check the next page is within the range to erase
@ -1232,8 +1236,7 @@ int32_t stlink_erase_flash_mass(stlink_t *sl) {
return (err);
}
int32_t stlink_mwrite_flash(stlink_t *sl, uint8_t *data, uint32_t length,
stm32_addr_t addr) {
int32_t stlink_mwrite_flash(stlink_t *sl, uint8_t *data, uint32_t length, stm32_addr_t addr) {
/* Write the block in flash at addr */
int32_t err;
uint32_t num_empty, idx;
@ -1416,12 +1419,10 @@ int32_t stlink_check_address_alignment(stlink_t *sl, stm32_addr_t addr) {
return 0;
}
int32_t stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t *base,
uint32_t len, uint8_t eraseonly) {
int32_t stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len, uint8_t eraseonly) {
int32_t ret;
flash_loader_t fl;
ILOG("Attempting to write %d (%#x) bytes to stm32 address: %u (%#x)\n", len,
len, addr, addr);
ILOG("Attempting to write %d (%#x) bytes to stm32 address: %u (%#x)\n", len, len, addr, addr);
// check addr range is inside the flash
stlink_calculate_pagesize(sl, addr);
@ -1438,6 +1439,9 @@ int32_t stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t *base,
(uint32_t)(sl->flash_pgsz));
return (-1);
}
if ((len % 16 <= 8) & (sl->flash_type == STM32_FLASH_TYPE_L5_U5)) {
len += 8;
}
// make sure we've loaded the context with the chip details
stlink_core_id(sl);
@ -1464,3 +1468,11 @@ int32_t stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t *base,
return (stlink_verify_write_flash(sl, addr, base, len));
}
void stlink_fwrite_finalize(stlink_t *sl, stm32_addr_t addr) {
uint32_t val;
// set PC to the reset routine
stlink_read_debug32(sl, addr + 4, &val);
stlink_write_reg(sl, val, 15);
stlink_run(sl, RUN_NORMAL);
}

Wyświetl plik

@ -9,22 +9,45 @@
#include <stdint.h>
void lock_flash(stlink_t *);
void clear_flash_error(stlink_t *);
void wait_flash_busy(stlink_t *);
int32_t check_flash_error(stlink_t *);
int32_t unlock_flash_if(stlink_t *);
int32_t lock_flash_option(stlink_t *);
int32_t unlock_flash_option_if(stlink_t *);
void write_flash_cr_psiz(stlink_t *, uint32_t, unsigned);
void clear_flash_cr_pg(stlink_t *, unsigned);
// TODO: move to private defines if possible
#define BANK_1 0
#define BANK_2 1
uint32_t read_flash_cr(stlink_t *, unsigned);
uint32_t get_stm32l0_flash_base(stlink_t *);
uint32_t read_flash_cr(stlink_t *, uint32_t);
void lock_flash(stlink_t *);
// static inline int32_t write_flash_sr(stlink_t *sl, uint32_t bank, uint32_t val);
void clear_flash_error(stlink_t *);
uint32_t read_flash_sr(stlink_t *sl, uint32_t bank);
uint32_t is_flash_busy(stlink_t *sl);
void wait_flash_busy(stlink_t *);
int32_t check_flash_error(stlink_t *);
// static inline uint32_t is_flash_locked(stlink_t *sl);
// static void unlock_flash(stlink_t *sl);
int32_t unlock_flash_if(stlink_t *);
int32_t lock_flash_option(stlink_t *);
// static bool is_flash_option_locked(stlink_t *sl);
// static int32_t unlock_flash_option(stlink_t *sl);
int32_t unlock_flash_option_if(stlink_t *);
void write_flash_cr_psiz(stlink_t *, uint32_t, uint32_t);
void clear_flash_cr_pg(stlink_t *, uint32_t);
// static void wait_flash_busy_progress(stlink_t *sl);
// static inline void write_flash_ar(stlink_t *sl, uint32_t n, uint32_t bank);
// static inline void write_flash_cr_snb(stlink_t *sl, uint32_t n, uint32_t bank);
// static void set_flash_cr_per(stlink_t *sl, uint32_t bank);
// static void clear_flash_cr_per(stlink_t *sl, uint32_t bank);
// static inline void write_flash_cr_bker_pnb(stlink_t *sl, uint32_t n);
// static void set_flash_cr_strt(stlink_t *sl, uint32_t bank);
// static void set_flash_cr_mer(stlink_t *sl, bool v, uint32_t bank);
int32_t stlink_erase_flash_page(stlink_t *sl, stm32_addr_t flashaddr);
int32_t stlink_erase_flash_section(stlink_t *sl, stm32_addr_t base_addr, size_t size, bool align_size);
int32_t stlink_erase_flash_mass(stlink_t *sl);
int32_t stlink_mwrite_flash(stlink_t *sl, uint8_t *data, uint32_t length, stm32_addr_t addr);
int32_t stlink_fwrite_flash(stlink_t *sl, const char *path, stm32_addr_t addr);
int32_t stlink_fcheck_flash(stlink_t *sl, const char *path, stm32_addr_t addr);
int32_t stlink_verify_write_flash(stlink_t *sl, stm32_addr_t address, uint8_t *data, uint32_t length);
int32_t stlink_check_address_range_validity(stlink_t *sl, stm32_addr_t addr, size_t size);
int32_t stlink_check_address_alignment(stlink_t *sl, stm32_addr_t addr);
int32_t stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len, uint8_t eraseonly);
void stlink_fwrite_finalize(stlink_t *, stm32_addr_t);
#endif // COMMON_FLASH_H

Wyświetl plik

@ -11,9 +11,12 @@
#include <stm32.h>
#include <stlink.h>
#include <helper.h>
#include "flash_loader.h"
#include "common_flash.h"
#include "helper.h"
#include "logging.h"
#include "register.h"
#define FLASH_REGS_BANK2_OFS 0x40
#define FLASH_BANK2_START_ADDR 0x08080000
@ -757,7 +760,9 @@ int32_t stlink_flashloader_write(stlink_t *sl, flash_loader_t *fl, stm32_addr_t
fflush(stdout);
}
write_uint32((unsigned char *)&data, *(uint32_t *)(base + off));
// write_uint32((unsigned char *)&data, *(uint32_t *)(base + off));
data = 0;
memcpy(&data, base + off, (len - off) < 4 ? (len - off) : 4);
stlink_write_debug32(sl, addr + (uint32_t)off, data);
wait_flash_busy(sl); // wait for 'busy' bit in FLASH_SR to clear
}

Wyświetl plik

@ -8,10 +8,6 @@
#define FLASH_LOADER_H
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <stlink.h>
int32_t stlink_flash_loader_init(stlink_t *sl, flash_loader_t* fl);
// static int32_t loader_v_dependent_assignment(stlink_t *sl,

Wyświetl plik

@ -1,21 +1,22 @@
/* == nightwalker-87: TODO: CONTENT AND USE OF THIS SOURCE FILE IS TO BE VERIFIED (07.06.2023) == */
/*
* File: helper.c
*
* General helper functions
*/
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include "helper.h"
#ifdef STLINK_HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <sys_time.h>
#endif
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include "helper.h"
uint32_t time_ms() {
struct timeval tv;
gettimeofday(&tv, NULL);

Wyświetl plik

@ -1,3 +1,5 @@
/* == nightwalker-87: TODO: CONTENT AND USE OF THIS SOURCE FILE IS TO BE VERIFIED (07.06.2023) == */
/*
* File: helper.h
*

Wyświetl plik

@ -0,0 +1,280 @@
/*
* WjCryptLib_Md5 (https://github.com/WaterJuice/WjCryptLib)
* Implementation of MD5 hash function. Originally written by Alexander Peslyak.
* Modified by WaterJuice retaining Public Domain license.
* This is free and unencumbered software released into the public domain - June 2013 - waterjuice.org
*/
#include <stdint.h>
#include <memory.h>
#include "lib_md5.h"
/* INTERNAL FUNCTIONS */
/* F, G, H, I
*
* The basic MD5 functions.
* F and G are optimised compared to their RFC 1321 definitions for architectures
* that lack an AND-NOT instruction, just like in Colin Plumb's implementation.
*/
#define F( x, y, z ) ((z) ^ ((x) & ((y) ^ (z))))
#define G( x, y, z ) ((y) ^ ((z) & ((x) ^ (y))))
#define H( x, y, z ) ((x) ^ (y) ^ (z))
#define I( x, y, z ) ((y) ^ ((x) | ~(z)))
/* STEP: The MD5 transformation for all four rounds. */
#define STEP( f, a, b, c, d, x, t, s ) \
(a) += f((b), (c), (d)) + (x) + (t); \
(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
(a) += (b);
/* TransformFunction
* This processes one or more 64-byte data blocks, but does NOT update the bit counters.
* There are no alignment requirements.
*/
static void* TransformFunction(Md5Context* ctx, void const* data, uintmax_t size) {
uint8_t* ptr;
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
uint32_t saved_a;
uint32_t saved_b;
uint32_t saved_c;
uint32_t saved_d;
#define GET(n) (ctx->block[(n)])
#define SET(n) (ctx->block[(n)] = ((uint32_t)ptr[(n) * 4 + 0] << 0) | \
((uint32_t)ptr[(n) * 4 + 1] << 8) | \
((uint32_t)ptr[(n) * 4 + 2] << 16) | \
((uint32_t)ptr[(n) * 4 + 3] << 24))
ptr = (uint8_t*)data;
a = ctx->a;
b = ctx->b;
c = ctx->c;
d = ctx->d;
do {
saved_a = a;
saved_b = b;
saved_c = c;
saved_d = d;
// Round 1
STEP( F, a, b, c, d, SET(0), 0xd76aa478, 7 )
STEP( F, d, a, b, c, SET(1), 0xe8c7b756, 12 )
STEP( F, c, d, a, b, SET(2), 0x242070db, 17 )
STEP( F, b, c, d, a, SET(3), 0xc1bdceee, 22 )
STEP( F, a, b, c, d, SET(4), 0xf57c0faf, 7 )
STEP( F, d, a, b, c, SET(5), 0x4787c62a, 12 )
STEP( F, c, d, a, b, SET(6), 0xa8304613, 17 )
STEP( F, b, c, d, a, SET(7), 0xfd469501, 22 )
STEP( F, a, b, c, d, SET(8 ), 0x698098d8, 7 )
STEP( F, d, a, b, c, SET(9 ), 0x8b44f7af, 12 )
STEP( F, c, d, a, b, SET(10 ), 0xffff5bb1, 17 )
STEP( F, b, c, d, a, SET(11 ), 0x895cd7be, 22 )
STEP( F, a, b, c, d, SET(12 ), 0x6b901122, 7 )
STEP( F, d, a, b, c, SET(13 ), 0xfd987193, 12 )
STEP( F, c, d, a, b, SET(14 ), 0xa679438e, 17 )
STEP( F, b, c, d, a, SET(15 ), 0x49b40821, 22 )
// Round 2
STEP( G, a, b, c, d, GET(1), 0xf61e2562, 5 )
STEP( G, d, a, b, c, GET(6), 0xc040b340, 9 )
STEP( G, c, d, a, b, GET(11), 0x265e5a51, 14 )
STEP( G, b, c, d, a, GET(0), 0xe9b6c7aa, 20 )
STEP( G, a, b, c, d, GET(5), 0xd62f105d, 5 )
STEP( G, d, a, b, c, GET(10), 0x02441453, 9 )
STEP( G, c, d, a, b, GET(15), 0xd8a1e681, 14 )
STEP( G, b, c, d, a, GET(4), 0xe7d3fbc8, 20 )
STEP( G, a, b, c, d, GET(9), 0x21e1cde6, 5 )
STEP( G, d, a, b, c, GET(14), 0xc33707d6, 9 )
STEP( G, c, d, a, b, GET(3), 0xf4d50d87, 14 )
STEP( G, b, c, d, a, GET(8), 0x455a14ed, 20 )
STEP( G, a, b, c, d, GET(13), 0xa9e3e905, 5 )
STEP( G, d, a, b, c, GET(2), 0xfcefa3f8, 9 )
STEP( G, c, d, a, b, GET(7), 0x676f02d9, 14 )
STEP( G, b, c, d, a, GET(12), 0x8d2a4c8a, 20 )
// Round 3
STEP( H, a, b, c, d, GET(5), 0xfffa3942, 4 )
STEP( H, d, a, b, c, GET(8), 0x8771f681, 11 )
STEP( H, c, d, a, b, GET(11), 0x6d9d6122, 16 )
STEP( H, b, c, d, a, GET(14), 0xfde5380c, 23 )
STEP( H, a, b, c, d, GET(1), 0xa4beea44, 4 )
STEP( H, d, a, b, c, GET(4), 0x4bdecfa9, 11 )
STEP( H, c, d, a, b, GET(7), 0xf6bb4b60, 16 )
STEP( H, b, c, d, a, GET(10), 0xbebfbc70, 23 )
STEP( H, a, b, c, d, GET(13), 0x289b7ec6, 4 )
STEP( H, d, a, b, c, GET(0), 0xeaa127fa, 11 )
STEP( H, c, d, a, b, GET(3), 0xd4ef3085, 16 )
STEP( H, b, c, d, a, GET(6), 0x04881d05, 23 )
STEP( H, a, b, c, d, GET(9), 0xd9d4d039, 4 )
STEP( H, d, a, b, c, GET(12), 0xe6db99e5, 11 )
STEP( H, c, d, a, b, GET(15), 0x1fa27cf8, 16 )
STEP( H, b, c, d, a, GET(2), 0xc4ac5665, 23 )
// Round 4
STEP( I, a, b, c, d, GET(0), 0xf4292244, 6 )
STEP( I, d, a, b, c, GET(7), 0x432aff97, 10 )
STEP( I, c, d, a, b, GET(14), 0xab9423a7, 15 )
STEP( I, b, c, d, a, GET(5), 0xfc93a039, 21 )
STEP( I, a, b, c, d, GET(12), 0x655b59c3, 6 )
STEP( I, d, a, b, c, GET(3), 0x8f0ccc92, 10 )
STEP( I, c, d, a, b, GET(10), 0xffeff47d, 15 )
STEP( I, b, c, d, a, GET(1), 0x85845dd1, 21 )
STEP( I, a, b, c, d, GET(8), 0x6fa87e4f, 6 )
STEP( I, d, a, b, c, GET(15), 0xfe2ce6e0, 10 )
STEP( I, c, d, a, b, GET(6), 0xa3014314, 15 )
STEP( I, b, c, d, a, GET(13), 0x4e0811a1, 21 )
STEP( I, a, b, c, d, GET(4), 0xf7537e82, 6 )
STEP( I, d, a, b, c, GET(11), 0xbd3af235, 10 )
STEP( I, c, d, a, b, GET(2), 0x2ad7d2bb, 15 )
STEP( I, b, c, d, a, GET(9), 0xeb86d391, 21 )
a += saved_a;
b += saved_b;
c += saved_c;
d += saved_d;
ptr += 64;
} while ( size -= 64 );
ctx->a = a;
ctx->b = b;
ctx->c = c;
ctx->d = d;
#undef GET
#undef SET
return(ptr);
}
/* EXPORTED FUNCTIONS */
/* Md5Initialise
* Initialises an MD5 Context.
* Use this to initialise/reset a context.
*/
void Md5Initialise(Md5Context* Context /* [out] */) {
Context->a = 0x67452301;
Context->b = 0xefcdab89;
Context->c = 0x98badcfe;
Context->d = 0x10325476;
Context->lo = 0;
Context->hi = 0;
}
/* Md5Update
* Adds data to the MD5 context.
* This will process the data and update the internal state of the context.
* Keep on calling this function until all the data has been added.
* Then call Md5Finalise to calculate the hash.
*/
void Md5Update(Md5Context* Context /* [in out] */, void const* Buffer /* [in] */, uint32_t BufferSize /* [in] */) {
uint32_t saved_lo;
uint32_t used;
uint32_t free;
saved_lo = Context->lo;
if ((Context->lo = (saved_lo + BufferSize) & 0x1fffffff) < saved_lo) {
Context->hi++;
}
Context->hi += (uint32_t)(BufferSize >> 29);
used = saved_lo & 0x3f;
if ( used ) {
free = 64 - used;
if ( BufferSize < free ) {
memcpy( &Context->buffer[used], Buffer, BufferSize );
return;
}
memcpy( &Context->buffer[used], Buffer, free );
Buffer = (uint8_t*)Buffer + free;
BufferSize -= free;
TransformFunction(Context, Context->buffer, 64);
}
if ( BufferSize >= 64 ) {
Buffer = TransformFunction( Context, Buffer, BufferSize & ~(uint32_t)0x3f );
BufferSize &= 0x3f;
}
memcpy( Context->buffer, Buffer, BufferSize );
}
/* Md5Finalise
* Performs the final calculation of the hash and returns the digest
* (16 byte buffer containing 128bit hash).
* After calling this, Md5Initialised must be used to reuse the context.
*/
void Md5Finalise(Md5Context* Context /* [in out] */, MD5_HASH* Digest /* [in] */) {
uint32_t used;
uint32_t free;
used = Context->lo & 0x3f;
Context->buffer[used++] = 0x80;
free = 64 - used;
if (free < 8) {
memset( &Context->buffer[used], 0, free );
TransformFunction( Context, Context->buffer, 64 );
used = 0;
free = 64;
}
memset( &Context->buffer[used], 0, free - 8 );
Context->lo <<= 3;
Context->buffer[56] = (uint8_t)(Context->lo);
Context->buffer[57] = (uint8_t)(Context->lo >> 8);
Context->buffer[58] = (uint8_t)(Context->lo >> 16);
Context->buffer[59] = (uint8_t)(Context->lo >> 24);
Context->buffer[60] = (uint8_t)(Context->hi);
Context->buffer[61] = (uint8_t)(Context->hi >> 8);
Context->buffer[62] = (uint8_t)(Context->hi >> 16);
Context->buffer[63] = (uint8_t)(Context->hi >> 24);
TransformFunction( Context, Context->buffer, 64 );
Digest->bytes[0] = (uint8_t)(Context->a);
Digest->bytes[1] = (uint8_t)(Context->a >> 8);
Digest->bytes[2] = (uint8_t)(Context->a >> 16);
Digest->bytes[3] = (uint8_t)(Context->a >> 24);
Digest->bytes[4] = (uint8_t)(Context->b);
Digest->bytes[5] = (uint8_t)(Context->b >> 8);
Digest->bytes[6] = (uint8_t)(Context->b >> 16);
Digest->bytes[7] = (uint8_t)(Context->b >> 24);
Digest->bytes[8] = (uint8_t)(Context->c);
Digest->bytes[9] = (uint8_t)(Context->c >> 8);
Digest->bytes[10] = (uint8_t)(Context->c >> 16);
Digest->bytes[11] = (uint8_t)(Context->c >> 24);
Digest->bytes[12] = (uint8_t)(Context->d);
Digest->bytes[13] = (uint8_t)(Context->d >> 8);
Digest->bytes[14] = (uint8_t)(Context->d >> 16);
Digest->bytes[15] = (uint8_t)(Context->d >> 24);
}
/* Md5Calculate
* Combines Md5Initialise, Md5Update, and Md5Finalise into one function.
* Calculates the MD5 hash of the buffer.
*/
void Md5Calculate(void const* Buffer /* [in] */, uint32_t BufferSize /* [in] */, MD5_HASH* Digest /* [in] */) {
Md5Context context;
Md5Initialise( &context );
Md5Update( &context, Buffer, BufferSize );
Md5Finalise( &context, Digest );
}

Wyświetl plik

@ -0,0 +1,68 @@
/*
* WjCryptLib_Md5 (https://github.com/WaterJuice/WjCryptLib)
* Implementation of MD5 hash function. Originally written by Alexander Peslyak.
* Modified by WaterJuice retaining Public Domain license.
* This is free and unencumbered software released into the public domain - June 2013 - waterjuice.org
*/
#ifndef LIB_MD5_H
#define LIB_MD5_H
#pragma once
#include <stdint.h>
#include <stdio.h>
/* TYPES */
/* Md5Context
* This must be initialised using Md5Initialised.
* Do not modify the contents of this structure directly.
*/
typedef struct {
uint32_t lo;
uint32_t hi;
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
uint8_t buffer[64];
uint32_t block[16];
} Md5Context;
#define MD5_HASH_SIZE (128 / 8)
typedef struct {
uint8_t bytes [MD5_HASH_SIZE];
} MD5_HASH;
/* PUBLIC FUNCTIONS */
/* Md5Initialise
* Initialises an MD5 Context.
* Use this to initialise/reset a context.
*/
void Md5Initialise(Md5Context* Context /* [out] */);
/* Md5Update
* Adds data to the MD5 context.
* This will process the data and update the internal state of the context.
* Keep on calling this function until all the data has been added.
* Then call Md5Finalise to calculate the hash.
*/
void Md5Update(Md5Context* Context /* [in out] */, void const* Buffer /* [in] */, uint32_t BufferSize /* [in] */);
/* Md5Finalise
* Performs the final calculation of the hash and returns the digest
* (16 byte buffer containing 128bit hash).
* After calling this, Md5Initialised must be used to reuse the context.
*/
void Md5Finalise(Md5Context* Context /* [in out] */, MD5_HASH* Digest /* [in] */);
/* Md5Calculate
* Combines Md5Initialise, Md5Update, and Md5Finalise into one function.
* Calculates the MD5 hash of the buffer.
*/
void Md5Calculate(void const* Buffer /* [in] */, uint32_t BufferSize /* [in] */, MD5_HASH* Digest /* [in] */);
#endif // LIB_MD5_H

Wyświetl plik

@ -1,15 +1,15 @@
/*
* UglyLogging
* File: logging.c
*
* Slow, yet another wheel reinvented, but enough to make the rest of our code pretty enough.
* UglyLogging: Slow, yet another wheel reinvented, but enough to make the rest of our code pretty enough.
*/
#include <stdarg.h>
#include <stddef.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdarg.h>
#include <time.h>
#include "logging.h"

Wyświetl plik

@ -1,4 +1,7 @@
/*
* File: logging.h
*
* UglyLogging: Slow, yet another wheel reinvented, but enough to make the rest of our code pretty enough.
* Ugly, low performance, configurable level, logging "framework"
*/

Wyświetl plik

@ -4,24 +4,62 @@
* File mapping
*/
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stlink.h>
#include "logging.h"
#include "map_file.h"
#include "md5.h"
#ifndef O_BINARY
#define O_BINARY 0
#endif
// 1 GB max file size
#ifndef MAX_FILE_SIZE
#define MAX_FILE_SIZE (1<<20) // 1 GB max file size
#define MAX_FILE_SIZE (1<<20)
#endif
/* Limit the block size to compare to 0x1800 as anything larger will stall the
* STLINK2 Maybe STLINK V1 needs smaller value!
*/
int32_t check_file(stlink_t *sl, mapped_file_t *mf, stm32_addr_t addr) {
size_t off;
size_t n_cmp = sl->flash_pgsz;
if (n_cmp > 0x1800) {
n_cmp = 0x1800;
}
for (off = 0; off < mf->len; off += n_cmp) {
size_t aligned_size;
size_t cmp_size = n_cmp; // adjust last page size
if ((off + n_cmp) > mf->len) {
cmp_size = mf->len - off;
}
aligned_size = cmp_size;
if (aligned_size & (4 - 1)) {
aligned_size = (cmp_size + 4) & ~(4 - 1);
}
stlink_read_mem32(sl, addr + (uint32_t)off, (uint16_t)aligned_size);
if (memcmp(sl->q_buf, mf->base + off, cmp_size)) {
return (-1);
}
}
return (0);
}
int32_t map_file(mapped_file_t *mf, const char *path) {
int32_t error = -1;
struct stat st;

Wyświetl plik

@ -28,6 +28,7 @@ typedef struct mapped_file {
#define MAPPED_FILE_INITIALIZER \
{ NULL, 0 }
int32_t check_file(stlink_t *, mapped_file_t *, stm32_addr_t);
int32_t map_file(mapped_file_t *, const char *);
void unmap_file(mapped_file_t *);

Wyświetl plik

@ -1,10 +1,3 @@
/*
* WjCryptLib_Md5 (https://github.com/WaterJuice/WjCryptLib)
* Implementation of MD5 hash function. Originally written by Alexander Peslyak.
* Modified by WaterJuice retaining Public Domain license.
* This is free and unencumbered software released into the public domain - June 2013 - waterjuice.org
*/
/*
* File: md5.c
*
@ -12,275 +5,37 @@
*/
#include <stdint.h>
#include <memory.h>
#include <stlink.h>
#include "md5.h"
/* INTERNAL FUNCTIONS */
#include "map_file.h"
#include "lib_md5.h"
/* F, G, H, I
*
* The basic MD5 functions.
* F and G are optimised compared to their RFC 1321 definitions for architectures
* that lack an AND-NOT instruction, just like in Colin Plumb's implementation.
*/
#define F( x, y, z ) ((z) ^ ((x) & ((y) ^ (z))))
#define G( x, y, z ) ((y) ^ ((z) & ((x) ^ (y))))
#define H( x, y, z ) ((x) ^ (y) ^ (z))
#define I( x, y, z ) ((y) ^ ((x) | ~(z)))
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: ");
/* STEP: The MD5 transformation for all four rounds. */
#define STEP( f, a, b, c, d, x, t, s ) \
(a) += f((b), (c), (d)) + (x) + (t); \
(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
(a) += (b);
for (int32_t i = 0; i < (int32_t)sizeof(md5Hash); i++) {
printf("%x", md5Hash.bytes[i]);
}
/* TransformFunction
* This processes one or more 64-byte data blocks, but does NOT update the bit counters.
* There are no alignment requirements.
*/
static void* TransformFunction(Md5Context* ctx, void const* data, uintmax_t size) {
uint8_t* ptr;
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
uint32_t saved_a;
uint32_t saved_b;
uint32_t saved_c;
uint32_t saved_d;
#define GET(n) (ctx->block[(n)])
#define SET(n) (ctx->block[(n)] = ((uint32_t)ptr[(n) * 4 + 0] << 0) | \
((uint32_t)ptr[(n) * 4 + 1] << 8) | \
((uint32_t)ptr[(n) * 4 + 2] << 16) | \
((uint32_t)ptr[(n) * 4 + 3] << 24))
ptr = (uint8_t*)data;
a = ctx->a;
b = ctx->b;
c = ctx->c;
d = ctx->d;
do {
saved_a = a;
saved_b = b;
saved_c = c;
saved_d = d;
// Round 1
STEP( F, a, b, c, d, SET(0), 0xd76aa478, 7 )
STEP( F, d, a, b, c, SET(1), 0xe8c7b756, 12 )
STEP( F, c, d, a, b, SET(2), 0x242070db, 17 )
STEP( F, b, c, d, a, SET(3), 0xc1bdceee, 22 )
STEP( F, a, b, c, d, SET(4), 0xf57c0faf, 7 )
STEP( F, d, a, b, c, SET(5), 0x4787c62a, 12 )
STEP( F, c, d, a, b, SET(6), 0xa8304613, 17 )
STEP( F, b, c, d, a, SET(7), 0xfd469501, 22 )
STEP( F, a, b, c, d, SET(8 ), 0x698098d8, 7 )
STEP( F, d, a, b, c, SET(9 ), 0x8b44f7af, 12 )
STEP( F, c, d, a, b, SET(10 ), 0xffff5bb1, 17 )
STEP( F, b, c, d, a, SET(11 ), 0x895cd7be, 22 )
STEP( F, a, b, c, d, SET(12 ), 0x6b901122, 7 )
STEP( F, d, a, b, c, SET(13 ), 0xfd987193, 12 )
STEP( F, c, d, a, b, SET(14 ), 0xa679438e, 17 )
STEP( F, b, c, d, a, SET(15 ), 0x49b40821, 22 )
// Round 2
STEP( G, a, b, c, d, GET(1), 0xf61e2562, 5 )
STEP( G, d, a, b, c, GET(6), 0xc040b340, 9 )
STEP( G, c, d, a, b, GET(11), 0x265e5a51, 14 )
STEP( G, b, c, d, a, GET(0), 0xe9b6c7aa, 20 )
STEP( G, a, b, c, d, GET(5), 0xd62f105d, 5 )
STEP( G, d, a, b, c, GET(10), 0x02441453, 9 )
STEP( G, c, d, a, b, GET(15), 0xd8a1e681, 14 )
STEP( G, b, c, d, a, GET(4), 0xe7d3fbc8, 20 )
STEP( G, a, b, c, d, GET(9), 0x21e1cde6, 5 )
STEP( G, d, a, b, c, GET(14), 0xc33707d6, 9 )
STEP( G, c, d, a, b, GET(3), 0xf4d50d87, 14 )
STEP( G, b, c, d, a, GET(8), 0x455a14ed, 20 )
STEP( G, a, b, c, d, GET(13), 0xa9e3e905, 5 )
STEP( G, d, a, b, c, GET(2), 0xfcefa3f8, 9 )
STEP( G, c, d, a, b, GET(7), 0x676f02d9, 14 )
STEP( G, b, c, d, a, GET(12), 0x8d2a4c8a, 20 )
// Round 3
STEP( H, a, b, c, d, GET(5), 0xfffa3942, 4 )
STEP( H, d, a, b, c, GET(8), 0x8771f681, 11 )
STEP( H, c, d, a, b, GET(11), 0x6d9d6122, 16 )
STEP( H, b, c, d, a, GET(14), 0xfde5380c, 23 )
STEP( H, a, b, c, d, GET(1), 0xa4beea44, 4 )
STEP( H, d, a, b, c, GET(4), 0x4bdecfa9, 11 )
STEP( H, c, d, a, b, GET(7), 0xf6bb4b60, 16 )
STEP( H, b, c, d, a, GET(10), 0xbebfbc70, 23 )
STEP( H, a, b, c, d, GET(13), 0x289b7ec6, 4 )
STEP( H, d, a, b, c, GET(0), 0xeaa127fa, 11 )
STEP( H, c, d, a, b, GET(3), 0xd4ef3085, 16 )
STEP( H, b, c, d, a, GET(6), 0x04881d05, 23 )
STEP( H, a, b, c, d, GET(9), 0xd9d4d039, 4 )
STEP( H, d, a, b, c, GET(12), 0xe6db99e5, 11 )
STEP( H, c, d, a, b, GET(15), 0x1fa27cf8, 16 )
STEP( H, b, c, d, a, GET(2), 0xc4ac5665, 23 )
// Round 4
STEP( I, a, b, c, d, GET(0), 0xf4292244, 6 )
STEP( I, d, a, b, c, GET(7), 0x432aff97, 10 )
STEP( I, c, d, a, b, GET(14), 0xab9423a7, 15 )
STEP( I, b, c, d, a, GET(5), 0xfc93a039, 21 )
STEP( I, a, b, c, d, GET(12), 0x655b59c3, 6 )
STEP( I, d, a, b, c, GET(3), 0x8f0ccc92, 10 )
STEP( I, c, d, a, b, GET(10), 0xffeff47d, 15 )
STEP( I, b, c, d, a, GET(1), 0x85845dd1, 21 )
STEP( I, a, b, c, d, GET(8), 0x6fa87e4f, 6 )
STEP( I, d, a, b, c, GET(15), 0xfe2ce6e0, 10 )
STEP( I, c, d, a, b, GET(6), 0xa3014314, 15 )
STEP( I, b, c, d, a, GET(13), 0x4e0811a1, 21 )
STEP( I, a, b, c, d, GET(4), 0xf7537e82, 6 )
STEP( I, d, a, b, c, GET(11), 0xbd3af235, 10 )
STEP( I, c, d, a, b, GET(2), 0x2ad7d2bb, 15 )
STEP( I, b, c, d, a, GET(9), 0xeb86d391, 21 )
a += saved_a;
b += saved_b;
c += saved_c;
d += saved_d;
ptr += 64;
} while ( size -= 64 );
ctx->a = a;
ctx->b = b;
ctx->c = c;
ctx->d = d;
#undef GET
#undef SET
return(ptr);
printf(", ");
}
/* EXPORTED FUNCTIONS */
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;
/* Md5Initialise
* Initialises an MD5 Context.
* Use this to initialise/reset a context.
*/
void Md5Initialise(Md5Context* Context /* [out] */) {
Context->a = 0x67452301;
Context->b = 0xefcdab89;
Context->c = 0x98badcfe;
Context->d = 0x10325476;
for (size_t i = 0; i < mp->len; ++i) {
sum += mp_byte[i];
}
Context->lo = 0;
Context->hi = 0;
}
/* Md5Update
* Adds data to the MD5 context.
* This will process the data and update the internal state of the context.
* Keep on calling this function until all the data has been added.
* Then call Md5Finalise to calculate the hash.
*/
void Md5Update(Md5Context* Context /* [in out] */, void const* Buffer /* [in] */, uint32_t BufferSize /* [in] */) {
uint32_t saved_lo;
uint32_t used;
uint32_t free;
saved_lo = Context->lo;
if ((Context->lo = (saved_lo + BufferSize) & 0x1fffffff) < saved_lo) {
Context->hi++;
}
Context->hi += (uint32_t)(BufferSize >> 29);
used = saved_lo & 0x3f;
if ( used ) {
free = 64 - used;
if ( BufferSize < free ) {
memcpy( &Context->buffer[used], Buffer, BufferSize );
return;
}
memcpy( &Context->buffer[used], Buffer, free );
Buffer = (uint8_t*)Buffer + free;
BufferSize -= free;
TransformFunction(Context, Context->buffer, 64);
}
if ( BufferSize >= 64 ) {
Buffer = TransformFunction( Context, Buffer, BufferSize & ~(uint32_t)0x3f );
BufferSize &= 0x3f;
}
memcpy( Context->buffer, Buffer, BufferSize );
}
/* Md5Finalise
* Performs the final calculation of the hash and returns the digest
* (16 byte buffer containing 128bit hash).
* After calling this, Md5Initialised must be used to reuse the context.
*/
void Md5Finalise(Md5Context* Context /* [in out] */, MD5_HASH* Digest /* [in] */) {
uint32_t used;
uint32_t free;
used = Context->lo & 0x3f;
Context->buffer[used++] = 0x80;
free = 64 - used;
if (free < 8) {
memset( &Context->buffer[used], 0, free );
TransformFunction( Context, Context->buffer, 64 );
used = 0;
free = 64;
}
memset( &Context->buffer[used], 0, free - 8 );
Context->lo <<= 3;
Context->buffer[56] = (uint8_t)(Context->lo);
Context->buffer[57] = (uint8_t)(Context->lo >> 8);
Context->buffer[58] = (uint8_t)(Context->lo >> 16);
Context->buffer[59] = (uint8_t)(Context->lo >> 24);
Context->buffer[60] = (uint8_t)(Context->hi);
Context->buffer[61] = (uint8_t)(Context->hi >> 8);
Context->buffer[62] = (uint8_t)(Context->hi >> 16);
Context->buffer[63] = (uint8_t)(Context->hi >> 24);
TransformFunction( Context, Context->buffer, 64 );
Digest->bytes[0] = (uint8_t)(Context->a);
Digest->bytes[1] = (uint8_t)(Context->a >> 8);
Digest->bytes[2] = (uint8_t)(Context->a >> 16);
Digest->bytes[3] = (uint8_t)(Context->a >> 24);
Digest->bytes[4] = (uint8_t)(Context->b);
Digest->bytes[5] = (uint8_t)(Context->b >> 8);
Digest->bytes[6] = (uint8_t)(Context->b >> 16);
Digest->bytes[7] = (uint8_t)(Context->b >> 24);
Digest->bytes[8] = (uint8_t)(Context->c);
Digest->bytes[9] = (uint8_t)(Context->c >> 8);
Digest->bytes[10] = (uint8_t)(Context->c >> 16);
Digest->bytes[11] = (uint8_t)(Context->c >> 24);
Digest->bytes[12] = (uint8_t)(Context->d);
Digest->bytes[13] = (uint8_t)(Context->d >> 8);
Digest->bytes[14] = (uint8_t)(Context->d >> 16);
Digest->bytes[15] = (uint8_t)(Context->d >> 24);
}
/* Md5Calculate
* Combines Md5Initialise, Md5Update, and Md5Finalise into one function.
* Calculates the MD5 hash of the buffer.
*/
void Md5Calculate(void const* Buffer /* [in] */, uint32_t BufferSize /* [in] */, MD5_HASH* Digest /* [in] */) {
Md5Context context;
Md5Initialise( &context );
Md5Update( &context, Buffer, BufferSize );
Md5Finalise( &context, Digest );
}
printf("stlink checksum: 0x%08x\n", sum);
}

Wyświetl plik

@ -1,10 +1,3 @@
/*
* WjCryptLib_Md5 (https://github.com/WaterJuice/WjCryptLib)
* Implementation of MD5 hash function. Originally written by Alexander Peslyak.
* Modified by WaterJuice retaining Public Domain license.
* This is free and unencumbered software released into the public domain - June 2013 - waterjuice.org
*/
/*
* File: md5.h
*
@ -14,61 +7,11 @@
#ifndef MD5_H
#define MD5_H
#pragma once
#include <stdint.h>
#include <stdio.h>
/* TYPES */
#include "map_file.h"
/* Md5Context
* This must be initialised using Md5Initialised.
* Do not modify the contents of this structure directly.
*/
typedef struct {
uint32_t lo;
uint32_t hi;
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
uint8_t buffer[64];
uint32_t block[16];
} Md5Context;
#define MD5_HASH_SIZE (128 / 8)
typedef struct {
uint8_t bytes [MD5_HASH_SIZE];
} MD5_HASH;
/* PUBLIC FUNCTIONS */
/* Md5Initialise
* Initialises an MD5 Context.
* Use this to initialise/reset a context.
*/
void Md5Initialise(Md5Context* Context /* [out] */);
/* Md5Update
* Adds data to the MD5 context.
* This will process the data and update the internal state of the context.
* Keep on calling this function until all the data has been added.
* Then call Md5Finalise to calculate the hash.
*/
void Md5Update(Md5Context* Context /* [in out] */, void const* Buffer /* [in] */, uint32_t BufferSize /* [in] */);
/* Md5Finalise
* Performs the final calculation of the hash and returns the digest
* (16 byte buffer containing 128bit hash).
* After calling this, Md5Initialised must be used to reuse the context.
*/
void Md5Finalise(Md5Context* Context /* [in out] */, MD5_HASH* Digest /* [in] */);
/* Md5Calculate
* Combines Md5Initialise, Md5Update, and Md5Finalise into one function.
* Calculates the MD5 hash of the buffer.
*/
void Md5Calculate(void const* Buffer /* [in] */, uint32_t BufferSize /* [in] */, MD5_HASH* Digest /* [in] */);
void md5_calculate(mapped_file_t *);
void stlink_checksum(mapped_file_t *);
#endif // MD5_H

Wyświetl plik

@ -9,11 +9,13 @@
#include <string.h>
#include <stlink.h>
#include "common.h"
#include "common_flash.h"
#include "map_file.h"
#include "option_bytes.h"
#include "common_flash.h"
#include "flash_loader.h"
#include "logging.h"
#include "map_file.h"
#include "md5.h"
/**
* Read option control register F0

Wyświetl plik

@ -8,9 +8,6 @@
#define OPTION_BYTES_H
#include <stdint.h>
#include <stdio.h>
#include <stlink.h>
int32_t stlink_read_option_bytes32(stlink_t *sl, uint32_t* option_byte);
int32_t stlink_read_option_bytes_boot_add32(stlink_t *sl, uint32_t* option_byte);

Wyświetl plik

@ -4,6 +4,8 @@
#include <stlink.h>
#include "logging.h"
// Endianness
// https://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html
// These functions encode and decode little endian uint16 and uint32 values.

Wyświetl plik

@ -1,5 +1,3 @@
/* == nightwalker-87: TODO: CONTENT AND USE OF THIS SOURCE FILE IS TO BE VERIFIED (07.06.2023) == */
/*
* Copyright (c) 2010 "Capt'ns Missing Link" Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
@ -84,17 +82,21 @@
#define __USE_GNU
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <assert.h>
// #include <sys/types.h>
#include <stlink.h>
#include "logging.h"
#include "sg.h"
#include "commands.h"
#include "logging.h"
#include "register.h"
#include "usb.h"
// #include <stlink.h>
#define STLINK_OK 0x80
#define STLINK_FALSE 0x81

Wyświetl plik

@ -1,5 +1,3 @@
/* == nightwalker-87: TODO: CONTENT AND USE OF THIS HEADER FILE IS TO BE VERIFIED (07.06.2023) == */
/*
* File: sg.h
*
@ -12,6 +10,7 @@
#include <stdint.h>
#include <stlink.h>
#include "libusb_settings.h"
/* Device access */

Wyświetl plik

@ -4,28 +4,31 @@
* USB commands & interaction with ST-LINK devices
*/
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined(_MSC_VER)
#include <sys/time.h>
#endif
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#if defined(_WIN32)
#include <win32_socket.h>
#endif
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <unistd.h>
#include <stlink.h>
#include "helper.h"
#include "usb.h"
#include "commands.h"
#include "logging.h"
#include "register.h"
enum SCSI_Generic_Direction {SG_DXFER_TO_DEV = 0, SG_DXFER_FROM_DEV = 0x80};
static inline uint32_t le_to_h_u32(const uint8_t* buf) {

Wyświetl plik

@ -7,10 +7,8 @@
#ifndef USB_H
#define USB_H
#include <stdbool.h>
#include <stdint.h>
#include <stlink.h>
#include "libusb_settings.h"
#include "logging.h"