PyDfuUtil - Pure python fork of dfu-util wrappers to libusb
Project description
PyDfuUtil - Pure python fork of dfu-util wrappers to libusb
Table of contents
- Introduction
- Requirements
- Installing
- Usage
- Library usage
- Todos
- Known bugs and workarounds
- Getting help
- About
- Footnotes
Introduction
- PyDFUUtil provides for easy access to the devices that supports DFU interface over host machine's Universal Serial Bus (USB) system for Python 3.
- PyDFUUtil is an open realisation of original dfu-util and thin wrapper over libusb (uses PyUsb library as a backend).
[!WARNING] The current status of the project is BETA version. Use it for your own risk
[!IMPORTANT] Current version implements but not tested with real
dfusedevices!
[!TIP] Searching for contributors for testing the library
Requirements and platform support
- Since PyDFUUtil uses the libusb library it has similar dependencies for using libusb
- PyDFUUtil primarily tested on Linux and Windows, but also can work on each platform where PyUsb library are available, including MacOS
Installing
PyDFUUtil is generally installed through pip
# the latest official release
python -m pip install pydfuutil
# install a specific version (e.g. 0.0.1b1)
python -m pip install pydfuutil==0.0.1b1
Usage
pydfuutil
pydfuutil -h
# or
python -m pydfuutil -h
####### usage:
usage: pydfuutil [-h] [-V] [-v] [-l] [-e] [-E <seconds>]
[-d <vid>:<pid>[,<vid_dfu>:<pid_dfu>]] [-n <dnum>] [-p <bus-port. ... .port>]
[-c <config_nr>] [-i <intf_nr>] [-S <serial_str>[,<serial_str_dfu>]]
[-a <alt>] [-t <size>] [-U <file>] [-Z <bytes>] [-D <file>] [-R] [-w]
[-s <address><:...>]
Python implementation of DFU-Util tools
options:
-h, --help show this help message and exit
-V, --version Print the version number
-v, --verbose Print verbose debug statements
-l, --list List the currently attached DFU capable USB devices
-e, --detach Detach the currently attached DFU capable USB devices
-E <seconds>, --detach-delay <seconds>
Time to wait before reopening a device after detach
-d <vid>:<pid>[,<vid_dfu>:<pid_dfu>], --device <vid>:<pid>[,<vid_dfu>:<pid_dfu>]
Specify Vendor/Product ID(s) of DFU device
-n <dnum>, --devnum <dnum>
Match given device number (devnum from --list)
-p <bus-port. ... .port>, --path <bus-port. ... .port>
Specify path to DFU device
-c <config_nr>, --cfg <config_nr>
Specify the Configuration of DFU device
-i <intf_nr>, --intf <intf_nr>
Specify the DFU Interface number
-S <serial_str>[,<serial_str_dfu>], --serial <serial_str>[,<serial_str_dfu>]
Specify Serial String of DFU device
-a <alt>, --alt <alt>
Specify the Altsetting of the DFU Interface
-t <size>, --transfer-size <size>
Specify the number of bytes per USB Transfer
-U <file>, --upload <file>
Read firmware from device into <file>
-Z <bytes>, --upload-size <bytes>
Read firmware from device into <file>
-D <file>, --download <file>
Read firmware from device into <file>
-R, --reset Issue USB Reset signalling once we`re finished
-w, --wait Wait for device to appear
-s <address><:...>, --dfuse-address <address><:...>
ST DfuSe mode string, specifying target
address for raw file download or upload
(not applicable for DfuSe file (.dfu) downloads).
Add more DfuSe options separated with ':'
leave
Leave DFU mode (jump to application)
mass-erase
Erase the whole device (requires "force")
unprotect
Erase read protected device (requires "force")
will-reset
Expect device to reset (e.g. option bytes write)
force
You really know what you are doing!
<length>
Length of firmware to upload from device
pydfuutil-suffix
pydfuutil-suffix -h
# or
python -m pydfuutil.suffix -h
####### usage:
usage: dfu-suffix [-h] [-V] (-c | -a | -D) [-p <productID>] [-v <vendorID>] [-d <deviceID>] [-s <address>] [-T] <file>
positional arguments:
<file> Target filename
options:
-h, --help Print this help message
-V, --version Print the version number
-c, --check Check DFU suffix of <file>
-a, --add Add DFU suffix to <file>
-D, --delete Delete DFU suffix from <file>
-p <productID>, --pid <productID>
Add product ID into DFU suffix in <file>
-v <vendorID>, --vid <vendorID>
Add vendor ID into DFU suffix in <file>
-d <deviceID>, --did <deviceID>
Add device ID into DFU suffix in <file>
-s <address>, --stellaris-address <address>
Specify lmdfu address for LMDFU_ADD
-T, --stellaris Set lmdfu mode to LMDFU_CHECK
pydfuutil-prefix
pydfuutil-prefix -h
# or
python -m pydfuutil.prefix -h
####### usage:
usage: pydfuutil-prefix [-h] [-V] (-c | -D | -a) [-s <address>] [-T]
[-L]
<file>
positional arguments:
<file> Target filename
options:
-h, --help show this help message and exit
-V, --version Print the version number
-c, --check Check DFU suffix of <file>
-D, --delete Delete DFU suffix from <file>
-a, --add Add DFU suffix to <file>
In combination with -a:
-s <address>, --stellaris-address <address>
Add TI Stellaris address prefix to <file>
In combination with -a or -D or -c:
-T, --stellaris Act on TI Stellaris address prefix of <file>
In combination with -a or -D or -c:
-L, --lpc-prefix Use NXP LPC DFU prefix format
pydfuutil-lsusb
pydfuutil-lsusb -h
# or
python -m pydfuutil.lsusb -h
####### usage:
usage: pydfuutil-prefix [-v] [-s [[bus]:][devnum]]
[-d vendor:[product]] [-D device] [-t] [-V]
[-h]
options:
-v, --verbose Increase verbosity (show descriptors)
-s [[bus]:][devnum] Show only devices with specified device and/or
bus numbers (in decimal)
-d vendor:[product] Show only devices with the specified vendor
and product ID numbers (in hexadecimal)
-D device Selects which device lsusb will examine by
UNIX-like path simulate
-t, --tree Simulate UNIX-like physical USB device
hierarchy
-V, --version Print the version number
-h, --help Show this help message and exit
Library usage
PyDFUUtil isn't just the four pydfuutil-* CLI entry points above — each of them is a thin
argparse wrapper around a plain, importable Python API in the pydfuutil package. The CLI
tools' own source (pydfuutil/__main__.py, suffix.py, prefix.py, lsusb.py) is itself the
most complete, always-up-to-date reference for driving the API directly.
Inspecting a DFU file
from pydfuutil.dfu_file import DfuFile, SuffixReq, PrefixReq
file = DfuFile(name="firmware.dfu")
file.load(SuffixReq.MAYBE_SUFFIX, PrefixReq.MAYBE_PREFIX)
print(f"Vendor: 0x{file.idVendor:04x}")
print(f"Product: 0x{file.idProduct:04x}")
print(f"Size: {file.size.total} bytes")
file.show_suffix_and_prefix() # same output as `pydfuutil-suffix -c`/`pydfuutil-prefix -c`
Listing attached DFU-capable devices
import usb.core
from pydfuutil.dfu_util import probe_devices, list_dfu_interfaces
ctx = list(usb.core.find(find_all=True))
probe_devices(ctx) # populates DfuUtil.dfu_root as a side effect
list_dfu_interfaces() # prints the same table as `pydfuutil -l`
Uploading/downloading firmware
The full runtime→DFU-mode transition, device claiming, and the plain-DFU/DfuSe upload/download
dispatch (pydfuutil.dfu, pydfuutil.dfu_load, pydfuutil.dfuse) are involved enough — and
device/quirk-dependent enough — that pydfuutil/__main__.py::main() is the canonical, tested
example to copy from rather than a shortened README snippet that could drift out of sync with it.
[!TIP] Every module in
pydfuutil/docstrings/type-hints its public functions;import pydfuutil.dfu,pydfuutil.dfu_load,pydfuutil.dfuse, andpydfuutil.quirksand read the correspondingdo_upload/do_downloadfunctions alongsidemain.c/dfuse.cin the upstream dfu-util source if you're embedding this in your own tool.
Done:
- dfu
- dfu_file
- dfu_load
- portable
- quirks
- suffix
- usb_dfu
- lmdfu
- dfuse_mem
- dfuse
- dfu-util
Todo
- Update sources to latest original version "dfu-util-0.11"
Known bugs and workarounds
PyDFUUtil is regularly audited function-by-function against the upstream C dfu-util source
to catch behavioral divergences introduced during the port; the full, itemized log — including
items deliberately left unfixed with the reasoning why — lives in
docs/SYNC_WITH_C_UPSTREAM_BACKLOG.md. A couple of
findings are pre-existing bugs in upstream dfu-util itself, faithfully reproduced here rather
than "fixed" out of sync with the reference implementation:
- "Check for DFU mode device" guard is a permanent no-op. Upstream's
main.c(and this port's__main__.py) useflags | DFU_IFF_DFUwhereflags & DFU_IFF_DFUwas clearly intended; ORing with the nonzeroDFU_IFF_DFUbit is always truthy, so the guard can never actually raise "Device is not in DFU mode". Left as-is for parity with upstream. -w/--waitargument-arity inconsistency in upstream's long-option table. Upstream declares--waitas taking a required argument in itsgetopt_longtable, but the short option string and the actual handler never read one. This port's-w/--waitis a clean no-argument flag for both forms — arguably a correction rather than a regression, so no change is planned to match upstream's inconsistency here.
See the "Needs human judgment" section of the backlog doc for the full analysis of these and a couple of other borderline/rare-edge-case items.
Getting help
- To report a bug or propose a new feature, use our issue tracker. But please search the database before opening a new issue.
About
Dfu-util - Device Firmware Upgrade Utilities
Dfu-util is the host side implementation of the DFU 1.0 and DFU 1.1 specification of the USB forum.
DFU is intended to download and upload firmware to devices connected over USB. It ranges from small devices like micro-controller boards up to mobile phones. With dfu-util you are able to download firmware to your device or upload firmware from it.
dfu-util has been tested with Openmoko Neo1973 and Freerunner and many other devices.
RISK NOTICE
[!IMPORTANT] THE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
Footnotes
- On systems that still default to Python 2, replace python with python3
- Project is in develop, it fulls issues not according to original dfu-util!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydfuutil-0.11.0.tar.gz.
File metadata
- Download URL: pydfuutil-0.11.0.tar.gz
- Upload date:
- Size: 130.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a8581c059ce2247b4a41525fef3e1c8fb489575af615772d95a989f74b279c6
|
|
| MD5 |
f7eec89473efb53adaef992ad4ab584a
|
|
| BLAKE2b-256 |
5e5abb0b69d85f0fa6975d0785911102732b28f5f7fe9e9e67c942afd6cc029b
|
File details
Details for the file pydfuutil-0.11.0-py3-none-any.whl.
File metadata
- Download URL: pydfuutil-0.11.0-py3-none-any.whl
- Upload date:
- Size: 66.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fec37c1c84aaa0d6ab3c715e6a6eb308059545d19b74d7dbf8204495d08d0478
|
|
| MD5 |
7c58a70f1f0dc68938bc81e139574542
|
|
| BLAKE2b-256 |
4f7b9a78f17fbab8c6bd18063aaa1c1b679e21b7367b31da6b98ed9d9940a2e6
|