1# Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14import json 15from xml.dom.minidom import Document 16import re 17import os 18 19 20NV_JSON_FILE_DIR = ["./output/ws63/acore/nv_bin/temp/cfg/acore_nv.json"] 21NV_TXT_FILE_DIR = ["./output/ws63/acore/nv_bin/temp/acore.etypes"] 22 23NV_XML_PATH = "./output/ws63/database/cco/system/hdbcfg/mss_nvi_db.xml" 24TXT_PATH = "./output/ws63/database/cco/system/nv/nv_struct_def.txt" 25 26 27def nv_json_handle(): 28 file_dic = {} 29 for file_path in NV_JSON_FILE_DIR: 30 if not os.path.isfile(file_path): 31 print("[!][nv handle]warning: ", file_path, " not found!") 32 break 33 nv_dic = {} 34 with open(file_path, "r") as f: 35 nv_api = json.load(f) 36 for i in nv_api["common"].keys(): 37 if nv_api["common"][i] != 0: 38 nv_dic[i] = nv_api["common"][i] 39 file_dic[file_path] = nv_dic 40 return file_dic 41 42 43def dic_to_nv(nv_dic: dict): 44 if not nv_dic: 45 print("[!][nv handle]warning: dic is Null.") 46 return 47 nv_data = Document() 48 root_node = nv_data.createElement("DebugKits") 49 nv_data.appendChild(root_node) 50 group_num = 0 51 for file_name in nv_dic.keys(): 52 print("NV file reading: ", file_name) 53 group_num += 1 54 group_id = hex(group_num) 55 56 file_cont = nv_dic[file_name] 57 58 # first node, GROUP attribute 59 first_son_node = nv_data.createElement("GROUP") 60 root_node.appendChild(first_son_node) 61 first_son_node.setAttribute("ID", group_id) 62 first_son_node.setAttribute("NAME", "NV") 63 first_son_node.setAttribute("PARAM_DEF_FILE", os.path.relpath(TXT_PATH, os.path.dirname(NV_XML_PATH))) 64 for nv_name in file_cont.keys(): 65 son_node = nv_data.createElement("NV") 66 first_son_node.appendChild(son_node) 67 son_node.setAttribute("CATEGORY", "FTM") 68 son_node.setAttribute("DESCRIPTION", "FTM") 69 son_node.setAttribute("DEV", "CCO") 70 son_node.setAttribute("ID", file_cont[nv_name]["key_id"]) 71 son_node.setAttribute("NAME", nv_name) 72 son_node.setAttribute("PARAM_NAME", file_cont[nv_name]["structure_type"]) 73 print("nv xml writing in: ", NV_XML_PATH) 74 try: 75 f = open(NV_XML_PATH, "w", encoding="utf-8") 76 f.write(nv_data.toprettyxml()) 77 f.close() 78 except FileNotFoundError: 79 print("[!][nv handle]warning: ", NV_XML_PATH, " not found!") 80 pass 81 except: 82 print("[!][nv handle]warning: NV error!") 83 pass 84 85def struct_to_txt(): 86 struct_list = [] 87 for file_path in NV_TXT_FILE_DIR: 88 if not os.path.isfile(file_path): 89 print("[!][nv handle]warning: ", file_path, " not found!") 90 break 91 print("struct file reading: ", file_path) 92 with open(file_path, "r", encoding="utf-8") as f: 93 data = f.readlines() 94 ret = re.findall("(typedef.*?;)", "".join(data)) 95 ret2 = re.findall("(typedef struct {.*?}.*?;)", "".join(data), re.S) 96 struct_list.extend(ret) 97 struct_list.extend(ret2) 98 print("struct writing in:", TXT_PATH) 99 try: 100 with open(TXT_PATH, "w", encoding="utf-8") as t_file: 101 t_file.write('''#include "base_datatype_def.txt"\n''') 102 for i in struct_list: 103 t_file.write(i + "\n") 104 except FileNotFoundError: 105 print("[!][nv handle]warning: ", TXT_PATH, " not found!") 106 pass 107 except: 108 print("[!][nv handle]warning: NV error!") 109 pass 110 return 111 112 113if __name__ == '__main__': 114 work_dir = os.getcwd() 115 print("current work direction: ", work_dir) 116 dic = nv_json_handle() 117 if dic: 118 dic_to_nv(dic) 119 struct_to_txt() 120 os.chdir(work_dir) 121