我正在为ATXmega128A4U创建自己的引导加载程序。要使用引导加载程序,我想将固件的ELF文件转换为ATXmega中使用的内存映射。 为此,我使用python和modul" pyelftools"。它的文档很差,所以我遇到了一个问题:我不知道我可以使用哪些信息从部分的数据中获取地址,偏移等。 我的目标是创建一个bytearray,将数据/代码复制到其中并将其传输到bootlaoder。以下是我的代码:
import sys
# If pyelftools is not installed, the example can also run from the root or
# examples/ dir of the source distribution.
sys.path[0:0] = ['.', '..']
from elftools.common.py3compat import bytes2str
from elftools.elf.elffile import ELFFile
# 128k flash for the ATXmega128a4u
flashsize = 128 * 1024
def process_file(filename):
with open(filename, 'rb') as f:
# get the data
elffile = ELFFile(f)
dataSec = elffile.get_section_by_name(b'.data')
textSec = elffile.get_section_by_name(b'.text')
# prepare the memory
flashMemory = bytearray(flashsize)
# the data section
startAddr = dataSec.header.sh_offset
am = dataSec.header.sh_size
i = 0
while i < am:
val = dataSec.stream.read(1)
flashMemory[startAddr] = val[0]
startAddr += 1
i += 1
# the text section
startAddr = textSec.header.sh_offset
am = textSec.header.sh_size
i = 0
while i < am:
print(str(startAddr) + ' : ' + str(i))
val = textSec.stream.read(1)
flashMemory[startAddr] = val[0]
startAddr += 1
i += 1
print('finished')
if __name__ == '__main__':
process_file('firmware.elf')
希望有人能告诉我如何解决这个问题。
答案 0 :(得分:0)
我为解决这个问题而设法。 不要通过“textSec.stream.read”使用“textSec.data()”从流中手动读取数据。 Internaly(参见“sections.py”)完成文件中的查找操作。然后读取数据。结果将是有效的数据块。 以下代码读取atxmega固件的代码(文本)部分,并将其复制到bytearray中,该bytearray具有atxmega128a4u设备的闪存布局。 @vlas_tepesch:不需要十六进制对话,避免了64k陷阱。
sys.path[0:0] = ['.', '..']
from elftools.common.py3compat import bytes2str
from elftools.elf.elffile import ELFFile
# 128k flash for the ATXmega128a4u
flashsize = 128 * 1024
def __printSectionInfo (s):
print ('[{nr}] {name} {type} {addr} {offs} {size}'.format(
nr = s.header['sh_name'],
name = s.name,
type = s.header['sh_type'],
addr = s.header['sh_addr'],
offs = s.header['sh_offset'],
size = s.header['sh_size']
)
)
def process_file(filename):
print('In file: ' + filename)
with open(filename, 'rb') as f:
# get the data
elffile = ELFFile(f)
print ('sections:')
for s in elffile.iter_sections():
__printSectionInfo(s)
print ('get the code from the .text section')
textSec = elffile.get_section_by_name(b'.text')
# prepare the memory
flashMemory = bytearray(flashsize)
# the text section
startAddr = textSec.header['sh_addr']
val = textSec.data()
flashMemory[startAddr:startAddr+len(val)] = val
# print memory
print('finished')
if __name__ == '__main__':
process_file('firmware.elf')
坦克的评论!