Skip to main content

Python module for communication with NXP MCU Bootloader

Project description

pyMBoot

Build Status PyPI Status Python Version

pyMBoot is an Open Source python based library for configuring and upgrading the firmware in NXP Microcontrolers via embedded MCUBOOT (MCU Bootloader). Detailed description of MCUBOOT / KBOOT key features and functionality is located here.

This project is still in developing phase. Please, test it and report founded issues.

Dependencies

  • Python >3.6 - The interpreter for Python programing language
  • Click - Python package for creating beautiful command line interface.
  • bincopy - Python package for parsing S-Record, Intel HEX and TI-TXT files.
  • easy_enum - User friendly implementation of documented Enum type for Python language.
  • PyUSB - Python package to access USB devices in Linux OS.
  • PyWinUSB - Python package that simplifies USB-HID communications on Windows OS.
  • pyserial - Python package for communication over Serial port in Linux and Windows OS.

Installation

 $ pip install mboot

To install the latest version from master branch execute in shell following command:

 $ pip install -U https://github.com/molejar/pyMBoot/archive/master.zip

In case of development, install it from cloned sources:

 $ git clone https://github.com/molejar/pyMBoot.git
 $ cd pyMBoot
 $ pip install -U -e .

NOTE: You may run into a permissions issues running these commands. Here are a few options how to fix it:

  1. Run with sudo to install pyMBoot and dependencies globally
  2. Specify the --user option to install locally into your home directory (export "~/.local/bin" into PATH variable if haven't).
  3. Run the command in a virtualenv local to a specific project working set.

For running mboot module or CLI without root privileges in Linux OS copy following udev rules 90-imx-sdp.rules into /etc/udev/rules.d directory and reload it with command: sudo udevadm control --reload-rules.

Usage

The API of mboot module is intuitive and fully reflecting the functionality described in reference manual of any supported device. It's basic usage is presented in following example.

import mboot

devices = mboot.scan_usb()

if devices:
    mb = mboot.McuBoot(devices[0])
    mb.open()
    # read 100 bytes from address 0
    data = mb.read_memory(0, 100)
    if data is None:
        print(mb.status_info)
        mb.close()
        exit()

    # other commands ...

    mb.close()

McuBoot class is supporting with statement what is eliminating the explicit call of open and close methods. The code then looks more cleaner as you can see in following example.

from mboot import scan_usb, McuBoot

devices = scan_usb()

if devices:
    with McuBoot(devices[0]) as mb:
        # read 100 bytes from address 0
        data = mb.read_memory(0, 100)
        if data is None:
            print(mb.status_info)
            exit()

        # other commands ...

If you call reset() command inside with block, the device is automatically reopened. You can skip this with explicit argument reset(reopen=False)

By default is command error propagated by return value and must be processed individually for every command. In many use-cases is code execution interrupted if any command finish with error. Therefore you have the option to enable the exception also for command error. The code is then much more readable as you can see in flowing example.

from mboot import scan_usb, McuBoot, McuBootError

devices = scan_usb()

if devices:
    try:
        with McuBoot(devices[0], True) as mb:
            # read 100 bytes from address 0
            data = mb.read_memory(0, 100)
            # other commands ...

    except McuBootError as e:
        print(str(e))

mboot module is implementing also logging functionality for easy debugging all communication interfaces. To get it working you need only import logging module and set the logging level (DEBUG or INFO) with following line of code: logging.basicConfig(level=logging.DEBUG)

import logging

logging.basicConfig(level=logging.DEBUG)

The example of terminal output with enabled logging functionality:

INFO:MBOOT:Connect: USB COMPOSITE DEVICE (0x15A2, 0x0073)
DEBUG:MBOOT:USB:Open Interface
INFO:MBOOT:CMD: ReadMemory(address=0x00000000, length=100, mem_id=0)
DEBUG:MBOOT:TX-PACKET: Tag=ReadMemory, Flags=0x00, p0=0x00000000, p1=0x00000064, p2=0x00000000
DEBUG:MBOOT:USB:OUT[64]: 01 00 20 00 03 00 00 03 00 00 00 00 64 00 00 00 00 00 00 00 00 00 00 00 00 ...
DEBUG:MBOOT:USB:IN [36]: 03 00 0C 00 A3 01 00 02 00 00 00 00 64 00 00 00 00 00 00 00 00 00 00 00 00 ...
INFO:MBOOT:RX-PACKET: Tag=ReadMemoryResponse, Status=Success, Length=100
DEBUG:MBOOT:USB:IN [36]: 04 00 20 00 00 60 00 20 C1 00 00 00 0D 85 00 00 09 01 00 00 00 00 00 00 00 ...
DEBUG:MBOOT:USB:IN [36]: 04 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 09 01 00 00 00 00 00 00 00 ...
DEBUG:MBOOT:USB:IN [36]: 04 00 20 00 09 01 00 00 09 01 00 00 09 01 00 00 09 01 00 00 09 01 00 00 09 ...
DEBUG:MBOOT:USB:IN [36]: 04 00 04 00 09 01 00 00 09 01 00 00 09 01 00 00 09 01 00 00 09 01 00 00 09 ...
DEBUG:MBOOT:USB:IN [36]: 03 00 0C 00 A0 00 00 02 00 00 00 00 03 00 00 00 09 01 00 00 09 01 00 00 09 ...
DEBUG:MBOOT:RX-PACKET: Tag=GenericResponse, Status=Success, Cmd=ReadMemory
INFO:MBOOT:CMD: Successfully Received 100 from 100 Bytes
DEBUG:MBOOT:USB:Close Interface

[ mboot ] Tool

The mboot module is distributed with command-line utility, which demonstrate the complete functionality of this library and can be used as replacement of blhos tool. If you write mboot into shell and click enter, then you get the description of its usage. For getting the help of individual commands just use mboot <command name> -?.

  $ mboot --help

    Usage: mboot [OPTIONS] COMMAND [ARGS]...

      NXP MCU Bootloader Command Line Interface, version: 0.3.0

      NOTE: Development version, be carefully with it usage !

    Options:
      -t, --target TEXT          Select target MKL27, LPC55, ... [optional]
      -d, --debug INTEGER RANGE  Debug level: 0-off, 1-info, 2-debug
      -v, --version              Show the version and exit.
      -?, --help                 Show this message and exit.

    Commands:
      call             Call code from specified address
      efuse            Read/Write eFuse from MCU
      erase            Erase MCU internal or external memory
      execute          Execute code from specified address
      fill             Fill MCU memory with specified pattern
      info             Get MCU info (mboot properties)
      keyblob          Generate the Blob for given DEK Key
      kp-enroll        Key provisioning: Enroll
      kp-gen-key       Key provisioning: Generate Intrinsic Key
      kp-read-kstore   Key provisioning: Read the key from key store area
      kp-read-nvm      Key provisioning: Read the key from nonvolatile memory
      kp-user-key      Key provisioning: Send the user key to a bootloader
      kp-write-kstore  Key provisioning: Write the key into key store area
      kp-write-nvm     Key provisioning: Write the key into nonvolatile memory
      mconf            Configure external memory
      mlist            Get list of available memories
      otp              Read/Write internal OTP segment
      read             Read data from MCU internal or external memory
      reset            Reset MCU
      resource         Flash read resource
      sbfile           Receive SB file
      unlock           Unlock MCU
      update           Copy backup app from address to main app region
      write            Write data into MCU internal or external memory

If USB device is not in known devices list, then use -t or --target argument and directly specify the device VID:PID. Example: -t 0x15A2:0x0073


$ mboot info

Read bootloader properties from connected MCU.

 $ mboot info

 DEVICE: Kinetis Bootloader (0x15A2, 0x0073)

 CurrentVersion: K1.0.0
 AvailablePeripherals:
  - UART
  - I2C-Slave
  - SPI-Slave
  - USB-HID
 FlashStartAddress: 0x00000000
 FlashSize: 256.0 kiB
 FlashSectorSize: 1.0 kiB
 FlashBlockCount: 2
 AvailableCommands:
  - FlashEraseAll
  - FlashEraseRegion
  - ReadMemory
  - FillMemory
  - FlashSecurityDisable
  - ReceiveSBFile
  - Call
  - Reset
  - SetProperty
 VerifyWrites: ON
 MaxPacketSize: 32 B
 ReservedRegions:
  - 0x1FFFF800 - 0x20000687, 3.6 kiB
 ValidateRegions: ON
 RamStartAddress: 0x1FFFE000
 RamSize: 32.0 kiB
 SystemDeviceIdent: 0x23160D82
 FlashSecurityState: Unlocked

$ mboot mlist

Get list of available memories (internal and external)

 $ mboot info

 DEVICE: Kinetis Bootloader (0x15A2, 0x0073)

 Internal Flash:
  0) 0x00000000 - 0x00040000, Size: 256.0 kiB, Sector Size: 1.0 kiB

 Internal Ram:
  0) 0x1FFFE000 - 0x20006000, Size: 32.0 kiB

