diff --git a/SECURITY.md b/SECURITY.md
index 9ace47066..054641822 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -7,7 +7,7 @@ The following versions of the stlink toolset are currently being supported.
#include
+#include
#include
#include
diff --git a/src/st-util/gdb-server.c b/src/st-util/gdb-server.c
index 8a187fb24..d001395e9 100644
--- a/src/st-util/gdb-server.c
+++ b/src/st-util/gdb-server.c
@@ -36,6 +36,7 @@
#include
#include
#include
+#include
#include
#include
diff --git a/src/st-util/semihosting.c b/src/st-util/semihosting.c
index a200a7408..8e9828ce8 100644
--- a/src/st-util/semihosting.c
+++ b/src/st-util/semihosting.c
@@ -8,9 +8,10 @@
#include
#include
+#include "semihosting.h"
#include
-#include "semihosting.h"
+#include
static int32_t mem_read_u8(stlink_t *sl, uint32_t addr, uint8_t *data) {
int32_t offset = addr % 4;
diff --git a/src/stlink-gui/gui.c b/src/stlink-gui/gui.c
index 4b312c8a1..e3eec34b8 100644
--- a/src/stlink-gui/gui.c
+++ b/src/stlink-gui/gui.c
@@ -9,6 +9,7 @@
#include
#include
+#include
#include
#define MEM_READ_SIZE 1024
diff --git a/src/stlink-lib/calculate.c b/src/stlink-lib/calculate.c
index 027239694..a7344e0b0 100644
--- a/src/stlink-lib/calculate.c
+++ b/src/stlink-lib/calculate.c
@@ -10,6 +10,7 @@
#include "calculate.h"
#include "common_flash.h"
+#include "read_write.h"
uint32_t calculate_F4_sectornum(uint32_t flashaddr) {
uint32_t offset = 0;
diff --git a/src/stlink-lib/common.c b/src/stlink-lib/common.c
index 70ecdd3a2..dbfcc8b9e 100644
--- a/src/stlink-lib/common.c
+++ b/src/stlink-lib/common.c
@@ -25,6 +25,7 @@
#include "logging.h"
#include "map_file.h"
#include "md5.h"
+#include "read_write.h"
#include "register.h"
#include "usb.h"
@@ -808,20 +809,20 @@ int32_t stlink_fread(stlink_t *sl, const char *path, bool is_ihex, stm32_addr_t
}
// 300
-int32_t write_buffer_to_sram(stlink_t *sl, flash_loader_t *fl, const uint8_t *buf, uint32_t size) {
+int32_t write_buffer_to_sram(stlink_t *sl, flash_loader_t *fl, const uint8_t *buf, uint16_t size) {
// write the buffer right after the loader
int32_t ret = 0;
- uint32_t chunk = size & ~0x3;
- uint32_t rem = size & 0x3;
+ uint16_t chunk = size & ~0x3;
+ uint16_t rem = size & 0x3;
if (chunk) {
memcpy(sl->q_buf, buf, chunk);
- ret = stlink_write_mem32(sl, fl->buf_addr, (uint16_t)chunk);
+ ret = stlink_write_mem32(sl, fl->buf_addr, chunk);
}
if (rem && !ret) {
memcpy(sl->q_buf, buf + chunk, rem);
- ret = stlink_write_mem8(sl, (fl->buf_addr) + chunk, (uint16_t)rem);
+ ret = stlink_write_mem8(sl, (fl->buf_addr) + chunk, rem);
}
return (ret);
diff --git a/src/stlink-lib/common_flash.c b/src/stlink-lib/common_flash.c
index 885be9c74..3038b53e7 100644
--- a/src/stlink-lib/common_flash.c
+++ b/src/stlink-lib/common_flash.c
@@ -18,6 +18,7 @@
#include "logging.h"
#include "map_file.h"
#include "md5.h"
+#include "read_write.h"
#define DEBUG_FLASH 0
diff --git a/src/stlink-lib/flash_loader.c b/src/stlink-lib/flash_loader.c
index 0f12c3131..e1e9e9995 100644
--- a/src/stlink-lib/flash_loader.c
+++ b/src/stlink-lib/flash_loader.c
@@ -16,6 +16,7 @@
#include "common_flash.h"
#include "helper.h"
#include "logging.h"
+#include "read_write.h"
#include "register.h"
#define FLASH_REGS_BANK2_OFS 0x40
diff --git a/src/stlink-lib/map_file.c b/src/stlink-lib/map_file.c
index e8c4b71a3..118c80d34 100644
--- a/src/stlink-lib/map_file.c
+++ b/src/stlink-lib/map_file.c
@@ -15,6 +15,8 @@
#include
#include "map_file.h"
+#include "read_write.h"
+
#ifndef O_BINARY
#define O_BINARY 0
#endif
diff --git a/src/stlink-lib/option_bytes.c b/src/stlink-lib/option_bytes.c
index c84ace348..ee03dced9 100644
--- a/src/stlink-lib/option_bytes.c
+++ b/src/stlink-lib/option_bytes.c
@@ -16,6 +16,7 @@
#include "logging.h"
#include "map_file.h"
#include "md5.h"
+#include "read_write.h"
/**
* Read option control register F0
diff --git a/src/stlink-lib/read_write.c b/src/stlink-lib/read_write.c
index 9149080af..4e43d8aa5 100644
--- a/src/stlink-lib/read_write.c
+++ b/src/stlink-lib/read_write.c
@@ -1,8 +1,15 @@
+/*
+ * File: read_write.c
+ *
+ * Read and write operations
+ */
+
#include
#include
#include
#include
+#include "read_write.h"
#include "logging.h"
@@ -10,11 +17,8 @@
// https://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html
// These functions encode and decode little endian uint16 and uint32 values.
-void write_uint32(unsigned char *buf, uint32_t ui) {
- buf[0] = ui;
- buf[1] = ui >> 8;
- buf[2] = ui >> 16;
- buf[3] = ui >> 24;
+uint16_t read_uint16(const unsigned char *c, const int32_t pt) {
+ return ((uint16_t)c[pt]) | ((uint16_t)c[pt + 1] << 8);
}
void write_uint16(unsigned char *buf, uint16_t ui) {
@@ -27,8 +31,11 @@ uint32_t read_uint32(const unsigned char *c, const int32_t pt) {
((uint32_t)c[pt + 2] << 16) | ((uint32_t)c[pt + 3] << 24);
}
-uint16_t read_uint16(const unsigned char *c, const int32_t pt) {
- return ((uint16_t)c[pt]) | ((uint16_t)c[pt + 1] << 8);
+void write_uint32(unsigned char *buf, uint32_t ui) {
+ buf[0] = ui;
+ buf[1] = ui >> 8;
+ buf[2] = ui >> 16;
+ buf[3] = ui >> 24;
}
int32_t stlink_read_debug32(stlink_t *sl, uint32_t addr, uint32_t *data) {
@@ -46,26 +53,26 @@ int32_t stlink_write_debug32(stlink_t *sl, uint32_t addr, uint32_t data) {
return sl->backend->write_debug32(sl, addr, data);
}
-int32_t stlink_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
- DLOG("*** stlink_write_mem32 %u bytes to %#x\n", len, addr);
+int32_t stlink_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
+ DLOG("*** stlink_read_mem32 ***\n");
- if (len % 4 != 0) {
+ if (len % 4 != 0) { // !!! never ever: fw gives just wrong values
ELOG("Data length doesn't have a 32 bit alignment: +%d byte.\n", len % 4);
return (-1);
}
- return (sl->backend->write_mem32(sl, addr, len));
+ return (sl->backend->read_mem32(sl, addr, len));
}
-int32_t stlink_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
- DLOG("*** stlink_read_mem32 ***\n");
+int32_t stlink_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
+ DLOG("*** stlink_write_mem32 %u bytes to %#x\n", len, addr);
- if (len % 4 != 0) { // !!! never ever: fw gives just wrong values
+ if (len % 4 != 0) {
ELOG("Data length doesn't have a 32 bit alignment: +%d byte.\n", len % 4);
return (-1);
}
- return (sl->backend->read_mem32(sl, addr, len));
+ return (sl->backend->write_mem32(sl, addr, len));
}
int32_t stlink_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len) {
@@ -73,21 +80,6 @@ int32_t stlink_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len) {
return (sl->backend->write_mem8(sl, addr, len));
}
-int32_t stlink_read_all_regs(stlink_t *sl, struct stlink_reg *regp) {
- DLOG("*** stlink_read_all_regs ***\n");
- return (sl->backend->read_all_regs(sl, regp));
-}
-
-int32_t stlink_read_all_unsupported_regs(stlink_t *sl, struct stlink_reg *regp) {
- DLOG("*** stlink_read_all_unsupported_regs ***\n");
- return (sl->backend->read_all_unsupported_regs(sl, regp));
-}
-
-int32_t stlink_write_reg(stlink_t *sl, uint32_t reg, int32_t idx) {
- DLOG("*** stlink_write_reg\n");
- return (sl->backend->write_reg(sl, reg, idx));
-}
-
int32_t stlink_read_reg(stlink_t *sl, int32_t r_idx, struct stlink_reg *regp) {
DLOG("*** stlink_read_reg\n");
DLOG(" (%d) ***\n", r_idx);
@@ -100,6 +92,11 @@ int32_t stlink_read_reg(stlink_t *sl, int32_t r_idx, struct stlink_reg *regp) {
return (sl->backend->read_reg(sl, r_idx, regp));
}
+int32_t stlink_write_reg(stlink_t *sl, uint32_t reg, int32_t idx) {
+ DLOG("*** stlink_write_reg\n");
+ return (sl->backend->write_reg(sl, reg, idx));
+}
+
int32_t stlink_read_unsupported_reg(stlink_t *sl, int32_t r_idx,
struct stlink_reg *regp) {
int32_t r_convert;
@@ -145,3 +142,13 @@ int32_t stlink_write_unsupported_reg(stlink_t *sl, uint32_t val, int32_t r_idx,
return (sl->backend->write_unsupported_reg(sl, val, r_convert, regp));
}
+
+int32_t stlink_read_all_regs(stlink_t *sl, struct stlink_reg *regp) {
+ DLOG("*** stlink_read_all_regs ***\n");
+ return (sl->backend->read_all_regs(sl, regp));
+}
+
+int32_t stlink_read_all_unsupported_regs(stlink_t *sl, struct stlink_reg *regp) {
+ DLOG("*** stlink_read_all_unsupported_regs ***\n");
+ return (sl->backend->read_all_unsupported_regs(sl, regp));
+}
diff --git a/src/stlink-lib/read_write.h b/src/stlink-lib/read_write.h
new file mode 100644
index 000000000..b9ca082c8
--- /dev/null
+++ b/src/stlink-lib/read_write.h
@@ -0,0 +1,27 @@
+/*
+ * File: read_write.h
+ *
+ * Read and write operations
+ */
+
+#ifndef READ_WRITE_H
+#define READ_WRITE_H
+
+uint16_t read_uint16(const unsigned char *c, const int32_t pt);
+void write_uint16(unsigned char *buf, uint16_t ui);
+uint32_t read_uint32(const unsigned char *c, const int32_t pt);
+void write_uint32(unsigned char *buf, uint32_t ui);
+
+int32_t stlink_read_debug32(stlink_t *sl, uint32_t addr, uint32_t *data);
+int32_t stlink_write_debug32(stlink_t *sl, uint32_t addr, uint32_t data);
+int32_t stlink_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len);
+int32_t stlink_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len);
+int32_t stlink_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len);
+int32_t stlink_read_reg(stlink_t *sl, int32_t r_idx, struct stlink_reg *regp);
+int32_t stlink_write_reg(stlink_t *sl, uint32_t reg, int32_t idx);
+int32_t stlink_read_unsupported_reg(stlink_t *sl, int32_t r_idx, struct stlink_reg *regp);
+int32_t stlink_write_unsupported_reg(stlink_t *sl, uint32_t value, int32_t r_idx, struct stlink_reg *regp);
+int32_t stlink_read_all_regs(stlink_t *sl, struct stlink_reg *regp);
+int32_t stlink_read_all_unsupported_regs(stlink_t *sl, struct stlink_reg *regp);
+
+#endif // READ_WRITE_H
diff --git a/src/stlink-lib/sg.c b/src/stlink-lib/sg.c
index b744389ba..d9bd8ab6a 100644
--- a/src/stlink-lib/sg.c
+++ b/src/stlink-lib/sg.c
@@ -95,6 +95,7 @@
#include "commands.h"
#include "logging.h"
+#include "read_write.h"
#include "register.h"
#include "usb.h"
// #include // TODO: Check use
diff --git a/src/stlink-lib/usb.c b/src/stlink-lib/usb.c
index 0934fe368..30134e161 100644
--- a/src/stlink-lib/usb.c
+++ b/src/stlink-lib/usb.c
@@ -27,6 +27,7 @@
#include "commands.h"
#include "logging.h"
+#include "read_write.h"
#include "register.h"
static inline uint32_t le_to_h_u32(const uint8_t* buf) {
diff --git a/tests/sg.c b/tests/sg.c
index 67d804b23..1284bb15b 100644
--- a/tests/sg.c
+++ b/tests/sg.c
@@ -8,6 +8,7 @@
#include
#include
+#include
#include
#if defined(_MSC_VER)
diff --git a/tests/usb.c b/tests/usb.c
index 5dbb1d83c..f8b60c306 100644
--- a/tests/usb.c
+++ b/tests/usb.c
@@ -6,6 +6,7 @@
#include
+#include
#include
#include