Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
src/*.o
tiny-AES-c/*.o
stlink-tool
stlink-tool
stlink-tool.exe
/.vscode
22 changes: 18 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
CFLAGS := -Wall -Wextra -Werror $(shell pkg-config --cflags libusb-1.0)
LDFLAGS := $(shell pkg-config --libs libusb-1.0)

OBJS := src/main.o src/stlink.o src/crypto.o tiny-AES-c/aes.o
ifeq ($(OS),Windows_NT)
ifeq ($(MINGW_CHOST),i686-w64-mingw32)
LIBARCH := _32
endif
ifeq ($(MINGW_CHOST),x86_64-w64-mingw32)
LIBARCH := _64
endif
LIBARCH ?=
CC := gcc.exe
CFLAGS := -DWINDOWS -Wall -Wextra -Werror -Wno-unused-parameter -Wno-error=unused-parameter -Ilibusb
LDFLAGS := -Llibusb -lusb-1.0$(LIBARCH) -lWs2_32 -lmsvcrt
OBJS := src/main.o src/getopt.o src/stlink.o src/crypto.o tiny-AES-c/aes.o
else
CFLAGS := -Wall -Wextra -Werror -Wno-unused-parameter -Wno-error=unused-parameter $(shell pkg-config --cflags libusb-1.0) -g -Og
LDFLAGS := $(shell pkg-config --libs libusb-1.0)
OBJS := src/main.o src/stlink.o src/crypto.o tiny-AES-c/aes.o
endif

%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $<
Expand All @@ -11,4 +24,5 @@ stlink-tool: $(OBJS)

clean:
rm -f src/*.o
rm -f tiny-AES-c/*.o
rm -f stlink-tool
121 changes: 111 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,124 @@
# stlink-tool

libusb tool for flashing chinese ST-Link dongles. Please note that similarly to ST's updater, the uploaded firmware won't replace the bootloader (meaning that you should be able to reflash the original afterwards using [ST's firmware update utility](http://www.st.com/en/development-tools/stsw-link007.html)).
libusb tool for flashing ST-Link dongles. Please note that similarly to ST's updater, the uploaded firmware won't replace the bootloader (meaning that you should be able to reflash the original afterwards using [ST's firmware update utility](http://www.st.com/en/development-tools/stsw-link007.html)).

```
Usage: ./stlink-tool [options] firmware.bin
Usage: stlink-tool.exe [options] [firmware.bin]
Options:
-p Probe the ST-Link adapter
-h Show help
-h, --help Show help
-p, --probe Probe the ST-Link adapter
-d, --decrypt KEY Decrypt Firmware using KEY. Pass "" to use internal key.
-sd, --save_dec Save decripted firmware as filename + .dec
-t, --st_type TYPE Change STLink type to TYPE.
A for "STM32 Debugger+Audio"
B for "STM32 Debug+Mass storage+VCP"
E for "STM32 Debug+Mass storage+VCP"
F for "STM8/STM32 Debug+Mass storage+VCP+Bridge" (for STLink V3)
G for "STM8 Debug+Mass storage+VCP"
J for "STM32 Debugger"
M for "STM8/STM32 Debugger"
S for "STM8 Debugger"
-v, --ver S.J.X Change reported STLink sersion.
S is STLink version, J is JTAG version,
X is SWIM or MSD version.
-f, --fix Flash Anti-Clone Tag and Firmware Exists/EOF Tag

Options for Modifying Device Config (Only for STLink v2 and up):
--usb_cur CURRENT Set the MaxPower reported in USB Descriptor
to CURRENT(mA)
--msd_name VOLUME Set the volsume name of the MSD drive to VOLUME.
--mbed_name NAME Set the MBED board name to NAME.
--dfu_opt OPT Set DFU Options to OPT.
OPT is the Decimal value of Bit Field:
bit1: "No Power Off"
bit2: "Autostart"
--dynamic_opt OPT Set Dynamic Option to OPT.
'V': MSD Off
'M': MSD On
'W': MSD Always Off
--mco_out OPT Set MCO Output to OPT. OPT is the Hex value of:
Lower Nybble(MCO Source):
0: None
1: HSI
2: HSE
3: PLL
Upper Nybble (Divider):
Divider - 1 (Valid Divider 1-5)
--startup OPT Set Startup Preferences to OPT.
0: High Power
1: Balanced
2: Low Power
3: Default
To remove a configuration you can use the "" argument with the option
(Ex. --usb_cur "") or prefix the option with rm_ (Ex. --rm_usb_cur).

Application in Flash is started when called without argument, after firmware
load or configuration change.
```

stlink-tool has been tested under Debian based Linux and Windows x86/x64.

## New stlink-tool Features

* show ST-Link dongle information
* can show and modify device configuration (show is only for ST-Link V2.1)
* can modify STLink type and reported firmware version
* can add "Anti-Clone" Tag and "Firmware Flashed/EOF" Tag (to make flashed firmware bootable without needing to exit DFU on V2.1)
* can decrypt and flash firmwares taken from `STLinkUpgrade.jar`

Examples:

STLinkV2

```
STLinkV2 Bootloader Found
STLink Type: M [STM8/STM32 Debugger]
Firmware Version: V2J38S7

Bootloader PID: 3748
Reported Flash Size: 64KB

STLink ID: ************************
Firmware Encryption Key: ********************************
Anti-Clone Key: ********************************
Current Mode: 1
```

STLinkV2-1

```
Trying to switch STLINK/Application to bootloader
STLinkV2-1 Bootloader Found
STLink Type: B [STM32 Debug+Mass storage+VCP]
Firmware Version: V2J38M27

Current Device Configuration:
USB Current: [300mA]

Bootloader PID: 3748
HW Version: V2.0 Flags: 0x000000
Reported Flash Size: 128KB

STLink ID: ************************
Firmware Encryption Key: ********************************
Anti-Clone Key: ********************************
Current Mode: 2
```

This changes were done by referencing:
[GMMan/st-link-hack](https://github.com/GMMan/st-link-hack)
[sakana280's fork](https://github.com/sakana280/stlink-tool)

## Compiling

Required dependencies :

* C compiler (both clang and gcc seems to work great)
* libusb1
* git
* C compiler (both clang and gcc seems to work great)
* libusb1
* git

```
git clone https://github.com/jeanthom/stlink-tool
git clone -b stlinkv21 https://github.com/GabyPCgeeK/stlink-tool.git
cd stlink-tool
git submodule init
git submodule update
Expand All @@ -32,5 +132,6 @@ make
ST's firmware upload protocol is USB's DFU protocol with some twists. Every DFU command is issued with the 0xF3 prefix, and the command set does not exactly match USB's.

Some documentation :
* http://www.st.com/content/ccc/resource/technical/document/application_note/6a/17/92/02/58/98/45/0c/CD00264379.pdf/files/CD00264379.pdf/jcr:content/translations/en.CD00264379.pdf
* http://www.usb.org/developers/docs/devclass_docs/DFU_1.1.pdf

* <http://www.st.com/content/ccc/resource/technical/document/application_note/6a/17/92/02/58/98/45/0c/CD00264379.pdf/files/CD00264379.pdf/jcr:content/translations/en.CD00264379.pdf>
* <http://www.usb.org/developers/docs/devclass_docs/DFU_1.1.pdf>
Loading