$ mboot read [OPTIONS] ADDRESS [LENGTH]

Read data from MCU memory and store it into file as binary (.bin), intel-hex (.hex, .ihex) or s-record (.srec, *.s19) format. If output file is not specified, the data are dumped into stdout in readable format.

LENGTH argument is optional and as default will be used the size to end of memory

options:
  • -c, --compress - Compress dump output. (default: False)
  • -f, --file - Output file name with extension: *.bin, *.hex, *.ihex, *.srec or *.s19
  • -?, --help - Show help message and exit.
 $ mboot read 0 200

 Reading from MCU memory, please wait !

  ADDRESS | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | 0123456789ABCDEF
 -----------------------------------------------------------------------------
 00000000 | 00 60 00 20 C1 00 00 00 D9 08 00 00 09 01 00 00 | .`. ............
 00000010 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
 00000020 | 00 00 00 00 00 00 00 00 00 00 00 00 09 01 00 00 | ................
 00000030 | 00 00 00 00 00 00 00 00 09 01 00 00 09 01 00 00 | ................
 00000040 | 09 01 00 00 09 01 00 00 09 01 00 00 09 01 00 00 | ................
 00000050 | 09 01 00 00 09 01 00 00 09 01 00 00 09 01 00 00 | ................
 00000060 | 09 01 00 00 09 01 00 00 09 01 00 00 09 01 00 00 | ................
 00000070 | 09 01 00 00 09 01 00 00 09 01 00 00 09 01 00 00 | ................
 00000080 | 09 01 00 00 09 01 00 00 09 01 00 00 09 01 00 00 | ................
 00000090 | 09 01 00 00 09 01 00 00 09 01 00 00 09 01 00 00 | ................
 000000A0 | 09 01 00 00 09 01 00 00 09 01 00 00 09 01 00 00 | ................
 000000B0 | 09 01 00 00 09 01 00 00 09 01 00 00 09 01 00 00 | ................
 000000C0 | 0A 49 0B 4A 0B 4B 9B 1A                         | .I.J.K..
 -----------------------------------------------------------------------------

$ mboot write [OPTIONS] FILE

Write data from attached FILE into MCU memory.

options:
  • -a, --address - Start Address. (default: 0)
  • -o, --offset - Offset of input data. (default: 0)
  • -?, --help - Show help message and exit.
 $ mboot write blink.srec

 Wrote Successfully.

$ mboot erase [OPTIONS]

Erase MCU memory from specified address and length or complete chip.

options:
  • -m, --mass - Erase complete MCU memory.
  • -a, --address - Start Address.
  • -l, --length - Count of bytes aligned to flash block size.
  • -?, --help - Show help message and exit.
 $ mboot erase -m

 Chip Erased Successfully.

$ mboot unlock [OPTIONS]

Unlock MCU memory.

options:
  • -k, --key - Use backdoor key as ASCII = S:123...8 or HEX = X:010203...08
  • -?, --help - Show help message and exit.
 $ mboot unlock

 Chip Unlocked Successfully.

$ mboot fill [OPTIONS] ADDRESS LENGTH

Fill MCU memory with specified pattern

options:
  • -p, --pattern - Pattern format (default: 0xFFFFFFFF).
  • -?, --help - Show help message and exit.
 $ mboot fill -p 0x11111111 0x1FFFE000 10

 Filled Successfully.

$ mboot reset

MCU SW reset

 $ mboot reset

TODO

  • Implement support for UART interface

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mboot-0.3.0.tar.gz (35.5 kB view details)

Uploaded Source

Built Distribution

mboot-0.3.0-py3-none-any.whl (35.8 kB view details)

Uploaded Python 3

File details

Details for the file mboot-0.3.0.tar.gz.

File metadata

  • Download URL: mboot-0.3.0.tar.gz
  • Upload date:
  • Size: 35.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for mboot-0.3.0.tar.gz
Algorithm Hash digest
SHA256 7a122eaada4c3b425cceb9d3ba6ae562e748f2d9d7448983ec0052c04a405712
MD5 f9c330a5e2cb4211d678677497538716
BLAKE2b-256 7e44dab8bdc1cfd1a17d1940c02c9e2906b451c9a0c2954cec38ba5501ca3ce4

See more details on using hashes here.

File details

Details for the file mboot-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: mboot-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 35.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for mboot-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6e398bd95d8c883736ab16f3c5b559c1b4b1bf07a901d1dd94a0ebe1f0f62d86
MD5 29088fd90b2b131bc176fb15915df2fd
BLAKE2b-256 31df5440998b889f575232624998cb88b39c149ea8c58384c76b360ee38a0b2c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page