一、前言
本文介绍如何使用python来实现mcu的flash镜像的制作,包括如何将boot和app连接起来,以及进行0xff填充到指定的大小,同时学习了python的文件操作知识。
二、用到的python基础知识
- 2.1、命令行参数传递
通过sys.argv
得到命令行传递过来的参数:import sys # 参数个数判读 if len(sys.argv) != 3: print("param err!!") exit(-1) # 获取参数(字符串),并打印 file1 = sys.argv[1] file2 = sys.argv[2] print("file1 : " + file1 ) print("file2 : " + file2 ) exit(0)
- 2.2、获取文件大小
通过os.path.getsize
获取文件大小:import sys file1 = "test.bin" file1_size = os.path.getsize(file1) print("file1 size: " + str(file1_size ))
- 2.3、删除文件
通过os.remove
删除文件:import sys if os.path.exists(file1): os.remove(file1) print("delete " + file1)
- 2.4、生成全0xff的bin文件
使用file操作实现,生成大小为128K全是0xff的bin文件:import os import sys import shutil import struct file1 = "128K_0xff.bin" with open(file1,'wb+') as f1: for i in range(128 * 1024): s = struct.pack('B',0xff) f1.write(s) f1.close()
- 2.5、将两个文件拼接起来
将file2.bin添加到file1.bin之后,生成新文件file3.bin:import os import sys import shutil import struct file1 = "file1.bin" file2 = "file2.bin" file3 = "file3.bin" with open(file1, 'rb') as f1, open(file2, 'rb') as f2, open(file3, 'wb') as f3: shutil.copyfileobj(f1, f3) shutil.copyfileobj(f2, f3) f1.close() f2.close() f3.close() os.remove(file1) os.remove(file2) exit(0)
- 2.6、在bin文件的指定位置写入想要的值
将0x55写入文件的当前位置:import os import sys import shutil import struct file5 = "test.file" value = 0x55 with open(file5, 'ab+') as f5: s= struct.pack('I',value) f5.write(s) f5.close(); exit(0)
- 2.7、文件重命名
将123.bin重命名为456.binimport os src_file = "123.bin" dst_file = "456.bin" os.rename(src_file , dst_file) exit(0)
三、示例模板
#coding=utf-8
import os
import sys
import shutil
import struct
# 参数判断
if len(sys.argv) != 3:
print("param err!!")
print("usage: python flash_mkimage.py boot2.bin boot3.bin2\n")
exit(-1)
print("\nbin info:")
# 获取文件名
boot2_file = sys.argv[1]
boot3_file = sys.argv[2]
print("boot2: " + boot2_file)
print("boot3: " + boot3_file)
boot2_size = os.path.getsize(boot2_file)
print("boot2 size: " + str(boot2_size))
boot3_size = os.path.getsize(boot3_file)
print("boot3 size: " + str(boot3_size))
print("\nmkimage [boot2 + boot3 + padding] start ...")
# 删除已有文件
target = "boot2_boot3_flash.bin"
if os.path.exists(target):
os.remove(target)
print("delete " + target)
# 将boot2填充0xff为128K
file1 = "128K_0xff.bin"
with open(file1,'wb+') as f1:
for i in range(128 * 1024 - boot2_size):
s = struct.pack('B',0xff)
f1.write(s)
f1.close()
file2 = "boot2_0xff.bin"
with open(boot2_file, 'rb') as f, open(file1, 'rb') as f1, open(file2, 'wb') as f2:
shutil.copyfileobj(f, f2)
shutil.copyfileobj(f1, f2)
f.close()
f1.close()
f2.close()
os.remove(file1)
# 拼接boot3
file3 = "boot2_boot3.bin"
with open(boot3_file, 'rb') as f, open(file2, 'rb') as f2, open(file3, 'wb') as f3:
shutil.copyfileobj(f2, f3)
shutil.copyfileobj(f, f3)
f.close()
f2.close()
f3.close()
os.remove(file2)
# 将file3填充0xff为 0xfff000 字节
file3_size = os.path.getsize(file3)
file4 = "16M_0xff.bin"
with open(file4,'wb+') as f4:
i = 0
for i in range(0xfff000 - file3_size):
s = struct.pack('B',0xff)
f4.write(s)
f4.close()
file5 = "boot2_boot3_16M.bin"
with open(file3, 'rb') as f3, open(file4, 'rb') as f4, open(file5, 'wb') as f5:
shutil.copyfileobj(f3, f5)
shutil.copyfileobj(f4, f5)
f3.close()
f4.close()
f5.close()
os.remove(file3)
os.remove(file4)
# 写入boot2文件大小
with open(file5, 'ab+') as f5:
binsize = struct.pack('I',boot2_size)
f5.write(binsize)
f5.close();
# padding to 16M
pad_size = 16 * 1024 * 1024 - os.path.getsize(file5)
print("pad_size: " + str(pad_size))
with open(file5,'ab+') as f5:
i = 0
for i in range(pad_size):
s = struct.pack('B',0xff)
f5.write(s)
print("file size: " + str(f5.tell()))
f5.close()
# 重命名
os.rename(file5, target)
print("\nmkimage success, output: " + target)
exit(0)