python实现flash镜像bin文件的制作

一、前言

本文介绍如何使用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.bin
    import 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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Pythonbinascii模块来实现hex文件bin文件的功能。具体的实现方法可以参考以下代码: ```python import binascii with open('input.hex', 'r') as f: hex_data = f.read() bin_data = binascii.unhexlify(hex_data) with open('output.bin', 'wb') as f: f.write(bin_data) ``` 其中,'input.hex'是需要转换的hex文件名,'output.bin'是转换后的bin文件名。使用with语句可以自动关闭文件,避免文件句柄泄漏的问题。 ### 回答2: Python可以使用以下代码将hex文件转换为bin文件: ```python def hex_to_bin(hex_file, bin_file): with open(hex_file, 'r') as f_hex, open(bin_file, 'wb') as f_bin: for line in f_hex: line = line.strip() if line and line[0] == ':': data = bytes.fromhex(line[1:]) f_bin.write(data) # 示例调用 hex_to_bin('example.hex', 'example.bin') ``` 这段代码定义了一个`hex_to_bin`函数,该函数接受两个参数:`hex_file`代表输入的hex文件路径,`bin_file`代表输出的bin文件路径。代码使用了Python文件操作和字节操作方法。 函数首先打开hex文件bin文件,然后通过读取hex文件的每一行,判断是否为数据行(以冒号开头)。如果是数据行,则从每一行获取数据部分,并使用`bytes.fromhex`方法将hex字符串转换为字节数据,最后将字节数据写入bin文件。 你可以将需要转换的hex文件路径和输出的bin文件路径作为示例调用函数的参数,这样就可以将hex文件转换为bin文件了。 ### 回答3: 要将hex文件转换为bin文件,可以使用Python中的binascii模块。下面是一个简单的示例代码: ```python import binascii def hex_to_bin(hex_file, bin_file): try: with open(hex_file, 'r') as f: hex_data = f.read().replace('\n', '') bin_data = binascii.unhexlify(hex_data) with open(bin_file, 'wb') as f: f.write(bin_data) print("转换成功!") except Exception as e: print("转换失败:", str(e)) # 以hex_file为输入,bin_file为输出,调用hex_to_bin函数 hex_to_bin("input.hex", "output.bin") ``` 此代码中,使用`with open`语句打开hex文件,并读取其中的内容。然后,使用`binascii.unhexlify`函数将hex文件中的十六进制数据转换为二进制数据。最后,将二进制数据写入指定的bin文件中。 要使用这个示例代码,只需将`hex_file`和`bin_file`参数替换为实际的文件路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值