Skip to content

Commit f8bb135

Browse files
author
Nick Miles
committed
Updated
1 parent 0db27b6 commit f8bb135

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

esp32_image_parser.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/env python
22

33
# Convert an ESP 32 OTA partition into an ELF
4-
4+
import sys
5+
import json
56
import os, argparse
67
from makeelf.elf import *
78
from esptool import *
89
from esp32_firmware_reader import *
10+
from read_nvs import *
911

1012
def image_base_name(path):
1113
filename_w_ext = os.path.basename(path)
@@ -209,9 +211,10 @@ def flash_dump_to_elf(filename, partition):
209211
def 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

260274
if __name__ == '__main__':
261275
main()

read_nvs.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import sys
2-
import json
3-
import struct
4-
import sys
51
import os
2+
import struct
63
import base64
74
import binascii
85
from hexdump import hexdump
9-
import argparse
106

117
nvs_types = {
128
0x01: "U8",
@@ -40,7 +36,7 @@
4036

4137
namespaces = {}
4238

43-
def parse_entries(entries, entry_state_bitmap):
39+
def parse_nvs_entries(entries, entry_state_bitmap):
4440
entries_out = []
4541
i = 0
4642
while i < 126:
@@ -196,7 +192,7 @@ def parse_entries(entries, entry_state_bitmap):
196192
print("")
197193
return entries_out
198194

199-
def read_pages(fh):
195+
def read_nvs_pages(fh):
200196
pages = []
201197
fh.seek(0, os.SEEK_END)
202198
file_len = fh.tell()
@@ -249,7 +245,7 @@ def read_pages(fh):
249245
entry_data = fh.read(32)
250246
entries.append(entry_data)
251247

252-
page_data["entries"] = parse_entries(entries, entry_state_bitmap_decoded)
248+
page_data["entries"] = parse_nvs_entries(entries, entry_state_bitmap_decoded)
253249

254250
print("")
255251
print("")
@@ -260,20 +256,20 @@ def read_pages(fh):
260256
print("")
261257
return pages
262258

263-
parser = argparse.ArgumentParser()
264-
parser.add_argument("nvs_bin_file", help="nvs partition binary file", type=str)
265-
parser.add_argument("-output_type", help="output type", type=str, choices=["text", "json"], default="text")
259+
#parser = argparse.ArgumentParser()
260+
#parser.add_argument("nvs_bin_file", help="nvs partition binary file", type=str)
261+
#parser.add_argument("-output_type", help="output type", type=str, choices=["text", "json"], default="text")
266262

267-
args = parser.parse_args()
263+
#args = parser.parse_args()
268264

269-
with open(args.nvs_bin_file, 'rb') as fh:
270-
if(args.output_type != "text"):
271-
sys.stdout = open(os.devnull, 'w') # block print()
265+
#with open(args.nvs_bin_file, 'rb') as fh:
266+
# if(args.output_type != "text"):
267+
# sys.stdout = open(os.devnull, 'w') # block print()
272268

273-
pages = read_pages(fh)
269+
# pages = read_pages(fh)
274270

275-
sys.stdout = sys.stdout = sys.__stdout__ # re-enable print()
271+
# sys.stdout = sys.stdout = sys.__stdout__ # re-enable print()
276272

277-
if(args.output_type == "json"):
278-
print(json.dumps(pages))
273+
# if(args.output_type == "json"):
274+
# print(json.dumps(pages))
279275

0 commit comments

Comments
 (0)