Skip to content

Commit

Permalink
dfu: add size check when restoring firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
dlech committed Apr 9, 2021
1 parent 8b3f7b3 commit 34ac155
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Size check when restoring firmware via USB/DFU.

## [1.0.0-alpha.3] - 2021-04-09
### Changed
- Print BLE download progress after chunk is complete instead of before.
Expand Down
9 changes: 8 additions & 1 deletion pybricksdev/dfu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2019-2020 The Pybricks Authors
# Copyright (c) 2019-2021 The Pybricks Authors

import errno
import os
Expand Down Expand Up @@ -93,6 +93,13 @@ def restore_dfu(file: BinaryIO) -> None:
Args:
file: the file that contains the firmware data
"""
file.seek(0, os.SEEK_END)
size = file.tell()
file.seek(0, os.SEEK_SET)

if size < 512:
raise ValueError("File is too small to be a valid firmware file")

try:
# TODO: implement this using pydfu
raise NoBackendError
Expand Down

0 comments on commit 34ac155

Please sign in to comment.