|
| 1 | +# This file is part of the MicroPython project, http://micropython.org/ |
| 2 | +# |
| 3 | +# The MIT License (MIT) |
| 4 | +# |
| 5 | +# Copyright (c) 2022 Ibrahim Abdelkader <[email protected]> |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in |
| 15 | +# all copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +# THE SOFTWARE. |
| 24 | + |
| 25 | +import argparse |
| 26 | +import hashlib |
| 27 | +import os |
| 28 | +import subprocess |
| 29 | + |
| 30 | +script = """import espflash |
| 31 | +esp = espflash.ESPFlash() |
| 32 | +esp.bootloader() |
| 33 | +esp.set_baudrate({:d}) |
| 34 | +esp.flash_attach() |
| 35 | +size = esp.flash_read_size() |
| 36 | +esp.flash_config(size) |
| 37 | +esp.flash_write_file('{:s}') |
| 38 | +esp.flash_verify_file('{:s}', '{:s}') |
| 39 | +esp.reboot()""" |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + parser = argparse.ArgumentParser(description="espflash-cmd.py") |
| 43 | + |
| 44 | + parser.add_argument( |
| 45 | + "-f", "--file", help="Upload binary firmware file.", dest="path", required=True |
| 46 | + ) |
| 47 | + parser.add_argument( |
| 48 | + "-b", "--baudrate", help="The secondary faster baudrate.", action="store", default=921600 |
| 49 | + ) |
| 50 | + parser.add_argument( |
| 51 | + "-v", "--verbose", help="Increase output verbosity", action="store_true", default=False |
| 52 | + ) |
| 53 | + args = parser.parse_args() |
| 54 | + |
| 55 | + with open(args.path, "rb") as f: |
| 56 | + digest = hashlib.md5(f.read()).hexdigest() |
| 57 | + |
| 58 | + path = os.path.basename(args.path) |
| 59 | + mount = os.path.dirname(args.path) |
| 60 | + baudrate = int(args.baudrate) |
| 61 | + |
| 62 | + cmd = [ |
| 63 | + "mpremote", |
| 64 | + "mount", |
| 65 | + mount, |
| 66 | + "exec", |
| 67 | + script.replace("\n", ";").format(baudrate, path, path, digest), |
| 68 | + ] |
| 69 | + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 70 | + while True: |
| 71 | + l = p.stdout.readline() |
| 72 | + if not l and p.poll() is not None: |
| 73 | + break |
| 74 | + if l: |
| 75 | + print(l.strip().decode("utf-8")) |
| 76 | + sys.exit(p.poll()) |
0 commit comments