Skip to main content

Flattened Device Tree Python Module

Project description

Flattened Device Tree Python Module

Build Status Coverage Status PyPI Status Python Version

This python module is usable for manipulation with Device Tree Data and primary was created for i.MX Smart-Boot Tool

Some parts in this module have been inspired from: https://github.com/superna9999/pyfdt project.

Installation

pip install fdt

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

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

In case of development, install it from cloned sources:

git clone https://github.com/molejar/pyFDT.git
cd pyFDT
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 fdt 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.

Usage

fdt module has intuitive and self describing API, what is presented in following example. Many of general requirements for manipulation with FDT Nodes, Properties and dts/dtb files are already implemented.

  import fdt

  #-----------------------------------------------
  # convert *.dtb to *.dts
  # ----------------------------------------------
  with open("example.dtb", "rb") as f:
      dtb_data = f.read()

  dt1 = fdt.parse_dtb(dtb_data)

  with open("example.dts", "w") as f:
      f.write(dt1.to_dts())

  #-----------------------------------------------
  # convert *.dts to *.dtb
  # ----------------------------------------------
  with open("example.dts", "r") as f:
      dts_text = f.read()

  dt2 = fdt.parse_dts(dts_text)

  with open("example.dtb", "wb") as f:
      f.write(dt2.to_dtb(version=17))

  #-----------------------------------------------
  # Add Property and Node into dt2
  # ----------------------------------------------
  node = fdt.Node('test_node')
  node.append(fdt.Property('basic_property'))
  node.append(fdt.PropStrings('string_property', 'value1', 'value2'))
  node.append(fdt.PropWords('words_property', 0x1000, 0x80000000))
  node.append(fdt.PropBytes('bytes_property', data=[0, 200, 12]))
  dt2.add_item(node)

  #-----------------------------------------------
  # merge dt2 into dt1
  # ----------------------------------------------
  dt1.merge(dt2)

  with open("merged.dtb", "wb") as f:
      f.write(dt1.to_dtb(version=17))

  #-----------------------------------------------
  # diff two fdt objects
  # ----------------------------------------------
  out = fdt.diff(dt1, dt2)

  print(out[0]) # same in dt1 and dt2
  print(out[1]) # specific for dt1
  print(out[2]) # specific for dt2

[ pydtc ] Tool

The python device tree converter pydtc is a tool for conversion *.dts to *.dtb and vice versa. Is distributed together with fdt module. This tool can be in some cases used as replacement of device tree compiler.

  $ pydtc -h

usage: pydtc [-h] [-v] {pack,unpack,merge,diff} ...

Flat Device Tree (FDT) tool for manipulation with *.dtb and *.dts files

positional arguments:
  {pack,unpack,merge,diff}
    pack                Pack *.dts into binary blob (*.dtb)
    unpack              Unpack *.dtb into readable format (*.dts)
    merge               Merge more files in *.dtb or *.dts format
    diff                Compare two files in *.dtb or *.dts format

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit

$ pydtc unpack [-h] [-s TAB_SIZE] [-o DTS_FILE] dtb_file

Unpack Device Tree from binary blob *.dtb into readable text file *.dts

dtb_file - Single DTB file with dtb extension

optional arguments:
  • -h, --help - Show this help message and exit
  • -s TAB_SIZE - Tabulator Size
  • -o DTS_FILE - Output path/file name (*.dts)
Example:
pydtc unpack test.dtb

DTS saved as: test.dts

$ pydtc pack [-h] [-v VERSION] [-l LC_VERSION] [-c CPU_ID] [-p] [-o DTB_FILE] dts_file

Pack Device Tree from readable text file *.dts into binary blob *.dtb

dts_file - Single DTS file as *.dts

optional arguments:
  • -h, --help - Show this help message and exit
  • -v VERSION - DTB Version
  • -l LC_VERSION - DTB Last Compatible Version
  • -c CPU_ID - Boot CPU ID
  • -p - Update phandle
  • -o DTB_FILE - Output path/file name (*.dtb)
Example:
pydtc pack -v 17 test.dts

DTB saved as: test.dtb

$ pydtc merge [-h] [-t {auto,dts,dtb}] [-s TAB_SIZE] out_file in_files [in_files ...]

Merge two and more *.dtb or *.dts files into one *.dts file

out_file - The output file name with *.dts extension
in_files - Two or more input files with *.dtb or *.dts extension

optional arguments:
  • -h, --help - Show this help message and exit
  • -t {auto,dts,dtb} - Input file type: 'auto', 'dts', 'dtb' (default: auto)
  • -s TAB_SIZE - Tabulator Size
Example:
pydtc merge out.dts test1.dtb test2.dtb

Output saved as: out.dts

$ pydtc diff [-h] [-t {auto,dts,dtb}] [-o OUT_DIR] in_file1 in_file2

Compare two dtb/dts files and generate 3 dts files (same in 1 and 2, specific for 1, specific for 2)

in_file1 - Input file 1
in_file2 - Input file 2

optional arguments:
  • -h, --help - Show this help message and exit
  • -t {auto,dts,dtb} - Input file type: 'auto', 'dts', 'dtb' (default: auto)
  • -o OUT_DIR - Output directory (default: diff_out)
Example:
pydtc diff test1.dtb test2.dtb

Output saved into: diff_out

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

fdt-0.2.0.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fdt-0.2.0-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file fdt-0.2.0.tar.gz.

File metadata

  • Download URL: fdt-0.2.0.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0

File hashes

Hashes for fdt-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b675139346946115513e27b5eed9aa882628ab74ed500bd5e25e122ee0afa2f6
MD5 a91daa36b3216f54feeac74ea8e5a475
BLAKE2b-256 0e447d3ff9e79f7ae7ac72c15d8bb60622d6997b17a9b9240ec939a7169dcc4c

See more details on using hashes here.

File details

Details for the file fdt-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: fdt-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0

File hashes

Hashes for fdt-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0a406155c1aada34f0baa7d44296540000e800b5a73411572f3423a3af0a947
MD5 e9fdbc5bbfa2069749861810a2a1378f
BLAKE2b-256 d6d3a9bf8bc20ddace74371afcf07b430f559d109372e1c2a25c133fe5fd4656

See more details on using hashes here.

Supported by

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