|
5 | 5 | # SPDX-License-Identifier: GPL-2.0-or-later
|
6 | 6 |
|
7 | 7 | import argparse
|
| 8 | +import os |
8 | 9 | import json
|
9 | 10 | import sys
|
10 | 11 |
|
@@ -173,12 +174,22 @@ def check_efuse_name(efuse_name, efuse_list):
|
173 | 174 | help="Display information about ADC calibration data stored in efuse.",
|
174 | 175 | )
|
175 | 176 |
|
176 |
| - dump_cmd = subparsers.add_parser("dump", help="Dump raw hex values of all efuses") |
| 177 | + dump_cmd = subparsers.add_parser("dump", help="Dump raw hex values of all eFuses") |
| 178 | + dump_cmd.add_argument( |
| 179 | + "--format", |
| 180 | + help="Select the dump format: " |
| 181 | + "default - usual console eFuse dump; " |
| 182 | + "united - all eFuse blocks are stored in one file; " |
| 183 | + "separated - each eFuse block is placed in a separate file. Tool will create multiple files based on " |
| 184 | + "the given --file_name (/path/blk.bin): blk0.bin, blk1.bin ... blkN.bin. Use the burn_block_data cmd " |
| 185 | + "to write it back to another chip.", |
| 186 | + choices=["default", "separated", "united"], |
| 187 | + default="default", |
| 188 | + ) |
177 | 189 | dump_cmd.add_argument(
|
178 | 190 | "--file_name",
|
179 |
| - help="Saves dump for each block into separate file. Provide the common " |
180 |
| - "path name /path/blk.bin, it will create: blk0.bin, blk1.bin ... blkN.bin. " |
181 |
| - "Use burn_block_data to write it back to another chip.", |
| 191 | + help="The path to the file in which to save the dump, if not specified, output to the console.", |
| 192 | + default=sys.stdout, |
182 | 193 | )
|
183 | 194 |
|
184 | 195 | summary_cmd = subparsers.add_parser(
|
@@ -379,23 +390,48 @@ def summary(esp, efuses, args):
|
379 | 390 |
|
380 | 391 | def dump(esp, efuses, args):
|
381 | 392 | """Dump raw efuse data registers"""
|
382 |
| - # Using --debug option allows to print dump. |
383 |
| - # Nothing to do here. The log will be printed |
384 |
| - # during EspEfuses.__init__() in self.read_blocks() |
385 |
| - if args.file_name: |
386 |
| - # save dump to the file |
| 393 | + dump_file = args.file_name |
| 394 | + to_console = args.file_name == sys.stdout |
| 395 | + |
| 396 | + def output_block_to_file(block, f, to_console): |
| 397 | + block_dump = BitStream(block.get_bitstring()) |
| 398 | + block_dump.byteswap() |
| 399 | + if to_console: |
| 400 | + f.write(block_dump.hex + "\n") |
| 401 | + else: |
| 402 | + block_dump.tofile(f) |
| 403 | + |
| 404 | + if args.format == "default": |
| 405 | + if to_console: |
| 406 | + # for "espefuse.py dump" cmd |
| 407 | + for block in efuses.blocks: |
| 408 | + block.print_block(block.get_bitstring(), "dump", debug=True) |
| 409 | + return |
| 410 | + else: |
| 411 | + # for back compatibility to support "espefuse.py dump --file_name dump.bin" |
| 412 | + args.format = "separated" |
| 413 | + |
| 414 | + if args.format == "separated": |
| 415 | + # each efuse block is placed in a separate file |
387 | 416 | for block in efuses.blocks:
|
388 |
| - file_dump_name = args.file_name |
389 |
| - place_for_index = file_dump_name.find(".bin") |
390 |
| - file_dump_name = ( |
391 |
| - file_dump_name[:place_for_index] |
392 |
| - + str(block.id) |
393 |
| - + file_dump_name[place_for_index:] |
394 |
| - ) |
395 |
| - print(file_dump_name) |
396 |
| - with open(file_dump_name, "wb") as f: |
397 |
| - block.get_bitstring().byteswap() |
398 |
| - block.get_bitstring().tofile(f) |
| 417 | + if not to_console: |
| 418 | + file_dump_name = args.file_name |
| 419 | + fname, fextension = os.path.splitext(file_dump_name) |
| 420 | + file_dump_name = f"{fname}{block.id}{fextension}" |
| 421 | + print(f"Dump efuse block{block.id} -> {file_dump_name}") |
| 422 | + dump_file = open(file_dump_name, "wb") |
| 423 | + output_block_to_file(block, dump_file, to_console) |
| 424 | + if not to_console: |
| 425 | + dump_file.close() |
| 426 | + elif args.format == "united": |
| 427 | + # all efuse blocks are stored in one file |
| 428 | + if not to_console: |
| 429 | + print(f"Dump efuse blocks -> {args.file_name}") |
| 430 | + dump_file = open(args.file_name, "wb") |
| 431 | + for block in efuses.blocks: |
| 432 | + output_block_to_file(block, dump_file, to_console) |
| 433 | + if not to_console: |
| 434 | + dump_file.close() |
399 | 435 |
|
400 | 436 |
|
401 | 437 | def burn_efuse(esp, efuses, args):
|
|
0 commit comments