-
Notifications
You must be signed in to change notification settings - Fork 11
/
ota.py
60 lines (44 loc) · 1.46 KB
/
ota.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import annotations
import logging
import pathlib
import click
from zigpy.ota.image import ElementTagId, parse_ota_image
from zigpy.ota.validators import validate_ota_image
from zigpy_cli.cli import cli
LOGGER = logging.getLogger(__name__)
@cli.group()
def ota():
pass
@ota.command()
@click.argument("files", nargs=-1, type=pathlib.Path)
def info(files):
for f in files:
if not f.is_file():
continue
try:
image, rest = parse_ota_image(f.read_bytes())
except Exception as e:
LOGGER.warning("Failed to parse %s: %s", f, e)
continue
if rest:
LOGGER.warning("Image has trailing data %s: %r", f, rest)
print(f"Header: {image.header}")
print(f"Number of subelements: {len(image.subelements)}")
try:
result = validate_ota_image(image)
except Exception as e:
LOGGER.warning("Image is invalid %s: %s", f, e)
else:
print(f"Validation result: {result}")
print()
@ota.command()
@click.argument("input", type=click.File("rb"))
@click.argument("output", type=click.File("wb"))
def dump_firmware(input, output):
image, _ = parse_ota_image(input.read())
for subelement in image.subelements:
if subelement.tag_id == ElementTagId.UPGRADE_IMAGE:
output.write(subelement.data)
break
else:
LOGGER.warning("Image has no UPGRADE_IMAGE subelements")