Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
python-version: ["3.11", "3.12", "3.13", "3.14"]

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
python-version: ["3.11", "3.12", "3.13", "3.14"]

steps:
- uses: actions/checkout@v4
Expand Down
145 changes: 31 additions & 114 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ pattern = '(?P<base>\d+\.\d+\.\d+)'
format-jinja = "{% if distance == 0 %}{{ base }}{% if dirty %}+dirty{% endif %}{% else %}{{ base }}-dev{{ distance }}+g{{ commit }}{% if dirty %}.dirty{% endif %}{% endif %}"

[tool.poetry.dependencies]
python = ">=3.10, <4"
smpclient = "^6.1.0"
typer = { extras = ["all"], version = "^0.16.0" }
python = ">=3.11, <4"
smpclient = { extras = ["all"], version = "^7" }
typer = { extras = ["all"], version = "^0.24.0" }
readchar = "^4.0.5"

[tool.poetry.group.dev.dependencies]
Expand Down
66 changes: 44 additions & 22 deletions smpmgr/image_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import asyncio
import logging
from enum import StrEnum, unique
from io import BufferedReader
from pathlib import Path
from typing import Annotated, cast
from typing import Annotated, TypeAlias, assert_never, cast

import typer
from rich import print
Expand All @@ -24,6 +25,32 @@

from smpmgr.common import Options, connect_with_spinner, get_smpclient, smp_request


@unique
class ImageFormat(StrEnum):
MCUBOOT = "mcuboot"
ANY = "any"


ImageFormatOption: TypeAlias = Annotated[
ImageFormat,
typer.Option(
"--format",
help="The expected image format for local inspection. "
"'mcuboot' (default) inspects the image as an MCUboot image before upload. "
"'any' skips local MCUboot inspection and does not attempt to interpret the file "
"as an MCUboot image. "
"This is useful when uploading images that are not in MCUboot format, such as "
"custom bootloader formats (e.g., NXP's SB3.1). "
"[bold red]WARNING[/bold red]: When using --format=any, the responsibility for "
"validating image integrity is placed entirely on the device's bootloader. "
"If the bootloader does not verify the image, corrupted firmware could be uploaded. "
"[bold red]Only use --format=any if your bootloader performs its own image integrity "
"validation.[/bold red]",
),
]


app = typer.Typer(name="image", help="The SMP Image Management Group.")
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -174,30 +201,25 @@ def upload(
ctx: typer.Context,
file: Annotated[Path, typer.Argument(help="Path to FW image")],
slot: Annotated[int, typer.Option(help="The image slot to upload to")] = 0,
bypass_inspect: Annotated[
bool,
typer.Option(
"--bypass-inspect",
help="Skip local MCUboot image inspection. "
"This is useful when uploading images that are not in MCUboot format, such as "
"custom bootloader formats (e.g., NXP's SB3.1). "
"[bold red]WARNING[/bold red]: When using this option, the responsibility for "
"validating image integrity is placed entirely on the device's bootloader. "
"If the bootloader does not verify the image, corrupted firmware could be uploaded. "
"[bold red]Only use this option if your bootloader performs its own image integrity "
"validation.[/bold red]",
),
] = False,
format: ImageFormatOption = ImageFormat.MCUBOOT,
) -> None:
"""Upload a FW image."""

if not bypass_inspect:
try:
image_info = ImageInfo.load_file(str(file))
logger.info(str(image_info))
except Exception:
logger.exception("Inspection of FW image failed")
raise typer.Exit(code=1)
match format:
case ImageFormat.MCUBOOT:
try:
image_info = ImageInfo.load_file(str(file))
logger.info(str(image_info))
except Exception:
logger.exception(
"Inspection of FW image failed. "
"If this is not an MCUboot image, retry with --format=any."
)
raise typer.Exit(code=1)
case ImageFormat.ANY:
pass
case _ as unreachable:
assert_never(unreachable)

options = cast(Options, ctx.obj)
smpclient = get_smpclient(options)
Expand Down
Loading
Loading