Skip to main content

PyDfuUtil - Pure python fork of dfu-util wrappers to libusb

license pypi py-versions coverage

Table of contents

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).
  • Tracks the master branch of the upstream dfu-util C sources for feature/behavioral parity — see docs/SYNC_WITH_C_UPSTREAM_BACKLOG.md for the itemized audit against it.

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
  • libusb-package (a bundled, platform-independent libusb binary) is an optional dependency, installed via the libusb extra (pip install pydfuutil[libusb]). Install it on platforms that don't already provide a system libusb; otherwise PyDFUUtil falls back to PyUsb's own system libusb search.

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.

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) use flags | DFU_IFF_DFU where flags & DFU_IFF_DFU was clearly intended; ORing with the nonzero DFU_IFF_DFU bit is always truthy, so the guard can never actually raise "Device is not in DFU mode". Left as-is for parity with upstream.
  • -w/--wait argument-arity inconsistency in upstream's long-option table. Upstream declares --wait as taking a required argument in its getopt_long table, but the short option string and the actual handler never read one. This port's -w/--wait is 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

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!

Metadata

Release files for pydfuutil 0.11.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydfuutil 0.11.4
File Size Uploaded
pydfuutil-0.11.4.tar.gz 133.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydfuutil 0.11.4
File Interpreter ABI Platform
pydfuutil-0.11.4-py3-none-any.whl Python 3 none any Details

Total release size: 200.9 kB

Release files / pydfuutil-0.11.4.tar.gz

Download URL pydfuutil-0.11.4.tar.gz
Size 133.9 kB
Tags Source
SHA-256 checksum
How to use checksums
bae0353cc3c866304a9d65ef14599299314995c683ce2dd754706f813f86bd01
BLAKE2b-256 checksum
How to use checksums
ded1878fcd44b149569f0952ecbf9786ca994b4ea4c1a81e31cd0a136fe07e00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","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}

Release files / pydfuutil-0.11.4-py3-none-any.whl

Download URL pydfuutil-0.11.4-py3-none-any.whl
Size 67.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ad8370683e5eacde7f9bbdb1f10a74b1ce8c46af5848725b5e89fa1db95fba36
BLAKE2b-256 checksum
How to use checksums
1e69fb615af8eb0f98199f8705f7a7d9c2843e3e69f447af517e0cf929568b1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","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}
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page