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 sys 19import struct 20sys.path.append(os.path.dirname(__file__)) 21from conf_parser import BuildConfParser, ParserError 22from utils.build_utils import fn_str_to_int 23 24## 25## Params Area Stucture 26## 27## Image_ID: 4B 28## Struct_Version: 4B 29## Version: 4B 30## Item_Offset: 2B 31## Item_Count: 1B 32## ItemID_List: Item_Count 33## Padding_Area: Item_Offset - 15 - Item_Count 34## Item_List: (4B + 4B) * Item_Count 35## 36def param_area(dst_file, image_id, stru_ver, version, param_info) -> None: 37 38 item_num = len(param_info) 39 item_offset = 15 + item_num 40 item_offset = int((item_offset + 3) / 4) * 4 41 42 ## image_id, stru_ver, version, item_offset, item_num 43 bin_size = 1024 44 45 binary = struct.pack("<IIIHB", image_id, stru_ver, version, item_offset, item_num) 46 47 ## item id list 48 for item in param_info: 49 binary += struct.pack("<B", item[0]) 50 51 ## pad for 4-byte-align 52 for i in range(0, item_offset - 15 - item_num): 53 binary += struct.pack("<B", 0x00) 54 55 ## item list 56 for item in param_info: 57 binary += struct.pack("<II", item[1], item[2]) 58 59 binary += bytearray(bin_size - len(binary)) 60 # print("len", len(binary), binary) 61 62 ## write to file 63 with open(dst_file, "wb") as f: 64 f.write(binary) 65 66 print("Build params success!") 67 68def gen_flash_part_bin(param_file, dst_file): 69 partition_cfg = BuildConfParser(param_file).get_conf_data().get("Partition_Tbl") 70 if partition_cfg is None: 71 msg = "[error] flash partiton config NULL:[%s]!" % param_file 72 raise ParserError(msg) 73 image_id = partition_cfg.get("image_id") 74 stru_ver = partition_cfg.get("stru_ver") 75 version = partition_cfg.get("version") 76 param_info = partition_cfg.get("param_info") 77 if image_id is None or stru_ver is None or version is None or param_info is None: 78 msg = "[error] flash partiton config err:[%s]!" % param_file 79 raise ParserError(msg) 80 81 table = [] 82 for param in param_info: 83 if len(param) != 3: 84 msg = "[error] flash partiton table err:[%s]!" % param_file 85 raise ParserError(msg) 86 table.append([fn_str_to_int(x) for x in param]) 87 param_area(dst_file, fn_str_to_int(image_id), fn_str_to_int(stru_ver), fn_str_to_int(version), table) 88 89## python3 param_packet.py XXX.json YYY.bin 90def test(): 91 param_file = sys.argv[1] 92 dst_file = sys.argv[2] 93 gen_flash_part_bin(param_file, dst_file) 94 95if __name__ == '__main__': 96 test() 97