Skip to content

Commit f51face

Browse files
committed
dfu: Check device ID against metadata.
1 parent 73d3cdb commit f51face

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

pybricksdev/dfu.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
SPIKE_ESSENTIAL_PID = 0x000C
2525
MINDSTORMS_INVENTOR_PID = 0x0011
2626

27-
28-
ALL_PIDS = [SPIKE_PRIME_PID, SPIKE_ESSENTIAL_PID, MINDSTORMS_INVENTOR_PID]
29-
ALL_DEVICES = [f"{LEGO_VID:04x}:{pid:04x}" for pid in ALL_PIDS]
27+
ALL_PIDS = {
28+
MINDSTORMS_INVENTOR_PID: HubKind.TECHNIC_LARGE,
29+
SPIKE_ESSENTIAL_PID: HubKind.TECHNIC_SMALL,
30+
SPIKE_PRIME_PID: HubKind.TECHNIC_LARGE,
31+
}
32+
ALL_DEVICES = [f"{LEGO_VID:04x}:{pid:04x}" for pid in ALL_PIDS.keys()]
3033

3134

3235
def _get_dfu_util() -> ContextManager[os.PathLike]:
@@ -157,10 +160,6 @@ def restore_dfu(file: BinaryIO) -> None:
157160
def flash_dfu(firmware_bin: bytes, metadata: dict) -> None:
158161
"""Flashes a firmware file using DFU."""
159162

160-
if metadata["device-id"] != HubKind.TECHNIC_LARGE:
161-
print("Unknown hub type:", metadata["device-id"], file=sys.stderr)
162-
exit(1)
163-
164163
with TemporaryDirectory() as out_dir:
165164
outfile = os.path.join(out_dir, "firmware.dfu")
166165
target = {"address": FIRMWARE_ADDRESS, "data": firmware_bin}
@@ -182,6 +181,10 @@ def flash_dfu(firmware_bin: bytes, metadata: dict) -> None:
182181
print(f"Unknown USB product ID: {product_id:04X}", file=sys.stderr)
183182
exit(1)
184183

184+
if ALL_PIDS[product_id] != metadata["device-id"]:
185+
print("Incorrect firmware type for this hub", file=sys.stderr)
186+
exit(1)
187+
185188
# Create dfu file
186189
device = "0x{0:04x}:0x{1:04x}".format(LEGO_VID, product_id)
187190
_dfu_create.build(outfile, [[target]], device)

0 commit comments

Comments
 (0)