11#!/usr/bin/env python
22
33# Convert an ESP 32 OTA partition into an ELF
4-
4+ import sys
5+ import json
56import os , argparse
67from makeelf .elf import *
78from esptool import *
89from esp32_firmware_reader import *
10+ from read_nvs import *
911
1012def image_base_name (path ):
1113 filename_w_ext = os .path .basename (path )
@@ -209,9 +211,10 @@ def flash_dump_to_elf(filename, partition):
209211def main ():
210212 desc = 'ESP32 Firmware Image Parser Utility'
211213 arg_parser = argparse .ArgumentParser (description = desc )
212- arg_parser .add_argument ('action' , choices = ['show_partitions' , 'dump_partition' , 'create_elf' ], help = 'Action to take' )
214+ arg_parser .add_argument ('action' , choices = ['show_partitions' , 'dump_partition' , 'create_elf' , 'dump_nvs' ], help = 'Action to take' )
213215 arg_parser .add_argument ('input' , help = 'Firmware image input file' )
214216 arg_parser .add_argument ('-output' , help = 'Output file name' )
217+ arg_parser .add_argument ('-nvs_output_type' , help = 'output type for nvs dump' , type = str , choices = ["text" ,"json" ], default = "text" )
215218 arg_parser .add_argument ('-partition' , help = 'Partition name (e.g. ota_0)' )
216219 arg_parser .add_argument ('-v' , default = False , help = 'Verbose output' , action = 'store_true' )
217220
@@ -226,7 +229,7 @@ def main():
226229 # parse that ish
227230 part_table = read_partition_table (fh , verbose )
228231
229- if args .action in ['dump_partition' , 'create_elf' ]:
232+ if args .action in ['dump_partition' , 'create_elf' , 'dump_nvs' ]:
230233 if (args .partition is None ):
231234 print ("Need partition name" )
232235 return
@@ -254,8 +257,19 @@ def main():
254257 # we have to load from a file
255258 output_file = args .output
256259 image2elf (dump_file , output_file , verbose )
260+ elif args .action == 'dump_nvs' :
261+ if part ['type' ] != 1 or part ['subtype' ] != 2 : # Wifi NVS partition (4 is for encryption key)
262+ 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 ))
257270 else :
258271 print ("Partition '" + part_name + "' not found." )
272+ pages = read_nvs_pages (dump_file )
259273
260274if __name__ == '__main__' :
261275 main ()
0 commit comments