1#!/usr/bin/env python3 2# encoding=utf-8 3 4# Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import os 18import json 19import sys 20import platform 21import subprocess 22import shutil 23import re 24 25from utils.build_utils import root_path, exec_shell, compare_bin 26 27from typing import Dict, Any 28 29 30SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) 31 32def do_cmd(target_name: str, hook_name: str, env: Dict[str, Any])->bool: 33 python_path = sys.executable 34 print("python path: ", python_path) 35 36 if not os.path.isfile(os.path.join(root_path, "output", "ws63", "acore", "boot_bin", "flashboot.bin")) and target_name != "ws63-flashboot" and hook_name == 'build_pre': 37 print("flashboot start build .....") 38 errcode = exec_shell([python_path, 'build.py', 'ws63-flashboot'], None, True) 39 return True 40 41 if target_name == 'ws63-liteos-mfg' and hook_name == 'build_post': 42 errcode = exec_shell([python_path, "build.py", "ws63-liteos-app"], None, True) 43 return True 44 45 if hook_name != 'build_post': 46 return True 47 48 if env.get('gen_mem_bin'): 49 script_path = os.path.join(SCRIPT_DIR, 'get_mem_bin.sh') 50 print("gen_mem_bin ing...") 51 errcode = exec_shell(['bash', script_path, root_path, target_name, env.get('bin_name')], None, True) 52 if errcode != 0: 53 print("gen_mem_bin failed!") 54 return False 55 print("gen_mem_bin done!") 56 57 if env.get('generate_efuse_bin'): 58 copy_py = os.path.join(SCRIPT_DIR, 'efuse_cfg_gen.py') 59 print("generate_efuse_bin ing...") 60 errcode = exec_shell([sys.executable, copy_py], None, True) 61 if errcode != 0: 62 print("generate_efuse_bin failed!") 63 return False 64 shutil.copy(os.path.join(root_path, 'output/ws63/acore/ws63-liteos-app/efuse_cfg.bin'), os.path.join(root_path, 'output/ws63/acore/boot_bin')) 65 print("generate_efuse_bin done!") 66 67 if env.get('copy_files_to_interim'): 68 # copy_files_to_interim 69 copy_py = os.path.join(SCRIPT_DIR, 'copy_files_to_interim.py') 70 print("copy_files_to_interim ing...") 71 errcode = exec_shell([sys.executable, copy_py, root_path], None, True) 72 if errcode != 0: 73 print("copy_files_to_interim failed!") 74 return False 75 print("copy_files_to_interim done!") 76 77 if env.get('pke_rom_bin'): 78 # gen pke_rom_bin 79 gen_pke_rom_bin_sh = os.path.join(SCRIPT_DIR, 'pke_rom.sh') 80 if os.path.exists(os.path.join(root_path, \ 81 'drivers/chips/ws63/rom/rom_boot/drivers/drivers/hal/security_unified/hal_cipher/pke/rom_lib.c')): 82 print("generate pke_rom_bin ing...") 83 errcode = exec_shell(['sh', gen_pke_rom_bin_sh, root_path], None, True) 84 if errcode != 0: 85 print("generate pke_rom_bin failed!") 86 return False 87 print("generate pke_rom_bin done!") 88 89 # verify pke rom bin 90 if env.get('fixed_pke'): 91 bin1 = os.path.join(root_path, "output", env.get('chip'), env.get('core'), 'pke_rom', 'pke_rom.bin') 92 bin2 = env.get('fixed_pke_path', '').replace('<root>', root_path) 93 if not compare_bin(bin1, bin2): 94 print(f"Verify pke rom bin ERROR! :{bin1} is not same with {bin2}") 95 return False 96 97 if env.get('rom_in_one'): 98 # gen rom_in_one 99 if "windows" in platform.platform().lower(): 100 rom_in_one = os.path.join(SCRIPT_DIR, 'rom_in_one.py') 101 print("generate rom_in_one ing...") 102 errcode = exec_shell([python_path, rom_in_one, root_path, env.get('bin_name')], None, True) 103 else: 104 rom_in_one = os.path.join(SCRIPT_DIR, 'rom_in_one.sh') 105 print("generate rom_in_one ing...") 106 errcode = exec_shell(['sh', rom_in_one, root_path, env.get('bin_name')], None, True) 107 108 if errcode != 0: 109 print("generate rom_in_one failed!") 110 return False 111 print("generate rom_in_one done!") 112 113 # verify codepoint bin 114 bin1 = os.path.join(root_path, "output", env.get('chip'), env.get('core'), \ 115 target_name, env.get('bin_name')+'_rompack.bin') 116 if env.get('fixed_rom_in_one') and os.path.isfile(bin1):# only rompack bin exists 117 bin2 = env.get('fixed_rom_in_one_path', '').replace('<root>', root_path) 118 if not compare_bin(bin1, bin2): 119 print(f"Verify rom_in_one bin ERROR! :{bin1} is not same with {bin2}") 120 return False 121 122 if env.get('fixed_bin_name'): 123 bin1 = os.path.join(root_path, "output", env.get('chip'), env.get('core'), \ 124 target_name, env.get('fixed_bin_name')) 125 bin2 = env.get('fixed_bin_path', '').replace('<root>', root_path) 126 if not compare_bin(bin1, bin2): 127 print(f"Verify bin ERROR! :{bin1} is not same with {bin2}") 128 return False 129 nv_handle = os.path.join(SCRIPT_DIR, 'nv_handle.py') 130 exec_shell([python_path, nv_handle], None, True) 131 return True 132 133 134def config_check(root_path, target_name): 135 config_file_path = os.path.join(root_path, "build", "config", "target_config", "ws63", "config.py") 136 with open(config_file_path, "r", encoding="utf-8") as f: 137 for i in f: 138 if target_name in i: 139 print(target_name, " in config.py.") 140 return True 141 return False