1# Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. 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 os 15import argparse 16 17# src: src_file_path 18# dst: dst_file_path 19# memory_size: malloced memory size, unit: byte 20def append_file_data(src, dst, memory_size): 21 src_file_size = os.stat(src).st_size 22 23 if(memory_size < 0): 24 raise Exception("memory_size < 0") 25 26 if(memory_size < src_file_size): 27 raise Exception("memory_size is too small to hold src_file_data") 28 29 src_fd = open(src, 'rb') 30 dst_fd = open(dst, 'ab') 31 32 # append src_file_data to dst file 33 buf = src_fd.read(1024) 34 while buf: 35 dst_fd.write(buf) 36 buf = src_fd.read(1024) 37 38 # fill free space with 0xff 39 buf = bytes([0xff]) 40 for i in range(memory_size - src_file_size): 41 dst_fd.write(buf) 42 43 src_fd.close() 44 dst_fd.close() 45 46def cmd_parse(): 47 parser=argparse.ArgumentParser() 48 parser.add_argument('--bootloader_path',help='bootloader_path', required=True) 49 parser.add_argument('--kernel_path',help='kernel_path', required=True) 50 parser.add_argument('--file_path',help='file_path') 51 parser.add_argument('--image_path',help='image_path', required=True) 52 parser.add_argument('--bootloader_zone_size', help='bootloader_zone_size,Unit: KB',type=int,default=64) 53 parser.add_argument('--kernel_zone_size', help='kernel_zone_size,Unit: KB',type=int,default=1984) 54 parser.add_argument('--file_zone_size', help='file_zone_size,Unit: KB',type=int,default=1024) 55 #parser.print_help() 56 args = parser.parse_args() 57 return args 58 59args = cmd_parse() 60 61# ensure image_file existing and set image_file empty 62image_fd = open(args.image_path, 'wb') 63image_fd.close() 64 65#add file to image 66append_file_data(args.bootloader_path, args.image_path, args.bootloader_zone_size * 1024) 67append_file_data(args.kernel_path, args.image_path, args.kernel_zone_size * 1024) 68if(args.file_path): 69 append_file_data(args.file_path, args.image_path, args.file_zone_size * 1024) 70