dji-firmware-tools icon indicating copy to clipboard operation
dji-firmware-tools copied to clipboard

Using the tools on Windows with MinGW from MSYS2

Open mefistotelis opened this issue 5 years ago • 1 comments

There are many Python version for Windows; here you will see how to install one which is definitely not the easiest, but it comes with a large collection of Unix tools which are very useful for everyday tasks - MinGW.

Minimal GNU for Windows is included in MSYS2 packages; MSYS2 is basically a wrapper containing MinGW in 32-bit and 64-bit versions, plus some tools from Cygwin. We will only use the MinGW64 part of MSYS2.

# download msys2-x86_64 from https://www.msys2.org/
# install msys2
# from start menu, run "MSYS2 MinGW 64-bit".
# in the terminal which will open, type the commands to install required packages:

pacman -Sy mingw-w64-x86_64-wget mingw-w64-x86_64-zlib mingw-w64-x86_64-pcre2
pacman -Sy mingw-w64-x86_64-python3
pacman -Sy mingw-w64-x86_64-toolchain
pacman -Sy mingw-w64-x86_64-cmake mingw-w64-x86_64-openssl
pacman -Sy git subversion mercurial
pacman -Sy tar unzip
pacman -Sy mingw-w64-x86_64-python3-pip
pacman -Sy mingw-w64-x86_64-python3-pyopenssl mingw-w64-x86_64-python3-cryptography

# Add serial port support (for the service tool):
pip3 install pyserial
# Add Crypto module (for fw extractors):
pip3 install pycrypto

# Now install assembler and disassembler (for hardcoder tools):
pacman -Sy mingw-w64-x86_64-python3-capstone
pacman -Sy mingw-w64-x86_64-python3-keystone-engine

If all commands ended with success - that's the end. But most likely that is not the case:

# at the moment I'm writing this, there is no keystone-engine package - the command to install it
#  will fail; instead, we need to install it via pip:

pip3 install keystone-engine

# but the pip command may also fail - due to lack of prebuilds in the pip package
# (though error message will say something about missing '.so' file, or missing 'cl' compiler);
# do NOT try to install 'keystone' package instead - that is a completely different library.
# In case of the installation fail, we need to add the prebuilds manually to the package:

# Go to http://www.keystone-engine.org/download/ and download archives under "Windows - Core engine"
# These are prebuilt libraries. In my case, the downloads were:
wget https://github.com/keystone-engine/keystone/releases/download/0.9.1/keystone-0.9.1-win32.zip
wget https://github.com/keystone-engine/keystone/releases/download/0.9.1/keystone-0.9.1-win64.zip

# Also get keystone sources, as we will need python bindings from them:
git clone https://github.com/keystone-engine/keystone.git

# Now, extract the prebuilds:
find . -maxdepth 1 -name 'keystone-*-win*.zip' | xargs -I {} unzip {}
# Add prebuilds to keystone sources
cd keystone/bindings/python
mkdir ./prebuilt ./prebuilt/win32 ./prebuilt/win64
cp ../../../keystone-*-win32/keystone.dll ./prebuilt/win32/
cp ../../../keystone-*-win64/keystone.dll ./prebuilt/win64/
# Make sure prebuilds are included in the package
grep "prebuilt" MANIFEST.in
# There should be one displayed, similar to "recursive-include prebuilt *"
# If no lines show, add the line to MANIFEST.in
echo "recursive-include prebuilt *.dll" | tee -a MANIFEST.in
# Make sure prebuilts will not be removed when re-creating the bindings
# Most 'make' targets remove the the prebuilds, and bindings setup calls one such target
# Comment out all 'make' calls in setup.py
sed -i 's/^\([ \t]*\)\(os.system[\(]'\''make .\+'\''[\)]\)$/\1#\2/' setup.py
# Now it's time to re-pack the bindings
python3 setup.py sdist
# install our generated package
pip3 install ./dist/keystone-engine-*.tar.gz
# get back to previous directory
cd ../../..

If after installation the FW decryptor claims wincrypto is not installed, you need to replace one line in the file:

sed -i 's/^import winrandom/from . import winrandom/' /mingw64/lib/python3.8/site-packages/Crypto/Random/OSRNG/nt.py

That one line change should fix the issue.

To convert ELF files back to BIN after modifications, you will need arm-none-eabi toochain. I don't have a verified source for it - try searching (I just used an old one I had).

UPDATE 2020-09-21 the msys2 keystone package deteriorated completely, so no longer using it in the instructions.

UPDATE 2021-11-14 the instruction is still valid - works on latest msys2 and latest keystone (v0.9.2).

mefistotelis avatar Dec 13 '18 23:12 mefistotelis

hi help me pls P3X_FW_V01.07.0060.bin I want to open files

edoman35 avatar May 26 '19 00:05 edoman35