Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KOLANICH committed Oct 9, 2023
0 parents commit e0e2b96
Show file tree
Hide file tree
Showing 16 changed files with 910 additions and 0 deletions.
Empty file added .ci/aptPackagesToInstall.txt
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
indent_style = tab
indent_size = 4
insert_final_newline = true
end_of_line = lf

[*.{yml,yaml}]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .github/.templateMarker
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KOLANICH/python_project_boilerplate.py
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "daily"
allow:
- dependency-type: "all"
15 changes: 15 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: CI
on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-22.04
steps:
- name: typical python workflow
uses: KOLANICH-GHActions/typical-python-workflow@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
__pycache__
*.py[co]
/*.egg-info
*.srctrlbm
*.srctrldb
build
dist
.eggs
monkeytype.sqlite3
/.ipynb_checkpoints
51 changes: 51 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
image: registry.gitlab.com/kolanich-subgroups/docker-images/fixed_python:latest

variables:
DOCKER_DRIVER: overlay2
SAST_ANALYZER_IMAGE_TAG: latest
SAST_DISABLE_DIND: "true"
SAST_CONFIDENCE_LEVEL: 5
CODECLIMATE_VERSION: latest

include:
- template: SAST.gitlab-ci.yml
- template: Code-Quality.gitlab-ci.yml
- template: License-Management.gitlab-ci.yml

build:
tags:
- shared
- linux
stage: build
variables:
GIT_DEPTH: "1"
PYTHONUSERBASE: ${CI_PROJECT_DIR}/python_user_packages

before_script:
- export PATH="$PATH:$PYTHONUSERBASE/bin" # don't move into `variables`
- apt-get update
# todo:
#- apt-get -y install
#- pip3 install --upgrade
#- python3 ./fix_python_modules_paths.py

script:
- python3 -m build -nw bdist_wheel
- mv ./dist/*.whl ./dist/mkits-0.CI-py3-none-any.whl
- pip3 install --upgrade ./dist/*.whl
- coverage run --source=mkits -m --branch pytest --junitxml=./rspec.xml ./tests/test.py
- coverage report -m
- coverage xml

coverage: "/^TOTAL(?:\\s+\\d+){4}\\s+(\\d+%).+/"

cache:
paths:
- $PYTHONUSERBASE

artifacts:
paths:
- dist
reports:
junit: ./rspec.xml
cobertura: ./coverage.xml
1 change: 1 addition & 0 deletions Code_Of_Conduct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No codes of conduct!
595 changes: 595 additions & 0 deletions LICENSE.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include UNLICENSE
include *.md
include tests
include .editorconfig
21 changes: 21 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
mkits.py [![Unlicensed work](https://raw.githubusercontent.com/unlicense/unlicense.org/master/static/favicon.png)](https://unlicense.org/)
========
~~[wheel (GitLab)](https://gitlab.com/KOLANICH-libs/mkits.py/-/jobs/artifacts/master/raw/dist/mkits-0.CI-py3-none-any.whl?job=build)~~
~~[wheel (GHA via `nightly.link`)](https://nightly.link/KOLANICH-libs/mkits.py/workflows/CI/master/mkits-0.CI-py3-none-any.whl)~~
~~![GitLab Build Status](https://gitlab.com/KOLANICH-libs/mkits.py/badges/master/pipeline.svg)~~
~~![GitLab Coverage](https://gitlab.com/KOLANICH-libs/mkits.py/badges/master/coverage.svg)~~
~~[![GitHub Actions](https://github.com/KOLANICH-libs/mkits.py/workflows/CI/badge.svg)](https://github.com/KOLANICH-libs/mkits.py/actions/)~~
[![Libraries.io Status](https://img.shields.io/librariesio/github/KOLANICH-libs/mkits.py.svg)](https://libraries.io/github/KOLANICH-libs/mkits.py)
[![Licensed under GNU GPL-3.0-or-later][https://www.gnu.org/graphics/gplv3-127x51.png]]
[![Code style: antiflash](https://img.shields.io/badge/code%20style-antiflash-FFF.svg)](https://codeberg.org/KOLANICH-tools/antiflash.py)

My rewrite of https://github.com/openwrt/openwrt/blob/master/scripts/mkits.sh into python.

Features compared to the original:

* possibility to use via API using `genITS` function;
* AST manipulation instead of string manipulation;
* autogenerated help messages;
* better CLI args validation;

https://github.com/siemens/u-boot/blob/master/doc/uImage.FIT/source_file_format.txt is the reference.
81 changes: 81 additions & 0 deletions openwrt_mkits/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import typing
from pathlib import Path

from fdt import Node, PropStrings, PropWords, FDT

__all__ = ("genITS", "__copyright__")

__copyright__ = """
Licensed under the terms of the GNU GPL License version 3 or later.
Author: Peter Tyser <ptyser@xes-inc.com>
Rewritten into python by: KOLANICH <KOLANICH@users.noreply.github.com>
"""

__doc__ = """
U-Boot firmware supports the booting of images in the Flattened Image
Tree (FIT) format. The FIT format uses a device tree structure to
describe a kernel image, device tree blob, ramdisk, etc. This script
creates an Image Tree Source (.its file) which can be passed to the
'mkimage' utility to generate an Image Tree Blob (.itb file). The .itb
file can then be booted by U-Boot (or other bootloaders which support
FIT images). See doc/uImage.FIT/howto.txt in U-Boot source code for
additional information on FIT images.
"""


# DEFAULT_INCBIN = Path("./incbin/").absolute()
DEFAULT_INCBIN = Path("./").absolute()

DEFAULT_CHECKSUM_ALGOS = ("crc32", "sha1")


def genITS(arch: str, osName: str, kernel_image: Path, kernel_ver: str, load_addr: int, entry_addr: int, config: Path, fdtnum: int, compression: str, checksumAlgos: typing.Iterable[str] = DEFAULT_CHECKSUM_ALGOS, human_name: typing.Optional[str] = None, device_tree_blob: typing.Optional[Path] = None, incbin: Path = DEFAULT_INCBIN, component: str="kernel") -> Node:
arch_upper = arch.upper()
checksum_algos = [Node("hash@" + str(i + 1), PropStrings("algo", el)) for i, el in enumerate(checksumAlgos)]

append_to_images = []

config_rel_str = str(config.relative_to(incbin).stem)

componentName = component + "@1"

images = Node(
"images",
Node(
componentName,
PropStrings("description", arch_upper + " OpenWrt Linux-" + str(kernel_ver)),
# data = /incbin/("'''+ str(kernel_image.relative_to(incbin)) + '''");
PropStrings("type", component),
PropStrings("arch", arch),
PropStrings("os", osName),
PropStrings("compression", compression),
PropWords("load", load_addr),
PropWords("entry", entry_addr),
*checksum_algos,
),
)

target_comp = Node(
config_rel_str,
PropStrings("description", "OpenWrt"),
PropStrings(component, componentName),
)

configurations = Node("configurations", PropStrings("default", config_rel_str), target_comp)

if device_tree_blob:
images.append_node(
Node(
"fdt@" + str(fdtnum),
PropStrings("description", arch_upper + " OpenWrt " + human_name + " device tree blob"),
# data = /incbin/("{""" + str(device_tree_blob.relative_to(incbin)) + '''}"),
PropStrings("type", "flat_dt"),
PropStrings("arch", arch),
PropStrings("compression", none),
*checksum_algos,
)
)
target_comp.append_prop(PropStrings("fdt", "fdt@" + str(fdtnum)))

return Node("", PropStrings("description", arch_upper + " OpenWrt FIT (Flattened Image Tree)"), PropWords("#address-cells", 1), images, configurations)
52 changes: 52 additions & 0 deletions openwrt_mkits/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from ast import literal_eval
from plumbum import cli
from fdt import FDT

from . import genITS, __copyright__, DEFAULT_CHECKSUM_ALGOS
from .uboot_constants import SUPPORTED_OSES, SUPPORTED_ARCHITECTURES, SUPPORTED_CHECKSUMS, SUPPORTED_COMPRESSIONS, SUPPORTED_COMPONENTS


class CLI(cli.Application):
kernel_ver = cli.SwitchAttr(["-v", "--kernel-version"], mandatory=True, help="set kernel version to 'version'")
kernel_image = cli.SwitchAttr(["-k", "--kernel"], cli.ExistingFile, mandatory=True, help="include kernel image 'kernel'")
arch = cli.SwitchAttr(["-A", "--architecture"], cli.Set(*SUPPORTED_ARCHITECTURES), mandatory=True, help="set architecture to 'arch'")
config = cli.SwitchAttr(["-c", "--config"], mandatory=True, help="set config name 'config'")
load_addr = cli.SwitchAttr(["-a", "--base"], str, mandatory=True, help="set load address to 'addr' (hex)")
entry_addr = cli.SwitchAttr(["-e", "--entry-point"], str, mandatory=True, help="set entry point to 'entry' (hex)")

fdtnum = cli.SwitchAttr(["-n", "--unit-address"], int, help="fdt unit-address 'address'", default=1)

component = cli.SwitchAttr(["--component"], cli.Set(*SUPPORTED_COMPONENTS), default="kernel")
compression = cli.SwitchAttr(["-C", "--compression"], cli.Set(*SUPPORTED_COMPRESSIONS), default="lzma", help="set compression type 'comp'")
checksumAlgos = cli.SwitchAttr(["--checksum-algos"], cli.Set(*SUPPORTED_CHECKSUMS, csv=True), help="Algorithms to use in checksum", default=DEFAULT_CHECKSUM_ALGOS)
os_name = cli.SwitchAttr(["-O", "--osname"], cli.Set(*SUPPORTED_OSES), help="set operating system to 'os'", default="linux")

its_file = cli.SwitchAttr(["-o", "--output"], help="create output file 'its_file'", default="-")

device_tree_blob = cli.SwitchAttr(["-d", "--dtb"], cli.ExistingFile, help="include Device Tree Blob 'dtb'", default=None)
human_name = cli.SwitchAttr(["-D", "--human-name"], help="human friendly Device Tree Blob 'name'")

def main(self):
self.kernel_image = Path(self.kernel_image).absolute()
self.config = Path(self.config).absolute()

if self.device_tree_blob:
self.device_tree_blob = Path(self.device_tree_blob).absolute()

self.load_addr = literal_eval(self.load_addr)
self.entry_addr = literal_eval(self.entry_addr)

resNode = genITS(self.arch, self.os_name, self.kernel_image, self.kernel_ver, self.load_addr, self.entry_addr, self.config, self.fdtnum, self.compression, self.checksumAlgos, human_name=self.human_name, device_tree_blob=self.device_tree_blob, component=self.component)

res = FDT()
res.root = resNode
res = res.to_dts()

if self.its_file == "-":
print(res)
else:
Path(self.its_file).write_text(res)


if __name__ == "__main__":
CLI.run()
20 changes: 20 additions & 0 deletions openwrt_mkits/uboot_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
__license__ = "GPL-2.0-or-later"

__copyright__ = """
(C) Copyright 2008 Semihalf
(C) Copyright 2000-2006
Wolfgang Denk, DENX Software Engineering, wd@denx.de.
"""

# https://github.com/u-boot/u-boot/blob/master/common/image.c

SUPPORTED_ARCHITECTURES = {"alpha", "arc", "arm", "arm64", "avr32", "blackfin", "i386", "ia64", "m68k", "microblaze", "mips", "mips64", "nds32", "nios2", "or1k", "powerpc", "ppc", "riscv", "s390", "sandbox", "sh", "sh", "sparc", "sparc64", "st200", "x86", "x86_64", "xtensa"}

SUPPORTED_COMPRESSIONS = {"none", "bzip2", "gzip", "lz4", "lzma", "lzo", "zstd"}

SUPPORTED_CHECKSUMS = {"crc32", "md5", "sha1"}

SUPPORTED_COMPONENTS = sorted(("aisimage", "standalone", "kernel", "kernel_noload", "ramdisk", "firmware", "script", "filesystem", "flat_dt", "gpimage", "kwbimage", "imximage", "imx8image", "imx8mimage", "invalid", "multi", "omapimage", "pblimage", "socfpgaimage", "socfpgaimage_v1", "ublimage", "mxsimage", "atmelimage", "x86_setup", "lpc32xximage", "rkimage", "rksd", "rkspi", "vybridimage", "zynqimage", "zynqmpimage", "zynqmpbif", "fpga", "tee", "firmware_ivt", "stm32image", "pmmc", "mtk_image", "copro", "sunxi_egon"))

SUPPORTED_OSES = {"invalid", "openbsd", "netbsd", "freebsd", "4_4bsd", "linux", "svr4", "esix", "solaris", "irix", "sco", "dell", "ncr", "lynxos", "vxworks", "psos", "qnx", "u_boot", "rtems", "unity", "integrity", "lynxos", "ose", "plan9", "tee", "u-boot", "openrtos", "opensbi", "efi"}
39 changes: 39 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[build-system]
requires = ["setuptools>=61.2.0", "setuptools_scm[toml]>=3.4.3"]
build-backend = "setuptools.build_meta"

[project]
name = "mkits"
readme = "ReadMe.md"
description = "A rewrite of OpenWRT mkits.sh into python"
authors = [{name = "KOLANICH"}]
classifiers = [
"Development Status :: 4 - Beta",
"Environment :: Other Environment",
"Intended Audience :: Developers",
"License :: OSI Approved",
"License :: OSI Approved :: GNU General Public License (GPL)",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Topic :: Software Development :: Libraries :: Python Modules",
]
keywords = ["mkits", "OpenWRT"]
license = {text = "GPL-3.0-or-later"}
requires-python = ">=3.4"
dynamic = ["version"]

[project.urls]
Homepage = "https://codeberg.org/KOLANICH-libs/openwrt_mkits.py"

[project.scripts]
mkits = "openwrt_mkits.__main__:CLI.run"

[tool.setuptools]
zip-safe = true

[tool.setuptools.packages.find]
include = ["openwrt_mkits", "openwrt_mkits.*"]

[tool.setuptools_scm]

0 comments on commit e0e2b96

Please sign in to comment.