diff --git a/components/nvs_flash/nvs_partition_generator/README.rst b/components/nvs_flash/nvs_partition_generator/README.rst index aa7a945c2..bea753864 100644 --- a/components/nvs_flash/nvs_partition_generator/README.rst +++ b/components/nvs_flash/nvs_partition_generator/README.rst @@ -116,6 +116,7 @@ A sample CSV file is provided with the utility:: python nvs_partition_gen.py sample_singlepage_blob.csv partition_single_page.bin --version v1 + +------------------------+----------------------------------------------------------------------------------------------+ | Arguments | Description | +========================+==============================================================================================+ @@ -123,7 +124,7 @@ A sample CSV file is provided with the utility:: +------------------------+----------------------------------------------------------------------------------------------+ | output | Path to output converted binary file. Will use stdout if omitted | +------------------------+----------------------------------------------------------------------------------------------+ -| size | Size of NVS Partition in KB. Eg. 12KB | +| size | Size of NVS Partition in bytes (must be multiple of 4096) | +------------------------+----------------------------------------------------------------------------------------------+ | --version {v1,v2} | Set version. Default: v2 | +-------------------------------+---------------------------------------------------------------------------------------+ diff --git a/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py b/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py index 0f641d612..416c146fc 100755 --- a/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py +++ b/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py @@ -17,8 +17,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from __future__ import division -from __future__ import print_function + +from __future__ import division, print_function +from builtins import int, range +from io import open import sys import argparse import binascii @@ -28,6 +30,7 @@ import os import array import csv import zlib +import codecs from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend @@ -79,20 +82,20 @@ class Page(object): global page_header # set page state to active - page_header= bytearray(b'\xff')*32 + page_header= bytearray(b'\xff') *32 page_state_active_seq = Page.ACTIVE - page_header[0:4] = struct.pack('= Page.PAGE_PARAMS["max_entries"]: raise PageFullError() - entry_struct = bytearray('\xff')*32 + entry_struct = bytearray(b'\xff')*32 entry_struct[0] = ns_index # namespace index entry_struct[2] = 0x01 # Span chunk_index = Page.CHUNK_ANY entry_struct[3] = chunk_index # write key - key_array = bytearray('\x00')*16 + key_array = b'\x00' *16 entry_struct[8:24] = key_array - entry_struct[8:8 + len(key)] = key + entry_struct[8:8 + len(key)] = key.encode() if encoding == "u8": entry_struct[1] = Page.U8 - entry_struct[24] = struct.pack('") - + raise SystemExit("Oops...data missing in config file at line no:%s \n" \ + % str(line_no) ) config_file.close() @@ -131,7 +132,7 @@ def verify_data_in_file(input_config_file, input_values_file, config_file_keys, verify_values_exist(input_values_file, keys_in_values_file) except StandardError as std_err: - print std_err + print(std_err) except: raise @@ -203,6 +204,7 @@ def add_data_to_file(config_data_to_write, key_value_pair, output_csv_file): data_to_write = [] target_csv_file = open(output_csv_file, 'w') + output_file_writer = csv.writer(target_csv_file, delimiter=',') output_file_writer.writerow(header) @@ -246,18 +248,18 @@ def set_repeat_value(total_keys_repeat, keys, csv_file): target_filename = filename + "_created" + file_ext with open(csv_file, 'r') as read_from, open(target_filename,'w') as write_to: csv_file_reader = csv.reader(read_from, delimiter=',') - headers = csv_file_reader.next() - values = csv_file_reader.next() - total_keys_values = map(None, keys, values) - + headers = next(csv_file_reader) + values = next(csv_file_reader) csv_file_writer = csv.writer(write_to, delimiter=',') csv_file_writer.writerow(headers) csv_file_writer.writerow(values) + total_keys_values = list(zip_longest(keys, values)) # read new data, add value if key has repeat tag, write to new file for row in csv_file_reader: index = -1 - key_val_new = map(None, keys, row) + key_val_new = list(zip_longest(keys, row)) + key_val_pair = total_keys_values[:] key_repeated = total_keys_repeat[:] while key_val_new and key_repeated: @@ -270,7 +272,7 @@ def set_repeat_value(total_keys_repeat, keys, csv_file): del key_repeated[0] del key_val_new[0] del key_val_pair[0] - + return target_filename @@ -284,6 +286,11 @@ file_identifier=None,output_dir_path=None): description="Create binary files from input config and values file", formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("--size", + dest='part_size', + required=True, + help='Size of NVS Partition in bytes (must be multiple of 4096)') + parser.add_argument('--conf', dest='config_file', required=True, @@ -320,6 +327,7 @@ file_identifier=None,output_dir_path=None): if not args.outdir.endswith('/'): args.outdir = args.outdir + '/' + input_part_size = args.part_size input_config_file = args.config_file input_values_file = args.values_file target_file_name_prefix = args.prefix @@ -345,11 +353,11 @@ file_identifier=None,output_dir_path=None): # Verify config file is not empty if os.stat(input_config_file).st_size == 0: - raise SystemExit("Oops...config file: " + input_config_file + " is empty...") + raise SystemExit("Oops...config file: %s is empty." % input_config_file) # Verify values file is not empty if os.stat(input_values_file).st_size == 0: - raise SystemExit("Oops...values file: " + input_values_file + " is empty...") + raise SystemExit("Oops...values file: %s is empty." % input_values_file ) # Verify config file does not have empty lines csv_config_file = open(input_config_file,'r') @@ -364,9 +372,9 @@ file_identifier=None,output_dir_path=None): is_empty_line = False break if is_empty_line: - raise SystemExit("Oops...config file: " + input_config_file + " cannot have empty lines...") + raise SystemExit("Oops...config file: %s cannot have empty lines. " % input_config_file ) if not config_data: - raise SystemExit("Oops...config file: " + input_config_file + " cannot have empty lines...") + raise SystemExit("Oops...config file: %s cannot have empty lines." % input_config_file ) csv_config_file.seek(0) @@ -381,7 +389,7 @@ file_identifier=None,output_dir_path=None): csv_config_file.close() except Exception as e: - print e + print(e) finally: csv_config_file.close() @@ -389,7 +397,7 @@ file_identifier=None,output_dir_path=None): # Verify values file does not have empty lines - csv_values_file = open(input_values_file,'rb') + csv_values_file = open(input_values_file, 'r') try: values_file_reader = csv.reader(csv_values_file, delimiter=',') for values_data in values_file_reader: @@ -401,18 +409,18 @@ file_identifier=None,output_dir_path=None): is_empty_line = False break if is_empty_line: - raise SystemExit("Oops...values file: " + input_values_file + " cannot have empty lines...") + raise SystemExit("Oops...values file: %s cannot have empty lines." % input_values_file ) if not values_data: - raise SystemExit("Oops...values file: " + input_values_file + " cannot have empty lines...") + raise SystemExit("Oops...values file: %s cannot have empty lines." % input_values_file ) csv_values_file.seek(0) # Extract keys from values file - keys_in_values_file = values_file_reader.next() + keys_in_values_file = next(values_file_reader) csv_values_file.close() except Exception as e: - print e + print(e) exit(1) finally: csv_values_file.close() @@ -420,8 +428,8 @@ file_identifier=None,output_dir_path=None): # Verify file identifier exists in values file if file_identifier: if file_identifier not in keys_in_values_file: - raise SystemExit('Oops...target_file_identifier: ' + file_identifier + \ - ' does not exist in values file...\n') + raise SystemExit('Oops...target_file_identifier: %s does not exist in values file.\n' % file_identifier ) + # Verify data in the input_config_file and input_values_file verify_data_in_file(input_config_file, input_values_file, keys_in_config_file,\ @@ -431,16 +439,20 @@ file_identifier=None,output_dir_path=None): config_data_to_write = add_config_data_per_namespace(input_config_file) try: - with open(input_values_file,'rb') as csv_values_file: + with open(input_values_file, 'r') as csv_values_file: values_file_reader = csv.reader(csv_values_file, delimiter=',') - keys = values_file_reader.next() + keys = next(values_file_reader) + target_values_file = set_repeat_value(keys_repeat, keys, input_values_file) - csv_values_file = open(target_values_file, 'rb') + + csv_values_file = open(target_values_file, 'r') + values_file_reader = csv.reader(csv_values_file, delimiter=',') - values_file_reader.next() + next(values_file_reader) + for values_data_line in values_file_reader: - key_value_data = map(None,keys_in_values_file,values_data_line) + key_value_data = list(zip_longest(keys_in_values_file,values_data_line)) # Get file identifier value from values file file_identifier_value = get_fileid_val(file_identifier, keys_in_config_file, \ @@ -456,7 +468,7 @@ file_identifier=None,output_dir_path=None): csv_file_list.append(csv_filename) output_csv_file = output_target_dir + csv_filename if os.path.isfile(output_csv_file): - raise SystemExit("Target csv file: `" + output_csv_file + "` already exists...") + raise SystemExit("Target csv file: %s already exists.`" % output_csv_file ) # Add values corresponding to each key to csv target file add_data_to_file(config_data_to_write, key_value_pair, output_csv_file) @@ -468,18 +480,19 @@ file_identifier=None,output_dir_path=None): output_bin_file = output_target_dir + target_file_name_prefix + "-" +\ file_identifier_value + ".bin" if os.path.isfile(output_bin_file): - raise SystemExit("Target csv file: `" + output_bin_file + "` already exists...") + raise SystemExit("Target csv file: %s already exists.`" % output_bin_file ) # Create output csv and bin file - print "CSV Generated: " + str(output_csv_file) - nvs_partition_gen.nvs_part_gen(input_filename = output_csv_file, output_filename = output_bin_file) - print "NVS Flash Binary Generated: " + str(output_bin_file) + print("CSV Generated: ", str(output_csv_file)) + nvs_partition_gen.nvs_part_gen(input_filename = output_csv_file, output_filename = output_bin_file,\ + input_size=input_part_size) + print("NVS Flash Binary Generated: ", str(output_bin_file)) files_created = True csv_values_file.close() except Exception as e: - print e + print(e) exit(1) finally: csv_values_file.close() @@ -487,8 +500,8 @@ file_identifier=None,output_dir_path=None): return csv_file_list, files_created - except StandardError as std_err: - print std_err + except ValueError as err: + print(err) except: raise