Skip to content

Commit 03602a1

Browse files
committed
improving partition dump logic and error handling
1 parent a762553 commit 03602a1

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

esp32_image_parser.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ def flash_dump_to_elf(filename, partition):
208208
fh.close()
209209
return part_table
210210

211+
def dump_partition(fh, part_name, offset, size, dump_file):
212+
print("Dumping partition '" + part_name + "' to " + dump_file)
213+
dump_bytes(fh, offset, size, dump_file)
214+
211215
def main():
212216
desc = 'ESP32 Firmware Image Parser Utility'
213217
arg_parser = argparse.ArgumentParser(description=desc)
@@ -242,10 +246,10 @@ def main():
242246
dump_file = part_name + '_out.bin'
243247

244248
if part_name in part_table:
245-
print("Dumping partition '" + part_name + "' to " + dump_file)
246249
part = part_table[part_name]
247-
dump_bytes(fh, part['offset'], part['size'], dump_file) # dump_file will be written out
248250

251+
if args.action == 'dump_partition':
252+
dump_partition(fh, part_name, part['offset'], part['size'], dump_file)
249253
if args.action == 'create_elf':
250254
# can only generate elf from 'app' partition type
251255
if part['type'] != 0:
@@ -254,19 +258,22 @@ def main():
254258
if args.output is None:
255259
print("Need output file name")
256260
else:
261+
dump_partition(fh, part_name, part['offset'], part['size'], dump_file)
257262
# we have to load from a file
258263
output_file = args.output
259264
image2elf(dump_file, output_file, verbose)
260265
elif args.action == 'dump_nvs':
261266
if part['type'] != 1 or part['subtype'] != 2: # Wifi NVS partition (4 is for encryption key)
262267
print("Uh oh... bad partition type. Can only dump NVS partition type.")
263-
with open(dump_file, 'rb') as fh:
264-
if(args.nvs_output_type != "text"):
265-
sys.stdout = open(os.devnull, 'w') # block print()
266-
pages = read_nvs_pages(fh)
267-
sys.stdout = sys.stdout = sys.__stdout__ # re-enable print()
268-
if(args.nvs_output_type == "json"):
269-
print(json.dumps(pages))
268+
else:
269+
dump_partition(fh, part_name, part['offset'], part['size'], dump_file)
270+
with open(dump_file, 'rb') as fh:
271+
if(args.nvs_output_type != "text"):
272+
sys.stdout = open(os.devnull, 'w') # block print()
273+
pages = read_nvs_pages(fh)
274+
sys.stdout = sys.stdout = sys.__stdout__ # re-enable print()
275+
if(args.nvs_output_type == "json"):
276+
print(json.dumps(pages))
270277
else:
271278
print("Partition '" + part_name + "' not found.")
272279

0 commit comments

Comments
 (0)