1#!/usr/bin/env python3 2# coding=utf-8 3 4''' 5* Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 6* Licensed under the Apache License, Version 2.0 (the "License"); 7* you may not use this file except in compliance with the License. 8* You may obtain a copy of the License at 9* 10* http://www.apache.org/licenses/LICENSE-2.0 11* 12* Unless required by applicable law or agreed to in writing, software 13* distributed under the License is distributed on an "AS IS" BASIS, 14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15* See the License for the specific language governing permissions and 16* limitations under the License. 17* 18* Description: efuse too scripts. 19''' 20 21import csv 22import struct 23import hashlib 24import os 25from sys import version_info 26 27def str_to_hex(s): 28 return ' '.join([hex(ord(c)).replace('0x', '') for c in s]) 29 30def print_bytes(bytes): 31 if version_info.major == 3: 32 c = bytes.hex() 33 print(c) 34 else: 35 c = bytes.encode('hex') 36 print(c) 37 38number = 0 39value_len = 0 40buf = b'' 41csv_dir = os.path.split(os.path.realpath(__file__))[0] 42csv_path = os.path.join(csv_dir, 'efuse.csv') 43#print('csv_path:', csv_path) 44bin_path = os.path.join(csv_dir, 'efuse_cfg.bin') 45#print('bin_path:', bin_path) 46 47# 用reader读取csv文件 48with open(csv_path, 'r') as csvFile: 49 reader = csv.reader(csvFile) 50 for line in reader: 51 if(line[0] == "1"): 52 size = int(line[3]) 53 if (size <= 32): 54 value_len = 4 55 elif (size <= 64): 56 value_len = 8 57 else: 58 value_len = size // 8 59 result = struct.pack('BBHHH', 0, 8, int(line[2]), size, value_len) 60 value_str = line[4] 61 value_list = value_str.split(" ") 62 value_struct = b'' 63 for i in range(value_len // 4): 64 value = int(value_list[i], 16) 65 value_struct = value_struct + struct.pack('I', value) 66 print_bytes(value_struct) 67 buf = buf + result + value_struct 68 number = number + 1 69header = struct.pack('BBHIII', 0, 48, number, len(buf) + 48, 0, 0) 70data = header + buf 71hash = hashlib.sha256(data).digest() 72bin_data = hash + data 73#print(bin_data) 74 75with open(bin_path, 'wb') as f: 76 f.write(bin_data) 77