diff --git a/CHANGELOG.md b/CHANGELOG.md index 03f5479..26c7dfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/pybricksdev/dfu.py b/pybricksdev/dfu.py index 9759f5b..085ad75 100644 --- a/pybricksdev/dfu.py +++ b/pybricksdev/dfu.py @@ -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 @@ -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