|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import hashlib |
| 4 | +import json |
3 | 5 | import logging |
4 | 6 | import pathlib |
5 | 7 |
|
@@ -65,3 +67,61 @@ def dump_firmware(input, output): |
65 | 67 | break |
66 | 68 | else: |
67 | 69 | LOGGER.warning("Image has no UPGRADE_IMAGE subelements") |
| 70 | + |
| 71 | + |
| 72 | +@ota.command() |
| 73 | +@click.pass_context |
| 74 | +@click.option("--ota-url-root", type=str, default=None) |
| 75 | +@click.option("--output", type=click.File("w"), default="-") |
| 76 | +@click.argument("files", nargs=-1, type=pathlib.Path) |
| 77 | +def generate_index(ctx, ota_url_root, output, files): |
| 78 | + if ctx.parent.parent.params["verbose"] == 0: |
| 79 | + cli.callback(verbose=1) |
| 80 | + |
| 81 | + ota_metadata = [] |
| 82 | + |
| 83 | + for f in files: |
| 84 | + if not f.is_file(): |
| 85 | + continue |
| 86 | + |
| 87 | + LOGGER.info("Parsing %s", f) |
| 88 | + contents = f.read_bytes() |
| 89 | + |
| 90 | + try: |
| 91 | + image, rest = parse_ota_image(contents) |
| 92 | + except Exception as e: |
| 93 | + LOGGER.error("Failed to parse: %s", e) |
| 94 | + continue |
| 95 | + |
| 96 | + if rest: |
| 97 | + LOGGER.error("Image has trailing data: %r", rest) |
| 98 | + continue |
| 99 | + |
| 100 | + try: |
| 101 | + validate_ota_image(image) |
| 102 | + except Exception as e: |
| 103 | + LOGGER.error("Image is invalid: %s", e) |
| 104 | + |
| 105 | + if ota_url_root is not None: |
| 106 | + url = f"{ota_url_root.rstrip('/')}/{f.name}" |
| 107 | + else: |
| 108 | + url = None |
| 109 | + |
| 110 | + metadata = { |
| 111 | + "binary_url": url, |
| 112 | + "file_version": image.header.file_version, |
| 113 | + "image_type": image.header.image_type, |
| 114 | + "manufacturer_id": image.header.manufacturer_id, |
| 115 | + "changelog": "", |
| 116 | + "checksum": f"sha3-256:{hashlib.sha3_256(contents).hexdigest()}", |
| 117 | + } |
| 118 | + |
| 119 | + if image.header.hardware_versions_present: |
| 120 | + metadata["min_hardware_version"] = image.header.minimum_hardware_version |
| 121 | + metadata["max_hardware_version"] = image.header.maximum_hardware_version |
| 122 | + |
| 123 | + LOGGER.info("Writing %s", f) |
| 124 | + ota_metadata.append(metadata) |
| 125 | + |
| 126 | + json.dump(ota_metadata, output, indent=4) |
| 127 | + output.write("\n") |
0 commit comments